51a0bfb89b3cc051be9ed0c49a5b5fc19fc25e4b
[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 (defcustom gnus-summary-save-parts-default-mime "image/.*"
940   "*A regexp to match MIME parts when saving multiple parts of a message
941 with gnus-summary-save-parts (X m). This regexp will be used by default
942 when prompting the user for which type of files to save."
943   :group 'gnus-summary
944   :type 'regexp)
945
946
947 ;;; Internal variables
948
949 (defvar gnus-article-mime-handles nil)
950 (defvar gnus-article-decoded-p nil)
951 (defvar gnus-scores-exclude-files nil)
952 (defvar gnus-page-broken nil)
953 (defvar gnus-inhibit-mime-unbuttonizing nil)
954
955 (defvar gnus-original-article nil)
956 (defvar gnus-article-internal-prepare-hook nil)
957 (defvar gnus-newsgroup-process-stack nil)
958
959 (defvar gnus-thread-indent-array nil)
960 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
961 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
962   "Function called to sort the articles within a thread after it has been gathered together.")
963
964 (defvar gnus-summary-save-parts-type-history nil)
965 (defvar gnus-summary-save-parts-last-directory nil)
966
967 ;; Avoid highlighting in kill files.
968 (defvar gnus-summary-inhibit-highlight nil)
969 (defvar gnus-newsgroup-selected-overlay nil)
970 (defvar gnus-inhibit-limiting nil)
971 (defvar gnus-newsgroup-adaptive-score-file nil)
972 (defvar gnus-current-score-file nil)
973 (defvar gnus-current-move-group nil)
974 (defvar gnus-current-copy-group nil)
975 (defvar gnus-current-crosspost-group nil)
976
977 (defvar gnus-newsgroup-dependencies nil)
978 (defvar gnus-newsgroup-adaptive nil)
979 (defvar gnus-summary-display-article-function nil)
980 (defvar gnus-summary-highlight-line-function nil
981   "Function called after highlighting a summary line.")
982
983 (defvar gnus-summary-line-format-alist
984   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
985     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
986     (?s gnus-tmp-subject-or-nil ?s)
987     (?n gnus-tmp-name ?s)
988     (?A (std11-address-string
989          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
990     (?a (or (std11-full-name-string
991              (car (mime-entity-read-field gnus-tmp-header 'From)))
992             gnus-tmp-from) ?s)
993     (?F gnus-tmp-from ?s)
994     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
995     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
996     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
997     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
998     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
999     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1000     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1001     (?L gnus-tmp-lines ?d)
1002     (?I gnus-tmp-indentation ?s)
1003     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1004     (?R gnus-tmp-replied ?c)
1005     (?\[ gnus-tmp-opening-bracket ?c)
1006     (?\] gnus-tmp-closing-bracket ?c)
1007     (?\> (make-string gnus-tmp-level ? ) ?s)
1008     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1009     (?i gnus-tmp-score ?d)
1010     (?z gnus-tmp-score-char ?c)
1011     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1012     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1013     (?U gnus-tmp-unread ?c)
1014     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1015     (?t (gnus-summary-number-of-articles-in-thread
1016          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1017         ?d)
1018     (?e (gnus-summary-number-of-articles-in-thread
1019          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1020         ?c)
1021     (?u gnus-tmp-user-defined ?s)
1022     (?P (gnus-pick-line-number) ?d))
1023   "An alist of format specifications that can appear in summary lines.
1024 These are paired with what variables they correspond with, along with
1025 the type of the variable (string, integer, character, etc).")
1026
1027 (defvar gnus-summary-dummy-line-format-alist
1028   `((?S gnus-tmp-subject ?s)
1029     (?N gnus-tmp-number ?d)
1030     (?u gnus-tmp-user-defined ?s)))
1031
1032 (defvar gnus-summary-mode-line-format-alist
1033   `((?G gnus-tmp-group-name ?s)
1034     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1035     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1036     (?A gnus-tmp-article-number ?d)
1037     (?Z gnus-tmp-unread-and-unselected ?s)
1038     (?V gnus-version ?s)
1039     (?U gnus-tmp-unread-and-unticked ?d)
1040     (?S gnus-tmp-subject ?s)
1041     (?e gnus-tmp-unselected ?d)
1042     (?u gnus-tmp-user-defined ?s)
1043     (?d (length gnus-newsgroup-dormant) ?d)
1044     (?t (length gnus-newsgroup-marked) ?d)
1045     (?r (length gnus-newsgroup-reads) ?d)
1046     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1047     (?E gnus-newsgroup-expunged-tally ?d)
1048     (?s (gnus-current-score-file-nondirectory) ?s)))
1049
1050 (defvar gnus-last-search-regexp nil
1051   "Default regexp for article search command.")
1052
1053 (defvar gnus-summary-search-article-matched-data nil
1054   "Last matched data of article search command.  It is the local variable
1055 in `gnus-article-buffer' which consists of the list of start position,
1056 end position and text.")
1057
1058 (defvar gnus-last-shell-command nil
1059   "Default shell command on article.")
1060
1061 (defvar gnus-newsgroup-begin nil)
1062 (defvar gnus-newsgroup-end nil)
1063 (defvar gnus-newsgroup-last-rmail nil)
1064 (defvar gnus-newsgroup-last-mail nil)
1065 (defvar gnus-newsgroup-last-folder nil)
1066 (defvar gnus-newsgroup-last-file nil)
1067 (defvar gnus-newsgroup-auto-expire nil)
1068 (defvar gnus-newsgroup-active nil)
1069
1070 (defvar gnus-newsgroup-data nil)
1071 (defvar gnus-newsgroup-data-reverse nil)
1072 (defvar gnus-newsgroup-limit nil)
1073 (defvar gnus-newsgroup-limits nil)
1074
1075 (defvar gnus-newsgroup-unreads nil
1076   "List of unread articles in the current newsgroup.")
1077
1078 (defvar gnus-newsgroup-unselected nil
1079   "List of unselected unread articles in the current newsgroup.")
1080
1081 (defvar gnus-newsgroup-reads nil
1082   "Alist of read articles and article marks in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-expunged-tally nil)
1085
1086 (defvar gnus-newsgroup-marked nil
1087   "List of ticked articles in the current newsgroup (a subset of unread art).")
1088
1089 (defvar gnus-newsgroup-killed nil
1090   "List of ranges of articles that have been through the scoring process.")
1091
1092 (defvar gnus-newsgroup-cached nil
1093   "List of articles that come from the article cache.")
1094
1095 (defvar gnus-newsgroup-saved nil
1096   "List of articles that have been saved.")
1097
1098 (defvar gnus-newsgroup-kill-headers nil)
1099
1100 (defvar gnus-newsgroup-replied nil
1101   "List of articles that have been replied to in the current newsgroup.")
1102
1103 (defvar gnus-newsgroup-expirable nil
1104   "List of articles in the current newsgroup that can be expired.")
1105
1106 (defvar gnus-newsgroup-processable nil
1107   "List of articles in the current newsgroup that can be processed.")
1108
1109 (defvar gnus-newsgroup-downloadable nil
1110   "List of articles in the current newsgroup that can be processed.")
1111
1112 (defvar gnus-newsgroup-undownloaded nil
1113   "List of articles in the current newsgroup that haven't been downloaded..")
1114
1115 (defvar gnus-newsgroup-unsendable nil
1116   "List of articles in the current newsgroup that won't be sent.")
1117
1118 (defvar gnus-newsgroup-bookmarks nil
1119   "List of articles in the current newsgroup that have bookmarks.")
1120
1121 (defvar gnus-newsgroup-dormant nil
1122   "List of dormant articles in the current newsgroup.")
1123
1124 (defvar gnus-newsgroup-scored nil
1125   "List of scored articles in the current newsgroup.")
1126
1127 (defvar gnus-newsgroup-incorporated nil
1128   "List of incorporated articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-headers nil
1131   "List of article headers in the current newsgroup.")
1132
1133 (defvar gnus-newsgroup-threads nil)
1134
1135 (defvar gnus-newsgroup-prepared nil
1136   "Whether the current group has been prepared properly.")
1137
1138 (defvar gnus-newsgroup-ancient nil
1139   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1140
1141 (defvar gnus-newsgroup-sparse nil)
1142
1143 (defvar gnus-current-article nil)
1144 (defvar gnus-article-current nil)
1145 (defvar gnus-current-headers nil)
1146 (defvar gnus-have-all-headers nil)
1147 (defvar gnus-last-article nil)
1148 (defvar gnus-newsgroup-history nil)
1149 (defvar gnus-newsgroup-charset nil)
1150 (defvar gnus-newsgroup-ephemeral-charset nil)
1151 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1152
1153 (defconst gnus-summary-local-variables
1154   '(gnus-newsgroup-name
1155     gnus-newsgroup-begin gnus-newsgroup-end
1156     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1157     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1158     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1159     gnus-newsgroup-unselected gnus-newsgroup-marked
1160     gnus-newsgroup-reads gnus-newsgroup-saved
1161     gnus-newsgroup-replied gnus-newsgroup-expirable
1162     gnus-newsgroup-processable gnus-newsgroup-killed
1163     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1164     gnus-newsgroup-unsendable
1165     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1166     gnus-newsgroup-headers gnus-newsgroup-threads
1167     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1168     gnus-current-article gnus-current-headers gnus-have-all-headers
1169     gnus-last-article gnus-article-internal-prepare-hook
1170     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1171     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1172     gnus-thread-expunge-below
1173     gnus-score-alist gnus-current-score-file
1174     (gnus-summary-expunge-below . global)
1175     (gnus-summary-mark-below . global)
1176     (gnus-orphan-score . global)
1177     gnus-newsgroup-active gnus-scores-exclude-files
1178     gnus-newsgroup-history gnus-newsgroup-ancient
1179     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1180     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1181     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1182     (gnus-newsgroup-expunged-tally . 0)
1183     gnus-cache-removable-articles gnus-newsgroup-cached
1184     gnus-newsgroup-data gnus-newsgroup-data-reverse
1185     gnus-newsgroup-limit gnus-newsgroup-limits
1186     gnus-newsgroup-charset
1187     gnus-newsgroup-incorporated)
1188   "Variables that are buffer-local to the summary buffers.")
1189
1190 ;; Byte-compiler warning.
1191 (defvar gnus-article-mode-map)
1192
1193 ;; Subject simplification.
1194
1195 (defun gnus-simplify-whitespace (str)
1196   "Remove excessive whitespace from STR."
1197   (let ((mystr str))
1198     ;; Multiple spaces.
1199     (while (string-match "[ \t][ \t]+" mystr)
1200       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1201                           " "
1202                           (substring mystr (match-end 0)))))
1203     ;; Leading spaces.
1204     (when (string-match "^[ \t]+" mystr)
1205       (setq mystr (substring mystr (match-end 0))))
1206     ;; Trailing spaces.
1207     (when (string-match "[ \t]+$" mystr)
1208       (setq mystr (substring mystr 0 (match-beginning 0))))
1209     mystr))
1210
1211 (defsubst gnus-simplify-subject-re (subject)
1212   "Remove \"Re:\" from subject lines."
1213   (if (string-match "^[Rr][Ee]: *" subject)
1214       (substring subject (match-end 0))
1215     subject))
1216
1217 (defun gnus-simplify-subject (subject &optional re-only)
1218   "Remove `Re:' and words in parentheses.
1219 If RE-ONLY is non-nil, strip leading `Re:'s only."
1220   (let ((case-fold-search t))           ;Ignore case.
1221     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1222     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1223       (setq subject (substring subject (match-end 0))))
1224     ;; Remove uninteresting prefixes.
1225     (when (and (not re-only)
1226                gnus-simplify-ignored-prefixes
1227                (string-match gnus-simplify-ignored-prefixes subject))
1228       (setq subject (substring subject (match-end 0))))
1229     ;; Remove words in parentheses from end.
1230     (unless re-only
1231       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1232         (setq subject (substring subject 0 (match-beginning 0)))))
1233     ;; Return subject string.
1234     subject))
1235
1236 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1237 ;; all whitespace.
1238 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1239   (goto-char (point-min))
1240   (while (re-search-forward regexp nil t)
1241     (replace-match (or newtext ""))))
1242
1243 (defun gnus-simplify-buffer-fuzzy ()
1244   "Simplify string in the buffer fuzzily.
1245 The string in the accessible portion of the current buffer is simplified.
1246 It is assumed to be a single-line subject.
1247 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1248 matter is removed.  Additional things can be deleted by setting
1249 `gnus-simplify-subject-fuzzy-regexp'."
1250   (let ((case-fold-search t)
1251         (modified-tick))
1252     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1253
1254     (while (not (eq modified-tick (buffer-modified-tick)))
1255       (setq modified-tick (buffer-modified-tick))
1256       (cond
1257        ((listp gnus-simplify-subject-fuzzy-regexp)
1258         (mapcar 'gnus-simplify-buffer-fuzzy-step
1259                 gnus-simplify-subject-fuzzy-regexp))
1260        (gnus-simplify-subject-fuzzy-regexp
1261         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1262       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1263       (gnus-simplify-buffer-fuzzy-step
1264        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1265       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1266
1267     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1268     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1269     (gnus-simplify-buffer-fuzzy-step " $")
1270     (gnus-simplify-buffer-fuzzy-step "^ +")))
1271
1272 (defun gnus-simplify-subject-fuzzy (subject)
1273   "Simplify a subject string fuzzily.
1274 See `gnus-simplify-buffer-fuzzy' for details."
1275   (save-excursion
1276     (gnus-set-work-buffer)
1277     (let ((case-fold-search t))
1278       ;; Remove uninteresting prefixes.
1279       (when (and gnus-simplify-ignored-prefixes
1280                  (string-match gnus-simplify-ignored-prefixes subject))
1281         (setq subject (substring subject (match-end 0))))
1282       (insert subject)
1283       (inline (gnus-simplify-buffer-fuzzy))
1284       (buffer-string))))
1285
1286 (defsubst gnus-simplify-subject-fully (subject)
1287   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1288   (cond
1289    (gnus-simplify-subject-functions
1290     (gnus-map-function gnus-simplify-subject-functions subject))
1291    ((null gnus-summary-gather-subject-limit)
1292     (gnus-simplify-subject-re subject))
1293    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1294     (gnus-simplify-subject-fuzzy subject))
1295    ((numberp gnus-summary-gather-subject-limit)
1296     (gnus-limit-string (gnus-simplify-subject-re subject)
1297                        gnus-summary-gather-subject-limit))
1298    (t
1299     subject)))
1300
1301 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1302   "Check whether two subjects are equal.
1303 If optional argument simple-first is t, first argument is already
1304 simplified."
1305   (cond
1306    ((null simple-first)
1307     (equal (gnus-simplify-subject-fully s1)
1308            (gnus-simplify-subject-fully s2)))
1309    (t
1310     (equal s1
1311            (gnus-simplify-subject-fully s2)))))
1312
1313 (defun gnus-summary-bubble-group ()
1314   "Increase the score of the current group.
1315 This is a handy function to add to `gnus-summary-exit-hook' to
1316 increase the score of each group you read."
1317   (gnus-group-add-score gnus-newsgroup-name))
1318
1319 \f
1320 ;;;
1321 ;;; Gnus summary mode
1322 ;;;
1323
1324 (put 'gnus-summary-mode 'mode-class 'special)
1325
1326 (when t
1327   ;; Non-orthogonal keys
1328
1329   (gnus-define-keys gnus-summary-mode-map
1330     " " gnus-summary-next-page
1331     "\177" gnus-summary-prev-page
1332     [delete] gnus-summary-prev-page
1333     [backspace] gnus-summary-prev-page
1334     "\r" gnus-summary-scroll-up
1335     "\M-\r" gnus-summary-scroll-down
1336     "n" gnus-summary-next-unread-article
1337     "p" gnus-summary-prev-unread-article
1338     "N" gnus-summary-next-article
1339     "P" gnus-summary-prev-article
1340     "\M-\C-n" gnus-summary-next-same-subject
1341     "\M-\C-p" gnus-summary-prev-same-subject
1342     "\M-n" gnus-summary-next-unread-subject
1343     "\M-p" gnus-summary-prev-unread-subject
1344     "." gnus-summary-first-unread-article
1345     "," gnus-summary-best-unread-article
1346     "\M-s" gnus-summary-search-article-forward
1347     "\M-r" gnus-summary-search-article-backward
1348     "<" gnus-summary-beginning-of-article
1349     ">" gnus-summary-end-of-article
1350     "j" gnus-summary-goto-article
1351     "^" gnus-summary-refer-parent-article
1352     "\M-^" gnus-summary-refer-article
1353     "u" gnus-summary-tick-article-forward
1354     "!" gnus-summary-tick-article-forward
1355     "U" gnus-summary-tick-article-backward
1356     "d" gnus-summary-mark-as-read-forward
1357     "D" gnus-summary-mark-as-read-backward
1358     "E" gnus-summary-mark-as-expirable
1359     "\M-u" gnus-summary-clear-mark-forward
1360     "\M-U" gnus-summary-clear-mark-backward
1361     "k" gnus-summary-kill-same-subject-and-select
1362     "\C-k" gnus-summary-kill-same-subject
1363     "\M-\C-k" gnus-summary-kill-thread
1364     "\M-\C-l" gnus-summary-lower-thread
1365     "e" gnus-summary-edit-article
1366     "#" gnus-summary-mark-as-processable
1367     "\M-#" gnus-summary-unmark-as-processable
1368     "\M-\C-t" gnus-summary-toggle-threads
1369     "\M-\C-s" gnus-summary-show-thread
1370     "\M-\C-h" gnus-summary-hide-thread
1371     "\M-\C-f" gnus-summary-next-thread
1372     "\M-\C-b" gnus-summary-prev-thread
1373     [(meta down)] gnus-summary-next-thread
1374     [(meta up)] gnus-summary-prev-thread
1375     "\M-\C-u" gnus-summary-up-thread
1376     "\M-\C-d" gnus-summary-down-thread
1377     "&" gnus-summary-execute-command
1378     "c" gnus-summary-catchup-and-exit
1379     "\C-w" gnus-summary-mark-region-as-read
1380     "\C-t" gnus-summary-toggle-truncation
1381     "?" gnus-summary-mark-as-dormant
1382     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1383     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1384     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1385     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1386     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1387     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1388     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1389     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1390     "=" gnus-summary-expand-window
1391     "\C-x\C-s" gnus-summary-reselect-current-group
1392     "\M-g" gnus-summary-rescan-group
1393     "w" gnus-summary-stop-page-breaking
1394     "\C-c\C-r" gnus-summary-caesar-message
1395     "\M-t" gnus-summary-toggle-mime
1396     "f" gnus-summary-followup
1397     "F" gnus-summary-followup-with-original
1398     "C" gnus-summary-cancel-article
1399     "r" gnus-summary-reply
1400     "R" gnus-summary-reply-with-original
1401     "\C-c\C-f" gnus-summary-mail-forward
1402     "o" gnus-summary-save-article
1403     "\C-o" gnus-summary-save-article-mail
1404     "|" gnus-summary-pipe-output
1405     "\M-k" gnus-summary-edit-local-kill
1406     "\M-K" gnus-summary-edit-global-kill
1407     ;; "V" gnus-version
1408     "\C-c\C-d" gnus-summary-describe-group
1409     "q" gnus-summary-exit
1410     "Q" gnus-summary-exit-no-update
1411     "\C-c\C-i" gnus-info-find-node
1412     gnus-mouse-2 gnus-mouse-pick-article
1413     "m" gnus-summary-mail-other-window
1414     "a" gnus-summary-post-news
1415     "x" gnus-summary-limit-to-unread
1416     "s" gnus-summary-isearch-article
1417     "t" gnus-article-toggle-headers
1418     "g" gnus-summary-show-article
1419     "l" gnus-summary-goto-last-article
1420     "v" gnus-summary-preview-mime-message
1421     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1422     "\C-d" gnus-summary-enter-digest-group
1423     "\M-\C-d" gnus-summary-read-document
1424     "\M-\C-e" gnus-summary-edit-parameters
1425     "\M-\C-a" gnus-summary-customize-parameters
1426     "\C-c\C-b" gnus-bug
1427     "*" gnus-cache-enter-article
1428     "\M-*" gnus-cache-remove-article
1429     "\M-&" gnus-summary-universal-argument
1430     "\C-l" gnus-recenter
1431     "I" gnus-summary-increase-score
1432     "L" gnus-summary-lower-score
1433     "\M-i" gnus-symbolic-argument
1434     "h" gnus-summary-select-article-buffer
1435
1436     "V" gnus-summary-score-map
1437     "X" gnus-uu-extract-map
1438     "S" gnus-summary-send-map)
1439
1440   ;; Sort of orthogonal keymap
1441   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1442     "t" gnus-summary-tick-article-forward
1443     "!" gnus-summary-tick-article-forward
1444     "d" gnus-summary-mark-as-read-forward
1445     "r" gnus-summary-mark-as-read-forward
1446     "c" gnus-summary-clear-mark-forward
1447     " " gnus-summary-clear-mark-forward
1448     "e" gnus-summary-mark-as-expirable
1449     "x" gnus-summary-mark-as-expirable
1450     "?" gnus-summary-mark-as-dormant
1451     "b" gnus-summary-set-bookmark
1452     "B" gnus-summary-remove-bookmark
1453     "#" gnus-summary-mark-as-processable
1454     "\M-#" gnus-summary-unmark-as-processable
1455     "S" gnus-summary-limit-include-expunged
1456     "C" gnus-summary-catchup
1457     "H" gnus-summary-catchup-to-here
1458     "\C-c" gnus-summary-catchup-all
1459     "k" gnus-summary-kill-same-subject-and-select
1460     "K" gnus-summary-kill-same-subject
1461     "P" gnus-uu-mark-map)
1462
1463   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1464     "c" gnus-summary-clear-above
1465     "u" gnus-summary-tick-above
1466     "m" gnus-summary-mark-above
1467     "k" gnus-summary-kill-below)
1468
1469   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1470     "/" gnus-summary-limit-to-subject
1471     "n" gnus-summary-limit-to-articles
1472     "w" gnus-summary-pop-limit
1473     "s" gnus-summary-limit-to-subject
1474     "a" gnus-summary-limit-to-author
1475     "u" gnus-summary-limit-to-unread
1476     "m" gnus-summary-limit-to-marks
1477     "M" gnus-summary-limit-exclude-marks
1478     "v" gnus-summary-limit-to-score
1479     "*" gnus-summary-limit-include-cached
1480     "D" gnus-summary-limit-include-dormant
1481     "T" gnus-summary-limit-include-thread
1482     "d" gnus-summary-limit-exclude-dormant
1483     "t" gnus-summary-limit-to-age
1484     "x" gnus-summary-limit-to-extra
1485     "E" gnus-summary-limit-include-expunged
1486     "c" gnus-summary-limit-exclude-childless-dormant
1487     "C" gnus-summary-limit-mark-excluded-as-read)
1488
1489   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1490     "n" gnus-summary-next-unread-article
1491     "p" gnus-summary-prev-unread-article
1492     "N" gnus-summary-next-article
1493     "P" gnus-summary-prev-article
1494     "\C-n" gnus-summary-next-same-subject
1495     "\C-p" gnus-summary-prev-same-subject
1496     "\M-n" gnus-summary-next-unread-subject
1497     "\M-p" gnus-summary-prev-unread-subject
1498     "f" gnus-summary-first-unread-article
1499     "b" gnus-summary-best-unread-article
1500     "j" gnus-summary-goto-article
1501     "g" gnus-summary-goto-subject
1502     "l" gnus-summary-goto-last-article
1503     "o" gnus-summary-pop-article)
1504
1505   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1506     "k" gnus-summary-kill-thread
1507     "l" gnus-summary-lower-thread
1508     "i" gnus-summary-raise-thread
1509     "T" gnus-summary-toggle-threads
1510     "t" gnus-summary-rethread-current
1511     "^" gnus-summary-reparent-thread
1512     "s" gnus-summary-show-thread
1513     "S" gnus-summary-show-all-threads
1514     "h" gnus-summary-hide-thread
1515     "H" gnus-summary-hide-all-threads
1516     "n" gnus-summary-next-thread
1517     "p" gnus-summary-prev-thread
1518     "u" gnus-summary-up-thread
1519     "o" gnus-summary-top-thread
1520     "d" gnus-summary-down-thread
1521     "#" gnus-uu-mark-thread
1522     "\M-#" gnus-uu-unmark-thread)
1523
1524   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1525     "g" gnus-summary-prepare
1526     "c" gnus-summary-insert-cached-articles)
1527
1528   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1529     "c" gnus-summary-catchup-and-exit
1530     "C" gnus-summary-catchup-all-and-exit
1531     "E" gnus-summary-exit-no-update
1532     "J" gnus-summary-jump-to-other-group
1533     "Q" gnus-summary-exit
1534     "Z" gnus-summary-exit
1535     "n" gnus-summary-catchup-and-goto-next-group
1536     "R" gnus-summary-reselect-current-group
1537     "G" gnus-summary-rescan-group
1538     "N" gnus-summary-next-group
1539     "s" gnus-summary-save-newsrc
1540     "P" gnus-summary-prev-group)
1541
1542   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1543     " " gnus-summary-next-page
1544     "n" gnus-summary-next-page
1545     "\177" gnus-summary-prev-page
1546     [delete] gnus-summary-prev-page
1547     "p" gnus-summary-prev-page
1548     "\r" gnus-summary-scroll-up
1549     "\M-\r" gnus-summary-scroll-down
1550     "<" gnus-summary-beginning-of-article
1551     ">" gnus-summary-end-of-article
1552     "b" gnus-summary-beginning-of-article
1553     "e" gnus-summary-end-of-article
1554     "^" gnus-summary-refer-parent-article
1555     "r" gnus-summary-refer-parent-article
1556     "D" gnus-summary-enter-digest-group
1557     "R" gnus-summary-refer-references
1558     "T" gnus-summary-refer-thread
1559     "g" gnus-summary-show-article
1560     "s" gnus-summary-isearch-article
1561     "P" gnus-summary-print-article
1562     "t" gnus-article-babel
1563     "d" gnus-summary-decrypt-article
1564     "v" gnus-summary-verify-article)
1565
1566   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1567     "b" gnus-article-add-buttons
1568     "B" gnus-article-add-buttons-to-head
1569     "o" gnus-article-treat-overstrike
1570     "e" gnus-article-emphasize
1571     "w" gnus-article-fill-cited-article
1572     "Q" gnus-article-fill-long-lines
1573     "C" gnus-article-capitalize-sentences
1574     "c" gnus-article-remove-cr
1575     "Z" gnus-article-decode-HZ
1576     "f" gnus-article-display-x-face
1577     "l" gnus-summary-stop-page-breaking
1578     "r" gnus-summary-caesar-message
1579     "t" gnus-article-toggle-headers
1580     "v" gnus-summary-verbose-headers
1581     "m" gnus-summary-toggle-mime
1582     "H" gnus-article-strip-headers-in-body
1583     "d" gnus-article-treat-dumbquotes
1584     "s" gnus-smiley-display)
1585
1586   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1587     "a" gnus-article-hide
1588     "h" gnus-article-toggle-headers
1589     "b" gnus-article-hide-boring-headers
1590     "s" gnus-article-hide-signature
1591     "c" gnus-article-hide-citation
1592     "C" gnus-article-hide-citation-in-followups
1593     "l" gnus-article-hide-list-identifiers
1594     "p" gnus-article-hide-pgp
1595     "B" gnus-article-strip-banner
1596     "P" gnus-article-hide-pem
1597     "\C-c" gnus-article-hide-citation-maybe)
1598
1599   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1600     "a" gnus-article-highlight
1601     "h" gnus-article-highlight-headers
1602     "c" gnus-article-highlight-citation
1603     "s" gnus-article-highlight-signature)
1604
1605   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1606     "z" gnus-article-date-ut
1607     "u" gnus-article-date-ut
1608     "l" gnus-article-date-local
1609     "e" gnus-article-date-lapsed
1610     "o" gnus-article-date-original
1611     "i" gnus-article-date-iso8601
1612     "s" gnus-article-date-user)
1613
1614   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1615     "t" gnus-article-remove-trailing-blank-lines
1616     "l" gnus-article-strip-leading-blank-lines
1617     "m" gnus-article-strip-multiple-blank-lines
1618     "a" gnus-article-strip-blank-lines
1619     "A" gnus-article-strip-all-blank-lines
1620     "s" gnus-article-strip-leading-space
1621     "e" gnus-article-strip-trailing-space)
1622
1623   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1624     "v" gnus-version
1625     "f" gnus-summary-fetch-faq
1626     "d" gnus-summary-describe-group
1627     "h" gnus-summary-describe-briefly
1628     "i" gnus-info-find-node)
1629
1630   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1631     "e" gnus-summary-expire-articles
1632     "\M-\C-e" gnus-summary-expire-articles-now
1633     "\177" gnus-summary-delete-article
1634     [delete] gnus-summary-delete-article
1635     [backspace] gnus-summary-delete-article
1636     "m" gnus-summary-move-article
1637     "r" gnus-summary-respool-article
1638     "w" gnus-summary-edit-article
1639     "c" gnus-summary-copy-article
1640     "B" gnus-summary-crosspost-article
1641     "q" gnus-summary-respool-query
1642     "t" gnus-summary-respool-trace
1643     "i" gnus-summary-import-article
1644     "p" gnus-summary-article-posted-p)
1645
1646   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1647     "o" gnus-summary-save-article
1648     "m" gnus-summary-save-article-mail
1649     "F" gnus-summary-write-article-file
1650     "r" gnus-summary-save-article-rmail
1651     "f" gnus-summary-save-article-file
1652     "b" gnus-summary-save-article-body-file
1653     "h" gnus-summary-save-article-folder
1654     "v" gnus-summary-save-article-vm
1655     "p" gnus-summary-pipe-output
1656     "s" gnus-soup-add-article)
1657
1658   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1659     "b" gnus-summary-display-buttonized
1660     "m" gnus-summary-repair-multipart
1661     "v" gnus-article-view-part
1662     "o" gnus-article-save-part
1663     "c" gnus-article-copy-part
1664     "e" gnus-article-externalize-part
1665     "i" gnus-article-inline-part
1666     "|" gnus-article-pipe-part))
1667
1668 (defun gnus-summary-make-menu-bar ()
1669   (gnus-turn-off-edit-menu 'summary)
1670
1671   (unless (boundp 'gnus-summary-misc-menu)
1672
1673     (easy-menu-define
1674      gnus-summary-kill-menu gnus-summary-mode-map ""
1675      (cons
1676       "Score"
1677       (nconc
1678        (list
1679         ["Enter score..." gnus-summary-score-entry t]
1680         ["Customize" gnus-score-customize t])
1681        (gnus-make-score-map 'increase)
1682        (gnus-make-score-map 'lower)
1683        '(("Mark"
1684           ["Kill below" gnus-summary-kill-below t]
1685           ["Mark above" gnus-summary-mark-above t]
1686           ["Tick above" gnus-summary-tick-above t]
1687           ["Clear above" gnus-summary-clear-above t])
1688          ["Current score" gnus-summary-current-score t]
1689          ["Set score" gnus-summary-set-score t]
1690          ["Switch current score file..." gnus-score-change-score-file t]
1691          ["Set mark below..." gnus-score-set-mark-below t]
1692          ["Set expunge below..." gnus-score-set-expunge-below t]
1693          ["Edit current score file" gnus-score-edit-current-scores t]
1694          ["Edit score file" gnus-score-edit-file t]
1695          ["Trace score" gnus-score-find-trace t]
1696          ["Find words" gnus-score-find-favourite-words t]
1697          ["Rescore buffer" gnus-summary-rescore t]
1698          ["Increase score..." gnus-summary-increase-score t]
1699          ["Lower score..." gnus-summary-lower-score t]))))
1700
1701     ;; Define both the Article menu in the summary buffer and the equivalent
1702     ;; Commands menu in the article buffer here for consistency.
1703     (let ((innards
1704            '(("Hide"
1705               ["All" gnus-article-hide t]
1706               ["Headers" gnus-article-toggle-headers t]
1707               ["Signature" gnus-article-hide-signature t]
1708               ["Citation" gnus-article-hide-citation t]
1709               ["List identifiers" gnus-article-hide-list-identifiers t]
1710               ["PGP" gnus-article-hide-pgp t]
1711               ["Banner" gnus-article-strip-banner t]
1712               ["Boring headers" gnus-article-hide-boring-headers t])
1713              ("Highlight"
1714               ["All" gnus-article-highlight t]
1715               ["Headers" gnus-article-highlight-headers t]
1716               ["Signature" gnus-article-highlight-signature t]
1717               ["Citation" gnus-article-highlight-citation t])
1718              ("Date"
1719               ["Local" gnus-article-date-local t]
1720               ["ISO8601" gnus-article-date-iso8601 t]
1721               ["UT" gnus-article-date-ut t]
1722               ["Original" gnus-article-date-original t]
1723               ["Lapsed" gnus-article-date-lapsed t]
1724               ["User-defined" gnus-article-date-user t])
1725              ("Washing"
1726               ("Remove Blanks"
1727                ["Leading" gnus-article-strip-leading-blank-lines t]
1728                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1729                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1730                ["All of the above" gnus-article-strip-blank-lines t]
1731                ["All" gnus-article-strip-all-blank-lines t]
1732                ["Leading space" gnus-article-strip-leading-space t]
1733                ["Trailing space" gnus-article-strip-trailing-space t])
1734               ["Overstrike" gnus-article-treat-overstrike t]
1735               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1736               ["Emphasis" gnus-article-emphasize t]
1737               ["Word wrap" gnus-article-fill-cited-article t]
1738               ["Fill long lines" gnus-article-fill-long-lines t]
1739               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1740               ["CR" gnus-article-remove-cr t]
1741               ["Show X-Face" gnus-article-display-x-face t]
1742               ["Rot 13" gnus-summary-caesar-message t]
1743               ["Unix pipe" gnus-summary-pipe-message t]
1744               ["Add buttons" gnus-article-add-buttons t]
1745               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1746               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1747               ["Toggle MIME" gnus-summary-toggle-mime t]
1748               ["Verbose header" gnus-summary-verbose-headers t]
1749               ["Toggle header" gnus-summary-toggle-header t]
1750               ["Toggle smileys" gnus-smiley-display t]
1751               ["HZ" gnus-article-decode-HZ t])
1752              ("Output"
1753               ["Save in default format" gnus-summary-save-article t]
1754               ["Save in file" gnus-summary-save-article-file t]
1755               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1756               ["Save in MH folder" gnus-summary-save-article-folder t]
1757               ["Save in VM folder" gnus-summary-save-article-vm t]
1758               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1759               ["Save body in file" gnus-summary-save-article-body-file t]
1760               ["Pipe through a filter" gnus-summary-pipe-output t]
1761               ["Add to SOUP packet" gnus-soup-add-article t]
1762               ["Print" gnus-summary-print-article t])
1763              ("Backend"
1764               ["Respool article..." gnus-summary-respool-article t]
1765               ["Move article..." gnus-summary-move-article
1766                (gnus-check-backend-function
1767                 'request-move-article gnus-newsgroup-name)]
1768               ["Copy article..." gnus-summary-copy-article t]
1769               ["Crosspost article..." gnus-summary-crosspost-article
1770                (gnus-check-backend-function
1771                 'request-replace-article gnus-newsgroup-name)]
1772               ["Import file..." gnus-summary-import-article t]
1773               ["Check if posted" gnus-summary-article-posted-p t]
1774               ["Edit article" gnus-summary-edit-article
1775                (not (gnus-group-read-only-p))]
1776               ["Delete article" gnus-summary-delete-article
1777                (gnus-check-backend-function
1778                 'request-expire-articles gnus-newsgroup-name)]
1779               ["Query respool" gnus-summary-respool-query t]
1780               ["Trace respool" gnus-summary-respool-trace t]
1781               ["Delete expirable articles" gnus-summary-expire-articles-now
1782                (gnus-check-backend-function
1783                 'request-expire-articles gnus-newsgroup-name)])
1784              ("Extract"
1785               ["Uudecode" gnus-uu-decode-uu t]
1786               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1787               ["Unshar" gnus-uu-decode-unshar t]
1788               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1789               ["Save" gnus-uu-decode-save t]
1790               ["Binhex" gnus-uu-decode-binhex t]
1791               ["Postscript" gnus-uu-decode-postscript t])
1792              ("Cache"
1793               ["Enter article" gnus-cache-enter-article t]
1794               ["Remove article" gnus-cache-remove-article t])
1795              ["Translate" gnus-article-babel t]
1796              ["Select article buffer" gnus-summary-select-article-buffer t]
1797              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1798              ["Isearch article..." gnus-summary-isearch-article t]
1799              ["Beginning of the article" gnus-summary-beginning-of-article t]
1800              ["End of the article" gnus-summary-end-of-article t]
1801              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1802              ["Fetch referenced articles" gnus-summary-refer-references t]
1803              ["Fetch current thread" gnus-summary-refer-thread t]
1804              ["Fetch article with id..." gnus-summary-refer-article t]
1805              ["Redisplay" gnus-summary-show-article t])))
1806       (easy-menu-define
1807        gnus-summary-article-menu gnus-summary-mode-map ""
1808        (cons "Article" innards))
1809
1810       (easy-menu-define
1811        gnus-article-commands-menu gnus-article-mode-map ""
1812        (cons "Commands" innards)))
1813
1814     (easy-menu-define
1815      gnus-summary-thread-menu gnus-summary-mode-map ""
1816      '("Threads"
1817        ["Toggle threading" gnus-summary-toggle-threads t]
1818        ["Hide threads" gnus-summary-hide-all-threads t]
1819        ["Show threads" gnus-summary-show-all-threads t]
1820        ["Hide thread" gnus-summary-hide-thread t]
1821        ["Show thread" gnus-summary-show-thread t]
1822        ["Go to next thread" gnus-summary-next-thread t]
1823        ["Go to previous thread" gnus-summary-prev-thread t]
1824        ["Go down thread" gnus-summary-down-thread t]
1825        ["Go up thread" gnus-summary-up-thread t]
1826        ["Top of thread" gnus-summary-top-thread t]
1827        ["Mark thread as read" gnus-summary-kill-thread t]
1828        ["Lower thread score" gnus-summary-lower-thread t]
1829        ["Raise thread score" gnus-summary-raise-thread t]
1830        ["Rethread current" gnus-summary-rethread-current t]))
1831
1832     (easy-menu-define
1833      gnus-summary-post-menu gnus-summary-mode-map ""
1834      '("Post"
1835        ["Post an article" gnus-summary-post-news t]
1836        ["Followup" gnus-summary-followup t]
1837        ["Followup and yank" gnus-summary-followup-with-original t]
1838        ["Supersede article" gnus-summary-supersede-article t]
1839        ["Cancel article" gnus-summary-cancel-article t]
1840        ["Reply" gnus-summary-reply t]
1841        ["Reply and yank" gnus-summary-reply-with-original t]
1842        ["Wide reply" gnus-summary-wide-reply t]
1843        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1844        ["Mail forward" gnus-summary-mail-forward t]
1845        ["Post forward" gnus-summary-post-forward t]
1846        ["Digest and mail" gnus-uu-digest-mail-forward t]
1847        ["Digest and post" gnus-uu-digest-post-forward t]
1848        ["Resend message" gnus-summary-resend-message t]
1849        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1850        ["Send a mail" gnus-summary-mail-other-window t]
1851        ["Uuencode and post" gnus-uu-post-news t]
1852        ["Followup via news" gnus-summary-followup-to-mail t]
1853        ["Followup via news and yank"
1854         gnus-summary-followup-to-mail-with-original t]
1855        ;;("Draft"
1856        ;;["Send" gnus-summary-send-draft t]
1857        ;;["Send bounced" gnus-resend-bounced-mail t])
1858        ))
1859
1860     (easy-menu-define
1861      gnus-summary-misc-menu gnus-summary-mode-map ""
1862      '("Misc"
1863        ("Mark Read"
1864         ["Mark as read" gnus-summary-mark-as-read-forward t]
1865         ["Mark same subject and select"
1866          gnus-summary-kill-same-subject-and-select t]
1867         ["Mark same subject" gnus-summary-kill-same-subject t]
1868         ["Catchup" gnus-summary-catchup t]
1869         ["Catchup all" gnus-summary-catchup-all t]
1870         ["Catchup to here" gnus-summary-catchup-to-here t]
1871         ["Catchup region" gnus-summary-mark-region-as-read t]
1872         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1873        ("Mark Various"
1874         ["Tick" gnus-summary-tick-article-forward t]
1875         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1876         ["Remove marks" gnus-summary-clear-mark-forward t]
1877         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1878         ["Set bookmark" gnus-summary-set-bookmark t]
1879         ["Remove bookmark" gnus-summary-remove-bookmark t])
1880        ("Mark Limit"
1881         ["Marks..." gnus-summary-limit-to-marks t]
1882         ["Subject..." gnus-summary-limit-to-subject t]
1883         ["Author..." gnus-summary-limit-to-author t]
1884         ["Age..." gnus-summary-limit-to-age t]
1885         ["Extra..." gnus-summary-limit-to-extra t]
1886         ["Score" gnus-summary-limit-to-score t]
1887         ["Unread" gnus-summary-limit-to-unread t]
1888         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1889         ["Articles" gnus-summary-limit-to-articles t]
1890         ["Pop limit" gnus-summary-pop-limit t]
1891         ["Show dormant" gnus-summary-limit-include-dormant t]
1892         ["Hide childless dormant"
1893          gnus-summary-limit-exclude-childless-dormant t]
1894         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1895         ["Hide marked" gnus-summary-limit-exclude-marks t]
1896         ["Show expunged" gnus-summary-show-all-expunged t])
1897        ("Process Mark"
1898         ["Set mark" gnus-summary-mark-as-processable t]
1899         ["Remove mark" gnus-summary-unmark-as-processable t]
1900         ["Remove all marks" gnus-summary-unmark-all-processable t]
1901         ["Mark above" gnus-uu-mark-over t]
1902         ["Mark series" gnus-uu-mark-series t]
1903         ["Mark region" gnus-uu-mark-region t]
1904         ["Unmark region" gnus-uu-unmark-region t]
1905         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1906         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1907         ["Mark all" gnus-uu-mark-all t]
1908         ["Mark buffer" gnus-uu-mark-buffer t]
1909         ["Mark sparse" gnus-uu-mark-sparse t]
1910         ["Mark thread" gnus-uu-mark-thread t]
1911         ["Unmark thread" gnus-uu-unmark-thread t]
1912         ("Process Mark Sets"
1913          ["Kill" gnus-summary-kill-process-mark t]
1914          ["Yank" gnus-summary-yank-process-mark
1915           gnus-newsgroup-process-stack]
1916          ["Save" gnus-summary-save-process-mark t]))
1917        ("Scroll article"
1918         ["Page forward" gnus-summary-next-page t]
1919         ["Page backward" gnus-summary-prev-page t]
1920         ["Line forward" gnus-summary-scroll-up t])
1921        ("Move"
1922         ["Next unread article" gnus-summary-next-unread-article t]
1923         ["Previous unread article" gnus-summary-prev-unread-article t]
1924         ["Next article" gnus-summary-next-article t]
1925         ["Previous article" gnus-summary-prev-article t]
1926         ["Next unread subject" gnus-summary-next-unread-subject t]
1927         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1928         ["Next article same subject" gnus-summary-next-same-subject t]
1929         ["Previous article same subject" gnus-summary-prev-same-subject t]
1930         ["First unread article" gnus-summary-first-unread-article t]
1931         ["Best unread article" gnus-summary-best-unread-article t]
1932         ["Go to subject number..." gnus-summary-goto-subject t]
1933         ["Go to article number..." gnus-summary-goto-article t]
1934         ["Go to the last article" gnus-summary-goto-last-article t]
1935         ["Pop article off history" gnus-summary-pop-article t])
1936        ("Sort"
1937         ["Sort by number" gnus-summary-sort-by-number t]
1938         ["Sort by author" gnus-summary-sort-by-author t]
1939         ["Sort by subject" gnus-summary-sort-by-subject t]
1940         ["Sort by date" gnus-summary-sort-by-date t]
1941         ["Sort by score" gnus-summary-sort-by-score t]
1942         ["Sort by lines" gnus-summary-sort-by-lines t]
1943         ["Sort by characters" gnus-summary-sort-by-chars t])
1944        ("Help"
1945         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1946         ["Describe group" gnus-summary-describe-group t]
1947         ["Read manual" gnus-info-find-node t])
1948        ("Modes"
1949         ["Pick and read" gnus-pick-mode t]
1950         ["Binary" gnus-binary-mode t])
1951        ("Regeneration"
1952         ["Regenerate" gnus-summary-prepare t]
1953         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1954         ["Toggle threading" gnus-summary-toggle-threads t])
1955        ["Filter articles..." gnus-summary-execute-command t]
1956        ["Run command on subjects..." gnus-summary-universal-argument t]
1957        ["Search articles forward..." gnus-summary-search-article-forward t]
1958        ["Search articles backward..." gnus-summary-search-article-backward t]
1959        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1960        ["Expand window" gnus-summary-expand-window t]
1961        ["Expire expirable articles" gnus-summary-expire-articles
1962         (gnus-check-backend-function
1963          'request-expire-articles gnus-newsgroup-name)]
1964        ["Edit local kill file" gnus-summary-edit-local-kill t]
1965        ["Edit main kill file" gnus-summary-edit-global-kill t]
1966        ["Edit group parameters" gnus-summary-edit-parameters t]
1967        ["Customize group parameters" gnus-summary-customize-parameters t]
1968        ["Send a bug report" gnus-bug t]
1969        ("Exit"
1970         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1971         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1972         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1973         ["Exit group" gnus-summary-exit t]
1974         ["Exit group without updating" gnus-summary-exit-no-update t]
1975         ["Exit and goto next group" gnus-summary-next-group t]
1976         ["Exit and goto prev group" gnus-summary-prev-group t]
1977         ["Reselect group" gnus-summary-reselect-current-group t]
1978         ["Rescan group" gnus-summary-rescan-group t]
1979         ["Update dribble" gnus-summary-save-newsrc t])))
1980
1981     (gnus-run-hooks 'gnus-summary-menu-hook)))
1982
1983 (defun gnus-score-set-default (var value)
1984   "A version of set that updates the GNU Emacs menu-bar."
1985   (set var value)
1986   ;; It is the message that forces the active status to be updated.
1987   (message ""))
1988
1989 (defun gnus-make-score-map (type)
1990   "Make a summary score map of type TYPE."
1991   (if t
1992       nil
1993     (let ((headers '(("author" "from" string)
1994                      ("subject" "subject" string)
1995                      ("article body" "body" string)
1996                      ("article head" "head" string)
1997                      ("xref" "xref" string)
1998                      ("extra header" "extra" string)
1999                      ("lines" "lines" number)
2000                      ("followups to author" "followup" string)))
2001           (types '((number ("less than" <)
2002                            ("greater than" >)
2003                            ("equal" =))
2004                    (string ("substring" s)
2005                            ("exact string" e)
2006                            ("fuzzy string" f)
2007                            ("regexp" r))))
2008           (perms '(("temporary" (current-time-string))
2009                    ("permanent" nil)
2010                    ("immediate" now)))
2011           header)
2012       (list
2013        (apply
2014         'nconc
2015         (list
2016          (if (eq type 'lower)
2017              "Lower score"
2018            "Increase score"))
2019         (let (outh)
2020           (while headers
2021             (setq header (car headers))
2022             (setq outh
2023                   (cons
2024                    (apply
2025                     'nconc
2026                     (list (car header))
2027                     (let ((ts (cdr (assoc (nth 2 header) types)))
2028                           outt)
2029                       (while ts
2030                         (setq outt
2031                               (cons
2032                                (apply
2033                                 'nconc
2034                                 (list (caar ts))
2035                                 (let ((ps perms)
2036                                       outp)
2037                                   (while ps
2038                                     (setq outp
2039                                           (cons
2040                                            (vector
2041                                             (caar ps)
2042                                             (list
2043                                              'gnus-summary-score-entry
2044                                              (nth 1 header)
2045                                              (if (or (string= (nth 1 header)
2046                                                               "head")
2047                                                      (string= (nth 1 header)
2048                                                               "body"))
2049                                                  ""
2050                                                (list 'gnus-summary-header
2051                                                      (nth 1 header)))
2052                                              (list 'quote (nth 1 (car ts)))
2053                                              (list 'gnus-score-delta-default
2054                                                    nil)
2055                                              (nth 1 (car ps))
2056                                              t)
2057                                             t)
2058                                            outp))
2059                                     (setq ps (cdr ps)))
2060                                   (list (nreverse outp))))
2061                                outt))
2062                         (setq ts (cdr ts)))
2063                       (list (nreverse outt))))
2064                    outh))
2065             (setq headers (cdr headers)))
2066           (list (nreverse outh))))))))
2067
2068 \f
2069
2070 (defun gnus-summary-mode (&optional group)
2071   "Major mode for reading articles.
2072
2073 All normal editing commands are switched off.
2074 \\<gnus-summary-mode-map>
2075 Each line in this buffer represents one article.  To read an
2076 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2077 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2078 respectively.
2079
2080 You can also post articles and send mail from this buffer.  To
2081 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2082 of an article, type `\\[gnus-summary-reply]'.
2083
2084 There are approx. one gazillion commands you can execute in this
2085 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2086
2087 The following commands are available:
2088
2089 \\{gnus-summary-mode-map}"
2090   (interactive)
2091   (when (gnus-visual-p 'summary-menu 'menu)
2092     (gnus-summary-make-menu-bar))
2093   (kill-all-local-variables)
2094   (gnus-summary-make-local-variables)
2095   (gnus-make-thread-indent-array)
2096   (gnus-simplify-mode-line)
2097   (setq major-mode 'gnus-summary-mode)
2098   (setq mode-name "Summary")
2099   (make-local-variable 'minor-mode-alist)
2100   (use-local-map gnus-summary-mode-map)
2101   (buffer-disable-undo)
2102   (setq buffer-read-only t)             ;Disable modification
2103   (setq truncate-lines t)
2104   (setq selective-display t)
2105   (setq selective-display-ellipses t)   ;Display `...'
2106   (gnus-summary-set-display-table)
2107   (gnus-set-default-directory)
2108   (setq gnus-newsgroup-name group)
2109   (unless (gnus-news-group-p group)
2110     (setq gnus-newsgroup-incorporated
2111           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2112   (make-local-variable 'gnus-summary-line-format)
2113   (make-local-variable 'gnus-summary-line-format-spec)
2114   (make-local-variable 'gnus-summary-dummy-line-format)
2115   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2116   (make-local-variable 'gnus-summary-mark-positions)
2117   (make-local-hook 'pre-command-hook)
2118   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2119   (gnus-run-hooks 'gnus-summary-mode-hook)
2120   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2121   (gnus-update-summary-mark-positions))
2122
2123 (defun gnus-summary-make-local-variables ()
2124   "Make all the local summary buffer variables."
2125   (let (global)
2126     (dolist (local gnus-summary-local-variables)
2127       (if (consp local)
2128           (progn
2129             (if (eq (cdr local) 'global)
2130                 ;; Copy the global value of the variable.
2131                 (setq global (symbol-value (car local)))
2132               ;; Use the value from the list.
2133               (setq global (eval (cdr local))))
2134             (set (make-local-variable (car local)) global))
2135         ;; Simple nil-valued local variable.
2136         (set (make-local-variable local) nil)))))
2137
2138 (defun gnus-summary-clear-local-variables ()
2139   (let ((locals gnus-summary-local-variables))
2140     (while locals
2141       (if (consp (car locals))
2142           (and (vectorp (caar locals))
2143                (set (caar locals) nil))
2144         (and (vectorp (car locals))
2145              (set (car locals) nil)))
2146       (setq locals (cdr locals)))))
2147
2148 ;; Summary data functions.
2149
2150 (defmacro gnus-data-number (data)
2151   `(car ,data))
2152
2153 (defmacro gnus-data-set-number (data number)
2154   `(setcar ,data ,number))
2155
2156 (defmacro gnus-data-mark (data)
2157   `(nth 1 ,data))
2158
2159 (defmacro gnus-data-set-mark (data mark)
2160   `(setcar (nthcdr 1 ,data) ,mark))
2161
2162 (defmacro gnus-data-pos (data)
2163   `(nth 2 ,data))
2164
2165 (defmacro gnus-data-set-pos (data pos)
2166   `(setcar (nthcdr 2 ,data) ,pos))
2167
2168 (defmacro gnus-data-header (data)
2169   `(nth 3 ,data))
2170
2171 (defmacro gnus-data-set-header (data header)
2172   `(setcar (nthcdr 3 ,data) ,header))
2173
2174 (defmacro gnus-data-level (data)
2175   `(nth 4 ,data))
2176
2177 (defmacro gnus-data-unread-p (data)
2178   `(= (nth 1 ,data) gnus-unread-mark))
2179
2180 (defmacro gnus-data-read-p (data)
2181   `(/= (nth 1 ,data) gnus-unread-mark))
2182
2183 (defmacro gnus-data-pseudo-p (data)
2184   `(consp (nth 3 ,data)))
2185
2186 (defmacro gnus-data-find (number)
2187   `(assq ,number gnus-newsgroup-data))
2188
2189 (defmacro gnus-data-find-list (number &optional data)
2190   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2191      (memq (assq ,number bdata)
2192            bdata)))
2193
2194 (defmacro gnus-data-make (number mark pos header level)
2195   `(list ,number ,mark ,pos ,header ,level))
2196
2197 (defun gnus-data-enter (after-article number mark pos header level offset)
2198   (let ((data (gnus-data-find-list after-article)))
2199     (unless data
2200       (error "No such article: %d" after-article))
2201     (setcdr data (cons (gnus-data-make number mark pos header level)
2202                        (cdr data)))
2203     (setq gnus-newsgroup-data-reverse nil)
2204     (gnus-data-update-list (cddr data) offset)))
2205
2206 (defun gnus-data-enter-list (after-article list &optional offset)
2207   (when list
2208     (let ((data (and after-article (gnus-data-find-list after-article)))
2209           (ilist list))
2210       (if (not (or data
2211                    after-article))
2212           (let ((odata gnus-newsgroup-data))
2213             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2214             (when offset
2215               (gnus-data-update-list odata offset)))
2216         ;; Find the last element in the list to be spliced into the main
2217         ;; list.
2218         (while (cdr list)
2219           (setq list (cdr list)))
2220         (if (not data)
2221             (progn
2222               (setcdr list gnus-newsgroup-data)
2223               (setq gnus-newsgroup-data ilist)
2224               (when offset
2225                 (gnus-data-update-list (cdr list) offset)))
2226           (setcdr list (cdr data))
2227           (setcdr data ilist)
2228           (when offset
2229             (gnus-data-update-list (cdr list) offset))))
2230       (setq gnus-newsgroup-data-reverse nil))))
2231
2232 (defun gnus-data-remove (article &optional offset)
2233   (let ((data gnus-newsgroup-data))
2234     (if (= (gnus-data-number (car data)) article)
2235         (progn
2236           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2237                 gnus-newsgroup-data-reverse nil)
2238           (when offset
2239             (gnus-data-update-list gnus-newsgroup-data offset)))
2240       (while (cdr data)
2241         (when (= (gnus-data-number (cadr data)) article)
2242           (setcdr data (cddr data))
2243           (when offset
2244             (gnus-data-update-list (cdr data) offset))
2245           (setq data nil
2246                 gnus-newsgroup-data-reverse nil))
2247         (setq data (cdr data))))))
2248
2249 (defmacro gnus-data-list (backward)
2250   `(if ,backward
2251        (or gnus-newsgroup-data-reverse
2252            (setq gnus-newsgroup-data-reverse
2253                  (reverse gnus-newsgroup-data)))
2254      gnus-newsgroup-data))
2255
2256 (defun gnus-data-update-list (data offset)
2257   "Add OFFSET to the POS of all data entries in DATA."
2258   (setq gnus-newsgroup-data-reverse nil)
2259   (while data
2260     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2261     (setq data (cdr data))))
2262
2263 (defun gnus-summary-article-pseudo-p (article)
2264   "Say whether this article is a pseudo article or not."
2265   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2266
2267 (defmacro gnus-summary-article-sparse-p (article)
2268   "Say whether this article is a sparse article or not."
2269   `(memq ,article gnus-newsgroup-sparse))
2270
2271 (defmacro gnus-summary-article-ancient-p (article)
2272   "Say whether this article is a sparse article or not."
2273   `(memq ,article gnus-newsgroup-ancient))
2274
2275 (defun gnus-article-parent-p (number)
2276   "Say whether this article is a parent or not."
2277   (let ((data (gnus-data-find-list number)))
2278     (and (cdr data)                     ; There has to be an article after...
2279          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2280             (gnus-data-level (nth 1 data))))))
2281
2282 (defun gnus-article-children (number)
2283   "Return a list of all children to NUMBER."
2284   (let* ((data (gnus-data-find-list number))
2285          (level (gnus-data-level (car data)))
2286          children)
2287     (setq data (cdr data))
2288     (while (and data
2289                 (= (gnus-data-level (car data)) (1+ level)))
2290       (push (gnus-data-number (car data)) children)
2291       (setq data (cdr data)))
2292     children))
2293
2294 (defmacro gnus-summary-skip-intangible ()
2295   "If the current article is intangible, then jump to a different article."
2296   '(let ((to (get-text-property (point) 'gnus-intangible)))
2297      (and to (gnus-summary-goto-subject to))))
2298
2299 (defmacro gnus-summary-article-intangible-p ()
2300   "Say whether this article is intangible or not."
2301   '(get-text-property (point) 'gnus-intangible))
2302
2303 (defun gnus-article-read-p (article)
2304   "Say whether ARTICLE is read or not."
2305   (not (or (memq article gnus-newsgroup-marked)
2306            (memq article gnus-newsgroup-unreads)
2307            (memq article gnus-newsgroup-unselected)
2308            (memq article gnus-newsgroup-dormant))))
2309
2310 ;; Some summary mode macros.
2311
2312 (defmacro gnus-summary-article-number ()
2313   "The article number of the article on the current line.
2314 If there isn's an article number here, then we return the current
2315 article number."
2316   '(progn
2317      (gnus-summary-skip-intangible)
2318      (or (get-text-property (point) 'gnus-number)
2319          (gnus-summary-last-subject))))
2320
2321 (defmacro gnus-summary-article-header (&optional number)
2322   "Return the header of article NUMBER."
2323   `(gnus-data-header (gnus-data-find
2324                       ,(or number '(gnus-summary-article-number)))))
2325
2326 (defmacro gnus-summary-thread-level (&optional number)
2327   "Return the level of thread that starts with article NUMBER."
2328   `(if (and (eq gnus-summary-make-false-root 'dummy)
2329             (get-text-property (point) 'gnus-intangible))
2330        0
2331      (gnus-data-level (gnus-data-find
2332                        ,(or number '(gnus-summary-article-number))))))
2333
2334 (defmacro gnus-summary-article-mark (&optional number)
2335   "Return the mark of article NUMBER."
2336   `(gnus-data-mark (gnus-data-find
2337                     ,(or number '(gnus-summary-article-number)))))
2338
2339 (defmacro gnus-summary-article-pos (&optional number)
2340   "Return the position of the line of article NUMBER."
2341   `(gnus-data-pos (gnus-data-find
2342                    ,(or number '(gnus-summary-article-number)))))
2343
2344 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2345 (defmacro gnus-summary-article-subject (&optional number)
2346   "Return current subject string or nil if nothing."
2347   `(let ((headers
2348           ,(if number
2349                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2350              '(gnus-data-header (assq (gnus-summary-article-number)
2351                                       gnus-newsgroup-data)))))
2352      (and headers
2353           (vectorp headers)
2354           (mail-header-subject headers))))
2355
2356 (defmacro gnus-summary-article-score (&optional number)
2357   "Return current article score."
2358   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2359                   gnus-newsgroup-scored))
2360        gnus-summary-default-score 0))
2361
2362 (defun gnus-summary-article-children (&optional number)
2363   "Return a list of article numbers that are children of article NUMBER."
2364   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2365          (level (gnus-data-level (car data)))
2366          l children)
2367     (while (and (setq data (cdr data))
2368                 (> (setq l (gnus-data-level (car data))) level))
2369       (and (= (1+ level) l)
2370            (push (gnus-data-number (car data))
2371                  children)))
2372     (nreverse children)))
2373
2374 (defun gnus-summary-article-parent (&optional number)
2375   "Return the article number of the parent of article NUMBER."
2376   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2377                                     (gnus-data-list t)))
2378          (level (gnus-data-level (car data))))
2379     (if (zerop level)
2380         ()                              ; This is a root.
2381       ;; We search until we find an article with a level less than
2382       ;; this one.  That function has to be the parent.
2383       (while (and (setq data (cdr data))
2384                   (not (< (gnus-data-level (car data)) level))))
2385       (and data (gnus-data-number (car data))))))
2386
2387 (defun gnus-unread-mark-p (mark)
2388   "Say whether MARK is the unread mark."
2389   (= mark gnus-unread-mark))
2390
2391 (defun gnus-read-mark-p (mark)
2392   "Say whether MARK is one of the marks that mark as read.
2393 This is all marks except unread, ticked, dormant, and expirable."
2394   (not (or (= mark gnus-unread-mark)
2395            (= mark gnus-ticked-mark)
2396            (= mark gnus-dormant-mark)
2397            (= mark gnus-expirable-mark))))
2398
2399 (defmacro gnus-article-mark (number)
2400   "Return the MARK of article NUMBER.
2401 This macro should only be used when computing the mark the \"first\"
2402 time; i.e., when generating the summary lines.  After that,
2403 `gnus-summary-article-mark' should be used to examine the
2404 marks of articles."
2405   `(cond
2406     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2407     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2408     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2409     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2410     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2411     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2412     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2413     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2414            gnus-ancient-mark))))
2415
2416 ;; Saving hidden threads.
2417
2418 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2419 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2420
2421 (defmacro gnus-save-hidden-threads (&rest forms)
2422   "Save hidden threads, eval FORMS, and restore the hidden threads."
2423   (let ((config (make-symbol "config")))
2424     `(let ((,config (gnus-hidden-threads-configuration)))
2425        (unwind-protect
2426            (save-excursion
2427              ,@forms)
2428          (gnus-restore-hidden-threads-configuration ,config)))))
2429
2430 (defun gnus-data-compute-positions ()
2431   "Compute the positions of all articles."
2432   (setq gnus-newsgroup-data-reverse nil)
2433   (let ((data gnus-newsgroup-data))
2434     (save-excursion
2435       (gnus-save-hidden-threads
2436         (gnus-summary-show-all-threads)
2437         (goto-char (point-min))
2438         (while data
2439           (while (get-text-property (point) 'gnus-intangible)
2440             (forward-line 1))
2441           (gnus-data-set-pos (car data) (+ (point) 3))
2442           (setq data (cdr data))
2443           (forward-line 1))))))
2444
2445 (defun gnus-hidden-threads-configuration ()
2446   "Return the current hidden threads configuration."
2447   (save-excursion
2448     (let (config)
2449       (goto-char (point-min))
2450       (while (search-forward "\r" nil t)
2451         (push (1- (point)) config))
2452       config)))
2453
2454 (defun gnus-restore-hidden-threads-configuration (config)
2455   "Restore hidden threads configuration from CONFIG."
2456   (save-excursion
2457     (let (point buffer-read-only)
2458       (while (setq point (pop config))
2459         (when (and (< point (point-max))
2460                    (goto-char point)
2461                    (eq (char-after) ?\n))
2462           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2463
2464 ;; Various summary mode internalish functions.
2465
2466 (defun gnus-mouse-pick-article (e)
2467   (interactive "e")
2468   (mouse-set-point e)
2469   (gnus-summary-next-page nil t))
2470
2471 (defun gnus-summary-set-display-table ()
2472   "Change the display table.
2473 Odd characters have a tendency to mess
2474 up nicely formatted displays - we make all possible glyphs
2475 display only a single character."
2476
2477   ;; We start from the standard display table, if any.
2478   (let ((table (or (copy-sequence standard-display-table)
2479                    (make-display-table)))
2480         (i 32))
2481     ;; Nix out all the control chars...
2482     (while (>= (setq i (1- i)) 0)
2483       (aset table i [??]))
2484     ;; ... but not newline and cr, of course.  (cr is necessary for the
2485     ;; selective display).
2486     (aset table ?\n nil)
2487     (aset table ?\r nil)
2488     ;; We keep TAB as well.
2489     (aset table ?\t nil)
2490     ;; We nix out any glyphs over 126 that are not set already.
2491     (let ((i 256))
2492       (while (>= (setq i (1- i)) 127)
2493         ;; Only modify if the entry is nil.
2494         (unless (aref table i)
2495           (aset table i [??]))))
2496     (setq buffer-display-table table)))
2497
2498 (defun gnus-summary-setup-buffer (group)
2499   "Initialize summary buffer."
2500   (let ((buffer (concat "*Summary " group "*")))
2501     (if (get-buffer buffer)
2502         (progn
2503           (set-buffer buffer)
2504           (setq gnus-summary-buffer (current-buffer))
2505           (not gnus-newsgroup-prepared))
2506       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2507       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2508       (gnus-summary-mode group)
2509       (when gnus-carpal
2510         (gnus-carpal-setup-buffer 'summary))
2511       (unless gnus-single-article-buffer
2512         (make-local-variable 'gnus-article-buffer)
2513         (make-local-variable 'gnus-article-current)
2514         (make-local-variable 'gnus-original-article-buffer))
2515       (setq gnus-newsgroup-name group)
2516       t)))
2517
2518 (defun gnus-set-global-variables ()
2519   "Set the global equivalents of the buffer-local variables.
2520 They are set to the latest values they had.  These reflect the summary
2521 buffer that was in action when the last article was fetched."
2522   (when (eq major-mode 'gnus-summary-mode)
2523     (setq gnus-summary-buffer (current-buffer))
2524     (let ((name gnus-newsgroup-name)
2525           (marked gnus-newsgroup-marked)
2526           (unread gnus-newsgroup-unreads)
2527           (headers gnus-current-headers)
2528           (data gnus-newsgroup-data)
2529           (summary gnus-summary-buffer)
2530           (article-buffer gnus-article-buffer)
2531           (original gnus-original-article-buffer)
2532           (gac gnus-article-current)
2533           (reffed gnus-reffed-article-number)
2534           (score-file gnus-current-score-file)
2535           (default-charset gnus-newsgroup-charset))
2536       (save-excursion
2537         (set-buffer gnus-group-buffer)
2538         (setq gnus-newsgroup-name name
2539               gnus-newsgroup-marked marked
2540               gnus-newsgroup-unreads unread
2541               gnus-current-headers headers
2542               gnus-newsgroup-data data
2543               gnus-article-current gac
2544               gnus-summary-buffer summary
2545               gnus-article-buffer article-buffer
2546               gnus-original-article-buffer original
2547               gnus-reffed-article-number reffed
2548               gnus-current-score-file score-file
2549               gnus-newsgroup-charset default-charset)
2550         ;; The article buffer also has local variables.
2551         (when (gnus-buffer-live-p gnus-article-buffer)
2552           (set-buffer gnus-article-buffer)
2553           (setq gnus-summary-buffer summary))))))
2554
2555 (defun gnus-summary-article-unread-p (article)
2556   "Say whether ARTICLE is unread or not."
2557   (memq article gnus-newsgroup-unreads))
2558
2559 (defun gnus-summary-first-article-p (&optional article)
2560   "Return whether ARTICLE is the first article in the buffer."
2561   (if (not (setq article (or article (gnus-summary-article-number))))
2562       nil
2563     (eq article (caar gnus-newsgroup-data))))
2564
2565 (defun gnus-summary-last-article-p (&optional article)
2566   "Return whether ARTICLE is the last article in the buffer."
2567   (if (not (setq article (or article (gnus-summary-article-number))))
2568       ;; All non-existent numbers are the last article.  :-)
2569       t
2570     (not (cdr (gnus-data-find-list article)))))
2571
2572 (defun gnus-make-thread-indent-array ()
2573   (let ((n 200))
2574     (unless (and gnus-thread-indent-array
2575                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2576       (setq gnus-thread-indent-array (make-vector 201 "")
2577             gnus-thread-indent-array-level gnus-thread-indent-level)
2578       (while (>= n 0)
2579         (aset gnus-thread-indent-array n
2580               (make-string (* n gnus-thread-indent-level) ? ))
2581         (setq n (1- n))))))
2582
2583 (defun gnus-update-summary-mark-positions ()
2584   "Compute where the summary marks are to go."
2585   (save-excursion
2586     (when (gnus-buffer-exists-p gnus-summary-buffer)
2587       (set-buffer gnus-summary-buffer))
2588     (let ((gnus-replied-mark 129)
2589           (gnus-score-below-mark 130)
2590           (gnus-score-over-mark 130)
2591           (gnus-download-mark 131)
2592           (spec gnus-summary-line-format-spec)
2593           gnus-visual pos)
2594       (save-excursion
2595         (gnus-set-work-buffer)
2596         (let ((gnus-summary-line-format-spec spec)
2597               (gnus-newsgroup-downloadable '((0 . t))))
2598           (gnus-summary-insert-line
2599            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2600            0 nil 128 t nil "" nil 1)
2601           (goto-char (point-min))
2602           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2603                                              (- (point) 2)))))
2604           (goto-char (point-min))
2605           (push (cons 'replied (and (search-forward "\201" nil t)
2606                                     (- (point) 2)))
2607                 pos)
2608           (goto-char (point-min))
2609           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2610                 pos)
2611           (goto-char (point-min))
2612           (push (cons 'download
2613                       (and (search-forward "\203" nil t) (- (point) 2)))
2614                 pos)))
2615       (setq gnus-summary-mark-positions pos))))
2616
2617 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2618   "Insert a dummy root in the summary buffer."
2619   (beginning-of-line)
2620   (gnus-add-text-properties
2621    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2622    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2623
2624 (defun gnus-summary-from-or-to-or-newsgroups (header)
2625   (let ((to (cdr (assq 'To (mail-header-extra header))))
2626         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2627         (default-mime-charset (with-current-buffer gnus-summary-buffer
2628                                 default-mime-charset)))
2629     (cond
2630      ((and to
2631            gnus-ignored-from-addresses
2632            (string-match gnus-ignored-from-addresses
2633                          (mail-header-from header)))
2634       (concat "-> "
2635               (or (car (funcall gnus-extract-address-components
2636                                 (funcall
2637                                  gnus-decode-encoded-word-function to)))
2638                   (funcall gnus-decode-encoded-word-function to))))
2639      ((and newsgroups
2640            gnus-ignored-from-addresses
2641            (string-match gnus-ignored-from-addresses
2642                          (mail-header-from header)))
2643       (concat "=> " newsgroups))
2644      (t
2645       (or (car (funcall gnus-extract-address-components
2646                         (mail-header-from header)))
2647           (mail-header-from header))))))
2648
2649 (defun gnus-summary-insert-line (gnus-tmp-header
2650                                  gnus-tmp-level gnus-tmp-current
2651                                  gnus-tmp-unread gnus-tmp-replied
2652                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2653                                  &optional gnus-tmp-dummy gnus-tmp-score
2654                                  gnus-tmp-process)
2655   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2656          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2657          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2658          (gnus-tmp-score-char
2659           (if (or (null gnus-summary-default-score)
2660                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2661                       gnus-summary-zcore-fuzz))
2662               ?  ;Whitespace
2663             (if (< gnus-tmp-score gnus-summary-default-score)
2664                 gnus-score-below-mark gnus-score-over-mark)))
2665          (gnus-tmp-replied
2666           (cond (gnus-tmp-process gnus-process-mark)
2667                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2668                  gnus-cached-mark)
2669                 (gnus-tmp-replied gnus-replied-mark)
2670                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2671                  gnus-saved-mark)
2672                 (t gnus-unread-mark)))
2673          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2674          (gnus-tmp-name
2675           (cond
2676            ((string-match "<[^>]+> *$" gnus-tmp-from)
2677             (let ((beg (match-beginning 0)))
2678               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2679                        (substring gnus-tmp-from (1+ (match-beginning 0))
2680                                   (1- (match-end 0))))
2681                   (substring gnus-tmp-from 0 beg))))
2682            ((string-match "(.+)" gnus-tmp-from)
2683             (substring gnus-tmp-from
2684                        (1+ (match-beginning 0)) (1- (match-end 0))))
2685            (t gnus-tmp-from)))
2686          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2687          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2688          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2689          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2690          (buffer-read-only nil))
2691     (when (string= gnus-tmp-name "")
2692       (setq gnus-tmp-name gnus-tmp-from))
2693     (unless (numberp gnus-tmp-lines)
2694       (setq gnus-tmp-lines 0))
2695     (gnus-put-text-property-excluding-characters-with-faces
2696      (point)
2697      (progn (eval gnus-summary-line-format-spec) (point))
2698      'gnus-number gnus-tmp-number)
2699     (when (gnus-visual-p 'summary-highlight 'highlight)
2700       (forward-line -1)
2701       (gnus-run-hooks 'gnus-summary-update-hook)
2702       (forward-line 1))))
2703
2704 (defun gnus-summary-update-line (&optional dont-update)
2705   "Update summary line after change."
2706   (when (and gnus-summary-default-score
2707              (not gnus-summary-inhibit-highlight))
2708     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2709            (article (gnus-summary-article-number))
2710            (score (gnus-summary-article-score article)))
2711       (unless dont-update
2712         (if (and gnus-summary-mark-below
2713                  (< (gnus-summary-article-score)
2714                     gnus-summary-mark-below))
2715             ;; This article has a low score, so we mark it as read.
2716             (when (memq article gnus-newsgroup-unreads)
2717               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2718           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2719             ;; This article was previously marked as read on account
2720             ;; of a low score, but now it has risen, so we mark it as
2721             ;; unread.
2722             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2723         (gnus-summary-update-mark
2724          (if (or (null gnus-summary-default-score)
2725                  (<= (abs (- score gnus-summary-default-score))
2726                      gnus-summary-zcore-fuzz))
2727              ?  ;Whitespace
2728            (if (< score gnus-summary-default-score)
2729                gnus-score-below-mark gnus-score-over-mark))
2730          'score))
2731       ;; Do visual highlighting.
2732       (when (gnus-visual-p 'summary-highlight 'highlight)
2733         (gnus-run-hooks 'gnus-summary-update-hook)))))
2734
2735 (defvar gnus-tmp-new-adopts nil)
2736
2737 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2738   "Return the number of articles in THREAD.
2739 This may be 0 in some cases -- if none of the articles in
2740 the thread are to be displayed."
2741   (let* ((number
2742           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2743           (cond
2744            ((not (listp thread))
2745             1)
2746            ((and (consp thread) (cdr thread))
2747             (apply
2748              '+ 1 (mapcar
2749                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2750            ((null thread)
2751             1)
2752            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2753             1)
2754            (t 0))))
2755     (when (and level (zerop level) gnus-tmp-new-adopts)
2756       (incf number
2757             (apply '+ (mapcar
2758                        'gnus-summary-number-of-articles-in-thread
2759                        gnus-tmp-new-adopts))))
2760     (if char
2761         (if (> number 1) gnus-not-empty-thread-mark
2762           gnus-empty-thread-mark)
2763       number)))
2764
2765 (defun gnus-summary-set-local-parameters (group)
2766   "Go through the local params of GROUP and set all variable specs in that list."
2767   (let ((params (gnus-group-find-parameter group))
2768         elem)
2769     (while params
2770       (setq elem (car params)
2771             params (cdr params))
2772       (and (consp elem)                 ; Has to be a cons.
2773            (consp (cdr elem))           ; The cdr has to be a list.
2774            (symbolp (car elem))         ; Has to be a symbol in there.
2775            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2776            (ignore-errors               ; So we set it.
2777              (make-local-variable (car elem))
2778              (set (car elem) (eval (nth 1 elem))))))))
2779
2780 (defun gnus-summary-read-group (group &optional show-all no-article
2781                                       kill-buffer no-display backward
2782                                       select-articles)
2783   "Start reading news in newsgroup GROUP.
2784 If SHOW-ALL is non-nil, already read articles are also listed.
2785 If NO-ARTICLE is non-nil, no article is selected initially.
2786 If NO-DISPLAY, don't generate a summary buffer."
2787   (let (result)
2788     (while (and group
2789                 (null (setq result
2790                             (let ((gnus-auto-select-next nil))
2791                               (or (gnus-summary-read-group-1
2792                                    group show-all no-article
2793                                    kill-buffer no-display
2794                                    select-articles)
2795                                   (setq show-all nil
2796                                         select-articles nil)))))
2797                 (eq gnus-auto-select-next 'quietly))
2798       (set-buffer gnus-group-buffer)
2799       ;; The entry function called above goes to the next
2800       ;; group automatically, so we go two groups back
2801       ;; if we are searching for the previous group.
2802       (when backward
2803         (gnus-group-prev-unread-group 2))
2804       (if (not (equal group (gnus-group-group-name)))
2805           (setq group (gnus-group-group-name))
2806         (setq group nil)))
2807     result))
2808
2809 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2810   "Directly jump to the other GROUP from summary buffer.
2811 If SHOW-ALL is non-nil, already read articles are also listed."
2812   (interactive
2813    (if (eq gnus-summary-buffer (current-buffer))
2814        (list (completing-read
2815               "Group: " gnus-active-hashtb nil t
2816               (when (and gnus-newsgroup-name
2817                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2818                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2819               'gnus-group-history)
2820              current-prefix-arg)
2821      (error "%s must be invoked from a gnus summary buffer." this-command)))
2822   (unless (or (zerop (length group))
2823               (and gnus-newsgroup-name
2824                    (string-equal gnus-newsgroup-name group)))
2825     (gnus-summary-exit)
2826     (gnus-summary-read-group group show-all
2827                              gnus-dont-select-after-jump-to-other-group)))
2828
2829 (defun gnus-summary-read-group-1 (group show-all no-article
2830                                         kill-buffer no-display
2831                                         &optional select-articles)
2832   ;; Killed foreign groups can't be entered.
2833   (when (and (not (gnus-group-native-p group))
2834              (not (gnus-gethash group gnus-newsrc-hashtb)))
2835     (error "Dead non-native groups can't be entered"))
2836   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2837   (let* ((new-group (gnus-summary-setup-buffer group))
2838          (quit-config (gnus-group-quit-config group))
2839          (did-select (and new-group (gnus-select-newsgroup
2840                                      group show-all select-articles))))
2841     (cond
2842      ;; This summary buffer exists already, so we just select it.
2843      ((not new-group)
2844       (gnus-set-global-variables)
2845       (when kill-buffer
2846         (gnus-kill-or-deaden-summary kill-buffer))
2847       (gnus-configure-windows 'summary 'force)
2848       (gnus-set-mode-line 'summary)
2849       (gnus-summary-position-point)
2850       (message "")
2851       t)
2852      ;; We couldn't select this group.
2853      ((null did-select)
2854       (when (and (eq major-mode 'gnus-summary-mode)
2855                  (not (equal (current-buffer) kill-buffer)))
2856         (kill-buffer (current-buffer))
2857         (if (not quit-config)
2858             (progn
2859               ;; Update the info -- marks might need to be removed,
2860               ;; for instance.
2861               (gnus-summary-update-info)
2862               (set-buffer gnus-group-buffer)
2863               (gnus-group-jump-to-group group)
2864               (gnus-group-next-unread-group 1))
2865           (gnus-handle-ephemeral-exit quit-config)))
2866       (gnus-message 3 "Can't select group")
2867       nil)
2868      ;; The user did a `C-g' while prompting for number of articles,
2869      ;; so we exit this group.
2870      ((eq did-select 'quit)
2871       (and (eq major-mode 'gnus-summary-mode)
2872            (not (equal (current-buffer) kill-buffer))
2873            (kill-buffer (current-buffer)))
2874       (when kill-buffer
2875         (gnus-kill-or-deaden-summary kill-buffer))
2876       (if (not quit-config)
2877           (progn
2878             (set-buffer gnus-group-buffer)
2879             (gnus-group-jump-to-group group)
2880             (gnus-group-next-unread-group 1)
2881             (gnus-configure-windows 'group 'force))
2882         (gnus-handle-ephemeral-exit quit-config))
2883       ;; Finally signal the quit.
2884       (signal 'quit nil))
2885      ;; The group was successfully selected.
2886      (t
2887       (gnus-set-global-variables)
2888       ;; Save the active value in effect when the group was entered.
2889       (setq gnus-newsgroup-active
2890             (gnus-copy-sequence
2891              (gnus-active gnus-newsgroup-name)))
2892       ;; You can change the summary buffer in some way with this hook.
2893       (gnus-run-hooks 'gnus-select-group-hook)
2894       ;; Set any local variables in the group parameters.
2895       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2896       (gnus-update-format-specifications
2897        nil 'summary 'summary-mode 'summary-dummy)
2898       (gnus-update-summary-mark-positions)
2899       ;; Do score processing.
2900       (when gnus-use-scoring
2901         (gnus-possibly-score-headers))
2902       ;; Check whether to fill in the gaps in the threads.
2903       (when gnus-build-sparse-threads
2904         (gnus-build-sparse-threads))
2905       ;; Find the initial limit.
2906       (if gnus-show-threads
2907           (if show-all
2908               (let ((gnus-newsgroup-dormant nil))
2909                 (gnus-summary-initial-limit show-all))
2910             (gnus-summary-initial-limit show-all))
2911         ;; When untreaded, all articles are always shown.
2912         (setq gnus-newsgroup-limit
2913               (mapcar
2914                (lambda (header) (mail-header-number header))
2915                gnus-newsgroup-headers)))
2916       ;; Generate the summary buffer.
2917       (unless no-display
2918         (gnus-summary-prepare))
2919       (when gnus-use-trees
2920         (gnus-tree-open group)
2921         (setq gnus-summary-highlight-line-function
2922               'gnus-tree-highlight-article))
2923       ;; If the summary buffer is empty, but there are some low-scored
2924       ;; articles or some excluded dormants, we include these in the
2925       ;; buffer.
2926       (when (and (zerop (buffer-size))
2927                  (not no-display))
2928         (cond (gnus-newsgroup-dormant
2929                (gnus-summary-limit-include-dormant))
2930               ((and gnus-newsgroup-scored show-all)
2931                (gnus-summary-limit-include-expunged t))))
2932       ;; Function `gnus-apply-kill-file' must be called in this hook.
2933       (gnus-run-hooks 'gnus-apply-kill-hook)
2934       (if (and (zerop (buffer-size))
2935                (not no-display))
2936           (progn
2937             ;; This newsgroup is empty.
2938             (gnus-summary-catchup-and-exit nil t)
2939             (gnus-message 6 "No unread news")
2940             (when kill-buffer
2941               (gnus-kill-or-deaden-summary kill-buffer))
2942             ;; Return nil from this function.
2943             nil)
2944         ;; Hide conversation thread subtrees.  We cannot do this in
2945         ;; gnus-summary-prepare-hook since kill processing may not
2946         ;; work with hidden articles.
2947         (and gnus-show-threads
2948              gnus-thread-hide-subtree
2949              (gnus-summary-hide-all-threads))
2950         (when kill-buffer
2951           (gnus-kill-or-deaden-summary kill-buffer))
2952         ;; Show first unread article if requested.
2953         (if (and (not no-article)
2954                  (not no-display)
2955                  gnus-newsgroup-unreads
2956                  gnus-auto-select-first)
2957             (progn
2958               (gnus-configure-windows 'summary)
2959               (cond
2960                ((eq gnus-auto-select-first 'best)
2961                 (gnus-summary-best-unread-article))
2962                ((eq gnus-auto-select-first t)
2963                 (gnus-summary-first-unread-article))
2964                ((gnus-functionp gnus-auto-select-first)
2965                 (funcall gnus-auto-select-first))))
2966           ;; Don't select any articles, just move point to the first
2967           ;; article in the group.
2968           (goto-char (point-min))
2969           (gnus-summary-position-point)
2970           (gnus-configure-windows 'summary 'force)
2971           (gnus-set-mode-line 'summary))
2972         (when (get-buffer-window gnus-group-buffer t)
2973           ;; Gotta use windows, because recenter does weird stuff if
2974           ;; the current buffer ain't the displayed window.
2975           (let ((owin (selected-window)))
2976             (select-window (get-buffer-window gnus-group-buffer t))
2977             (when (gnus-group-goto-group group)
2978               (recenter))
2979             (select-window owin)))
2980         ;; Mark this buffer as "prepared".
2981         (setq gnus-newsgroup-prepared t)
2982         (gnus-run-hooks 'gnus-summary-prepared-hook)
2983         t)))))
2984
2985 (defun gnus-summary-prepare ()
2986   "Generate the summary buffer."
2987   (interactive)
2988   (let ((buffer-read-only nil))
2989     (erase-buffer)
2990     (setq gnus-newsgroup-data nil
2991           gnus-newsgroup-data-reverse nil)
2992     (gnus-run-hooks 'gnus-summary-generate-hook)
2993     ;; Generate the buffer, either with threads or without.
2994     (when gnus-newsgroup-headers
2995       (gnus-summary-prepare-threads
2996        (if gnus-show-threads
2997            (gnus-sort-gathered-threads
2998             (funcall gnus-summary-thread-gathering-function
2999                      (gnus-sort-threads
3000                       (gnus-cut-threads (gnus-make-threads)))))
3001          ;; Unthreaded display.
3002          (gnus-sort-articles gnus-newsgroup-headers))))
3003     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3004     ;; Call hooks for modifying summary buffer.
3005     (goto-char (point-min))
3006     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3007
3008 (defsubst gnus-general-simplify-subject (subject)
3009   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3010   (setq subject
3011         (cond
3012          ;; Truncate the subject.
3013          (gnus-simplify-subject-functions
3014           (gnus-map-function gnus-simplify-subject-functions subject))
3015          ((numberp gnus-summary-gather-subject-limit)
3016           (setq subject (gnus-simplify-subject-re subject))
3017           (if (> (length subject) gnus-summary-gather-subject-limit)
3018               (substring subject 0 gnus-summary-gather-subject-limit)
3019             subject))
3020          ;; Fuzzily simplify it.
3021          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3022           (gnus-simplify-subject-fuzzy subject))
3023          ;; Just remove the leading "Re:".
3024          (t
3025           (gnus-simplify-subject-re subject))))
3026
3027   (if (and gnus-summary-gather-exclude-subject
3028            (string-match gnus-summary-gather-exclude-subject subject))
3029       nil                               ; This article shouldn't be gathered
3030     subject))
3031
3032 (defun gnus-summary-simplify-subject-query ()
3033   "Query where the respool algorithm would put this article."
3034   (interactive)
3035   (gnus-summary-select-article)
3036   (message "%s"
3037            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3038
3039 (defun gnus-gather-threads-by-subject (threads)
3040   "Gather threads by looking at Subject headers."
3041   (if (not gnus-summary-make-false-root)
3042       threads
3043     (let ((hashtb (gnus-make-hashtable 1024))
3044           (prev threads)
3045           (result threads)
3046           subject hthread whole-subject)
3047       (while threads
3048         (setq subject (gnus-general-simplify-subject
3049                        (setq whole-subject (mail-header-subject
3050                                             (caar threads)))))
3051         (when subject
3052           (if (setq hthread (gnus-gethash subject hashtb))
3053               (progn
3054                 ;; We enter a dummy root into the thread, if we
3055                 ;; haven't done that already.
3056                 (unless (stringp (caar hthread))
3057                   (setcar hthread (list whole-subject (car hthread))))
3058                 ;; We add this new gathered thread to this gathered
3059                 ;; thread.
3060                 (setcdr (car hthread)
3061                         (nconc (cdar hthread) (list (car threads))))
3062                 ;; Remove it from the list of threads.
3063                 (setcdr prev (cdr threads))
3064                 (setq threads prev))
3065             ;; Enter this thread into the hash table.
3066             (gnus-sethash subject threads hashtb)))
3067         (setq prev threads)
3068         (setq threads (cdr threads)))
3069       result)))
3070
3071 (defun gnus-gather-threads-by-references (threads)
3072   "Gather threads by looking at References headers."
3073   (let ((idhashtb (gnus-make-hashtable 1024))
3074         (thhashtb (gnus-make-hashtable 1024))
3075         (prev threads)
3076         (result threads)
3077         ids references id gthread gid entered ref)
3078     (while threads
3079       (when (setq references (mail-header-references (caar threads)))
3080         (setq id (mail-header-id (caar threads))
3081               ids (gnus-split-references references)
3082               entered nil)
3083         (while (setq ref (pop ids))
3084           (setq ids (delete ref ids))
3085           (if (not (setq gid (gnus-gethash ref idhashtb)))
3086               (progn
3087                 (gnus-sethash ref id idhashtb)
3088                 (gnus-sethash id threads thhashtb))
3089             (setq gthread (gnus-gethash gid thhashtb))
3090             (unless entered
3091               ;; We enter a dummy root into the thread, if we
3092               ;; haven't done that already.
3093               (unless (stringp (caar gthread))
3094                 (setcar gthread (list (mail-header-subject (caar gthread))
3095                                       (car gthread))))
3096               ;; We add this new gathered thread to this gathered
3097               ;; thread.
3098               (setcdr (car gthread)
3099                       (nconc (cdar gthread) (list (car threads)))))
3100             ;; Add it into the thread hash table.
3101             (gnus-sethash id gthread thhashtb)
3102             (setq entered t)
3103             ;; Remove it from the list of threads.
3104             (setcdr prev (cdr threads))
3105             (setq threads prev))))
3106       (setq prev threads)
3107       (setq threads (cdr threads)))
3108     result))
3109
3110 (defun gnus-sort-gathered-threads (threads)
3111   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3112   (let ((result threads))
3113     (while threads
3114       (when (stringp (caar threads))
3115         (setcdr (car threads)
3116                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3117       (setq threads (cdr threads)))
3118     result))
3119
3120 (defun gnus-thread-loop-p (root thread)
3121   "Say whether ROOT is in THREAD."
3122   (let ((stack (list thread))
3123         (infloop 0)
3124         th)
3125     (while (setq thread (pop stack))
3126       (setq th (cdr thread))
3127       (while (and th
3128                   (not (eq (caar th) root)))
3129         (pop th))
3130       (if th
3131           ;; We have found a loop.
3132           (let (ref-dep)
3133             (setcdr thread (delq (car th) (cdr thread)))
3134             (if (boundp (setq ref-dep (intern "none"
3135                                               gnus-newsgroup-dependencies)))
3136                 (setcdr (symbol-value ref-dep)
3137                         (nconc (cdr (symbol-value ref-dep))
3138                                (list (car th))))
3139               (set ref-dep (list nil (car th))))
3140             (setq infloop 1
3141                   stack nil))
3142         ;; Push all the subthreads onto the stack.
3143         (push (cdr thread) stack)))
3144     infloop))
3145
3146 (defun gnus-make-threads ()
3147   "Go through the dependency hashtb and find the roots.  Return all threads."
3148   (let (threads)
3149     (while (catch 'infloop
3150              (mapatoms
3151               (lambda (refs)
3152                 ;; Deal with self-referencing References loops.
3153                 (when (and (car (symbol-value refs))
3154                            (not (zerop
3155                                  (apply
3156                                   '+
3157                                   (mapcar
3158                                    (lambda (thread)
3159                                      (gnus-thread-loop-p
3160                                       (car (symbol-value refs)) thread))
3161                                    (cdr (symbol-value refs)))))))
3162                   (setq threads nil)
3163                   (throw 'infloop t))
3164                 (unless (car (symbol-value refs))
3165                   ;; These threads do not refer back to any other articles,
3166                   ;; so they're roots.
3167                   (setq threads (append (cdr (symbol-value refs)) threads))))
3168               gnus-newsgroup-dependencies)))
3169     threads))
3170
3171 ;; Build the thread tree.
3172 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3173   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3174
3175 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3176 if it was already present.
3177
3178 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3179 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3180 Message-IDs will be renamed be renamed to a unique Message-ID before
3181 being entered.
3182
3183 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3184   (let* ((id (mail-header-id header))
3185          (id-dep (and id (intern id dependencies)))
3186          ref ref-dep ref-header)
3187     ;; Enter this `header' in the `dependencies' table.
3188     (cond
3189      ((not id-dep)
3190       (setq header nil))
3191      ;; The first two cases do the normal part: enter a new `header'
3192      ;; in the `dependencies' table.
3193      ((not (boundp id-dep))
3194       (set id-dep (list header)))
3195      ((null (car (symbol-value id-dep)))
3196       (setcar (symbol-value id-dep) header))
3197
3198      ;; From here the `header' was already present in the
3199      ;; `dependencies' table.
3200      (force-new
3201       ;; Overrides an existing entry;
3202       ;; just set the header part of the entry.
3203       (setcar (symbol-value id-dep) header))
3204
3205      ;; Renames the existing `header' to a unique Message-ID.
3206      ((not gnus-summary-ignore-duplicates)
3207       ;; An article with this Message-ID has already been seen.
3208       ;; We rename the Message-ID.
3209       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3210            (list header))
3211       (mail-header-set-id header id))
3212
3213      ;; The last case ignores an existing entry, except it adds any
3214      ;; additional Xrefs (in case the two articles came from different
3215      ;; servers.
3216      ;; Also sets `header' to `nil' meaning that the `dependencies'
3217      ;; table was *not* modified.
3218      (t
3219       (mail-header-set-xref
3220        (car (symbol-value id-dep))
3221        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3222                    "")
3223                (or (mail-header-xref header) "")))
3224       (setq header nil)))
3225
3226     (when header
3227       ;; First check if that we are not creating a References loop.
3228       (setq ref (gnus-parent-id (mail-header-references header)))
3229       (while (and ref
3230                   (setq ref-dep (intern-soft ref dependencies))
3231                   (boundp ref-dep)
3232                   (setq ref-header (car (symbol-value ref-dep))))
3233         (if (string= id ref)
3234             ;; Yuk!  This is a reference loop.  Make the article be a
3235             ;; root article.
3236             (progn
3237               (mail-header-set-references (car (symbol-value id-dep)) "none")
3238               (setq ref nil))
3239           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3240       (setq ref (gnus-parent-id (mail-header-references header)))
3241       (setq ref-dep (intern (or ref "none") dependencies))
3242       (if (boundp ref-dep)
3243           (setcdr (symbol-value ref-dep)
3244                   (nconc (cdr (symbol-value ref-dep))
3245                          (list (symbol-value id-dep))))
3246         (set ref-dep (list nil (symbol-value id-dep)))))
3247     header))
3248
3249 (defun gnus-build-sparse-threads ()
3250   (let ((headers gnus-newsgroup-headers)
3251         (mail-parse-charset gnus-newsgroup-charset)
3252         (gnus-summary-ignore-duplicates t)
3253         header references generation relations
3254         subject child end new-child date)
3255     ;; First we create an alist of generations/relations, where
3256     ;; generations is how much we trust the relation, and the relation
3257     ;; is parent/child.
3258     (gnus-message 7 "Making sparse threads...")
3259     (save-excursion
3260       (nnheader-set-temp-buffer " *gnus sparse threads*")
3261       (while (setq header (pop headers))
3262         (when (and (setq references (mail-header-references header))
3263                    (not (string= references "")))
3264           (insert references)
3265           (setq child (mail-header-id header)
3266                 subject (mail-header-subject header)
3267                 date (mail-header-date header)
3268                 generation 0)
3269           (while (search-backward ">" nil t)
3270             (setq end (1+ (point)))
3271             (when (search-backward "<" nil t)
3272               (setq new-child (buffer-substring (point) end))
3273               (push (list (incf generation)
3274                           child (setq child new-child)
3275                           subject date)
3276                     relations)))
3277           (when child
3278             (push (list (1+ generation) child nil subject) relations))
3279           (erase-buffer)))
3280       (kill-buffer (current-buffer)))
3281     ;; Sort over trustworthiness.
3282     (mapcar
3283      (lambda (relation)
3284        (when (gnus-dependencies-add-header
3285               (make-full-mail-header
3286                gnus-reffed-article-number
3287                (nth 3 relation) "" (or (nth 4 relation) "")
3288                (nth 1 relation)
3289                (or (nth 2 relation) "") 0 0 "")
3290               gnus-newsgroup-dependencies nil)
3291          (push gnus-reffed-article-number gnus-newsgroup-limit)
3292          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3293          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3294                gnus-newsgroup-reads)
3295          (decf gnus-reffed-article-number)))
3296      (sort relations 'car-less-than-car))
3297     (gnus-message 7 "Making sparse threads...done")))
3298
3299 (defun gnus-build-old-threads ()
3300   ;; Look at all the articles that refer back to old articles, and
3301   ;; fetch the headers for the articles that aren't there.  This will
3302   ;; build complete threads - if the roots haven't been expired by the
3303   ;; server, that is.
3304   (let ((mail-parse-charset gnus-newsgroup-charset)
3305         id heads)
3306     (mapatoms
3307      (lambda (refs)
3308        (when (not (car (symbol-value refs)))
3309          (setq heads (cdr (symbol-value refs)))
3310          (while heads
3311            (if (memq (mail-header-number (caar heads))
3312                      gnus-newsgroup-dormant)
3313                (setq heads (cdr heads))
3314              (setq id (symbol-name refs))
3315              (while (and (setq id (gnus-build-get-header id))
3316                          (not (car (gnus-id-to-thread id)))))
3317              (setq heads nil)))))
3318      gnus-newsgroup-dependencies)))
3319
3320 ;; This function has to be called with point after the article number
3321 ;; on the beginning of the line.
3322 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3323   (let ((eol (gnus-point-at-eol))
3324         (buffer (current-buffer))
3325         header)
3326
3327     ;; overview: [num subject from date id refs chars lines misc]
3328     (unless (eobp)
3329       (forward-char))
3330
3331     (setq header
3332           (make-full-mail-header
3333            number                               ; number
3334            (nnheader-nov-field)                 ; subject
3335            (nnheader-nov-field)                 ; from
3336            (nnheader-nov-field)                 ; date
3337            (nnheader-nov-read-message-id)       ; id
3338            (nnheader-nov-field)                 ; refs
3339            (nnheader-nov-read-integer)          ; chars
3340            (nnheader-nov-read-integer)          ; lines
3341            (unless (eobp)
3342              (nnheader-nov-field))              ; misc
3343            (nnheader-nov-parse-extra)))         ; extra
3344
3345     (when gnus-alter-header-function
3346       (funcall gnus-alter-header-function header))
3347     (gnus-dependencies-add-header header dependencies force-new)))
3348
3349 (defun gnus-build-get-header (id)
3350   "Look through the buffer of NOV lines and find the header to ID.
3351 Enter this line into the dependencies hash table, and return
3352 the id of the parent article (if any)."
3353   (let ((deps gnus-newsgroup-dependencies)
3354         found header)
3355     (prog1
3356         (save-excursion
3357           (set-buffer nntp-server-buffer)
3358           (let ((case-fold-search nil))
3359             (goto-char (point-min))
3360             (while (and (not found)
3361                         (search-forward id nil t))
3362               (beginning-of-line)
3363               (setq found (looking-at
3364                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3365                                    (regexp-quote id))))
3366               (or found (beginning-of-line 2)))
3367             (when found
3368               (beginning-of-line)
3369               (and
3370                (setq header (gnus-nov-parse-line
3371                              (read (current-buffer)) deps))
3372                (gnus-parent-id (mail-header-references header))))))
3373       (when header
3374         (let ((number (mail-header-number header)))
3375           (push number gnus-newsgroup-limit)
3376           (push header gnus-newsgroup-headers)
3377           (if (memq number gnus-newsgroup-unselected)
3378               (progn
3379                 (push number gnus-newsgroup-unreads)
3380                 (setq gnus-newsgroup-unselected
3381                       (delq number gnus-newsgroup-unselected)))
3382             (push number gnus-newsgroup-ancient)))))))
3383
3384 (defun gnus-build-all-threads ()
3385   "Read all the headers."
3386   (let ((gnus-summary-ignore-duplicates t)
3387         (mail-parse-charset gnus-newsgroup-charset)
3388         (dependencies gnus-newsgroup-dependencies)
3389         header article)
3390     (save-excursion
3391       (set-buffer nntp-server-buffer)
3392       (let ((case-fold-search nil))
3393         (goto-char (point-min))
3394         (while (not (eobp))
3395           (ignore-errors
3396             (setq article (read (current-buffer))
3397                   header (gnus-nov-parse-line article dependencies)))
3398           (when header
3399             (save-excursion
3400               (set-buffer gnus-summary-buffer)
3401               (push header gnus-newsgroup-headers)
3402               (if (memq (setq article (mail-header-number header))
3403                         gnus-newsgroup-unselected)
3404                   (progn
3405                     (push article gnus-newsgroup-unreads)
3406                     (setq gnus-newsgroup-unselected
3407                           (delq article gnus-newsgroup-unselected)))
3408                 (push article gnus-newsgroup-ancient)))
3409             (forward-line 1)))))))
3410
3411 (defun gnus-summary-update-article-line (article header)
3412   "Update the line for ARTICLE using HEADERS."
3413   (let* ((id (mail-header-id header))
3414          (thread (gnus-id-to-thread id)))
3415     (unless thread
3416       (error "Article in no thread"))
3417     ;; Update the thread.
3418     (setcar thread header)
3419     (gnus-summary-goto-subject article)
3420     (let* ((datal (gnus-data-find-list article))
3421            (data (car datal))
3422            (length (when (cdr datal)
3423                      (- (gnus-data-pos data)
3424                         (gnus-data-pos (cadr datal)))))
3425            (buffer-read-only nil)
3426            (level (gnus-summary-thread-level)))
3427       (gnus-delete-line)
3428       (gnus-summary-insert-line
3429        header level nil (gnus-article-mark article)
3430        (memq article gnus-newsgroup-replied)
3431        (memq article gnus-newsgroup-expirable)
3432        ;; Only insert the Subject string when it's different
3433        ;; from the previous Subject string.
3434        (if (and
3435             gnus-show-threads
3436             (gnus-subject-equal
3437              (condition-case ()
3438                  (mail-header-subject
3439                   (gnus-data-header
3440                    (cadr
3441                     (gnus-data-find-list
3442                      article
3443                      (gnus-data-list t)))))
3444                ;; Error on the side of excessive subjects.
3445                (error ""))
3446              (mail-header-subject header)))
3447            ""
3448          (mail-header-subject header))
3449        nil (cdr (assq article gnus-newsgroup-scored))
3450        (memq article gnus-newsgroup-processable))
3451       (when length
3452         (gnus-data-update-list
3453          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3454
3455 (defun gnus-summary-update-article (article &optional iheader)
3456   "Update ARTICLE in the summary buffer."
3457   (set-buffer gnus-summary-buffer)
3458   (let* ((header (gnus-summary-article-header article))
3459          (id (mail-header-id header))
3460          (data (gnus-data-find article))
3461          (thread (gnus-id-to-thread id))
3462          (references (mail-header-references header))
3463          (parent
3464           (gnus-id-to-thread
3465            (or (gnus-parent-id
3466                 (when (and references
3467                            (not (equal "" references)))
3468                   references))
3469                "none")))
3470          (buffer-read-only nil)
3471          (old (car thread)))
3472     (when thread
3473       (unless iheader
3474         (setcar thread nil)
3475         (when parent
3476           (delq thread parent)))
3477       (if (gnus-summary-insert-subject id header)
3478           ;; Set the (possibly) new article number in the data structure.
3479           (gnus-data-set-number data (gnus-id-to-article id))
3480         (setcar thread old)
3481         nil))))
3482
3483 (defun gnus-rebuild-thread (id &optional line)
3484   "Rebuild the thread containing ID.
3485 If LINE, insert the rebuilt thread starting on line LINE."
3486   (let ((buffer-read-only nil)
3487         old-pos current thread data)
3488     (if (not gnus-show-threads)
3489         (setq thread (list (car (gnus-id-to-thread id))))
3490       ;; Get the thread this article is part of.
3491       (setq thread (gnus-remove-thread id)))
3492     (setq old-pos (gnus-point-at-bol))
3493     (setq current (save-excursion
3494                     (and (zerop (forward-line -1))
3495                          (gnus-summary-article-number))))
3496     ;; If this is a gathered thread, we have to go some re-gathering.
3497     (when (stringp (car thread))
3498       (let ((subject (car thread))
3499             roots thr)
3500         (setq thread (cdr thread))
3501         (while thread
3502           (unless (memq (setq thr (gnus-id-to-thread
3503                                    (gnus-root-id
3504                                     (mail-header-id (caar thread)))))
3505                         roots)
3506             (push thr roots))
3507           (setq thread (cdr thread)))
3508         ;; We now have all (unique) roots.
3509         (if (= (length roots) 1)
3510             ;; All the loose roots are now one solid root.
3511             (setq thread (car roots))
3512           (setq thread (cons subject (gnus-sort-threads roots))))))
3513     (let (threads)
3514       ;; We then insert this thread into the summary buffer.
3515       (when line
3516         (goto-char (point-min))
3517         (forward-line (1- line)))
3518       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3519         (if gnus-show-threads
3520             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3521           (gnus-summary-prepare-unthreaded thread))
3522         (setq data (nreverse gnus-newsgroup-data))
3523         (setq threads gnus-newsgroup-threads))
3524       ;; We splice the new data into the data structure.
3525       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3526       ;;!!! then we want to insert at the beginning of the buffer.
3527       ;;!!! That happens to be true with Gnus now, but that may
3528       ;;!!! change in the future.  Perhaps.
3529       (gnus-data-enter-list
3530        (if line nil current) data (- (point) old-pos))
3531       (setq gnus-newsgroup-threads
3532             (nconc threads gnus-newsgroup-threads))
3533       (gnus-data-compute-positions))))
3534
3535 (defun gnus-number-to-header (number)
3536   "Return the header for article NUMBER."
3537   (let ((headers gnus-newsgroup-headers))
3538     (while (and headers
3539                 (not (= number (mail-header-number (car headers)))))
3540       (pop headers))
3541     (when headers
3542       (car headers))))
3543
3544 (defun gnus-parent-headers (in-headers &optional generation)
3545   "Return the headers of the GENERATIONeth parent of HEADERS."
3546   (unless generation
3547     (setq generation 1))
3548   (let ((parent t)
3549         (headers in-headers)
3550         references)
3551     (while (and parent
3552                 (not (zerop generation))
3553                 (setq references (mail-header-references headers)))
3554       (setq headers (if (and references
3555                              (setq parent (gnus-parent-id references)))
3556                         (car (gnus-id-to-thread parent))
3557                       nil))
3558       (decf generation))
3559     (and (not (eq headers in-headers))
3560          headers)))
3561
3562 (defun gnus-id-to-thread (id)
3563   "Return the (sub-)thread where ID appears."
3564   (gnus-gethash id gnus-newsgroup-dependencies))
3565
3566 (defun gnus-id-to-article (id)
3567   "Return the article number of ID."
3568   (let ((thread (gnus-id-to-thread id)))
3569     (when (and thread
3570                (car thread))
3571       (mail-header-number (car thread)))))
3572
3573 (defun gnus-id-to-header (id)
3574   "Return the article headers of ID."
3575   (car (gnus-id-to-thread id)))
3576
3577 (defun gnus-article-displayed-root-p (article)
3578   "Say whether ARTICLE is a root(ish) article."
3579   (let ((level (gnus-summary-thread-level article))
3580         (refs (mail-header-references  (gnus-summary-article-header article)))
3581         particle)
3582     (cond
3583      ((null level) nil)
3584      ((zerop level) t)
3585      ((null refs) t)
3586      ((null (gnus-parent-id refs)) t)
3587      ((and (= 1 level)
3588            (null (setq particle (gnus-id-to-article
3589                                  (gnus-parent-id refs))))
3590            (null (gnus-summary-thread-level particle)))))))
3591
3592 (defun gnus-root-id (id)
3593   "Return the id of the root of the thread where ID appears."
3594   (let (last-id prev)
3595     (while (and id (setq prev (car (gnus-id-to-thread id))))
3596       (setq last-id id
3597             id (gnus-parent-id (mail-header-references prev))))
3598     last-id))
3599
3600 (defun gnus-articles-in-thread (thread)
3601   "Return the list of articles in THREAD."
3602   (cons (mail-header-number (car thread))
3603         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3604
3605 (defun gnus-remove-thread (id &optional dont-remove)
3606   "Remove the thread that has ID in it."
3607   (let (headers thread last-id)
3608     ;; First go up in this thread until we find the root.
3609     (setq last-id (gnus-root-id id)
3610           headers (message-flatten-list (gnus-id-to-thread last-id)))
3611     ;; We have now found the real root of this thread.  It might have
3612     ;; been gathered into some loose thread, so we have to search
3613     ;; through the threads to find the thread we wanted.
3614     (let ((threads gnus-newsgroup-threads)
3615           sub)
3616       (while threads
3617         (setq sub (car threads))
3618         (if (stringp (car sub))
3619             ;; This is a gathered thread, so we look at the roots
3620             ;; below it to find whether this article is in this
3621             ;; gathered root.
3622             (progn
3623               (setq sub (cdr sub))
3624               (while sub
3625                 (when (member (caar sub) headers)
3626                   (setq thread (car threads)
3627                         threads nil
3628                         sub nil))
3629                 (setq sub (cdr sub))))
3630           ;; It's an ordinary thread, so we check it.
3631           (when (eq (car sub) (car headers))
3632             (setq thread sub
3633                   threads nil)))
3634         (setq threads (cdr threads)))
3635       ;; If this article is in no thread, then it's a root.
3636       (if thread
3637           (unless dont-remove
3638             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3639         (setq thread (gnus-id-to-thread last-id)))
3640       (when thread
3641         (prog1
3642             thread                      ; We return this thread.
3643           (unless dont-remove
3644             (if (stringp (car thread))
3645                 (progn
3646                   ;; If we use dummy roots, then we have to remove the
3647                   ;; dummy root as well.
3648                   (when (eq gnus-summary-make-false-root 'dummy)
3649                     ;; We go to the dummy root by going to
3650                     ;; the first sub-"thread", and then one line up.
3651                     (gnus-summary-goto-article
3652                      (mail-header-number (caadr thread)))
3653                     (forward-line -1)
3654                     (gnus-delete-line)
3655                     (gnus-data-compute-positions))
3656                   (setq thread (cdr thread))
3657                   (while thread
3658                     (gnus-remove-thread-1 (car thread))
3659                     (setq thread (cdr thread))))
3660               (gnus-remove-thread-1 thread))))))))
3661
3662 (defun gnus-remove-thread-1 (thread)
3663   "Remove the thread THREAD recursively."
3664   (let ((number (mail-header-number (pop thread)))
3665         d)
3666     (setq thread (reverse thread))
3667     (while thread
3668       (gnus-remove-thread-1 (pop thread)))
3669     (when (setq d (gnus-data-find number))
3670       (goto-char (gnus-data-pos d))
3671       (gnus-summary-show-thread)
3672       (gnus-data-remove
3673        number
3674        (- (gnus-point-at-bol)
3675           (prog1
3676               (1+ (gnus-point-at-eol))
3677             (gnus-delete-line)))))))
3678
3679 (defun gnus-sort-threads-1 (threads func)
3680   (sort (mapcar (lambda (thread)
3681                   (cons (car thread)
3682                         (and (cdr thread)
3683                              (gnus-sort-threads-1 (cdr thread) func))))
3684                 threads) func))
3685
3686 (defun gnus-sort-threads (threads)
3687   "Sort THREADS."
3688   (if (not gnus-thread-sort-functions)
3689       threads
3690     (gnus-message 8 "Sorting threads...")
3691     (prog1
3692         (gnus-sort-threads-1 
3693          threads 
3694          (gnus-make-sort-function gnus-thread-sort-functions))
3695       (gnus-message 8 "Sorting threads...done"))))
3696
3697 (defun gnus-sort-articles (articles)
3698   "Sort ARTICLES."
3699   (when gnus-article-sort-functions
3700     (gnus-message 7 "Sorting articles...")
3701     (prog1
3702         (setq gnus-newsgroup-headers
3703               (sort articles (gnus-make-sort-function
3704                               gnus-article-sort-functions)))
3705       (gnus-message 7 "Sorting articles...done"))))
3706
3707 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3708 (defmacro gnus-thread-header (thread)
3709   "Return header of first article in THREAD.
3710 Note that THREAD must never, ever be anything else than a variable -
3711 using some other form will lead to serious barfage."
3712   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3713   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3714   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3715         (vector thread) 2))
3716
3717 (defsubst gnus-article-sort-by-number (h1 h2)
3718   "Sort articles by article number."
3719   (< (mail-header-number h1)
3720      (mail-header-number h2)))
3721
3722 (defun gnus-thread-sort-by-number (h1 h2)
3723   "Sort threads by root article number."
3724   (gnus-article-sort-by-number
3725    (gnus-thread-header h1) (gnus-thread-header h2)))
3726
3727 (defsubst gnus-article-sort-by-lines (h1 h2)
3728   "Sort articles by article Lines header."
3729   (< (mail-header-lines h1)
3730      (mail-header-lines h2)))
3731
3732 (defun gnus-thread-sort-by-lines (h1 h2)
3733   "Sort threads by root article Lines header."
3734   (gnus-article-sort-by-lines
3735    (gnus-thread-header h1) (gnus-thread-header h2)))
3736
3737 (defsubst gnus-article-sort-by-chars (h1 h2)
3738   "Sort articles by octet length."
3739   (< (mail-header-chars h1)
3740      (mail-header-chars h2)))
3741
3742 (defun gnus-thread-sort-by-chars (h1 h2)
3743   "Sort threads by root article octet length."
3744   (gnus-article-sort-by-chars
3745    (gnus-thread-header h1) (gnus-thread-header h2)))
3746
3747 (defsubst gnus-article-sort-by-author (h1 h2)
3748   "Sort articles by root author."
3749   (string-lessp
3750    (let ((addr (car (mime-entity-read-field h1 'From))))
3751      (or (std11-full-name-string addr)
3752          (std11-address-string addr)
3753          ""))
3754    (let ((addr (car (mime-entity-read-field h2 'From))))
3755      (or (std11-full-name-string addr)
3756          (std11-address-string addr)
3757          ""))
3758    ))
3759
3760 (defun gnus-thread-sort-by-author (h1 h2)
3761   "Sort threads by root author."
3762   (gnus-article-sort-by-author
3763    (gnus-thread-header h1)  (gnus-thread-header h2)))
3764
3765 (defsubst gnus-article-sort-by-subject (h1 h2)
3766   "Sort articles by root subject."
3767   (string-lessp
3768    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3769    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3770
3771 (defun gnus-thread-sort-by-subject (h1 h2)
3772   "Sort threads by root subject."
3773   (gnus-article-sort-by-subject
3774    (gnus-thread-header h1) (gnus-thread-header h2)))
3775
3776 (defsubst gnus-article-sort-by-date (h1 h2)
3777   "Sort articles by root article date."
3778   (time-less-p
3779    (gnus-date-get-time (mail-header-date h1))
3780    (gnus-date-get-time (mail-header-date h2))))
3781
3782 (defun gnus-thread-sort-by-date (h1 h2)
3783   "Sort threads by root article date."
3784   (gnus-article-sort-by-date
3785    (gnus-thread-header h1) (gnus-thread-header h2)))
3786
3787 (defsubst gnus-article-sort-by-score (h1 h2)
3788   "Sort articles by root article score.
3789 Unscored articles will be counted as having a score of zero."
3790   (> (or (cdr (assq (mail-header-number h1)
3791                     gnus-newsgroup-scored))
3792          gnus-summary-default-score 0)
3793      (or (cdr (assq (mail-header-number h2)
3794                     gnus-newsgroup-scored))
3795          gnus-summary-default-score 0)))
3796
3797 (defun gnus-thread-sort-by-score (h1 h2)
3798   "Sort threads by root article score."
3799   (gnus-article-sort-by-score
3800    (gnus-thread-header h1) (gnus-thread-header h2)))
3801
3802 (defun gnus-thread-sort-by-total-score (h1 h2)
3803   "Sort threads by the sum of all scores in the thread.
3804 Unscored articles will be counted as having a score of zero."
3805   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3806
3807 (defun gnus-thread-total-score (thread)
3808   ;; This function find the total score of THREAD.
3809   (cond ((null thread)
3810          0)
3811         ((consp thread)
3812          (if (stringp (car thread))
3813              (apply gnus-thread-score-function 0
3814                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3815            (gnus-thread-total-score-1 thread)))
3816         (t
3817          (gnus-thread-total-score-1 (list thread)))))
3818
3819 (defun gnus-thread-total-score-1 (root)
3820   ;; This function find the total score of the thread below ROOT.
3821   (setq root (car root))
3822   (apply gnus-thread-score-function
3823          (or (append
3824               (mapcar 'gnus-thread-total-score
3825                       (cdr (gnus-id-to-thread (mail-header-id root))))
3826               (when (> (mail-header-number root) 0)
3827                 (list (or (cdr (assq (mail-header-number root)
3828                                      gnus-newsgroup-scored))
3829                           gnus-summary-default-score 0))))
3830              (list gnus-summary-default-score)
3831              '(0))))
3832
3833 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3834 (defvar gnus-tmp-prev-subject nil)
3835 (defvar gnus-tmp-false-parent nil)
3836 (defvar gnus-tmp-root-expunged nil)
3837 (defvar gnus-tmp-dummy-line nil)
3838
3839 (defvar gnus-tmp-header)
3840 (defun gnus-extra-header (type &optional header)
3841   "Return the extra header of TYPE."
3842   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3843       ""))
3844
3845 (defun gnus-summary-prepare-threads (threads)
3846   "Prepare summary buffer from THREADS and indentation LEVEL.
3847 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3848 or a straight list of headers."
3849   (gnus-message 7 "Generating summary...")
3850
3851   (setq gnus-newsgroup-threads threads)
3852   (beginning-of-line)
3853
3854   (let ((gnus-tmp-level 0)
3855         (default-score (or gnus-summary-default-score 0))
3856         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3857         thread number subject stack state gnus-tmp-gathered beg-match
3858         new-roots gnus-tmp-new-adopts thread-end
3859         gnus-tmp-header gnus-tmp-unread
3860         gnus-tmp-replied gnus-tmp-subject-or-nil
3861         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3862         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3863         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3864
3865     (setq gnus-tmp-prev-subject nil)
3866
3867     (if (vectorp (car threads))
3868         ;; If this is a straight (sic) list of headers, then a
3869         ;; threaded summary display isn't required, so we just create
3870         ;; an unthreaded one.
3871         (gnus-summary-prepare-unthreaded threads)
3872
3873       ;; Do the threaded display.
3874
3875       (while (or threads stack gnus-tmp-new-adopts new-roots)
3876
3877         (if (and (= gnus-tmp-level 0)
3878                  (or (not stack)
3879                      (= (caar stack) 0))
3880                  (not gnus-tmp-false-parent)
3881                  (or gnus-tmp-new-adopts new-roots))
3882             (if gnus-tmp-new-adopts
3883                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3884                       thread (list (car gnus-tmp-new-adopts))
3885                       gnus-tmp-header (caar thread)
3886                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3887               (when new-roots
3888                 (setq thread (list (car new-roots))
3889                       gnus-tmp-header (caar thread)
3890                       new-roots (cdr new-roots))))
3891
3892           (if threads
3893               ;; If there are some threads, we do them before the
3894               ;; threads on the stack.
3895               (setq thread threads
3896                     gnus-tmp-header (caar thread))
3897             ;; There were no current threads, so we pop something off
3898             ;; the stack.
3899             (setq state (car stack)
3900                   gnus-tmp-level (car state)
3901                   thread (cdr state)
3902                   stack (cdr stack)
3903                   gnus-tmp-header (caar thread))))
3904
3905         (setq gnus-tmp-false-parent nil)
3906         (setq gnus-tmp-root-expunged nil)
3907         (setq thread-end nil)
3908
3909         (if (stringp gnus-tmp-header)
3910             ;; The header is a dummy root.
3911             (cond
3912              ((eq gnus-summary-make-false-root 'adopt)
3913               ;; We let the first article adopt the rest.
3914               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3915                                                (cddar thread)))
3916               (setq gnus-tmp-gathered
3917                     (nconc (mapcar
3918                             (lambda (h) (mail-header-number (car h)))
3919                             (cddar thread))
3920                            gnus-tmp-gathered))
3921               (setq thread (cons (list (caar thread)
3922                                        (cadar thread))
3923                                  (cdr thread)))
3924               (setq gnus-tmp-level -1
3925                     gnus-tmp-false-parent t))
3926              ((eq gnus-summary-make-false-root 'empty)
3927               ;; We print adopted articles with empty subject fields.
3928               (setq gnus-tmp-gathered
3929                     (nconc (mapcar
3930                             (lambda (h) (mail-header-number (car h)))
3931                             (cddar thread))
3932                            gnus-tmp-gathered))
3933               (setq gnus-tmp-level -1))
3934              ((eq gnus-summary-make-false-root 'dummy)
3935               ;; We remember that we probably want to output a dummy
3936               ;; root.
3937               (setq gnus-tmp-dummy-line gnus-tmp-header)
3938               (setq gnus-tmp-prev-subject gnus-tmp-header))
3939              (t
3940               ;; We do not make a root for the gathered
3941               ;; sub-threads at all.
3942               (setq gnus-tmp-level -1)))
3943
3944           (setq number (mail-header-number gnus-tmp-header)
3945                 subject (mail-header-subject gnus-tmp-header))
3946
3947           (cond
3948            ;; If the thread has changed subject, we might want to make
3949            ;; this subthread into a root.
3950            ((and (null gnus-thread-ignore-subject)
3951                  (not (zerop gnus-tmp-level))
3952                  gnus-tmp-prev-subject
3953                  (not (inline
3954                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3955             (setq new-roots (nconc new-roots (list (car thread)))
3956                   thread-end t
3957                   gnus-tmp-header nil))
3958            ;; If the article lies outside the current limit,
3959            ;; then we do not display it.
3960            ((not (memq number gnus-newsgroup-limit))
3961             (setq gnus-tmp-gathered
3962                   (nconc (mapcar
3963                           (lambda (h) (mail-header-number (car h)))
3964                           (cdar thread))
3965                          gnus-tmp-gathered))
3966             (setq gnus-tmp-new-adopts (if (cdar thread)
3967                                           (append gnus-tmp-new-adopts
3968                                                   (cdar thread))
3969                                         gnus-tmp-new-adopts)
3970                   thread-end t
3971                   gnus-tmp-header nil)
3972             (when (zerop gnus-tmp-level)
3973               (setq gnus-tmp-root-expunged t)))
3974            ;; Perhaps this article is to be marked as read?
3975            ((and gnus-summary-mark-below
3976                  (< (or (cdr (assq number gnus-newsgroup-scored))
3977                         default-score)
3978                     gnus-summary-mark-below)
3979                  ;; Don't touch sparse articles.
3980                  (not (gnus-summary-article-sparse-p number))
3981                  (not (gnus-summary-article-ancient-p number)))
3982             (setq gnus-newsgroup-unreads
3983                   (delq number gnus-newsgroup-unreads))
3984             (if gnus-newsgroup-auto-expire
3985                 (push number gnus-newsgroup-expirable)
3986               (push (cons number gnus-low-score-mark)
3987                     gnus-newsgroup-reads))))
3988
3989           (when gnus-tmp-header
3990             ;; We may have an old dummy line to output before this
3991             ;; article.
3992             (when (and gnus-tmp-dummy-line
3993                        (gnus-subject-equal
3994                         gnus-tmp-dummy-line
3995                         (mail-header-subject gnus-tmp-header)))
3996               (gnus-summary-insert-dummy-line
3997                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3998               (setq gnus-tmp-dummy-line nil))
3999
4000             ;; Compute the mark.
4001             (setq gnus-tmp-unread (gnus-article-mark number))
4002
4003             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4004                                   gnus-tmp-header gnus-tmp-level)
4005                   gnus-newsgroup-data)
4006
4007             ;; Actually insert the line.
4008             (setq
4009              gnus-tmp-subject-or-nil
4010              (cond
4011               ((and gnus-thread-ignore-subject
4012                     gnus-tmp-prev-subject
4013                     (not (inline (gnus-subject-equal
4014                                   gnus-tmp-prev-subject subject))))
4015                subject)
4016               ((zerop gnus-tmp-level)
4017                (if (and (eq gnus-summary-make-false-root 'empty)
4018                         (memq number gnus-tmp-gathered)
4019                         gnus-tmp-prev-subject
4020                         (inline (gnus-subject-equal
4021                                  gnus-tmp-prev-subject subject)))
4022                    gnus-summary-same-subject
4023                  subject))
4024               (t gnus-summary-same-subject)))
4025             (if (and (eq gnus-summary-make-false-root 'adopt)
4026                      (= gnus-tmp-level 1)
4027                      (memq number gnus-tmp-gathered))
4028                 (setq gnus-tmp-opening-bracket ?\<
4029                       gnus-tmp-closing-bracket ?\>)
4030               (setq gnus-tmp-opening-bracket ?\[
4031                     gnus-tmp-closing-bracket ?\]))
4032             (setq
4033              gnus-tmp-indentation
4034              (aref gnus-thread-indent-array gnus-tmp-level)
4035              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4036              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4037                                 gnus-summary-default-score 0)
4038              gnus-tmp-score-char
4039              (if (or (null gnus-summary-default-score)
4040                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4041                          gnus-summary-zcore-fuzz))
4042                  ?  ;Whitespace
4043                (if (< gnus-tmp-score gnus-summary-default-score)
4044                    gnus-score-below-mark gnus-score-over-mark))
4045              gnus-tmp-replied
4046              (cond ((memq number gnus-newsgroup-processable)
4047                     gnus-process-mark)
4048                    ((memq number gnus-newsgroup-cached)
4049                     gnus-cached-mark)
4050                    ((memq number gnus-newsgroup-replied)
4051                     gnus-replied-mark)
4052                    ((memq number gnus-newsgroup-saved)
4053                     gnus-saved-mark)
4054                    (t gnus-unread-mark))
4055              gnus-tmp-from (mail-header-from gnus-tmp-header)
4056              gnus-tmp-name
4057              (cond
4058               ((string-match "<[^>]+> *$" gnus-tmp-from)
4059                (setq beg-match (match-beginning 0))
4060                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4061                         (substring gnus-tmp-from (1+ (match-beginning 0))
4062                                    (1- (match-end 0))))
4063                    (substring gnus-tmp-from 0 beg-match)))
4064               ((string-match "(.+)" gnus-tmp-from)
4065                (substring gnus-tmp-from
4066                           (1+ (match-beginning 0)) (1- (match-end 0))))
4067               (t gnus-tmp-from)))
4068             (when (string= gnus-tmp-name "")
4069               (setq gnus-tmp-name gnus-tmp-from))
4070             (unless (numberp gnus-tmp-lines)
4071               (setq gnus-tmp-lines 0))
4072             (gnus-put-text-property
4073              (point)
4074              (progn (eval gnus-summary-line-format-spec) (point))
4075              'gnus-number number)
4076             (when gnus-visual-p
4077               (forward-line -1)
4078               (gnus-run-hooks 'gnus-summary-update-hook)
4079               (forward-line 1))
4080
4081             (setq gnus-tmp-prev-subject subject)))
4082
4083         (when (nth 1 thread)
4084           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4085         (incf gnus-tmp-level)
4086         (setq threads (if thread-end nil (cdar thread)))
4087         (unless threads
4088           (setq gnus-tmp-level 0)))))
4089   (gnus-message 7 "Generating summary...done"))
4090
4091 (defun gnus-summary-prepare-unthreaded (headers)
4092   "Generate an unthreaded summary buffer based on HEADERS."
4093   (let (header number mark)
4094
4095     (beginning-of-line)
4096
4097     (while headers
4098       ;; We may have to root out some bad articles...
4099       (when (memq (setq number (mail-header-number
4100                                 (setq header (pop headers))))
4101                   gnus-newsgroup-limit)
4102         ;; Mark article as read when it has a low score.
4103         (when (and gnus-summary-mark-below
4104                    (< (or (cdr (assq number gnus-newsgroup-scored))
4105                           gnus-summary-default-score 0)
4106                       gnus-summary-mark-below)
4107                    (not (gnus-summary-article-ancient-p number)))
4108           (setq gnus-newsgroup-unreads
4109                 (delq number gnus-newsgroup-unreads))
4110           (if gnus-newsgroup-auto-expire
4111               (push number gnus-newsgroup-expirable)
4112             (push (cons number gnus-low-score-mark)
4113                   gnus-newsgroup-reads)))
4114
4115         (setq mark (gnus-article-mark number))
4116         (push (gnus-data-make number mark (1+ (point)) header 0)
4117               gnus-newsgroup-data)
4118         (gnus-summary-insert-line
4119          header 0 number
4120          mark (memq number gnus-newsgroup-replied)
4121          (memq number gnus-newsgroup-expirable)
4122          (mail-header-subject header) nil
4123          (cdr (assq number gnus-newsgroup-scored))
4124          (memq number gnus-newsgroup-processable))))))
4125
4126 (defun gnus-summary-remove-list-identifiers ()
4127   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4128   (let ((regexp (if (stringp gnus-list-identifiers)
4129                     gnus-list-identifiers
4130                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4131     (dolist (header gnus-newsgroup-headers)
4132       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4133                                   " *\\)\\)+\\(Re: +\\)?\\)")
4134                           (mail-header-subject header))
4135         (mail-header-set-subject
4136          header (concat (substring (mail-header-subject header)
4137                                    0 (match-beginning 1))
4138                         (or
4139                          (match-string 3 (mail-header-subject header))
4140                          (match-string 5 (mail-header-subject header)))
4141                         (substring (mail-header-subject header)
4142                                    (match-end 1))))))))
4143
4144 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4145   "Select newsgroup GROUP.
4146 If READ-ALL is non-nil, all articles in the group are selected.
4147 If SELECT-ARTICLES, only select those articles from GROUP."
4148   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4149          ;;!!! Dirty hack; should be removed.
4150          (gnus-summary-ignore-duplicates
4151           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4152               t
4153             gnus-summary-ignore-duplicates))
4154          (info (nth 2 entry))
4155          articles fetched-articles cached)
4156
4157     (unless (gnus-check-server
4158              (setq gnus-current-select-method
4159                    (gnus-find-method-for-group group)))
4160       (error "Couldn't open server"))
4161
4162     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4163         (gnus-activate-group group)     ; Or we can activate it...
4164         (progn                          ; Or we bug out.
4165           (when (equal major-mode 'gnus-summary-mode)
4166             (kill-buffer (current-buffer)))
4167           (error "Couldn't request group %s: %s"
4168                  group (gnus-status-message group))))
4169
4170     (unless (gnus-request-group group t)
4171       (when (equal major-mode 'gnus-summary-mode)
4172         (kill-buffer (current-buffer)))
4173       (error "Couldn't request group %s: %s"
4174              group (gnus-status-message group)))
4175
4176     (setq gnus-newsgroup-name group)
4177     (setq gnus-newsgroup-unselected nil)
4178     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4179     (gnus-summary-setup-default-charset)
4180
4181     ;; Adjust and set lists of article marks.
4182     (when info
4183       (gnus-adjust-marked-articles info))
4184
4185     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4186     (setq cached
4187           (if (gnus-virtual-group-p group)
4188               gnus-newsgroup-cached
4189             (gnus-cache-articles-in-group group)))
4190
4191     (setq gnus-newsgroup-unreads
4192           (gnus-set-difference
4193            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4194            gnus-newsgroup-dormant))
4195
4196     (setq gnus-newsgroup-processable nil)
4197
4198     (gnus-update-read-articles group gnus-newsgroup-unreads)
4199
4200     (if (setq articles select-articles)
4201         (setq gnus-newsgroup-unselected
4202               (gnus-sorted-intersection
4203                gnus-newsgroup-unreads
4204                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4205       (setq articles (gnus-articles-to-read group read-all)))
4206
4207     (cond
4208      ((null articles)
4209       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4210       'quit)
4211      ((eq articles 0) nil)
4212      (t
4213       ;; Init the dependencies hash table.
4214       (setq gnus-newsgroup-dependencies
4215             (gnus-make-hashtable (length articles)))
4216       (gnus-set-global-variables)
4217       ;; Retrieve the headers and read them in.
4218       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4219       (setq gnus-newsgroup-headers
4220             (gnus-retrieve-parsed-headers
4221              articles gnus-newsgroup-name
4222              ;; We might want to fetch old headers, but
4223              ;; not if there is only 1 article.
4224              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4225                            (not (numberp gnus-fetch-old-headers)))
4226                       (> (length articles) 1))
4227                   gnus-fetch-old-headers)))
4228       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4229
4230       ;; Suppress duplicates?
4231       (when gnus-suppress-duplicates
4232         (gnus-dup-suppress-articles))
4233
4234       ;; Set the initial limit.
4235       (setq gnus-newsgroup-limit (copy-sequence articles))
4236       ;; Remove canceled articles from the list of unread articles.
4237       (setq gnus-newsgroup-unreads
4238             (gnus-set-sorted-intersection
4239              gnus-newsgroup-unreads
4240              (setq fetched-articles
4241                    (mapcar (lambda (headers) (mail-header-number headers))
4242                            gnus-newsgroup-headers))))
4243       ;; Removed marked articles that do not exist.
4244       (gnus-update-missing-marks
4245        (gnus-sorted-complement fetched-articles articles))
4246
4247       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4248       (when cached
4249         (setq gnus-newsgroup-cached cached))
4250
4251       ;; We might want to build some more threads first.
4252       (when (and gnus-fetch-old-headers
4253                  (eq gnus-headers-retrieved-by 'nov))
4254         (if (eq gnus-fetch-old-headers 'invisible)
4255             (gnus-build-all-threads)
4256           (gnus-build-old-threads)))
4257       ;; Let the Gnus agent mark articles as read.
4258       (when gnus-agent
4259         (gnus-agent-get-undownloaded-list))
4260       ;; Remove list identifiers from subject
4261       (when gnus-list-identifiers
4262         (gnus-summary-remove-list-identifiers))
4263       ;; Check whether auto-expire is to be done in this group.
4264       (setq gnus-newsgroup-auto-expire
4265             (gnus-group-auto-expirable-p group))
4266       ;; Set up the article buffer now, if necessary.
4267       (unless gnus-single-article-buffer
4268         (gnus-article-setup-buffer))
4269       ;; First and last article in this newsgroup.
4270       (when gnus-newsgroup-headers
4271         (setq gnus-newsgroup-begin
4272               (mail-header-number (car gnus-newsgroup-headers))
4273               gnus-newsgroup-end
4274               (mail-header-number
4275                (gnus-last-element gnus-newsgroup-headers))))
4276       ;; GROUP is successfully selected.
4277       (or gnus-newsgroup-headers t)))))
4278
4279 (defun gnus-articles-to-read (group &optional read-all)
4280   "Find out what articles the user wants to read."
4281   (let* ((articles
4282           ;; Select all articles if `read-all' is non-nil, or if there
4283           ;; are no unread articles.
4284           (if (or read-all
4285                   (and (zerop (length gnus-newsgroup-marked))
4286                        (zerop (length gnus-newsgroup-unreads)))
4287                   (eq (gnus-group-find-parameter group 'display)
4288                       'all))
4289               (or
4290                (gnus-uncompress-range (gnus-active group))
4291                (gnus-cache-articles-in-group group))
4292             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4293                           (copy-sequence gnus-newsgroup-unreads))
4294                   '<)))
4295          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4296          (scored (length scored-list))
4297          (number (length articles))
4298          (marked (+ (length gnus-newsgroup-marked)
4299                     (length gnus-newsgroup-dormant)))
4300          (select
4301           (cond
4302            ((numberp read-all)
4303             read-all)
4304            (t
4305             (condition-case ()
4306                 (cond
4307                  ((and (or (<= scored marked) (= scored number))
4308                        (natnump gnus-large-newsgroup)
4309                        (> number gnus-large-newsgroup))
4310                   (let* ((cursor-in-echo-area nil)
4311                          (input (read-from-minibuffer
4312                                  (format
4313                                   "How many articles from %s (max %d): "
4314                                   (gnus-limit-string gnus-newsgroup-name 35)
4315                                   number)
4316                                  (cons (number-to-string gnus-large-newsgroup)
4317                                        0))))
4318                     (if (string-match "^[ \t]*$" input)
4319                         number
4320                       input)))
4321                  ((and (> scored marked) (< scored number)
4322                        (> (- scored number) 20))
4323                   (let ((input
4324                          (read-string
4325                           (format "%s %s (%d scored, %d total): "
4326                                   "How many articles from"
4327                                   group scored number))))
4328                     (if (string-match "^[ \t]*$" input)
4329                         number input)))
4330                  (t number))
4331               (quit nil))))))
4332     (setq select (if (stringp select) (string-to-number select) select))
4333     (if (or (null select) (zerop select))
4334         select
4335       (if (and (not (zerop scored)) (<= (abs select) scored))
4336           (progn
4337             (setq articles (sort scored-list '<))
4338             (setq number (length articles)))
4339         (setq articles (copy-sequence articles)))
4340
4341       (when (< (abs select) number)
4342         (if (< select 0)
4343             ;; Select the N oldest articles.
4344             (setcdr (nthcdr (1- (abs select)) articles) nil)
4345           ;; Select the N most recent articles.
4346           (setq articles (nthcdr (- number select) articles))))
4347       (setq gnus-newsgroup-unselected
4348             (gnus-sorted-intersection
4349              gnus-newsgroup-unreads
4350              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4351       (when gnus-alter-articles-to-read-function
4352         (setq gnus-newsgroup-unreads
4353               (sort
4354                (funcall gnus-alter-articles-to-read-function
4355                         gnus-newsgroup-name gnus-newsgroup-unreads)
4356                '<)))
4357       articles)))
4358
4359 (defun gnus-killed-articles (killed articles)
4360   (let (out)
4361     (while articles
4362       (when (inline (gnus-member-of-range (car articles) killed))
4363         (push (car articles) out))
4364       (setq articles (cdr articles)))
4365     out))
4366
4367 (defun gnus-uncompress-marks (marks)
4368   "Uncompress the mark ranges in MARKS."
4369   (let ((uncompressed '(score bookmark))
4370         out)
4371     (while marks
4372       (if (memq (caar marks) uncompressed)
4373           (push (car marks) out)
4374         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4375       (setq marks (cdr marks)))
4376     out))
4377
4378 (defun gnus-adjust-marked-articles (info)
4379   "Set all article lists and remove all marks that are no longer valid."
4380   (let* ((marked-lists (gnus-info-marks info))
4381          (active (gnus-active (gnus-info-group info)))
4382          (min (car active))
4383          (max (cdr active))
4384          (types gnus-article-mark-lists)
4385          (uncompressed '(score bookmark killed))
4386          marks var articles article mark)
4387
4388     (while marked-lists
4389       (setq marks (pop marked-lists))
4390       (set (setq var (intern (format "gnus-newsgroup-%s"
4391                                      (car (rassq (setq mark (car marks))
4392                                                  types)))))
4393            (if (memq (car marks) uncompressed) (cdr marks)
4394              (gnus-uncompress-range (cdr marks))))
4395
4396       (setq articles (symbol-value var))
4397
4398       ;; All articles have to be subsets of the active articles.
4399       (cond
4400        ;; Adjust "simple" lists.
4401        ((memq mark '(tick dormant expire reply save))
4402         (while articles
4403           (when (or (< (setq article (pop articles)) min) (> article max))
4404             (set var (delq article (symbol-value var))))))
4405        ;; Adjust assocs.
4406        ((memq mark uncompressed)
4407         (when (not (listp (cdr (symbol-value var))))
4408           (set var (list (symbol-value var))))
4409         (when (not (listp (cdr articles)))
4410           (setq articles (list articles)))
4411         (while articles
4412           (when (or (not (consp (setq article (pop articles))))
4413                     (< (car article) min)
4414                     (> (car article) max))
4415             (set var (delq article (symbol-value var))))))))))
4416
4417 (defun gnus-update-missing-marks (missing)
4418   "Go through the list of MISSING articles and remove them from the mark lists."
4419   (when missing
4420     (let ((types gnus-article-mark-lists)
4421           var m)
4422       ;; Go through all types.
4423       (while types
4424         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4425         (when (symbol-value var)
4426           ;; This list has articles.  So we delete all missing articles
4427           ;; from it.
4428           (setq m missing)
4429           (while m
4430             (set var (delq (pop m) (symbol-value var)))))))))
4431
4432 (defun gnus-update-marks ()
4433   "Enter the various lists of marked articles into the newsgroup info list."
4434   (let ((types gnus-article-mark-lists)
4435         (info (gnus-get-info gnus-newsgroup-name))
4436         (uncompressed '(score bookmark killed))
4437         type list newmarked symbol delta-marks)
4438     (when info
4439       ;; Add all marks lists to the list of marks lists.
4440       (while (setq type (pop types))
4441         (setq list (symbol-value
4442                     (setq symbol
4443                           (intern (format "gnus-newsgroup-%s"
4444                                           (car type))))))
4445
4446         (when list
4447           ;; Get rid of the entries of the articles that have the
4448           ;; default score.
4449           (when (and (eq (cdr type) 'score)
4450                      gnus-save-score
4451                      list)
4452             (let* ((arts list)
4453                    (prev (cons nil list))
4454                    (all prev))
4455               (while arts
4456                 (if (or (not (consp (car arts)))
4457                         (= (cdar arts) gnus-summary-default-score))
4458                     (setcdr prev (cdr arts))
4459                   (setq prev arts))
4460                 (setq arts (cdr arts)))
4461               (setq list (cdr all)))))
4462
4463         (unless (memq (cdr type) uncompressed)
4464           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4465
4466         (when (gnus-check-backend-function
4467                'request-set-mark gnus-newsgroup-name)
4468           ;; propagate flags to server, with the following exceptions:
4469           ;; uncompressed:s are not proper flags (they are cons cells)
4470           ;; cache is a internal gnus flag
4471           ;; download are local to one gnus installation (well)
4472           ;; unsend are for nndraft groups only
4473           ;; xxx: generality of this?  this suits nnimap anyway
4474           (unless (memq (cdr type) (append '(cache download unsend)
4475                                            uncompressed))
4476             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4477                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4478                    (add (gnus-remove-from-range
4479                          (gnus-copy-sequence list) old)))
4480               (when add
4481                 (push (list add 'add (list (cdr type))) delta-marks))
4482               (when del
4483                 (push (list del 'del (list (cdr type))) delta-marks)))))
4484
4485         (when list
4486           (push (cons (cdr type) list) newmarked)))
4487
4488       (when delta-marks
4489         (unless (gnus-check-group gnus-newsgroup-name)
4490           (error "Can't open server for %s" gnus-newsgroup-name))
4491         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4492
4493       ;; Enter these new marks into the info of the group.
4494       (if (nthcdr 3 info)
4495           (setcar (nthcdr 3 info) newmarked)
4496         ;; Add the marks lists to the end of the info.
4497         (when newmarked
4498           (setcdr (nthcdr 2 info) (list newmarked))))
4499
4500       ;; Cut off the end of the info if there's nothing else there.
4501       (let ((i 5))
4502         (while (and (> i 2)
4503                     (not (nth i info)))
4504           (when (nthcdr (decf i) info)
4505             (setcdr (nthcdr i info) nil)))))))
4506
4507 (defun gnus-set-mode-line (where)
4508   "Set the mode line of the article or summary buffers.
4509 If WHERE is `summary', the summary mode line format will be used."
4510   ;; Is this mode line one we keep updated?
4511   (when (and (memq where gnus-updated-mode-lines)
4512              (symbol-value
4513               (intern (format "gnus-%s-mode-line-format-spec" where))))
4514     (let (mode-string)
4515       (save-excursion
4516         ;; We evaluate this in the summary buffer since these
4517         ;; variables are buffer-local to that buffer.
4518         (set-buffer gnus-summary-buffer)
4519         ;; We bind all these variables that are used in the `eval' form
4520         ;; below.
4521         (let* ((mformat (symbol-value
4522                          (intern
4523                           (format "gnus-%s-mode-line-format-spec" where))))
4524                (gnus-tmp-group-name (gnus-group-name-decode
4525                                      gnus-newsgroup-name
4526                                      (gnus-group-name-charset
4527                                       nil
4528                                       gnus-newsgroup-name)))
4529                (gnus-tmp-article-number (or gnus-current-article 0))
4530                (gnus-tmp-unread gnus-newsgroup-unreads)
4531                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4532                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4533                (gnus-tmp-unread-and-unselected
4534                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4535                             (zerop gnus-tmp-unselected))
4536                        "")
4537                       ((zerop gnus-tmp-unselected)
4538                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4539                       (t (format "{%d(+%d) more}"
4540                                  gnus-tmp-unread-and-unticked
4541                                  gnus-tmp-unselected))))
4542                (gnus-tmp-subject
4543                 (if (and gnus-current-headers
4544                          (vectorp gnus-current-headers))
4545                     (gnus-mode-string-quote
4546                      (mail-header-subject gnus-current-headers))
4547                   ""))
4548                bufname-length max-len
4549                gnus-tmp-header);; passed as argument to any user-format-funcs
4550           (setq mode-string (eval mformat))
4551           (setq bufname-length (if (string-match "%b" mode-string)
4552                                    (- (length
4553                                        (buffer-name
4554                                         (if (eq where 'summary)
4555                                             nil
4556                                           (get-buffer gnus-article-buffer))))
4557                                       2)
4558                                  0))
4559           (setq max-len (max 4 (if gnus-mode-non-string-length
4560                                    (- (window-width)
4561                                       gnus-mode-non-string-length
4562                                       bufname-length)
4563                                  (length mode-string))))
4564           ;; We might have to chop a bit of the string off...
4565           (when (> (length mode-string) max-len)
4566             (setq mode-string
4567                   (concat (gnus-truncate-string mode-string (- max-len 3))
4568                           "...")))
4569           ;; Pad the mode string a bit.
4570           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4571       ;; Update the mode line.
4572       (setq mode-line-buffer-identification
4573             (gnus-mode-line-buffer-identification (list mode-string)))
4574       (set-buffer-modified-p t))))
4575
4576 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4577   "Go through the HEADERS list and add all Xrefs to a hash table.
4578 The resulting hash table is returned, or nil if no Xrefs were found."
4579   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4580          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4581          (xref-hashtb (gnus-make-hashtable))
4582          start group entry number xrefs header)
4583     (while headers
4584       (setq header (pop headers))
4585       (when (and (setq xrefs (mail-header-xref header))
4586                  (not (memq (setq number (mail-header-number header))
4587                             unreads)))
4588         (setq start 0)
4589         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4590           (setq start (match-end 0))
4591           (setq group (if prefix
4592                           (concat prefix (substring xrefs (match-beginning 1)
4593                                                     (match-end 1)))
4594                         (substring xrefs (match-beginning 1) (match-end 1))))
4595           (setq number
4596                 (string-to-int (substring xrefs (match-beginning 2)
4597                                           (match-end 2))))
4598           (if (setq entry (gnus-gethash group xref-hashtb))
4599               (setcdr entry (cons number (cdr entry)))
4600             (gnus-sethash group (cons number nil) xref-hashtb)))))
4601     (and start xref-hashtb)))
4602
4603 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4604   "Look through all the headers and mark the Xrefs as read."
4605   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4606         name entry info xref-hashtb idlist method nth4)
4607     (save-excursion
4608       (set-buffer gnus-group-buffer)
4609       (when (setq xref-hashtb
4610                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4611         (mapatoms
4612          (lambda (group)
4613            (unless (string= from-newsgroup (setq name (symbol-name group)))
4614              (setq idlist (symbol-value group))
4615              ;; Dead groups are not updated.
4616              (and (prog1
4617                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4618                             info (nth 2 entry))
4619                     (when (stringp (setq nth4 (gnus-info-method info)))
4620                       (setq nth4 (gnus-server-to-method nth4))))
4621                   ;; Only do the xrefs if the group has the same
4622                   ;; select method as the group we have just read.
4623                   (or (gnus-methods-equal-p
4624                        nth4 (gnus-find-method-for-group from-newsgroup))
4625                       virtual
4626                       (equal nth4 (setq method (gnus-find-method-for-group
4627                                                 from-newsgroup)))
4628                       (and (equal (car nth4) (car method))
4629                            (equal (nth 1 nth4) (nth 1 method))))
4630                   gnus-use-cross-reference
4631                   (or (not (eq gnus-use-cross-reference t))
4632                       virtual
4633                       ;; Only do cross-references on subscribed
4634                       ;; groups, if that is what is wanted.
4635                       (<= (gnus-info-level info) gnus-level-subscribed))
4636                   (gnus-group-make-articles-read name idlist))))
4637          xref-hashtb)))))
4638
4639 (defun gnus-compute-read-articles (group articles)
4640   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4641          (info (nth 2 entry))
4642          (active (gnus-active group))
4643          ninfo)
4644     (when entry
4645       ;; First peel off all invalid article numbers.
4646       (when active
4647         (let ((ids articles)
4648               id first)
4649           (while (setq id (pop ids))
4650             (when (and first (> id (cdr active)))
4651               ;; We'll end up in this situation in one particular
4652               ;; obscure situation.  If you re-scan a group and get
4653               ;; a new article that is cross-posted to a different
4654               ;; group that has not been re-scanned, you might get
4655               ;; crossposted article that has a higher number than
4656               ;; Gnus believes possible.  So we re-activate this
4657               ;; group as well.  This might mean doing the
4658               ;; crossposting thingy will *increase* the number
4659               ;; of articles in some groups.  Tsk, tsk.
4660               (setq active (or (gnus-activate-group group) active)))
4661             (when (or (> id (cdr active))
4662                       (< id (car active)))
4663               (setq articles (delq id articles))))))
4664       ;; If the read list is nil, we init it.
4665       (if (and active
4666                (null (gnus-info-read info))
4667                (> (car active) 1))
4668           (setq ninfo (cons 1 (1- (car active))))
4669         (setq ninfo (gnus-info-read info)))
4670       ;; Then we add the read articles to the range.
4671       (gnus-add-to-range
4672        ninfo (setq articles (sort articles '<))))))
4673
4674 (defun gnus-group-make-articles-read (group articles)
4675   "Update the info of GROUP to say that ARTICLES are read."
4676   (let* ((num 0)
4677          (entry (gnus-gethash group gnus-newsrc-hashtb))
4678          (info (nth 2 entry))
4679          (active (gnus-active group))
4680          range)
4681     (when entry
4682       (setq range (gnus-compute-read-articles group articles))
4683       (save-excursion
4684         (set-buffer gnus-group-buffer)
4685         (gnus-undo-register
4686           `(progn
4687              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4688              (gnus-info-set-read ',info ',(gnus-info-read info))
4689              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4690              (gnus-group-update-group ,group t))))
4691       ;; Add the read articles to the range.
4692       (gnus-info-set-read info range)
4693       ;; Then we have to re-compute how many unread
4694       ;; articles there are in this group.
4695       (when active
4696         (cond
4697          ((not range)
4698           (setq num (- (1+ (cdr active)) (car active))))
4699          ((not (listp (cdr range)))
4700           (setq num (- (cdr active) (- (1+ (cdr range))
4701                                        (car range)))))
4702          (t
4703           (while range
4704             (if (numberp (car range))
4705                 (setq num (1+ num))
4706               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4707             (setq range (cdr range)))
4708           (setq num (- (cdr active) num))))
4709         ;; Update the number of unread articles.
4710         (setcar entry num)
4711         ;; Update the group buffer.
4712         (gnus-group-update-group group t)))))
4713
4714 (defvar gnus-newsgroup-none-id 0)
4715
4716 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4717   (let ((cur nntp-server-buffer)
4718         (dependencies
4719          (or dependencies
4720              (save-excursion (set-buffer gnus-summary-buffer)
4721                              gnus-newsgroup-dependencies)))
4722         headers id end ref
4723         (mail-parse-charset gnus-newsgroup-charset)
4724         (mail-parse-ignored-charsets
4725          (save-excursion (condition-case nil
4726                              (set-buffer gnus-summary-buffer)
4727                            (error))
4728                          gnus-newsgroup-ignored-charsets)))
4729     (save-excursion
4730       (set-buffer nntp-server-buffer)
4731       ;; Translate all TAB characters into SPACE characters.
4732       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4733       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4734       (gnus-run-hooks 'gnus-parse-headers-hook)
4735       (let ((case-fold-search t)
4736             in-reply-to header p lines chars ctype)
4737         (goto-char (point-min))
4738         ;; Search to the beginning of the next header.  Error messages
4739         ;; do not begin with 2 or 3.
4740         (while (re-search-forward "^[23][0-9]+ " nil t)
4741           (setq id nil
4742                 ref nil)
4743           ;; This implementation of this function, with nine
4744           ;; search-forwards instead of the one re-search-forward and
4745           ;; a case (which basically was the old function) is actually
4746           ;; about twice as fast, even though it looks messier.  You
4747           ;; can't have everything, I guess.  Speed and elegance
4748           ;; doesn't always go hand in hand.
4749           (setq
4750            header
4751            (make-full-mail-header
4752             ;; Number.
4753             (prog1
4754                 (read cur)
4755               (end-of-line)
4756               (setq p (point))
4757               (narrow-to-region (point)
4758                                 (or (and (search-forward "\n.\n" nil t)
4759                                          (- (point) 2))
4760                                     (point))))
4761             ;; Subject.
4762             (progn
4763               (goto-char p)
4764               (if (search-forward "\nsubject: " nil t)
4765                   (buffer-substring (match-end 0) (std11-field-end))
4766                 "(none)"))
4767             ;; From.
4768             (progn
4769               (goto-char p)
4770               (if (search-forward "\nfrom: " nil t)
4771                   (buffer-substring (match-end 0) (std11-field-end))
4772                 "(nobody)"))
4773             ;; Date.
4774             (progn
4775               (goto-char p)
4776               (if (search-forward "\ndate: " nil t)
4777                   (buffer-substring (match-end 0) (std11-field-end))
4778                 ""))
4779             ;; Message-ID.
4780             (progn
4781               (goto-char p)
4782               (setq id (if (re-search-forward
4783                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4784                            ;; We do it this way to make sure the Message-ID
4785                            ;; is (somewhat) syntactically valid.
4786                            (buffer-substring (match-beginning 1)
4787                                              (match-end 1))
4788                          ;; If there was no message-id, we just fake one
4789                          ;; to make subsequent routines simpler.
4790                          (nnheader-generate-fake-message-id))))
4791             ;; References.
4792             (progn
4793               (goto-char p)
4794               (if (search-forward "\nreferences: " nil t)
4795                   (progn
4796                     (setq end (point))
4797                     (prog1
4798                         (buffer-substring (match-end 0) (std11-field-end))
4799                       (setq ref
4800                             (buffer-substring
4801                              (progn
4802                                ;; (end-of-line)
4803                                (search-backward ">" end t)
4804                                (1+ (point)))
4805                              (progn
4806                                (search-backward "<" end t)
4807                                (point))))))
4808                 ;; Get the references from the in-reply-to header if there
4809                 ;; were no references and the in-reply-to header looks
4810                 ;; promising.
4811                 (if (and (search-forward "\nin-reply-to: " nil t)
4812                          (setq in-reply-to
4813                                (buffer-substring (match-end 0)
4814                                                  (std11-field-end)))
4815                          (string-match "<[^>]+>" in-reply-to))
4816                     (let (ref2)
4817                       (setq ref (substring in-reply-to (match-beginning 0)
4818                                            (match-end 0)))
4819                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4820                         (setq ref2 (substring in-reply-to (match-beginning 0)
4821                                               (match-end 0)))
4822                         (when (> (length ref2) (length ref))
4823                           (setq ref ref2)))
4824                       ref)
4825                   (setq ref nil))))
4826             ;; Chars.
4827             (progn
4828               (goto-char p)
4829               (if (search-forward "\nchars: " nil t)
4830                   (if (numberp (setq chars (ignore-errors (read cur))))
4831                       chars 0)
4832                 0))
4833             ;; Lines.
4834             (progn
4835               (goto-char p)
4836               (if (search-forward "\nlines: " nil t)
4837                   (if (numberp (setq lines (ignore-errors (read cur))))
4838                       lines 0)
4839                 0))
4840             ;; Xref.
4841             (progn
4842               (goto-char p)
4843               (and (search-forward "\nxref: " nil t)
4844                    (buffer-substring (match-end 0) (std11-field-end))))
4845             ;; Extra.
4846             (when gnus-extra-headers
4847               (let ((extra gnus-extra-headers)
4848                     out)
4849                 (while extra
4850                   (goto-char p)
4851                   (when (search-forward
4852                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4853                     (push (cons (car extra)
4854                                 (buffer-substring (match-end 0)
4855                                                   (std11-field-end)))
4856                           out))
4857                   (pop extra))
4858                 out))))
4859           (goto-char p)
4860           (if (and (search-forward "\ncontent-type: " nil t)
4861                    (setq ctype
4862                          (buffer-substring (match-end 0) (std11-field-end))))
4863               (mime-entity-set-content-type-internal
4864                header (mime-parse-Content-Type ctype)))
4865           (when (equal id ref)
4866             (setq ref nil))
4867
4868           (when gnus-alter-header-function
4869             (funcall gnus-alter-header-function header)
4870             (setq id (mail-header-id header)
4871                   ref (gnus-parent-id (mail-header-references header))))
4872
4873           (when (setq header
4874                       (gnus-dependencies-add-header
4875                        header dependencies force-new))
4876             (push header headers))
4877           (goto-char (point-max))
4878           (widen))
4879         (nreverse headers)))))
4880
4881 ;; Goes through the xover lines and returns a list of vectors
4882 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4883                                                   force-new dependencies
4884                                                   group also-fetch-heads)
4885   "Parse the news overview data in the server buffer.
4886 Return a list of headers that match SEQUENCE (see
4887 `nntp-retrieve-headers')."
4888   ;; Get the Xref when the users reads the articles since most/some
4889   ;; NNTP servers do not include Xrefs when using XOVER.
4890   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4891   (let ((mail-parse-charset gnus-newsgroup-charset)
4892         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4893         (cur nntp-server-buffer)
4894         (dependencies (or dependencies gnus-newsgroup-dependencies))
4895         number headers header)
4896     (save-excursion
4897       (set-buffer nntp-server-buffer)
4898       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4899       ;; Allow the user to mangle the headers before parsing them.
4900       (gnus-run-hooks 'gnus-parse-headers-hook)
4901       (goto-char (point-min))
4902       (while (not (eobp))
4903         (condition-case ()
4904             (while (and sequence (not (eobp)))
4905               (setq number (read cur))
4906               (while (and sequence
4907                           (< (car sequence) number))
4908                 (setq sequence (cdr sequence)))
4909               (and sequence
4910                    (eq number (car sequence))
4911                    (progn
4912                      (setq sequence (cdr sequence))
4913                      (setq header (inline
4914                                     (gnus-nov-parse-line
4915                                      number dependencies force-new))))
4916                    (push header headers))
4917               (forward-line 1))
4918           (error
4919            (gnus-error 4 "Strange nov line (%d)"
4920                        (count-lines (point-min) (point)))))
4921         (forward-line 1))
4922       ;; A common bug in inn is that if you have posted an article and
4923       ;; then retrieves the active file, it will answer correctly --
4924       ;; the new article is included.  However, a NOV entry for the
4925       ;; article may not have been generated yet, so this may fail.
4926       ;; We work around this problem by retrieving the last few
4927       ;; headers using HEAD.
4928       (if (or (not also-fetch-heads)
4929               (not sequence))
4930           ;; We (probably) got all the headers.
4931           (nreverse headers)
4932         (let ((gnus-nov-is-evil t))
4933           (nconc
4934            (nreverse headers)
4935            (gnus-retrieve-parsed-headers sequence group)
4936            ))))))
4937
4938 (defun gnus-article-get-xrefs ()
4939   "Fill in the Xref value in `gnus-current-headers', if necessary.
4940 This is meant to be called in `gnus-article-internal-prepare-hook'."
4941   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4942                                  gnus-current-headers)))
4943     (or (not gnus-use-cross-reference)
4944         (not headers)
4945         (and (mail-header-xref headers)
4946              (not (string= (mail-header-xref headers) "")))
4947         (let ((case-fold-search t)
4948               xref)
4949           (save-restriction
4950             (nnheader-narrow-to-headers)
4951             (goto-char (point-min))
4952             (when (or (and (not (eobp))
4953                            (eq (downcase (char-after)) ?x)
4954                            (looking-at "Xref:"))
4955                       (search-forward "\nXref:" nil t))
4956               (goto-char (1+ (match-end 0)))
4957               (setq xref (buffer-substring (point)
4958                                            (progn (end-of-line) (point))))
4959               (mail-header-set-xref headers xref)))))))
4960
4961 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4962   "Find article ID and insert the summary line for that article.
4963 OLD-HEADER can either be a header or a line number to insert
4964 the subject line on."
4965   (let* ((line (and (numberp old-header) old-header))
4966          (old-header (and (vectorp old-header) old-header))
4967          (header (cond ((and old-header use-old-header)
4968                         old-header)
4969                        ((and (numberp id)
4970                              (gnus-number-to-header id))
4971                         (gnus-number-to-header id))
4972                        (t
4973                         (gnus-read-header id))))
4974          (number (and (numberp id) id))
4975          d)
4976     (when header
4977       ;; Rebuild the thread that this article is part of and go to the
4978       ;; article we have fetched.
4979       (when (and (not gnus-show-threads)
4980                  old-header)
4981         (when (and number
4982                    (setq d (gnus-data-find (mail-header-number old-header))))
4983           (goto-char (gnus-data-pos d))
4984           (gnus-data-remove
4985            number
4986            (- (gnus-point-at-bol)
4987               (prog1
4988                   (1+ (gnus-point-at-eol))
4989                 (gnus-delete-line))))))
4990       (when old-header
4991         (mail-header-set-number header (mail-header-number old-header)))
4992       (setq gnus-newsgroup-sparse
4993             (delq (setq number (mail-header-number header))
4994                   gnus-newsgroup-sparse))
4995       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4996       (push number gnus-newsgroup-limit)
4997       (gnus-rebuild-thread (mail-header-id header) line)
4998       (gnus-summary-goto-subject number nil t))
4999     (when (and (numberp number)
5000                (> number 0))
5001       ;; We have to update the boundaries even if we can't fetch the
5002       ;; article if ID is a number -- so that the next `P' or `N'
5003       ;; command will fetch the previous (or next) article even
5004       ;; if the one we tried to fetch this time has been canceled.
5005       (when (> number gnus-newsgroup-end)
5006         (setq gnus-newsgroup-end number))
5007       (when (< number gnus-newsgroup-begin)
5008         (setq gnus-newsgroup-begin number))
5009       (setq gnus-newsgroup-unselected
5010             (delq number gnus-newsgroup-unselected)))
5011     ;; Report back a success?
5012     (and header (mail-header-number header))))
5013
5014 ;;; Process/prefix in the summary buffer
5015
5016 (defun gnus-summary-work-articles (n)
5017   "Return a list of articles to be worked upon.
5018 The prefix argument, the list of process marked articles, and the
5019 current article will be taken into consideration."
5020   (save-excursion
5021     (set-buffer gnus-summary-buffer)
5022     (cond
5023      (n
5024       ;; A numerical prefix has been given.
5025       (setq n (prefix-numeric-value n))
5026       (let ((backward (< n 0))
5027             (n (abs (prefix-numeric-value n)))
5028             articles article)
5029         (save-excursion
5030           (while
5031               (and (> n 0)
5032                    (push (setq article (gnus-summary-article-number))
5033                          articles)
5034                    (if backward
5035                        (gnus-summary-find-prev nil article)
5036                      (gnus-summary-find-next nil article)))
5037             (decf n)))
5038         (nreverse articles)))
5039      ((and (gnus-region-active-p) (mark))
5040       (message "region active")
5041       ;; Work on the region between point and mark.
5042       (let ((max (max (point) (mark)))
5043             articles article)
5044         (save-excursion
5045           (goto-char (min (point) (mark)))
5046           (while
5047               (and
5048                (push (setq article (gnus-summary-article-number)) articles)
5049                (gnus-summary-find-next nil article)
5050                (< (point) max)))
5051           (nreverse articles))))
5052      (gnus-newsgroup-processable
5053       ;; There are process-marked articles present.
5054       ;; Save current state.
5055       (gnus-summary-save-process-mark)
5056       ;; Return the list.
5057       (reverse gnus-newsgroup-processable))
5058      (t
5059       ;; Just return the current article.
5060       (list (gnus-summary-article-number))))))
5061
5062 (defmacro gnus-summary-iterate (arg &rest forms)
5063   "Iterate over the process/prefixed articles and do FORMS.
5064 ARG is the interactive prefix given to the command.  FORMS will be
5065 executed with point over the summary line of the articles."
5066   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5067     `(let ((,articles (gnus-summary-work-articles ,arg)))
5068        (while ,articles
5069          (gnus-summary-goto-subject (car ,articles))
5070          ,@forms
5071          (pop ,articles)))))
5072
5073 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5074 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5075
5076 (defun gnus-summary-save-process-mark ()
5077   "Push the current set of process marked articles on the stack."
5078   (interactive)
5079   (push (copy-sequence gnus-newsgroup-processable)
5080         gnus-newsgroup-process-stack))
5081
5082 (defun gnus-summary-kill-process-mark ()
5083   "Push the current set of process marked articles on the stack and unmark."
5084   (interactive)
5085   (gnus-summary-save-process-mark)
5086   (gnus-summary-unmark-all-processable))
5087
5088 (defun gnus-summary-yank-process-mark ()
5089   "Pop the last process mark state off the stack and restore it."
5090   (interactive)
5091   (unless gnus-newsgroup-process-stack
5092     (error "Empty mark stack"))
5093   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5094
5095 (defun gnus-summary-process-mark-set (set)
5096   "Make SET into the current process marked articles."
5097   (gnus-summary-unmark-all-processable)
5098   (while set
5099     (gnus-summary-set-process-mark (pop set))))
5100
5101 ;;; Searching and stuff
5102
5103 (defun gnus-summary-search-group (&optional backward use-level)
5104   "Search for next unread newsgroup.
5105 If optional argument BACKWARD is non-nil, search backward instead."
5106   (save-excursion
5107     (set-buffer gnus-group-buffer)
5108     (when (gnus-group-search-forward
5109            backward nil (if use-level (gnus-group-group-level) nil))
5110       (gnus-group-group-name))))
5111
5112 (defun gnus-summary-best-group (&optional exclude-group)
5113   "Find the name of the best unread group.
5114 If EXCLUDE-GROUP, do not go to this group."
5115   (save-excursion
5116     (set-buffer gnus-group-buffer)
5117     (save-excursion
5118       (gnus-group-best-unread-group exclude-group))))
5119
5120 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5121   (if backward (gnus-summary-find-prev)
5122     (let* ((dummy (gnus-summary-article-intangible-p))
5123            (article (or article (gnus-summary-article-number)))
5124            (arts (gnus-data-find-list article))
5125            result)
5126       (when (and (not dummy)
5127                  (or (not gnus-summary-check-current)
5128                      (not unread)
5129                      (not (gnus-data-unread-p (car arts)))))
5130         (setq arts (cdr arts)))
5131       (when (setq result
5132                   (if unread
5133                       (progn
5134                         (while arts
5135                           (when (or (and undownloaded
5136                                          (eq gnus-undownloaded-mark
5137                                              (gnus-data-mark (car arts))))
5138                                     (gnus-data-unread-p (car arts)))
5139                             (setq result (car arts)
5140                                   arts nil))
5141                           (setq arts (cdr arts)))
5142                         result)
5143                     (car arts)))
5144         (goto-char (gnus-data-pos result))
5145         (gnus-data-number result)))))
5146
5147 (defun gnus-summary-find-prev (&optional unread article)
5148   (let* ((eobp (eobp))
5149          (article (or article (gnus-summary-article-number)))
5150          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5151          result)
5152     (when (and (not eobp)
5153                (or (not gnus-summary-check-current)
5154                    (not unread)
5155                    (not (gnus-data-unread-p (car arts)))))
5156       (setq arts (cdr arts)))
5157     (when (setq result
5158                 (if unread
5159                     (progn
5160                       (while arts
5161                         (when (gnus-data-unread-p (car arts))
5162                           (setq result (car arts)
5163                                 arts nil))
5164                         (setq arts (cdr arts)))
5165                       result)
5166                   (car arts)))
5167       (goto-char (gnus-data-pos result))
5168       (gnus-data-number result))))
5169
5170 (defun gnus-summary-find-subject (subject &optional unread backward article)
5171   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5172          (article (or article (gnus-summary-article-number)))
5173          (articles (gnus-data-list backward))
5174          (arts (gnus-data-find-list article articles))
5175          result)
5176     (when (or (not gnus-summary-check-current)
5177               (not unread)
5178               (not (gnus-data-unread-p (car arts))))
5179       (setq arts (cdr arts)))
5180     (while arts
5181       (and (or (not unread)
5182                (gnus-data-unread-p (car arts)))
5183            (vectorp (gnus-data-header (car arts)))
5184            (gnus-subject-equal
5185             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5186            (setq result (car arts)
5187                  arts nil))
5188       (setq arts (cdr arts)))
5189     (and result
5190          (goto-char (gnus-data-pos result))
5191          (gnus-data-number result))))
5192
5193 (defun gnus-summary-search-forward (&optional unread subject backward)
5194   "Search forward for an article.
5195 If UNREAD, look for unread articles.  If SUBJECT, look for
5196 articles with that subject.  If BACKWARD, search backward instead."
5197   (cond (subject (gnus-summary-find-subject subject unread backward))
5198         (backward (gnus-summary-find-prev unread))
5199         (t (gnus-summary-find-next unread))))
5200
5201 (defun gnus-recenter (&optional n)
5202   "Center point in window and redisplay frame.
5203 Also do horizontal recentering."
5204   (interactive "P")
5205   (when (and gnus-auto-center-summary
5206              (not (eq gnus-auto-center-summary 'vertical)))
5207     (gnus-horizontal-recenter))
5208   (recenter n))
5209
5210 (defun gnus-summary-recenter ()
5211   "Center point in the summary window.
5212 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5213 displayed, no centering will be performed."
5214   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5215   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5216   (interactive)
5217   (let* ((top (cond ((< (window-height) 4) 0)
5218                     ((< (window-height) 7) 1)
5219                     (t (if (numberp gnus-auto-center-summary)
5220                            gnus-auto-center-summary
5221                          2))))
5222          (height (1- (window-height)))
5223          (bottom (save-excursion (goto-char (point-max))
5224                                  (forward-line (- height))
5225                                  (point)))
5226          (window (get-buffer-window (current-buffer))))
5227     ;; The user has to want it.
5228     (when gnus-auto-center-summary
5229       (when (get-buffer-window gnus-article-buffer)
5230         ;; Only do recentering when the article buffer is displayed,
5231         ;; Set the window start to either `bottom', which is the biggest
5232         ;; possible valid number, or the second line from the top,
5233         ;; whichever is the least.
5234         (set-window-start
5235          window (min bottom (save-excursion
5236                               (forward-line (- top)) (point)))
5237          t))
5238       ;; Do horizontal recentering while we're at it.
5239       (when (and (get-buffer-window (current-buffer) t)
5240                  (not (eq gnus-auto-center-summary 'vertical)))
5241         (let ((selected (selected-window)))
5242           (select-window (get-buffer-window (current-buffer) t))
5243           (gnus-summary-position-point)
5244           (gnus-horizontal-recenter)
5245           (select-window selected))))))
5246
5247 (defun gnus-summary-jump-to-group (newsgroup)
5248   "Move point to NEWSGROUP in group mode buffer."
5249   ;; Keep update point of group mode buffer if visible.
5250   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5251       (save-window-excursion
5252         ;; Take care of tree window mode.
5253         (when (get-buffer-window gnus-group-buffer)
5254           (pop-to-buffer gnus-group-buffer))
5255         (gnus-group-jump-to-group newsgroup))
5256     (save-excursion
5257       ;; Take care of tree window mode.
5258       (if (get-buffer-window gnus-group-buffer)
5259           (pop-to-buffer gnus-group-buffer)
5260         (set-buffer gnus-group-buffer))
5261       (gnus-group-jump-to-group newsgroup))))
5262
5263 ;; This function returns a list of article numbers based on the
5264 ;; difference between the ranges of read articles in this group and
5265 ;; the range of active articles.
5266 (defun gnus-list-of-unread-articles (group)
5267   (let* ((read (gnus-info-read (gnus-get-info group)))
5268          (active (or (gnus-active group) (gnus-activate-group group)))
5269          (last (cdr active))
5270          first nlast unread)
5271     ;; If none are read, then all are unread.
5272     (if (not read)
5273         (setq first (car active))
5274       ;; If the range of read articles is a single range, then the
5275       ;; first unread article is the article after the last read
5276       ;; article.  Sounds logical, doesn't it?
5277       (if (and (not (listp (cdr read)))
5278                (or (< (car read) (car active))
5279                    (progn (setq read (list read))
5280                           nil)))
5281           (setq first (max (car active) (1+ (cdr read))))
5282         ;; `read' is a list of ranges.
5283         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5284                                   (caar read)))
5285                   1)
5286           (setq first (car active)))
5287         (while read
5288           (when first
5289             (while (< first nlast)
5290               (push first unread)
5291               (setq first (1+ first))))
5292           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5293           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5294           (setq read (cdr read)))))
5295     ;; And add the last unread articles.
5296     (while (<= first last)
5297       (push first unread)
5298       (setq first (1+ first)))
5299     ;; Return the list of unread articles.
5300     (delq 0 (nreverse unread))))
5301
5302 (defun gnus-list-of-read-articles (group)
5303   "Return a list of unread, unticked and non-dormant articles."
5304   (let* ((info (gnus-get-info group))
5305          (marked (gnus-info-marks info))
5306          (active (gnus-active group)))
5307     (and info active
5308          (gnus-set-difference
5309           (gnus-sorted-complement
5310            (gnus-uncompress-range active)
5311            (gnus-list-of-unread-articles group))
5312           (append
5313            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5314            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5315
5316 ;; Various summary commands
5317
5318 (defun gnus-summary-select-article-buffer ()
5319   "Reconfigure windows to show article buffer."
5320   (interactive)
5321   (if (not (gnus-buffer-live-p gnus-article-buffer))
5322       (error "There is no article buffer for this summary buffer")
5323     (gnus-configure-windows 'article)
5324     (select-window (get-buffer-window gnus-article-buffer))))
5325
5326 (defun gnus-summary-universal-argument (arg)
5327   "Perform any operation on all articles that are process/prefixed."
5328   (interactive "P")
5329   (let ((articles (gnus-summary-work-articles arg))
5330         func article)
5331     (if (eq
5332          (setq
5333           func
5334           (key-binding
5335            (read-key-sequence
5336             (substitute-command-keys
5337              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5338          'undefined)
5339         (gnus-error 1 "Undefined key")
5340       (save-excursion
5341         (while articles
5342           (gnus-summary-goto-subject (setq article (pop articles)))
5343           (let (gnus-newsgroup-processable)
5344             (command-execute func))
5345           (gnus-summary-remove-process-mark article)))))
5346   (gnus-summary-position-point))
5347
5348 (defun gnus-summary-toggle-truncation (&optional arg)
5349   "Toggle truncation of summary lines.
5350 With arg, turn line truncation on iff arg is positive."
5351   (interactive "P")
5352   (setq truncate-lines
5353         (if (null arg) (not truncate-lines)
5354           (> (prefix-numeric-value arg) 0)))
5355   (redraw-display))
5356
5357 (defun gnus-summary-reselect-current-group (&optional all rescan)
5358   "Exit and then reselect the current newsgroup.
5359 The prefix argument ALL means to select all articles."
5360   (interactive "P")
5361   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5362     (error "Ephemeral groups can't be reselected"))
5363   (let ((current-subject (gnus-summary-article-number))
5364         (group gnus-newsgroup-name))
5365     (setq gnus-newsgroup-begin nil)
5366     (gnus-summary-exit)
5367     ;; We have to adjust the point of group mode buffer because
5368     ;; point was moved to the next unread newsgroup by exiting.
5369     (gnus-summary-jump-to-group group)
5370     (when rescan
5371       (save-excursion
5372         (save-window-excursion
5373           ;; Don't show group contents.
5374           (set-window-start (selected-window) (point-max))
5375           (gnus-group-get-new-news-this-group 1))))
5376     (gnus-group-read-group all t)
5377     (gnus-summary-goto-subject current-subject nil t)))
5378
5379 (defun gnus-summary-rescan-group (&optional all)
5380   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5381   (interactive "P")
5382   (gnus-summary-reselect-current-group all t))
5383
5384 (defun gnus-summary-update-info (&optional non-destructive)
5385   (save-excursion
5386     (let ((group gnus-newsgroup-name))
5387       (when group
5388         (when gnus-newsgroup-kill-headers
5389           (setq gnus-newsgroup-killed
5390                 (gnus-compress-sequence
5391                  (nconc
5392                   (gnus-set-sorted-intersection
5393                    (gnus-uncompress-range gnus-newsgroup-killed)
5394                    (setq gnus-newsgroup-unselected
5395                          (sort gnus-newsgroup-unselected '<)))
5396                   (setq gnus-newsgroup-unreads
5397                         (sort gnus-newsgroup-unreads '<)))
5398                  t)))
5399         (unless (listp (cdr gnus-newsgroup-killed))
5400           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5401         (let ((headers gnus-newsgroup-headers))
5402           ;; Set the new ranges of read articles.
5403           (save-excursion
5404             (set-buffer gnus-group-buffer)
5405             (gnus-undo-force-boundary))
5406           (gnus-update-read-articles
5407            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5408           ;; Set the current article marks.
5409           (let ((gnus-newsgroup-scored
5410                  (if (and (not gnus-save-score)
5411                           (not non-destructive))
5412                      nil
5413                    gnus-newsgroup-scored)))
5414             (save-excursion
5415               (gnus-update-marks)))
5416           ;; Do the cross-ref thing.
5417           (when gnus-use-cross-reference
5418             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5419           ;; Do not switch windows but change the buffer to work.
5420           (set-buffer gnus-group-buffer)
5421           (unless (gnus-ephemeral-group-p group)
5422             (gnus-group-update-group group)))))))
5423
5424 (defun gnus-summary-save-newsrc (&optional force)
5425   "Save the current number of read/marked articles in the dribble buffer.
5426 The dribble buffer will then be saved.
5427 If FORCE (the prefix), also save the .newsrc file(s)."
5428   (interactive "P")
5429   (gnus-summary-update-info t)
5430   (if force
5431       (gnus-save-newsrc-file)
5432     (gnus-dribble-save)))
5433
5434 (defun gnus-summary-exit (&optional temporary)
5435   "Exit reading current newsgroup, and then return to group selection mode.
5436 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5437   (interactive)
5438   (gnus-set-global-variables)
5439   (gnus-kill-save-kill-buffer)
5440   (gnus-async-halt-prefetch)
5441   (let* ((group gnus-newsgroup-name)
5442          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5443          (mode major-mode)
5444          (group-point nil)
5445          (buf (current-buffer)))
5446     (unless quit-config
5447       ;; Do adaptive scoring, and possibly save score files.
5448       (when gnus-newsgroup-adaptive
5449         (gnus-score-adaptive))
5450       (when gnus-use-scoring
5451         (gnus-score-save)))
5452     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5453     ;; If we have several article buffers, we kill them at exit.
5454     (unless gnus-single-article-buffer
5455       (gnus-kill-buffer gnus-original-article-buffer)
5456       (setq gnus-article-current nil))
5457     (when gnus-use-cache
5458       (gnus-cache-possibly-remove-articles)
5459       (gnus-cache-save-buffers))
5460     (gnus-async-prefetch-remove-group group)
5461     (when gnus-suppress-duplicates
5462       (gnus-dup-enter-articles))
5463     (when gnus-use-trees
5464       (gnus-tree-close group))
5465     (when gnus-use-cache
5466       (gnus-cache-write-active))
5467     ;; Remove entries for this group.
5468     (nnmail-purge-split-history (gnus-group-real-name group))
5469     ;; Make all changes in this group permanent.
5470     (unless quit-config
5471       (gnus-run-hooks 'gnus-exit-group-hook)
5472       (gnus-summary-update-info))
5473     (gnus-close-group group)
5474     ;; Make sure where we were, and go to next newsgroup.
5475     (set-buffer gnus-group-buffer)
5476     (unless quit-config
5477       (gnus-group-jump-to-group group))
5478     (gnus-run-hooks 'gnus-summary-exit-hook)
5479     (unless (or quit-config
5480                 ;; If this group has disappeared from the summary
5481                 ;; buffer, don't skip forwards.
5482                 (not (string= group (gnus-group-group-name))))
5483       (gnus-group-next-unread-group 1))
5484     (setq group-point (point))
5485     (if temporary
5486         nil                             ;Nothing to do.
5487       ;; If we have several article buffers, we kill them at exit.
5488       (unless gnus-single-article-buffer
5489         (gnus-kill-buffer gnus-article-buffer)
5490         (gnus-kill-buffer gnus-original-article-buffer)
5491         (setq gnus-article-current nil))
5492       (set-buffer buf)
5493       (if (not gnus-kill-summary-on-exit)
5494           (gnus-deaden-summary)
5495         ;; We set all buffer-local variables to nil.  It is unclear why
5496         ;; this is needed, but if we don't, buffer-local variables are
5497         ;; not garbage-collected, it seems.  This would the lead to en
5498         ;; ever-growing Emacs.
5499         (gnus-summary-clear-local-variables)
5500         (when (get-buffer gnus-article-buffer)
5501           (bury-buffer gnus-article-buffer))
5502         ;; We clear the global counterparts of the buffer-local
5503         ;; variables as well, just to be on the safe side.
5504         (set-buffer gnus-group-buffer)
5505         (gnus-summary-clear-local-variables)
5506         ;; Return to group mode buffer.
5507         (when (eq mode 'gnus-summary-mode)
5508           (gnus-kill-buffer buf)))
5509       (setq gnus-current-select-method gnus-select-method)
5510       (pop-to-buffer gnus-group-buffer)
5511       (if (not quit-config)
5512           (progn
5513             (goto-char group-point)
5514             (gnus-configure-windows 'group 'force)
5515             (unless (pos-visible-in-window-p)
5516               (forward-line (/ (static-if (featurep 'xemacs)
5517                                    (window-displayed-height)
5518                                  (1- (window-height)))
5519                                -2))
5520               (set-window-start (selected-window) (point))
5521               (goto-char group-point)))
5522         (gnus-handle-ephemeral-exit quit-config))
5523       ;; Clear the current group name.
5524       (unless quit-config
5525         (setq gnus-newsgroup-name nil)))))
5526
5527 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5528 (defun gnus-summary-exit-no-update (&optional no-questions)
5529   "Quit reading current newsgroup without updating read article info."
5530   (interactive)
5531   (let* ((group gnus-newsgroup-name)
5532          (quit-config (gnus-group-quit-config group)))
5533     (when (or no-questions
5534               gnus-expert-user
5535               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5536       (gnus-async-halt-prefetch)
5537       (mapcar 'funcall
5538               (delq 'gnus-summary-expire-articles
5539                     (copy-sequence gnus-summary-prepare-exit-hook)))
5540       ;; If we have several article buffers, we kill them at exit.
5541       (unless gnus-single-article-buffer
5542         (gnus-kill-buffer gnus-article-buffer)
5543         (gnus-kill-buffer gnus-original-article-buffer)
5544         (setq gnus-article-current nil))
5545       (if (not gnus-kill-summary-on-exit)
5546           (gnus-deaden-summary)
5547         (gnus-close-group group)
5548         (gnus-summary-clear-local-variables)
5549         (set-buffer gnus-group-buffer)
5550         (gnus-summary-clear-local-variables)
5551         (when (get-buffer gnus-summary-buffer)
5552           (kill-buffer gnus-summary-buffer)))
5553       (unless gnus-single-article-buffer
5554         (setq gnus-article-current nil))
5555       (when gnus-use-trees
5556         (gnus-tree-close group))
5557       (gnus-async-prefetch-remove-group group)
5558       (when (get-buffer gnus-article-buffer)
5559         (bury-buffer gnus-article-buffer))
5560       ;; Return to the group buffer.
5561       (gnus-configure-windows 'group 'force)
5562       ;; Clear the current group name.
5563       (setq gnus-newsgroup-name nil)
5564       (when (equal (gnus-group-group-name) group)
5565         (gnus-group-next-unread-group 1))
5566       (when quit-config
5567         (gnus-handle-ephemeral-exit quit-config)))))
5568
5569 (defun gnus-handle-ephemeral-exit (quit-config)
5570   "Handle movement when leaving an ephemeral group.
5571 The state which existed when entering the ephemeral is reset."
5572   (if (not (buffer-name (car quit-config)))
5573       (gnus-configure-windows 'group 'force)
5574     (set-buffer (car quit-config))
5575     (cond ((eq major-mode 'gnus-summary-mode)
5576            (gnus-set-global-variables))
5577           ((eq major-mode 'gnus-article-mode)
5578            (save-excursion
5579              ;; The `gnus-summary-buffer' variable may point
5580              ;; to the old summary buffer when using a single
5581              ;; article buffer.
5582              (unless (gnus-buffer-live-p gnus-summary-buffer)
5583                (set-buffer gnus-group-buffer))
5584              (set-buffer gnus-summary-buffer)
5585              (gnus-set-global-variables))))
5586     (if (or (eq (cdr quit-config) 'article)
5587             (eq (cdr quit-config) 'pick))
5588         (progn
5589           ;; The current article may be from the ephemeral group
5590           ;; thus it is best that we reload this article
5591           (gnus-summary-show-article)
5592           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5593               (gnus-configure-windows 'pick 'force)
5594             (gnus-configure-windows (cdr quit-config) 'force)))
5595       (gnus-configure-windows (cdr quit-config) 'force))
5596     (when (eq major-mode 'gnus-summary-mode)
5597       (gnus-summary-next-subject 1 nil t)
5598       (gnus-summary-recenter)
5599       (gnus-summary-position-point))))
5600
5601 (defun gnus-summary-preview-mime-message ()
5602   "MIME decode and play this message."
5603   (interactive)
5604   (let ((gnus-break-pages nil)
5605         (gnus-show-mime t))
5606     (gnus-summary-select-article gnus-show-all-headers t))
5607   (select-window (get-buffer-window gnus-article-buffer)))
5608
5609 ;;; Dead summaries.
5610
5611 (defvar gnus-dead-summary-mode-map nil)
5612
5613 (unless gnus-dead-summary-mode-map
5614   (setq gnus-dead-summary-mode-map (make-keymap))
5615   (suppress-keymap gnus-dead-summary-mode-map)
5616   (substitute-key-definition
5617    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5618   (let ((keys '("\C-d" "\r" "\177" [delete])))
5619     (while keys
5620       (define-key gnus-dead-summary-mode-map
5621         (pop keys) 'gnus-summary-wake-up-the-dead))))
5622
5623 (defvar gnus-dead-summary-mode nil
5624   "Minor mode for Gnus summary buffers.")
5625
5626 (defun gnus-dead-summary-mode (&optional arg)
5627   "Minor mode for Gnus summary buffers."
5628   (interactive "P")
5629   (when (eq major-mode 'gnus-summary-mode)
5630     (make-local-variable 'gnus-dead-summary-mode)
5631     (setq gnus-dead-summary-mode
5632           (if (null arg) (not gnus-dead-summary-mode)
5633             (> (prefix-numeric-value arg) 0)))
5634     (when gnus-dead-summary-mode
5635       (gnus-add-minor-mode
5636        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5637
5638 (defun gnus-deaden-summary ()
5639   "Make the current summary buffer into a dead summary buffer."
5640   ;; Kill any previous dead summary buffer.
5641   (when (and gnus-dead-summary
5642              (buffer-name gnus-dead-summary))
5643     (save-excursion
5644       (set-buffer gnus-dead-summary)
5645       (when gnus-dead-summary-mode
5646         (kill-buffer (current-buffer)))))
5647   ;; Make this the current dead summary.
5648   (setq gnus-dead-summary (current-buffer))
5649   (gnus-dead-summary-mode 1)
5650   (let ((name (buffer-name)))
5651     (when (string-match "Summary" name)
5652       (rename-buffer
5653        (concat (substring name 0 (match-beginning 0)) "Dead "
5654                (substring name (match-beginning 0)))
5655        t)
5656       (bury-buffer))))
5657
5658 (defun gnus-kill-or-deaden-summary (buffer)
5659   "Kill or deaden the summary BUFFER."
5660   (save-excursion
5661     (when (and (buffer-name buffer)
5662                (not gnus-single-article-buffer))
5663       (save-excursion
5664         (set-buffer buffer)
5665         (gnus-kill-buffer gnus-article-buffer)
5666         (gnus-kill-buffer gnus-original-article-buffer)))
5667     (cond (gnus-kill-summary-on-exit
5668            (when (and gnus-use-trees
5669                       (gnus-buffer-exists-p buffer))
5670              (save-excursion
5671                (set-buffer buffer)
5672                (gnus-tree-close gnus-newsgroup-name)))
5673            (gnus-kill-buffer buffer))
5674           ((gnus-buffer-exists-p buffer)
5675            (save-excursion
5676              (set-buffer buffer)
5677              (gnus-deaden-summary))))))
5678
5679 (defun gnus-summary-wake-up-the-dead (&rest args)
5680   "Wake up the dead summary buffer."
5681   (interactive)
5682   (gnus-dead-summary-mode -1)
5683   (let ((name (buffer-name)))
5684     (when (string-match "Dead " name)
5685       (rename-buffer
5686        (concat (substring name 0 (match-beginning 0))
5687                (substring name (match-end 0)))
5688        t)))
5689   (gnus-message 3 "This dead summary is now alive again"))
5690
5691 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5692 (defun gnus-summary-fetch-faq (&optional faq-dir)
5693   "Fetch the FAQ for the current group.
5694 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5695 in."
5696   (interactive
5697    (list
5698     (when current-prefix-arg
5699       (completing-read
5700        "Faq dir: " (and (listp gnus-group-faq-directory)
5701                         (mapcar (lambda (file) (list file))
5702                                 gnus-group-faq-directory))))))
5703   (let (gnus-faq-buffer)
5704     (when (setq gnus-faq-buffer
5705                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5706       (gnus-configure-windows 'summary-faq))))
5707
5708 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5709 (defun gnus-summary-describe-group (&optional force)
5710   "Describe the current newsgroup."
5711   (interactive "P")
5712   (gnus-group-describe-group force gnus-newsgroup-name))
5713
5714 (defun gnus-summary-describe-briefly ()
5715   "Describe summary mode commands briefly."
5716   (interactive)
5717   (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")))
5718
5719 ;; Walking around group mode buffer from summary mode.
5720
5721 (defun gnus-summary-next-group (&optional no-article target-group backward)
5722   "Exit current newsgroup and then select next unread newsgroup.
5723 If prefix argument NO-ARTICLE is non-nil, no article is selected
5724 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5725 previous group instead."
5726   (interactive "P")
5727   ;; Stop pre-fetching.
5728   (gnus-async-halt-prefetch)
5729   (let ((current-group gnus-newsgroup-name)
5730         (current-buffer (current-buffer))
5731         entered)
5732     ;; First we semi-exit this group to update Xrefs and all variables.
5733     ;; We can't do a real exit, because the window conf must remain
5734     ;; the same in case the user is prompted for info, and we don't
5735     ;; want the window conf to change before that...
5736     (gnus-summary-exit t)
5737     (while (not entered)
5738       ;; Then we find what group we are supposed to enter.
5739       (set-buffer gnus-group-buffer)
5740       (gnus-group-jump-to-group current-group)
5741       (setq target-group
5742             (or target-group
5743                 (if (eq gnus-keep-same-level 'best)
5744                     (gnus-summary-best-group gnus-newsgroup-name)
5745                   (gnus-summary-search-group backward gnus-keep-same-level))))
5746       (if (not target-group)
5747           ;; There are no further groups, so we return to the group
5748           ;; buffer.
5749           (progn
5750             (gnus-message 5 "Returning to the group buffer")
5751             (setq entered t)
5752             (when (gnus-buffer-live-p current-buffer)
5753               (set-buffer current-buffer)
5754               (gnus-summary-exit))
5755             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5756         ;; We try to enter the target group.
5757         (gnus-group-jump-to-group target-group)
5758         (let ((unreads (gnus-group-group-unread)))
5759           (if (and (or (eq t unreads)
5760                        (and unreads (not (zerop unreads))))
5761                    (gnus-summary-read-group
5762                     target-group nil no-article
5763                     (and (buffer-name current-buffer) current-buffer)
5764                     nil backward))
5765               (setq entered t)
5766             (setq current-group target-group
5767                   target-group nil)))))))
5768
5769 (defun gnus-summary-prev-group (&optional no-article)
5770   "Exit current newsgroup and then select previous unread newsgroup.
5771 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5772   (interactive "P")
5773   (gnus-summary-next-group no-article nil t))
5774
5775 ;; Walking around summary lines.
5776
5777 (defun gnus-summary-first-subject (&optional unread undownloaded)
5778   "Go to the first unread subject.
5779 If UNREAD is non-nil, go to the first unread article.
5780 Returns the article selected or nil if there are no unread articles."
5781   (interactive "P")
5782   (prog1
5783       (cond
5784        ;; Empty summary.
5785        ((null gnus-newsgroup-data)
5786         (gnus-message 3 "No articles in the group")
5787         nil)
5788        ;; Pick the first article.
5789        ((not unread)
5790         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5791         (gnus-data-number (car gnus-newsgroup-data)))
5792        ;; No unread articles.
5793        ((null gnus-newsgroup-unreads)
5794         (gnus-message 3 "No more unread articles")
5795         nil)
5796        ;; Find the first unread article.
5797        (t
5798         (let ((data gnus-newsgroup-data))
5799           (while (and data
5800                       (and (not (and undownloaded
5801                                      (eq gnus-undownloaded-mark
5802                                          (gnus-data-mark (car data)))))
5803                            (not (gnus-data-unread-p (car data)))))
5804             (setq data (cdr data)))
5805           (when data
5806             (goto-char (gnus-data-pos (car data)))
5807             (gnus-data-number (car data))))))
5808     (gnus-summary-position-point)))
5809
5810 (defun gnus-summary-next-subject (n &optional unread dont-display)
5811   "Go to next N'th summary line.
5812 If N is negative, go to the previous N'th subject line.
5813 If UNREAD is non-nil, only unread articles are selected.
5814 The difference between N and the actual number of steps taken is
5815 returned."
5816   (interactive "p")
5817   (let ((backward (< n 0))
5818         (n (abs n)))
5819     (while (and (> n 0)
5820                 (if backward
5821                     (gnus-summary-find-prev unread)
5822                   (gnus-summary-find-next unread)))
5823       (unless (zerop (setq n (1- n)))
5824         (gnus-summary-show-thread)))
5825     (when (/= 0 n)
5826       (gnus-message 7 "No more%s articles"
5827                     (if unread " unread" "")))
5828     (unless dont-display
5829       (gnus-summary-recenter)
5830       (gnus-summary-position-point))
5831     n))
5832
5833 (defun gnus-summary-next-unread-subject (n)
5834   "Go to next N'th unread summary line."
5835   (interactive "p")
5836   (gnus-summary-next-subject n t))
5837
5838 (defun gnus-summary-prev-subject (n &optional unread)
5839   "Go to previous N'th summary line.
5840 If optional argument UNREAD is non-nil, only unread article is selected."
5841   (interactive "p")
5842   (gnus-summary-next-subject (- n) unread))
5843
5844 (defun gnus-summary-prev-unread-subject (n)
5845   "Go to previous N'th unread summary line."
5846   (interactive "p")
5847   (gnus-summary-next-subject (- n) t))
5848
5849 (defun gnus-summary-goto-subject (article &optional force silent)
5850   "Go the subject line of ARTICLE.
5851 If FORCE, also allow jumping to articles not currently shown."
5852   (interactive "nArticle number: ")
5853   (let ((b (point))
5854         (data (gnus-data-find article)))
5855     ;; We read in the article if we have to.
5856     (and (not data)
5857          force
5858          (gnus-summary-insert-subject
5859           article
5860           (if (or (numberp force) (vectorp force)) force)
5861           t)
5862          (setq data (gnus-data-find article)))
5863     (goto-char b)
5864     (if (not data)
5865         (progn
5866           (unless silent
5867             (gnus-message 3 "Can't find article %d" article))
5868           nil)
5869       (goto-char (gnus-data-pos data))
5870       (gnus-summary-position-point)
5871       article)))
5872
5873 ;; Walking around summary lines with displaying articles.
5874
5875 (defun gnus-summary-expand-window (&optional arg)
5876   "Make the summary buffer take up the entire Emacs frame.
5877 Given a prefix, will force an `article' buffer configuration."
5878   (interactive "P")
5879   (if arg
5880       (gnus-configure-windows 'article 'force)
5881     (gnus-configure-windows 'summary 'force)))
5882
5883 (defun gnus-summary-display-article (article &optional all-header)
5884   "Display ARTICLE in article buffer."
5885   (gnus-set-global-variables)
5886   (if (null article)
5887       nil
5888     (prog1
5889         (if gnus-summary-display-article-function
5890             (funcall gnus-summary-display-article-function article all-header)
5891           (gnus-article-prepare article all-header))
5892       (with-current-buffer gnus-article-buffer
5893         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5894              nil))
5895       (gnus-run-hooks 'gnus-select-article-hook)
5896       (when (and gnus-current-article
5897                  (not (zerop gnus-current-article)))
5898         (gnus-summary-goto-subject gnus-current-article))
5899       (gnus-summary-recenter)
5900       (when (and gnus-use-trees gnus-show-threads)
5901         (gnus-possibly-generate-tree article)
5902         (gnus-highlight-selected-tree article))
5903       ;; Successfully display article.
5904       (gnus-article-set-window-start
5905        (cdr (assq article gnus-newsgroup-bookmarks))))))
5906
5907 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5908   "Select the current article.
5909 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5910 non-nil, the article will be re-fetched even if it already present in
5911 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5912 be displayed."
5913   ;; Make sure we are in the summary buffer to work around bbdb bug.
5914   (unless (eq major-mode 'gnus-summary-mode)
5915     (set-buffer gnus-summary-buffer))
5916   (let ((article (or article (gnus-summary-article-number)))
5917         (all-headers (not (not all-headers))) ;Must be T or NIL.
5918         gnus-summary-display-article-function)
5919     (and (not pseudo)
5920          (gnus-summary-article-pseudo-p article)
5921          (error "This is a pseudo-article"))
5922     (save-excursion
5923       (set-buffer gnus-summary-buffer)
5924       (if (or (and gnus-single-article-buffer
5925                    (or (null gnus-current-article)
5926                        (null gnus-article-current)
5927                        (null (get-buffer gnus-article-buffer))
5928                        (not (eq article (cdr gnus-article-current)))
5929                        (not (equal (car gnus-article-current)
5930                                    gnus-newsgroup-name))))
5931               (and (not gnus-single-article-buffer)
5932                    (or (null gnus-current-article)
5933                        (not (eq gnus-current-article article))))
5934               force)
5935           ;; The requested article is different from the current article.
5936           (progn
5937             (gnus-summary-display-article article all-headers)
5938             (when (or all-headers gnus-show-all-headers)
5939               (gnus-article-show-all-headers))
5940             (gnus-article-set-window-start
5941              (cdr (assq article gnus-newsgroup-bookmarks)))
5942             article)
5943         (when (or all-headers gnus-show-all-headers)
5944           (gnus-article-show-all-headers))
5945         'old))))
5946
5947 (defun gnus-summary-set-current-mark (&optional current-mark)
5948   "Obsolete function."
5949   nil)
5950
5951 (defun gnus-summary-next-article (&optional unread subject backward push)
5952   "Select the next article.
5953 If UNREAD, only unread articles are selected.
5954 If SUBJECT, only articles with SUBJECT are selected.
5955 If BACKWARD, the previous article is selected instead of the next."
5956   (interactive "P")
5957   (cond
5958    ;; Is there such an article?
5959    ((and (gnus-summary-search-forward unread subject backward)
5960          (or (gnus-summary-display-article (gnus-summary-article-number))
5961              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5962     (gnus-summary-position-point))
5963    ;; If not, we try the first unread, if that is wanted.
5964    ((and subject
5965          gnus-auto-select-same
5966          (gnus-summary-first-unread-article))
5967     (gnus-summary-position-point)
5968     (gnus-message 6 "Wrapped"))
5969    ;; Try to get next/previous article not displayed in this group.
5970    ((and gnus-auto-extend-newsgroup
5971          (not unread) (not subject))
5972     (gnus-summary-goto-article
5973      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5974      nil (count-lines (point-min) (point))))
5975    ;; Go to next/previous group.
5976    (t
5977     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5978       (gnus-summary-jump-to-group gnus-newsgroup-name))
5979     (let ((cmd last-command-char)
5980           (point
5981            (save-excursion
5982              (set-buffer gnus-group-buffer)
5983              (point)))
5984           (group
5985            (if (eq gnus-keep-same-level 'best)
5986                (gnus-summary-best-group gnus-newsgroup-name)
5987              (gnus-summary-search-group backward gnus-keep-same-level))))
5988       ;; For some reason, the group window gets selected.  We change
5989       ;; it back.
5990       (select-window (get-buffer-window (current-buffer)))
5991       ;; Select next unread newsgroup automagically.
5992       (cond
5993        ((or (not gnus-auto-select-next)
5994             (not cmd))
5995         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5996        ((or (eq gnus-auto-select-next 'quietly)
5997             (and (eq gnus-auto-select-next 'slightly-quietly)
5998                  push)
5999             (and (eq gnus-auto-select-next 'almost-quietly)
6000                  (gnus-summary-last-article-p)))
6001         ;; Select quietly.
6002         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6003             (gnus-summary-exit)
6004           (gnus-message 7 "No more%s articles (%s)..."
6005                         (if unread " unread" "")
6006                         (if group (concat "selecting " group)
6007                           "exiting"))
6008           (gnus-summary-next-group nil group backward)))
6009        (t
6010         (when (gnus-key-press-event-p last-input-event)
6011           (gnus-summary-walk-group-buffer
6012            gnus-newsgroup-name cmd unread backward point))))))))
6013
6014 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6015   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6016                       (?\C-p (gnus-group-prev-unread-group 1))))
6017         (cursor-in-echo-area t)
6018         keve key group ended)
6019     (save-excursion
6020       (set-buffer gnus-group-buffer)
6021       (goto-char start)
6022       (setq group
6023             (if (eq gnus-keep-same-level 'best)
6024                 (gnus-summary-best-group gnus-newsgroup-name)
6025               (gnus-summary-search-group backward gnus-keep-same-level))))
6026     (while (not ended)
6027       (gnus-message
6028        5 "No more%s articles%s" (if unread " unread" "")
6029        (if (and group
6030                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6031            (format " (Type %s for %s [%s])"
6032                    (single-key-description cmd) group
6033                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6034          (format " (Type %s to exit %s)"
6035                  (single-key-description cmd)
6036                  gnus-newsgroup-name)))
6037       ;; Confirm auto selection.
6038       (setq key (car (setq keve (gnus-read-event-char))))
6039       (setq ended t)
6040       (cond
6041        ((assq key keystrokes)
6042         (let ((obuf (current-buffer)))
6043           (switch-to-buffer gnus-group-buffer)
6044           (when group
6045             (gnus-group-jump-to-group group))
6046           (eval (cadr (assq key keystrokes)))
6047           (setq group (gnus-group-group-name))
6048           (switch-to-buffer obuf))
6049         (setq ended nil))
6050        ((equal key cmd)
6051         (if (or (not group)
6052                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6053             (gnus-summary-exit)
6054           (gnus-summary-next-group nil group backward)))
6055        (t
6056         (push (cdr keve) unread-command-events))))))
6057
6058 (defun gnus-summary-next-unread-article ()
6059   "Select unread article after current one."
6060   (interactive)
6061   (gnus-summary-next-article
6062    (or (not (eq gnus-summary-goto-unread 'never))
6063        (gnus-summary-last-article-p (gnus-summary-article-number)))
6064    (and gnus-auto-select-same
6065         (gnus-summary-article-subject))))
6066
6067 (defun gnus-summary-prev-article (&optional unread subject)
6068   "Select the article after the current one.
6069 If UNREAD is non-nil, only unread articles are selected."
6070   (interactive "P")
6071   (gnus-summary-next-article unread subject t))
6072
6073 (defun gnus-summary-prev-unread-article ()
6074   "Select unread article before current one."
6075   (interactive)
6076   (gnus-summary-prev-article
6077    (or (not (eq gnus-summary-goto-unread 'never))
6078        (gnus-summary-first-article-p (gnus-summary-article-number)))
6079    (and gnus-auto-select-same
6080         (gnus-summary-article-subject))))
6081
6082 (defun gnus-summary-next-page (&optional lines circular)
6083   "Show next page of the selected article.
6084 If at the end of the current article, select the next article.
6085 LINES says how many lines should be scrolled up.
6086
6087 If CIRCULAR is non-nil, go to the start of the article instead of
6088 selecting the next article when reaching the end of the current
6089 article."
6090   (interactive "P")
6091   (setq gnus-summary-buffer (current-buffer))
6092   (gnus-set-global-variables)
6093   (let ((article (gnus-summary-article-number))
6094         (article-window (get-buffer-window gnus-article-buffer t))
6095         endp)
6096     ;; If the buffer is empty, we have no article.
6097     (unless article
6098       (error "No article to select"))
6099     (gnus-configure-windows 'article)
6100     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6101         (if (and (eq gnus-summary-goto-unread 'never)
6102                  (not (gnus-summary-last-article-p article)))
6103             (gnus-summary-next-article)
6104           (gnus-summary-next-unread-article))
6105       (if (or (null gnus-current-article)
6106               (null gnus-article-current)
6107               (/= article (cdr gnus-article-current))
6108               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6109           ;; Selected subject is different from current article's.
6110           (gnus-summary-display-article article)
6111         (when article-window
6112           (gnus-eval-in-buffer-window gnus-article-buffer
6113             (setq endp (gnus-article-next-page lines)))
6114           (when endp
6115             (cond (circular
6116                    (gnus-summary-beginning-of-article))
6117                   (lines
6118                    (gnus-message 3 "End of message"))
6119                   ((null lines)
6120                    (if (and (eq gnus-summary-goto-unread 'never)
6121                             (not (gnus-summary-last-article-p article)))
6122                        (gnus-summary-next-article)
6123                      (gnus-summary-next-unread-article))))))))
6124     (gnus-summary-recenter)
6125     (gnus-summary-position-point)))
6126
6127 (defun gnus-summary-prev-page (&optional lines move)
6128   "Show previous page of selected article.
6129 Argument LINES specifies lines to be scrolled down.
6130 If MOVE, move to the previous unread article if point is at
6131 the beginning of the buffer."
6132   (interactive "P")
6133   (let ((article (gnus-summary-article-number))
6134         (article-window (get-buffer-window gnus-article-buffer t))
6135         endp)
6136     (gnus-configure-windows 'article)
6137     (if (or (null gnus-current-article)
6138             (null gnus-article-current)
6139             (/= article (cdr gnus-article-current))
6140             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6141         ;; Selected subject is different from current article's.
6142         (gnus-summary-display-article article)
6143       (gnus-summary-recenter)
6144       (when article-window
6145         (gnus-eval-in-buffer-window gnus-article-buffer
6146           (setq endp (gnus-article-prev-page lines)))
6147         (when (and move endp)
6148           (cond (lines
6149                  (gnus-message 3 "Beginning of message"))
6150                 ((null lines)
6151                  (if (and (eq gnus-summary-goto-unread 'never)
6152                           (not (gnus-summary-first-article-p article)))
6153                      (gnus-summary-prev-article)
6154                    (gnus-summary-prev-unread-article))))))))
6155   (gnus-summary-position-point))
6156
6157 (defun gnus-summary-prev-page-or-article (&optional lines)
6158   "Show previous page of selected article.
6159 Argument LINES specifies lines to be scrolled down.
6160 If at the beginning of the article, go to the next article."
6161   (interactive "P")
6162   (gnus-summary-prev-page lines t))
6163
6164 (defun gnus-summary-scroll-up (lines)
6165   "Scroll up (or down) one line current article.
6166 Argument LINES specifies lines to be scrolled up (or down if negative)."
6167   (interactive "p")
6168   (gnus-configure-windows 'article)
6169   (gnus-summary-show-thread)
6170   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6171     (gnus-eval-in-buffer-window gnus-article-buffer
6172       (cond ((> lines 0)
6173              (when (gnus-article-next-page lines)
6174                (gnus-message 3 "End of message")))
6175             ((< lines 0)
6176              (gnus-article-prev-page (- lines))))))
6177   (gnus-summary-recenter)
6178   (gnus-summary-position-point))
6179
6180 (defun gnus-summary-scroll-down (lines)
6181   "Scroll down (or up) one line current article.
6182 Argument LINES specifies lines to be scrolled down (or up if negative)."
6183   (interactive "p")
6184   (gnus-summary-scroll-up (- lines)))
6185
6186 (defun gnus-summary-next-same-subject ()
6187   "Select next article which has the same subject as current one."
6188   (interactive)
6189   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6190
6191 (defun gnus-summary-prev-same-subject ()
6192   "Select previous article which has the same subject as current one."
6193   (interactive)
6194   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6195
6196 (defun gnus-summary-next-unread-same-subject ()
6197   "Select next unread article which has the same subject as current one."
6198   (interactive)
6199   (gnus-summary-next-article t (gnus-summary-article-subject)))
6200
6201 (defun gnus-summary-prev-unread-same-subject ()
6202   "Select previous unread article which has the same subject as current one."
6203   (interactive)
6204   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6205
6206 (defun gnus-summary-first-unread-article ()
6207   "Select 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-display-article (gnus-summary-article-number)))
6215     (gnus-summary-position-point)))
6216
6217 (defun gnus-summary-first-unread-subject ()
6218   "Place the point on the subject line of the first unread article.
6219 Return nil if there are no unread articles."
6220   (interactive)
6221   (prog1
6222       (when (gnus-summary-first-subject t)
6223         (gnus-summary-show-thread)
6224         (gnus-summary-first-subject t))
6225     (gnus-summary-position-point)))
6226
6227 (defun gnus-summary-first-article ()
6228   "Select the first article.
6229 Return nil if there are no articles."
6230   (interactive)
6231   (prog1
6232       (when (gnus-summary-first-subject)
6233         (gnus-summary-show-thread)
6234         (gnus-summary-first-subject)
6235         (gnus-summary-display-article (gnus-summary-article-number)))
6236     (gnus-summary-position-point)))
6237
6238 (defun gnus-summary-best-unread-article ()
6239   "Select the unread article with the highest score."
6240   (interactive)
6241   (let ((best -1000000)
6242         (data gnus-newsgroup-data)
6243         article score)
6244     (while data
6245       (and (gnus-data-unread-p (car data))
6246            (> (setq score
6247                     (gnus-summary-article-score (gnus-data-number (car data))))
6248               best)
6249            (setq best score
6250                  article (gnus-data-number (car data))))
6251       (setq data (cdr data)))
6252     (prog1
6253         (if article
6254             (gnus-summary-goto-article article)
6255           (error "No unread articles"))
6256       (gnus-summary-position-point))))
6257
6258 (defun gnus-summary-last-subject ()
6259   "Go to the last displayed subject line in the group."
6260   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6261     (when article
6262       (gnus-summary-goto-subject article))))
6263
6264 (defun gnus-summary-goto-article (article &optional all-headers force)
6265   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6266 If ALL-HEADERS is non-nil, no header lines are hidden.
6267 If FORCE, go to the article even if it isn't displayed.  If FORCE
6268 is a number, it is the line the article is to be displayed on."
6269   (interactive
6270    (list
6271     (completing-read
6272      "Article number or Message-ID: "
6273      (mapcar (lambda (number) (list (int-to-string number)))
6274              gnus-newsgroup-limit))
6275     current-prefix-arg
6276     t))
6277   (prog1
6278       (if (and (stringp article)
6279                (string-match "@" article))
6280           (gnus-summary-refer-article article)
6281         (when (stringp article)
6282           (setq article (string-to-number article)))
6283         (if (gnus-summary-goto-subject article force)
6284             (gnus-summary-display-article article all-headers)
6285           (gnus-message 4 "Couldn't go to article %s" article) nil))
6286     (gnus-summary-position-point)))
6287
6288 (defun gnus-summary-goto-last-article ()
6289   "Go to the previously read article."
6290   (interactive)
6291   (prog1
6292       (when gnus-last-article
6293         (gnus-summary-goto-article gnus-last-article nil t))
6294     (gnus-summary-position-point)))
6295
6296 (defun gnus-summary-pop-article (number)
6297   "Pop one article off the history and go to the previous.
6298 NUMBER articles will be popped off."
6299   (interactive "p")
6300   (let (to)
6301     (setq gnus-newsgroup-history
6302           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6303     (if to
6304         (gnus-summary-goto-article (car to) nil t)
6305       (error "Article history empty")))
6306   (gnus-summary-position-point))
6307
6308 ;; Summary commands and functions for limiting the summary buffer.
6309
6310 (defun gnus-summary-limit-to-articles (n)
6311   "Limit the summary buffer to the next N articles.
6312 If not given a prefix, use the process marked articles instead."
6313   (interactive "P")
6314   (prog1
6315       (let ((articles (gnus-summary-work-articles n)))
6316         (setq gnus-newsgroup-processable nil)
6317         (gnus-summary-limit articles))
6318     (gnus-summary-position-point)))
6319
6320 (defun gnus-summary-pop-limit (&optional total)
6321   "Restore the previous limit.
6322 If given a prefix, remove all limits."
6323   (interactive "P")
6324   (when total
6325     (setq gnus-newsgroup-limits
6326           (list (mapcar (lambda (h) (mail-header-number h))
6327                         gnus-newsgroup-headers))))
6328   (unless gnus-newsgroup-limits
6329     (error "No limit to pop"))
6330   (prog1
6331       (gnus-summary-limit nil 'pop)
6332     (gnus-summary-position-point)))
6333
6334 (defun gnus-summary-limit-to-subject (subject &optional header)
6335   "Limit the summary buffer to articles that have subjects that match a regexp."
6336   (interactive "sLimit to subject (regexp): ")
6337   (unless header
6338     (setq header "subject"))
6339   (when (not (equal "" subject))
6340     (prog1
6341         (let ((articles (gnus-summary-find-matching
6342                          (or header "subject") subject 'all)))
6343           (unless articles
6344             (error "Found no matches for \"%s\"" subject))
6345           (gnus-summary-limit articles))
6346       (gnus-summary-position-point))))
6347
6348 (defun gnus-summary-limit-to-author (from)
6349   "Limit the summary buffer to articles that have authors that match a regexp."
6350   (interactive "sLimit to author (regexp): ")
6351   (gnus-summary-limit-to-subject from "from"))
6352
6353 (defun gnus-summary-limit-to-age (age &optional younger-p)
6354   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6355 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6356 articles that are younger than AGE days."
6357   (interactive
6358    (let ((younger current-prefix-arg)
6359          (days-got nil)
6360          days)
6361      (while (not days-got)
6362        (setq days (if younger
6363                       (read-string "Limit to articles within (in days): ")
6364                     (read-string "Limit to articles old than (in days): ")))
6365        (when (> (length days) 0)
6366          (setq days (read days)))
6367        (if (numberp days)
6368            (setq days-got t)
6369          (message "Please enter a number.")
6370          (sleep-for 1)))
6371      (list days younger)))
6372   (prog1
6373       (let ((data gnus-newsgroup-data)
6374             (cutoff (days-to-time age))
6375             articles d date is-younger)
6376         (while (setq d (pop data))
6377           (when (and (vectorp (gnus-data-header d))
6378                      (setq date (mail-header-date (gnus-data-header d))))
6379             (setq is-younger (time-less-p
6380                               (time-since (condition-case ()
6381                                               (date-to-time date)
6382                                             (error '(0 0))))
6383                               cutoff))
6384             (when (if younger-p
6385                       is-younger
6386                     (not is-younger))
6387               (push (gnus-data-number d) articles))))
6388         (gnus-summary-limit (nreverse articles)))
6389     (gnus-summary-position-point)))
6390
6391 (defun gnus-summary-limit-to-extra (header regexp)
6392   "Limit the summary buffer to articles that match an 'extra' header."
6393   (interactive
6394    (let ((header
6395           (intern
6396            (gnus-completing-read
6397             (symbol-name (car gnus-extra-headers))
6398             "Limit extra header:"
6399             (mapcar (lambda (x)
6400                       (cons (symbol-name x) x))
6401                     gnus-extra-headers)
6402             nil
6403             t))))
6404      (list header
6405            (read-string (format "Limit to header %s (regexp): " header)))))
6406   (when (not (equal "" regexp))
6407     (prog1
6408         (let ((articles (gnus-summary-find-matching
6409                          (cons 'extra header) regexp 'all)))
6410           (unless articles
6411             (error "Found no matches for \"%s\"" regexp))
6412           (gnus-summary-limit articles))
6413       (gnus-summary-position-point))))
6414
6415 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6416 (make-obsolete
6417  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6418
6419 (defun gnus-summary-limit-to-unread (&optional all)
6420   "Limit the summary buffer to articles that are not marked as read.
6421 If ALL is non-nil, limit strictly to unread articles."
6422   (interactive "P")
6423   (if all
6424       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6425     (gnus-summary-limit-to-marks
6426      ;; Concat all the marks that say that an article is read and have
6427      ;; those removed.
6428      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6429            gnus-killed-mark gnus-kill-file-mark
6430            gnus-low-score-mark gnus-expirable-mark
6431            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6432            gnus-duplicate-mark gnus-souped-mark)
6433      'reverse)))
6434
6435 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6436 (make-obsolete 'gnus-summary-delete-marked-with
6437                'gnus-summary-limit-exlude-marks)
6438
6439 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6440   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6441 If REVERSE, limit the summary buffer to articles that are marked
6442 with MARKS.  MARKS can either be a string of marks or a list of marks.
6443 Returns how many articles were removed."
6444   (interactive "sMarks: ")
6445   (gnus-summary-limit-to-marks marks t))
6446
6447 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6448   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6449 If REVERSE (the prefix), limit the summary buffer to articles that are
6450 not marked with MARKS.  MARKS can either be a string of marks or a
6451 list of marks.
6452 Returns how many articles were removed."
6453   (interactive "sMarks: \nP")
6454   (prog1
6455       (let ((data gnus-newsgroup-data)
6456             (marks (if (listp marks) marks
6457                      (append marks nil))) ; Transform to list.
6458             articles)
6459         (while data
6460           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6461                   (memq (gnus-data-mark (car data)) marks))
6462             (push (gnus-data-number (car data)) articles))
6463           (setq data (cdr data)))
6464         (gnus-summary-limit articles))
6465     (gnus-summary-position-point)))
6466
6467 (defun gnus-summary-limit-to-score (&optional score)
6468   "Limit to articles with score at or above SCORE."
6469   (interactive "P")
6470   (setq score (if score
6471                   (prefix-numeric-value score)
6472                 (or gnus-summary-default-score 0)))
6473   (let ((data gnus-newsgroup-data)
6474         articles)
6475     (while data
6476       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6477                 score)
6478         (push (gnus-data-number (car data)) articles))
6479       (setq data (cdr data)))
6480     (prog1
6481         (gnus-summary-limit articles)
6482       (gnus-summary-position-point))))
6483
6484 (defun gnus-summary-limit-include-thread (id)
6485   "Display all the hidden articles that in the current thread."
6486   (interactive (list (mail-header-id (gnus-summary-article-header))))
6487   (let ((articles (gnus-articles-in-thread
6488                    (gnus-id-to-thread (gnus-root-id id)))))
6489     (prog1
6490         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6491       (gnus-summary-position-point))))
6492
6493 (defun gnus-summary-limit-include-dormant ()
6494   "Display all the hidden articles that are marked as dormant.
6495 Note that this command only works on a subset of the articles currently
6496 fetched for this group."
6497   (interactive)
6498   (unless gnus-newsgroup-dormant
6499     (error "There are no dormant articles in this group"))
6500   (prog1
6501       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6502     (gnus-summary-position-point)))
6503
6504 (defun gnus-summary-limit-exclude-dormant ()
6505   "Hide all dormant articles."
6506   (interactive)
6507   (prog1
6508       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6509     (gnus-summary-position-point)))
6510
6511 (defun gnus-summary-limit-exclude-childless-dormant ()
6512   "Hide all dormant articles that have no children."
6513   (interactive)
6514   (let ((data (gnus-data-list t))
6515         articles d children)
6516     ;; Find all articles that are either not dormant or have
6517     ;; children.
6518     (while (setq d (pop data))
6519       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6520                 (and (setq children
6521                            (gnus-article-children (gnus-data-number d)))
6522                      (let (found)
6523                        (while children
6524                          (when (memq (car children) articles)
6525                            (setq children nil
6526                                  found t))
6527                          (pop children))
6528                        found)))
6529         (push (gnus-data-number d) articles)))
6530     ;; Do the limiting.
6531     (prog1
6532         (gnus-summary-limit articles)
6533       (gnus-summary-position-point))))
6534
6535 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6536   "Mark all unread excluded articles as read.
6537 If ALL, mark even excluded ticked and dormants as read."
6538   (interactive "P")
6539   (let ((articles (gnus-sorted-complement
6540                    (sort
6541                     (mapcar (lambda (h) (mail-header-number h))
6542                             gnus-newsgroup-headers)
6543                     '<)
6544                    (sort gnus-newsgroup-limit '<)))
6545         article)
6546     (setq gnus-newsgroup-unreads
6547           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6548     (if all
6549         (setq gnus-newsgroup-dormant nil
6550               gnus-newsgroup-marked nil
6551               gnus-newsgroup-reads
6552               (nconc
6553                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6554                gnus-newsgroup-reads))
6555       (while (setq article (pop articles))
6556         (unless (or (memq article gnus-newsgroup-dormant)
6557                     (memq article gnus-newsgroup-marked))
6558           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6559
6560 (defun gnus-summary-limit (articles &optional pop)
6561   (if pop
6562       ;; We pop the previous limit off the stack and use that.
6563       (setq articles (car gnus-newsgroup-limits)
6564             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6565     ;; We use the new limit, so we push the old limit on the stack.
6566     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6567   ;; Set the limit.
6568   (setq gnus-newsgroup-limit articles)
6569   (let ((total (length gnus-newsgroup-data))
6570         (data (gnus-data-find-list (gnus-summary-article-number)))
6571         (gnus-summary-mark-below nil)   ; Inhibit this.
6572         found)
6573     ;; This will do all the work of generating the new summary buffer
6574     ;; according to the new limit.
6575     (gnus-summary-prepare)
6576     ;; Hide any threads, possibly.
6577     (and gnus-show-threads
6578          gnus-thread-hide-subtree
6579          (gnus-summary-hide-all-threads))
6580     ;; Try to return to the article you were at, or one in the
6581     ;; neighborhood.
6582     (when data
6583       ;; We try to find some article after the current one.
6584       (while data
6585         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6586           (setq data nil
6587                 found t))
6588         (setq data (cdr data))))
6589     (unless found
6590       ;; If there is no data, that means that we were after the last
6591       ;; article.  The same goes when we can't find any articles
6592       ;; after the current one.
6593       (goto-char (point-max))
6594       (gnus-summary-find-prev))
6595     (gnus-set-mode-line 'summary)
6596     ;; We return how many articles were removed from the summary
6597     ;; buffer as a result of the new limit.
6598     (- total (length gnus-newsgroup-data))))
6599
6600 (defsubst gnus-invisible-cut-children (threads)
6601   (let ((num 0))
6602     (while threads
6603       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6604         (incf num))
6605       (pop threads))
6606     (< num 2)))
6607
6608 (defsubst gnus-cut-thread (thread)
6609   "Go forwards in the thread until we find an article that we want to display."
6610   (when (or (eq gnus-fetch-old-headers 'some)
6611             (eq gnus-fetch-old-headers 'invisible)
6612             (numberp gnus-fetch-old-headers)
6613             (eq gnus-build-sparse-threads 'some)
6614             (eq gnus-build-sparse-threads 'more))
6615     ;; Deal with old-fetched headers and sparse threads.
6616     (while (and
6617             thread
6618             (or
6619              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6620              (gnus-summary-article-ancient-p
6621               (mail-header-number (car thread))))
6622             (if (or (<= (length (cdr thread)) 1)
6623                     (eq gnus-fetch-old-headers 'invisible))
6624                 (setq gnus-newsgroup-limit
6625                       (delq (mail-header-number (car thread))
6626                             gnus-newsgroup-limit)
6627                       thread (cadr thread))
6628               (when (gnus-invisible-cut-children (cdr thread))
6629                 (let ((th (cdr thread)))
6630                   (while th
6631                     (if (memq (mail-header-number (caar th))
6632                               gnus-newsgroup-limit)
6633                         (setq thread (car th)
6634                               th nil)
6635                       (setq th (cdr th))))))))))
6636   thread)
6637
6638 (defun gnus-cut-threads (threads)
6639   "Cut off all uninteresting articles from the beginning of threads."
6640   (when (or (eq gnus-fetch-old-headers 'some)
6641             (eq gnus-fetch-old-headers 'invisible)
6642             (numberp gnus-fetch-old-headers)
6643             (eq gnus-build-sparse-threads 'some)
6644             (eq gnus-build-sparse-threads 'more))
6645     (let ((th threads))
6646       (while th
6647         (setcar th (gnus-cut-thread (car th)))
6648         (setq th (cdr th)))))
6649   ;; Remove nixed out threads.
6650   (delq nil threads))
6651
6652 (defun gnus-summary-initial-limit (&optional show-if-empty)
6653   "Figure out what the initial limit is supposed to be on group entry.
6654 This entails weeding out unwanted dormants, low-scored articles,
6655 fetch-old-headers verbiage, and so on."
6656   ;; Most groups have nothing to remove.
6657   (if (or gnus-inhibit-limiting
6658           (and (null gnus-newsgroup-dormant)
6659                (not (eq gnus-fetch-old-headers 'some))
6660                (not (numberp gnus-fetch-old-headers))
6661                (not (eq gnus-fetch-old-headers 'invisible))
6662                (null gnus-summary-expunge-below)
6663                (not (eq gnus-build-sparse-threads 'some))
6664                (not (eq gnus-build-sparse-threads 'more))
6665                (null gnus-thread-expunge-below)
6666                (not gnus-use-nocem)))
6667       ()                                ; Do nothing.
6668     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6669     (setq gnus-newsgroup-limit nil)
6670     (mapatoms
6671      (lambda (node)
6672        (unless (car (symbol-value node))
6673          ;; These threads have no parents -- they are roots.
6674          (let ((nodes (cdr (symbol-value node)))
6675                thread)
6676            (while nodes
6677              (if (and gnus-thread-expunge-below
6678                       (< (gnus-thread-total-score (car nodes))
6679                          gnus-thread-expunge-below))
6680                  (gnus-expunge-thread (pop nodes))
6681                (setq thread (pop nodes))
6682                (gnus-summary-limit-children thread))))))
6683      gnus-newsgroup-dependencies)
6684     ;; If this limitation resulted in an empty group, we might
6685     ;; pop the previous limit and use it instead.
6686     (when (and (not gnus-newsgroup-limit)
6687                show-if-empty)
6688       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6689     gnus-newsgroup-limit))
6690
6691 (defun gnus-summary-limit-children (thread)
6692   "Return 1 if this subthread is visible and 0 if it is not."
6693   ;; First we get the number of visible children to this thread.  This
6694   ;; is done by recursing down the thread using this function, so this
6695   ;; will really go down to a leaf article first, before slowly
6696   ;; working its way up towards the root.
6697   (when thread
6698     (let ((children
6699            (if (cdr thread)
6700                (apply '+ (mapcar 'gnus-summary-limit-children
6701                                  (cdr thread)))
6702              0))
6703           (number (mail-header-number (car thread)))
6704           score)
6705       (if (and
6706            (not (memq number gnus-newsgroup-marked))
6707            (or
6708             ;; If this article is dormant and has absolutely no visible
6709             ;; children, then this article isn't visible.
6710             (and (memq number gnus-newsgroup-dormant)
6711                  (zerop children))
6712             ;; If this is "fetch-old-headered" and there is no
6713             ;; visible children, then we don't want this article.
6714             (and (or (eq gnus-fetch-old-headers 'some)
6715                      (numberp gnus-fetch-old-headers))
6716                  (gnus-summary-article-ancient-p number)
6717                  (zerop children))
6718             ;; If this is "fetch-old-headered" and `invisible', then
6719             ;; we don't want this article.
6720             (and (eq gnus-fetch-old-headers 'invisible)
6721                  (gnus-summary-article-ancient-p number))
6722             ;; If this is a sparsely inserted article with no children,
6723             ;; we don't want it.
6724             (and (eq gnus-build-sparse-threads 'some)
6725                  (gnus-summary-article-sparse-p number)
6726                  (zerop children))
6727             ;; If we use expunging, and this article is really
6728             ;; low-scored, then we don't want this article.
6729             (when (and gnus-summary-expunge-below
6730                        (< (setq score
6731                                 (or (cdr (assq number gnus-newsgroup-scored))
6732                                     gnus-summary-default-score))
6733                           gnus-summary-expunge-below))
6734               ;; We increase the expunge-tally here, but that has
6735               ;; nothing to do with the limits, really.
6736               (incf gnus-newsgroup-expunged-tally)
6737               ;; We also mark as read here, if that's wanted.
6738               (when (and gnus-summary-mark-below
6739                          (< score gnus-summary-mark-below))
6740                 (setq gnus-newsgroup-unreads
6741                       (delq number gnus-newsgroup-unreads))
6742                 (if gnus-newsgroup-auto-expire
6743                     (push number gnus-newsgroup-expirable)
6744                   (push (cons number gnus-low-score-mark)
6745                         gnus-newsgroup-reads)))
6746               t)
6747             ;; Check NoCeM things.
6748             (if (and gnus-use-nocem
6749                      (gnus-nocem-unwanted-article-p
6750                       (mail-header-id (car thread))))
6751                 (progn
6752                   (setq gnus-newsgroup-unreads
6753                         (delq number gnus-newsgroup-unreads))
6754                   t))))
6755           ;; Nope, invisible article.
6756           0
6757         ;; Ok, this article is to be visible, so we add it to the limit
6758         ;; and return 1.
6759         (push number gnus-newsgroup-limit)
6760         1))))
6761
6762 (defun gnus-expunge-thread (thread)
6763   "Mark all articles in THREAD as read."
6764   (let* ((number (mail-header-number (car thread))))
6765     (incf gnus-newsgroup-expunged-tally)
6766     ;; We also mark as read here, if that's wanted.
6767     (setq gnus-newsgroup-unreads
6768           (delq number gnus-newsgroup-unreads))
6769     (if gnus-newsgroup-auto-expire
6770         (push number gnus-newsgroup-expirable)
6771       (push (cons number gnus-low-score-mark)
6772             gnus-newsgroup-reads)))
6773   ;; Go recursively through all subthreads.
6774   (mapcar 'gnus-expunge-thread (cdr thread)))
6775
6776 ;; Summary article oriented commands
6777
6778 (defun gnus-summary-refer-parent-article (n)
6779   "Refer parent article N times.
6780 If N is negative, go to ancestor -N instead.
6781 The difference between N and the number of articles fetched is returned."
6782   (interactive "p")
6783   (let ((skip 1)
6784         error header ref)
6785     (when (not (natnump n))
6786       (setq skip (abs n)
6787             n 1))
6788     (while (and (> n 0)
6789                 (not error))
6790       (setq header (gnus-summary-article-header))
6791       (if (and (eq (mail-header-number header)
6792                    (cdr gnus-article-current))
6793                (equal gnus-newsgroup-name
6794                       (car gnus-article-current)))
6795           ;; If we try to find the parent of the currently
6796           ;; displayed article, then we take a look at the actual
6797           ;; References header, since this is slightly more
6798           ;; reliable than the References field we got from the
6799           ;; server.
6800           (save-excursion
6801             (set-buffer gnus-original-article-buffer)
6802             (nnheader-narrow-to-headers)
6803             (unless (setq ref (message-fetch-field "references"))
6804               (setq ref (message-fetch-field "in-reply-to")))
6805             (widen))
6806         (setq ref
6807               ;; It's not the current article, so we take a bet on
6808               ;; the value we got from the server.
6809               (mail-header-references header)))
6810       (if (and ref
6811                (not (equal ref "")))
6812           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6813             (gnus-message 1 "Couldn't find parent"))
6814         (gnus-message 1 "No references in article %d"
6815                       (gnus-summary-article-number))
6816         (setq error t))
6817       (decf n))
6818     (gnus-summary-position-point)
6819     n))
6820
6821 (defun gnus-summary-refer-references ()
6822   "Fetch all articles mentioned in the References header.
6823 Return the number of articles fetched."
6824   (interactive)
6825   (let ((ref (mail-header-references (gnus-summary-article-header)))
6826         (current (gnus-summary-article-number))
6827         (n 0))
6828     (if (or (not ref)
6829             (equal ref ""))
6830         (error "No References in the current article")
6831       ;; For each Message-ID in the References header...
6832       (while (string-match "<[^>]*>" ref)
6833         (incf n)
6834         ;; ... fetch that article.
6835         (gnus-summary-refer-article
6836          (prog1 (match-string 0 ref)
6837            (setq ref (substring ref (match-end 0))))))
6838       (gnus-summary-goto-subject current)
6839       (gnus-summary-position-point)
6840       n)))
6841
6842 (defun gnus-summary-refer-thread (&optional limit)
6843   "Fetch all articles in the current thread.
6844 If LIMIT (the numerical prefix), fetch that many old headers instead
6845 of what's specified by the `gnus-refer-thread-limit' variable."
6846   (interactive "P")
6847   (let ((id (mail-header-id (gnus-summary-article-header)))
6848         (limit (if limit (prefix-numeric-value limit)
6849                  gnus-refer-thread-limit)))
6850     ;; We want to fetch LIMIT *old* headers, but we also have to
6851     ;; re-fetch all the headers in the current buffer, because many of
6852     ;; them may be undisplayed.  So we adjust LIMIT.
6853     (when (numberp limit)
6854       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6855     (unless (eq gnus-fetch-old-headers 'invisible)
6856       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6857       ;; Retrieve the headers and read them in.
6858       (if (eq (gnus-retrieve-headers
6859                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6860               'nov)
6861           (gnus-build-all-threads)
6862         (error "Can't fetch thread from backends that don't support NOV"))
6863       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6864     (gnus-summary-limit-include-thread id)))
6865
6866 (defun gnus-summary-refer-article (message-id)
6867   "Fetch an article specified by MESSAGE-ID."
6868   (interactive "sMessage-ID: ")
6869   (when (and (stringp message-id)
6870              (not (zerop (length message-id))))
6871     ;; Construct the correct Message-ID if necessary.
6872     ;; Suggested by tale@pawl.rpi.edu.
6873     (unless (string-match "^<" message-id)
6874       (setq message-id (concat "<" message-id)))
6875     (unless (string-match ">$" message-id)
6876       (setq message-id (concat message-id ">")))
6877     (let* ((header (gnus-id-to-header message-id))
6878            (sparse (and header
6879                         (gnus-summary-article-sparse-p
6880                          (mail-header-number header))
6881                         (memq (mail-header-number header)
6882                               gnus-newsgroup-limit)))
6883            number)
6884       (cond
6885        ;; If the article is present in the buffer we just go to it.
6886        ((and header
6887              (or (not (gnus-summary-article-sparse-p
6888                        (mail-header-number header)))
6889                  sparse))
6890         (prog1
6891             (gnus-summary-goto-article
6892              (mail-header-number header) nil t)
6893           (when sparse
6894             (gnus-summary-update-article (mail-header-number header)))))
6895        (t
6896         ;; We fetch the article.
6897         (catch 'found
6898           (dolist (gnus-override-method (gnus-refer-article-methods))
6899             (gnus-check-server gnus-override-method)
6900             ;; Fetch the header, and display the article.
6901             (when (setq number (gnus-summary-insert-subject message-id))
6902               (gnus-summary-select-article nil nil nil number)
6903               (throw 'found t)))
6904           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6905
6906 (defun gnus-refer-article-methods ()
6907   "Return a list of referrable methods."
6908   (cond
6909    ;; No method, so we default to current and native.
6910    ((null gnus-refer-article-method)
6911     (list gnus-current-select-method gnus-select-method))
6912    ;; Current.
6913    ((eq 'current gnus-refer-article-method)
6914     (list gnus-current-select-method))
6915    ;; List of select methods.
6916    ((not (stringp (cadr gnus-refer-article-method)))
6917     (let (out)
6918       (dolist (method gnus-refer-article-method)
6919         (push (if (eq 'current method)
6920                   gnus-current-select-method
6921                 method)
6922               out))
6923       (nreverse out)))
6924    ;; One single select method.
6925    (t
6926     (list gnus-refer-article-method))))
6927
6928 (defun gnus-summary-edit-parameters ()
6929   "Edit the group parameters of the current group."
6930   (interactive)
6931   (gnus-group-edit-group gnus-newsgroup-name 'params))
6932
6933 (defun gnus-summary-customize-parameters ()
6934   "Customize the group parameters of the current group."
6935   (interactive)
6936   (gnus-group-customize gnus-newsgroup-name))
6937
6938 (defun gnus-summary-enter-digest-group (&optional force)
6939   "Enter an nndoc group based on the current article.
6940 If FORCE, force a digest interpretation.  If not, try
6941 to guess what the document format is."
6942   (interactive "P")
6943   (let ((conf gnus-current-window-configuration))
6944     (save-excursion
6945       (gnus-summary-select-article))
6946     (setq gnus-current-window-configuration conf)
6947     (let* ((name (format "%s-%d"
6948                          (gnus-group-prefixed-name
6949                           gnus-newsgroup-name (list 'nndoc ""))
6950                          (save-excursion
6951                            (set-buffer gnus-summary-buffer)
6952                            gnus-current-article)))
6953            (ogroup gnus-newsgroup-name)
6954            (params (append (gnus-info-params (gnus-get-info ogroup))
6955                            (list (cons 'to-group ogroup))
6956                            (list (cons 'save-article-group ogroup))))
6957            (case-fold-search t)
6958            (buf (current-buffer))
6959            dig to-address)
6960       (save-excursion
6961         (set-buffer gnus-original-article-buffer)
6962         ;; Have the digest group inherit the main mail address of
6963         ;; the parent article.
6964         (when (setq to-address (or (message-fetch-field "reply-to")
6965                                    (message-fetch-field "from")))
6966           (setq params (append (list (cons 'to-address to-address)))))
6967         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6968         (insert-buffer-substring gnus-original-article-buffer)
6969         ;; Remove lines that may lead nndoc to misinterpret the
6970         ;; document type.
6971         (narrow-to-region
6972          (goto-char (point-min))
6973          (or (search-forward "\n\n" nil t) (point)))
6974         (goto-char (point-min))
6975         (delete-matching-lines "^Path:\\|^From ")
6976         (widen))
6977       (unwind-protect
6978           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6979                     (gnus-newsgroup-ephemeral-ignored-charsets
6980                      gnus-newsgroup-ignored-charsets))
6981                 (gnus-group-read-ephemeral-group
6982                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6983                               (nndoc-article-type
6984                                ,(if force 'mbox 'guess))) t))
6985               ;; Make all postings to this group go to the parent group.
6986               (nconc (gnus-info-params (gnus-get-info name))
6987                      params)
6988             ;; Couldn't select this doc group.
6989             (switch-to-buffer buf)
6990             (gnus-set-global-variables)
6991             (gnus-configure-windows 'summary)
6992             (gnus-message 3 "Article couldn't be entered?"))
6993         (kill-buffer dig)))))
6994
6995 (defun gnus-summary-read-document (n)
6996   "Open a new group based on the current article(s).
6997 This will allow you to read digests and other similar
6998 documents as newsgroups.
6999 Obeys the standard process/prefix convention."
7000   (interactive "P")
7001   (let* ((articles (gnus-summary-work-articles n))
7002          (ogroup gnus-newsgroup-name)
7003          (params (append (gnus-info-params (gnus-get-info ogroup))
7004                          (list (cons 'to-group ogroup))))
7005          article group egroup groups vgroup)
7006     (while (setq article (pop articles))
7007       (setq group (format "%s-%d" gnus-newsgroup-name article))
7008       (gnus-summary-remove-process-mark article)
7009       (when (gnus-summary-display-article article)
7010         (save-excursion
7011           (with-temp-buffer
7012             (insert-buffer-substring gnus-original-article-buffer)
7013             ;; Remove some headers that may lead nndoc to make
7014             ;; the wrong guess.
7015             (message-narrow-to-head)
7016             (goto-char (point-min))
7017             (delete-matching-lines "^\\(Path\\):\\|^From ")
7018             (widen)
7019             (if (setq egroup
7020                       (gnus-group-read-ephemeral-group
7021                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7022                                      (nndoc-article-type guess))
7023                        t nil t))
7024                 (progn
7025                   ;; Make all postings to this group go to the parent group.
7026                   (nconc (gnus-info-params (gnus-get-info egroup))
7027                          params)
7028                   (push egroup groups))
7029               ;; Couldn't select this doc group.
7030               (gnus-error 3 "Article couldn't be entered"))))))
7031     ;; Now we have selected all the documents.
7032     (cond
7033      ((not groups)
7034       (error "None of the articles could be interpreted as documents"))
7035      ((gnus-group-read-ephemeral-group
7036        (setq vgroup (format
7037                      "nnvirtual:%s-%s" gnus-newsgroup-name
7038                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7039        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7040        t
7041        (cons (current-buffer) 'summary)))
7042      (t
7043       (error "Couldn't select virtual nndoc group")))))
7044
7045 (defun gnus-summary-isearch-article (&optional regexp-p)
7046   "Do incremental search forward on the current article.
7047 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7048   (interactive "P")
7049   (let* ((gnus-inhibit-treatment t)
7050          (old (gnus-summary-select-article)))
7051     (gnus-configure-windows 'article)
7052     (gnus-eval-in-buffer-window gnus-article-buffer
7053       (save-restriction
7054         (widen)
7055         (when (eq 'old old)
7056           (gnus-article-show-all-headers))
7057         (goto-char (point-min))
7058         (isearch-forward regexp-p)))))
7059
7060 (defun gnus-summary-search-article-forward (regexp &optional backward)
7061   "Search for an article containing REGEXP forward.
7062 If BACKWARD, search backward instead."
7063   (interactive
7064    (list (read-string
7065           (format "Search article %s (regexp%s): "
7066                   (if current-prefix-arg "backward" "forward")
7067                   (if gnus-last-search-regexp
7068                       (concat ", default " gnus-last-search-regexp)
7069                     "")))
7070          current-prefix-arg))
7071   (if (string-equal regexp "")
7072       (setq regexp (or gnus-last-search-regexp ""))
7073     (setq gnus-last-search-regexp regexp))
7074   (if (gnus-summary-search-article regexp backward)
7075       (gnus-summary-show-thread)
7076     (error "Search failed: \"%s\"" regexp)))
7077
7078 (defun gnus-summary-search-article-backward (regexp)
7079   "Search for an article containing REGEXP backward."
7080   (interactive
7081    (list (read-string
7082           (format "Search article backward (regexp%s): "
7083                   (if gnus-last-search-regexp
7084                       (concat ", default " gnus-last-search-regexp)
7085                     "")))))
7086   (gnus-summary-search-article-forward regexp 'backward))
7087
7088 (eval-when-compile
7089   (defmacro gnus-summary-search-article-position-point (regexp backward)
7090     "Dehighlight the last matched text and goto the beginning position."
7091     (` (if (and gnus-summary-search-article-matched-data
7092                 (let ((text (caddr gnus-summary-search-article-matched-data))
7093                       (inhibit-read-only t)
7094                       buffer-read-only)
7095                   (delete-region
7096                    (goto-char (car gnus-summary-search-article-matched-data))
7097                    (cadr gnus-summary-search-article-matched-data))
7098                   (insert text)
7099                   (string-match (, regexp) text)))
7100            (if (, backward) (beginning-of-line) (end-of-line))
7101          (goto-char (if (, backward) (point-max) (point-min))))))
7102
7103   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7104     "Place point where X-Face image is displayed."
7105     (if (featurep 'xemacs)
7106         (` (let ((end (if (search-forward "\n\n" nil t)
7107                           (goto-char (1- (point)))
7108                         (point-min)))
7109                  extent)
7110              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7111              (unless (and (re-search-forward "^From:" end t)
7112                           (setq extent (extent-at (point)))
7113                           (extent-begin-glyph extent))
7114                (goto-char (, opoint)))))
7115       (` (let ((end (if (search-forward "\n\n" nil t)
7116                         (goto-char (1- (point)))
7117                       (point-min))))
7118            (goto-char
7119             (or (text-property-any (or (search-backward "\n\n" nil t)
7120                                        (point-min))
7121                                    end 'x-face-mule-bitmap-image t)
7122                 (, opoint)))))))
7123
7124   (defmacro gnus-summary-search-article-highlight-matched-text
7125     (backward treated x-face)
7126     "Highlight matched text in the function `gnus-summary-search-article'."
7127     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7128              (end (set-marker (make-marker) (match-end 0)))
7129              (inhibit-read-only t)
7130              buffer-read-only)
7131          (unless treated
7132            (let ((,@
7133                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7134                     (mapcar
7135                      (lambda (item) (setq items (delq item items)))
7136                      '(gnus-treat-buttonize
7137                        gnus-treat-fill-article
7138                        gnus-treat-fill-long-lines
7139                        gnus-treat-emphasize
7140                        gnus-treat-highlight-headers
7141                        gnus-treat-highlight-citation
7142                        gnus-treat-highlight-signature
7143                        gnus-treat-overstrike
7144                        gnus-treat-display-xface
7145                        gnus-treat-buttonize-head
7146                        gnus-treat-decode-article-as-default-mime-charset))
7147                     (static-if (featurep 'xemacs)
7148                         items
7149                       (cons '(x-face-mule-delete-x-face-field
7150                               (quote never))
7151                             items))))
7152                  (gnus-treat-display-xface
7153                   (when (, x-face) gnus-treat-display-xface)))
7154              (gnus-article-prepare-mime-display)))
7155          (goto-char (if (, backward) start end))
7156          (when (, x-face)
7157            (gnus-summary-search-article-highlight-goto-x-face (point)))
7158          (setq gnus-summary-search-article-matched-data
7159                (list start end (buffer-substring start end)))
7160          (unless (eq start end);; matched text has been deleted. :-<
7161            (put-text-property start end 'face
7162                               (or (find-face 'isearch)
7163                                   'secondary-selection))))))
7164   )
7165
7166 (defun gnus-summary-search-article (regexp &optional backward)
7167   "Search for an article containing REGEXP.
7168 Optional argument BACKWARD means do search for backward.
7169 `gnus-select-article-hook' is not called during the search."
7170   ;; We have to require this here to make sure that the following
7171   ;; dynamic binding isn't shadowed by autoloading.
7172   (require 'gnus-async)
7173   (require 'gnus-art)
7174   (let ((gnus-select-article-hook nil)  ;Disable hook.
7175         (gnus-article-display-hook nil)
7176         (gnus-article-prepare-hook nil)
7177         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7178         (gnus-use-article-prefetch nil)
7179         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7180         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7181         (sum (current-buffer))
7182         (found nil)
7183         point treated)
7184     (gnus-save-hidden-threads
7185       (static-if (featurep 'xemacs)
7186           (let ((gnus-inhibit-treatment t))
7187             (setq treated (eq 'old (gnus-summary-select-article)))
7188             (when (and treated
7189                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7190                                  (window-live-p (get-buffer-window
7191                                                  gnus-article-buffer t)))))
7192               (gnus-summary-select-article nil t)
7193               (setq treated nil)))
7194         (let ((gnus-inhibit-treatment t)
7195               (x-face-mule-delete-x-face-field 'never))
7196           (setq treated (eq 'old (gnus-summary-select-article)))
7197           (when (and treated
7198                      (not
7199                       (and (gnus-buffer-live-p gnus-article-buffer)
7200                            (window-live-p (get-buffer-window
7201                                            gnus-article-buffer t))
7202                            (or (not (string-match "^\\^X-Face:" regexp))
7203                                (with-current-buffer gnus-article-buffer
7204                                  gnus-summary-search-article-matched-data)))))
7205             (gnus-summary-select-article nil t)
7206             (setq treated nil))))
7207       (set-buffer gnus-article-buffer)
7208       (widen)
7209       (if treated
7210           (progn
7211             (gnus-article-show-all-headers)
7212             (gnus-summary-search-article-position-point regexp backward))
7213         (goto-char (if backward (point-max) (point-min))))
7214       (while (not found)
7215         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7216         (if (if backward
7217                 (re-search-backward regexp nil t)
7218               (re-search-forward regexp nil t))
7219             ;; We found the regexp.
7220             (progn
7221               (gnus-summary-search-article-highlight-matched-text
7222                backward treated (string-match "^\\^X-Face:" regexp))
7223               (setq found 'found)
7224               (forward-line
7225                (/ (- 2 (window-height
7226                         (get-buffer-window gnus-article-buffer t)))
7227                   2))
7228               (set-window-start
7229                (get-buffer-window (current-buffer))
7230                (point))
7231               (set-buffer sum)
7232               (setq point (point)))
7233           ;; We didn't find it, so we go to the next article.
7234           (set-buffer sum)
7235           (setq found 'not)
7236           (while (eq found 'not)
7237             (if (not (if backward (gnus-summary-find-prev)
7238                        (gnus-summary-find-next)))
7239                 ;; No more articles.
7240                 (setq found t)
7241               ;; Select the next article and adjust point.
7242               (unless (gnus-summary-article-sparse-p
7243                        (gnus-summary-article-number))
7244                 (setq found nil)
7245                 (let ((gnus-inhibit-treatment t))
7246                   (gnus-summary-select-article))
7247                 (setq treated nil)
7248                 (set-buffer gnus-article-buffer)
7249                 (widen)
7250                 (goto-char (if backward (point-max) (point-min))))))))
7251       (gnus-message 7 ""))
7252     ;; Return whether we found the regexp.
7253     (when (eq found 'found)
7254       (goto-char point)
7255       (gnus-summary-show-thread)
7256       (gnus-summary-goto-subject gnus-current-article)
7257       (gnus-summary-position-point)
7258       t)))
7259
7260 (defun gnus-summary-find-matching (header regexp &optional backward unread
7261                                           not-case-fold)
7262   "Return a list of all articles that match REGEXP on HEADER.
7263 The search stars on the current article and goes forwards unless
7264 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7265 If UNREAD is non-nil, only unread articles will
7266 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7267 in the comparisons."
7268   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7269                 (gnus-data-find-list
7270                  (gnus-summary-article-number) (gnus-data-list backward))))
7271         (case-fold-search (not not-case-fold))
7272         articles d func)
7273     (if (consp header)
7274         (if (eq (car header) 'extra)
7275             (setq func
7276                   `(lambda (h)
7277                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7278                          "")))
7279           (error "%s is an invalid header" header))
7280       (unless (fboundp (intern (concat "mail-header-" header)))
7281         (error "%s is not a valid header" header))
7282       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7283     (while data
7284       (setq d (car data))
7285       (and (or (not unread)             ; We want all articles...
7286                (gnus-data-unread-p d))  ; Or just unreads.
7287            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7288            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7289            (push (gnus-data-number d) articles)) ; Success!
7290       (setq data (cdr data)))
7291     (nreverse articles)))
7292
7293 (defun gnus-summary-execute-command (header regexp command &optional backward)
7294   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7295 If HEADER is an empty string (or nil), the match is done on the entire
7296 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7297   (interactive
7298    (list (let ((completion-ignore-case t))
7299            (completing-read
7300             "Header name: "
7301             (mapcar (lambda (string) (list string))
7302                     '("Number" "Subject" "From" "Lines" "Date"
7303                       "Message-ID" "Xref" "References" "Body"))
7304             nil 'require-match))
7305          (read-string "Regexp: ")
7306          (read-key-sequence "Command: ")
7307          current-prefix-arg))
7308   (when (equal header "Body")
7309     (setq header ""))
7310   ;; Hidden thread subtrees must be searched as well.
7311   (gnus-summary-show-all-threads)
7312   ;; We don't want to change current point nor window configuration.
7313   (save-excursion
7314     (save-window-excursion
7315       (gnus-message 6 "Executing %s..." (key-description command))
7316       ;; We'd like to execute COMMAND interactively so as to give arguments.
7317       (gnus-execute header regexp
7318                     `(call-interactively ',(key-binding command))
7319                     backward)
7320       (gnus-message 6 "Executing %s...done" (key-description command)))))
7321
7322 (defun gnus-summary-beginning-of-article ()
7323   "Scroll the article back to the beginning."
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-min))
7330     (when gnus-page-broken
7331       (gnus-narrow-to-page))))
7332
7333 (defun gnus-summary-end-of-article ()
7334   "Scroll to the end of the article."
7335   (interactive)
7336   (gnus-summary-select-article)
7337   (gnus-configure-windows 'article)
7338   (gnus-eval-in-buffer-window gnus-article-buffer
7339     (widen)
7340     (goto-char (point-max))
7341     (recenter -3)
7342     (when gnus-page-broken
7343       (gnus-narrow-to-page))))
7344
7345 (defun gnus-summary-print-article (&optional filename n)
7346   "Generate and print a PostScript image of the N next (mail) articles.
7347
7348 If N is negative, print the N previous articles.  If N is nil and articles
7349 have been marked with the process mark, print these instead.
7350
7351 If the optional first argument FILENAME is nil, send the image to the
7352 printer.  If FILENAME is a string, save the PostScript image in a file with
7353 that name.  If FILENAME is a number, prompt the user for the name of the file
7354 to save in."
7355   (interactive (list (ps-print-preprint current-prefix-arg)
7356                      current-prefix-arg))
7357   (dolist (article (gnus-summary-work-articles n))
7358     (gnus-summary-select-article nil nil 'pseudo article)
7359     (gnus-eval-in-buffer-window gnus-article-buffer
7360       (let ((buffer (generate-new-buffer " *print*")))
7361         (unwind-protect
7362             (progn
7363               (copy-to-buffer buffer (point-min) (point-max))
7364               (set-buffer buffer)
7365               (gnus-article-delete-invisible-text)
7366               (let ((ps-left-header
7367                      (list
7368                       (concat "("
7369                               (mail-header-subject gnus-current-headers) ")")
7370                       (concat "("
7371                               (mail-header-from gnus-current-headers) ")")))
7372                     (ps-right-header
7373                      (list
7374                       "/pagenumberstring load"
7375                       (concat "("
7376                               (mail-header-date gnus-current-headers) ")"))))
7377                 (gnus-run-hooks 'gnus-ps-print-hook)
7378                 (save-excursion
7379                   (ps-print-buffer-with-faces filename))))
7380           (kill-buffer buffer))))))
7381
7382 (defun gnus-summary-show-article (&optional arg)
7383   "Force re-fetching of the current article.
7384 If ARG (the prefix) is a number, show the article with the charset
7385 defined in `gnus-summary-show-article-charset-alist', or the charset
7386 inputed.
7387 If ARG (the prefix) is non-nil and not a number, show the raw article
7388 without any article massaging functions being run."
7389   (interactive "P")
7390   (cond
7391    ((numberp arg)
7392     (let ((gnus-newsgroup-charset
7393            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7394                (read-coding-system "Charset: ")))
7395           (gnus-newsgroup-ignored-charsets 'gnus-all))
7396       (gnus-summary-select-article nil 'force)))
7397    ((not arg)
7398     ;; Select the article the normal way.
7399     (gnus-summary-select-article nil 'force))
7400    (t
7401     ;; We have to require this here to make sure that the following
7402     ;; dynamic binding isn't shadowed by autoloading.
7403     (require 'gnus-async)
7404     (require 'gnus-art)
7405     ;; Bind the article treatment functions to nil.
7406     (let ((gnus-have-all-headers t)
7407           gnus-article-display-hook
7408           gnus-article-prepare-hook
7409           gnus-article-decode-hook
7410           gnus-break-pages
7411           gnus-show-mime)
7412       (gnus-summary-select-article nil 'force))))
7413   (gnus-summary-goto-subject gnus-current-article)
7414   (gnus-summary-position-point))
7415
7416 (defun gnus-summary-verbose-headers (&optional arg)
7417   "Toggle permanent full header display.
7418 If ARG is a positive number, turn header display on.
7419 If ARG is a negative number, turn header display off."
7420   (interactive "P")
7421   (setq gnus-show-all-headers
7422         (cond ((or (not (numberp arg))
7423                    (zerop arg))
7424                (not gnus-show-all-headers))
7425               ((natnump arg)
7426                t)))
7427   (gnus-summary-show-article))
7428
7429 (defun gnus-summary-toggle-header (&optional arg)
7430   "Show the headers if they are hidden, or hide them if they are shown.
7431 If ARG is a positive number, show the entire header.
7432 If ARG is a negative number, hide the unwanted header lines."
7433   (interactive "P")
7434   (save-excursion
7435     (set-buffer gnus-article-buffer)
7436     (save-restriction
7437       (let* ((buffer-read-only nil)
7438              (inhibit-point-motion-hooks t)
7439              hidden e)
7440         (setq hidden
7441               (if (numberp arg)
7442                   (>= arg 0)
7443                 (save-restriction
7444                   (article-narrow-to-head)
7445                   (gnus-article-hidden-text-p 'headers))))
7446         (goto-char (point-min))
7447         (when (search-forward "\n\n" nil t)
7448           (delete-region (point-min) (1- (point))))
7449         (goto-char (point-min))
7450         (save-excursion
7451           (set-buffer gnus-original-article-buffer)
7452           (goto-char (point-min))
7453           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7454         (insert-buffer-substring gnus-original-article-buffer 1 e)
7455         (save-restriction
7456           (narrow-to-region (point-min) (point))
7457           (article-decode-encoded-words)
7458           (if  hidden
7459               (let ((gnus-treat-hide-headers nil)
7460                     (gnus-treat-hide-boring-headers nil))
7461                 (setq gnus-article-wash-types
7462                       (delq 'headers gnus-article-wash-types))
7463                 (gnus-treat-article 'head))
7464             (gnus-treat-article 'head)))
7465         (gnus-set-mode-line 'article)))))
7466
7467 (defun gnus-summary-show-all-headers ()
7468   "Make all header lines visible."
7469   (interactive)
7470   (gnus-article-show-all-headers))
7471
7472 (defun gnus-summary-toggle-mime (&optional arg)
7473   "Toggle MIME processing.
7474 If ARG is a positive number, turn MIME processing on."
7475   (interactive "P")
7476   (setq gnus-show-mime
7477         (if (null arg)
7478             (not gnus-show-mime)
7479           (> (prefix-numeric-value arg) 0)))
7480   (gnus-summary-select-article t 'force))
7481
7482 (defun gnus-summary-caesar-message (&optional arg)
7483   "Caesar rotate the current article by 13.
7484 The numerical prefix specifies how many places to rotate each letter
7485 forward."
7486   (interactive "P")
7487   (gnus-summary-select-article)
7488   (let ((mail-header-separator ""))
7489     (gnus-eval-in-buffer-window gnus-article-buffer
7490       (save-restriction
7491         (widen)
7492         (let ((start (window-start))
7493               buffer-read-only)
7494           (message-caesar-buffer-body arg)
7495           (set-window-start (get-buffer-window (current-buffer)) start))))))
7496
7497 (defun gnus-summary-stop-page-breaking ()
7498   "Stop page breaking in the current article."
7499   (interactive)
7500   (gnus-summary-select-article)
7501   (gnus-eval-in-buffer-window gnus-article-buffer
7502     (widen)
7503     (when (gnus-visual-p 'page-marker)
7504       (let ((buffer-read-only nil))
7505         (gnus-remove-text-with-property 'gnus-prev)
7506         (gnus-remove-text-with-property 'gnus-next))
7507       (setq gnus-page-broken nil))))
7508
7509 (defun gnus-summary-move-article (&optional n to-newsgroup
7510                                             select-method action)
7511   "Move the current article to a different newsgroup.
7512 If N is a positive number, move the N next articles.
7513 If N is a negative number, move the N previous articles.
7514 If N is nil and any articles have been marked with the process mark,
7515 move those articles instead.
7516 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7517 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7518 re-spool using this method.
7519
7520 For this function to work, both the current newsgroup and the
7521 newsgroup that you want to move to have to support the `request-move'
7522 and `request-accept' functions.
7523
7524 ACTION can be either `move' (the default), `crosspost' or `copy'."
7525   (interactive "P")
7526   (unless action
7527     (setq action 'move))
7528   ;; Disable marking as read.
7529   (let (gnus-mark-article-hook)
7530     (save-window-excursion
7531       (gnus-summary-select-article)))
7532   ;; Check whether the source group supports the required functions.
7533   (cond ((and (eq action 'move)
7534               (not (gnus-check-backend-function
7535                     'request-move-article gnus-newsgroup-name)))
7536          (error "The current group does not support article moving"))
7537         ((and (eq action 'crosspost)
7538               (not (gnus-check-backend-function
7539                     'request-replace-article gnus-newsgroup-name)))
7540          (error "The current group does not support article editing")))
7541   (let ((articles (gnus-summary-work-articles n))
7542         (prefix (if (gnus-check-backend-function
7543                     'request-move-article gnus-newsgroup-name)
7544                     (gnus-group-real-prefix gnus-newsgroup-name)
7545                   ""))
7546         (names '((move "Move" "Moving")
7547                  (copy "Copy" "Copying")
7548                  (crosspost "Crosspost" "Crossposting")))
7549         (copy-buf (save-excursion
7550                     (nnheader-set-temp-buffer " *copy article*")))
7551         (default-marks gnus-article-mark-lists)
7552         (no-expire-marks (delete '(expirable . expire)
7553                                  (copy-sequence gnus-article-mark-lists)))
7554         art-group to-method new-xref article to-groups)
7555     (unless (assq action names)
7556       (error "Unknown action %s" action))
7557     ;; Read the newsgroup name.
7558     (when (and (not to-newsgroup)
7559                (not select-method))
7560       (setq to-newsgroup
7561             (gnus-read-move-group-name
7562              (cadr (assq action names))
7563              (symbol-value (intern (format "gnus-current-%s-group" action)))
7564              articles prefix))
7565       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7566     (setq to-method (or select-method
7567                         (gnus-server-to-method
7568                          (gnus-group-method to-newsgroup))))
7569     ;; Check the method we are to move this article to...
7570     (unless (gnus-check-backend-function
7571              'request-accept-article (car to-method))
7572       (error "%s does not support article copying" (car to-method)))
7573     (unless (gnus-check-server to-method)
7574       (error "Can't open server %s" (car to-method)))
7575     (gnus-message 6 "%s to %s: %s..."
7576                   (caddr (assq action names))
7577                   (or (car select-method) to-newsgroup) articles)
7578     (while articles
7579       (setq article (pop articles))
7580       (setq
7581        art-group
7582        (cond
7583         ;; Move the article.
7584         ((eq action 'move)
7585          ;; Remove this article from future suppression.
7586          (gnus-dup-unsuppress-article article)
7587          (gnus-request-move-article
7588           article                       ; Article to move
7589           gnus-newsgroup-name           ; From newsgroup
7590           (nth 1 (gnus-find-method-for-group
7591                   gnus-newsgroup-name)) ; Server
7592           (list 'gnus-request-accept-article
7593                 to-newsgroup (list 'quote select-method)
7594                 (not articles) t)       ; Accept form
7595           (not articles)))              ; Only save nov last time
7596         ;; Copy the article.
7597         ((eq action 'copy)
7598          (save-excursion
7599            (set-buffer copy-buf)
7600            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7601              (gnus-request-accept-article
7602               to-newsgroup select-method (not articles) t))))
7603         ;; Crosspost the article.
7604         ((eq action 'crosspost)
7605          (let ((xref (message-tokenize-header
7606                       (mail-header-xref (gnus-summary-article-header article))
7607                       " ")))
7608            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7609                                   ":" article))
7610            (unless xref
7611              (setq xref (list (system-name))))
7612            (setq new-xref
7613                  (concat
7614                   (mapconcat 'identity
7615                              (delete "Xref:" (delete new-xref xref))
7616                              " ")
7617                   " " new-xref))
7618            (save-excursion
7619              (set-buffer copy-buf)
7620              ;; First put the article in the destination group.
7621              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7622              (when (consp (setq art-group
7623                                 (gnus-request-accept-article
7624                                  to-newsgroup select-method (not articles))))
7625                (setq new-xref (concat new-xref " " (car art-group)
7626                                       ":" (cdr art-group)))
7627                ;; Now we have the new Xrefs header, so we insert
7628                ;; it and replace the new article.
7629                (nnheader-replace-header "Xref" new-xref)
7630                (gnus-request-replace-article
7631                 (cdr art-group) to-newsgroup (current-buffer))
7632                art-group))))))
7633       (cond
7634        ((not art-group)
7635         (gnus-message 1 "Couldn't %s article %s: %s"
7636                       (cadr (assq action names)) article
7637                       (nnheader-get-report (car to-method))))
7638        ((eq art-group 'junk)
7639         (when (eq action 'move)
7640           (gnus-summary-mark-article article gnus-canceled-mark)
7641           (gnus-message 4 "Deleted article %s" article)))
7642        (t
7643         (let* ((pto-group (gnus-group-prefixed-name
7644                            (car art-group) to-method))
7645                (entry
7646                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7647                (info (nth 2 entry))
7648                (to-group (gnus-info-group info))
7649                to-marks)
7650           ;; Update the group that has been moved to.
7651           (when (and info
7652                      (memq action '(move copy)))
7653             (unless (member to-group to-groups)
7654               (push to-group to-groups))
7655
7656             (unless (memq article gnus-newsgroup-unreads)
7657               (push 'read to-marks)
7658               (gnus-info-set-read
7659                info (gnus-add-to-range (gnus-info-read info)
7660                                        (list (cdr art-group)))))
7661
7662             ;; See whether the article is to be put in the cache.
7663             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7664                              default-marks
7665                            no-expire-marks))
7666                   (to-article (cdr art-group)))
7667
7668               ;; Enter the article into the cache in the new group,
7669               ;; if that is required.
7670               (when gnus-use-cache
7671                 (gnus-cache-possibly-enter-article
7672                  to-group to-article
7673                  (let ((header (copy-sequence
7674                                 (gnus-summary-article-header article))))
7675                    (mail-header-set-number header to-article)
7676                    header)
7677                  (memq article gnus-newsgroup-marked)
7678                  (memq article gnus-newsgroup-dormant)
7679                  (memq article gnus-newsgroup-unreads)))
7680
7681               (when gnus-preserve-marks
7682                 ;; Copy any marks over to the new group.
7683                 (when (and (equal to-group gnus-newsgroup-name)
7684                            (not (memq article gnus-newsgroup-unreads)))
7685                   ;; Mark this article as read in this group.
7686                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7687                   (setcdr (gnus-active to-group) to-article)
7688                   (setcdr gnus-newsgroup-active to-article))
7689
7690                 (while marks
7691                   (when (memq article (symbol-value
7692                                        (intern (format "gnus-newsgroup-%s"
7693                                                        (caar marks)))))
7694                     (push (cdar marks) to-marks)
7695                     ;; If the other group is the same as this group,
7696                     ;; then we have to add the mark to the list.
7697                     (when (equal to-group gnus-newsgroup-name)
7698                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7699                            (cons to-article
7700                                  (symbol-value
7701                                   (intern (format "gnus-newsgroup-%s"
7702                                                   (caar marks)))))))
7703                     ;; Copy the marks to other group.
7704                     (gnus-add-marked-articles
7705                      to-group (cdar marks) (list to-article) info))
7706                   (setq marks (cdr marks)))
7707
7708                 (gnus-request-set-mark to-group (list (list (list to-article)
7709                                                             'set
7710                                                             to-marks))))
7711
7712               (gnus-dribble-enter
7713                (concat "(gnus-group-set-info '"
7714                        (gnus-prin1-to-string (gnus-get-info to-group))
7715                        ")"))))
7716
7717           ;; Update the Xref header in this article to point to
7718           ;; the new crossposted article we have just created.
7719           (when (eq action 'crosspost)
7720             (save-excursion
7721               (set-buffer copy-buf)
7722               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7723               (nnheader-replace-header "Xref" new-xref)
7724               (gnus-request-replace-article
7725                article gnus-newsgroup-name (current-buffer)))))
7726
7727         ;;;!!!Why is this necessary?
7728         (set-buffer gnus-summary-buffer)
7729
7730         (gnus-summary-goto-subject article)
7731         (when (eq action 'move)
7732           (gnus-summary-mark-article article gnus-canceled-mark))))
7733       (gnus-summary-remove-process-mark article))
7734     ;; Re-activate all groups that have been moved to.
7735     (while to-groups
7736       (save-excursion
7737         (set-buffer gnus-group-buffer)
7738         (when (gnus-group-goto-group (car to-groups) t)
7739           (gnus-group-get-new-news-this-group 1 t))
7740         (pop to-groups)))
7741
7742     (gnus-kill-buffer copy-buf)
7743     (gnus-summary-position-point)
7744     (gnus-set-mode-line 'summary)))
7745
7746 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7747   "Move the current article to a different newsgroup.
7748 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7749 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7750 re-spool using this method."
7751   (interactive "P")
7752   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7753
7754 (defun gnus-summary-crosspost-article (&optional n)
7755   "Crosspost the current article to some other group."
7756   (interactive "P")
7757   (gnus-summary-move-article n nil nil 'crosspost))
7758
7759 (defcustom gnus-summary-respool-default-method nil
7760   "Default method for respooling an article.
7761 If nil, use to the current newsgroup method."
7762   :type '(choice (gnus-select-method :value (nnml ""))
7763                  (const nil))
7764   :group 'gnus-summary-mail)
7765
7766 (defun gnus-summary-respool-article (&optional n method)
7767   "Respool the current article.
7768 The article will be squeezed through the mail spooling process again,
7769 which means that it will be put in some mail newsgroup or other
7770 depending on `nnmail-split-methods'.
7771 If N is a positive number, respool the N next articles.
7772 If N is a negative number, respool the N previous articles.
7773 If N is nil and any articles have been marked with the process mark,
7774 respool those articles instead.
7775
7776 Respooling can be done both from mail groups and \"real\" newsgroups.
7777 In the former case, the articles in question will be moved from the
7778 current group into whatever groups they are destined to.  In the
7779 latter case, they will be copied into the relevant groups."
7780   (interactive
7781    (list current-prefix-arg
7782          (let* ((methods (gnus-methods-using 'respool))
7783                 (methname
7784                  (symbol-name (or gnus-summary-respool-default-method
7785                                   (car (gnus-find-method-for-group
7786                                         gnus-newsgroup-name)))))
7787                 (method
7788                  (gnus-completing-read
7789                   methname "What backend do you want to use when respooling?"
7790                   methods nil t nil 'gnus-mail-method-history))
7791                 ms)
7792            (cond
7793             ((zerop (length (setq ms (gnus-servers-using-backend
7794                                       (intern method)))))
7795              (list (intern method) ""))
7796             ((= 1 (length ms))
7797              (car ms))
7798             (t
7799              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7800                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7801                            ms-alist))))))))
7802   (unless method
7803     (error "No method given for respooling"))
7804   (if (assoc (symbol-name
7805               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7806              (gnus-methods-using 'respool))
7807       (gnus-summary-move-article n nil method)
7808     (gnus-summary-copy-article n nil method)))
7809
7810 (defun gnus-summary-import-article (file)
7811   "Import an arbitrary file into a mail newsgroup."
7812   (interactive "fImport file: ")
7813   (let ((group gnus-newsgroup-name)
7814         (now (current-time))
7815         atts lines)
7816     (unless (gnus-check-backend-function 'request-accept-article group)
7817       (error "%s does not support article importing" group))
7818     (or (file-readable-p file)
7819         (not (file-regular-p file))
7820         (error "Can't read %s" file))
7821     (save-excursion
7822       (set-buffer (gnus-get-buffer-create " *import file*"))
7823       (erase-buffer)
7824       (nnheader-insert-file-contents file)
7825       (goto-char (point-min))
7826       (unless (nnheader-article-p)
7827         ;; This doesn't look like an article, so we fudge some headers.
7828         (setq atts (file-attributes file)
7829               lines (count-lines (point-min) (point-max)))
7830         (insert "From: " (read-string "From: ") "\n"
7831                 "Subject: " (read-string "Subject: ") "\n"
7832                 "Date: " (message-make-date (nth 5 atts))
7833                 "\n"
7834                 "Message-ID: " (message-make-message-id) "\n"
7835                 "Lines: " (int-to-string lines) "\n"
7836                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7837       (gnus-request-accept-article group nil t)
7838       (kill-buffer (current-buffer)))))
7839
7840 (defun gnus-summary-article-posted-p ()
7841   "Say whether the current (mail) article is available from news as well.
7842 This will be the case if the article has both been mailed and posted."
7843   (interactive)
7844   (let ((id (mail-header-references (gnus-summary-article-header)))
7845         (gnus-override-method (car (gnus-refer-article-methods))))
7846     (if (gnus-request-head id "")
7847         (gnus-message 2 "The current message was found on %s"
7848                       gnus-override-method)
7849       (gnus-message 2 "The current message couldn't be found on %s"
7850                     gnus-override-method)
7851       nil)))
7852
7853 (defun gnus-summary-expire-articles (&optional now)
7854   "Expire all articles that are marked as expirable in the current group."
7855   (interactive)
7856   (when (gnus-check-backend-function
7857          'request-expire-articles gnus-newsgroup-name)
7858     ;; This backend supports expiry.
7859     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7860            (expirable (if total
7861                           (progn
7862                             ;; We need to update the info for
7863                             ;; this group for `gnus-list-of-read-articles'
7864                             ;; to give us the right answer.
7865                             (gnus-run-hooks 'gnus-exit-group-hook)
7866                             (gnus-summary-update-info)
7867                             (gnus-list-of-read-articles gnus-newsgroup-name))
7868                         (setq gnus-newsgroup-expirable
7869                               (sort gnus-newsgroup-expirable '<))))
7870            (expiry-wait (if now 'immediate
7871                           (gnus-group-find-parameter
7872                            gnus-newsgroup-name 'expiry-wait)))
7873            (nnmail-expiry-target
7874             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7875                 nnmail-expiry-target))
7876            es)
7877       (when expirable
7878         ;; There are expirable articles in this group, so we run them
7879         ;; through the expiry process.
7880         (gnus-message 6 "Expiring articles...")
7881         (unless (gnus-check-group gnus-newsgroup-name)
7882           (error "Can't open server for %s" gnus-newsgroup-name))
7883         ;; The list of articles that weren't expired is returned.
7884         (save-excursion
7885           (if expiry-wait
7886               (let ((nnmail-expiry-wait-function nil)
7887                     (nnmail-expiry-wait expiry-wait))
7888                 (setq es (gnus-request-expire-articles
7889                           expirable gnus-newsgroup-name)))
7890             (setq es (gnus-request-expire-articles
7891                       expirable gnus-newsgroup-name))))
7892         (unless total
7893           (setq gnus-newsgroup-expirable es))
7894         ;; We go through the old list of expirable, and mark all
7895         ;; really expired articles as nonexistent.
7896         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7897           (let ((gnus-use-cache nil))
7898             (while expirable
7899               (unless (memq (car expirable) es)
7900                 (when (gnus-data-find (car expirable))
7901                   (gnus-summary-mark-article
7902                    (car expirable) gnus-canceled-mark)))
7903               (setq expirable (cdr expirable)))))
7904         (gnus-message 6 "Expiring articles...done")))))
7905
7906 (defun gnus-summary-expire-articles-now ()
7907   "Expunge all expirable articles in the current group.
7908 This means that *all* articles that are marked as expirable will be
7909 deleted forever, right now."
7910   (interactive)
7911   (or gnus-expert-user
7912       (gnus-yes-or-no-p
7913        "Are you really, really, really sure you want to delete all these messages? ")
7914       (error "Phew!"))
7915   (gnus-summary-expire-articles t))
7916
7917 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7918 (defun gnus-summary-delete-article (&optional n)
7919   "Delete the N next (mail) articles.
7920 This command actually deletes articles.  This is not a marking
7921 command.  The article will disappear forever from your life, never to
7922 return.
7923 If N is negative, delete backwards.
7924 If N is nil and articles have been marked with the process mark,
7925 delete these instead."
7926   (interactive "P")
7927   (unless (gnus-check-backend-function 'request-expire-articles
7928                                        gnus-newsgroup-name)
7929     (error "The current newsgroup does not support article deletion"))
7930   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7931     (error "Couldn't open server"))
7932   ;; Compute the list of articles to delete.
7933   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7934         not-deleted)
7935     (if (and gnus-novice-user
7936              (not (gnus-yes-or-no-p
7937                    (format "Do you really want to delete %s forever? "
7938                            (if (> (length articles) 1)
7939                                (format "these %s articles" (length articles))
7940                              "this article")))))
7941         ()
7942       ;; Delete the articles.
7943       (setq not-deleted (gnus-request-expire-articles
7944                          articles gnus-newsgroup-name 'force))
7945       (while articles
7946         (gnus-summary-remove-process-mark (car articles))
7947         ;; The backend might not have been able to delete the article
7948         ;; after all.
7949         (unless (memq (car articles) not-deleted)
7950           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7951         (setq articles (cdr articles)))
7952       (when not-deleted
7953         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7954     (gnus-summary-position-point)
7955     (gnus-set-mode-line 'summary)
7956     not-deleted))
7957
7958 (defun gnus-summary-edit-article (&optional force)
7959   "Edit the current article.
7960 This will have permanent effect only in mail groups.
7961 If FORCE is non-nil, allow editing of articles even in read-only
7962 groups."
7963   (interactive "P")
7964   (save-excursion
7965     (set-buffer gnus-summary-buffer)
7966     (let ((mail-parse-charset gnus-newsgroup-charset)
7967           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7968       (gnus-set-global-variables)
7969       (when (and (not force)
7970                  (gnus-group-read-only-p))
7971         (error "The current newsgroup does not support article editing"))
7972       (gnus-summary-show-article t)
7973       (gnus-article-edit-article
7974        'ignore
7975        `(lambda (no-highlight)
7976           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7977                 (mail-parse-ignored-charsets
7978                  ',gnus-newsgroup-ignored-charsets))
7979             (gnus-summary-edit-article-done
7980              ,(or (mail-header-references gnus-current-headers) "")
7981              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7982
7983 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7984
7985 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7986                                                  no-highlight)
7987   "Make edits to the current article permanent."
7988   (interactive)
7989   ;; Replace the article.
7990   (let ((buf (current-buffer)))
7991     (with-temp-buffer
7992       (insert-buffer-substring buf)
7993       (if (and (not read-only)
7994                (not (gnus-request-replace-article
7995                      (cdr gnus-article-current) (car gnus-article-current)
7996                      (current-buffer) t)))
7997           (error "Couldn't replace article")
7998         ;; Update the summary buffer.
7999         (if (and references
8000                  (equal (message-tokenize-header references " ")
8001                         (message-tokenize-header
8002                          (or (message-fetch-field "references") "") " ")))
8003             ;; We only have to update this line.
8004             (save-excursion
8005               (save-restriction
8006                 (message-narrow-to-head)
8007                 (let ((head (buffer-string))
8008                       header)
8009                   (with-temp-buffer
8010                     (insert (format "211 %d Article retrieved.\n"
8011                                     (cdr gnus-article-current)))
8012                     (insert head)
8013                     (insert ".\n")
8014                     (let ((nntp-server-buffer (current-buffer)))
8015                       (setq header (car (gnus-get-newsgroup-headers
8016                                          (save-excursion
8017                                            (set-buffer gnus-summary-buffer)
8018                                            gnus-newsgroup-dependencies)
8019                                          t))))
8020                     (save-excursion
8021                       (set-buffer gnus-summary-buffer)
8022                       (gnus-data-set-header
8023                        (gnus-data-find (cdr gnus-article-current))
8024                        header)
8025                       (gnus-summary-update-article-line
8026                        (cdr gnus-article-current) header))))))
8027           ;; Update threads.
8028           (set-buffer (or buffer gnus-summary-buffer))
8029           (gnus-summary-update-article (cdr gnus-article-current)))
8030         ;; Prettify the article buffer again.
8031         (unless no-highlight
8032           (save-excursion
8033             (set-buffer gnus-article-buffer)
8034             ;;;!!! Fix this -- article should be rehighlighted.
8035             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8036             (set-buffer gnus-original-article-buffer)
8037             (gnus-request-article
8038              (cdr gnus-article-current)
8039              (car gnus-article-current) (current-buffer))))
8040         ;; Prettify the summary buffer line.
8041         (when (gnus-visual-p 'summary-highlight 'highlight)
8042           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8043
8044 (defun gnus-summary-edit-wash (key)
8045   "Perform editing command KEY in the article buffer."
8046   (interactive
8047    (list
8048     (progn
8049       (message "%s" (concat (this-command-keys) "- "))
8050       (read-char))))
8051   (message "")
8052   (gnus-summary-edit-article)
8053   (execute-kbd-macro (concat (this-command-keys) key))
8054   (gnus-article-edit-done))
8055
8056 ;;; Respooling
8057
8058 (defun gnus-summary-respool-query (&optional silent trace)
8059   "Query where the respool algorithm would put this article."
8060   (interactive)
8061   (let (gnus-mark-article-hook)
8062     (gnus-summary-select-article)
8063     (save-excursion
8064       (set-buffer gnus-original-article-buffer)
8065       (save-restriction
8066         (message-narrow-to-head)
8067         (let ((groups (nnmail-article-group 'identity trace)))
8068           (unless silent
8069             (if groups
8070                 (message "This message would go to %s"
8071                          (mapconcat 'car groups ", "))
8072               (message "This message would go to no groups"))
8073             groups))))))
8074
8075 (defun gnus-summary-respool-trace ()
8076   "Trace where the respool algorithm would put this article.
8077 Display a buffer showing all fancy splitting patterns which matched."
8078   (interactive)
8079   (gnus-summary-respool-query nil t))
8080
8081 ;; Summary marking commands.
8082
8083 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8084   "Mark articles which has the same subject as read, and then select the next.
8085 If UNMARK is positive, remove any kind of mark.
8086 If UNMARK is negative, tick articles."
8087   (interactive "P")
8088   (when unmark
8089     (setq unmark (prefix-numeric-value unmark)))
8090   (let ((count
8091          (gnus-summary-mark-same-subject
8092           (gnus-summary-article-subject) unmark)))
8093     ;; Select next unread article.  If auto-select-same mode, should
8094     ;; select the first unread article.
8095     (gnus-summary-next-article t (and gnus-auto-select-same
8096                                       (gnus-summary-article-subject)))
8097     (gnus-message 7 "%d article%s marked as %s"
8098                   count (if (= count 1) " is" "s are")
8099                   (if unmark "unread" "read"))))
8100
8101 (defun gnus-summary-kill-same-subject (&optional unmark)
8102   "Mark articles which has the same subject as read.
8103 If UNMARK is positive, remove any kind of mark.
8104 If UNMARK is negative, tick articles."
8105   (interactive "P")
8106   (when unmark
8107     (setq unmark (prefix-numeric-value unmark)))
8108   (let ((count
8109          (gnus-summary-mark-same-subject
8110           (gnus-summary-article-subject) unmark)))
8111     ;; If marked as read, go to next unread subject.
8112     (when (null unmark)
8113       ;; Go to next unread subject.
8114       (gnus-summary-next-subject 1 t))
8115     (gnus-message 7 "%d articles are marked as %s"
8116                   count (if unmark "unread" "read"))))
8117
8118 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8119   "Mark articles with same SUBJECT as read, and return marked number.
8120 If optional argument UNMARK is positive, remove any kinds of marks.
8121 If optional argument UNMARK is negative, mark articles as unread instead."
8122   (let ((count 1))
8123     (save-excursion
8124       (cond
8125        ((null unmark)                   ; Mark as read.
8126         (while (and
8127                 (progn
8128                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8129                   (gnus-summary-show-thread) t)
8130                 (gnus-summary-find-subject subject))
8131           (setq count (1+ count))))
8132        ((> unmark 0)                    ; Tick.
8133         (while (and
8134                 (progn
8135                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8136                   (gnus-summary-show-thread) t)
8137                 (gnus-summary-find-subject subject))
8138           (setq count (1+ count))))
8139        (t                               ; Mark as unread.
8140         (while (and
8141                 (progn
8142                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8143                   (gnus-summary-show-thread) t)
8144                 (gnus-summary-find-subject subject))
8145           (setq count (1+ count)))))
8146       (gnus-set-mode-line 'summary)
8147       ;; Return the number of marked articles.
8148       count)))
8149
8150 (defun gnus-summary-mark-as-processable (n &optional unmark)
8151   "Set the process mark on the next N articles.
8152 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8153 the process mark instead.  The difference between N and the actual
8154 number of articles marked is returned."
8155   (interactive "p")
8156   (let ((backward (< n 0))
8157         (n (abs n)))
8158     (while (and
8159             (> n 0)
8160             (if unmark
8161                 (gnus-summary-remove-process-mark
8162                  (gnus-summary-article-number))
8163               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8164             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8165       (setq n (1- n)))
8166     (when (/= 0 n)
8167       (gnus-message 7 "No more articles"))
8168     (gnus-summary-recenter)
8169     (gnus-summary-position-point)
8170     n))
8171
8172 (defun gnus-summary-unmark-as-processable (n)
8173   "Remove the process mark from the next N articles.
8174 If N is negative, unmark backward instead.  The difference between N and
8175 the actual number of articles unmarked is returned."
8176   (interactive "p")
8177   (gnus-summary-mark-as-processable n t))
8178
8179 (defun gnus-summary-unmark-all-processable ()
8180   "Remove the process mark from all articles."
8181   (interactive)
8182   (save-excursion
8183     (while gnus-newsgroup-processable
8184       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8185   (gnus-summary-position-point))
8186
8187 (defun gnus-summary-mark-as-expirable (n)
8188   "Mark N articles forward as expirable.
8189 If N is negative, mark backward instead.  The difference between N and
8190 the actual number of articles marked is returned."
8191   (interactive "p")
8192   (gnus-summary-mark-forward n gnus-expirable-mark))
8193
8194 (defun gnus-summary-mark-article-as-replied (article)
8195   "Mark ARTICLE replied and update the summary line."
8196   (push article gnus-newsgroup-replied)
8197   (let ((buffer-read-only nil))
8198     (when (gnus-summary-goto-subject article nil t)
8199       (gnus-summary-update-secondary-mark article))))
8200
8201 (defun gnus-summary-set-bookmark (article)
8202   "Set a bookmark in current article."
8203   (interactive (list (gnus-summary-article-number)))
8204   (when (or (not (get-buffer gnus-article-buffer))
8205             (not gnus-current-article)
8206             (not gnus-article-current)
8207             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8208     (error "No current article selected"))
8209   ;; Remove old bookmark, if one exists.
8210   (let ((old (assq article gnus-newsgroup-bookmarks)))
8211     (when old
8212       (setq gnus-newsgroup-bookmarks
8213             (delq old gnus-newsgroup-bookmarks))))
8214   ;; Set the new bookmark, which is on the form
8215   ;; (article-number . line-number-in-body).
8216   (push
8217    (cons article
8218          (save-excursion
8219            (set-buffer gnus-article-buffer)
8220            (count-lines
8221             (min (point)
8222                  (save-excursion
8223                    (goto-char (point-min))
8224                    (search-forward "\n\n" nil t)
8225                    (point)))
8226             (point))))
8227    gnus-newsgroup-bookmarks)
8228   (gnus-message 6 "A bookmark has been added to the current article."))
8229
8230 (defun gnus-summary-remove-bookmark (article)
8231   "Remove the bookmark from the current article."
8232   (interactive (list (gnus-summary-article-number)))
8233   ;; Remove old bookmark, if one exists.
8234   (let ((old (assq article gnus-newsgroup-bookmarks)))
8235     (if old
8236         (progn
8237           (setq gnus-newsgroup-bookmarks
8238                 (delq old gnus-newsgroup-bookmarks))
8239           (gnus-message 6 "Removed bookmark."))
8240       (gnus-message 6 "No bookmark in current article."))))
8241
8242 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8243 (defun gnus-summary-mark-as-dormant (n)
8244   "Mark N articles forward as dormant.
8245 If N is negative, mark backward instead.  The difference between N and
8246 the actual number of articles marked is returned."
8247   (interactive "p")
8248   (gnus-summary-mark-forward n gnus-dormant-mark))
8249
8250 (defun gnus-summary-set-process-mark (article)
8251   "Set the process mark on ARTICLE and update the summary line."
8252   (setq gnus-newsgroup-processable
8253         (cons article
8254               (delq article gnus-newsgroup-processable)))
8255   (when (gnus-summary-goto-subject article)
8256     (gnus-summary-show-thread)
8257     (gnus-summary-goto-subject article)
8258     (gnus-summary-update-secondary-mark article)))
8259
8260 (defun gnus-summary-remove-process-mark (article)
8261   "Remove the process mark from ARTICLE and update the summary line."
8262   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8263   (when (gnus-summary-goto-subject article)
8264     (gnus-summary-show-thread)
8265     (gnus-summary-goto-subject article)
8266     (gnus-summary-update-secondary-mark article)))
8267
8268 (defun gnus-summary-set-saved-mark (article)
8269   "Set the process mark on ARTICLE and update the summary line."
8270   (push article gnus-newsgroup-saved)
8271   (when (gnus-summary-goto-subject article)
8272     (gnus-summary-update-secondary-mark article)))
8273
8274 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8275   "Mark N articles as read forwards.
8276 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8277 The difference between N and the actual number of articles marked is
8278 returned.
8279 Iff NO-EXPIRE, auto-expiry will be inhibited."
8280   (interactive "p")
8281   (gnus-summary-show-thread)
8282   (let ((backward (< n 0))
8283         (gnus-summary-goto-unread
8284          (and gnus-summary-goto-unread
8285               (not (eq gnus-summary-goto-unread 'never))
8286               (not (memq mark (list gnus-unread-mark
8287                                     gnus-ticked-mark gnus-dormant-mark)))))
8288         (n (abs n))
8289         (mark (or mark gnus-del-mark)))
8290     (while (and (> n 0)
8291                 (gnus-summary-mark-article nil mark no-expire)
8292                 (zerop (gnus-summary-next-subject
8293                         (if backward -1 1)
8294                         (and gnus-summary-goto-unread
8295                              (not (eq gnus-summary-goto-unread 'never)))
8296                         t)))
8297       (setq n (1- n)))
8298     (when (/= 0 n)
8299       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8300     (gnus-summary-recenter)
8301     (gnus-summary-position-point)
8302     (gnus-set-mode-line 'summary)
8303     n))
8304
8305 (defun gnus-summary-mark-article-as-read (mark)
8306   "Mark the current article quickly as read with MARK."
8307   (let ((article (gnus-summary-article-number)))
8308     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8309     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8310     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8311     (push (cons article mark) gnus-newsgroup-reads)
8312     ;; Possibly remove from cache, if that is used.
8313     (when gnus-use-cache
8314       (gnus-cache-enter-remove-article article))
8315     ;; Allow the backend to change the mark.
8316     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8317     ;; Check for auto-expiry.
8318     (when (and gnus-newsgroup-auto-expire
8319                (memq mark gnus-auto-expirable-marks))
8320       (setq mark gnus-expirable-mark)
8321       ;; Let the backend know about the mark change.
8322       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8323       (push article gnus-newsgroup-expirable))
8324     ;; Set the mark in the buffer.
8325     (gnus-summary-update-mark mark 'unread)
8326     t))
8327
8328 (defun gnus-summary-mark-article-as-unread (mark)
8329   "Mark the current article quickly as unread with MARK."
8330   (let* ((article (gnus-summary-article-number))
8331          (old-mark (gnus-summary-article-mark article)))
8332     ;; Allow the backend to change the mark.
8333     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8334     (if (eq mark old-mark)
8335         t
8336       (if (<= article 0)
8337           (progn
8338             (gnus-error 1 "Can't mark negative article numbers")
8339             nil)
8340         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8341         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8342         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8343         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8344         (cond ((= mark gnus-ticked-mark)
8345                (push article gnus-newsgroup-marked))
8346               ((= mark gnus-dormant-mark)
8347                (push article gnus-newsgroup-dormant))
8348               (t
8349                (push article gnus-newsgroup-unreads)))
8350         (gnus-pull article gnus-newsgroup-reads)
8351
8352         ;; See whether the article is to be put in the cache.
8353         (and gnus-use-cache
8354              (vectorp (gnus-summary-article-header article))
8355              (save-excursion
8356                (gnus-cache-possibly-enter-article
8357                 gnus-newsgroup-name article
8358                 (gnus-summary-article-header article)
8359                 (= mark gnus-ticked-mark)
8360                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8361
8362         ;; Fix the mark.
8363         (gnus-summary-update-mark mark 'unread)
8364         t))))
8365
8366 (defun gnus-summary-mark-article (&optional article mark no-expire)
8367   "Mark ARTICLE with MARK.  MARK can be any character.
8368 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8369 `??' (dormant) and `?E' (expirable).
8370 If MARK is nil, then the default character `?r' is used.
8371 If ARTICLE is nil, then the article on the current line will be
8372 marked.
8373 Iff NO-EXPIRE, auto-expiry will be inhibited."
8374   ;; The mark might be a string.
8375   (when (stringp mark)
8376     (setq mark (aref mark 0)))
8377   ;; If no mark is given, then we check auto-expiring.
8378   (when (null mark)
8379     (setq mark gnus-del-mark))
8380   (when (and (not no-expire)
8381              gnus-newsgroup-auto-expire
8382              (memq mark gnus-auto-expirable-marks))
8383     (setq mark gnus-expirable-mark))
8384   (let ((article (or article (gnus-summary-article-number)))
8385         (old-mark (gnus-summary-article-mark article)))
8386     ;; Allow the backend to change the mark.
8387     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8388     (if (eq mark old-mark)
8389         t
8390       (unless article
8391         (error "No article on current line"))
8392       (if (not (if (or (= mark gnus-unread-mark)
8393                        (= mark gnus-ticked-mark)
8394                        (= mark gnus-dormant-mark))
8395                    (gnus-mark-article-as-unread article mark)
8396                  (gnus-mark-article-as-read article mark)))
8397           t
8398         ;; See whether the article is to be put in the cache.
8399         (and gnus-use-cache
8400              (not (= mark gnus-canceled-mark))
8401              (vectorp (gnus-summary-article-header article))
8402              (save-excursion
8403                (gnus-cache-possibly-enter-article
8404                 gnus-newsgroup-name article
8405                 (gnus-summary-article-header article)
8406                 (= mark gnus-ticked-mark)
8407                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8408
8409         (when (gnus-summary-goto-subject article nil t)
8410           (let ((buffer-read-only nil))
8411             (gnus-summary-show-thread)
8412             ;; Fix the mark.
8413             (gnus-summary-update-mark mark 'unread)
8414             t))))))
8415
8416 (defun gnus-summary-update-secondary-mark (article)
8417   "Update the secondary (read, process, cache) mark."
8418   (gnus-summary-update-mark
8419    (cond ((memq article gnus-newsgroup-processable)
8420           gnus-process-mark)
8421          ((memq article gnus-newsgroup-cached)
8422           gnus-cached-mark)
8423          ((memq article gnus-newsgroup-replied)
8424           gnus-replied-mark)
8425          ((memq article gnus-newsgroup-saved)
8426           gnus-saved-mark)
8427          (t gnus-unread-mark))
8428    'replied)
8429   (when (gnus-visual-p 'summary-highlight 'highlight)
8430     (gnus-run-hooks 'gnus-summary-update-hook))
8431   t)
8432
8433 (defun gnus-summary-update-mark (mark type)
8434   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8435         (buffer-read-only nil))
8436     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8437     (when forward
8438       (when (looking-at "\r")
8439         (incf forward))
8440       (when (<= (+ forward (point)) (point-max))
8441         ;; Go to the right position on the line.
8442         (goto-char (+ forward (point)))
8443         ;; Replace the old mark with the new mark.
8444         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8445         ;; Optionally update the marks by some user rule.
8446         (when (eq type 'unread)
8447           (gnus-data-set-mark
8448            (gnus-data-find (gnus-summary-article-number)) mark)
8449           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8450
8451 (defun gnus-mark-article-as-read (article &optional mark)
8452   "Enter ARTICLE in the pertinent lists and remove it from others."
8453   ;; Make the article expirable.
8454   (let ((mark (or mark gnus-del-mark)))
8455     (if (= mark gnus-expirable-mark)
8456         (push article gnus-newsgroup-expirable)
8457       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8458     ;; Remove from unread and marked lists.
8459     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8460     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8461     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8462     (push (cons article mark) gnus-newsgroup-reads)
8463     ;; Possibly remove from cache, if that is used.
8464     (when gnus-use-cache
8465       (gnus-cache-enter-remove-article article))
8466     t))
8467
8468 (defun gnus-mark-article-as-unread (article &optional mark)
8469   "Enter ARTICLE in the pertinent lists and remove it from others."
8470   (let ((mark (or mark gnus-ticked-mark)))
8471     (if (<= article 0)
8472         (progn
8473           (gnus-error 1 "Can't mark negative article numbers")
8474           nil)
8475       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8476             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8477             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8478             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8479
8480       ;; Unsuppress duplicates?
8481       (when gnus-suppress-duplicates
8482         (gnus-dup-unsuppress-article article))
8483
8484       (cond ((= mark gnus-ticked-mark)
8485              (push article gnus-newsgroup-marked))
8486             ((= mark gnus-dormant-mark)
8487              (push article gnus-newsgroup-dormant))
8488             (t
8489              (push article gnus-newsgroup-unreads)))
8490       (gnus-pull article gnus-newsgroup-reads)
8491       t)))
8492
8493 (defalias 'gnus-summary-mark-as-unread-forward
8494   'gnus-summary-tick-article-forward)
8495 (make-obsolete 'gnus-summary-mark-as-unread-forward
8496                'gnus-summary-tick-article-forward)
8497 (defun gnus-summary-tick-article-forward (n)
8498   "Tick N articles forwards.
8499 If N is negative, tick backwards instead.
8500 The difference between N and the number of articles ticked is returned."
8501   (interactive "p")
8502   (gnus-summary-mark-forward n gnus-ticked-mark))
8503
8504 (defalias 'gnus-summary-mark-as-unread-backward
8505   'gnus-summary-tick-article-backward)
8506 (make-obsolete 'gnus-summary-mark-as-unread-backward
8507                'gnus-summary-tick-article-backward)
8508 (defun gnus-summary-tick-article-backward (n)
8509   "Tick N articles backwards.
8510 The difference between N and the number of articles ticked is returned."
8511   (interactive "p")
8512   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8513
8514 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8515 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8516 (defun gnus-summary-tick-article (&optional article clear-mark)
8517   "Mark current article as unread.
8518 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8519 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8520   (interactive)
8521   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8522                                        gnus-ticked-mark)))
8523
8524 (defun gnus-summary-mark-as-read-forward (n)
8525   "Mark N articles as read forwards.
8526 If N is negative, mark backwards instead.
8527 The difference between N and the actual number of articles marked is
8528 returned."
8529   (interactive "p")
8530   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8531
8532 (defun gnus-summary-mark-as-read-backward (n)
8533   "Mark the N articles as read backwards.
8534 The difference between N and the actual number of articles marked is
8535 returned."
8536   (interactive "p")
8537   (gnus-summary-mark-forward
8538    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8539
8540 (defun gnus-summary-mark-as-read (&optional article mark)
8541   "Mark current article as read.
8542 ARTICLE specifies the article to be marked as read.
8543 MARK specifies a string to be inserted at the beginning of the line."
8544   (gnus-summary-mark-article article mark))
8545
8546 (defun gnus-summary-clear-mark-forward (n)
8547   "Clear marks from N articles forward.
8548 If N is negative, clear backward instead.
8549 The difference between N and the number of marks cleared is returned."
8550   (interactive "p")
8551   (gnus-summary-mark-forward n gnus-unread-mark))
8552
8553 (defun gnus-summary-clear-mark-backward (n)
8554   "Clear marks from N articles backward.
8555 The difference between N and the number of marks cleared is returned."
8556   (interactive "p")
8557   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8558
8559 (defun gnus-summary-mark-unread-as-read ()
8560   "Intended to be used by `gnus-summary-mark-article-hook'."
8561   (when (memq gnus-current-article gnus-newsgroup-unreads)
8562     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8563
8564 (defun gnus-summary-mark-read-and-unread-as-read ()
8565   "Intended to be used by `gnus-summary-mark-article-hook'."
8566   (let ((mark (gnus-summary-article-mark)))
8567     (when (or (gnus-unread-mark-p mark)
8568               (gnus-read-mark-p mark))
8569       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8570
8571 (defun gnus-summary-mark-region-as-read (point mark all)
8572   "Mark all unread articles between point and mark as read.
8573 If given a prefix, mark all articles between point and mark as read,
8574 even ticked and dormant ones."
8575   (interactive "r\nP")
8576   (save-excursion
8577     (let (article)
8578       (goto-char point)
8579       (beginning-of-line)
8580       (while (and
8581               (< (point) mark)
8582               (progn
8583                 (when (or all
8584                           (memq (setq article (gnus-summary-article-number))
8585                                 gnus-newsgroup-unreads))
8586                   (gnus-summary-mark-article article gnus-del-mark))
8587                 t)
8588               (gnus-summary-find-next))))))
8589
8590 (defun gnus-summary-mark-below (score mark)
8591   "Mark articles with score less than SCORE with MARK."
8592   (interactive "P\ncMark: ")
8593   (setq score (if score
8594                   (prefix-numeric-value score)
8595                 (or gnus-summary-default-score 0)))
8596   (save-excursion
8597     (set-buffer gnus-summary-buffer)
8598     (goto-char (point-min))
8599     (while
8600         (progn
8601           (and (< (gnus-summary-article-score) score)
8602                (gnus-summary-mark-article nil mark))
8603           (gnus-summary-find-next)))))
8604
8605 (defun gnus-summary-kill-below (&optional score)
8606   "Mark articles with score below SCORE as read."
8607   (interactive "P")
8608   (gnus-summary-mark-below score gnus-killed-mark))
8609
8610 (defun gnus-summary-clear-above (&optional score)
8611   "Clear all marks from articles with score above SCORE."
8612   (interactive "P")
8613   (gnus-summary-mark-above score gnus-unread-mark))
8614
8615 (defun gnus-summary-tick-above (&optional score)
8616   "Tick all articles with score above SCORE."
8617   (interactive "P")
8618   (gnus-summary-mark-above score gnus-ticked-mark))
8619
8620 (defun gnus-summary-mark-above (score mark)
8621   "Mark articles with score over SCORE with MARK."
8622   (interactive "P\ncMark: ")
8623   (setq score (if score
8624                   (prefix-numeric-value score)
8625                 (or gnus-summary-default-score 0)))
8626   (save-excursion
8627     (set-buffer gnus-summary-buffer)
8628     (goto-char (point-min))
8629     (while (and (progn
8630                   (when (> (gnus-summary-article-score) score)
8631                     (gnus-summary-mark-article nil mark))
8632                   t)
8633                 (gnus-summary-find-next)))))
8634
8635 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8636 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8637 (defun gnus-summary-limit-include-expunged (&optional no-error)
8638   "Display all the hidden articles that were expunged for low scores."
8639   (interactive)
8640   (let ((buffer-read-only nil))
8641     (let ((scored gnus-newsgroup-scored)
8642           headers h)
8643       (while scored
8644         (unless (gnus-summary-goto-subject (caar scored))
8645           (and (setq h (gnus-summary-article-header (caar scored)))
8646                (< (cdar scored) gnus-summary-expunge-below)
8647                (push h headers)))
8648         (setq scored (cdr scored)))
8649       (if (not headers)
8650           (when (not no-error)
8651             (error "No expunged articles hidden"))
8652         (goto-char (point-min))
8653         (gnus-summary-prepare-unthreaded (nreverse headers))
8654         (goto-char (point-min))
8655         (gnus-summary-position-point)
8656         t))))
8657
8658 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8659   "Mark all unread articles in this newsgroup as read.
8660 If prefix argument ALL is non-nil, ticked and dormant articles will
8661 also be marked as read.
8662 If QUIETLY is non-nil, no questions will be asked.
8663 If TO-HERE is non-nil, it should be a point in the buffer.  All
8664 articles before this point will be marked as read.
8665 Note that this function will only catch up the unread article
8666 in the current summary buffer limitation.
8667 The number of articles marked as read is returned."
8668   (interactive "P")
8669   (prog1
8670       (save-excursion
8671         (when (or quietly
8672                   (not gnus-interactive-catchup) ;Without confirmation?
8673                   gnus-expert-user
8674                   (gnus-y-or-n-p
8675                    (if all
8676                        "Mark absolutely all articles as read? "
8677                      "Mark all unread articles as read? ")))
8678           (if (and not-mark
8679                    (not gnus-newsgroup-adaptive)
8680                    (not gnus-newsgroup-auto-expire)
8681                    (not gnus-suppress-duplicates)
8682                    (or (not gnus-use-cache)
8683                        (eq gnus-use-cache 'passive)))
8684               (progn
8685                 (when all
8686                   (setq gnus-newsgroup-marked nil
8687                         gnus-newsgroup-dormant nil))
8688                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8689             ;; We actually mark all articles as canceled, which we
8690             ;; have to do when using auto-expiry or adaptive scoring.
8691             (gnus-summary-show-all-threads)
8692             (when (gnus-summary-first-subject (not all) t)
8693               (while (and
8694                       (if to-here (< (point) to-here) t)
8695                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8696                       (gnus-summary-find-next (not all) nil nil t))))
8697             (gnus-set-mode-line 'summary))
8698           t))
8699     (gnus-summary-position-point)))
8700
8701 (defun gnus-summary-catchup-to-here (&optional all)
8702   "Mark all unticked articles before the current one as read.
8703 If ALL is non-nil, also mark ticked and dormant articles as read."
8704   (interactive "P")
8705   (save-excursion
8706     (gnus-save-hidden-threads
8707       (let ((beg (point)))
8708         ;; We check that there are unread articles.
8709         (when (or all (gnus-summary-find-prev))
8710           (gnus-summary-catchup all t beg)))))
8711   (gnus-summary-position-point))
8712
8713 (defun gnus-summary-catchup-all (&optional quietly)
8714   "Mark all articles in this newsgroup as read."
8715   (interactive "P")
8716   (gnus-summary-catchup t quietly))
8717
8718 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8719   "Mark all unread articles in this group as read, then exit.
8720 If prefix argument ALL is non-nil, all articles are marked as read."
8721   (interactive "P")
8722   (when (gnus-summary-catchup all quietly nil 'fast)
8723     ;; Select next newsgroup or exit.
8724     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8725              (eq gnus-auto-select-next 'quietly))
8726         (gnus-summary-next-group nil)
8727       (gnus-summary-exit))))
8728
8729 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8730   "Mark all articles in this newsgroup as read, and then exit."
8731   (interactive "P")
8732   (gnus-summary-catchup-and-exit t quietly))
8733
8734 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8735   "Mark all articles in this group as read and select the next group.
8736 If given a prefix, mark all articles, unread as well as ticked, as
8737 read."
8738   (interactive "P")
8739   (save-excursion
8740     (gnus-summary-catchup all))
8741   (gnus-summary-next-group))
8742
8743 ;;;
8744 ;;; with article
8745 ;;;
8746
8747 (defmacro gnus-with-article (article &rest forms)
8748   "Select ARTICLE and perform FORMS in the original article buffer.
8749 Then replace the article with the result."
8750   `(progn
8751      ;; We don't want the article to be marked as read.
8752      (let (gnus-mark-article-hook)
8753        (gnus-summary-select-article t t nil ,article))
8754      (set-buffer gnus-original-article-buffer)
8755      ,@forms
8756      (if (not (gnus-check-backend-function
8757                'request-replace-article (car gnus-article-current)))
8758          (gnus-message 5 "Read-only group; not replacing")
8759        (unless (gnus-request-replace-article
8760                 ,article (car gnus-article-current)
8761                 (current-buffer) t)
8762          (error "Couldn't replace article")))
8763      ;; The cache and backlog have to be flushed somewhat.
8764      (when gnus-keep-backlog
8765        (gnus-backlog-remove-article
8766         (car gnus-article-current) (cdr gnus-article-current)))
8767      (when gnus-use-cache
8768        (gnus-cache-update-article
8769         (car gnus-article-current) (cdr gnus-article-current)))))
8770
8771 (put 'gnus-with-article 'lisp-indent-function 1)
8772 (put 'gnus-with-article 'edebug-form-spec '(form body))
8773
8774 ;; Thread-based commands.
8775
8776 (defun gnus-summary-articles-in-thread (&optional article)
8777   "Return a list of all articles in the current thread.
8778 If ARTICLE is non-nil, return all articles in the thread that starts
8779 with that article."
8780   (let* ((article (or article (gnus-summary-article-number)))
8781          (data (gnus-data-find-list article))
8782          (top-level (gnus-data-level (car data)))
8783          (top-subject
8784           (cond ((null gnus-thread-operation-ignore-subject)
8785                  (gnus-simplify-subject-re
8786                   (mail-header-subject (gnus-data-header (car data)))))
8787                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8788                  (gnus-simplify-subject-fuzzy
8789                   (mail-header-subject (gnus-data-header (car data)))))
8790                 (t nil)))
8791          (end-point (save-excursion
8792                       (if (gnus-summary-go-to-next-thread)
8793                           (point) (point-max))))
8794          articles)
8795     (while (and data
8796                 (< (gnus-data-pos (car data)) end-point))
8797       (when (or (not top-subject)
8798                 (string= top-subject
8799                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8800                              (gnus-simplify-subject-fuzzy
8801                               (mail-header-subject
8802                                (gnus-data-header (car data))))
8803                            (gnus-simplify-subject-re
8804                             (mail-header-subject
8805                              (gnus-data-header (car data)))))))
8806         (push (gnus-data-number (car data)) articles))
8807       (unless (and (setq data (cdr data))
8808                    (> (gnus-data-level (car data)) top-level))
8809         (setq data nil)))
8810     ;; Return the list of articles.
8811     (nreverse articles)))
8812
8813 (defun gnus-summary-rethread-current ()
8814   "Rethread the thread the current article is part of."
8815   (interactive)
8816   (let* ((gnus-show-threads t)
8817          (article (gnus-summary-article-number))
8818          (id (mail-header-id (gnus-summary-article-header)))
8819          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8820     (unless id
8821       (error "No article on the current line"))
8822     (gnus-rebuild-thread id)
8823     (gnus-summary-goto-subject article)))
8824
8825 (defun gnus-summary-reparent-thread ()
8826   "Make the current article child of the marked (or previous) article.
8827
8828 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8829 is non-nil or the Subject: of both articles are the same."
8830   (interactive)
8831   (unless (not (gnus-group-read-only-p))
8832     (error "The current newsgroup does not support article editing"))
8833   (unless (<= (length gnus-newsgroup-processable) 1)
8834     (error "No more than one article may be marked"))
8835   (save-window-excursion
8836     (let ((gnus-article-buffer " *reparent*")
8837           (current-article (gnus-summary-article-number))
8838           ;; First grab the marked article, otherwise one line up.
8839           (parent-article (if (not (null gnus-newsgroup-processable))
8840                               (car gnus-newsgroup-processable)
8841                             (save-excursion
8842                               (if (eq (forward-line -1) 0)
8843                                   (gnus-summary-article-number)
8844                                 (error "Beginning of summary buffer"))))))
8845       (unless (not (eq current-article parent-article))
8846         (error "An article may not be self-referential"))
8847       (let ((message-id (mail-header-id
8848                          (gnus-summary-article-header parent-article))))
8849         (unless (and message-id (not (equal message-id "")))
8850           (error "No message-id in desired parent"))
8851         (gnus-with-article current-article
8852           (save-restriction
8853             (goto-char (point-min))
8854             (message-narrow-to-head)
8855             (if (re-search-forward "^References: " nil t)
8856                 (progn
8857                   (re-search-forward "^[^ \t]" nil t)
8858                   (forward-line -1)
8859                   (end-of-line)
8860                   (insert " " message-id))
8861               (insert "References: " message-id "\n"))))
8862         (set-buffer gnus-summary-buffer)
8863         (gnus-summary-unmark-all-processable)
8864         (gnus-summary-update-article current-article)
8865         (gnus-summary-rethread-current)
8866         (gnus-message 3 "Article %d is now the child of article %d"
8867                       current-article parent-article)))))
8868
8869 (defun gnus-summary-toggle-threads (&optional arg)
8870   "Toggle showing conversation threads.
8871 If ARG is positive number, turn showing conversation threads on."
8872   (interactive "P")
8873   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8874     (setq gnus-show-threads
8875           (if (null arg) (not gnus-show-threads)
8876             (> (prefix-numeric-value arg) 0)))
8877     (gnus-summary-prepare)
8878     (gnus-summary-goto-subject current)
8879     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8880     (gnus-summary-position-point)))
8881
8882 (defun gnus-summary-show-all-threads ()
8883   "Show all threads."
8884   (interactive)
8885   (save-excursion
8886     (let ((buffer-read-only nil))
8887       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8888   (gnus-summary-position-point))
8889
8890 (defun gnus-summary-show-thread ()
8891   "Show thread subtrees.
8892 Returns nil if no thread was there to be shown."
8893   (interactive)
8894   (let ((buffer-read-only nil)
8895         (orig (point))
8896         ;; first goto end then to beg, to have point at beg after let
8897         (end (progn (end-of-line) (point)))
8898         (beg (progn (beginning-of-line) (point))))
8899     (prog1
8900         ;; Any hidden lines here?
8901         (search-forward "\r" end t)
8902       (subst-char-in-region beg end ?\^M ?\n t)
8903       (goto-char orig)
8904       (gnus-summary-position-point))))
8905
8906 (defun gnus-summary-hide-all-threads ()
8907   "Hide all thread subtrees."
8908   (interactive)
8909   (save-excursion
8910     (goto-char (point-min))
8911     (gnus-summary-hide-thread)
8912     (while (zerop (gnus-summary-next-thread 1 t))
8913       (gnus-summary-hide-thread)))
8914   (gnus-summary-position-point))
8915
8916 (defun gnus-summary-hide-thread ()
8917   "Hide thread subtrees.
8918 Returns nil if no threads were there to be hidden."
8919   (interactive)
8920   (let ((buffer-read-only nil)
8921         (start (point))
8922         (article (gnus-summary-article-number)))
8923     (goto-char start)
8924     ;; Go forward until either the buffer ends or the subthread
8925     ;; ends.
8926     (when (and (not (eobp))
8927                (or (zerop (gnus-summary-next-thread 1 t))
8928                    (goto-char (point-max))))
8929       (prog1
8930           (if (and (> (point) start)
8931                    (search-backward "\n" start t))
8932               (progn
8933                 (subst-char-in-region start (point) ?\n ?\^M)
8934                 (gnus-summary-goto-subject article))
8935             (goto-char start)
8936             nil)))))
8937
8938 (defun gnus-summary-go-to-next-thread (&optional previous)
8939   "Go to the same level (or less) next thread.
8940 If PREVIOUS is non-nil, go to previous thread instead.
8941 Return the article number moved to, or nil if moving was impossible."
8942   (let ((level (gnus-summary-thread-level))
8943         (way (if previous -1 1))
8944         (beg (point)))
8945     (forward-line way)
8946     (while (and (not (eobp))
8947                 (< level (gnus-summary-thread-level)))
8948       (forward-line way))
8949     (if (eobp)
8950         (progn
8951           (goto-char beg)
8952           nil)
8953       (setq beg (point))
8954       (prog1
8955           (gnus-summary-article-number)
8956         (goto-char beg)))))
8957
8958 (defun gnus-summary-next-thread (n &optional silent)
8959   "Go to the same level next N'th thread.
8960 If N is negative, search backward instead.
8961 Returns the difference between N and the number of skips actually
8962 done.
8963
8964 If SILENT, don't output messages."
8965   (interactive "p")
8966   (let ((backward (< n 0))
8967         (n (abs n)))
8968     (while (and (> n 0)
8969                 (gnus-summary-go-to-next-thread backward))
8970       (decf n))
8971     (unless silent
8972       (gnus-summary-position-point))
8973     (when (and (not silent) (/= 0 n))
8974       (gnus-message 7 "No more threads"))
8975     n))
8976
8977 (defun gnus-summary-prev-thread (n)
8978   "Go to the same level previous N'th thread.
8979 Returns the difference between N and the number of skips actually
8980 done."
8981   (interactive "p")
8982   (gnus-summary-next-thread (- n)))
8983
8984 (defun gnus-summary-go-down-thread ()
8985   "Go down one level in the current thread."
8986   (let ((children (gnus-summary-article-children)))
8987     (when children
8988       (gnus-summary-goto-subject (car children)))))
8989
8990 (defun gnus-summary-go-up-thread ()
8991   "Go up one level in the current thread."
8992   (let ((parent (gnus-summary-article-parent)))
8993     (when parent
8994       (gnus-summary-goto-subject parent))))
8995
8996 (defun gnus-summary-down-thread (n)
8997   "Go down thread N steps.
8998 If N is negative, go up instead.
8999 Returns the difference between N and how many steps down that were
9000 taken."
9001   (interactive "p")
9002   (let ((up (< n 0))
9003         (n (abs n)))
9004     (while (and (> n 0)
9005                 (if up (gnus-summary-go-up-thread)
9006                   (gnus-summary-go-down-thread)))
9007       (setq n (1- n)))
9008     (gnus-summary-position-point)
9009     (when (/= 0 n)
9010       (gnus-message 7 "Can't go further"))
9011     n))
9012
9013 (defun gnus-summary-up-thread (n)
9014   "Go up thread N steps.
9015 If N is negative, go up instead.
9016 Returns the difference between N and how many steps down that were
9017 taken."
9018   (interactive "p")
9019   (gnus-summary-down-thread (- n)))
9020
9021 (defun gnus-summary-top-thread ()
9022   "Go to the top of the thread."
9023   (interactive)
9024   (while (gnus-summary-go-up-thread))
9025   (gnus-summary-article-number))
9026
9027 (defun gnus-summary-kill-thread (&optional unmark)
9028   "Mark articles under current thread as read.
9029 If the prefix argument is positive, remove any kinds of marks.
9030 If the prefix argument is negative, tick articles instead."
9031   (interactive "P")
9032   (when unmark
9033     (setq unmark (prefix-numeric-value unmark)))
9034   (let ((articles (gnus-summary-articles-in-thread)))
9035     (save-excursion
9036       ;; Expand the thread.
9037       (gnus-summary-show-thread)
9038       ;; Mark all the articles.
9039       (while articles
9040         (gnus-summary-goto-subject (car articles))
9041         (cond ((null unmark)
9042                (gnus-summary-mark-article-as-read gnus-killed-mark))
9043               ((> unmark 0)
9044                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9045               (t
9046                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9047         (setq articles (cdr articles))))
9048     ;; Hide killed subtrees.
9049     (and (null unmark)
9050          gnus-thread-hide-killed
9051          (gnus-summary-hide-thread))
9052     ;; If marked as read, go to next unread subject.
9053     (when (null unmark)
9054       ;; Go to next unread subject.
9055       (gnus-summary-next-subject 1 t)))
9056   (gnus-set-mode-line 'summary))
9057
9058 ;; Summary sorting commands
9059
9060 (defun gnus-summary-sort-by-number (&optional reverse)
9061   "Sort the summary buffer by article number.
9062 Argument REVERSE means reverse order."
9063   (interactive "P")
9064   (gnus-summary-sort 'number reverse))
9065
9066 (defun gnus-summary-sort-by-author (&optional reverse)
9067   "Sort the summary buffer by author name alphabetically.
9068 If `case-fold-search' is non-nil, case of letters is ignored.
9069 Argument REVERSE means reverse order."
9070   (interactive "P")
9071   (gnus-summary-sort 'author reverse))
9072
9073 (defun gnus-summary-sort-by-subject (&optional reverse)
9074   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9075 If `case-fold-search' is non-nil, case of letters is ignored.
9076 Argument REVERSE means reverse order."
9077   (interactive "P")
9078   (gnus-summary-sort 'subject reverse))
9079
9080 (defun gnus-summary-sort-by-date (&optional reverse)
9081   "Sort the summary buffer by date.
9082 Argument REVERSE means reverse order."
9083   (interactive "P")
9084   (gnus-summary-sort 'date reverse))
9085
9086 (defun gnus-summary-sort-by-score (&optional reverse)
9087   "Sort the summary buffer by score.
9088 Argument REVERSE means reverse order."
9089   (interactive "P")
9090   (gnus-summary-sort 'score reverse))
9091
9092 (defun gnus-summary-sort-by-lines (&optional reverse)
9093   "Sort the summary buffer by the number of lines.
9094 Argument REVERSE means reverse order."
9095   (interactive "P")
9096   (gnus-summary-sort 'lines reverse))
9097
9098 (defun gnus-summary-sort-by-chars (&optional reverse)
9099   "Sort the summary buffer by article length.
9100 Argument REVERSE means reverse order."
9101   (interactive "P")
9102   (gnus-summary-sort 'chars reverse))
9103
9104 (defun gnus-summary-sort (predicate reverse)
9105   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9106   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9107          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9108          (gnus-thread-sort-functions
9109           (if (not reverse)
9110               thread
9111             `(lambda (t1 t2)
9112                (,thread t2 t1))))
9113          (gnus-sort-gathered-threads-function
9114           gnus-thread-sort-functions)
9115          (gnus-article-sort-functions
9116           (if (not reverse)
9117               article
9118             `(lambda (t1 t2)
9119                (,article t2 t1))))
9120          (buffer-read-only)
9121          (gnus-summary-prepare-hook nil))
9122     ;; We do the sorting by regenerating the threads.
9123     (gnus-summary-prepare)
9124     ;; Hide subthreads if needed.
9125     (when (and gnus-show-threads gnus-thread-hide-subtree)
9126       (gnus-summary-hide-all-threads))))
9127
9128 ;; Summary saving commands.
9129
9130 (defun gnus-summary-save-article (&optional n not-saved)
9131   "Save the current article using the default saver function.
9132 If N is a positive number, save the N next articles.
9133 If N is a negative number, save the N previous articles.
9134 If N is nil and any articles have been marked with the process mark,
9135 save those articles instead.
9136 The variable `gnus-default-article-saver' specifies the saver function."
9137   (interactive "P")
9138   (let* ((articles (gnus-summary-work-articles n))
9139          (save-buffer (save-excursion
9140                         (nnheader-set-temp-buffer " *Gnus Save*")))
9141          (num (length articles))
9142          header file)
9143     (dolist (article articles)
9144       (setq header (gnus-summary-article-header article))
9145       (if (not (vectorp header))
9146           ;; This is a pseudo-article.
9147           (if (assq 'name header)
9148               (gnus-copy-file (cdr (assq 'name header)))
9149             (gnus-message 1 "Article %d is unsaveable" article))
9150         ;; This is a real article.
9151         (save-window-excursion
9152           (gnus-summary-select-article t nil nil article))
9153         (save-excursion
9154           (set-buffer save-buffer)
9155           (erase-buffer)
9156           (insert-buffer-substring gnus-original-article-buffer))
9157         (setq file (gnus-article-save save-buffer file num))
9158         (gnus-summary-remove-process-mark article)
9159         (unless not-saved
9160           (gnus-summary-set-saved-mark article))))
9161     (gnus-kill-buffer save-buffer)
9162     (gnus-summary-position-point)
9163     (gnus-set-mode-line 'summary)
9164     n))
9165
9166 (defun gnus-summary-pipe-output (&optional arg)
9167   "Pipe the current article to a subprocess.
9168 If N is a positive number, pipe the N next articles.
9169 If N is a negative number, pipe the N previous articles.
9170 If N is nil and any articles have been marked with the process mark,
9171 pipe those articles instead."
9172   (interactive "P")
9173   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9174     (gnus-summary-save-article arg t))
9175   (gnus-configure-windows 'pipe))
9176
9177 (defun gnus-summary-save-article-mail (&optional arg)
9178   "Append the current article to an mail file.
9179 If N is a positive number, save the N next articles.
9180 If N is a negative number, save the N previous articles.
9181 If N is nil and any articles have been marked with the process mark,
9182 save those articles instead."
9183   (interactive "P")
9184   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9185     (gnus-summary-save-article arg)))
9186
9187 (defun gnus-summary-save-article-rmail (&optional arg)
9188   "Append the current article to an rmail file.
9189 If N is a positive number, save the N next articles.
9190 If N is a negative number, save the N previous articles.
9191 If N is nil and any articles have been marked with the process mark,
9192 save those articles instead."
9193   (interactive "P")
9194   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9195     (gnus-summary-save-article arg)))
9196
9197 (defun gnus-summary-save-article-file (&optional arg)
9198   "Append the current article to a file.
9199 If N is a positive number, save the N next articles.
9200 If N is a negative number, save the N previous articles.
9201 If N is nil and any articles have been marked with the process mark,
9202 save those articles instead."
9203   (interactive "P")
9204   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9205     (gnus-summary-save-article arg)))
9206
9207 (defun gnus-summary-write-article-file (&optional arg)
9208   "Write the current article to a file, deleting the previous file.
9209 If N is a positive number, save the N next articles.
9210 If N is a negative number, save the N previous articles.
9211 If N is nil and any articles have been marked with the process mark,
9212 save those articles instead."
9213   (interactive "P")
9214   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9215     (gnus-summary-save-article arg)))
9216
9217 (defun gnus-summary-save-article-body-file (&optional arg)
9218   "Append the current article body to a file.
9219 If N is a positive number, save the N next articles.
9220 If N is a negative number, save the N previous articles.
9221 If N is nil and any articles have been marked with the process mark,
9222 save those articles instead."
9223   (interactive "P")
9224   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9225     (gnus-summary-save-article arg)))
9226
9227 (defun gnus-summary-pipe-message (program)
9228   "Pipe the current article through PROGRAM."
9229   (interactive "sProgram: ")
9230   (gnus-summary-select-article)
9231   (let ((mail-header-separator ""))
9232     (gnus-eval-in-buffer-window gnus-article-buffer
9233       (save-restriction
9234         (widen)
9235         (let ((start (window-start))
9236               buffer-read-only)
9237           (message-pipe-buffer-body program)
9238           (set-window-start (get-buffer-window (current-buffer)) start))))))
9239
9240 (defun gnus-get-split-value (methods)
9241   "Return a value based on the split METHODS."
9242   (let (split-name method result match)
9243     (when methods
9244       (save-excursion
9245         (set-buffer gnus-original-article-buffer)
9246         (save-restriction
9247           (nnheader-narrow-to-headers)
9248           (while methods
9249             (goto-char (point-min))
9250             (setq method (pop methods))
9251             (setq match (car method))
9252             (when (cond
9253                    ((stringp match)
9254                     ;; Regular expression.
9255                     (ignore-errors
9256                       (re-search-forward match nil t)))
9257                    ((gnus-functionp match)
9258                     ;; Function.
9259                     (save-restriction
9260                       (widen)
9261                       (setq result (funcall match gnus-newsgroup-name))))
9262                    ((consp match)
9263                     ;; Form.
9264                     (save-restriction
9265                       (widen)
9266                       (setq result (eval match)))))
9267               (setq split-name (append (cdr method) split-name))
9268               (cond ((stringp result)
9269                      (push (expand-file-name
9270                             result gnus-article-save-directory)
9271                            split-name))
9272                     ((consp result)
9273                      (setq split-name (append result split-name)))))))))
9274     (nreverse split-name)))
9275
9276 (defun gnus-valid-move-group-p (group)
9277   (and (boundp group)
9278        (symbol-name group)
9279        (symbol-value group)
9280        (gnus-get-function (gnus-find-method-for-group
9281                            (symbol-name group)) 'request-accept-article t)))
9282
9283 (defun gnus-read-move-group-name (prompt default articles prefix)
9284   "Read a group name."
9285   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9286          (minibuffer-confirm-incomplete nil) ; XEmacs
9287          (prom
9288           (format "%s %s to:"
9289                   prompt
9290                   (if (> (length articles) 1)
9291                       (format "these %d articles" (length articles))
9292                     "this article")))
9293          (to-newsgroup
9294           (cond
9295            ((null split-name)
9296             (gnus-completing-read default prom
9297                                   gnus-active-hashtb
9298                                   'gnus-valid-move-group-p
9299                                   nil prefix
9300                                   'gnus-group-history))
9301            ((= 1 (length split-name))
9302             (gnus-completing-read (car split-name) prom
9303                                   gnus-active-hashtb
9304                                   'gnus-valid-move-group-p
9305                                   nil nil
9306                                   'gnus-group-history))
9307            (t
9308             (gnus-completing-read nil prom
9309                                   (mapcar (lambda (el) (list el))
9310                                           (nreverse split-name))
9311                                   nil nil nil
9312                                   'gnus-group-history))))
9313          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9314     (when to-newsgroup
9315       (if (or (string= to-newsgroup "")
9316               (string= to-newsgroup prefix))
9317           (setq to-newsgroup default))
9318       (unless to-newsgroup
9319         (error "No group name entered"))
9320       (or (gnus-active to-newsgroup)
9321           (gnus-activate-group to-newsgroup nil nil to-method)
9322           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9323                                      to-newsgroup))
9324               (or (and (gnus-request-create-group to-newsgroup to-method)
9325                        (gnus-activate-group
9326                         to-newsgroup nil nil to-method)
9327                        (gnus-subscribe-group to-newsgroup))
9328                   (error "Couldn't create group %s" to-newsgroup)))
9329           (error "No such group: %s" to-newsgroup)))
9330     to-newsgroup))
9331
9332 (defun gnus-summary-save-parts (type dir n &optional reverse)
9333   "Save parts matching TYPE to DIR.
9334 If REVERSE, save parts that do not match TYPE."
9335   (interactive
9336    (list (read-string "Save parts of type: " 
9337                       (or (car gnus-summary-save-parts-type-history)
9338                           gnus-summary-save-parts-default-mime)
9339                       'gnus-summary-save-parts-type-history)
9340          (setq gnus-summary-save-parts-last-directory
9341                (read-file-name "Save to directory: " 
9342                                gnus-summary-save-parts-last-directory
9343                                nil t))
9344          current-prefix-arg))
9345   (gnus-summary-iterate n
9346     (let ((gnus-display-mime-function nil)
9347           (gnus-inhibit-treatment t))
9348       (gnus-summary-select-article))
9349     (save-excursion
9350       (set-buffer gnus-article-buffer)
9351       (let ((handles (or gnus-article-mime-handles
9352                          (mm-dissect-buffer) (mm-uu-dissect))))
9353         (when handles
9354           (gnus-summary-save-parts-1 type dir handles reverse)
9355           (unless gnus-article-mime-handles ;; Don't destroy this case.
9356             (mm-destroy-parts handles)))))))
9357
9358 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9359   (if (stringp (car handle))
9360       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9361               (cdr handle))
9362     (when (if reverse
9363               (not (string-match type (mm-handle-media-type handle)))
9364             (string-match type (mm-handle-media-type handle)))
9365       (let ((file (expand-file-name
9366                    (file-name-nondirectory
9367                     (or
9368                      (mail-content-type-get
9369                       (mm-handle-disposition handle) 'filename)
9370                      (concat gnus-newsgroup-name
9371                              "." (number-to-string
9372                                   (cdr gnus-article-current)))))
9373                    dir)))
9374         (unless (file-exists-p file)
9375           (mm-save-part-to-file handle file))))))
9376
9377 ;; Summary extract commands
9378
9379 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9380   (let ((buffer-read-only nil)
9381         (article (gnus-summary-article-number))
9382         after-article b e)
9383     (unless (gnus-summary-goto-subject article)
9384       (error "No such article: %d" article))
9385     (gnus-summary-position-point)
9386     ;; If all commands are to be bunched up on one line, we collect
9387     ;; them here.
9388     (unless gnus-view-pseudos-separately
9389       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9390             files action)
9391         (while ps
9392           (setq action (cdr (assq 'action (car ps))))
9393           (setq files (list (cdr (assq 'name (car ps)))))
9394           (while (and ps (cdr ps)
9395                       (string= (or action "1")
9396                                (or (cdr (assq 'action (cadr ps))) "2")))
9397             (push (cdr (assq 'name (cadr ps))) files)
9398             (setcdr ps (cddr ps)))
9399           (when files
9400             (when (not (string-match "%s" action))
9401               (push " " files))
9402             (push " " files)
9403             (when (assq 'execute (car ps))
9404               (setcdr (assq 'execute (car ps))
9405                       (funcall (if (string-match "%s" action)
9406                                    'format 'concat)
9407                                action
9408                                (mapconcat
9409                                 (lambda (f)
9410                                   (if (equal f " ")
9411                                       f
9412                                     (gnus-quote-arg-for-sh-or-csh f)))
9413                                 files " ")))))
9414           (setq ps (cdr ps)))))
9415     (if (and gnus-view-pseudos (not not-view))
9416         (while pslist
9417           (when (assq 'execute (car pslist))
9418             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9419                                   (eq gnus-view-pseudos 'not-confirm)))
9420           (setq pslist (cdr pslist)))
9421       (save-excursion
9422         (while pslist
9423           (setq after-article (or (cdr (assq 'article (car pslist)))
9424                                   (gnus-summary-article-number)))
9425           (gnus-summary-goto-subject after-article)
9426           (forward-line 1)
9427           (setq b (point))
9428           (insert "    " (file-name-nondirectory
9429                           (cdr (assq 'name (car pslist))))
9430                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9431           (setq e (point))
9432           (forward-line -1)             ; back to `b'
9433           (gnus-add-text-properties
9434            b (1- e) (list 'gnus-number gnus-reffed-article-number
9435                           gnus-mouse-face-prop gnus-mouse-face))
9436           (gnus-data-enter
9437            after-article gnus-reffed-article-number
9438            gnus-unread-mark b (car pslist) 0 (- e b))
9439           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9440           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9441           (setq pslist (cdr pslist)))))))
9442
9443 (defun gnus-pseudos< (p1 p2)
9444   (let ((c1 (cdr (assq 'action p1)))
9445         (c2 (cdr (assq 'action p2))))
9446     (and c1 c2 (string< c1 c2))))
9447
9448 (defun gnus-request-pseudo-article (props)
9449   (cond ((assq 'execute props)
9450          (gnus-execute-command (cdr (assq 'execute props)))))
9451   (let ((gnus-current-article (gnus-summary-article-number)))
9452     (gnus-run-hooks 'gnus-mark-article-hook)))
9453
9454 (defun gnus-execute-command (command &optional automatic)
9455   (save-excursion
9456     (gnus-article-setup-buffer)
9457     (set-buffer gnus-article-buffer)
9458     (setq buffer-read-only nil)
9459     (let ((command (if automatic command
9460                      (read-string "Command: " (cons command 0)))))
9461       (erase-buffer)
9462       (insert "$ " command "\n\n")
9463       (if gnus-view-pseudo-asynchronously
9464           (start-process "gnus-execute" (current-buffer) shell-file-name
9465                          shell-command-switch command)
9466         (call-process shell-file-name nil t nil
9467                       shell-command-switch command)))))
9468
9469 ;; Summary kill commands.
9470
9471 (defun gnus-summary-edit-global-kill (article)
9472   "Edit the \"global\" kill file."
9473   (interactive (list (gnus-summary-article-number)))
9474   (gnus-group-edit-global-kill article))
9475
9476 (defun gnus-summary-edit-local-kill ()
9477   "Edit a local kill file applied to the current newsgroup."
9478   (interactive)
9479   (setq gnus-current-headers (gnus-summary-article-header))
9480   (gnus-group-edit-local-kill
9481    (gnus-summary-article-number) gnus-newsgroup-name))
9482
9483 ;;; Header reading.
9484
9485 (defun gnus-read-header (id &optional header)
9486   "Read the headers of article ID and enter them into the Gnus system."
9487   (let ((group gnus-newsgroup-name)
9488         (gnus-override-method
9489          (or
9490           gnus-override-method
9491           (and (gnus-news-group-p gnus-newsgroup-name)
9492                (car (gnus-refer-article-methods)))))
9493         where)
9494     ;; First we check to see whether the header in question is already
9495     ;; fetched.
9496     (if (stringp id)
9497         ;; This is a Message-ID.
9498         (setq header (or header (gnus-id-to-header id)))
9499       ;; This is an article number.
9500       (setq header (or header (gnus-summary-article-header id))))
9501     (if (and header
9502              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9503         ;; We have found the header.
9504         header
9505       ;; If this is a sparse article, we have to nix out its
9506       ;; previous entry in the thread hashtb.
9507       (when (and header
9508                  (gnus-summary-article-sparse-p (mail-header-number header)))
9509         (let* ((parent (gnus-parent-id (mail-header-references header)))
9510                (thread (and parent (gnus-id-to-thread parent))))
9511           (when thread
9512             (delq (assq header thread) thread))))
9513       ;; We have to really fetch the header to this article.
9514       (save-excursion
9515         (set-buffer nntp-server-buffer)
9516         (when (setq where (gnus-request-head id group))
9517           (nnheader-fold-continuation-lines)
9518           (goto-char (point-max))
9519           (insert ".\n")
9520           (goto-char (point-min))
9521           (insert "211 ")
9522           (princ (cond
9523                   ((numberp id) id)
9524                   ((cdr where) (cdr where))
9525                   (header (mail-header-number header))
9526                   (t gnus-reffed-article-number))
9527                  (current-buffer))
9528           (insert " Article retrieved.\n"))
9529         (if (or (not where)
9530                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9531             ()                          ; Malformed head.
9532           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9533             (when (and (stringp id)
9534                        (not (string= (gnus-group-real-name group)
9535                                      (car where))))
9536               ;; If we fetched by Message-ID and the article came
9537               ;; from a different group, we fudge some bogus article
9538               ;; numbers for this article.
9539               (mail-header-set-number header gnus-reffed-article-number))
9540             (save-excursion
9541               (set-buffer gnus-summary-buffer)
9542               (decf gnus-reffed-article-number)
9543               (gnus-remove-header (mail-header-number header))
9544               (push header gnus-newsgroup-headers)
9545               (setq gnus-current-headers header)
9546               (push (mail-header-number header) gnus-newsgroup-limit)))
9547           header)))))
9548
9549 (defun gnus-remove-header (number)
9550   "Remove header NUMBER from `gnus-newsgroup-headers'."
9551   (if (and gnus-newsgroup-headers
9552            (= number (mail-header-number (car gnus-newsgroup-headers))))
9553       (pop gnus-newsgroup-headers)
9554     (let ((headers gnus-newsgroup-headers))
9555       (while (and (cdr headers)
9556                   (not (= number (mail-header-number (cadr headers)))))
9557         (pop headers))
9558       (when (cdr headers)
9559         (setcdr headers (cddr headers))))))
9560
9561 ;;;
9562 ;;; summary highlights
9563 ;;;
9564
9565 (defun gnus-highlight-selected-summary ()
9566   "Highlight selected article in summary buffer."
9567   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9568   (when gnus-summary-selected-face
9569     (save-excursion
9570       (let* ((beg (progn (beginning-of-line) (point)))
9571              (end (progn (end-of-line) (point)))
9572              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9573              (from (if (get-text-property beg gnus-mouse-face-prop)
9574                        beg
9575                      (or (next-single-property-change
9576                           beg gnus-mouse-face-prop nil end)
9577                          beg)))
9578              (to
9579               (if (= from end)
9580                   (- from 2)
9581                 (or (next-single-property-change
9582                      from gnus-mouse-face-prop nil end)
9583                     end))))
9584         ;; If no mouse-face prop on line we will have to = from = end,
9585         ;; so we highlight the entire line instead.
9586         (when (= (+ to 2) from)
9587           (setq from beg)
9588           (setq to end))
9589         (if gnus-newsgroup-selected-overlay
9590             ;; Move old overlay.
9591             (gnus-move-overlay
9592              gnus-newsgroup-selected-overlay from to (current-buffer))
9593           ;; Create new overlay.
9594           (gnus-overlay-put
9595            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9596            'face gnus-summary-selected-face))))))
9597
9598 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9599 (defun gnus-summary-highlight-line ()
9600   "Highlight current line according to `gnus-summary-highlight'."
9601   (let* ((list gnus-summary-highlight)
9602          (p (point))
9603          (end (progn (end-of-line) (point)))
9604          ;; now find out where the line starts and leave point there.
9605          (beg (progn (beginning-of-line) (point)))
9606          (article (gnus-summary-article-number))
9607          (score (or (cdr (assq (or article gnus-current-article)
9608                                gnus-newsgroup-scored))
9609                     gnus-summary-default-score 0))
9610          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9611          (inhibit-read-only t))
9612     ;; Eval the cars of the lists until we find a match.
9613     (let ((default gnus-summary-default-score))
9614       (while (and list
9615                   (not (eval (caar list))))
9616         (setq list (cdr list))))
9617     (let ((face (cdar list)))
9618       (unless (eq face (get-text-property beg 'face))
9619         (gnus-put-text-property-excluding-characters-with-faces
9620          beg end 'face
9621          (setq face (if (boundp face) (symbol-value face) face)))
9622         (when gnus-summary-highlight-line-function
9623           (funcall gnus-summary-highlight-line-function article face))))
9624     (goto-char p)))
9625
9626 (defun gnus-update-read-articles (group unread &optional compute)
9627   "Update the list of read articles in GROUP."
9628   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9629          (entry (gnus-gethash group gnus-newsrc-hashtb))
9630          (info (nth 2 entry))
9631          (prev 1)
9632          (unread (sort (copy-sequence unread) '<))
9633          read)
9634     (if (or (not info) (not active))
9635         ;; There is no info on this group if it was, in fact,
9636         ;; killed.  Gnus stores no information on killed groups, so
9637         ;; there's nothing to be done.
9638         ;; One could store the information somewhere temporarily,
9639         ;; perhaps...  Hmmm...
9640         ()
9641       ;; Remove any negative articles numbers.
9642       (while (and unread (< (car unread) 0))
9643         (setq unread (cdr unread)))
9644       ;; Remove any expired article numbers
9645       (while (and unread (< (car unread) (car active)))
9646         (setq unread (cdr unread)))
9647       ;; Compute the ranges of read articles by looking at the list of
9648       ;; unread articles.
9649       (while unread
9650         (when (/= (car unread) prev)
9651           (push (if (= prev (1- (car unread))) prev
9652                   (cons prev (1- (car unread))))
9653                 read))
9654         (setq prev (1+ (car unread)))
9655         (setq unread (cdr unread)))
9656       (when (<= prev (cdr active))
9657         (push (cons prev (cdr active)) read))
9658       (setq read (if (> (length read) 1) (nreverse read) read))
9659       (if compute
9660           read
9661         (save-excursion
9662           (let (setmarkundo)
9663             ;; Propagate the read marks to the backend.
9664             (when (gnus-check-backend-function 'request-set-mark group)
9665               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9666                     (add (gnus-remove-from-range read (gnus-info-read info))))
9667                 (when (or add del)
9668                   (unless (gnus-check-group group)
9669                     (error "Can't open server for %s" group))
9670                   (gnus-request-set-mark
9671                    group (delq nil (list (if add (list add 'add '(read)))
9672                                          (if del (list del 'del '(read))))))
9673                   (setq setmarkundo
9674                         `(gnus-request-set-mark
9675                           ,group
9676                           ',(delq nil (list
9677                                        (if del (list del 'add '(read)))
9678                                        (if add (list add 'del '(read))))))))))
9679             (set-buffer gnus-group-buffer)
9680             (gnus-undo-register
9681               `(progn
9682                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9683                  (gnus-info-set-read ',info ',(gnus-info-read info))
9684                  (gnus-get-unread-articles-in-group ',info 
9685                                                     (gnus-active ,group))
9686                  (gnus-group-update-group ,group t)
9687                  ,setmarkundo))))
9688         ;; Enter this list into the group info.
9689         (gnus-info-set-read info read)
9690         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9691         (gnus-get-unread-articles-in-group info (gnus-active group))
9692         t))))
9693
9694 (defun gnus-offer-save-summaries ()
9695   "Offer to save all active summary buffers."
9696   (save-excursion
9697     (let ((buflist (buffer-list))
9698           buffers bufname)
9699       ;; Go through all buffers and find all summaries.
9700       (while buflist
9701         (and (setq bufname (buffer-name (car buflist)))
9702              (string-match "Summary" bufname)
9703              (save-excursion
9704                (set-buffer bufname)
9705                ;; We check that this is, indeed, a summary buffer.
9706                (and (eq major-mode 'gnus-summary-mode)
9707                     ;; Also make sure this isn't bogus.
9708                     gnus-newsgroup-prepared
9709                     ;; Also make sure that this isn't a dead summary buffer.
9710                     (not gnus-dead-summary-mode)))
9711              (push bufname buffers))
9712         (setq buflist (cdr buflist)))
9713       ;; Go through all these summary buffers and offer to save them.
9714       (when buffers
9715         (map-y-or-n-p
9716          "Update summary buffer %s? "
9717          (lambda (buf)
9718            (switch-to-buffer buf)
9719            (gnus-summary-exit))
9720          buffers)))))
9721
9722
9723 ;;; @ for mime-partial
9724 ;;;
9725
9726 (defun gnus-request-partial-message ()
9727   (save-excursion
9728     (let ((number (gnus-summary-article-number))
9729           (group gnus-newsgroup-name)
9730           (mother gnus-article-buffer))
9731       (set-buffer (get-buffer-create " *Partial Article*"))
9732       (erase-buffer)
9733       (setq mime-preview-buffer mother)
9734       (gnus-request-article-this-buffer number group)
9735       (mime-parse-buffer)
9736       )))
9737
9738 (autoload 'mime-combine-message/partial-pieces-automatically
9739   "mime-partial"
9740   "Internal method to combine message/partial messages automatically.")
9741
9742 (mime-add-condition
9743  'action '((type . message)(subtype . partial)
9744            (major-mode . gnus-original-article-mode)
9745            (method . mime-combine-message/partial-pieces-automatically)
9746            (summary-buffer-exp . gnus-summary-buffer)
9747            (request-partial-message-method . gnus-request-partial-message)
9748            ))
9749
9750
9751 ;;; @ for message/rfc822
9752 ;;;
9753
9754 (defun gnus-mime-extract-message/rfc822 (entity situation)
9755   (let (group article num cwin swin cur)
9756     (with-temp-buffer
9757       (mime-insert-entity-content entity)
9758       (setq group (or (cdr (assq 'group situation))
9759                       (completing-read "Group: "
9760                                        gnus-active-hashtb
9761                                        nil
9762                                        (gnus-read-active-file-p)
9763                                        gnus-newsgroup-name))
9764             article (gnus-request-accept-article group)))
9765     (when (and (consp article)
9766                (numberp (setq article (cdr article))))
9767       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9768             cwin (get-buffer-window (current-buffer) t))
9769       (save-window-excursion
9770         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9771             (select-window swin)
9772           (set-buffer gnus-summary-buffer))
9773         (setq cur gnus-current-article)
9774         (forward-line num)
9775         (let (gnus-show-threads)
9776           (gnus-summary-goto-subject article t))
9777         (gnus-summary-clear-mark-forward 1)
9778         (gnus-summary-goto-subject cur))
9779       (when (and cwin (window-frame cwin))
9780         (select-frame (window-frame cwin)))
9781       (when (boundp 'mime-acting-situation-to-override)
9782         (set-alist 'mime-acting-situation-to-override
9783                    'group
9784                    group)
9785         (set-alist 'mime-acting-situation-to-override
9786                    'after-method
9787                    `(progn
9788                       (save-current-buffer
9789                         (set-buffer gnus-group-buffer)
9790                         (gnus-activate-group ,group))
9791                       (gnus-summary-goto-article ,cur
9792                                                  gnus-show-all-headers)))
9793         (set-alist 'mime-acting-situation-to-override
9794                    'number num)))))
9795
9796 (mime-add-condition
9797  'action '((type . message)(subtype . rfc822)
9798            (major-mode . gnus-original-article-mode)
9799            (method . gnus-mime-extract-message/rfc822)
9800            (mode . "extract")
9801            ))
9802
9803 (mime-add-condition
9804  'action '((type . message)(subtype . news)
9805            (major-mode . gnus-original-article-mode)
9806            (method . gnus-mime-extract-message/rfc822)
9807            (mode . "extract")
9808            ))
9809
9810 (defun gnus-mime-extract-multipart (entity situation)
9811   (let ((children (mime-entity-children entity))
9812         mime-acting-situation-to-override
9813         f)
9814     (while children
9815       (mime-play-entity (car children)
9816                         (cons (assq 'mode situation)
9817                               mime-acting-situation-to-override))
9818       (setq children (cdr children)))
9819     (if (setq f (cdr (assq 'after-method
9820                            mime-acting-situation-to-override)))
9821         (eval f)
9822       )))
9823
9824 (mime-add-condition
9825  'action '((type . multipart)
9826            (method . gnus-mime-extract-multipart)
9827            (mode . "extract")
9828            )
9829  'with-default)
9830
9831
9832 ;;; @ end
9833 ;;;
9834
9835 (defun gnus-summary-setup-default-charset ()
9836   "Setup newsgroup default charset."
9837   (if (equal gnus-newsgroup-name "nndraft:drafts")
9838       (setq gnus-newsgroup-charset nil)
9839     (let* ((name (and gnus-newsgroup-name
9840                       (gnus-group-real-name gnus-newsgroup-name)))
9841            (ignored-charsets
9842             (or gnus-newsgroup-ephemeral-ignored-charsets
9843                 (append
9844                  (and gnus-newsgroup-name
9845                       (or (gnus-group-find-parameter gnus-newsgroup-name
9846                                                      'ignored-charsets t)
9847                           (let ((alist gnus-group-ignored-charsets-alist)
9848                                 elem (charsets nil))
9849                             (while (setq elem (pop alist))
9850                               (when (and name
9851                                          (string-match (car elem) name))
9852                                 (setq alist nil
9853                                       charsets (cdr elem))))
9854                             charsets)))
9855                  gnus-newsgroup-ignored-charsets))))
9856       (setq gnus-newsgroup-charset
9857             (or gnus-newsgroup-ephemeral-charset
9858                 (and gnus-newsgroup-name
9859                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9860                          (let ((alist gnus-group-charset-alist)
9861                                elem charset)
9862                            (while (setq elem (pop alist))
9863                              (when (and name
9864                                         (string-match (car elem) name))
9865                                (setq alist nil
9866                                      charset (cadr elem))))
9867                            charset)))
9868                 gnus-default-charset))
9869       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9870            ignored-charsets))))
9871
9872 ;;;
9873 ;;; Mime Commands
9874 ;;;
9875
9876 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9877   "Display the current article buffer fully MIME-buttonized.
9878 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9879 treated as multipart/mixed."
9880   (interactive "P")
9881   (require 'gnus-art)
9882   (let ((gnus-unbuttonized-mime-types nil)
9883         (gnus-mime-display-multipart-as-mixed show-all-parts))
9884     (gnus-summary-show-article)))
9885
9886 (defun gnus-summary-repair-multipart (article)
9887   "Add a Content-Type header to a multipart article without one."
9888   (interactive (list (gnus-summary-article-number)))
9889   (gnus-with-article article
9890     (message-narrow-to-head)
9891     (goto-char (point-max))
9892     (widen)
9893     (when (search-forward "\n--" nil t)
9894       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9895         (message-narrow-to-head)
9896         (message-remove-header "Mime-Version")
9897         (message-remove-header "Content-Type")
9898         (goto-char (point-max))
9899         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9900                         separator))
9901         (insert "Mime-Version: 1.0\n")
9902         (widen))))
9903   (let (gnus-mark-article-hook)
9904     (gnus-summary-select-article t t nil article)))
9905
9906 (defun gnus-summary-toggle-display-buttonized ()
9907   "Toggle the buttonizing of the article buffer."
9908   (interactive)
9909   (require 'gnus-art)
9910   (if (setq gnus-inhibit-mime-unbuttonizing
9911             (not gnus-inhibit-mime-unbuttonizing))
9912       (let ((gnus-unbuttonized-mime-types nil))
9913         (gnus-summary-show-article))
9914     (gnus-summary-show-article)))
9915
9916 ;;;
9917 ;;; Intelli-mouse commmands
9918 ;;;
9919
9920 (defun gnus-wheel-summary-scroll (event)
9921   (interactive "e")
9922   (let ((amount (if (memq 'shift (event-modifiers event))
9923                     (car gnus-wheel-scroll-amount)
9924                   (cdr gnus-wheel-scroll-amount)))
9925         (direction (- (* (static-if (featurep 'xemacs)
9926                              (event-button event)
9927                            (cond ((eq 'mouse-4 (event-basic-type event))
9928                                   4)
9929                                  ((eq 'mouse-5 (event-basic-type event))
9930                                   5)))
9931                          2) 9))
9932         edge)
9933     (gnus-summary-scroll-up (* amount direction))
9934     (when (gnus-eval-in-buffer-window gnus-article-buffer
9935             (save-restriction
9936               (widen)
9937               (and (if (< 0 direction)
9938                        (gnus-article-next-page 0)
9939                      (gnus-article-prev-page 0)
9940                      (bobp))
9941                    (if (setq edge (get-text-property
9942                                    (point-min) 'gnus-wheel-edge))
9943                        (setq edge (* edge direction))
9944                      (setq edge -1))
9945                    (or (plusp edge)
9946                        (let ((buffer-read-only nil)
9947                              (inhibit-read-only t))
9948                          (put-text-property (point-min) (point-max)
9949                                             'gnus-wheel-edge direction)
9950                          nil))
9951                    (or (> edge gnus-wheel-edge-resistance)
9952                        (let ((buffer-read-only nil)
9953                              (inhibit-read-only t))
9954                          (put-text-property (point-min) (point-max)
9955                                             'gnus-wheel-edge
9956                                             (* (1+ edge) direction))
9957                          nil))
9958                    (eq last-command 'gnus-wheel-summary-scroll))))
9959       (gnus-summary-next-article nil nil (minusp direction)))))
9960
9961 (defun gnus-wheel-install ()
9962   "Enable mouse wheel support on summary window."
9963   (when gnus-use-wheel
9964     (let ((keys
9965            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9966       (dolist (key keys)
9967         (define-key gnus-summary-mode-map key
9968           'gnus-wheel-summary-scroll)))))
9969
9970 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9971
9972 ;;;
9973 ;;; Traditional PGP commmands
9974 ;;;
9975
9976 (defun gnus-summary-decrypt-article (&optional force)
9977   "Decrypt the current article in traditional PGP way.
9978 This will have permanent effect only in mail groups.
9979 If FORCE is non-nil, allow editing of articles even in read-only
9980 groups."
9981   (interactive "P")
9982   (gnus-summary-select-article t)
9983   (gnus-eval-in-buffer-window gnus-article-buffer
9984     (save-excursion
9985       (save-restriction
9986         (widen)
9987         (goto-char (point-min))
9988         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9989           (error "Not a traditional PGP message!"))
9990         (let ((armor-start (match-beginning 0)))
9991           (if (and (pgg-decrypt-region armor-start (point-max))
9992                    (or force (not (gnus-group-read-only-p))))
9993               (let ((inhibit-read-only t)
9994                     buffer-read-only)
9995                 (delete-region armor-start
9996                                (progn
9997                                  (re-search-forward "^-+END PGP" nil t)
9998                                  (beginning-of-line 2)
9999                                  (point)))
10000                 (insert-buffer-substring pgg-output-buffer))))))))
10001
10002 (defun gnus-summary-verify-article ()
10003   "Verify the current article in traditional PGP way."
10004   (interactive)
10005   (save-excursion
10006     (set-buffer gnus-original-article-buffer)
10007     (goto-char (point-min))
10008     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10009       (error "Not a traditional PGP message!"))
10010     (re-search-forward "^-+END PGP" nil t)
10011     (beginning-of-line 2)
10012     (call-interactively (function pgg-verify-region))))
10013
10014 ;;;
10015 ;;; Generic summary marking commands
10016 ;;;
10017
10018 (defvar gnus-summary-marking-alist
10019   '((read gnus-del-mark "d")
10020     (unread gnus-unread-mark "u")
10021     (ticked gnus-ticked-mark "!")
10022     (dormant gnus-dormant-mark "?")
10023     (expirable gnus-expirable-mark "e"))
10024   "An alist of names/marks/keystrokes.")
10025
10026 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10027 (defvar gnus-summary-mark-map)
10028
10029 (defun gnus-summary-make-all-marking-commands ()
10030   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10031   (dolist (elem gnus-summary-marking-alist)
10032     (apply 'gnus-summary-make-marking-command elem)))
10033
10034 (defun gnus-summary-make-marking-command (name mark keystroke)
10035   (let ((map (make-sparse-keymap)))
10036     (define-key gnus-summary-generic-mark-map keystroke map)
10037     (dolist (lway `((next "next" next nil "n")
10038                     (next-unread "next unread" next t "N")
10039                     (prev "previous" prev nil "p")
10040                     (prev-unread "previous unread" prev t "P")
10041                     (nomove "" nil nil ,keystroke)))
10042       (let ((func (gnus-summary-make-marking-command-1
10043                    mark (car lway) lway name)))
10044         (setq func (eval func))
10045         (define-key map (nth 4 lway) func)))))
10046
10047 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10048   `(defun ,(intern
10049             (format "gnus-summary-put-mark-as-%s%s"
10050                     name (if (eq way 'nomove)
10051                              ""
10052                            (concat "-" (symbol-name way)))))
10053      (n)
10054      ,(format
10055        "Mark the current article as %s%s.
10056 If N, the prefix, then repeat N times.
10057 If N is negative, move in reverse order.
10058 The difference between N and the actual number of articles marked is
10059 returned."
10060        name (car (cdr lway)))
10061      (interactive "p")
10062      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10063
10064 (defun gnus-summary-generic-mark (n mark move unread)
10065   "Mark N articles with MARK."
10066   (unless (eq major-mode 'gnus-summary-mode)
10067     (error "This command can only be used in the summary buffer"))
10068   (gnus-summary-show-thread)
10069   (let ((nummove
10070          (cond
10071           ((eq move 'next) 1)
10072           ((eq move 'prev) -1)
10073           (t 0))))
10074     (if (zerop nummove)
10075         (setq n 1)
10076       (when (< n 0)
10077         (setq n (abs n)
10078               nummove (* -1 nummove))))
10079     (while (and (> n 0)
10080                 (gnus-summary-mark-article nil mark)
10081                 (zerop (gnus-summary-next-subject nummove unread t)))
10082       (setq n (1- n)))
10083     (when (/= 0 n)
10084       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10085     (gnus-summary-recenter)
10086     (gnus-summary-position-point)
10087     (gnus-set-mode-line 'summary)
10088     n))
10089
10090 (gnus-summary-make-all-marking-commands)
10091
10092 (gnus-ems-redefine)
10093
10094 (provide 'gnus-sum)
10095
10096 (run-hooks 'gnus-sum-load-hook)
10097
10098 ;;; gnus-sum.el ends here