Synch with Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, many commands will go to the next unread article.
186 This applies to marking commands as well as other commands that
187 \"naturally\" select the next article, like, for instance, `SPC' at
188 the end of an article.
189
190 If nil, the marking commands do NOT go to the next unread article
191 (they go to the next article instead).  If `never', commands that
192 usually go to the next unread article, will go to the next article,
193 whether it is read or not."
194   :group 'gnus-summary-marks
195   :link '(custom-manual "(gnus)Setting Marks")
196   :type '(choice (const :tag "off" nil)
197                  (const never)
198                  (sexp :menu-tag "on" t)))
199
200 (defcustom gnus-summary-default-score 0
201   "*Default article score level.
202 All scores generated by the score files will be added to this score.
203 If this variable is nil, scoring will be disabled."
204   :group 'gnus-score-default
205   :type '(choice (const :tag "disable")
206                  integer))
207
208 (defcustom gnus-summary-zcore-fuzz 0
209   "*Fuzziness factor for the zcore in the summary buffer.
210 Articles with scores closer than this to `gnus-summary-default-score'
211 will not be marked."
212   :group 'gnus-summary-format
213   :type 'integer)
214
215 (defcustom gnus-simplify-subject-fuzzy-regexp nil
216   "*Strings to be removed when doing fuzzy matches.
217 This can either be a regular expression or list of regular expressions
218 that will be removed from subject strings if fuzzy subject
219 simplification is selected."
220   :group 'gnus-thread
221   :type '(repeat regexp))
222
223 (defcustom gnus-show-threads t
224   "*If non-nil, display threads in summary mode."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-subtree nil
229   "*If non-nil, hide all threads initially.
230 If threads are hidden, you have to run the command
231 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
232 to expose hidden threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-killed t
237   "*If non-nil, hide killed threads automatically."
238   :group 'gnus-thread
239   :type 'boolean)
240
241 (defcustom gnus-thread-ignore-subject t
242   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
243 If nil, articles that have different subjects from their parents will
244 start separate threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-operation-ignore-subject t
249   "*If non-nil, subjects will be ignored when doing thread commands.
250 This affects commands like `gnus-summary-kill-thread' and
251 `gnus-summary-lower-thread'.
252
253 If this variable is nil, articles in the same thread with different
254 subjects will not be included in the operation in question.  If this
255 variable is `fuzzy', only articles that have subjects that are fuzzily
256 equal will be included."
257   :group 'gnus-thread
258   :type '(choice (const :tag "off" nil)
259                  (const fuzzy)
260                  (sexp :tag "on" t)))
261
262 (defcustom gnus-thread-indent-level 4
263   "*Number that says how much each sub-thread should be indented."
264   :group 'gnus-thread
265   :type 'integer)
266
267 (defcustom gnus-auto-extend-newsgroup t
268   "*If non-nil, extend newsgroup forward and backward when requested."
269   :group 'gnus-summary-choose
270   :type 'boolean)
271
272 (defcustom gnus-auto-select-first t
273   "*If nil, don't select the first unread article when entering a group.
274 If this variable is `best', select the highest-scored unread article
275 in the group.  If t, select the first unread article.
276
277 This variable can also be a function to place point on a likely
278 subject line.  Useful values include `gnus-summary-first-unread-subject',
279 `gnus-summary-first-unread-article' and
280 `gnus-summary-best-unread-article'.
281
282 If you want to prevent automatic selection of the first unread article
283 in some newsgroups, set the variable to nil in
284 `gnus-select-group-hook'."
285   :group 'gnus-group-select
286   :type '(choice (const :tag "none" nil)
287                  (const best)
288                  (sexp :menu-tag "first" t)
289                  (function-item gnus-summary-first-unread-subject)
290                  (function-item gnus-summary-first-unread-article)
291                  (function-item gnus-summary-best-unread-article)))
292
293 (defcustom gnus-dont-select-after-jump-to-other-group nil
294   "If non-nil, don't select the first unread article after entering the
295 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
296 it is depend on the value of `gnus-auto-select-first' whether to select
297 or not."
298   :group 'gnus-group-select
299   :type 'boolean)
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-show-mime t
368   "*If non-nil, do mime processing of articles.
369 The articles will simply be fed to the function given by
370 `gnus-article-display-method-for-mime'."
371   :group 'gnus-article-mime
372   :type 'boolean)
373
374 (defcustom gnus-move-split-methods nil
375   "*Variable used to suggest where articles are to be moved to.
376 It uses the same syntax as the `gnus-split-methods' variable."
377   :group 'gnus-summary-mail
378   :type '(repeat (choice (list :value (fun) function)
379                          (cons :value ("" "") regexp (repeat string))
380                          (sexp :value nil))))
381
382 (defcustom gnus-unread-mark ?  ;Whitespace
383   "*Mark used for unread articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-ticked-mark ?!
388   "*Mark used for ticked articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-dormant-mark ??
393   "*Mark used for dormant articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-del-mark ?r
398   "*Mark used for del'd articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-read-mark ?R
403   "*Mark used for read articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-expirable-mark ?E
408   "*Mark used for expirable articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-killed-mark ?K
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-souped-mark ?F
418   "*Mark used for killed articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-kill-file-mark ?X
423   "*Mark used for articles killed by kill files."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-low-score-mark ?Y
428   "*Mark used for articles with a low score."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-catchup-mark ?C
433   "*Mark used for articles that are caught up."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-replied-mark ?A
438   "*Mark used for articles that have been replied to."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-cached-mark ?*
443   "*Mark used for articles that are in the cache."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-saved-mark ?S
448   "*Mark used for articles that have been saved to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ancient-mark ?O
453   "*Mark used for ancient articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-sparse-mark ?Q
458   "*Mark used for sparsely reffed articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-canceled-mark ?G
463   "*Mark used for canceled articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-duplicate-mark ?M
468   "*Mark used for duplicate articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-undownloaded-mark ?@
473   "*Mark used for articles that weren't downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-downloadable-mark ?%
478   "*Mark used for articles that are to be downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unsendable-mark ?=
483   "*Mark used for articles that won't be sent."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-over-mark ?+
488   "*Score mark used for articles with high scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-below-mark ?-
493   "*Score mark used for articles with low scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-empty-thread-mark ?  ;Whitespace
498   "*There is no thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-not-empty-thread-mark ?=
503   "*There is a thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-view-pseudo-asynchronously nil
508   "*If non-nil, Gnus will view pseudo-articles asynchronously."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-auto-expirable-marks
513   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
514         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
515         gnus-souped-mark gnus-duplicate-mark)
516   "*The list of marks converted into expiration if a group is auto-expirable."
517   :group 'gnus-summary
518   :type '(repeat character))
519
520 (defcustom gnus-inhibit-user-auto-expire t
521   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
522   :group 'gnus-summary
523   :type 'boolean)
524
525 (defcustom gnus-view-pseudos nil
526   "*If `automatic', pseudo-articles will be viewed automatically.
527 If `not-confirm', pseudos will be viewed automatically, and the user
528 will not be asked to confirm the command."
529   :group 'gnus-extract-view
530   :type '(choice (const :tag "off" nil)
531                  (const automatic)
532                  (const not-confirm)))
533
534 (defcustom gnus-view-pseudos-separately t
535   "*If non-nil, one pseudo-article will be created for each file to be viewed.
536 If nil, all files that use the same viewing command will be given as a
537 list of parameters to that command."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-insert-pseudo-articles t
542   "*If non-nil, insert pseudo-articles when decoding articles."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-summary-dummy-line-format
547   "  %(:                          :%) %S\n"
548   "*The format specification for the dummy roots in the summary buffer.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions.
551
552 %S  The subject"
553   :group 'gnus-threading
554   :type 'string)
555
556 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
557   "*The format specification for the summary mode line.
558 It works along the same lines as a normal formatting string,
559 with some simple extensions:
560
561 %G  Group name
562 %p  Unprefixed group name
563 %A  Current article number
564 %z  Current article score
565 %V  Gnus version
566 %U  Number of unread articles in the group
567 %e  Number of unselected articles in the group
568 %Z  A string with unread/unselected article counts
569 %g  Shortish group name
570 %S  Subject of the current article
571 %u  User-defined spec
572 %s  Current score file name
573 %d  Number of dormant articles
574 %r  Number of articles that have been marked as read in this session
575 %E  Number of articles expunged by the score files"
576   :group 'gnus-summary-format
577   :type 'string)
578
579 (defcustom gnus-list-identifiers nil
580   "Regexp that matches list identifiers to be removed from subject.
581 This can also be a list of regexps."
582   :group 'gnus-summary-format
583   :group 'gnus-article-hiding
584   :type '(choice (const :tag "none" nil)
585                  (regexp :value ".*")
586                  (repeat :value (".*") regexp)))
587
588 (defcustom gnus-summary-mark-below 0
589   "*Mark all articles with a score below this variable as read.
590 This variable is local to each summary buffer and usually set by the
591 score file."
592   :group 'gnus-score-default
593   :type 'integer)
594
595 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
596   "*List of functions used for sorting articles in the summary buffer.
597 This variable is only used when not using a threaded display."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-article-sort-by-number)
600                          (function-item gnus-article-sort-by-author)
601                          (function-item gnus-article-sort-by-subject)
602                          (function-item gnus-article-sort-by-date)
603                          (function-item gnus-article-sort-by-score)
604                          (function :tag "other"))))
605
606 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
607   "*List of functions used for sorting threads in the summary buffer.
608 By default, threads are sorted by article number.
609
610 Each function takes two threads and return non-nil if the first thread
611 should be sorted before the other.  If you use more than one function,
612 the primary sort function should be the last.  You should probably
613 always include `gnus-thread-sort-by-number' in the list of sorting
614 functions -- preferably first.
615
616 Ready-made functions include `gnus-thread-sort-by-number',
617 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
618 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
619 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
620   :group 'gnus-summary-sort
621   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
622                          (function-item gnus-thread-sort-by-author)
623                          (function-item gnus-thread-sort-by-subject)
624                          (function-item gnus-thread-sort-by-date)
625                          (function-item gnus-thread-sort-by-score)
626                          (function-item gnus-thread-sort-by-total-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-score-function '+
630   "*Function used for calculating the total score of a thread.
631
632 The function is called with the scores of the article and each
633 subthread and should then return the score of the thread.
634
635 Some functions you can use are `+', `max', or `min'."
636   :group 'gnus-summary-sort
637   :type 'function)
638
639 (defcustom gnus-summary-expunge-below nil
640   "All articles that have a score less than this variable will be expunged.
641 This variable is local to the summary buffers."
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-thread-expunge-below nil
647   "All threads that have a total score less than this variable will be expunged.
648 See `gnus-thread-score-function' for en explanation of what a
649 \"thread score\" is.
650
651 This variable is local to the summary buffers."
652   :group 'gnus-threading
653   :group 'gnus-score-default
654   :type '(choice (const :tag "off" nil)
655                  integer))
656
657 (defcustom gnus-summary-mode-hook nil
658   "*A hook for Gnus summary mode.
659 This hook is run before any variables are set in the summary buffer."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-menu-hook nil
664   "*Hook run after the creation of the summary mode menu."
665   :group 'gnus-summary-visual
666   :type 'hook)
667
668 (defcustom gnus-summary-exit-hook nil
669   "*A hook called on exit from the summary buffer.
670 It will be called with point in the group buffer."
671   :group 'gnus-summary-exit
672   :type 'hook)
673
674 (defcustom gnus-summary-prepare-hook nil
675   "*A hook called after the summary buffer has been generated.
676 If you want to modify the summary buffer, you can use this hook."
677   :group 'gnus-summary-various
678   :type 'hook)
679
680 (defcustom gnus-summary-prepared-hook nil
681   "*A hook called as the last thing after the summary buffer has been generated."
682   :group 'gnus-summary-various
683   :type 'hook)
684
685 (defcustom gnus-summary-generate-hook nil
686   "*A hook run just before generating the summary buffer.
687 This hook is commonly used to customize threading variables and the
688 like."
689   :group 'gnus-summary-various
690   :type 'hook)
691
692 (defcustom gnus-select-group-hook nil
693   "*A hook called when a newsgroup is selected.
694
695 If you'd like to simplify subjects like the
696 `gnus-summary-next-same-subject' command does, you can use the
697 following hook:
698
699  (setq gnus-select-group-hook
700       (list
701         (lambda ()
702           (mapcar (lambda (header)
703                      (mail-header-set-subject
704                       header
705                       (gnus-simplify-subject
706                        (mail-header-subject header) 're-only)))
707                   gnus-newsgroup-headers))))"
708   :group 'gnus-group-select
709   :type 'hook)
710
711 (defcustom gnus-select-article-hook nil
712   "*A hook called when an article is selected."
713   :group 'gnus-summary-choose
714   :type 'hook)
715
716 (defcustom gnus-visual-mark-article-hook
717   (list 'gnus-highlight-selected-summary)
718   "*Hook run after selecting an article in the summary buffer.
719 It is meant to be used for highlighting the article in some way.  It
720 is not run if `gnus-visual' is nil."
721   :group 'gnus-summary-visual
722   :type 'hook)
723
724 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
725   "*A hook called before parsing the headers."
726   :group 'gnus-various
727   :type 'hook)
728
729 (defcustom gnus-exit-group-hook nil
730   "*A hook called when exiting summary mode.
731 This hook is not called from the non-updating exit commands like `Q'."
732   :group 'gnus-various
733   :type 'hook)
734
735 (defcustom gnus-summary-update-hook
736   (list 'gnus-summary-highlight-line)
737   "*A hook called when a summary line is changed.
738 The hook will not be called if `gnus-visual' is nil.
739
740 The default function `gnus-summary-highlight-line' will
741 highlight the line according to the `gnus-summary-highlight'
742 variable."
743   :group 'gnus-summary-visual
744   :type 'hook)
745
746 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
747   "*A hook called when an article is selected for the first time.
748 The hook is intended to mark an article as read (or unread)
749 automatically when it is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-group-no-more-groups-hook nil
754   "*A hook run when returning to group mode having no more (unread) groups."
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-ps-print-hook nil
759   "*A hook run before ps-printing something from Gnus."
760   :group 'gnus-summary
761   :type 'hook)
762
763 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
764   "Face used for highlighting the current article in the summary buffer."
765   :group 'gnus-summary-visual
766   :type 'face)
767
768 (defcustom gnus-summary-highlight
769   '(((= mark gnus-canceled-mark)
770      . gnus-summary-cancelled-face)
771     ((and (> score default)
772           (or (= mark gnus-dormant-mark)
773               (= mark gnus-ticked-mark)))
774      . gnus-summary-high-ticked-face)
775     ((and (< score default)
776           (or (= mark gnus-dormant-mark)
777               (= mark gnus-ticked-mark)))
778      . gnus-summary-low-ticked-face)
779     ((or (= mark gnus-dormant-mark)
780          (= mark gnus-ticked-mark))
781      . gnus-summary-normal-ticked-face)
782     ((and (> score default) (= mark gnus-ancient-mark))
783      . gnus-summary-high-ancient-face)
784     ((and (< score default) (= mark gnus-ancient-mark))
785      . gnus-summary-low-ancient-face)
786     ((= mark gnus-ancient-mark)
787      . gnus-summary-normal-ancient-face)
788     ((and (> score default) (= mark gnus-unread-mark))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (= mark gnus-unread-mark))
791      . gnus-summary-low-unread-face)
792     ((and (memq article gnus-newsgroup-incorporated)
793           (= mark gnus-unread-mark))
794      . gnus-summary-incorporated-face)
795     ((= mark gnus-unread-mark)
796      . gnus-summary-normal-unread-face)
797     ((and (> score default) (memq mark (list gnus-downloadable-mark
798                                              gnus-undownloaded-mark)))
799      . gnus-summary-high-unread-face)
800     ((and (< score default) (memq mark (list gnus-downloadable-mark
801                                              gnus-undownloaded-mark)))
802      . gnus-summary-low-unread-face)
803     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
804      . gnus-summary-normal-unread-face)
805     ((> score default)
806      . gnus-summary-high-read-face)
807     ((< score default)
808      . gnus-summary-low-read-face)
809     (t
810      . gnus-summary-normal-read-face))
811   "*Controls the highlighting of summary buffer lines.
812
813 A list of (FORM . FACE) pairs.  When deciding how a a particular
814 summary line should be displayed, each form is evaluated.  The content
815 of the face field after the first true form is used.  You can change
816 how those summary lines are displayed, by editing the face field.
817
818 You can use the following variables in the FORM field.
819
820 score:   The articles score
821 default: The default article score.
822 below:   The score below which articles are automatically marked as read.
823 mark:    The articles mark."
824   :group 'gnus-summary-visual
825   :type '(repeat (cons (sexp :tag "Form" nil)
826                        face)))
827
828 (defcustom gnus-alter-header-function nil
829   "Function called to allow alteration of article header structures.
830 The function is called with one parameter, the article header vector,
831 which it may alter in any way.")
832
833 (defvar gnus-decode-encoded-word-function
834   (mime-find-field-decoder 'From 'nov)
835   "Variable that says which function should be used to decode a string with encoded words.")
836
837 (defcustom gnus-extra-headers nil
838   "*Extra headers to parse."
839   :group 'gnus-summary
840   :type '(repeat symbol))
841
842 (defcustom gnus-ignored-from-addresses
843   (and user-mail-address (regexp-quote user-mail-address))
844   "*Regexp of From headers that may be suppressed in favor of To headers."
845   :group 'gnus-summary
846   :type 'regexp)
847
848 (defcustom gnus-group-charset-alist
849   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
850     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
851     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
852     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
853     ("^relcom\\>" koi8-r)
854     ("^fido7\\>" koi8-r)
855     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
856     ("^israel\\>" iso-8859-1)
857     ("^han\\>" euc-kr)
858     ("^alt.chinese.text.big5\\>" chinese-big5)
859     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
860     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
861     (".*" iso-8859-1))
862   "Alist of regexps (to match group names) and default charsets to be used when reading."
863   :type '(repeat (list (regexp :tag "Group")
864                        (symbol :tag "Charset")))
865   :group 'gnus-charset)
866
867 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
868   "List of charsets that should be ignored.
869 When these charsets are used in the \"charset\" parameter, the
870 default charset will be used instead."
871   :type '(repeat symbol)
872   :group 'gnus-charset)
873
874 (defcustom gnus-group-ignored-charsets-alist
875   '(("alt\\.chinese\\.text" iso-8859-1))
876   "Alist of regexps (to match group names) and charsets that should be ignored.
877 When these charsets are used in the \"charset\" parameter, the
878 default charset will be used instead."
879   :type '(repeat (cons (regexp :tag "Group")
880                        (repeat symbol)))
881   :group 'gnus-charset)
882
883 (defcustom gnus-group-highlight-words-alist nil
884   "Alist of group regexps and highlight regexps.
885 This variable uses the same syntax as `gnus-emphasis-alist'."
886   :type '(repeat (cons (regexp :tag "Group")
887                        (repeat (list (regexp :tag "Highlight regexp")
888                                      (number :tag "Group for entire word" 0)
889                                      (number :tag "Group for displayed part" 0)
890                                      (symbol :tag "Face"
891                                              gnus-emphasis-highlight-words)))))
892   :group 'gnus-summary-visual)
893
894 (defcustom gnus-use-wheel nil
895   "Use Intelli-mouse on summary movement"
896   :type 'boolean
897   :group 'gnus-summary-maneuvering)
898
899 (defcustom gnus-wheel-scroll-amount '(5 . 1)
900   "Amount to scroll messages by spinning the mouse wheel.
901 This is actually a cons cell, where the first item is the amount to scroll
902 on a normal wheel event, and the second is the amount to scroll when the
903 wheel is moved with the shift key depressed."
904   :type '(cons (integer :tag "Shift") integer)
905   :group 'gnus-summary-maneuvering)
906
907 (defcustom gnus-wheel-edge-resistance 2
908   "How hard it should be to change the current article
909 by moving the mouse over the edge of the article window."
910   :type 'integer
911   :group 'gnus-summary-maneuvering)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :type '(repeat (cons (number :tag "Argument" 1)
920                        (symbol :tag "Charset")))
921   :group 'gnus-charset)
922
923 (defcustom gnus-preserve-marks t
924   "Whether marks are preserved when moving, copying and respooling messages."
925   :type 'boolean
926   :group 'gnus-summary-marks)
927
928 (defcustom gnus-alter-articles-to-read-function nil
929   "Function to be called to alter the list of articles to be selected."
930   :type 'function
931   :group 'gnus-summary)
932
933 (defcustom gnus-orphan-score nil
934   "*All orphans get this score added.  Set in the score file."
935   :group 'gnus-score-default
936   :type '(choice (const nil)
937                  integer))
938
939 (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     (unwind-protect
3329         (progn
3330           (narrow-to-region (point) eol)
3331           (unless (eobp)
3332             (forward-char))
3333
3334           (setq header
3335                 (make-full-mail-header
3336                  number                         ; number
3337                  (nnheader-nov-field)           ; subject
3338                  (nnheader-nov-field)           ; from
3339                  (nnheader-nov-field)           ; date
3340                  (nnheader-nov-read-message-id) ; id
3341                  (nnheader-nov-field)           ; refs
3342                  (nnheader-nov-read-integer)    ; chars
3343                  (nnheader-nov-read-integer)    ; lines
3344                  (unless (eobp)
3345                    (if (looking-at "Xref: ")
3346                        (goto-char (match-end 0)))
3347                    (nnheader-nov-field))        ; Xref
3348                  (nnheader-nov-parse-extra))))  ; extra
3349
3350       (widen))
3351
3352     (when gnus-alter-header-function
3353       (funcall gnus-alter-header-function header))
3354     (gnus-dependencies-add-header header dependencies force-new)))
3355
3356 (defun gnus-build-get-header (id)
3357   "Look through the buffer of NOV lines and find the header to ID.
3358 Enter this line into the dependencies hash table, and return
3359 the id of the parent article (if any)."
3360   (let ((deps gnus-newsgroup-dependencies)
3361         found header)
3362     (prog1
3363         (save-excursion
3364           (set-buffer nntp-server-buffer)
3365           (let ((case-fold-search nil))
3366             (goto-char (point-min))
3367             (while (and (not found)
3368                         (search-forward id nil t))
3369               (beginning-of-line)
3370               (setq found (looking-at
3371                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3372                                    (regexp-quote id))))
3373               (or found (beginning-of-line 2)))
3374             (when found
3375               (beginning-of-line)
3376               (and
3377                (setq header (gnus-nov-parse-line
3378                              (read (current-buffer)) deps))
3379                (gnus-parent-id (mail-header-references header))))))
3380       (when header
3381         (let ((number (mail-header-number header)))
3382           (push number gnus-newsgroup-limit)
3383           (push header gnus-newsgroup-headers)
3384           (if (memq number gnus-newsgroup-unselected)
3385               (progn
3386                 (push number gnus-newsgroup-unreads)
3387                 (setq gnus-newsgroup-unselected
3388                       (delq number gnus-newsgroup-unselected)))
3389             (push number gnus-newsgroup-ancient)))))))
3390
3391 (defun gnus-build-all-threads ()
3392   "Read all the headers."
3393   (let ((gnus-summary-ignore-duplicates t)
3394         (mail-parse-charset gnus-newsgroup-charset)
3395         (dependencies gnus-newsgroup-dependencies)
3396         header article)
3397     (save-excursion
3398       (set-buffer nntp-server-buffer)
3399       (let ((case-fold-search nil))
3400         (goto-char (point-min))
3401         (while (not (eobp))
3402           (ignore-errors
3403             (setq article (read (current-buffer))
3404                   header (gnus-nov-parse-line article dependencies)))
3405           (when header
3406             (save-excursion
3407               (set-buffer gnus-summary-buffer)
3408               (push header gnus-newsgroup-headers)
3409               (if (memq (setq article (mail-header-number header))
3410                         gnus-newsgroup-unselected)
3411                   (progn
3412                     (push article gnus-newsgroup-unreads)
3413                     (setq gnus-newsgroup-unselected
3414                           (delq article gnus-newsgroup-unselected)))
3415                 (push article gnus-newsgroup-ancient)))
3416             (forward-line 1)))))))
3417
3418 (defun gnus-summary-update-article-line (article header)
3419   "Update the line for ARTICLE using HEADERS."
3420   (let* ((id (mail-header-id header))
3421          (thread (gnus-id-to-thread id)))
3422     (unless thread
3423       (error "Article in no thread"))
3424     ;; Update the thread.
3425     (setcar thread header)
3426     (gnus-summary-goto-subject article)
3427     (let* ((datal (gnus-data-find-list article))
3428            (data (car datal))
3429            (length (when (cdr datal)
3430                      (- (gnus-data-pos data)
3431                         (gnus-data-pos (cadr datal)))))
3432            (buffer-read-only nil)
3433            (level (gnus-summary-thread-level)))
3434       (gnus-delete-line)
3435       (gnus-summary-insert-line
3436        header level nil (gnus-article-mark article)
3437        (memq article gnus-newsgroup-replied)
3438        (memq article gnus-newsgroup-expirable)
3439        ;; Only insert the Subject string when it's different
3440        ;; from the previous Subject string.
3441        (if (and
3442             gnus-show-threads
3443             (gnus-subject-equal
3444              (condition-case ()
3445                  (mail-header-subject
3446                   (gnus-data-header
3447                    (cadr
3448                     (gnus-data-find-list
3449                      article
3450                      (gnus-data-list t)))))
3451                ;; Error on the side of excessive subjects.
3452                (error ""))
3453              (mail-header-subject header)))
3454            ""
3455          (mail-header-subject header))
3456        nil (cdr (assq article gnus-newsgroup-scored))
3457        (memq article gnus-newsgroup-processable))
3458       (when length
3459         (gnus-data-update-list
3460          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3461
3462 (defun gnus-summary-update-article (article &optional iheader)
3463   "Update ARTICLE in the summary buffer."
3464   (set-buffer gnus-summary-buffer)
3465   (let* ((header (gnus-summary-article-header article))
3466          (id (mail-header-id header))
3467          (data (gnus-data-find article))
3468          (thread (gnus-id-to-thread id))
3469          (references (mail-header-references header))
3470          (parent
3471           (gnus-id-to-thread
3472            (or (gnus-parent-id
3473                 (when (and references
3474                            (not (equal "" references)))
3475                   references))
3476                "none")))
3477          (buffer-read-only nil)
3478          (old (car thread)))
3479     (when thread
3480       (unless iheader
3481         (setcar thread nil)
3482         (when parent
3483           (delq thread parent)))
3484       (if (gnus-summary-insert-subject id header)
3485           ;; Set the (possibly) new article number in the data structure.
3486           (gnus-data-set-number data (gnus-id-to-article id))
3487         (setcar thread old)
3488         nil))))
3489
3490 (defun gnus-rebuild-thread (id &optional line)
3491   "Rebuild the thread containing ID.
3492 If LINE, insert the rebuilt thread starting on line LINE."
3493   (let ((buffer-read-only nil)
3494         old-pos current thread data)
3495     (if (not gnus-show-threads)
3496         (setq thread (list (car (gnus-id-to-thread id))))
3497       ;; Get the thread this article is part of.
3498       (setq thread (gnus-remove-thread id)))
3499     (setq old-pos (gnus-point-at-bol))
3500     (setq current (save-excursion
3501                     (and (zerop (forward-line -1))
3502                          (gnus-summary-article-number))))
3503     ;; If this is a gathered thread, we have to go some re-gathering.
3504     (when (stringp (car thread))
3505       (let ((subject (car thread))
3506             roots thr)
3507         (setq thread (cdr thread))
3508         (while thread
3509           (unless (memq (setq thr (gnus-id-to-thread
3510                                    (gnus-root-id
3511                                     (mail-header-id (caar thread)))))
3512                         roots)
3513             (push thr roots))
3514           (setq thread (cdr thread)))
3515         ;; We now have all (unique) roots.
3516         (if (= (length roots) 1)
3517             ;; All the loose roots are now one solid root.
3518             (setq thread (car roots))
3519           (setq thread (cons subject (gnus-sort-threads roots))))))
3520     (let (threads)
3521       ;; We then insert this thread into the summary buffer.
3522       (when line
3523         (goto-char (point-min))
3524         (forward-line (1- line)))
3525       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3526         (if gnus-show-threads
3527             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3528           (gnus-summary-prepare-unthreaded thread))
3529         (setq data (nreverse gnus-newsgroup-data))
3530         (setq threads gnus-newsgroup-threads))
3531       ;; We splice the new data into the data structure.
3532       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3533       ;;!!! then we want to insert at the beginning of the buffer.
3534       ;;!!! That happens to be true with Gnus now, but that may
3535       ;;!!! change in the future.  Perhaps.
3536       (gnus-data-enter-list
3537        (if line nil current) data (- (point) old-pos))
3538       (setq gnus-newsgroup-threads
3539             (nconc threads gnus-newsgroup-threads))
3540       (gnus-data-compute-positions))))
3541
3542 (defun gnus-number-to-header (number)
3543   "Return the header for article NUMBER."
3544   (let ((headers gnus-newsgroup-headers))
3545     (while (and headers
3546                 (not (= number (mail-header-number (car headers)))))
3547       (pop headers))
3548     (when headers
3549       (car headers))))
3550
3551 (defun gnus-parent-headers (in-headers &optional generation)
3552   "Return the headers of the GENERATIONeth parent of HEADERS."
3553   (unless generation
3554     (setq generation 1))
3555   (let ((parent t)
3556         (headers in-headers)
3557         references)
3558     (while (and parent
3559                 (not (zerop generation))
3560                 (setq references (mail-header-references headers)))
3561       (setq headers (if (and references
3562                              (setq parent (gnus-parent-id references)))
3563                         (car (gnus-id-to-thread parent))
3564                       nil))
3565       (decf generation))
3566     (and (not (eq headers in-headers))
3567          headers)))
3568
3569 (defun gnus-id-to-thread (id)
3570   "Return the (sub-)thread where ID appears."
3571   (gnus-gethash id gnus-newsgroup-dependencies))
3572
3573 (defun gnus-id-to-article (id)
3574   "Return the article number of ID."
3575   (let ((thread (gnus-id-to-thread id)))
3576     (when (and thread
3577                (car thread))
3578       (mail-header-number (car thread)))))
3579
3580 (defun gnus-id-to-header (id)
3581   "Return the article headers of ID."
3582   (car (gnus-id-to-thread id)))
3583
3584 (defun gnus-article-displayed-root-p (article)
3585   "Say whether ARTICLE is a root(ish) article."
3586   (let ((level (gnus-summary-thread-level article))
3587         (refs (mail-header-references  (gnus-summary-article-header article)))
3588         particle)
3589     (cond
3590      ((null level) nil)
3591      ((zerop level) t)
3592      ((null refs) t)
3593      ((null (gnus-parent-id refs)) t)
3594      ((and (= 1 level)
3595            (null (setq particle (gnus-id-to-article
3596                                  (gnus-parent-id refs))))
3597            (null (gnus-summary-thread-level particle)))))))
3598
3599 (defun gnus-root-id (id)
3600   "Return the id of the root of the thread where ID appears."
3601   (let (last-id prev)
3602     (while (and id (setq prev (car (gnus-id-to-thread id))))
3603       (setq last-id id
3604             id (gnus-parent-id (mail-header-references prev))))
3605     last-id))
3606
3607 (defun gnus-articles-in-thread (thread)
3608   "Return the list of articles in THREAD."
3609   (cons (mail-header-number (car thread))
3610         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3611
3612 (defun gnus-remove-thread (id &optional dont-remove)
3613   "Remove the thread that has ID in it."
3614   (let (headers thread last-id)
3615     ;; First go up in this thread until we find the root.
3616     (setq last-id (gnus-root-id id)
3617           headers (message-flatten-list (gnus-id-to-thread last-id)))
3618     ;; We have now found the real root of this thread.  It might have
3619     ;; been gathered into some loose thread, so we have to search
3620     ;; through the threads to find the thread we wanted.
3621     (let ((threads gnus-newsgroup-threads)
3622           sub)
3623       (while threads
3624         (setq sub (car threads))
3625         (if (stringp (car sub))
3626             ;; This is a gathered thread, so we look at the roots
3627             ;; below it to find whether this article is in this
3628             ;; gathered root.
3629             (progn
3630               (setq sub (cdr sub))
3631               (while sub
3632                 (when (member (caar sub) headers)
3633                   (setq thread (car threads)
3634                         threads nil
3635                         sub nil))
3636                 (setq sub (cdr sub))))
3637           ;; It's an ordinary thread, so we check it.
3638           (when (eq (car sub) (car headers))
3639             (setq thread sub
3640                   threads nil)))
3641         (setq threads (cdr threads)))
3642       ;; If this article is in no thread, then it's a root.
3643       (if thread
3644           (unless dont-remove
3645             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3646         (setq thread (gnus-id-to-thread last-id)))
3647       (when thread
3648         (prog1
3649             thread                      ; We return this thread.
3650           (unless dont-remove
3651             (if (stringp (car thread))
3652                 (progn
3653                   ;; If we use dummy roots, then we have to remove the
3654                   ;; dummy root as well.
3655                   (when (eq gnus-summary-make-false-root 'dummy)
3656                     ;; We go to the dummy root by going to
3657                     ;; the first sub-"thread", and then one line up.
3658                     (gnus-summary-goto-article
3659                      (mail-header-number (caadr thread)))
3660                     (forward-line -1)
3661                     (gnus-delete-line)
3662                     (gnus-data-compute-positions))
3663                   (setq thread (cdr thread))
3664                   (while thread
3665                     (gnus-remove-thread-1 (car thread))
3666                     (setq thread (cdr thread))))
3667               (gnus-remove-thread-1 thread))))))))
3668
3669 (defun gnus-remove-thread-1 (thread)
3670   "Remove the thread THREAD recursively."
3671   (let ((number (mail-header-number (pop thread)))
3672         d)
3673     (setq thread (reverse thread))
3674     (while thread
3675       (gnus-remove-thread-1 (pop thread)))
3676     (when (setq d (gnus-data-find number))
3677       (goto-char (gnus-data-pos d))
3678       (gnus-summary-show-thread)
3679       (gnus-data-remove
3680        number
3681        (- (gnus-point-at-bol)
3682           (prog1
3683               (1+ (gnus-point-at-eol))
3684             (gnus-delete-line)))))))
3685
3686 (defun gnus-sort-threads-1 (threads func)
3687   (sort (mapcar (lambda (thread)
3688                   (cons (car thread)
3689                         (and (cdr thread)
3690                              (gnus-sort-threads-1 (cdr thread) func))))
3691                 threads) func))
3692
3693 (defun gnus-sort-threads (threads)
3694   "Sort THREADS."
3695   (if (not gnus-thread-sort-functions)
3696       threads
3697     (gnus-message 8 "Sorting threads...")
3698     (prog1
3699         (gnus-sort-threads-1 
3700          threads 
3701          (gnus-make-sort-function gnus-thread-sort-functions))
3702       (gnus-message 8 "Sorting threads...done"))))
3703
3704 (defun gnus-sort-articles (articles)
3705   "Sort ARTICLES."
3706   (when gnus-article-sort-functions
3707     (gnus-message 7 "Sorting articles...")
3708     (prog1
3709         (setq gnus-newsgroup-headers
3710               (sort articles (gnus-make-sort-function
3711                               gnus-article-sort-functions)))
3712       (gnus-message 7 "Sorting articles...done"))))
3713
3714 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3715 (defmacro gnus-thread-header (thread)
3716   "Return header of first article in THREAD.
3717 Note that THREAD must never, ever be anything else than a variable -
3718 using some other form will lead to serious barfage."
3719   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3720   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3721   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3722         (vector thread) 2))
3723
3724 (defsubst gnus-article-sort-by-number (h1 h2)
3725   "Sort articles by article number."
3726   (< (mail-header-number h1)
3727      (mail-header-number h2)))
3728
3729 (defun gnus-thread-sort-by-number (h1 h2)
3730   "Sort threads by root article number."
3731   (gnus-article-sort-by-number
3732    (gnus-thread-header h1) (gnus-thread-header h2)))
3733
3734 (defsubst gnus-article-sort-by-lines (h1 h2)
3735   "Sort articles by article Lines header."
3736   (< (mail-header-lines h1)
3737      (mail-header-lines h2)))
3738
3739 (defun gnus-thread-sort-by-lines (h1 h2)
3740   "Sort threads by root article Lines header."
3741   (gnus-article-sort-by-lines
3742    (gnus-thread-header h1) (gnus-thread-header h2)))
3743
3744 (defsubst gnus-article-sort-by-chars (h1 h2)
3745   "Sort articles by octet length."
3746   (< (mail-header-chars h1)
3747      (mail-header-chars h2)))
3748
3749 (defun gnus-thread-sort-by-chars (h1 h2)
3750   "Sort threads by root article octet length."
3751   (gnus-article-sort-by-chars
3752    (gnus-thread-header h1) (gnus-thread-header h2)))
3753
3754 (defsubst gnus-article-sort-by-author (h1 h2)
3755   "Sort articles by root author."
3756   (string-lessp
3757    (let ((addr (car (mime-entity-read-field h1 'From))))
3758      (or (std11-full-name-string addr)
3759          (std11-address-string addr)
3760          ""))
3761    (let ((addr (car (mime-entity-read-field h2 'From))))
3762      (or (std11-full-name-string addr)
3763          (std11-address-string addr)
3764          ""))
3765    ))
3766
3767 (defun gnus-thread-sort-by-author (h1 h2)
3768   "Sort threads by root author."
3769   (gnus-article-sort-by-author
3770    (gnus-thread-header h1)  (gnus-thread-header h2)))
3771
3772 (defsubst gnus-article-sort-by-subject (h1 h2)
3773   "Sort articles by root subject."
3774   (string-lessp
3775    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3777
3778 (defun gnus-thread-sort-by-subject (h1 h2)
3779   "Sort threads by root subject."
3780   (gnus-article-sort-by-subject
3781    (gnus-thread-header h1) (gnus-thread-header h2)))
3782
3783 (defsubst gnus-article-sort-by-date (h1 h2)
3784   "Sort articles by root article date."
3785   (time-less-p
3786    (gnus-date-get-time (mail-header-date h1))
3787    (gnus-date-get-time (mail-header-date h2))))
3788
3789 (defun gnus-thread-sort-by-date (h1 h2)
3790   "Sort threads by root article date."
3791   (gnus-article-sort-by-date
3792    (gnus-thread-header h1) (gnus-thread-header h2)))
3793
3794 (defsubst gnus-article-sort-by-score (h1 h2)
3795   "Sort articles by root article score.
3796 Unscored articles will be counted as having a score of zero."
3797   (> (or (cdr (assq (mail-header-number h1)
3798                     gnus-newsgroup-scored))
3799          gnus-summary-default-score 0)
3800      (or (cdr (assq (mail-header-number h2)
3801                     gnus-newsgroup-scored))
3802          gnus-summary-default-score 0)))
3803
3804 (defun gnus-thread-sort-by-score (h1 h2)
3805   "Sort threads by root article score."
3806   (gnus-article-sort-by-score
3807    (gnus-thread-header h1) (gnus-thread-header h2)))
3808
3809 (defun gnus-thread-sort-by-total-score (h1 h2)
3810   "Sort threads by the sum of all scores in the thread.
3811 Unscored articles will be counted as having a score of zero."
3812   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3813
3814 (defun gnus-thread-total-score (thread)
3815   ;; This function find the total score of THREAD.
3816   (cond ((null thread)
3817          0)
3818         ((consp thread)
3819          (if (stringp (car thread))
3820              (apply gnus-thread-score-function 0
3821                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3822            (gnus-thread-total-score-1 thread)))
3823         (t
3824          (gnus-thread-total-score-1 (list thread)))))
3825
3826 (defun gnus-thread-total-score-1 (root)
3827   ;; This function find the total score of the thread below ROOT.
3828   (setq root (car root))
3829   (apply gnus-thread-score-function
3830          (or (append
3831               (mapcar 'gnus-thread-total-score
3832                       (cdr (gnus-id-to-thread (mail-header-id root))))
3833               (when (> (mail-header-number root) 0)
3834                 (list (or (cdr (assq (mail-header-number root)
3835                                      gnus-newsgroup-scored))
3836                           gnus-summary-default-score 0))))
3837              (list gnus-summary-default-score)
3838              '(0))))
3839
3840 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3841 (defvar gnus-tmp-prev-subject nil)
3842 (defvar gnus-tmp-false-parent nil)
3843 (defvar gnus-tmp-root-expunged nil)
3844 (defvar gnus-tmp-dummy-line nil)
3845
3846 (defvar gnus-tmp-header)
3847 (defun gnus-extra-header (type &optional header)
3848   "Return the extra header of TYPE."
3849   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3850       ""))
3851
3852 (defun gnus-summary-prepare-threads (threads)
3853   "Prepare summary buffer from THREADS and indentation LEVEL.
3854 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3855 or a straight list of headers."
3856   (gnus-message 7 "Generating summary...")
3857
3858   (setq gnus-newsgroup-threads threads)
3859   (beginning-of-line)
3860
3861   (let ((gnus-tmp-level 0)
3862         (default-score (or gnus-summary-default-score 0))
3863         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3864         thread number subject stack state gnus-tmp-gathered beg-match
3865         new-roots gnus-tmp-new-adopts thread-end
3866         gnus-tmp-header gnus-tmp-unread
3867         gnus-tmp-replied gnus-tmp-subject-or-nil
3868         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3869         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3870         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3871
3872     (setq gnus-tmp-prev-subject nil)
3873
3874     (if (vectorp (car threads))
3875         ;; If this is a straight (sic) list of headers, then a
3876         ;; threaded summary display isn't required, so we just create
3877         ;; an unthreaded one.
3878         (gnus-summary-prepare-unthreaded threads)
3879
3880       ;; Do the threaded display.
3881
3882       (while (or threads stack gnus-tmp-new-adopts new-roots)
3883
3884         (if (and (= gnus-tmp-level 0)
3885                  (or (not stack)
3886                      (= (caar stack) 0))
3887                  (not gnus-tmp-false-parent)
3888                  (or gnus-tmp-new-adopts new-roots))
3889             (if gnus-tmp-new-adopts
3890                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3891                       thread (list (car gnus-tmp-new-adopts))
3892                       gnus-tmp-header (caar thread)
3893                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3894               (when new-roots
3895                 (setq thread (list (car new-roots))
3896                       gnus-tmp-header (caar thread)
3897                       new-roots (cdr new-roots))))
3898
3899           (if threads
3900               ;; If there are some threads, we do them before the
3901               ;; threads on the stack.
3902               (setq thread threads
3903                     gnus-tmp-header (caar thread))
3904             ;; There were no current threads, so we pop something off
3905             ;; the stack.
3906             (setq state (car stack)
3907                   gnus-tmp-level (car state)
3908                   thread (cdr state)
3909                   stack (cdr stack)
3910                   gnus-tmp-header (caar thread))))
3911
3912         (setq gnus-tmp-false-parent nil)
3913         (setq gnus-tmp-root-expunged nil)
3914         (setq thread-end nil)
3915
3916         (if (stringp gnus-tmp-header)
3917             ;; The header is a dummy root.
3918             (cond
3919              ((eq gnus-summary-make-false-root 'adopt)
3920               ;; We let the first article adopt the rest.
3921               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3922                                                (cddar thread)))
3923               (setq gnus-tmp-gathered
3924                     (nconc (mapcar
3925                             (lambda (h) (mail-header-number (car h)))
3926                             (cddar thread))
3927                            gnus-tmp-gathered))
3928               (setq thread (cons (list (caar thread)
3929                                        (cadar thread))
3930                                  (cdr thread)))
3931               (setq gnus-tmp-level -1
3932                     gnus-tmp-false-parent t))
3933              ((eq gnus-summary-make-false-root 'empty)
3934               ;; We print adopted articles with empty subject fields.
3935               (setq gnus-tmp-gathered
3936                     (nconc (mapcar
3937                             (lambda (h) (mail-header-number (car h)))
3938                             (cddar thread))
3939                            gnus-tmp-gathered))
3940               (setq gnus-tmp-level -1))
3941              ((eq gnus-summary-make-false-root 'dummy)
3942               ;; We remember that we probably want to output a dummy
3943               ;; root.
3944               (setq gnus-tmp-dummy-line gnus-tmp-header)
3945               (setq gnus-tmp-prev-subject gnus-tmp-header))
3946              (t
3947               ;; We do not make a root for the gathered
3948               ;; sub-threads at all.
3949               (setq gnus-tmp-level -1)))
3950
3951           (setq number (mail-header-number gnus-tmp-header)
3952                 subject (mail-header-subject gnus-tmp-header))
3953
3954           (cond
3955            ;; If the thread has changed subject, we might want to make
3956            ;; this subthread into a root.
3957            ((and (null gnus-thread-ignore-subject)
3958                  (not (zerop gnus-tmp-level))
3959                  gnus-tmp-prev-subject
3960                  (not (inline
3961                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3962             (setq new-roots (nconc new-roots (list (car thread)))
3963                   thread-end t
3964                   gnus-tmp-header nil))
3965            ;; If the article lies outside the current limit,
3966            ;; then we do not display it.
3967            ((not (memq number gnus-newsgroup-limit))
3968             (setq gnus-tmp-gathered
3969                   (nconc (mapcar
3970                           (lambda (h) (mail-header-number (car h)))
3971                           (cdar thread))
3972                          gnus-tmp-gathered))
3973             (setq gnus-tmp-new-adopts (if (cdar thread)
3974                                           (append gnus-tmp-new-adopts
3975                                                   (cdar thread))
3976                                         gnus-tmp-new-adopts)
3977                   thread-end t
3978                   gnus-tmp-header nil)
3979             (when (zerop gnus-tmp-level)
3980               (setq gnus-tmp-root-expunged t)))
3981            ;; Perhaps this article is to be marked as read?
3982            ((and gnus-summary-mark-below
3983                  (< (or (cdr (assq number gnus-newsgroup-scored))
3984                         default-score)
3985                     gnus-summary-mark-below)
3986                  ;; Don't touch sparse articles.
3987                  (not (gnus-summary-article-sparse-p number))
3988                  (not (gnus-summary-article-ancient-p number)))
3989             (setq gnus-newsgroup-unreads
3990                   (delq number gnus-newsgroup-unreads))
3991             (if gnus-newsgroup-auto-expire
3992                 (push number gnus-newsgroup-expirable)
3993               (push (cons number gnus-low-score-mark)
3994                     gnus-newsgroup-reads))))
3995
3996           (when gnus-tmp-header
3997             ;; We may have an old dummy line to output before this
3998             ;; article.
3999             (when (and gnus-tmp-dummy-line
4000                        (gnus-subject-equal
4001                         gnus-tmp-dummy-line
4002                         (mail-header-subject gnus-tmp-header)))
4003               (gnus-summary-insert-dummy-line
4004                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4005               (setq gnus-tmp-dummy-line nil))
4006
4007             ;; Compute the mark.
4008             (setq gnus-tmp-unread (gnus-article-mark number))
4009
4010             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4011                                   gnus-tmp-header gnus-tmp-level)
4012                   gnus-newsgroup-data)
4013
4014             ;; Actually insert the line.
4015             (setq
4016              gnus-tmp-subject-or-nil
4017              (cond
4018               ((and gnus-thread-ignore-subject
4019                     gnus-tmp-prev-subject
4020                     (not (inline (gnus-subject-equal
4021                                   gnus-tmp-prev-subject subject))))
4022                subject)
4023               ((zerop gnus-tmp-level)
4024                (if (and (eq gnus-summary-make-false-root 'empty)
4025                         (memq number gnus-tmp-gathered)
4026                         gnus-tmp-prev-subject
4027                         (inline (gnus-subject-equal
4028                                  gnus-tmp-prev-subject subject)))
4029                    gnus-summary-same-subject
4030                  subject))
4031               (t gnus-summary-same-subject)))
4032             (if (and (eq gnus-summary-make-false-root 'adopt)
4033                      (= gnus-tmp-level 1)
4034                      (memq number gnus-tmp-gathered))
4035                 (setq gnus-tmp-opening-bracket ?\<
4036                       gnus-tmp-closing-bracket ?\>)
4037               (setq gnus-tmp-opening-bracket ?\[
4038                     gnus-tmp-closing-bracket ?\]))
4039             (setq
4040              gnus-tmp-indentation
4041              (aref gnus-thread-indent-array gnus-tmp-level)
4042              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4043              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4044                                 gnus-summary-default-score 0)
4045              gnus-tmp-score-char
4046              (if (or (null gnus-summary-default-score)
4047                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4048                          gnus-summary-zcore-fuzz))
4049                  ?  ;Whitespace
4050                (if (< gnus-tmp-score gnus-summary-default-score)
4051                    gnus-score-below-mark gnus-score-over-mark))
4052              gnus-tmp-replied
4053              (cond ((memq number gnus-newsgroup-processable)
4054                     gnus-process-mark)
4055                    ((memq number gnus-newsgroup-cached)
4056                     gnus-cached-mark)
4057                    ((memq number gnus-newsgroup-replied)
4058                     gnus-replied-mark)
4059                    ((memq number gnus-newsgroup-saved)
4060                     gnus-saved-mark)
4061                    (t gnus-unread-mark))
4062              gnus-tmp-from (mail-header-from gnus-tmp-header)
4063              gnus-tmp-name
4064              (cond
4065               ((string-match "<[^>]+> *$" gnus-tmp-from)
4066                (setq beg-match (match-beginning 0))
4067                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4068                         (substring gnus-tmp-from (1+ (match-beginning 0))
4069                                    (1- (match-end 0))))
4070                    (substring gnus-tmp-from 0 beg-match)))
4071               ((string-match "(.+)" gnus-tmp-from)
4072                (substring gnus-tmp-from
4073                           (1+ (match-beginning 0)) (1- (match-end 0))))
4074               (t gnus-tmp-from)))
4075             (when (string= gnus-tmp-name "")
4076               (setq gnus-tmp-name gnus-tmp-from))
4077             (unless (numberp gnus-tmp-lines)
4078               (setq gnus-tmp-lines 0))
4079             (gnus-put-text-property
4080              (point)
4081              (progn (eval gnus-summary-line-format-spec) (point))
4082              'gnus-number number)
4083             (when gnus-visual-p
4084               (forward-line -1)
4085               (gnus-run-hooks 'gnus-summary-update-hook)
4086               (forward-line 1))
4087
4088             (setq gnus-tmp-prev-subject subject)))
4089
4090         (when (nth 1 thread)
4091           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4092         (incf gnus-tmp-level)
4093         (setq threads (if thread-end nil (cdar thread)))
4094         (unless threads
4095           (setq gnus-tmp-level 0)))))
4096   (gnus-message 7 "Generating summary...done"))
4097
4098 (defun gnus-summary-prepare-unthreaded (headers)
4099   "Generate an unthreaded summary buffer based on HEADERS."
4100   (let (header number mark)
4101
4102     (beginning-of-line)
4103
4104     (while headers
4105       ;; We may have to root out some bad articles...
4106       (when (memq (setq number (mail-header-number
4107                                 (setq header (pop headers))))
4108                   gnus-newsgroup-limit)
4109         ;; Mark article as read when it has a low score.
4110         (when (and gnus-summary-mark-below
4111                    (< (or (cdr (assq number gnus-newsgroup-scored))
4112                           gnus-summary-default-score 0)
4113                       gnus-summary-mark-below)
4114                    (not (gnus-summary-article-ancient-p number)))
4115           (setq gnus-newsgroup-unreads
4116                 (delq number gnus-newsgroup-unreads))
4117           (if gnus-newsgroup-auto-expire
4118               (push number gnus-newsgroup-expirable)
4119             (push (cons number gnus-low-score-mark)
4120                   gnus-newsgroup-reads)))
4121
4122         (setq mark (gnus-article-mark number))
4123         (push (gnus-data-make number mark (1+ (point)) header 0)
4124               gnus-newsgroup-data)
4125         (gnus-summary-insert-line
4126          header 0 number
4127          mark (memq number gnus-newsgroup-replied)
4128          (memq number gnus-newsgroup-expirable)
4129          (mail-header-subject header) nil
4130          (cdr (assq number gnus-newsgroup-scored))
4131          (memq number gnus-newsgroup-processable))))))
4132
4133 (defun gnus-summary-remove-list-identifiers ()
4134   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4135   (let ((regexp (if (stringp gnus-list-identifiers)
4136                     gnus-list-identifiers
4137                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4138     (dolist (header gnus-newsgroup-headers)
4139       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4140                                   " *\\)\\)+\\(Re: +\\)?\\)")
4141                           (mail-header-subject header))
4142         (mail-header-set-subject
4143          header (concat (substring (mail-header-subject header)
4144                                    0 (match-beginning 1))
4145                         (or
4146                          (match-string 3 (mail-header-subject header))
4147                          (match-string 5 (mail-header-subject header)))
4148                         (substring (mail-header-subject header)
4149                                    (match-end 1))))))))
4150
4151 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4152   "Select newsgroup GROUP.
4153 If READ-ALL is non-nil, all articles in the group are selected.
4154 If SELECT-ARTICLES, only select those articles from GROUP."
4155   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4156          ;;!!! Dirty hack; should be removed.
4157          (gnus-summary-ignore-duplicates
4158           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4159               t
4160             gnus-summary-ignore-duplicates))
4161          (info (nth 2 entry))
4162          articles fetched-articles cached)
4163
4164     (unless (gnus-check-server
4165              (setq gnus-current-select-method
4166                    (gnus-find-method-for-group group)))
4167       (error "Couldn't open server"))
4168
4169     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4170         (gnus-activate-group group)     ; Or we can activate it...
4171         (progn                          ; Or we bug out.
4172           (when (equal major-mode 'gnus-summary-mode)
4173             (kill-buffer (current-buffer)))
4174           (error "Couldn't request group %s: %s"
4175                  group (gnus-status-message group))))
4176
4177     (unless (gnus-request-group group t)
4178       (when (equal major-mode 'gnus-summary-mode)
4179         (kill-buffer (current-buffer)))
4180       (error "Couldn't request group %s: %s"
4181              group (gnus-status-message group)))
4182
4183     (setq gnus-newsgroup-name group)
4184     (setq gnus-newsgroup-unselected nil)
4185     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4186     (gnus-summary-setup-default-charset)
4187
4188     ;; Adjust and set lists of article marks.
4189     (when info
4190       (gnus-adjust-marked-articles info))
4191
4192     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4193     (setq cached
4194           (if (gnus-virtual-group-p group)
4195               gnus-newsgroup-cached
4196             (gnus-cache-articles-in-group group)))
4197
4198     (setq gnus-newsgroup-unreads
4199           (gnus-set-difference
4200            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4201            gnus-newsgroup-dormant))
4202
4203     (setq gnus-newsgroup-processable nil)
4204
4205     (gnus-update-read-articles group gnus-newsgroup-unreads)
4206
4207     (if (setq articles select-articles)
4208         (setq gnus-newsgroup-unselected
4209               (gnus-sorted-intersection
4210                gnus-newsgroup-unreads
4211                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4212       (setq articles (gnus-articles-to-read group read-all)))
4213
4214     (cond
4215      ((null articles)
4216       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4217       'quit)
4218      ((eq articles 0) nil)
4219      (t
4220       ;; Init the dependencies hash table.
4221       (setq gnus-newsgroup-dependencies
4222             (gnus-make-hashtable (length articles)))
4223       (gnus-set-global-variables)
4224       ;; Retrieve the headers and read them in.
4225       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4226       (setq gnus-newsgroup-headers
4227             (gnus-retrieve-parsed-headers
4228              articles gnus-newsgroup-name
4229              ;; We might want to fetch old headers, but
4230              ;; not if there is only 1 article.
4231              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4232                            (not (numberp gnus-fetch-old-headers)))
4233                       (> (length articles) 1))
4234                   gnus-fetch-old-headers)))
4235       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4236
4237       ;; Suppress duplicates?
4238       (when gnus-suppress-duplicates
4239         (gnus-dup-suppress-articles))
4240
4241       ;; Set the initial limit.
4242       (setq gnus-newsgroup-limit (copy-sequence articles))
4243       ;; Remove canceled articles from the list of unread articles.
4244       (setq gnus-newsgroup-unreads
4245             (gnus-set-sorted-intersection
4246              gnus-newsgroup-unreads
4247              (setq fetched-articles
4248                    (mapcar (lambda (headers) (mail-header-number headers))
4249                            gnus-newsgroup-headers))))
4250       ;; Removed marked articles that do not exist.
4251       (gnus-update-missing-marks
4252        (gnus-sorted-complement fetched-articles articles))
4253
4254       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4255       (when cached
4256         (setq gnus-newsgroup-cached cached))
4257
4258       ;; We might want to build some more threads first.
4259       (when (and gnus-fetch-old-headers
4260                  (eq gnus-headers-retrieved-by 'nov))
4261         (if (eq gnus-fetch-old-headers 'invisible)
4262             (gnus-build-all-threads)
4263           (gnus-build-old-threads)))
4264       ;; Let the Gnus agent mark articles as read.
4265       (when gnus-agent
4266         (gnus-agent-get-undownloaded-list))
4267       ;; Remove list identifiers from subject
4268       (when gnus-list-identifiers
4269         (gnus-summary-remove-list-identifiers))
4270       ;; Check whether auto-expire is to be done in this group.
4271       (setq gnus-newsgroup-auto-expire
4272             (gnus-group-auto-expirable-p group))
4273       ;; Set up the article buffer now, if necessary.
4274       (unless gnus-single-article-buffer
4275         (gnus-article-setup-buffer))
4276       ;; First and last article in this newsgroup.
4277       (when gnus-newsgroup-headers
4278         (setq gnus-newsgroup-begin
4279               (mail-header-number (car gnus-newsgroup-headers))
4280               gnus-newsgroup-end
4281               (mail-header-number
4282                (gnus-last-element gnus-newsgroup-headers))))
4283       ;; GROUP is successfully selected.
4284       (or gnus-newsgroup-headers t)))))
4285
4286 (defun gnus-articles-to-read (group &optional read-all)
4287   "Find out what articles the user wants to read."
4288   (let* ((articles
4289           ;; Select all articles if `read-all' is non-nil, or if there
4290           ;; are no unread articles.
4291           (if (or read-all
4292                   (and (zerop (length gnus-newsgroup-marked))
4293                        (zerop (length gnus-newsgroup-unreads)))
4294                   (eq (gnus-group-find-parameter group 'display)
4295                       'all))
4296               (or
4297                (gnus-uncompress-range (gnus-active group))
4298                (gnus-cache-articles-in-group group))
4299             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4300                           (copy-sequence gnus-newsgroup-unreads))
4301                   '<)))
4302          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4303          (scored (length scored-list))
4304          (number (length articles))
4305          (marked (+ (length gnus-newsgroup-marked)
4306                     (length gnus-newsgroup-dormant)))
4307          (select
4308           (cond
4309            ((numberp read-all)
4310             read-all)
4311            (t
4312             (condition-case ()
4313                 (cond
4314                  ((and (or (<= scored marked) (= scored number))
4315                        (natnump gnus-large-newsgroup)
4316                        (> number gnus-large-newsgroup))
4317                   (let* ((cursor-in-echo-area nil)
4318                          (input (read-from-minibuffer
4319                                  (format
4320                                   "How many articles from %s (max %d): "
4321                                   (gnus-limit-string gnus-newsgroup-name 35)
4322                                   number)
4323                                  (cons (number-to-string gnus-large-newsgroup)
4324                                        0))))
4325                     (if (string-match "^[ \t]*$" input)
4326                         number
4327                       input)))
4328                  ((and (> scored marked) (< scored number)
4329                        (> (- scored number) 20))
4330                   (let ((input
4331                          (read-string
4332                           (format "%s %s (%d scored, %d total): "
4333                                   "How many articles from"
4334                                   group scored number))))
4335                     (if (string-match "^[ \t]*$" input)
4336                         number input)))
4337                  (t number))
4338               (quit nil))))))
4339     (setq select (if (stringp select) (string-to-number select) select))
4340     (if (or (null select) (zerop select))
4341         select
4342       (if (and (not (zerop scored)) (<= (abs select) scored))
4343           (progn
4344             (setq articles (sort scored-list '<))
4345             (setq number (length articles)))
4346         (setq articles (copy-sequence articles)))
4347
4348       (when (< (abs select) number)
4349         (if (< select 0)
4350             ;; Select the N oldest articles.
4351             (setcdr (nthcdr (1- (abs select)) articles) nil)
4352           ;; Select the N most recent articles.
4353           (setq articles (nthcdr (- number select) articles))))
4354       (setq gnus-newsgroup-unselected
4355             (gnus-sorted-intersection
4356              gnus-newsgroup-unreads
4357              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4358       (when gnus-alter-articles-to-read-function
4359         (setq gnus-newsgroup-unreads
4360               (sort
4361                (funcall gnus-alter-articles-to-read-function
4362                         gnus-newsgroup-name gnus-newsgroup-unreads)
4363                '<)))
4364       articles)))
4365
4366 (defun gnus-killed-articles (killed articles)
4367   (let (out)
4368     (while articles
4369       (when (inline (gnus-member-of-range (car articles) killed))
4370         (push (car articles) out))
4371       (setq articles (cdr articles)))
4372     out))
4373
4374 (defun gnus-uncompress-marks (marks)
4375   "Uncompress the mark ranges in MARKS."
4376   (let ((uncompressed '(score bookmark))
4377         out)
4378     (while marks
4379       (if (memq (caar marks) uncompressed)
4380           (push (car marks) out)
4381         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4382       (setq marks (cdr marks)))
4383     out))
4384
4385 (defun gnus-adjust-marked-articles (info)
4386   "Set all article lists and remove all marks that are no longer valid."
4387   (let* ((marked-lists (gnus-info-marks info))
4388          (active (gnus-active (gnus-info-group info)))
4389          (min (car active))
4390          (max (cdr active))
4391          (types gnus-article-mark-lists)
4392          (uncompressed '(score bookmark killed))
4393          marks var articles article mark)
4394
4395     (while marked-lists
4396       (setq marks (pop marked-lists))
4397       (set (setq var (intern (format "gnus-newsgroup-%s"
4398                                      (car (rassq (setq mark (car marks))
4399                                                  types)))))
4400            (if (memq (car marks) uncompressed) (cdr marks)
4401              (gnus-uncompress-range (cdr marks))))
4402
4403       (setq articles (symbol-value var))
4404
4405       ;; All articles have to be subsets of the active articles.
4406       (cond
4407        ;; Adjust "simple" lists.
4408        ((memq mark '(tick dormant expire reply save))
4409         (while articles
4410           (when (or (< (setq article (pop articles)) min) (> article max))
4411             (set var (delq article (symbol-value var))))))
4412        ;; Adjust assocs.
4413        ((memq mark uncompressed)
4414         (when (not (listp (cdr (symbol-value var))))
4415           (set var (list (symbol-value var))))
4416         (when (not (listp (cdr articles)))
4417           (setq articles (list articles)))
4418         (while articles
4419           (when (or (not (consp (setq article (pop articles))))
4420                     (< (car article) min)
4421                     (> (car article) max))
4422             (set var (delq article (symbol-value var))))))))))
4423
4424 (defun gnus-update-missing-marks (missing)
4425   "Go through the list of MISSING articles and remove them from the mark lists."
4426   (when missing
4427     (let ((types gnus-article-mark-lists)
4428           var m)
4429       ;; Go through all types.
4430       (while types
4431         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4432         (when (symbol-value var)
4433           ;; This list has articles.  So we delete all missing articles
4434           ;; from it.
4435           (setq m missing)
4436           (while m
4437             (set var (delq (pop m) (symbol-value var)))))))))
4438
4439 (defun gnus-update-marks ()
4440   "Enter the various lists of marked articles into the newsgroup info list."
4441   (let ((types gnus-article-mark-lists)
4442         (info (gnus-get-info gnus-newsgroup-name))
4443         (uncompressed '(score bookmark killed))
4444         type list newmarked symbol delta-marks)
4445     (when info
4446       ;; Add all marks lists to the list of marks lists.
4447       (while (setq type (pop types))
4448         (setq list (symbol-value
4449                     (setq symbol
4450                           (intern (format "gnus-newsgroup-%s"
4451                                           (car type))))))
4452
4453         (when list
4454           ;; Get rid of the entries of the articles that have the
4455           ;; default score.
4456           (when (and (eq (cdr type) 'score)
4457                      gnus-save-score
4458                      list)
4459             (let* ((arts list)
4460                    (prev (cons nil list))
4461                    (all prev))
4462               (while arts
4463                 (if (or (not (consp (car arts)))
4464                         (= (cdar arts) gnus-summary-default-score))
4465                     (setcdr prev (cdr arts))
4466                   (setq prev arts))
4467                 (setq arts (cdr arts)))
4468               (setq list (cdr all)))))
4469
4470         (unless (memq (cdr type) uncompressed)
4471           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4472
4473         (when (gnus-check-backend-function
4474                'request-set-mark gnus-newsgroup-name)
4475           ;; propagate flags to server, with the following exceptions:
4476           ;; uncompressed:s are not proper flags (they are cons cells)
4477           ;; cache is a internal gnus flag
4478           ;; download are local to one gnus installation (well)
4479           ;; unsend are for nndraft groups only
4480           ;; xxx: generality of this?  this suits nnimap anyway
4481           (unless (memq (cdr type) (append '(cache download unsend)
4482                                            uncompressed))
4483             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4484                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4485                    (add (gnus-remove-from-range
4486                          (gnus-copy-sequence list) old)))
4487               (when add
4488                 (push (list add 'add (list (cdr type))) delta-marks))
4489               (when del
4490                 (push (list del 'del (list (cdr type))) delta-marks)))))
4491
4492         (when list
4493           (push (cons (cdr type) list) newmarked)))
4494
4495       (when delta-marks
4496         (unless (gnus-check-group gnus-newsgroup-name)
4497           (error "Can't open server for %s" gnus-newsgroup-name))
4498         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4499
4500       ;; Enter these new marks into the info of the group.
4501       (if (nthcdr 3 info)
4502           (setcar (nthcdr 3 info) newmarked)
4503         ;; Add the marks lists to the end of the info.
4504         (when newmarked
4505           (setcdr (nthcdr 2 info) (list newmarked))))
4506
4507       ;; Cut off the end of the info if there's nothing else there.
4508       (let ((i 5))
4509         (while (and (> i 2)
4510                     (not (nth i info)))
4511           (when (nthcdr (decf i) info)
4512             (setcdr (nthcdr i info) nil)))))))
4513
4514 (defun gnus-set-mode-line (where)
4515   "Set the mode line of the article or summary buffers.
4516 If WHERE is `summary', the summary mode line format will be used."
4517   ;; Is this mode line one we keep updated?
4518   (when (and (memq where gnus-updated-mode-lines)
4519              (symbol-value
4520               (intern (format "gnus-%s-mode-line-format-spec" where))))
4521     (let (mode-string)
4522       (save-excursion
4523         ;; We evaluate this in the summary buffer since these
4524         ;; variables are buffer-local to that buffer.
4525         (set-buffer gnus-summary-buffer)
4526         ;; We bind all these variables that are used in the `eval' form
4527         ;; below.
4528         (let* ((mformat (symbol-value
4529                          (intern
4530                           (format "gnus-%s-mode-line-format-spec" where))))
4531                (gnus-tmp-group-name (gnus-group-name-decode
4532                                      gnus-newsgroup-name
4533                                      (gnus-group-name-charset
4534                                       nil
4535                                       gnus-newsgroup-name)))
4536                (gnus-tmp-article-number (or gnus-current-article 0))
4537                (gnus-tmp-unread gnus-newsgroup-unreads)
4538                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4539                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4540                (gnus-tmp-unread-and-unselected
4541                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4542                             (zerop gnus-tmp-unselected))
4543                        "")
4544                       ((zerop gnus-tmp-unselected)
4545                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4546                       (t (format "{%d(+%d) more}"
4547                                  gnus-tmp-unread-and-unticked
4548                                  gnus-tmp-unselected))))
4549                (gnus-tmp-subject
4550                 (if (and gnus-current-headers
4551                          (vectorp gnus-current-headers))
4552                     (gnus-mode-string-quote
4553                      (mail-header-subject gnus-current-headers))
4554                   ""))
4555                bufname-length max-len
4556                gnus-tmp-header);; passed as argument to any user-format-funcs
4557           (setq mode-string (eval mformat))
4558           (setq bufname-length (if (string-match "%b" mode-string)
4559                                    (- (length
4560                                        (buffer-name
4561                                         (if (eq where 'summary)
4562                                             nil
4563                                           (get-buffer gnus-article-buffer))))
4564                                       2)
4565                                  0))
4566           (setq max-len (max 4 (if gnus-mode-non-string-length
4567                                    (- (window-width)
4568                                       gnus-mode-non-string-length
4569                                       bufname-length)
4570                                  (length mode-string))))
4571           ;; We might have to chop a bit of the string off...
4572           (when (> (length mode-string) max-len)
4573             (setq mode-string
4574                   (concat (gnus-truncate-string mode-string (- max-len 3))
4575                           "...")))
4576           ;; Pad the mode string a bit.
4577           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4578       ;; Update the mode line.
4579       (setq mode-line-buffer-identification
4580             (gnus-mode-line-buffer-identification (list mode-string)))
4581       (set-buffer-modified-p t))))
4582
4583 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4584   "Go through the HEADERS list and add all Xrefs to a hash table.
4585 The resulting hash table is returned, or nil if no Xrefs were found."
4586   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4587          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4588          (xref-hashtb (gnus-make-hashtable))
4589          start group entry number xrefs header)
4590     (while headers
4591       (setq header (pop headers))
4592       (when (and (setq xrefs (mail-header-xref header))
4593                  (not (memq (setq number (mail-header-number header))
4594                             unreads)))
4595         (setq start 0)
4596         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4597           (setq start (match-end 0))
4598           (setq group (if prefix
4599                           (concat prefix (substring xrefs (match-beginning 1)
4600                                                     (match-end 1)))
4601                         (substring xrefs (match-beginning 1) (match-end 1))))
4602           (setq number
4603                 (string-to-int (substring xrefs (match-beginning 2)
4604                                           (match-end 2))))
4605           (if (setq entry (gnus-gethash group xref-hashtb))
4606               (setcdr entry (cons number (cdr entry)))
4607             (gnus-sethash group (cons number nil) xref-hashtb)))))
4608     (and start xref-hashtb)))
4609
4610 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4611   "Look through all the headers and mark the Xrefs as read."
4612   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4613         name entry info xref-hashtb idlist method nth4)
4614     (save-excursion
4615       (set-buffer gnus-group-buffer)
4616       (when (setq xref-hashtb
4617                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4618         (mapatoms
4619          (lambda (group)
4620            (unless (string= from-newsgroup (setq name (symbol-name group)))
4621              (setq idlist (symbol-value group))
4622              ;; Dead groups are not updated.
4623              (and (prog1
4624                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4625                             info (nth 2 entry))
4626                     (when (stringp (setq nth4 (gnus-info-method info)))
4627                       (setq nth4 (gnus-server-to-method nth4))))
4628                   ;; Only do the xrefs if the group has the same
4629                   ;; select method as the group we have just read.
4630                   (or (gnus-methods-equal-p
4631                        nth4 (gnus-find-method-for-group from-newsgroup))
4632                       virtual
4633                       (equal nth4 (setq method (gnus-find-method-for-group
4634                                                 from-newsgroup)))
4635                       (and (equal (car nth4) (car method))
4636                            (equal (nth 1 nth4) (nth 1 method))))
4637                   gnus-use-cross-reference
4638                   (or (not (eq gnus-use-cross-reference t))
4639                       virtual
4640                       ;; Only do cross-references on subscribed
4641                       ;; groups, if that is what is wanted.
4642                       (<= (gnus-info-level info) gnus-level-subscribed))
4643                   (gnus-group-make-articles-read name idlist))))
4644          xref-hashtb)))))
4645
4646 (defun gnus-compute-read-articles (group articles)
4647   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4648          (info (nth 2 entry))
4649          (active (gnus-active group))
4650          ninfo)
4651     (when entry
4652       ;; First peel off all invalid article numbers.
4653       (when active
4654         (let ((ids articles)
4655               id first)
4656           (while (setq id (pop ids))
4657             (when (and first (> id (cdr active)))
4658               ;; We'll end up in this situation in one particular
4659               ;; obscure situation.  If you re-scan a group and get
4660               ;; a new article that is cross-posted to a different
4661               ;; group that has not been re-scanned, you might get
4662               ;; crossposted article that has a higher number than
4663               ;; Gnus believes possible.  So we re-activate this
4664               ;; group as well.  This might mean doing the
4665               ;; crossposting thingy will *increase* the number
4666               ;; of articles in some groups.  Tsk, tsk.
4667               (setq active (or (gnus-activate-group group) active)))
4668             (when (or (> id (cdr active))
4669                       (< id (car active)))
4670               (setq articles (delq id articles))))))
4671       ;; If the read list is nil, we init it.
4672       (if (and active
4673                (null (gnus-info-read info))
4674                (> (car active) 1))
4675           (setq ninfo (cons 1 (1- (car active))))
4676         (setq ninfo (gnus-info-read info)))
4677       ;; Then we add the read articles to the range.
4678       (gnus-add-to-range
4679        ninfo (setq articles (sort articles '<))))))
4680
4681 (defun gnus-group-make-articles-read (group articles)
4682   "Update the info of GROUP to say that ARTICLES are read."
4683   (let* ((num 0)
4684          (entry (gnus-gethash group gnus-newsrc-hashtb))
4685          (info (nth 2 entry))
4686          (active (gnus-active group))
4687          range)
4688     (when entry
4689       (setq range (gnus-compute-read-articles group articles))
4690       (save-excursion
4691         (set-buffer gnus-group-buffer)
4692         (gnus-undo-register
4693           `(progn
4694              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4695              (gnus-info-set-read ',info ',(gnus-info-read info))
4696              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4697              (gnus-group-update-group ,group t))))
4698       ;; Add the read articles to the range.
4699       (gnus-info-set-read info range)
4700       ;; Then we have to re-compute how many unread
4701       ;; articles there are in this group.
4702       (when active
4703         (cond
4704          ((not range)
4705           (setq num (- (1+ (cdr active)) (car active))))
4706          ((not (listp (cdr range)))
4707           (setq num (- (cdr active) (- (1+ (cdr range))
4708                                        (car range)))))
4709          (t
4710           (while range
4711             (if (numberp (car range))
4712                 (setq num (1+ num))
4713               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4714             (setq range (cdr range)))
4715           (setq num (- (cdr active) num))))
4716         ;; Update the number of unread articles.
4717         (setcar entry num)
4718         ;; Update the group buffer.
4719         (gnus-group-update-group group t)))))
4720
4721 (defvar gnus-newsgroup-none-id 0)
4722
4723 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4724   (let ((cur nntp-server-buffer)
4725         (dependencies
4726          (or dependencies
4727              (save-excursion (set-buffer gnus-summary-buffer)
4728                              gnus-newsgroup-dependencies)))
4729         headers id end ref
4730         (mail-parse-charset gnus-newsgroup-charset)
4731         (mail-parse-ignored-charsets
4732          (save-excursion (condition-case nil
4733                              (set-buffer gnus-summary-buffer)
4734                            (error))
4735                          gnus-newsgroup-ignored-charsets)))
4736     (save-excursion
4737       (set-buffer nntp-server-buffer)
4738       ;; Translate all TAB characters into SPACE characters.
4739       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4740       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4741       (gnus-run-hooks 'gnus-parse-headers-hook)
4742       (let ((case-fold-search t)
4743             in-reply-to header p lines chars ctype)
4744         (goto-char (point-min))
4745         ;; Search to the beginning of the next header.  Error messages
4746         ;; do not begin with 2 or 3.
4747         (while (re-search-forward "^[23][0-9]+ " nil t)
4748           (setq id nil
4749                 ref nil)
4750           ;; This implementation of this function, with nine
4751           ;; search-forwards instead of the one re-search-forward and
4752           ;; a case (which basically was the old function) is actually
4753           ;; about twice as fast, even though it looks messier.  You
4754           ;; can't have everything, I guess.  Speed and elegance
4755           ;; doesn't always go hand in hand.
4756           (setq
4757            header
4758            (make-full-mail-header
4759             ;; Number.
4760             (prog1
4761                 (read cur)
4762               (end-of-line)
4763               (setq p (point))
4764               (narrow-to-region (point)
4765                                 (or (and (search-forward "\n.\n" nil t)
4766                                          (- (point) 2))
4767                                     (point))))
4768             ;; Subject.
4769             (progn
4770               (goto-char p)
4771               (if (search-forward "\nsubject: " nil t)
4772                   (buffer-substring (match-end 0) (std11-field-end))
4773                 "(none)"))
4774             ;; From.
4775             (progn
4776               (goto-char p)
4777               (if (search-forward "\nfrom: " nil t)
4778                   (buffer-substring (match-end 0) (std11-field-end))
4779                 "(nobody)"))
4780             ;; Date.
4781             (progn
4782               (goto-char p)
4783               (if (search-forward "\ndate: " nil t)
4784                   (buffer-substring (match-end 0) (std11-field-end))
4785                 ""))
4786             ;; Message-ID.
4787             (progn
4788               (goto-char p)
4789               (setq id (if (re-search-forward
4790                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4791                            ;; We do it this way to make sure the Message-ID
4792                            ;; is (somewhat) syntactically valid.
4793                            (buffer-substring (match-beginning 1)
4794                                              (match-end 1))
4795                          ;; If there was no message-id, we just fake one
4796                          ;; to make subsequent routines simpler.
4797                          (nnheader-generate-fake-message-id))))
4798             ;; References.
4799             (progn
4800               (goto-char p)
4801               (if (search-forward "\nreferences: " nil t)
4802                   (progn
4803                     (setq end (point))
4804                     (prog1
4805                         (buffer-substring (match-end 0) (std11-field-end))
4806                       (setq ref
4807                             (buffer-substring
4808                              (progn
4809                                ;; (end-of-line)
4810                                (search-backward ">" end t)
4811                                (1+ (point)))
4812                              (progn
4813                                (search-backward "<" end t)
4814                                (point))))))
4815                 ;; Get the references from the in-reply-to header if there
4816                 ;; were no references and the in-reply-to header looks
4817                 ;; promising.
4818                 (if (and (search-forward "\nin-reply-to: " nil t)
4819                          (setq in-reply-to
4820                                (buffer-substring (match-end 0)
4821                                                  (std11-field-end)))
4822                          (string-match "<[^>]+>" in-reply-to))
4823                     (let (ref2)
4824                       (setq ref (substring in-reply-to (match-beginning 0)
4825                                            (match-end 0)))
4826                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4827                         (setq ref2 (substring in-reply-to (match-beginning 0)
4828                                               (match-end 0)))
4829                         (when (> (length ref2) (length ref))
4830                           (setq ref ref2)))
4831                       ref)
4832                   (setq ref nil))))
4833             ;; Chars.
4834             (progn
4835               (goto-char p)
4836               (if (search-forward "\nchars: " nil t)
4837                   (if (numberp (setq chars (ignore-errors (read cur))))
4838                       chars 0)
4839                 0))
4840             ;; Lines.
4841             (progn
4842               (goto-char p)
4843               (if (search-forward "\nlines: " nil t)
4844                   (if (numberp (setq lines (ignore-errors (read cur))))
4845                       lines 0)
4846                 0))
4847             ;; Xref.
4848             (progn
4849               (goto-char p)
4850               (and (search-forward "\nxref: " nil t)
4851                    (buffer-substring (match-end 0) (std11-field-end))))
4852             ;; Extra.
4853             (when gnus-extra-headers
4854               (let ((extra gnus-extra-headers)
4855                     out)
4856                 (while extra
4857                   (goto-char p)
4858                   (when (search-forward
4859                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4860                     (push (cons (car extra)
4861                                 (buffer-substring (match-end 0)
4862                                                   (std11-field-end)))
4863                           out))
4864                   (pop extra))
4865                 out))))
4866           (goto-char p)
4867           (if (and (search-forward "\ncontent-type: " nil t)
4868                    (setq ctype
4869                          (buffer-substring (match-end 0) (std11-field-end))))
4870               (mime-entity-set-content-type-internal
4871                header (mime-parse-Content-Type ctype)))
4872           (when (equal id ref)
4873             (setq ref nil))
4874
4875           (when gnus-alter-header-function
4876             (funcall gnus-alter-header-function header)
4877             (setq id (mail-header-id header)
4878                   ref (gnus-parent-id (mail-header-references header))))
4879
4880           (when (setq header
4881                       (gnus-dependencies-add-header
4882                        header dependencies force-new))
4883             (push header headers))
4884           (goto-char (point-max))
4885           (widen))
4886         (nreverse headers)))))
4887
4888 ;; Goes through the xover lines and returns a list of vectors
4889 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4890                                                   force-new dependencies
4891                                                   group also-fetch-heads)
4892   "Parse the news overview data in the server buffer.
4893 Return a list of headers that match SEQUENCE (see
4894 `nntp-retrieve-headers')."
4895   ;; Get the Xref when the users reads the articles since most/some
4896   ;; NNTP servers do not include Xrefs when using XOVER.
4897   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4898   (let ((mail-parse-charset gnus-newsgroup-charset)
4899         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4900         (cur nntp-server-buffer)
4901         (dependencies (or dependencies gnus-newsgroup-dependencies))
4902         number headers header)
4903     (save-excursion
4904       (set-buffer nntp-server-buffer)
4905       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4906       ;; Allow the user to mangle the headers before parsing them.
4907       (gnus-run-hooks 'gnus-parse-headers-hook)
4908       (goto-char (point-min))
4909       (while (not (eobp))
4910         (condition-case ()
4911             (while (and sequence (not (eobp)))
4912               (setq number (read cur))
4913               (while (and sequence
4914                           (< (car sequence) number))
4915                 (setq sequence (cdr sequence)))
4916               (and sequence
4917                    (eq number (car sequence))
4918                    (progn
4919                      (setq sequence (cdr sequence))
4920                      (setq header (inline
4921                                     (gnus-nov-parse-line
4922                                      number dependencies force-new))))
4923                    (push header headers))
4924               (forward-line 1))
4925           (error
4926            (gnus-error 4 "Strange nov line (%d)"
4927                        (count-lines (point-min) (point)))))
4928         (forward-line 1))
4929       ;; A common bug in inn is that if you have posted an article and
4930       ;; then retrieves the active file, it will answer correctly --
4931       ;; the new article is included.  However, a NOV entry for the
4932       ;; article may not have been generated yet, so this may fail.
4933       ;; We work around this problem by retrieving the last few
4934       ;; headers using HEAD.
4935       (if (or (not also-fetch-heads)
4936               (not sequence))
4937           ;; We (probably) got all the headers.
4938           (nreverse headers)
4939         (let ((gnus-nov-is-evil t))
4940           (nconc
4941            (nreverse headers)
4942            (gnus-retrieve-parsed-headers sequence group)
4943            ))))))
4944
4945 (defun gnus-article-get-xrefs ()
4946   "Fill in the Xref value in `gnus-current-headers', if necessary.
4947 This is meant to be called in `gnus-article-internal-prepare-hook'."
4948   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4949                                  gnus-current-headers)))
4950     (or (not gnus-use-cross-reference)
4951         (not headers)
4952         (and (mail-header-xref headers)
4953              (not (string= (mail-header-xref headers) "")))
4954         (let ((case-fold-search t)
4955               xref)
4956           (save-restriction
4957             (nnheader-narrow-to-headers)
4958             (goto-char (point-min))
4959             (when (or (and (not (eobp))
4960                            (eq (downcase (char-after)) ?x)
4961                            (looking-at "Xref:"))
4962                       (search-forward "\nXref:" nil t))
4963               (goto-char (1+ (match-end 0)))
4964               (setq xref (buffer-substring (point)
4965                                            (progn (end-of-line) (point))))
4966               (mail-header-set-xref headers xref)))))))
4967
4968 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4969   "Find article ID and insert the summary line for that article.
4970 OLD-HEADER can either be a header or a line number to insert
4971 the subject line on."
4972   (let* ((line (and (numberp old-header) old-header))
4973          (old-header (and (vectorp old-header) old-header))
4974          (header (cond ((and old-header use-old-header)
4975                         old-header)
4976                        ((and (numberp id)
4977                              (gnus-number-to-header id))
4978                         (gnus-number-to-header id))
4979                        (t
4980                         (gnus-read-header id))))
4981          (number (and (numberp id) id))
4982          d)
4983     (when header
4984       ;; Rebuild the thread that this article is part of and go to the
4985       ;; article we have fetched.
4986       (when (and (not gnus-show-threads)
4987                  old-header)
4988         (when (and number
4989                    (setq d (gnus-data-find (mail-header-number old-header))))
4990           (goto-char (gnus-data-pos d))
4991           (gnus-data-remove
4992            number
4993            (- (gnus-point-at-bol)
4994               (prog1
4995                   (1+ (gnus-point-at-eol))
4996                 (gnus-delete-line))))))
4997       (when old-header
4998         (mail-header-set-number header (mail-header-number old-header)))
4999       (setq gnus-newsgroup-sparse
5000             (delq (setq number (mail-header-number header))
5001                   gnus-newsgroup-sparse))
5002       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5003       (push number gnus-newsgroup-limit)
5004       (gnus-rebuild-thread (mail-header-id header) line)
5005       (gnus-summary-goto-subject number nil t))
5006     (when (and (numberp number)
5007                (> number 0))
5008       ;; We have to update the boundaries even if we can't fetch the
5009       ;; article if ID is a number -- so that the next `P' or `N'
5010       ;; command will fetch the previous (or next) article even
5011       ;; if the one we tried to fetch this time has been canceled.
5012       (when (> number gnus-newsgroup-end)
5013         (setq gnus-newsgroup-end number))
5014       (when (< number gnus-newsgroup-begin)
5015         (setq gnus-newsgroup-begin number))
5016       (setq gnus-newsgroup-unselected
5017             (delq number gnus-newsgroup-unselected)))
5018     ;; Report back a success?
5019     (and header (mail-header-number header))))
5020
5021 ;;; Process/prefix in the summary buffer
5022
5023 (defun gnus-summary-work-articles (n)
5024   "Return a list of articles to be worked upon.
5025 The prefix argument, the list of process marked articles, and the
5026 current article will be taken into consideration."
5027   (save-excursion
5028     (set-buffer gnus-summary-buffer)
5029     (cond
5030      (n
5031       ;; A numerical prefix has been given.
5032       (setq n (prefix-numeric-value n))
5033       (let ((backward (< n 0))
5034             (n (abs (prefix-numeric-value n)))
5035             articles article)
5036         (save-excursion
5037           (while
5038               (and (> n 0)
5039                    (push (setq article (gnus-summary-article-number))
5040                          articles)
5041                    (if backward
5042                        (gnus-summary-find-prev nil article)
5043                      (gnus-summary-find-next nil article)))
5044             (decf n)))
5045         (nreverse articles)))
5046      ((and (gnus-region-active-p) (mark))
5047       (message "region active")
5048       ;; Work on the region between point and mark.
5049       (let ((max (max (point) (mark)))
5050             articles article)
5051         (save-excursion
5052           (goto-char (min (point) (mark)))
5053           (while
5054               (and
5055                (push (setq article (gnus-summary-article-number)) articles)
5056                (gnus-summary-find-next nil article)
5057                (< (point) max)))
5058           (nreverse articles))))
5059      (gnus-newsgroup-processable
5060       ;; There are process-marked articles present.
5061       ;; Save current state.
5062       (gnus-summary-save-process-mark)
5063       ;; Return the list.
5064       (reverse gnus-newsgroup-processable))
5065      (t
5066       ;; Just return the current article.
5067       (list (gnus-summary-article-number))))))
5068
5069 (defmacro gnus-summary-iterate (arg &rest forms)
5070   "Iterate over the process/prefixed articles and do FORMS.
5071 ARG is the interactive prefix given to the command.  FORMS will be
5072 executed with point over the summary line of the articles."
5073   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5074     `(let ((,articles (gnus-summary-work-articles ,arg)))
5075        (while ,articles
5076          (gnus-summary-goto-subject (car ,articles))
5077          ,@forms
5078          (pop ,articles)))))
5079
5080 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5081 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5082
5083 (defun gnus-summary-save-process-mark ()
5084   "Push the current set of process marked articles on the stack."
5085   (interactive)
5086   (push (copy-sequence gnus-newsgroup-processable)
5087         gnus-newsgroup-process-stack))
5088
5089 (defun gnus-summary-kill-process-mark ()
5090   "Push the current set of process marked articles on the stack and unmark."
5091   (interactive)
5092   (gnus-summary-save-process-mark)
5093   (gnus-summary-unmark-all-processable))
5094
5095 (defun gnus-summary-yank-process-mark ()
5096   "Pop the last process mark state off the stack and restore it."
5097   (interactive)
5098   (unless gnus-newsgroup-process-stack
5099     (error "Empty mark stack"))
5100   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5101
5102 (defun gnus-summary-process-mark-set (set)
5103   "Make SET into the current process marked articles."
5104   (gnus-summary-unmark-all-processable)
5105   (while set
5106     (gnus-summary-set-process-mark (pop set))))
5107
5108 ;;; Searching and stuff
5109
5110 (defun gnus-summary-search-group (&optional backward use-level)
5111   "Search for next unread newsgroup.
5112 If optional argument BACKWARD is non-nil, search backward instead."
5113   (save-excursion
5114     (set-buffer gnus-group-buffer)
5115     (when (gnus-group-search-forward
5116            backward nil (if use-level (gnus-group-group-level) nil))
5117       (gnus-group-group-name))))
5118
5119 (defun gnus-summary-best-group (&optional exclude-group)
5120   "Find the name of the best unread group.
5121 If EXCLUDE-GROUP, do not go to this group."
5122   (save-excursion
5123     (set-buffer gnus-group-buffer)
5124     (save-excursion
5125       (gnus-group-best-unread-group exclude-group))))
5126
5127 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5128   (if backward (gnus-summary-find-prev)
5129     (let* ((dummy (gnus-summary-article-intangible-p))
5130            (article (or article (gnus-summary-article-number)))
5131            (arts (gnus-data-find-list article))
5132            result)
5133       (when (and (not dummy)
5134                  (or (not gnus-summary-check-current)
5135                      (not unread)
5136                      (not (gnus-data-unread-p (car arts)))))
5137         (setq arts (cdr arts)))
5138       (when (setq result
5139                   (if unread
5140                       (progn
5141                         (while arts
5142                           (when (or (and undownloaded
5143                                          (eq gnus-undownloaded-mark
5144                                              (gnus-data-mark (car arts))))
5145                                     (gnus-data-unread-p (car arts)))
5146                             (setq result (car arts)
5147                                   arts nil))
5148                           (setq arts (cdr arts)))
5149                         result)
5150                     (car arts)))
5151         (goto-char (gnus-data-pos result))
5152         (gnus-data-number result)))))
5153
5154 (defun gnus-summary-find-prev (&optional unread article)
5155   (let* ((eobp (eobp))
5156          (article (or article (gnus-summary-article-number)))
5157          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5158          result)
5159     (when (and (not eobp)
5160                (or (not gnus-summary-check-current)
5161                    (not unread)
5162                    (not (gnus-data-unread-p (car arts)))))
5163       (setq arts (cdr arts)))
5164     (when (setq result
5165                 (if unread
5166                     (progn
5167                       (while arts
5168                         (when (gnus-data-unread-p (car arts))
5169                           (setq result (car arts)
5170                                 arts nil))
5171                         (setq arts (cdr arts)))
5172                       result)
5173                   (car arts)))
5174       (goto-char (gnus-data-pos result))
5175       (gnus-data-number result))))
5176
5177 (defun gnus-summary-find-subject (subject &optional unread backward article)
5178   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5179          (article (or article (gnus-summary-article-number)))
5180          (articles (gnus-data-list backward))
5181          (arts (gnus-data-find-list article articles))
5182          result)
5183     (when (or (not gnus-summary-check-current)
5184               (not unread)
5185               (not (gnus-data-unread-p (car arts))))
5186       (setq arts (cdr arts)))
5187     (while arts
5188       (and (or (not unread)
5189                (gnus-data-unread-p (car arts)))
5190            (vectorp (gnus-data-header (car arts)))
5191            (gnus-subject-equal
5192             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5193            (setq result (car arts)
5194                  arts nil))
5195       (setq arts (cdr arts)))
5196     (and result
5197          (goto-char (gnus-data-pos result))
5198          (gnus-data-number result))))
5199
5200 (defun gnus-summary-search-forward (&optional unread subject backward)
5201   "Search forward for an article.
5202 If UNREAD, look for unread articles.  If SUBJECT, look for
5203 articles with that subject.  If BACKWARD, search backward instead."
5204   (cond (subject (gnus-summary-find-subject subject unread backward))
5205         (backward (gnus-summary-find-prev unread))
5206         (t (gnus-summary-find-next unread))))
5207
5208 (defun gnus-recenter (&optional n)
5209   "Center point in window and redisplay frame.
5210 Also do horizontal recentering."
5211   (interactive "P")
5212   (when (and gnus-auto-center-summary
5213              (not (eq gnus-auto-center-summary 'vertical)))
5214     (gnus-horizontal-recenter))
5215   (recenter n))
5216
5217 (defun gnus-summary-recenter ()
5218   "Center point in the summary window.
5219 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5220 displayed, no centering will be performed."
5221   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5222   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5223   (interactive)
5224   (let* ((top (cond ((< (window-height) 4) 0)
5225                     ((< (window-height) 7) 1)
5226                     (t (if (numberp gnus-auto-center-summary)
5227                            gnus-auto-center-summary
5228                          2))))
5229          (height (1- (window-height)))
5230          (bottom (save-excursion (goto-char (point-max))
5231                                  (forward-line (- height))
5232                                  (point)))
5233          (window (get-buffer-window (current-buffer))))
5234     ;; The user has to want it.
5235     (when gnus-auto-center-summary
5236       (when (get-buffer-window gnus-article-buffer)
5237         ;; Only do recentering when the article buffer is displayed,
5238         ;; Set the window start to either `bottom', which is the biggest
5239         ;; possible valid number, or the second line from the top,
5240         ;; whichever is the least.
5241         (set-window-start
5242          window (min bottom (save-excursion
5243                               (forward-line (- top)) (point)))
5244          t))
5245       ;; Do horizontal recentering while we're at it.
5246       (when (and (get-buffer-window (current-buffer) t)
5247                  (not (eq gnus-auto-center-summary 'vertical)))
5248         (let ((selected (selected-window)))
5249           (select-window (get-buffer-window (current-buffer) t))
5250           (gnus-summary-position-point)
5251           (gnus-horizontal-recenter)
5252           (select-window selected))))))
5253
5254 (defun gnus-summary-jump-to-group (newsgroup)
5255   "Move point to NEWSGROUP in group mode buffer."
5256   ;; Keep update point of group mode buffer if visible.
5257   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5258       (save-window-excursion
5259         ;; Take care of tree window mode.
5260         (when (get-buffer-window gnus-group-buffer)
5261           (pop-to-buffer gnus-group-buffer))
5262         (gnus-group-jump-to-group newsgroup))
5263     (save-excursion
5264       ;; Take care of tree window mode.
5265       (if (get-buffer-window gnus-group-buffer)
5266           (pop-to-buffer gnus-group-buffer)
5267         (set-buffer gnus-group-buffer))
5268       (gnus-group-jump-to-group newsgroup))))
5269
5270 ;; This function returns a list of article numbers based on the
5271 ;; difference between the ranges of read articles in this group and
5272 ;; the range of active articles.
5273 (defun gnus-list-of-unread-articles (group)
5274   (let* ((read (gnus-info-read (gnus-get-info group)))
5275          (active (or (gnus-active group) (gnus-activate-group group)))
5276          (last (cdr active))
5277          first nlast unread)
5278     ;; If none are read, then all are unread.
5279     (if (not read)
5280         (setq first (car active))
5281       ;; If the range of read articles is a single range, then the
5282       ;; first unread article is the article after the last read
5283       ;; article.  Sounds logical, doesn't it?
5284       (if (and (not (listp (cdr read)))
5285                (or (< (car read) (car active))
5286                    (progn (setq read (list read))
5287                           nil)))
5288           (setq first (max (car active) (1+ (cdr read))))
5289         ;; `read' is a list of ranges.
5290         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5291                                   (caar read)))
5292                   1)
5293           (setq first (car active)))
5294         (while read
5295           (when first
5296             (while (< first nlast)
5297               (push first unread)
5298               (setq first (1+ first))))
5299           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5300           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5301           (setq read (cdr read)))))
5302     ;; And add the last unread articles.
5303     (while (<= first last)
5304       (push first unread)
5305       (setq first (1+ first)))
5306     ;; Return the list of unread articles.
5307     (delq 0 (nreverse unread))))
5308
5309 (defun gnus-list-of-read-articles (group)
5310   "Return a list of unread, unticked and non-dormant articles."
5311   (let* ((info (gnus-get-info group))
5312          (marked (gnus-info-marks info))
5313          (active (gnus-active group)))
5314     (and info active
5315          (gnus-set-difference
5316           (gnus-sorted-complement
5317            (gnus-uncompress-range active)
5318            (gnus-list-of-unread-articles group))
5319           (append
5320            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5321            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5322
5323 ;; Various summary commands
5324
5325 (defun gnus-summary-select-article-buffer ()
5326   "Reconfigure windows to show article buffer."
5327   (interactive)
5328   (if (not (gnus-buffer-live-p gnus-article-buffer))
5329       (error "There is no article buffer for this summary buffer")
5330     (gnus-configure-windows 'article)
5331     (select-window (get-buffer-window gnus-article-buffer))))
5332
5333 (defun gnus-summary-universal-argument (arg)
5334   "Perform any operation on all articles that are process/prefixed."
5335   (interactive "P")
5336   (let ((articles (gnus-summary-work-articles arg))
5337         func article)
5338     (if (eq
5339          (setq
5340           func
5341           (key-binding
5342            (read-key-sequence
5343             (substitute-command-keys
5344              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5345          'undefined)
5346         (gnus-error 1 "Undefined key")
5347       (save-excursion
5348         (while articles
5349           (gnus-summary-goto-subject (setq article (pop articles)))
5350           (let (gnus-newsgroup-processable)
5351             (command-execute func))
5352           (gnus-summary-remove-process-mark article)))))
5353   (gnus-summary-position-point))
5354
5355 (defun gnus-summary-toggle-truncation (&optional arg)
5356   "Toggle truncation of summary lines.
5357 With arg, turn line truncation on iff arg is positive."
5358   (interactive "P")
5359   (setq truncate-lines
5360         (if (null arg) (not truncate-lines)
5361           (> (prefix-numeric-value arg) 0)))
5362   (redraw-display))
5363
5364 (defun gnus-summary-reselect-current-group (&optional all rescan)
5365   "Exit and then reselect the current newsgroup.
5366 The prefix argument ALL means to select all articles."
5367   (interactive "P")
5368   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5369     (error "Ephemeral groups can't be reselected"))
5370   (let ((current-subject (gnus-summary-article-number))
5371         (group gnus-newsgroup-name))
5372     (setq gnus-newsgroup-begin nil)
5373     (gnus-summary-exit)
5374     ;; We have to adjust the point of group mode buffer because
5375     ;; point was moved to the next unread newsgroup by exiting.
5376     (gnus-summary-jump-to-group group)
5377     (when rescan
5378       (save-excursion
5379         (save-window-excursion
5380           ;; Don't show group contents.
5381           (set-window-start (selected-window) (point-max))
5382           (gnus-group-get-new-news-this-group 1))))
5383     (gnus-group-read-group all t)
5384     (gnus-summary-goto-subject current-subject nil t)))
5385
5386 (defun gnus-summary-rescan-group (&optional all)
5387   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5388   (interactive "P")
5389   (gnus-summary-reselect-current-group all t))
5390
5391 (defun gnus-summary-update-info (&optional non-destructive)
5392   (save-excursion
5393     (let ((group gnus-newsgroup-name))
5394       (when group
5395         (when gnus-newsgroup-kill-headers
5396           (setq gnus-newsgroup-killed
5397                 (gnus-compress-sequence
5398                  (nconc
5399                   (gnus-set-sorted-intersection
5400                    (gnus-uncompress-range gnus-newsgroup-killed)
5401                    (setq gnus-newsgroup-unselected
5402                          (sort gnus-newsgroup-unselected '<)))
5403                   (setq gnus-newsgroup-unreads
5404                         (sort gnus-newsgroup-unreads '<)))
5405                  t)))
5406         (unless (listp (cdr gnus-newsgroup-killed))
5407           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5408         (let ((headers gnus-newsgroup-headers))
5409           ;; Set the new ranges of read articles.
5410           (save-excursion
5411             (set-buffer gnus-group-buffer)
5412             (gnus-undo-force-boundary))
5413           (gnus-update-read-articles
5414            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5415           ;; Set the current article marks.
5416           (let ((gnus-newsgroup-scored
5417                  (if (and (not gnus-save-score)
5418                           (not non-destructive))
5419                      nil
5420                    gnus-newsgroup-scored)))
5421             (save-excursion
5422               (gnus-update-marks)))
5423           ;; Do the cross-ref thing.
5424           (when gnus-use-cross-reference
5425             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5426           ;; Do not switch windows but change the buffer to work.
5427           (set-buffer gnus-group-buffer)
5428           (unless (gnus-ephemeral-group-p group)
5429             (gnus-group-update-group group)))))))
5430
5431 (defun gnus-summary-save-newsrc (&optional force)
5432   "Save the current number of read/marked articles in the dribble buffer.
5433 The dribble buffer will then be saved.
5434 If FORCE (the prefix), also save the .newsrc file(s)."
5435   (interactive "P")
5436   (gnus-summary-update-info t)
5437   (if force
5438       (gnus-save-newsrc-file)
5439     (gnus-dribble-save)))
5440
5441 (defun gnus-summary-exit (&optional temporary)
5442   "Exit reading current newsgroup, and then return to group selection mode.
5443 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5444   (interactive)
5445   (gnus-set-global-variables)
5446   (gnus-kill-save-kill-buffer)
5447   (gnus-async-halt-prefetch)
5448   (let* ((group gnus-newsgroup-name)
5449          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5450          (mode major-mode)
5451          (group-point nil)
5452          (buf (current-buffer)))
5453     (unless quit-config
5454       ;; Do adaptive scoring, and possibly save score files.
5455       (when gnus-newsgroup-adaptive
5456         (gnus-score-adaptive))
5457       (when gnus-use-scoring
5458         (gnus-score-save)))
5459     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5460     ;; If we have several article buffers, we kill them at exit.
5461     (unless gnus-single-article-buffer
5462       (gnus-kill-buffer gnus-original-article-buffer)
5463       (setq gnus-article-current nil))
5464     (when gnus-use-cache
5465       (gnus-cache-possibly-remove-articles)
5466       (gnus-cache-save-buffers))
5467     (gnus-async-prefetch-remove-group group)
5468     (when gnus-suppress-duplicates
5469       (gnus-dup-enter-articles))
5470     (when gnus-use-trees
5471       (gnus-tree-close group))
5472     (when gnus-use-cache
5473       (gnus-cache-write-active))
5474     ;; Remove entries for this group.
5475     (nnmail-purge-split-history (gnus-group-real-name group))
5476     ;; Make all changes in this group permanent.
5477     (unless quit-config
5478       (gnus-run-hooks 'gnus-exit-group-hook)
5479       (gnus-summary-update-info))
5480     (gnus-close-group group)
5481     ;; Make sure where we were, and go to next newsgroup.
5482     (set-buffer gnus-group-buffer)
5483     (unless quit-config
5484       (gnus-group-jump-to-group group))
5485     (gnus-run-hooks 'gnus-summary-exit-hook)
5486     (unless (or quit-config
5487                 ;; If this group has disappeared from the summary
5488                 ;; buffer, don't skip forwards.
5489                 (not (string= group (gnus-group-group-name))))
5490       (gnus-group-next-unread-group 1))
5491     (setq group-point (point))
5492     (if temporary
5493         nil                             ;Nothing to do.
5494       ;; If we have several article buffers, we kill them at exit.
5495       (unless gnus-single-article-buffer
5496         (gnus-kill-buffer gnus-article-buffer)
5497         (gnus-kill-buffer gnus-original-article-buffer)
5498         (setq gnus-article-current nil))
5499       (set-buffer buf)
5500       (if (not gnus-kill-summary-on-exit)
5501           (gnus-deaden-summary)
5502         ;; We set all buffer-local variables to nil.  It is unclear why
5503         ;; this is needed, but if we don't, buffer-local variables are
5504         ;; not garbage-collected, it seems.  This would the lead to en
5505         ;; ever-growing Emacs.
5506         (gnus-summary-clear-local-variables)
5507         (when (get-buffer gnus-article-buffer)
5508           (bury-buffer gnus-article-buffer))
5509         ;; We clear the global counterparts of the buffer-local
5510         ;; variables as well, just to be on the safe side.
5511         (set-buffer gnus-group-buffer)
5512         (gnus-summary-clear-local-variables)
5513         ;; Return to group mode buffer.
5514         (when (eq mode 'gnus-summary-mode)
5515           (gnus-kill-buffer buf)))
5516       (setq gnus-current-select-method gnus-select-method)
5517       (pop-to-buffer gnus-group-buffer)
5518       (if (not quit-config)
5519           (progn
5520             (goto-char group-point)
5521             (gnus-configure-windows 'group 'force)
5522             (unless (pos-visible-in-window-p)
5523               (forward-line (/ (static-if (featurep 'xemacs)
5524                                    (window-displayed-height)
5525                                  (1- (window-height)))
5526                                -2))
5527               (set-window-start (selected-window) (point))
5528               (goto-char group-point)))
5529         (gnus-handle-ephemeral-exit quit-config))
5530       ;; Clear the current group name.
5531       (unless quit-config
5532         (setq gnus-newsgroup-name nil)))))
5533
5534 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5535 (defun gnus-summary-exit-no-update (&optional no-questions)
5536   "Quit reading current newsgroup without updating read article info."
5537   (interactive)
5538   (let* ((group gnus-newsgroup-name)
5539          (quit-config (gnus-group-quit-config group)))
5540     (when (or no-questions
5541               gnus-expert-user
5542               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5543       (gnus-async-halt-prefetch)
5544       (mapcar 'funcall
5545               (delq 'gnus-summary-expire-articles
5546                     (copy-sequence gnus-summary-prepare-exit-hook)))
5547       ;; If we have several article buffers, we kill them at exit.
5548       (unless gnus-single-article-buffer
5549         (gnus-kill-buffer gnus-article-buffer)
5550         (gnus-kill-buffer gnus-original-article-buffer)
5551         (setq gnus-article-current nil))
5552       (if (not gnus-kill-summary-on-exit)
5553           (gnus-deaden-summary)
5554         (gnus-close-group group)
5555         (gnus-summary-clear-local-variables)
5556         (set-buffer gnus-group-buffer)
5557         (gnus-summary-clear-local-variables)
5558         (when (get-buffer gnus-summary-buffer)
5559           (kill-buffer gnus-summary-buffer)))
5560       (unless gnus-single-article-buffer
5561         (setq gnus-article-current nil))
5562       (when gnus-use-trees
5563         (gnus-tree-close group))
5564       (gnus-async-prefetch-remove-group group)
5565       (when (get-buffer gnus-article-buffer)
5566         (bury-buffer gnus-article-buffer))
5567       ;; Return to the group buffer.
5568       (gnus-configure-windows 'group 'force)
5569       ;; Clear the current group name.
5570       (setq gnus-newsgroup-name nil)
5571       (when (equal (gnus-group-group-name) group)
5572         (gnus-group-next-unread-group 1))
5573       (when quit-config
5574         (gnus-handle-ephemeral-exit quit-config)))))
5575
5576 (defun gnus-handle-ephemeral-exit (quit-config)
5577   "Handle movement when leaving an ephemeral group.
5578 The state which existed when entering the ephemeral is reset."
5579   (if (not (buffer-name (car quit-config)))
5580       (gnus-configure-windows 'group 'force)
5581     (set-buffer (car quit-config))
5582     (cond ((eq major-mode 'gnus-summary-mode)
5583            (gnus-set-global-variables))
5584           ((eq major-mode 'gnus-article-mode)
5585            (save-excursion
5586              ;; The `gnus-summary-buffer' variable may point
5587              ;; to the old summary buffer when using a single
5588              ;; article buffer.
5589              (unless (gnus-buffer-live-p gnus-summary-buffer)
5590                (set-buffer gnus-group-buffer))
5591              (set-buffer gnus-summary-buffer)
5592              (gnus-set-global-variables))))
5593     (if (or (eq (cdr quit-config) 'article)
5594             (eq (cdr quit-config) 'pick))
5595         (progn
5596           ;; The current article may be from the ephemeral group
5597           ;; thus it is best that we reload this article
5598           (gnus-summary-show-article)
5599           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5600               (gnus-configure-windows 'pick 'force)
5601             (gnus-configure-windows (cdr quit-config) 'force)))
5602       (gnus-configure-windows (cdr quit-config) 'force))
5603     (when (eq major-mode 'gnus-summary-mode)
5604       (gnus-summary-next-subject 1 nil t)
5605       (gnus-summary-recenter)
5606       (gnus-summary-position-point))))
5607
5608 (defun gnus-summary-preview-mime-message ()
5609   "MIME decode and play this message."
5610   (interactive)
5611   (let ((gnus-break-pages nil)
5612         (gnus-show-mime t))
5613     (gnus-summary-select-article gnus-show-all-headers t))
5614   (select-window (get-buffer-window gnus-article-buffer)))
5615
5616 ;;; Dead summaries.
5617
5618 (defvar gnus-dead-summary-mode-map nil)
5619
5620 (unless gnus-dead-summary-mode-map
5621   (setq gnus-dead-summary-mode-map (make-keymap))
5622   (suppress-keymap gnus-dead-summary-mode-map)
5623   (substitute-key-definition
5624    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5625   (let ((keys '("\C-d" "\r" "\177" [delete])))
5626     (while keys
5627       (define-key gnus-dead-summary-mode-map
5628         (pop keys) 'gnus-summary-wake-up-the-dead))))
5629
5630 (defvar gnus-dead-summary-mode nil
5631   "Minor mode for Gnus summary buffers.")
5632
5633 (defun gnus-dead-summary-mode (&optional arg)
5634   "Minor mode for Gnus summary buffers."
5635   (interactive "P")
5636   (when (eq major-mode 'gnus-summary-mode)
5637     (make-local-variable 'gnus-dead-summary-mode)
5638     (setq gnus-dead-summary-mode
5639           (if (null arg) (not gnus-dead-summary-mode)
5640             (> (prefix-numeric-value arg) 0)))
5641     (when gnus-dead-summary-mode
5642       (gnus-add-minor-mode
5643        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5644
5645 (defun gnus-deaden-summary ()
5646   "Make the current summary buffer into a dead summary buffer."
5647   ;; Kill any previous dead summary buffer.
5648   (when (and gnus-dead-summary
5649              (buffer-name gnus-dead-summary))
5650     (save-excursion
5651       (set-buffer gnus-dead-summary)
5652       (when gnus-dead-summary-mode
5653         (kill-buffer (current-buffer)))))
5654   ;; Make this the current dead summary.
5655   (setq gnus-dead-summary (current-buffer))
5656   (gnus-dead-summary-mode 1)
5657   (let ((name (buffer-name)))
5658     (when (string-match "Summary" name)
5659       (rename-buffer
5660        (concat (substring name 0 (match-beginning 0)) "Dead "
5661                (substring name (match-beginning 0)))
5662        t)
5663       (bury-buffer))))
5664
5665 (defun gnus-kill-or-deaden-summary (buffer)
5666   "Kill or deaden the summary BUFFER."
5667   (save-excursion
5668     (when (and (buffer-name buffer)
5669                (not gnus-single-article-buffer))
5670       (save-excursion
5671         (set-buffer buffer)
5672         (gnus-kill-buffer gnus-article-buffer)
5673         (gnus-kill-buffer gnus-original-article-buffer)))
5674     (cond (gnus-kill-summary-on-exit
5675            (when (and gnus-use-trees
5676                       (gnus-buffer-exists-p buffer))
5677              (save-excursion
5678                (set-buffer buffer)
5679                (gnus-tree-close gnus-newsgroup-name)))
5680            (gnus-kill-buffer buffer))
5681           ((gnus-buffer-exists-p buffer)
5682            (save-excursion
5683              (set-buffer buffer)
5684              (gnus-deaden-summary))))))
5685
5686 (defun gnus-summary-wake-up-the-dead (&rest args)
5687   "Wake up the dead summary buffer."
5688   (interactive)
5689   (gnus-dead-summary-mode -1)
5690   (let ((name (buffer-name)))
5691     (when (string-match "Dead " name)
5692       (rename-buffer
5693        (concat (substring name 0 (match-beginning 0))
5694                (substring name (match-end 0)))
5695        t)))
5696   (gnus-message 3 "This dead summary is now alive again"))
5697
5698 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5699 (defun gnus-summary-fetch-faq (&optional faq-dir)
5700   "Fetch the FAQ for the current group.
5701 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5702 in."
5703   (interactive
5704    (list
5705     (when current-prefix-arg
5706       (completing-read
5707        "Faq dir: " (and (listp gnus-group-faq-directory)
5708                         (mapcar (lambda (file) (list file))
5709                                 gnus-group-faq-directory))))))
5710   (let (gnus-faq-buffer)
5711     (when (setq gnus-faq-buffer
5712                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5713       (gnus-configure-windows 'summary-faq))))
5714
5715 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5716 (defun gnus-summary-describe-group (&optional force)
5717   "Describe the current newsgroup."
5718   (interactive "P")
5719   (gnus-group-describe-group force gnus-newsgroup-name))
5720
5721 (defun gnus-summary-describe-briefly ()
5722   "Describe summary mode commands briefly."
5723   (interactive)
5724   (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")))
5725
5726 ;; Walking around group mode buffer from summary mode.
5727
5728 (defun gnus-summary-next-group (&optional no-article target-group backward)
5729   "Exit current newsgroup and then select next unread newsgroup.
5730 If prefix argument NO-ARTICLE is non-nil, no article is selected
5731 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5732 previous group instead."
5733   (interactive "P")
5734   ;; Stop pre-fetching.
5735   (gnus-async-halt-prefetch)
5736   (let ((current-group gnus-newsgroup-name)
5737         (current-buffer (current-buffer))
5738         entered)
5739     ;; First we semi-exit this group to update Xrefs and all variables.
5740     ;; We can't do a real exit, because the window conf must remain
5741     ;; the same in case the user is prompted for info, and we don't
5742     ;; want the window conf to change before that...
5743     (gnus-summary-exit t)
5744     (while (not entered)
5745       ;; Then we find what group we are supposed to enter.
5746       (set-buffer gnus-group-buffer)
5747       (gnus-group-jump-to-group current-group)
5748       (setq target-group
5749             (or target-group
5750                 (if (eq gnus-keep-same-level 'best)
5751                     (gnus-summary-best-group gnus-newsgroup-name)
5752                   (gnus-summary-search-group backward gnus-keep-same-level))))
5753       (if (not target-group)
5754           ;; There are no further groups, so we return to the group
5755           ;; buffer.
5756           (progn
5757             (gnus-message 5 "Returning to the group buffer")
5758             (setq entered t)
5759             (when (gnus-buffer-live-p current-buffer)
5760               (set-buffer current-buffer)
5761               (gnus-summary-exit))
5762             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5763         ;; We try to enter the target group.
5764         (gnus-group-jump-to-group target-group)
5765         (let ((unreads (gnus-group-group-unread)))
5766           (if (and (or (eq t unreads)
5767                        (and unreads (not (zerop unreads))))
5768                    (gnus-summary-read-group
5769                     target-group nil no-article
5770                     (and (buffer-name current-buffer) current-buffer)
5771                     nil backward))
5772               (setq entered t)
5773             (setq current-group target-group
5774                   target-group nil)))))))
5775
5776 (defun gnus-summary-prev-group (&optional no-article)
5777   "Exit current newsgroup and then select previous unread newsgroup.
5778 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5779   (interactive "P")
5780   (gnus-summary-next-group no-article nil t))
5781
5782 ;; Walking around summary lines.
5783
5784 (defun gnus-summary-first-subject (&optional unread undownloaded)
5785   "Go to the first unread subject.
5786 If UNREAD is non-nil, go to the first unread article.
5787 Returns the article selected or nil if there are no unread articles."
5788   (interactive "P")
5789   (prog1
5790       (cond
5791        ;; Empty summary.
5792        ((null gnus-newsgroup-data)
5793         (gnus-message 3 "No articles in the group")
5794         nil)
5795        ;; Pick the first article.
5796        ((not unread)
5797         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5798         (gnus-data-number (car gnus-newsgroup-data)))
5799        ;; No unread articles.
5800        ((null gnus-newsgroup-unreads)
5801         (gnus-message 3 "No more unread articles")
5802         nil)
5803        ;; Find the first unread article.
5804        (t
5805         (let ((data gnus-newsgroup-data))
5806           (while (and data
5807                       (and (not (and undownloaded
5808                                      (eq gnus-undownloaded-mark
5809                                          (gnus-data-mark (car data)))))
5810                            (not (gnus-data-unread-p (car data)))))
5811             (setq data (cdr data)))
5812           (when data
5813             (goto-char (gnus-data-pos (car data)))
5814             (gnus-data-number (car data))))))
5815     (gnus-summary-position-point)))
5816
5817 (defun gnus-summary-next-subject (n &optional unread dont-display)
5818   "Go to next N'th summary line.
5819 If N is negative, go to the previous N'th subject line.
5820 If UNREAD is non-nil, only unread articles are selected.
5821 The difference between N and the actual number of steps taken is
5822 returned."
5823   (interactive "p")
5824   (let ((backward (< n 0))
5825         (n (abs n)))
5826     (while (and (> n 0)
5827                 (if backward
5828                     (gnus-summary-find-prev unread)
5829                   (gnus-summary-find-next unread)))
5830       (unless (zerop (setq n (1- n)))
5831         (gnus-summary-show-thread)))
5832     (when (/= 0 n)
5833       (gnus-message 7 "No more%s articles"
5834                     (if unread " unread" "")))
5835     (unless dont-display
5836       (gnus-summary-recenter)
5837       (gnus-summary-position-point))
5838     n))
5839
5840 (defun gnus-summary-next-unread-subject (n)
5841   "Go to next N'th unread summary line."
5842   (interactive "p")
5843   (gnus-summary-next-subject n t))
5844
5845 (defun gnus-summary-prev-subject (n &optional unread)
5846   "Go to previous N'th summary line.
5847 If optional argument UNREAD is non-nil, only unread article is selected."
5848   (interactive "p")
5849   (gnus-summary-next-subject (- n) unread))
5850
5851 (defun gnus-summary-prev-unread-subject (n)
5852   "Go to previous N'th unread summary line."
5853   (interactive "p")
5854   (gnus-summary-next-subject (- n) t))
5855
5856 (defun gnus-summary-goto-subject (article &optional force silent)
5857   "Go the subject line of ARTICLE.
5858 If FORCE, also allow jumping to articles not currently shown."
5859   (interactive "nArticle number: ")
5860   (let ((b (point))
5861         (data (gnus-data-find article)))
5862     ;; We read in the article if we have to.
5863     (and (not data)
5864          force
5865          (gnus-summary-insert-subject
5866           article
5867           (if (or (numberp force) (vectorp force)) force)
5868           t)
5869          (setq data (gnus-data-find article)))
5870     (goto-char b)
5871     (if (not data)
5872         (progn
5873           (unless silent
5874             (gnus-message 3 "Can't find article %d" article))
5875           nil)
5876       (goto-char (gnus-data-pos data))
5877       (gnus-summary-position-point)
5878       article)))
5879
5880 ;; Walking around summary lines with displaying articles.
5881
5882 (defun gnus-summary-expand-window (&optional arg)
5883   "Make the summary buffer take up the entire Emacs frame.
5884 Given a prefix, will force an `article' buffer configuration."
5885   (interactive "P")
5886   (if arg
5887       (gnus-configure-windows 'article 'force)
5888     (gnus-configure-windows 'summary 'force)))
5889
5890 (defun gnus-summary-display-article (article &optional all-header)
5891   "Display ARTICLE in article buffer."
5892   (gnus-set-global-variables)
5893   (if (null article)
5894       nil
5895     (prog1
5896         (if gnus-summary-display-article-function
5897             (funcall gnus-summary-display-article-function article all-header)
5898           (gnus-article-prepare article all-header))
5899       (with-current-buffer gnus-article-buffer
5900         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5901              nil))
5902       (gnus-run-hooks 'gnus-select-article-hook)
5903       (when (and gnus-current-article
5904                  (not (zerop gnus-current-article)))
5905         (gnus-summary-goto-subject gnus-current-article))
5906       (gnus-summary-recenter)
5907       (when (and gnus-use-trees gnus-show-threads)
5908         (gnus-possibly-generate-tree article)
5909         (gnus-highlight-selected-tree article))
5910       ;; Successfully display article.
5911       (gnus-article-set-window-start
5912        (cdr (assq article gnus-newsgroup-bookmarks))))))
5913
5914 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5915   "Select the current article.
5916 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5917 non-nil, the article will be re-fetched even if it already present in
5918 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5919 be displayed."
5920   ;; Make sure we are in the summary buffer to work around bbdb bug.
5921   (unless (eq major-mode 'gnus-summary-mode)
5922     (set-buffer gnus-summary-buffer))
5923   (let ((article (or article (gnus-summary-article-number)))
5924         (all-headers (not (not all-headers))) ;Must be T or NIL.
5925         gnus-summary-display-article-function)
5926     (and (not pseudo)
5927          (gnus-summary-article-pseudo-p article)
5928          (error "This is a pseudo-article"))
5929     (save-excursion
5930       (set-buffer gnus-summary-buffer)
5931       (if (or (and gnus-single-article-buffer
5932                    (or (null gnus-current-article)
5933                        (null gnus-article-current)
5934                        (null (get-buffer gnus-article-buffer))
5935                        (not (eq article (cdr gnus-article-current)))
5936                        (not (equal (car gnus-article-current)
5937                                    gnus-newsgroup-name))))
5938               (and (not gnus-single-article-buffer)
5939                    (or (null gnus-current-article)
5940                        (not (eq gnus-current-article article))))
5941               force)
5942           ;; The requested article is different from the current article.
5943           (progn
5944             (gnus-summary-display-article article all-headers)
5945             (when (or all-headers gnus-show-all-headers)
5946               (gnus-article-show-all-headers))
5947             (gnus-article-set-window-start
5948              (cdr (assq article gnus-newsgroup-bookmarks)))
5949             article)
5950         (when (or all-headers gnus-show-all-headers)
5951           (gnus-article-show-all-headers))
5952         'old))))
5953
5954 (defun gnus-summary-set-current-mark (&optional current-mark)
5955   "Obsolete function."
5956   nil)
5957
5958 (defun gnus-summary-next-article (&optional unread subject backward push)
5959   "Select the next article.
5960 If UNREAD, only unread articles are selected.
5961 If SUBJECT, only articles with SUBJECT are selected.
5962 If BACKWARD, the previous article is selected instead of the next."
5963   (interactive "P")
5964   (cond
5965    ;; Is there such an article?
5966    ((and (gnus-summary-search-forward unread subject backward)
5967          (or (gnus-summary-display-article (gnus-summary-article-number))
5968              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5969     (gnus-summary-position-point))
5970    ;; If not, we try the first unread, if that is wanted.
5971    ((and subject
5972          gnus-auto-select-same
5973          (gnus-summary-first-unread-article))
5974     (gnus-summary-position-point)
5975     (gnus-message 6 "Wrapped"))
5976    ;; Try to get next/previous article not displayed in this group.
5977    ((and gnus-auto-extend-newsgroup
5978          (not unread) (not subject))
5979     (gnus-summary-goto-article
5980      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5981      nil (count-lines (point-min) (point))))
5982    ;; Go to next/previous group.
5983    (t
5984     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5985       (gnus-summary-jump-to-group gnus-newsgroup-name))
5986     (let ((cmd last-command-char)
5987           (point
5988            (save-excursion
5989              (set-buffer gnus-group-buffer)
5990              (point)))
5991           (group
5992            (if (eq gnus-keep-same-level 'best)
5993                (gnus-summary-best-group gnus-newsgroup-name)
5994              (gnus-summary-search-group backward gnus-keep-same-level))))
5995       ;; For some reason, the group window gets selected.  We change
5996       ;; it back.
5997       (select-window (get-buffer-window (current-buffer)))
5998       ;; Select next unread newsgroup automagically.
5999       (cond
6000        ((or (not gnus-auto-select-next)
6001             (not cmd))
6002         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6003        ((or (eq gnus-auto-select-next 'quietly)
6004             (and (eq gnus-auto-select-next 'slightly-quietly)
6005                  push)
6006             (and (eq gnus-auto-select-next 'almost-quietly)
6007                  (gnus-summary-last-article-p)))
6008         ;; Select quietly.
6009         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6010             (gnus-summary-exit)
6011           (gnus-message 7 "No more%s articles (%s)..."
6012                         (if unread " unread" "")
6013                         (if group (concat "selecting " group)
6014                           "exiting"))
6015           (gnus-summary-next-group nil group backward)))
6016        (t
6017         (when (gnus-key-press-event-p last-input-event)
6018           (gnus-summary-walk-group-buffer
6019            gnus-newsgroup-name cmd unread backward point))))))))
6020
6021 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6022   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6023                       (?\C-p (gnus-group-prev-unread-group 1))))
6024         (cursor-in-echo-area t)
6025         keve key group ended)
6026     (save-excursion
6027       (set-buffer gnus-group-buffer)
6028       (goto-char start)
6029       (setq group
6030             (if (eq gnus-keep-same-level 'best)
6031                 (gnus-summary-best-group gnus-newsgroup-name)
6032               (gnus-summary-search-group backward gnus-keep-same-level))))
6033     (while (not ended)
6034       (gnus-message
6035        5 "No more%s articles%s" (if unread " unread" "")
6036        (if (and group
6037                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6038            (format " (Type %s for %s [%s])"
6039                    (single-key-description cmd) group
6040                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6041          (format " (Type %s to exit %s)"
6042                  (single-key-description cmd)
6043                  gnus-newsgroup-name)))
6044       ;; Confirm auto selection.
6045       (setq key (car (setq keve (gnus-read-event-char))))
6046       (setq ended t)
6047       (cond
6048        ((assq key keystrokes)
6049         (let ((obuf (current-buffer)))
6050           (switch-to-buffer gnus-group-buffer)
6051           (when group
6052             (gnus-group-jump-to-group group))
6053           (eval (cadr (assq key keystrokes)))
6054           (setq group (gnus-group-group-name))
6055           (switch-to-buffer obuf))
6056         (setq ended nil))
6057        ((equal key cmd)
6058         (if (or (not group)
6059                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6060             (gnus-summary-exit)
6061           (gnus-summary-next-group nil group backward)))
6062        (t
6063         (push (cdr keve) unread-command-events))))))
6064
6065 (defun gnus-summary-next-unread-article ()
6066   "Select unread article after current one."
6067   (interactive)
6068   (gnus-summary-next-article
6069    (or (not (eq gnus-summary-goto-unread 'never))
6070        (gnus-summary-last-article-p (gnus-summary-article-number)))
6071    (and gnus-auto-select-same
6072         (gnus-summary-article-subject))))
6073
6074 (defun gnus-summary-prev-article (&optional unread subject)
6075   "Select the article after the current one.
6076 If UNREAD is non-nil, only unread articles are selected."
6077   (interactive "P")
6078   (gnus-summary-next-article unread subject t))
6079
6080 (defun gnus-summary-prev-unread-article ()
6081   "Select unread article before current one."
6082   (interactive)
6083   (gnus-summary-prev-article
6084    (or (not (eq gnus-summary-goto-unread 'never))
6085        (gnus-summary-first-article-p (gnus-summary-article-number)))
6086    (and gnus-auto-select-same
6087         (gnus-summary-article-subject))))
6088
6089 (defun gnus-summary-next-page (&optional lines circular)
6090   "Show next page of the selected article.
6091 If at the end of the current article, select the next article.
6092 LINES says how many lines should be scrolled up.
6093
6094 If CIRCULAR is non-nil, go to the start of the article instead of
6095 selecting the next article when reaching the end of the current
6096 article."
6097   (interactive "P")
6098   (setq gnus-summary-buffer (current-buffer))
6099   (gnus-set-global-variables)
6100   (let ((article (gnus-summary-article-number))
6101         (article-window (get-buffer-window gnus-article-buffer t))
6102         endp)
6103     ;; If the buffer is empty, we have no article.
6104     (unless article
6105       (error "No article to select"))
6106     (gnus-configure-windows 'article)
6107     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6108         (if (and (eq gnus-summary-goto-unread 'never)
6109                  (not (gnus-summary-last-article-p article)))
6110             (gnus-summary-next-article)
6111           (gnus-summary-next-unread-article))
6112       (if (or (null gnus-current-article)
6113               (null gnus-article-current)
6114               (/= article (cdr gnus-article-current))
6115               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6116           ;; Selected subject is different from current article's.
6117           (gnus-summary-display-article article)
6118         (when article-window
6119           (gnus-eval-in-buffer-window gnus-article-buffer
6120             (setq endp (gnus-article-next-page lines)))
6121           (when endp
6122             (cond (circular
6123                    (gnus-summary-beginning-of-article))
6124                   (lines
6125                    (gnus-message 3 "End of message"))
6126                   ((null lines)
6127                    (if (and (eq gnus-summary-goto-unread 'never)
6128                             (not (gnus-summary-last-article-p article)))
6129                        (gnus-summary-next-article)
6130                      (gnus-summary-next-unread-article))))))))
6131     (gnus-summary-recenter)
6132     (gnus-summary-position-point)))
6133
6134 (defun gnus-summary-prev-page (&optional lines move)
6135   "Show previous page of selected article.
6136 Argument LINES specifies lines to be scrolled down.
6137 If MOVE, move to the previous unread article if point is at
6138 the beginning of the buffer."
6139   (interactive "P")
6140   (let ((article (gnus-summary-article-number))
6141         (article-window (get-buffer-window gnus-article-buffer t))
6142         endp)
6143     (gnus-configure-windows 'article)
6144     (if (or (null gnus-current-article)
6145             (null gnus-article-current)
6146             (/= article (cdr gnus-article-current))
6147             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6148         ;; Selected subject is different from current article's.
6149         (gnus-summary-display-article article)
6150       (gnus-summary-recenter)
6151       (when article-window
6152         (gnus-eval-in-buffer-window gnus-article-buffer
6153           (setq endp (gnus-article-prev-page lines)))
6154         (when (and move endp)
6155           (cond (lines
6156                  (gnus-message 3 "Beginning of message"))
6157                 ((null lines)
6158                  (if (and (eq gnus-summary-goto-unread 'never)
6159                           (not (gnus-summary-first-article-p article)))
6160                      (gnus-summary-prev-article)
6161                    (gnus-summary-prev-unread-article))))))))
6162   (gnus-summary-position-point))
6163
6164 (defun gnus-summary-prev-page-or-article (&optional lines)
6165   "Show previous page of selected article.
6166 Argument LINES specifies lines to be scrolled down.
6167 If at the beginning of the article, go to the next article."
6168   (interactive "P")
6169   (gnus-summary-prev-page lines t))
6170
6171 (defun gnus-summary-scroll-up (lines)
6172   "Scroll up (or down) one line current article.
6173 Argument LINES specifies lines to be scrolled up (or down if negative)."
6174   (interactive "p")
6175   (gnus-configure-windows 'article)
6176   (gnus-summary-show-thread)
6177   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6178     (gnus-eval-in-buffer-window gnus-article-buffer
6179       (cond ((> lines 0)
6180              (when (gnus-article-next-page lines)
6181                (gnus-message 3 "End of message")))
6182             ((< lines 0)
6183              (gnus-article-prev-page (- lines))))))
6184   (gnus-summary-recenter)
6185   (gnus-summary-position-point))
6186
6187 (defun gnus-summary-scroll-down (lines)
6188   "Scroll down (or up) one line current article.
6189 Argument LINES specifies lines to be scrolled down (or up if negative)."
6190   (interactive "p")
6191   (gnus-summary-scroll-up (- lines)))
6192
6193 (defun gnus-summary-next-same-subject ()
6194   "Select next article which has the same subject as current one."
6195   (interactive)
6196   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6197
6198 (defun gnus-summary-prev-same-subject ()
6199   "Select previous article which has the same subject as current one."
6200   (interactive)
6201   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6202
6203 (defun gnus-summary-next-unread-same-subject ()
6204   "Select next unread article which has the same subject as current one."
6205   (interactive)
6206   (gnus-summary-next-article t (gnus-summary-article-subject)))
6207
6208 (defun gnus-summary-prev-unread-same-subject ()
6209   "Select previous unread article which has the same subject as current one."
6210   (interactive)
6211   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6212
6213 (defun gnus-summary-first-unread-article ()
6214   "Select the first unread article.
6215 Return nil if there are no unread articles."
6216   (interactive)
6217   (prog1
6218       (when (gnus-summary-first-subject t)
6219         (gnus-summary-show-thread)
6220         (gnus-summary-first-subject t)
6221         (gnus-summary-display-article (gnus-summary-article-number)))
6222     (gnus-summary-position-point)))
6223
6224 (defun gnus-summary-first-unread-subject ()
6225   "Place the point on the subject line of the first unread article.
6226 Return nil if there are no unread articles."
6227   (interactive)
6228   (prog1
6229       (when (gnus-summary-first-subject t)
6230         (gnus-summary-show-thread)
6231         (gnus-summary-first-subject t))
6232     (gnus-summary-position-point)))
6233
6234 (defun gnus-summary-first-article ()
6235   "Select the first article.
6236 Return nil if there are no articles."
6237   (interactive)
6238   (prog1
6239       (when (gnus-summary-first-subject)
6240         (gnus-summary-show-thread)
6241         (gnus-summary-first-subject)
6242         (gnus-summary-display-article (gnus-summary-article-number)))
6243     (gnus-summary-position-point)))
6244
6245 (defun gnus-summary-best-unread-article ()
6246   "Select the unread article with the highest score."
6247   (interactive)
6248   (let ((best -1000000)
6249         (data gnus-newsgroup-data)
6250         article score)
6251     (while data
6252       (and (gnus-data-unread-p (car data))
6253            (> (setq score
6254                     (gnus-summary-article-score (gnus-data-number (car data))))
6255               best)
6256            (setq best score
6257                  article (gnus-data-number (car data))))
6258       (setq data (cdr data)))
6259     (prog1
6260         (if article
6261             (gnus-summary-goto-article article)
6262           (error "No unread articles"))
6263       (gnus-summary-position-point))))
6264
6265 (defun gnus-summary-last-subject ()
6266   "Go to the last displayed subject line in the group."
6267   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6268     (when article
6269       (gnus-summary-goto-subject article))))
6270
6271 (defun gnus-summary-goto-article (article &optional all-headers force)
6272   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6273 If ALL-HEADERS is non-nil, no header lines are hidden.
6274 If FORCE, go to the article even if it isn't displayed.  If FORCE
6275 is a number, it is the line the article is to be displayed on."
6276   (interactive
6277    (list
6278     (completing-read
6279      "Article number or Message-ID: "
6280      (mapcar (lambda (number) (list (int-to-string number)))
6281              gnus-newsgroup-limit))
6282     current-prefix-arg
6283     t))
6284   (prog1
6285       (if (and (stringp article)
6286                (string-match "@" article))
6287           (gnus-summary-refer-article article)
6288         (when (stringp article)
6289           (setq article (string-to-number article)))
6290         (if (gnus-summary-goto-subject article force)
6291             (gnus-summary-display-article article all-headers)
6292           (gnus-message 4 "Couldn't go to article %s" article) nil))
6293     (gnus-summary-position-point)))
6294
6295 (defun gnus-summary-goto-last-article ()
6296   "Go to the previously read article."
6297   (interactive)
6298   (prog1
6299       (when gnus-last-article
6300         (gnus-summary-goto-article gnus-last-article nil t))
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-pop-article (number)
6304   "Pop one article off the history and go to the previous.
6305 NUMBER articles will be popped off."
6306   (interactive "p")
6307   (let (to)
6308     (setq gnus-newsgroup-history
6309           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6310     (if to
6311         (gnus-summary-goto-article (car to) nil t)
6312       (error "Article history empty")))
6313   (gnus-summary-position-point))
6314
6315 ;; Summary commands and functions for limiting the summary buffer.
6316
6317 (defun gnus-summary-limit-to-articles (n)
6318   "Limit the summary buffer to the next N articles.
6319 If not given a prefix, use the process marked articles instead."
6320   (interactive "P")
6321   (prog1
6322       (let ((articles (gnus-summary-work-articles n)))
6323         (setq gnus-newsgroup-processable nil)
6324         (gnus-summary-limit articles))
6325     (gnus-summary-position-point)))
6326
6327 (defun gnus-summary-pop-limit (&optional total)
6328   "Restore the previous limit.
6329 If given a prefix, remove all limits."
6330   (interactive "P")
6331   (when total
6332     (setq gnus-newsgroup-limits
6333           (list (mapcar (lambda (h) (mail-header-number h))
6334                         gnus-newsgroup-headers))))
6335   (unless gnus-newsgroup-limits
6336     (error "No limit to pop"))
6337   (prog1
6338       (gnus-summary-limit nil 'pop)
6339     (gnus-summary-position-point)))
6340
6341 (defun gnus-summary-limit-to-subject (subject &optional header)
6342   "Limit the summary buffer to articles that have subjects that match a regexp."
6343   (interactive "sLimit to subject (regexp): ")
6344   (unless header
6345     (setq header "subject"))
6346   (when (not (equal "" subject))
6347     (prog1
6348         (let ((articles (gnus-summary-find-matching
6349                          (or header "subject") subject 'all)))
6350           (unless articles
6351             (error "Found no matches for \"%s\"" subject))
6352           (gnus-summary-limit articles))
6353       (gnus-summary-position-point))))
6354
6355 (defun gnus-summary-limit-to-author (from)
6356   "Limit the summary buffer to articles that have authors that match a regexp."
6357   (interactive "sLimit to author (regexp): ")
6358   (gnus-summary-limit-to-subject from "from"))
6359
6360 (defun gnus-summary-limit-to-age (age &optional younger-p)
6361   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6362 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6363 articles that are younger than AGE days."
6364   (interactive
6365    (let ((younger current-prefix-arg)
6366          (days-got nil)
6367          days)
6368      (while (not days-got)
6369        (setq days (if younger
6370                       (read-string "Limit to articles within (in days): ")
6371                     (read-string "Limit to articles old than (in days): ")))
6372        (when (> (length days) 0)
6373          (setq days (read days)))
6374        (if (numberp days)
6375            (setq days-got t)
6376          (message "Please enter a number.")
6377          (sleep-for 1)))
6378      (list days younger)))
6379   (prog1
6380       (let ((data gnus-newsgroup-data)
6381             (cutoff (days-to-time age))
6382             articles d date is-younger)
6383         (while (setq d (pop data))
6384           (when (and (vectorp (gnus-data-header d))
6385                      (setq date (mail-header-date (gnus-data-header d))))
6386             (setq is-younger (time-less-p
6387                               (time-since (condition-case ()
6388                                               (date-to-time date)
6389                                             (error '(0 0))))
6390                               cutoff))
6391             (when (if younger-p
6392                       is-younger
6393                     (not is-younger))
6394               (push (gnus-data-number d) articles))))
6395         (gnus-summary-limit (nreverse articles)))
6396     (gnus-summary-position-point)))
6397
6398 (defun gnus-summary-limit-to-extra (header regexp)
6399   "Limit the summary buffer to articles that match an 'extra' header."
6400   (interactive
6401    (let ((header
6402           (intern
6403            (gnus-completing-read
6404             (symbol-name (car gnus-extra-headers))
6405             "Limit extra header:"
6406             (mapcar (lambda (x)
6407                       (cons (symbol-name x) x))
6408                     gnus-extra-headers)
6409             nil
6410             t))))
6411      (list header
6412            (read-string (format "Limit to header %s (regexp): " header)))))
6413   (when (not (equal "" regexp))
6414     (prog1
6415         (let ((articles (gnus-summary-find-matching
6416                          (cons 'extra header) regexp 'all)))
6417           (unless articles
6418             (error "Found no matches for \"%s\"" regexp))
6419           (gnus-summary-limit articles))
6420       (gnus-summary-position-point))))
6421
6422 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6423 (make-obsolete
6424  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6425
6426 (defun gnus-summary-limit-to-unread (&optional all)
6427   "Limit the summary buffer to articles that are not marked as read.
6428 If ALL is non-nil, limit strictly to unread articles."
6429   (interactive "P")
6430   (if all
6431       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6432     (gnus-summary-limit-to-marks
6433      ;; Concat all the marks that say that an article is read and have
6434      ;; those removed.
6435      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6436            gnus-killed-mark gnus-kill-file-mark
6437            gnus-low-score-mark gnus-expirable-mark
6438            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6439            gnus-duplicate-mark gnus-souped-mark)
6440      'reverse)))
6441
6442 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6443 (make-obsolete 'gnus-summary-delete-marked-with
6444                'gnus-summary-limit-exlude-marks)
6445
6446 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6447   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6448 If REVERSE, limit the summary buffer to articles that are marked
6449 with MARKS.  MARKS can either be a string of marks or a list of marks.
6450 Returns how many articles were removed."
6451   (interactive "sMarks: ")
6452   (gnus-summary-limit-to-marks marks t))
6453
6454 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6455   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6456 If REVERSE (the prefix), limit the summary buffer to articles that are
6457 not marked with MARKS.  MARKS can either be a string of marks or a
6458 list of marks.
6459 Returns how many articles were removed."
6460   (interactive "sMarks: \nP")
6461   (prog1
6462       (let ((data gnus-newsgroup-data)
6463             (marks (if (listp marks) marks
6464                      (append marks nil))) ; Transform to list.
6465             articles)
6466         (while data
6467           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6468                   (memq (gnus-data-mark (car data)) marks))
6469             (push (gnus-data-number (car data)) articles))
6470           (setq data (cdr data)))
6471         (gnus-summary-limit articles))
6472     (gnus-summary-position-point)))
6473
6474 (defun gnus-summary-limit-to-score (&optional score)
6475   "Limit to articles with score at or above SCORE."
6476   (interactive "P")
6477   (setq score (if score
6478                   (prefix-numeric-value score)
6479                 (or gnus-summary-default-score 0)))
6480   (let ((data gnus-newsgroup-data)
6481         articles)
6482     (while data
6483       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6484                 score)
6485         (push (gnus-data-number (car data)) articles))
6486       (setq data (cdr data)))
6487     (prog1
6488         (gnus-summary-limit articles)
6489       (gnus-summary-position-point))))
6490
6491 (defun gnus-summary-limit-include-thread (id)
6492   "Display all the hidden articles that in the current thread."
6493   (interactive (list (mail-header-id (gnus-summary-article-header))))
6494   (let ((articles (gnus-articles-in-thread
6495                    (gnus-id-to-thread (gnus-root-id id)))))
6496     (prog1
6497         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6498       (gnus-summary-position-point))))
6499
6500 (defun gnus-summary-limit-include-dormant ()
6501   "Display all the hidden articles that are marked as dormant.
6502 Note that this command only works on a subset of the articles currently
6503 fetched for this group."
6504   (interactive)
6505   (unless gnus-newsgroup-dormant
6506     (error "There are no dormant articles in this group"))
6507   (prog1
6508       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6509     (gnus-summary-position-point)))
6510
6511 (defun gnus-summary-limit-exclude-dormant ()
6512   "Hide all dormant articles."
6513   (interactive)
6514   (prog1
6515       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6516     (gnus-summary-position-point)))
6517
6518 (defun gnus-summary-limit-exclude-childless-dormant ()
6519   "Hide all dormant articles that have no children."
6520   (interactive)
6521   (let ((data (gnus-data-list t))
6522         articles d children)
6523     ;; Find all articles that are either not dormant or have
6524     ;; children.
6525     (while (setq d (pop data))
6526       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6527                 (and (setq children
6528                            (gnus-article-children (gnus-data-number d)))
6529                      (let (found)
6530                        (while children
6531                          (when (memq (car children) articles)
6532                            (setq children nil
6533                                  found t))
6534                          (pop children))
6535                        found)))
6536         (push (gnus-data-number d) articles)))
6537     ;; Do the limiting.
6538     (prog1
6539         (gnus-summary-limit articles)
6540       (gnus-summary-position-point))))
6541
6542 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6543   "Mark all unread excluded articles as read.
6544 If ALL, mark even excluded ticked and dormants as read."
6545   (interactive "P")
6546   (let ((articles (gnus-sorted-complement
6547                    (sort
6548                     (mapcar (lambda (h) (mail-header-number h))
6549                             gnus-newsgroup-headers)
6550                     '<)
6551                    (sort gnus-newsgroup-limit '<)))
6552         article)
6553     (setq gnus-newsgroup-unreads
6554           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6555     (if all
6556         (setq gnus-newsgroup-dormant nil
6557               gnus-newsgroup-marked nil
6558               gnus-newsgroup-reads
6559               (nconc
6560                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6561                gnus-newsgroup-reads))
6562       (while (setq article (pop articles))
6563         (unless (or (memq article gnus-newsgroup-dormant)
6564                     (memq article gnus-newsgroup-marked))
6565           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6566
6567 (defun gnus-summary-limit (articles &optional pop)
6568   (if pop
6569       ;; We pop the previous limit off the stack and use that.
6570       (setq articles (car gnus-newsgroup-limits)
6571             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6572     ;; We use the new limit, so we push the old limit on the stack.
6573     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6574   ;; Set the limit.
6575   (setq gnus-newsgroup-limit articles)
6576   (let ((total (length gnus-newsgroup-data))
6577         (data (gnus-data-find-list (gnus-summary-article-number)))
6578         (gnus-summary-mark-below nil)   ; Inhibit this.
6579         found)
6580     ;; This will do all the work of generating the new summary buffer
6581     ;; according to the new limit.
6582     (gnus-summary-prepare)
6583     ;; Hide any threads, possibly.
6584     (and gnus-show-threads
6585          gnus-thread-hide-subtree
6586          (gnus-summary-hide-all-threads))
6587     ;; Try to return to the article you were at, or one in the
6588     ;; neighborhood.
6589     (when data
6590       ;; We try to find some article after the current one.
6591       (while data
6592         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6593           (setq data nil
6594                 found t))
6595         (setq data (cdr data))))
6596     (unless found
6597       ;; If there is no data, that means that we were after the last
6598       ;; article.  The same goes when we can't find any articles
6599       ;; after the current one.
6600       (goto-char (point-max))
6601       (gnus-summary-find-prev))
6602     (gnus-set-mode-line 'summary)
6603     ;; We return how many articles were removed from the summary
6604     ;; buffer as a result of the new limit.
6605     (- total (length gnus-newsgroup-data))))
6606
6607 (defsubst gnus-invisible-cut-children (threads)
6608   (let ((num 0))
6609     (while threads
6610       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6611         (incf num))
6612       (pop threads))
6613     (< num 2)))
6614
6615 (defsubst gnus-cut-thread (thread)
6616   "Go forwards in the thread until we find an article that we want to display."
6617   (when (or (eq gnus-fetch-old-headers 'some)
6618             (eq gnus-fetch-old-headers 'invisible)
6619             (numberp gnus-fetch-old-headers)
6620             (eq gnus-build-sparse-threads 'some)
6621             (eq gnus-build-sparse-threads 'more))
6622     ;; Deal with old-fetched headers and sparse threads.
6623     (while (and
6624             thread
6625             (or
6626              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6627              (gnus-summary-article-ancient-p
6628               (mail-header-number (car thread))))
6629             (if (or (<= (length (cdr thread)) 1)
6630                     (eq gnus-fetch-old-headers 'invisible))
6631                 (setq gnus-newsgroup-limit
6632                       (delq (mail-header-number (car thread))
6633                             gnus-newsgroup-limit)
6634                       thread (cadr thread))
6635               (when (gnus-invisible-cut-children (cdr thread))
6636                 (let ((th (cdr thread)))
6637                   (while th
6638                     (if (memq (mail-header-number (caar th))
6639                               gnus-newsgroup-limit)
6640                         (setq thread (car th)
6641                               th nil)
6642                       (setq th (cdr th))))))))))
6643   thread)
6644
6645 (defun gnus-cut-threads (threads)
6646   "Cut off all uninteresting articles from the beginning of threads."
6647   (when (or (eq gnus-fetch-old-headers 'some)
6648             (eq gnus-fetch-old-headers 'invisible)
6649             (numberp gnus-fetch-old-headers)
6650             (eq gnus-build-sparse-threads 'some)
6651             (eq gnus-build-sparse-threads 'more))
6652     (let ((th threads))
6653       (while th
6654         (setcar th (gnus-cut-thread (car th)))
6655         (setq th (cdr th)))))
6656   ;; Remove nixed out threads.
6657   (delq nil threads))
6658
6659 (defun gnus-summary-initial-limit (&optional show-if-empty)
6660   "Figure out what the initial limit is supposed to be on group entry.
6661 This entails weeding out unwanted dormants, low-scored articles,
6662 fetch-old-headers verbiage, and so on."
6663   ;; Most groups have nothing to remove.
6664   (if (or gnus-inhibit-limiting
6665           (and (null gnus-newsgroup-dormant)
6666                (not (eq gnus-fetch-old-headers 'some))
6667                (not (numberp gnus-fetch-old-headers))
6668                (not (eq gnus-fetch-old-headers 'invisible))
6669                (null gnus-summary-expunge-below)
6670                (not (eq gnus-build-sparse-threads 'some))
6671                (not (eq gnus-build-sparse-threads 'more))
6672                (null gnus-thread-expunge-below)
6673                (not gnus-use-nocem)))
6674       ()                                ; Do nothing.
6675     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6676     (setq gnus-newsgroup-limit nil)
6677     (mapatoms
6678      (lambda (node)
6679        (unless (car (symbol-value node))
6680          ;; These threads have no parents -- they are roots.
6681          (let ((nodes (cdr (symbol-value node)))
6682                thread)
6683            (while nodes
6684              (if (and gnus-thread-expunge-below
6685                       (< (gnus-thread-total-score (car nodes))
6686                          gnus-thread-expunge-below))
6687                  (gnus-expunge-thread (pop nodes))
6688                (setq thread (pop nodes))
6689                (gnus-summary-limit-children thread))))))
6690      gnus-newsgroup-dependencies)
6691     ;; If this limitation resulted in an empty group, we might
6692     ;; pop the previous limit and use it instead.
6693     (when (and (not gnus-newsgroup-limit)
6694                show-if-empty)
6695       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6696     gnus-newsgroup-limit))
6697
6698 (defun gnus-summary-limit-children (thread)
6699   "Return 1 if this subthread is visible and 0 if it is not."
6700   ;; First we get the number of visible children to this thread.  This
6701   ;; is done by recursing down the thread using this function, so this
6702   ;; will really go down to a leaf article first, before slowly
6703   ;; working its way up towards the root.
6704   (when thread
6705     (let ((children
6706            (if (cdr thread)
6707                (apply '+ (mapcar 'gnus-summary-limit-children
6708                                  (cdr thread)))
6709              0))
6710           (number (mail-header-number (car thread)))
6711           score)
6712       (if (and
6713            (not (memq number gnus-newsgroup-marked))
6714            (or
6715             ;; If this article is dormant and has absolutely no visible
6716             ;; children, then this article isn't visible.
6717             (and (memq number gnus-newsgroup-dormant)
6718                  (zerop children))
6719             ;; If this is "fetch-old-headered" and there is no
6720             ;; visible children, then we don't want this article.
6721             (and (or (eq gnus-fetch-old-headers 'some)
6722                      (numberp gnus-fetch-old-headers))
6723                  (gnus-summary-article-ancient-p number)
6724                  (zerop children))
6725             ;; If this is "fetch-old-headered" and `invisible', then
6726             ;; we don't want this article.
6727             (and (eq gnus-fetch-old-headers 'invisible)
6728                  (gnus-summary-article-ancient-p number))
6729             ;; If this is a sparsely inserted article with no children,
6730             ;; we don't want it.
6731             (and (eq gnus-build-sparse-threads 'some)
6732                  (gnus-summary-article-sparse-p number)
6733                  (zerop children))
6734             ;; If we use expunging, and this article is really
6735             ;; low-scored, then we don't want this article.
6736             (when (and gnus-summary-expunge-below
6737                        (< (setq score
6738                                 (or (cdr (assq number gnus-newsgroup-scored))
6739                                     gnus-summary-default-score))
6740                           gnus-summary-expunge-below))
6741               ;; We increase the expunge-tally here, but that has
6742               ;; nothing to do with the limits, really.
6743               (incf gnus-newsgroup-expunged-tally)
6744               ;; We also mark as read here, if that's wanted.
6745               (when (and gnus-summary-mark-below
6746                          (< score gnus-summary-mark-below))
6747                 (setq gnus-newsgroup-unreads
6748                       (delq number gnus-newsgroup-unreads))
6749                 (if gnus-newsgroup-auto-expire
6750                     (push number gnus-newsgroup-expirable)
6751                   (push (cons number gnus-low-score-mark)
6752                         gnus-newsgroup-reads)))
6753               t)
6754             ;; Check NoCeM things.
6755             (if (and gnus-use-nocem
6756                      (gnus-nocem-unwanted-article-p
6757                       (mail-header-id (car thread))))
6758                 (progn
6759                   (setq gnus-newsgroup-unreads
6760                         (delq number gnus-newsgroup-unreads))
6761                   t))))
6762           ;; Nope, invisible article.
6763           0
6764         ;; Ok, this article is to be visible, so we add it to the limit
6765         ;; and return 1.
6766         (push number gnus-newsgroup-limit)
6767         1))))
6768
6769 (defun gnus-expunge-thread (thread)
6770   "Mark all articles in THREAD as read."
6771   (let* ((number (mail-header-number (car thread))))
6772     (incf gnus-newsgroup-expunged-tally)
6773     ;; We also mark as read here, if that's wanted.
6774     (setq gnus-newsgroup-unreads
6775           (delq number gnus-newsgroup-unreads))
6776     (if gnus-newsgroup-auto-expire
6777         (push number gnus-newsgroup-expirable)
6778       (push (cons number gnus-low-score-mark)
6779             gnus-newsgroup-reads)))
6780   ;; Go recursively through all subthreads.
6781   (mapcar 'gnus-expunge-thread (cdr thread)))
6782
6783 ;; Summary article oriented commands
6784
6785 (defun gnus-summary-refer-parent-article (n)
6786   "Refer parent article N times.
6787 If N is negative, go to ancestor -N instead.
6788 The difference between N and the number of articles fetched is returned."
6789   (interactive "p")
6790   (let ((skip 1)
6791         error header ref)
6792     (when (not (natnump n))
6793       (setq skip (abs n)
6794             n 1))
6795     (while (and (> n 0)
6796                 (not error))
6797       (setq header (gnus-summary-article-header))
6798       (if (and (eq (mail-header-number header)
6799                    (cdr gnus-article-current))
6800                (equal gnus-newsgroup-name
6801                       (car gnus-article-current)))
6802           ;; If we try to find the parent of the currently
6803           ;; displayed article, then we take a look at the actual
6804           ;; References header, since this is slightly more
6805           ;; reliable than the References field we got from the
6806           ;; server.
6807           (save-excursion
6808             (set-buffer gnus-original-article-buffer)
6809             (nnheader-narrow-to-headers)
6810             (unless (setq ref (message-fetch-field "references"))
6811               (setq ref (message-fetch-field "in-reply-to")))
6812             (widen))
6813         (setq ref
6814               ;; It's not the current article, so we take a bet on
6815               ;; the value we got from the server.
6816               (mail-header-references header)))
6817       (if (and ref
6818                (not (equal ref "")))
6819           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6820             (gnus-message 1 "Couldn't find parent"))
6821         (gnus-message 1 "No references in article %d"
6822                       (gnus-summary-article-number))
6823         (setq error t))
6824       (decf n))
6825     (gnus-summary-position-point)
6826     n))
6827
6828 (defun gnus-summary-refer-references ()
6829   "Fetch all articles mentioned in the References header.
6830 Return the number of articles fetched."
6831   (interactive)
6832   (let ((ref (mail-header-references (gnus-summary-article-header)))
6833         (current (gnus-summary-article-number))
6834         (n 0))
6835     (if (or (not ref)
6836             (equal ref ""))
6837         (error "No References in the current article")
6838       ;; For each Message-ID in the References header...
6839       (while (string-match "<[^>]*>" ref)
6840         (incf n)
6841         ;; ... fetch that article.
6842         (gnus-summary-refer-article
6843          (prog1 (match-string 0 ref)
6844            (setq ref (substring ref (match-end 0))))))
6845       (gnus-summary-goto-subject current)
6846       (gnus-summary-position-point)
6847       n)))
6848
6849 (defun gnus-summary-refer-thread (&optional limit)
6850   "Fetch all articles in the current thread.
6851 If LIMIT (the numerical prefix), fetch that many old headers instead
6852 of what's specified by the `gnus-refer-thread-limit' variable."
6853   (interactive "P")
6854   (let ((id (mail-header-id (gnus-summary-article-header)))
6855         (limit (if limit (prefix-numeric-value limit)
6856                  gnus-refer-thread-limit)))
6857     ;; We want to fetch LIMIT *old* headers, but we also have to
6858     ;; re-fetch all the headers in the current buffer, because many of
6859     ;; them may be undisplayed.  So we adjust LIMIT.
6860     (when (numberp limit)
6861       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6862     (unless (eq gnus-fetch-old-headers 'invisible)
6863       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6864       ;; Retrieve the headers and read them in.
6865       (if (eq (gnus-retrieve-headers
6866                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6867               'nov)
6868           (gnus-build-all-threads)
6869         (error "Can't fetch thread from backends that don't support NOV"))
6870       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6871     (gnus-summary-limit-include-thread id)))
6872
6873 (defun gnus-summary-refer-article (message-id)
6874   "Fetch an article specified by MESSAGE-ID."
6875   (interactive "sMessage-ID: ")
6876   (when (and (stringp message-id)
6877              (not (zerop (length message-id))))
6878     ;; Construct the correct Message-ID if necessary.
6879     ;; Suggested by tale@pawl.rpi.edu.
6880     (unless (string-match "^<" message-id)
6881       (setq message-id (concat "<" message-id)))
6882     (unless (string-match ">$" message-id)
6883       (setq message-id (concat message-id ">")))
6884     (let* ((header (gnus-id-to-header message-id))
6885            (sparse (and header
6886                         (gnus-summary-article-sparse-p
6887                          (mail-header-number header))
6888                         (memq (mail-header-number header)
6889                               gnus-newsgroup-limit)))
6890            number)
6891       (cond
6892        ;; If the article is present in the buffer we just go to it.
6893        ((and header
6894              (or (not (gnus-summary-article-sparse-p
6895                        (mail-header-number header)))
6896                  sparse))
6897         (prog1
6898             (gnus-summary-goto-article
6899              (mail-header-number header) nil t)
6900           (when sparse
6901             (gnus-summary-update-article (mail-header-number header)))))
6902        (t
6903         ;; We fetch the article.
6904         (catch 'found
6905           (dolist (gnus-override-method (gnus-refer-article-methods))
6906             (gnus-check-server gnus-override-method)
6907             ;; Fetch the header, and display the article.
6908             (when (setq number (gnus-summary-insert-subject message-id))
6909               (gnus-summary-select-article nil nil nil number)
6910               (throw 'found t)))
6911           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6912
6913 (defun gnus-refer-article-methods ()
6914   "Return a list of referrable methods."
6915   (cond
6916    ;; No method, so we default to current and native.
6917    ((null gnus-refer-article-method)
6918     (list gnus-current-select-method gnus-select-method))
6919    ;; Current.
6920    ((eq 'current gnus-refer-article-method)
6921     (list gnus-current-select-method))
6922    ;; List of select methods.
6923    ((not (stringp (cadr gnus-refer-article-method)))
6924     (let (out)
6925       (dolist (method gnus-refer-article-method)
6926         (push (if (eq 'current method)
6927                   gnus-current-select-method
6928                 method)
6929               out))
6930       (nreverse out)))
6931    ;; One single select method.
6932    (t
6933     (list gnus-refer-article-method))))
6934
6935 (defun gnus-summary-edit-parameters ()
6936   "Edit the group parameters of the current group."
6937   (interactive)
6938   (gnus-group-edit-group gnus-newsgroup-name 'params))
6939
6940 (defun gnus-summary-customize-parameters ()
6941   "Customize the group parameters of the current group."
6942   (interactive)
6943   (gnus-group-customize gnus-newsgroup-name))
6944
6945 (defun gnus-summary-enter-digest-group (&optional force)
6946   "Enter an nndoc group based on the current article.
6947 If FORCE, force a digest interpretation.  If not, try
6948 to guess what the document format is."
6949   (interactive "P")
6950   (let ((conf gnus-current-window-configuration))
6951     (save-excursion
6952       (gnus-summary-select-article))
6953     (setq gnus-current-window-configuration conf)
6954     (let* ((name (format "%s-%d"
6955                          (gnus-group-prefixed-name
6956                           gnus-newsgroup-name (list 'nndoc ""))
6957                          (save-excursion
6958                            (set-buffer gnus-summary-buffer)
6959                            gnus-current-article)))
6960            (ogroup gnus-newsgroup-name)
6961            (params (append (gnus-info-params (gnus-get-info ogroup))
6962                            (list (cons 'to-group ogroup))
6963                            (list (cons 'save-article-group ogroup))))
6964            (case-fold-search t)
6965            (buf (current-buffer))
6966            dig to-address)
6967       (save-excursion
6968         (set-buffer gnus-original-article-buffer)
6969         ;; Have the digest group inherit the main mail address of
6970         ;; the parent article.
6971         (when (setq to-address (or (message-fetch-field "reply-to")
6972                                    (message-fetch-field "from")))
6973           (setq params (append (list (cons 'to-address to-address)))))
6974         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6975         (insert-buffer-substring gnus-original-article-buffer)
6976         ;; Remove lines that may lead nndoc to misinterpret the
6977         ;; document type.
6978         (narrow-to-region
6979          (goto-char (point-min))
6980          (or (search-forward "\n\n" nil t) (point)))
6981         (goto-char (point-min))
6982         (delete-matching-lines "^Path:\\|^From ")
6983         (widen))
6984       (unwind-protect
6985           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6986                     (gnus-newsgroup-ephemeral-ignored-charsets
6987                      gnus-newsgroup-ignored-charsets))
6988                 (gnus-group-read-ephemeral-group
6989                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6990                               (nndoc-article-type
6991                                ,(if force 'mbox 'guess))) t))
6992               ;; Make all postings to this group go to the parent group.
6993               (nconc (gnus-info-params (gnus-get-info name))
6994                      params)
6995             ;; Couldn't select this doc group.
6996             (switch-to-buffer buf)
6997             (gnus-set-global-variables)
6998             (gnus-configure-windows 'summary)
6999             (gnus-message 3 "Article couldn't be entered?"))
7000         (kill-buffer dig)))))
7001
7002 (defun gnus-summary-read-document (n)
7003   "Open a new group based on the current article(s).
7004 This will allow you to read digests and other similar
7005 documents as newsgroups.
7006 Obeys the standard process/prefix convention."
7007   (interactive "P")
7008   (let* ((articles (gnus-summary-work-articles n))
7009          (ogroup gnus-newsgroup-name)
7010          (params (append (gnus-info-params (gnus-get-info ogroup))
7011                          (list (cons 'to-group ogroup))))
7012          article group egroup groups vgroup)
7013     (while (setq article (pop articles))
7014       (setq group (format "%s-%d" gnus-newsgroup-name article))
7015       (gnus-summary-remove-process-mark article)
7016       (when (gnus-summary-display-article article)
7017         (save-excursion
7018           (with-temp-buffer
7019             (insert-buffer-substring gnus-original-article-buffer)
7020             ;; Remove some headers that may lead nndoc to make
7021             ;; the wrong guess.
7022             (message-narrow-to-head)
7023             (goto-char (point-min))
7024             (delete-matching-lines "^\\(Path\\):\\|^From ")
7025             (widen)
7026             (if (setq egroup
7027                       (gnus-group-read-ephemeral-group
7028                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7029                                      (nndoc-article-type guess))
7030                        t nil t))
7031                 (progn
7032                   ;; Make all postings to this group go to the parent group.
7033                   (nconc (gnus-info-params (gnus-get-info egroup))
7034                          params)
7035                   (push egroup groups))
7036               ;; Couldn't select this doc group.
7037               (gnus-error 3 "Article couldn't be entered"))))))
7038     ;; Now we have selected all the documents.
7039     (cond
7040      ((not groups)
7041       (error "None of the articles could be interpreted as documents"))
7042      ((gnus-group-read-ephemeral-group
7043        (setq vgroup (format
7044                      "nnvirtual:%s-%s" gnus-newsgroup-name
7045                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7046        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7047        t
7048        (cons (current-buffer) 'summary)))
7049      (t
7050       (error "Couldn't select virtual nndoc group")))))
7051
7052 (defun gnus-summary-isearch-article (&optional regexp-p)
7053   "Do incremental search forward on the current article.
7054 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7055   (interactive "P")
7056   (let* ((gnus-inhibit-treatment t)
7057          (old (gnus-summary-select-article)))
7058     (gnus-configure-windows 'article)
7059     (gnus-eval-in-buffer-window gnus-article-buffer
7060       (save-restriction
7061         (widen)
7062         (when (eq 'old old)
7063           (gnus-article-show-all-headers))
7064         (goto-char (point-min))
7065         (isearch-forward regexp-p)))))
7066
7067 (defun gnus-summary-search-article-forward (regexp &optional backward)
7068   "Search for an article containing REGEXP forward.
7069 If BACKWARD, search backward instead."
7070   (interactive
7071    (list (read-string
7072           (format "Search article %s (regexp%s): "
7073                   (if current-prefix-arg "backward" "forward")
7074                   (if gnus-last-search-regexp
7075                       (concat ", default " gnus-last-search-regexp)
7076                     "")))
7077          current-prefix-arg))
7078   (if (string-equal regexp "")
7079       (setq regexp (or gnus-last-search-regexp ""))
7080     (setq gnus-last-search-regexp regexp))
7081   (if (gnus-summary-search-article regexp backward)
7082       (gnus-summary-show-thread)
7083     (error "Search failed: \"%s\"" regexp)))
7084
7085 (defun gnus-summary-search-article-backward (regexp)
7086   "Search for an article containing REGEXP backward."
7087   (interactive
7088    (list (read-string
7089           (format "Search article backward (regexp%s): "
7090                   (if gnus-last-search-regexp
7091                       (concat ", default " gnus-last-search-regexp)
7092                     "")))))
7093   (gnus-summary-search-article-forward regexp 'backward))
7094
7095 (eval-when-compile
7096   (defmacro gnus-summary-search-article-position-point (regexp backward)
7097     "Dehighlight the last matched text and goto the beginning position."
7098     (` (if (and gnus-summary-search-article-matched-data
7099                 (let ((text (caddr gnus-summary-search-article-matched-data))
7100                       (inhibit-read-only t)
7101                       buffer-read-only)
7102                   (delete-region
7103                    (goto-char (car gnus-summary-search-article-matched-data))
7104                    (cadr gnus-summary-search-article-matched-data))
7105                   (insert text)
7106                   (string-match (, regexp) text)))
7107            (if (, backward) (beginning-of-line) (end-of-line))
7108          (goto-char (if (, backward) (point-max) (point-min))))))
7109
7110   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7111     "Place point where X-Face image is displayed."
7112     (if (featurep 'xemacs)
7113         (` (let ((end (if (search-forward "\n\n" nil t)
7114                           (goto-char (1- (point)))
7115                         (point-min)))
7116                  extent)
7117              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7118              (unless (and (re-search-forward "^From:" end t)
7119                           (setq extent (extent-at (point)))
7120                           (extent-begin-glyph extent))
7121                (goto-char (, opoint)))))
7122       (` (let ((end (if (search-forward "\n\n" nil t)
7123                         (goto-char (1- (point)))
7124                       (point-min))))
7125            (goto-char
7126             (or (text-property-any (or (search-backward "\n\n" nil t)
7127                                        (point-min))
7128                                    end 'x-face-mule-bitmap-image t)
7129                 (, opoint)))))))
7130
7131   (defmacro gnus-summary-search-article-highlight-matched-text
7132     (backward treated x-face)
7133     "Highlight matched text in the function `gnus-summary-search-article'."
7134     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7135              (end (set-marker (make-marker) (match-end 0)))
7136              (inhibit-read-only t)
7137              buffer-read-only)
7138          (unless treated
7139            (let ((,@
7140                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7141                     (mapcar
7142                      (lambda (item) (setq items (delq item items)))
7143                      '(gnus-treat-buttonize
7144                        gnus-treat-fill-article
7145                        gnus-treat-fill-long-lines
7146                        gnus-treat-emphasize
7147                        gnus-treat-highlight-headers
7148                        gnus-treat-highlight-citation
7149                        gnus-treat-highlight-signature
7150                        gnus-treat-overstrike
7151                        gnus-treat-display-xface
7152                        gnus-treat-buttonize-head
7153                        gnus-treat-decode-article-as-default-mime-charset))
7154                     (static-if (featurep 'xemacs)
7155                         items
7156                       (cons '(x-face-mule-delete-x-face-field
7157                               (quote never))
7158                             items))))
7159                  (gnus-treat-display-xface
7160                   (when (, x-face) gnus-treat-display-xface)))
7161              (gnus-article-prepare-mime-display)))
7162          (goto-char (if (, backward) start end))
7163          (when (, x-face)
7164            (gnus-summary-search-article-highlight-goto-x-face (point)))
7165          (setq gnus-summary-search-article-matched-data
7166                (list start end (buffer-substring start end)))
7167          (unless (eq start end);; matched text has been deleted. :-<
7168            (put-text-property start end 'face
7169                               (or (find-face 'isearch)
7170                                   'secondary-selection))))))
7171   )
7172
7173 (defun gnus-summary-search-article (regexp &optional backward)
7174   "Search for an article containing REGEXP.
7175 Optional argument BACKWARD means do search for backward.
7176 `gnus-select-article-hook' is not called during the search."
7177   ;; We have to require this here to make sure that the following
7178   ;; dynamic binding isn't shadowed by autoloading.
7179   (require 'gnus-async)
7180   (require 'gnus-art)
7181   (let ((gnus-select-article-hook nil)  ;Disable hook.
7182         (gnus-article-display-hook nil)
7183         (gnus-article-prepare-hook nil)
7184         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7185         (gnus-use-article-prefetch nil)
7186         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7187         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7188         (sum (current-buffer))
7189         (found nil)
7190         point treated)
7191     (gnus-save-hidden-threads
7192       (static-if (featurep 'xemacs)
7193           (let ((gnus-inhibit-treatment t))
7194             (setq treated (eq 'old (gnus-summary-select-article)))
7195             (when (and treated
7196                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7197                                  (window-live-p (get-buffer-window
7198                                                  gnus-article-buffer t)))))
7199               (gnus-summary-select-article nil t)
7200               (setq treated nil)))
7201         (let ((gnus-inhibit-treatment t)
7202               (x-face-mule-delete-x-face-field 'never))
7203           (setq treated (eq 'old (gnus-summary-select-article)))
7204           (when (and treated
7205                      (not
7206                       (and (gnus-buffer-live-p gnus-article-buffer)
7207                            (window-live-p (get-buffer-window
7208                                            gnus-article-buffer t))
7209                            (or (not (string-match "^\\^X-Face:" regexp))
7210                                (with-current-buffer gnus-article-buffer
7211                                  gnus-summary-search-article-matched-data)))))
7212             (gnus-summary-select-article nil t)
7213             (setq treated nil))))
7214       (set-buffer gnus-article-buffer)
7215       (widen)
7216       (if treated
7217           (progn
7218             (gnus-article-show-all-headers)
7219             (gnus-summary-search-article-position-point regexp backward))
7220         (goto-char (if backward (point-max) (point-min))))
7221       (while (not found)
7222         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7223         (if (if backward
7224                 (re-search-backward regexp nil t)
7225               (re-search-forward regexp nil t))
7226             ;; We found the regexp.
7227             (progn
7228               (gnus-summary-search-article-highlight-matched-text
7229                backward treated (string-match "^\\^X-Face:" regexp))
7230               (setq found 'found)
7231               (forward-line
7232                (/ (- 2 (window-height
7233                         (get-buffer-window gnus-article-buffer t)))
7234                   2))
7235               (set-window-start
7236                (get-buffer-window (current-buffer))
7237                (point))
7238               (set-buffer sum)
7239               (setq point (point)))
7240           ;; We didn't find it, so we go to the next article.
7241           (set-buffer sum)
7242           (setq found 'not)
7243           (while (eq found 'not)
7244             (if (not (if backward (gnus-summary-find-prev)
7245                        (gnus-summary-find-next)))
7246                 ;; No more articles.
7247                 (setq found t)
7248               ;; Select the next article and adjust point.
7249               (unless (gnus-summary-article-sparse-p
7250                        (gnus-summary-article-number))
7251                 (setq found nil)
7252                 (let ((gnus-inhibit-treatment t))
7253                   (gnus-summary-select-article))
7254                 (setq treated nil)
7255                 (set-buffer gnus-article-buffer)
7256                 (widen)
7257                 (goto-char (if backward (point-max) (point-min))))))))
7258       (gnus-message 7 ""))
7259     ;; Return whether we found the regexp.
7260     (when (eq found 'found)
7261       (goto-char point)
7262       (gnus-summary-show-thread)
7263       (gnus-summary-goto-subject gnus-current-article)
7264       (gnus-summary-position-point)
7265       t)))
7266
7267 (defun gnus-summary-find-matching (header regexp &optional backward unread
7268                                           not-case-fold)
7269   "Return a list of all articles that match REGEXP on HEADER.
7270 The search stars on the current article and goes forwards unless
7271 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7272 If UNREAD is non-nil, only unread articles will
7273 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7274 in the comparisons."
7275   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7276                 (gnus-data-find-list
7277                  (gnus-summary-article-number) (gnus-data-list backward))))
7278         (case-fold-search (not not-case-fold))
7279         articles d func)
7280     (if (consp header)
7281         (if (eq (car header) 'extra)
7282             (setq func
7283                   `(lambda (h)
7284                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7285                          "")))
7286           (error "%s is an invalid header" header))
7287       (unless (fboundp (intern (concat "mail-header-" header)))
7288         (error "%s is not a valid header" header))
7289       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7290     (while data
7291       (setq d (car data))
7292       (and (or (not unread)             ; We want all articles...
7293                (gnus-data-unread-p d))  ; Or just unreads.
7294            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7295            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7296            (push (gnus-data-number d) articles)) ; Success!
7297       (setq data (cdr data)))
7298     (nreverse articles)))
7299
7300 (defun gnus-summary-execute-command (header regexp command &optional backward)
7301   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7302 If HEADER is an empty string (or nil), the match is done on the entire
7303 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7304   (interactive
7305    (list (let ((completion-ignore-case t))
7306            (completing-read
7307             "Header name: "
7308             (mapcar (lambda (string) (list string))
7309                     '("Number" "Subject" "From" "Lines" "Date"
7310                       "Message-ID" "Xref" "References" "Body"))
7311             nil 'require-match))
7312          (read-string "Regexp: ")
7313          (read-key-sequence "Command: ")
7314          current-prefix-arg))
7315   (when (equal header "Body")
7316     (setq header ""))
7317   ;; Hidden thread subtrees must be searched as well.
7318   (gnus-summary-show-all-threads)
7319   ;; We don't want to change current point nor window configuration.
7320   (save-excursion
7321     (save-window-excursion
7322       (gnus-message 6 "Executing %s..." (key-description command))
7323       ;; We'd like to execute COMMAND interactively so as to give arguments.
7324       (gnus-execute header regexp
7325                     `(call-interactively ',(key-binding command))
7326                     backward)
7327       (gnus-message 6 "Executing %s...done" (key-description command)))))
7328
7329 (defun gnus-summary-beginning-of-article ()
7330   "Scroll the article back to the beginning."
7331   (interactive)
7332   (gnus-summary-select-article)
7333   (gnus-configure-windows 'article)
7334   (gnus-eval-in-buffer-window gnus-article-buffer
7335     (widen)
7336     (goto-char (point-min))
7337     (when gnus-page-broken
7338       (gnus-narrow-to-page))))
7339
7340 (defun gnus-summary-end-of-article ()
7341   "Scroll to the end of the article."
7342   (interactive)
7343   (gnus-summary-select-article)
7344   (gnus-configure-windows 'article)
7345   (gnus-eval-in-buffer-window gnus-article-buffer
7346     (widen)
7347     (goto-char (point-max))
7348     (recenter -3)
7349     (when gnus-page-broken
7350       (gnus-narrow-to-page))))
7351
7352 (defun gnus-summary-print-article (&optional filename n)
7353   "Generate and print a PostScript image of the N next (mail) articles.
7354
7355 If N is negative, print the N previous articles.  If N is nil and articles
7356 have been marked with the process mark, print these instead.
7357
7358 If the optional first argument FILENAME is nil, send the image to the
7359 printer.  If FILENAME is a string, save the PostScript image in a file with
7360 that name.  If FILENAME is a number, prompt the user for the name of the file
7361 to save in."
7362   (interactive (list (ps-print-preprint current-prefix-arg)
7363                      current-prefix-arg))
7364   (dolist (article (gnus-summary-work-articles n))
7365     (gnus-summary-select-article nil nil 'pseudo article)
7366     (gnus-eval-in-buffer-window gnus-article-buffer
7367       (let ((buffer (generate-new-buffer " *print*")))
7368         (unwind-protect
7369             (progn
7370               (copy-to-buffer buffer (point-min) (point-max))
7371               (set-buffer buffer)
7372               (gnus-article-delete-invisible-text)
7373               (let ((ps-left-header
7374                      (list
7375                       (concat "("
7376                               (mail-header-subject gnus-current-headers) ")")
7377                       (concat "("
7378                               (mail-header-from gnus-current-headers) ")")))
7379                     (ps-right-header
7380                      (list
7381                       "/pagenumberstring load"
7382                       (concat "("
7383                               (mail-header-date gnus-current-headers) ")"))))
7384                 (gnus-run-hooks 'gnus-ps-print-hook)
7385                 (save-excursion
7386                   (ps-print-buffer-with-faces filename))))
7387           (kill-buffer buffer))))))
7388
7389 (defun gnus-summary-show-article (&optional arg)
7390   "Force re-fetching of the current article.
7391 If ARG (the prefix) is a number, show the article with the charset
7392 defined in `gnus-summary-show-article-charset-alist', or the charset
7393 inputed.
7394 If ARG (the prefix) is non-nil and not a number, show the raw article
7395 without any article massaging functions being run."
7396   (interactive "P")
7397   (cond
7398    ((numberp arg)
7399     (let ((gnus-newsgroup-charset
7400            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7401                (read-coding-system "Charset: ")))
7402           (gnus-newsgroup-ignored-charsets 'gnus-all))
7403       (gnus-summary-select-article nil 'force)))
7404    ((not arg)
7405     ;; Select the article the normal way.
7406     (gnus-summary-select-article nil 'force))
7407    (t
7408     ;; We have to require this here to make sure that the following
7409     ;; dynamic binding isn't shadowed by autoloading.
7410     (require 'gnus-async)
7411     (require 'gnus-art)
7412     ;; Bind the article treatment functions to nil.
7413     (let ((gnus-have-all-headers t)
7414           gnus-article-display-hook
7415           gnus-article-prepare-hook
7416           gnus-article-decode-hook
7417           gnus-break-pages
7418           gnus-show-mime)
7419       (gnus-summary-select-article nil 'force))))
7420   (gnus-summary-goto-subject gnus-current-article)
7421   (gnus-summary-position-point))
7422
7423 (defun gnus-summary-verbose-headers (&optional arg)
7424   "Toggle permanent full header display.
7425 If ARG is a positive number, turn header display on.
7426 If ARG is a negative number, turn header display off."
7427   (interactive "P")
7428   (setq gnus-show-all-headers
7429         (cond ((or (not (numberp arg))
7430                    (zerop arg))
7431                (not gnus-show-all-headers))
7432               ((natnump arg)
7433                t)))
7434   (gnus-summary-show-article))
7435
7436 (defun gnus-summary-toggle-header (&optional arg)
7437   "Show the headers if they are hidden, or hide them if they are shown.
7438 If ARG is a positive number, show the entire header.
7439 If ARG is a negative number, hide the unwanted header lines."
7440   (interactive "P")
7441   (save-excursion
7442     (set-buffer gnus-article-buffer)
7443     (save-restriction
7444       (let* ((buffer-read-only nil)
7445              (inhibit-point-motion-hooks t)
7446              hidden e)
7447         (setq hidden
7448               (if (numberp arg)
7449                   (>= arg 0)
7450                 (save-restriction
7451                   (article-narrow-to-head)
7452                   (gnus-article-hidden-text-p 'headers))))
7453         (goto-char (point-min))
7454         (when (search-forward "\n\n" nil t)
7455           (delete-region (point-min) (1- (point))))
7456         (goto-char (point-min))
7457         (save-excursion
7458           (set-buffer gnus-original-article-buffer)
7459           (goto-char (point-min))
7460           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7461         (insert-buffer-substring gnus-original-article-buffer 1 e)
7462         (save-restriction
7463           (narrow-to-region (point-min) (point))
7464           (article-decode-encoded-words)
7465           (if  hidden
7466               (let ((gnus-treat-hide-headers nil)
7467                     (gnus-treat-hide-boring-headers nil))
7468                 (setq gnus-article-wash-types
7469                       (delq 'headers gnus-article-wash-types))
7470                 (gnus-treat-article 'head))
7471             (gnus-treat-article 'head)))
7472         (gnus-set-mode-line 'article)))))
7473
7474 (defun gnus-summary-show-all-headers ()
7475   "Make all header lines visible."
7476   (interactive)
7477   (gnus-article-show-all-headers))
7478
7479 (defun gnus-summary-toggle-mime (&optional arg)
7480   "Toggle MIME processing.
7481 If ARG is a positive number, turn MIME processing on."
7482   (interactive "P")
7483   (setq gnus-show-mime
7484         (if (null arg)
7485             (not gnus-show-mime)
7486           (> (prefix-numeric-value arg) 0)))
7487   (gnus-summary-select-article t 'force))
7488
7489 (defun gnus-summary-caesar-message (&optional arg)
7490   "Caesar rotate the current article by 13.
7491 The numerical prefix specifies how many places to rotate each letter
7492 forward."
7493   (interactive "P")
7494   (gnus-summary-select-article)
7495   (let ((mail-header-separator ""))
7496     (gnus-eval-in-buffer-window gnus-article-buffer
7497       (save-restriction
7498         (widen)
7499         (let ((start (window-start))
7500               buffer-read-only)
7501           (message-caesar-buffer-body arg)
7502           (set-window-start (get-buffer-window (current-buffer)) start))))))
7503
7504 (defun gnus-summary-stop-page-breaking ()
7505   "Stop page breaking in the current article."
7506   (interactive)
7507   (gnus-summary-select-article)
7508   (gnus-eval-in-buffer-window gnus-article-buffer
7509     (widen)
7510     (when (gnus-visual-p 'page-marker)
7511       (let ((buffer-read-only nil))
7512         (gnus-remove-text-with-property 'gnus-prev)
7513         (gnus-remove-text-with-property 'gnus-next))
7514       (setq gnus-page-broken nil))))
7515
7516 (defun gnus-summary-move-article (&optional n to-newsgroup
7517                                             select-method action)
7518   "Move the current article to a different newsgroup.
7519 If N is a positive number, move the N next articles.
7520 If N is a negative number, move the N previous articles.
7521 If N is nil and any articles have been marked with the process mark,
7522 move those articles instead.
7523 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7524 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7525 re-spool using this method.
7526
7527 For this function to work, both the current newsgroup and the
7528 newsgroup that you want to move to have to support the `request-move'
7529 and `request-accept' functions.
7530
7531 ACTION can be either `move' (the default), `crosspost' or `copy'."
7532   (interactive "P")
7533   (unless action
7534     (setq action 'move))
7535   ;; Disable marking as read.
7536   (let (gnus-mark-article-hook)
7537     (save-window-excursion
7538       (gnus-summary-select-article)))
7539   ;; Check whether the source group supports the required functions.
7540   (cond ((and (eq action 'move)
7541               (not (gnus-check-backend-function
7542                     'request-move-article gnus-newsgroup-name)))
7543          (error "The current group does not support article moving"))
7544         ((and (eq action 'crosspost)
7545               (not (gnus-check-backend-function
7546                     'request-replace-article gnus-newsgroup-name)))
7547          (error "The current group does not support article editing")))
7548   (let ((articles (gnus-summary-work-articles n))
7549         (prefix (if (gnus-check-backend-function
7550                     'request-move-article gnus-newsgroup-name)
7551                     (gnus-group-real-prefix gnus-newsgroup-name)
7552                   ""))
7553         (names '((move "Move" "Moving")
7554                  (copy "Copy" "Copying")
7555                  (crosspost "Crosspost" "Crossposting")))
7556         (copy-buf (save-excursion
7557                     (nnheader-set-temp-buffer " *copy article*")))
7558         (default-marks gnus-article-mark-lists)
7559         (no-expire-marks (delete '(expirable . expire)
7560                                  (copy-sequence gnus-article-mark-lists)))
7561         art-group to-method new-xref article to-groups)
7562     (unless (assq action names)
7563       (error "Unknown action %s" action))
7564     ;; Read the newsgroup name.
7565     (when (and (not to-newsgroup)
7566                (not select-method))
7567       (setq to-newsgroup
7568             (gnus-read-move-group-name
7569              (cadr (assq action names))
7570              (symbol-value (intern (format "gnus-current-%s-group" action)))
7571              articles prefix))
7572       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7573     (setq to-method (or select-method
7574                         (gnus-server-to-method
7575                          (gnus-group-method to-newsgroup))))
7576     ;; Check the method we are to move this article to...
7577     (unless (gnus-check-backend-function
7578              'request-accept-article (car to-method))
7579       (error "%s does not support article copying" (car to-method)))
7580     (unless (gnus-check-server to-method)
7581       (error "Can't open server %s" (car to-method)))
7582     (gnus-message 6 "%s to %s: %s..."
7583                   (caddr (assq action names))
7584                   (or (car select-method) to-newsgroup) articles)
7585     (while articles
7586       (setq article (pop articles))
7587       (setq
7588        art-group
7589        (cond
7590         ;; Move the article.
7591         ((eq action 'move)
7592          ;; Remove this article from future suppression.
7593          (gnus-dup-unsuppress-article article)
7594          (gnus-request-move-article
7595           article                       ; Article to move
7596           gnus-newsgroup-name           ; From newsgroup
7597           (nth 1 (gnus-find-method-for-group
7598                   gnus-newsgroup-name)) ; Server
7599           (list 'gnus-request-accept-article
7600                 to-newsgroup (list 'quote select-method)
7601                 (not articles) t)       ; Accept form
7602           (not articles)))              ; Only save nov last time
7603         ;; Copy the article.
7604         ((eq action 'copy)
7605          (save-excursion
7606            (set-buffer copy-buf)
7607            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7608              (gnus-request-accept-article
7609               to-newsgroup select-method (not articles) t))))
7610         ;; Crosspost the article.
7611         ((eq action 'crosspost)
7612          (let ((xref (message-tokenize-header
7613                       (mail-header-xref (gnus-summary-article-header article))
7614                       " ")))
7615            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7616                                   ":" article))
7617            (unless xref
7618              (setq xref (list (system-name))))
7619            (setq new-xref
7620                  (concat
7621                   (mapconcat 'identity
7622                              (delete "Xref:" (delete new-xref xref))
7623                              " ")
7624                   " " new-xref))
7625            (save-excursion
7626              (set-buffer copy-buf)
7627              ;; First put the article in the destination group.
7628              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7629              (when (consp (setq art-group
7630                                 (gnus-request-accept-article
7631                                  to-newsgroup select-method (not articles))))
7632                (setq new-xref (concat new-xref " " (car art-group)
7633                                       ":" (cdr art-group)))
7634                ;; Now we have the new Xrefs header, so we insert
7635                ;; it and replace the new article.
7636                (nnheader-replace-header "Xref" new-xref)
7637                (gnus-request-replace-article
7638                 (cdr art-group) to-newsgroup (current-buffer))
7639                art-group))))))
7640       (cond
7641        ((not art-group)
7642         (gnus-message 1 "Couldn't %s article %s: %s"
7643                       (cadr (assq action names)) article
7644                       (nnheader-get-report (car to-method))))
7645        ((eq art-group 'junk)
7646         (when (eq action 'move)
7647           (gnus-summary-mark-article article gnus-canceled-mark)
7648           (gnus-message 4 "Deleted article %s" article)))
7649        (t
7650         (let* ((pto-group (gnus-group-prefixed-name
7651                            (car art-group) to-method))
7652                (entry
7653                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7654                (info (nth 2 entry))
7655                (to-group (gnus-info-group info))
7656                to-marks)
7657           ;; Update the group that has been moved to.
7658           (when (and info
7659                      (memq action '(move copy)))
7660             (unless (member to-group to-groups)
7661               (push to-group to-groups))
7662
7663             (unless (memq article gnus-newsgroup-unreads)
7664               (push 'read to-marks)
7665               (gnus-info-set-read
7666                info (gnus-add-to-range (gnus-info-read info)
7667                                        (list (cdr art-group)))))
7668
7669             ;; See whether the article is to be put in the cache.
7670             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7671                              default-marks
7672                            no-expire-marks))
7673                   (to-article (cdr art-group)))
7674
7675               ;; Enter the article into the cache in the new group,
7676               ;; if that is required.
7677               (when gnus-use-cache
7678                 (gnus-cache-possibly-enter-article
7679                  to-group to-article
7680                  (let ((header (copy-sequence
7681                                 (gnus-summary-article-header article))))
7682                    (mail-header-set-number header to-article)
7683                    header)
7684                  (memq article gnus-newsgroup-marked)
7685                  (memq article gnus-newsgroup-dormant)
7686                  (memq article gnus-newsgroup-unreads)))
7687
7688               (when gnus-preserve-marks
7689                 ;; Copy any marks over to the new group.
7690                 (when (and (equal to-group gnus-newsgroup-name)
7691                            (not (memq article gnus-newsgroup-unreads)))
7692                   ;; Mark this article as read in this group.
7693                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7694                   (setcdr (gnus-active to-group) to-article)
7695                   (setcdr gnus-newsgroup-active to-article))
7696
7697                 (while marks
7698                   (when (memq article (symbol-value
7699                                        (intern (format "gnus-newsgroup-%s"
7700                                                        (caar marks)))))
7701                     (push (cdar marks) to-marks)
7702                     ;; If the other group is the same as this group,
7703                     ;; then we have to add the mark to the list.
7704                     (when (equal to-group gnus-newsgroup-name)
7705                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7706                            (cons to-article
7707                                  (symbol-value
7708                                   (intern (format "gnus-newsgroup-%s"
7709                                                   (caar marks)))))))
7710                     ;; Copy the marks to other group.
7711                     (gnus-add-marked-articles
7712                      to-group (cdar marks) (list to-article) info))
7713                   (setq marks (cdr marks)))
7714
7715                 (gnus-request-set-mark to-group (list (list (list to-article)
7716                                                             'set
7717                                                             to-marks))))
7718
7719               (gnus-dribble-enter
7720                (concat "(gnus-group-set-info '"
7721                        (gnus-prin1-to-string (gnus-get-info to-group))
7722                        ")"))))
7723
7724           ;; Update the Xref header in this article to point to
7725           ;; the new crossposted article we have just created.
7726           (when (eq action 'crosspost)
7727             (save-excursion
7728               (set-buffer copy-buf)
7729               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7730               (nnheader-replace-header "Xref" new-xref)
7731               (gnus-request-replace-article
7732                article gnus-newsgroup-name (current-buffer)))))
7733
7734         ;;;!!!Why is this necessary?
7735         (set-buffer gnus-summary-buffer)
7736
7737         (gnus-summary-goto-subject article)
7738         (when (eq action 'move)
7739           (gnus-summary-mark-article article gnus-canceled-mark))))
7740       (gnus-summary-remove-process-mark article))
7741     ;; Re-activate all groups that have been moved to.
7742     (while to-groups
7743       (save-excursion
7744         (set-buffer gnus-group-buffer)
7745         (when (gnus-group-goto-group (car to-groups) t)
7746           (gnus-group-get-new-news-this-group 1 t))
7747         (pop to-groups)))
7748
7749     (gnus-kill-buffer copy-buf)
7750     (gnus-summary-position-point)
7751     (gnus-set-mode-line 'summary)))
7752
7753 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7754   "Move the current article to a different newsgroup.
7755 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7756 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7757 re-spool using this method."
7758   (interactive "P")
7759   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7760
7761 (defun gnus-summary-crosspost-article (&optional n)
7762   "Crosspost the current article to some other group."
7763   (interactive "P")
7764   (gnus-summary-move-article n nil nil 'crosspost))
7765
7766 (defcustom gnus-summary-respool-default-method nil
7767   "Default method for respooling an article.
7768 If nil, use to the current newsgroup method."
7769   :type '(choice (gnus-select-method :value (nnml ""))
7770                  (const nil))
7771   :group 'gnus-summary-mail)
7772
7773 (defun gnus-summary-respool-article (&optional n method)
7774   "Respool the current article.
7775 The article will be squeezed through the mail spooling process again,
7776 which means that it will be put in some mail newsgroup or other
7777 depending on `nnmail-split-methods'.
7778 If N is a positive number, respool the N next articles.
7779 If N is a negative number, respool the N previous articles.
7780 If N is nil and any articles have been marked with the process mark,
7781 respool those articles instead.
7782
7783 Respooling can be done both from mail groups and \"real\" newsgroups.
7784 In the former case, the articles in question will be moved from the
7785 current group into whatever groups they are destined to.  In the
7786 latter case, they will be copied into the relevant groups."
7787   (interactive
7788    (list current-prefix-arg
7789          (let* ((methods (gnus-methods-using 'respool))
7790                 (methname
7791                  (symbol-name (or gnus-summary-respool-default-method
7792                                   (car (gnus-find-method-for-group
7793                                         gnus-newsgroup-name)))))
7794                 (method
7795                  (gnus-completing-read
7796                   methname "What backend do you want to use when respooling?"
7797                   methods nil t nil 'gnus-mail-method-history))
7798                 ms)
7799            (cond
7800             ((zerop (length (setq ms (gnus-servers-using-backend
7801                                       (intern method)))))
7802              (list (intern method) ""))
7803             ((= 1 (length ms))
7804              (car ms))
7805             (t
7806              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7807                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7808                            ms-alist))))))))
7809   (unless method
7810     (error "No method given for respooling"))
7811   (if (assoc (symbol-name
7812               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7813              (gnus-methods-using 'respool))
7814       (gnus-summary-move-article n nil method)
7815     (gnus-summary-copy-article n nil method)))
7816
7817 (defun gnus-summary-import-article (file)
7818   "Import an arbitrary file into a mail newsgroup."
7819   (interactive "fImport file: ")
7820   (let ((group gnus-newsgroup-name)
7821         (now (current-time))
7822         atts lines)
7823     (unless (gnus-check-backend-function 'request-accept-article group)
7824       (error "%s does not support article importing" group))
7825     (or (file-readable-p file)
7826         (not (file-regular-p file))
7827         (error "Can't read %s" file))
7828     (save-excursion
7829       (set-buffer (gnus-get-buffer-create " *import file*"))
7830       (erase-buffer)
7831       (nnheader-insert-file-contents file)
7832       (goto-char (point-min))
7833       (unless (nnheader-article-p)
7834         ;; This doesn't look like an article, so we fudge some headers.
7835         (setq atts (file-attributes file)
7836               lines (count-lines (point-min) (point-max)))
7837         (insert "From: " (read-string "From: ") "\n"
7838                 "Subject: " (read-string "Subject: ") "\n"
7839                 "Date: " (message-make-date (nth 5 atts))
7840                 "\n"
7841                 "Message-ID: " (message-make-message-id) "\n"
7842                 "Lines: " (int-to-string lines) "\n"
7843                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7844       (gnus-request-accept-article group nil t)
7845       (kill-buffer (current-buffer)))))
7846
7847 (defun gnus-summary-article-posted-p ()
7848   "Say whether the current (mail) article is available from news as well.
7849 This will be the case if the article has both been mailed and posted."
7850   (interactive)
7851   (let ((id (mail-header-references (gnus-summary-article-header)))
7852         (gnus-override-method (car (gnus-refer-article-methods))))
7853     (if (gnus-request-head id "")
7854         (gnus-message 2 "The current message was found on %s"
7855                       gnus-override-method)
7856       (gnus-message 2 "The current message couldn't be found on %s"
7857                     gnus-override-method)
7858       nil)))
7859
7860 (defun gnus-summary-expire-articles (&optional now)
7861   "Expire all articles that are marked as expirable in the current group."
7862   (interactive)
7863   (when (gnus-check-backend-function
7864          'request-expire-articles gnus-newsgroup-name)
7865     ;; This backend supports expiry.
7866     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7867            (expirable (if total
7868                           (progn
7869                             ;; We need to update the info for
7870                             ;; this group for `gnus-list-of-read-articles'
7871                             ;; to give us the right answer.
7872                             (gnus-run-hooks 'gnus-exit-group-hook)
7873                             (gnus-summary-update-info)
7874                             (gnus-list-of-read-articles gnus-newsgroup-name))
7875                         (setq gnus-newsgroup-expirable
7876                               (sort gnus-newsgroup-expirable '<))))
7877            (expiry-wait (if now 'immediate
7878                           (gnus-group-find-parameter
7879                            gnus-newsgroup-name 'expiry-wait)))
7880            (nnmail-expiry-target
7881             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7882                 nnmail-expiry-target))
7883            es)
7884       (when expirable
7885         ;; There are expirable articles in this group, so we run them
7886         ;; through the expiry process.
7887         (gnus-message 6 "Expiring articles...")
7888         (unless (gnus-check-group gnus-newsgroup-name)
7889           (error "Can't open server for %s" gnus-newsgroup-name))
7890         ;; The list of articles that weren't expired is returned.
7891         (save-excursion
7892           (if expiry-wait
7893               (let ((nnmail-expiry-wait-function nil)
7894                     (nnmail-expiry-wait expiry-wait))
7895                 (setq es (gnus-request-expire-articles
7896                           expirable gnus-newsgroup-name)))
7897             (setq es (gnus-request-expire-articles
7898                       expirable gnus-newsgroup-name))))
7899         (unless total
7900           (setq gnus-newsgroup-expirable es))
7901         ;; We go through the old list of expirable, and mark all
7902         ;; really expired articles as nonexistent.
7903         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7904           (let ((gnus-use-cache nil))
7905             (while expirable
7906               (unless (memq (car expirable) es)
7907                 (when (gnus-data-find (car expirable))
7908                   (gnus-summary-mark-article
7909                    (car expirable) gnus-canceled-mark)))
7910               (setq expirable (cdr expirable)))))
7911         (gnus-message 6 "Expiring articles...done")))))
7912
7913 (defun gnus-summary-expire-articles-now ()
7914   "Expunge all expirable articles in the current group.
7915 This means that *all* articles that are marked as expirable will be
7916 deleted forever, right now."
7917   (interactive)
7918   (or gnus-expert-user
7919       (gnus-yes-or-no-p
7920        "Are you really, really, really sure you want to delete all these messages? ")
7921       (error "Phew!"))
7922   (gnus-summary-expire-articles t))
7923
7924 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7925 (defun gnus-summary-delete-article (&optional n)
7926   "Delete the N next (mail) articles.
7927 This command actually deletes articles.  This is not a marking
7928 command.  The article will disappear forever from your life, never to
7929 return.
7930 If N is negative, delete backwards.
7931 If N is nil and articles have been marked with the process mark,
7932 delete these instead."
7933   (interactive "P")
7934   (unless (gnus-check-backend-function 'request-expire-articles
7935                                        gnus-newsgroup-name)
7936     (error "The current newsgroup does not support article deletion"))
7937   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7938     (error "Couldn't open server"))
7939   ;; Compute the list of articles to delete.
7940   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7941         not-deleted)
7942     (if (and gnus-novice-user
7943              (not (gnus-yes-or-no-p
7944                    (format "Do you really want to delete %s forever? "
7945                            (if (> (length articles) 1)
7946                                (format "these %s articles" (length articles))
7947                              "this article")))))
7948         ()
7949       ;; Delete the articles.
7950       (setq not-deleted (gnus-request-expire-articles
7951                          articles gnus-newsgroup-name 'force))
7952       (while articles
7953         (gnus-summary-remove-process-mark (car articles))
7954         ;; The backend might not have been able to delete the article
7955         ;; after all.
7956         (unless (memq (car articles) not-deleted)
7957           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7958         (setq articles (cdr articles)))
7959       (when not-deleted
7960         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7961     (gnus-summary-position-point)
7962     (gnus-set-mode-line 'summary)
7963     not-deleted))
7964
7965 (defun gnus-summary-edit-article (&optional force)
7966   "Edit the current article.
7967 This will have permanent effect only in mail groups.
7968 If FORCE is non-nil, allow editing of articles even in read-only
7969 groups."
7970   (interactive "P")
7971   (save-excursion
7972     (set-buffer gnus-summary-buffer)
7973     (let ((mail-parse-charset gnus-newsgroup-charset)
7974           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7975       (gnus-set-global-variables)
7976       (when (and (not force)
7977                  (gnus-group-read-only-p))
7978         (error "The current newsgroup does not support article editing"))
7979       (gnus-summary-show-article t)
7980       (gnus-article-edit-article
7981        'ignore
7982        `(lambda (no-highlight)
7983           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7984                 (mail-parse-ignored-charsets
7985                  ',gnus-newsgroup-ignored-charsets))
7986             (gnus-summary-edit-article-done
7987              ,(or (mail-header-references gnus-current-headers) "")
7988              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7989
7990 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7991
7992 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7993                                                  no-highlight)
7994   "Make edits to the current article permanent."
7995   (interactive)
7996   ;; Replace the article.
7997   (let ((buf (current-buffer)))
7998     (with-temp-buffer
7999       (insert-buffer-substring buf)
8000       (if (and (not read-only)
8001                (not (gnus-request-replace-article
8002                      (cdr gnus-article-current) (car gnus-article-current)
8003                      (current-buffer) t)))
8004           (error "Couldn't replace article")
8005         ;; Update the summary buffer.
8006         (if (and references
8007                  (equal (message-tokenize-header references " ")
8008                         (message-tokenize-header
8009                          (or (message-fetch-field "references") "") " ")))
8010             ;; We only have to update this line.
8011             (save-excursion
8012               (save-restriction
8013                 (message-narrow-to-head)
8014                 (let ((head (buffer-string))
8015                       header)
8016                   (with-temp-buffer
8017                     (insert (format "211 %d Article retrieved.\n"
8018                                     (cdr gnus-article-current)))
8019                     (insert head)
8020                     (insert ".\n")
8021                     (let ((nntp-server-buffer (current-buffer)))
8022                       (setq header (car (gnus-get-newsgroup-headers
8023                                          (save-excursion
8024                                            (set-buffer gnus-summary-buffer)
8025                                            gnus-newsgroup-dependencies)
8026                                          t))))
8027                     (save-excursion
8028                       (set-buffer gnus-summary-buffer)
8029                       (gnus-data-set-header
8030                        (gnus-data-find (cdr gnus-article-current))
8031                        header)
8032                       (gnus-summary-update-article-line
8033                        (cdr gnus-article-current) header))))))
8034           ;; Update threads.
8035           (set-buffer (or buffer gnus-summary-buffer))
8036           (gnus-summary-update-article (cdr gnus-article-current)))
8037         ;; Prettify the article buffer again.
8038         (unless no-highlight
8039           (save-excursion
8040             (set-buffer gnus-article-buffer)
8041             ;;;!!! Fix this -- article should be rehighlighted.
8042             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8043             (set-buffer gnus-original-article-buffer)
8044             (gnus-request-article
8045              (cdr gnus-article-current)
8046              (car gnus-article-current) (current-buffer))))
8047         ;; Prettify the summary buffer line.
8048         (when (gnus-visual-p 'summary-highlight 'highlight)
8049           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8050
8051 (defun gnus-summary-edit-wash (key)
8052   "Perform editing command KEY in the article buffer."
8053   (interactive
8054    (list
8055     (progn
8056       (message "%s" (concat (this-command-keys) "- "))
8057       (read-char))))
8058   (message "")
8059   (gnus-summary-edit-article)
8060   (execute-kbd-macro (concat (this-command-keys) key))
8061   (gnus-article-edit-done))
8062
8063 ;;; Respooling
8064
8065 (defun gnus-summary-respool-query (&optional silent trace)
8066   "Query where the respool algorithm would put this article."
8067   (interactive)
8068   (let (gnus-mark-article-hook)
8069     (gnus-summary-select-article)
8070     (save-excursion
8071       (set-buffer gnus-original-article-buffer)
8072       (save-restriction
8073         (message-narrow-to-head)
8074         (let ((groups (nnmail-article-group 'identity trace)))
8075           (unless silent
8076             (if groups
8077                 (message "This message would go to %s"
8078                          (mapconcat 'car groups ", "))
8079               (message "This message would go to no groups"))
8080             groups))))))
8081
8082 (defun gnus-summary-respool-trace ()
8083   "Trace where the respool algorithm would put this article.
8084 Display a buffer showing all fancy splitting patterns which matched."
8085   (interactive)
8086   (gnus-summary-respool-query nil t))
8087
8088 ;; Summary marking commands.
8089
8090 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8091   "Mark articles which has the same subject as read, and then select the next.
8092 If UNMARK is positive, remove any kind of mark.
8093 If UNMARK is negative, tick articles."
8094   (interactive "P")
8095   (when unmark
8096     (setq unmark (prefix-numeric-value unmark)))
8097   (let ((count
8098          (gnus-summary-mark-same-subject
8099           (gnus-summary-article-subject) unmark)))
8100     ;; Select next unread article.  If auto-select-same mode, should
8101     ;; select the first unread article.
8102     (gnus-summary-next-article t (and gnus-auto-select-same
8103                                       (gnus-summary-article-subject)))
8104     (gnus-message 7 "%d article%s marked as %s"
8105                   count (if (= count 1) " is" "s are")
8106                   (if unmark "unread" "read"))))
8107
8108 (defun gnus-summary-kill-same-subject (&optional unmark)
8109   "Mark articles which has the same subject as read.
8110 If UNMARK is positive, remove any kind of mark.
8111 If UNMARK is negative, tick articles."
8112   (interactive "P")
8113   (when unmark
8114     (setq unmark (prefix-numeric-value unmark)))
8115   (let ((count
8116          (gnus-summary-mark-same-subject
8117           (gnus-summary-article-subject) unmark)))
8118     ;; If marked as read, go to next unread subject.
8119     (when (null unmark)
8120       ;; Go to next unread subject.
8121       (gnus-summary-next-subject 1 t))
8122     (gnus-message 7 "%d articles are marked as %s"
8123                   count (if unmark "unread" "read"))))
8124
8125 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8126   "Mark articles with same SUBJECT as read, and return marked number.
8127 If optional argument UNMARK is positive, remove any kinds of marks.
8128 If optional argument UNMARK is negative, mark articles as unread instead."
8129   (let ((count 1))
8130     (save-excursion
8131       (cond
8132        ((null unmark)                   ; Mark as read.
8133         (while (and
8134                 (progn
8135                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8136                   (gnus-summary-show-thread) t)
8137                 (gnus-summary-find-subject subject))
8138           (setq count (1+ count))))
8139        ((> unmark 0)                    ; Tick.
8140         (while (and
8141                 (progn
8142                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8143                   (gnus-summary-show-thread) t)
8144                 (gnus-summary-find-subject subject))
8145           (setq count (1+ count))))
8146        (t                               ; Mark as unread.
8147         (while (and
8148                 (progn
8149                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8150                   (gnus-summary-show-thread) t)
8151                 (gnus-summary-find-subject subject))
8152           (setq count (1+ count)))))
8153       (gnus-set-mode-line 'summary)
8154       ;; Return the number of marked articles.
8155       count)))
8156
8157 (defun gnus-summary-mark-as-processable (n &optional unmark)
8158   "Set the process mark on the next N articles.
8159 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8160 the process mark instead.  The difference between N and the actual
8161 number of articles marked is returned."
8162   (interactive "p")
8163   (let ((backward (< n 0))
8164         (n (abs n)))
8165     (while (and
8166             (> n 0)
8167             (if unmark
8168                 (gnus-summary-remove-process-mark
8169                  (gnus-summary-article-number))
8170               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8171             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8172       (setq n (1- n)))
8173     (when (/= 0 n)
8174       (gnus-message 7 "No more articles"))
8175     (gnus-summary-recenter)
8176     (gnus-summary-position-point)
8177     n))
8178
8179 (defun gnus-summary-unmark-as-processable (n)
8180   "Remove the process mark from the next N articles.
8181 If N is negative, unmark backward instead.  The difference between N and
8182 the actual number of articles unmarked is returned."
8183   (interactive "p")
8184   (gnus-summary-mark-as-processable n t))
8185
8186 (defun gnus-summary-unmark-all-processable ()
8187   "Remove the process mark from all articles."
8188   (interactive)
8189   (save-excursion
8190     (while gnus-newsgroup-processable
8191       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8192   (gnus-summary-position-point))
8193
8194 (defun gnus-summary-mark-as-expirable (n)
8195   "Mark N articles forward as expirable.
8196 If N is negative, mark backward instead.  The difference between N and
8197 the actual number of articles marked is returned."
8198   (interactive "p")
8199   (gnus-summary-mark-forward n gnus-expirable-mark))
8200
8201 (defun gnus-summary-mark-article-as-replied (article)
8202   "Mark ARTICLE replied and update the summary line."
8203   (push article gnus-newsgroup-replied)
8204   (let ((buffer-read-only nil))
8205     (when (gnus-summary-goto-subject article nil t)
8206       (gnus-summary-update-secondary-mark article))))
8207
8208 (defun gnus-summary-set-bookmark (article)
8209   "Set a bookmark in current article."
8210   (interactive (list (gnus-summary-article-number)))
8211   (when (or (not (get-buffer gnus-article-buffer))
8212             (not gnus-current-article)
8213             (not gnus-article-current)
8214             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8215     (error "No current article selected"))
8216   ;; Remove old bookmark, if one exists.
8217   (let ((old (assq article gnus-newsgroup-bookmarks)))
8218     (when old
8219       (setq gnus-newsgroup-bookmarks
8220             (delq old gnus-newsgroup-bookmarks))))
8221   ;; Set the new bookmark, which is on the form
8222   ;; (article-number . line-number-in-body).
8223   (push
8224    (cons article
8225          (save-excursion
8226            (set-buffer gnus-article-buffer)
8227            (count-lines
8228             (min (point)
8229                  (save-excursion
8230                    (goto-char (point-min))
8231                    (search-forward "\n\n" nil t)
8232                    (point)))
8233             (point))))
8234    gnus-newsgroup-bookmarks)
8235   (gnus-message 6 "A bookmark has been added to the current article."))
8236
8237 (defun gnus-summary-remove-bookmark (article)
8238   "Remove the bookmark from the current article."
8239   (interactive (list (gnus-summary-article-number)))
8240   ;; Remove old bookmark, if one exists.
8241   (let ((old (assq article gnus-newsgroup-bookmarks)))
8242     (if old
8243         (progn
8244           (setq gnus-newsgroup-bookmarks
8245                 (delq old gnus-newsgroup-bookmarks))
8246           (gnus-message 6 "Removed bookmark."))
8247       (gnus-message 6 "No bookmark in current article."))))
8248
8249 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8250 (defun gnus-summary-mark-as-dormant (n)
8251   "Mark N articles forward as dormant.
8252 If N is negative, mark backward instead.  The difference between N and
8253 the actual number of articles marked is returned."
8254   (interactive "p")
8255   (gnus-summary-mark-forward n gnus-dormant-mark))
8256
8257 (defun gnus-summary-set-process-mark (article)
8258   "Set the process mark on ARTICLE and update the summary line."
8259   (setq gnus-newsgroup-processable
8260         (cons article
8261               (delq article gnus-newsgroup-processable)))
8262   (when (gnus-summary-goto-subject article)
8263     (gnus-summary-show-thread)
8264     (gnus-summary-goto-subject article)
8265     (gnus-summary-update-secondary-mark article)))
8266
8267 (defun gnus-summary-remove-process-mark (article)
8268   "Remove the process mark from ARTICLE and update the summary line."
8269   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8270   (when (gnus-summary-goto-subject article)
8271     (gnus-summary-show-thread)
8272     (gnus-summary-goto-subject article)
8273     (gnus-summary-update-secondary-mark article)))
8274
8275 (defun gnus-summary-set-saved-mark (article)
8276   "Set the process mark on ARTICLE and update the summary line."
8277   (push article gnus-newsgroup-saved)
8278   (when (gnus-summary-goto-subject article)
8279     (gnus-summary-update-secondary-mark article)))
8280
8281 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8282   "Mark N articles as read forwards.
8283 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8284 The difference between N and the actual number of articles marked is
8285 returned.
8286 Iff NO-EXPIRE, auto-expiry will be inhibited."
8287   (interactive "p")
8288   (gnus-summary-show-thread)
8289   (let ((backward (< n 0))
8290         (gnus-summary-goto-unread
8291          (and gnus-summary-goto-unread
8292               (not (eq gnus-summary-goto-unread 'never))
8293               (not (memq mark (list gnus-unread-mark
8294                                     gnus-ticked-mark gnus-dormant-mark)))))
8295         (n (abs n))
8296         (mark (or mark gnus-del-mark)))
8297     (while (and (> n 0)
8298                 (gnus-summary-mark-article nil mark no-expire)
8299                 (zerop (gnus-summary-next-subject
8300                         (if backward -1 1)
8301                         (and gnus-summary-goto-unread
8302                              (not (eq gnus-summary-goto-unread 'never)))
8303                         t)))
8304       (setq n (1- n)))
8305     (when (/= 0 n)
8306       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8307     (gnus-summary-recenter)
8308     (gnus-summary-position-point)
8309     (gnus-set-mode-line 'summary)
8310     n))
8311
8312 (defun gnus-summary-mark-article-as-read (mark)
8313   "Mark the current article quickly as read with MARK."
8314   (let ((article (gnus-summary-article-number)))
8315     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8316     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8317     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8318     (push (cons article mark) gnus-newsgroup-reads)
8319     ;; Possibly remove from cache, if that is used.
8320     (when gnus-use-cache
8321       (gnus-cache-enter-remove-article article))
8322     ;; Allow the backend to change the mark.
8323     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8324     ;; Check for auto-expiry.
8325     (when (and gnus-newsgroup-auto-expire
8326                (memq mark gnus-auto-expirable-marks))
8327       (setq mark gnus-expirable-mark)
8328       ;; Let the backend know about the mark change.
8329       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8330       (push article gnus-newsgroup-expirable))
8331     ;; Set the mark in the buffer.
8332     (gnus-summary-update-mark mark 'unread)
8333     t))
8334
8335 (defun gnus-summary-mark-article-as-unread (mark)
8336   "Mark the current article quickly as unread with MARK."
8337   (let* ((article (gnus-summary-article-number))
8338          (old-mark (gnus-summary-article-mark article)))
8339     ;; Allow the backend to change the mark.
8340     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8341     (if (eq mark old-mark)
8342         t
8343       (if (<= article 0)
8344           (progn
8345             (gnus-error 1 "Can't mark negative article numbers")
8346             nil)
8347         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8348         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8349         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8350         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8351         (cond ((= mark gnus-ticked-mark)
8352                (push article gnus-newsgroup-marked))
8353               ((= mark gnus-dormant-mark)
8354                (push article gnus-newsgroup-dormant))
8355               (t
8356                (push article gnus-newsgroup-unreads)))
8357         (gnus-pull article gnus-newsgroup-reads)
8358
8359         ;; See whether the article is to be put in the cache.
8360         (and gnus-use-cache
8361              (vectorp (gnus-summary-article-header article))
8362              (save-excursion
8363                (gnus-cache-possibly-enter-article
8364                 gnus-newsgroup-name article
8365                 (gnus-summary-article-header article)
8366                 (= mark gnus-ticked-mark)
8367                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8368
8369         ;; Fix the mark.
8370         (gnus-summary-update-mark mark 'unread)
8371         t))))
8372
8373 (defun gnus-summary-mark-article (&optional article mark no-expire)
8374   "Mark ARTICLE with MARK.  MARK can be any character.
8375 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8376 `??' (dormant) and `?E' (expirable).
8377 If MARK is nil, then the default character `?r' is used.
8378 If ARTICLE is nil, then the article on the current line will be
8379 marked.
8380 Iff NO-EXPIRE, auto-expiry will be inhibited."
8381   ;; The mark might be a string.
8382   (when (stringp mark)
8383     (setq mark (aref mark 0)))
8384   ;; If no mark is given, then we check auto-expiring.
8385   (when (null mark)
8386     (setq mark gnus-del-mark))
8387   (when (and (not no-expire)
8388              gnus-newsgroup-auto-expire
8389              (memq mark gnus-auto-expirable-marks))
8390     (setq mark gnus-expirable-mark))
8391   (let ((article (or article (gnus-summary-article-number)))
8392         (old-mark (gnus-summary-article-mark article)))
8393     ;; Allow the backend to change the mark.
8394     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8395     (if (eq mark old-mark)
8396         t
8397       (unless article
8398         (error "No article on current line"))
8399       (if (not (if (or (= mark gnus-unread-mark)
8400                        (= mark gnus-ticked-mark)
8401                        (= mark gnus-dormant-mark))
8402                    (gnus-mark-article-as-unread article mark)
8403                  (gnus-mark-article-as-read article mark)))
8404           t
8405         ;; See whether the article is to be put in the cache.
8406         (and gnus-use-cache
8407              (not (= mark gnus-canceled-mark))
8408              (vectorp (gnus-summary-article-header article))
8409              (save-excursion
8410                (gnus-cache-possibly-enter-article
8411                 gnus-newsgroup-name article
8412                 (gnus-summary-article-header article)
8413                 (= mark gnus-ticked-mark)
8414                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8415
8416         (when (gnus-summary-goto-subject article nil t)
8417           (let ((buffer-read-only nil))
8418             (gnus-summary-show-thread)
8419             ;; Fix the mark.
8420             (gnus-summary-update-mark mark 'unread)
8421             t))))))
8422
8423 (defun gnus-summary-update-secondary-mark (article)
8424   "Update the secondary (read, process, cache) mark."
8425   (gnus-summary-update-mark
8426    (cond ((memq article gnus-newsgroup-processable)
8427           gnus-process-mark)
8428          ((memq article gnus-newsgroup-cached)
8429           gnus-cached-mark)
8430          ((memq article gnus-newsgroup-replied)
8431           gnus-replied-mark)
8432          ((memq article gnus-newsgroup-saved)
8433           gnus-saved-mark)
8434          (t gnus-unread-mark))
8435    'replied)
8436   (when (gnus-visual-p 'summary-highlight 'highlight)
8437     (gnus-run-hooks 'gnus-summary-update-hook))
8438   t)
8439
8440 (defun gnus-summary-update-mark (mark type)
8441   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8442         (buffer-read-only nil))
8443     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8444     (when forward
8445       (when (looking-at "\r")
8446         (incf forward))
8447       (when (<= (+ forward (point)) (point-max))
8448         ;; Go to the right position on the line.
8449         (goto-char (+ forward (point)))
8450         ;; Replace the old mark with the new mark.
8451         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8452         ;; Optionally update the marks by some user rule.
8453         (when (eq type 'unread)
8454           (gnus-data-set-mark
8455            (gnus-data-find (gnus-summary-article-number)) mark)
8456           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8457
8458 (defun gnus-mark-article-as-read (article &optional mark)
8459   "Enter ARTICLE in the pertinent lists and remove it from others."
8460   ;; Make the article expirable.
8461   (let ((mark (or mark gnus-del-mark)))
8462     (if (= mark gnus-expirable-mark)
8463         (push article gnus-newsgroup-expirable)
8464       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8465     ;; Remove from unread and marked lists.
8466     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8467     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8468     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8469     (push (cons article mark) gnus-newsgroup-reads)
8470     ;; Possibly remove from cache, if that is used.
8471     (when gnus-use-cache
8472       (gnus-cache-enter-remove-article article))
8473     t))
8474
8475 (defun gnus-mark-article-as-unread (article &optional mark)
8476   "Enter ARTICLE in the pertinent lists and remove it from others."
8477   (let ((mark (or mark gnus-ticked-mark)))
8478     (if (<= article 0)
8479         (progn
8480           (gnus-error 1 "Can't mark negative article numbers")
8481           nil)
8482       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8483             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8484             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8485             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8486
8487       ;; Unsuppress duplicates?
8488       (when gnus-suppress-duplicates
8489         (gnus-dup-unsuppress-article article))
8490
8491       (cond ((= mark gnus-ticked-mark)
8492              (push article gnus-newsgroup-marked))
8493             ((= mark gnus-dormant-mark)
8494              (push article gnus-newsgroup-dormant))
8495             (t
8496              (push article gnus-newsgroup-unreads)))
8497       (gnus-pull article gnus-newsgroup-reads)
8498       t)))
8499
8500 (defalias 'gnus-summary-mark-as-unread-forward
8501   'gnus-summary-tick-article-forward)
8502 (make-obsolete 'gnus-summary-mark-as-unread-forward
8503                'gnus-summary-tick-article-forward)
8504 (defun gnus-summary-tick-article-forward (n)
8505   "Tick N articles forwards.
8506 If N is negative, tick backwards instead.
8507 The difference between N and the number of articles ticked is returned."
8508   (interactive "p")
8509   (gnus-summary-mark-forward n gnus-ticked-mark))
8510
8511 (defalias 'gnus-summary-mark-as-unread-backward
8512   'gnus-summary-tick-article-backward)
8513 (make-obsolete 'gnus-summary-mark-as-unread-backward
8514                'gnus-summary-tick-article-backward)
8515 (defun gnus-summary-tick-article-backward (n)
8516   "Tick N articles backwards.
8517 The difference between N and the number of articles ticked is returned."
8518   (interactive "p")
8519   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8520
8521 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8522 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8523 (defun gnus-summary-tick-article (&optional article clear-mark)
8524   "Mark current article as unread.
8525 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8526 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8527   (interactive)
8528   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8529                                        gnus-ticked-mark)))
8530
8531 (defun gnus-summary-mark-as-read-forward (n)
8532   "Mark N articles as read forwards.
8533 If N is negative, mark backwards instead.
8534 The difference between N and the actual number of articles marked is
8535 returned."
8536   (interactive "p")
8537   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8538
8539 (defun gnus-summary-mark-as-read-backward (n)
8540   "Mark the N articles as read backwards.
8541 The difference between N and the actual number of articles marked is
8542 returned."
8543   (interactive "p")
8544   (gnus-summary-mark-forward
8545    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8546
8547 (defun gnus-summary-mark-as-read (&optional article mark)
8548   "Mark current article as read.
8549 ARTICLE specifies the article to be marked as read.
8550 MARK specifies a string to be inserted at the beginning of the line."
8551   (gnus-summary-mark-article article mark))
8552
8553 (defun gnus-summary-clear-mark-forward (n)
8554   "Clear marks from N articles forward.
8555 If N is negative, clear backward instead.
8556 The difference between N and the number of marks cleared is returned."
8557   (interactive "p")
8558   (gnus-summary-mark-forward n gnus-unread-mark))
8559
8560 (defun gnus-summary-clear-mark-backward (n)
8561   "Clear marks from N articles backward.
8562 The difference between N and the number of marks cleared is returned."
8563   (interactive "p")
8564   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8565
8566 (defun gnus-summary-mark-unread-as-read ()
8567   "Intended to be used by `gnus-summary-mark-article-hook'."
8568   (when (memq gnus-current-article gnus-newsgroup-unreads)
8569     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8570
8571 (defun gnus-summary-mark-read-and-unread-as-read ()
8572   "Intended to be used by `gnus-summary-mark-article-hook'."
8573   (let ((mark (gnus-summary-article-mark)))
8574     (when (or (gnus-unread-mark-p mark)
8575               (gnus-read-mark-p mark))
8576       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8577
8578 (defun gnus-summary-mark-region-as-read (point mark all)
8579   "Mark all unread articles between point and mark as read.
8580 If given a prefix, mark all articles between point and mark as read,
8581 even ticked and dormant ones."
8582   (interactive "r\nP")
8583   (save-excursion
8584     (let (article)
8585       (goto-char point)
8586       (beginning-of-line)
8587       (while (and
8588               (< (point) mark)
8589               (progn
8590                 (when (or all
8591                           (memq (setq article (gnus-summary-article-number))
8592                                 gnus-newsgroup-unreads))
8593                   (gnus-summary-mark-article article gnus-del-mark))
8594                 t)
8595               (gnus-summary-find-next))))))
8596
8597 (defun gnus-summary-mark-below (score mark)
8598   "Mark articles with score less than SCORE with MARK."
8599   (interactive "P\ncMark: ")
8600   (setq score (if score
8601                   (prefix-numeric-value score)
8602                 (or gnus-summary-default-score 0)))
8603   (save-excursion
8604     (set-buffer gnus-summary-buffer)
8605     (goto-char (point-min))
8606     (while
8607         (progn
8608           (and (< (gnus-summary-article-score) score)
8609                (gnus-summary-mark-article nil mark))
8610           (gnus-summary-find-next)))))
8611
8612 (defun gnus-summary-kill-below (&optional score)
8613   "Mark articles with score below SCORE as read."
8614   (interactive "P")
8615   (gnus-summary-mark-below score gnus-killed-mark))
8616
8617 (defun gnus-summary-clear-above (&optional score)
8618   "Clear all marks from articles with score above SCORE."
8619   (interactive "P")
8620   (gnus-summary-mark-above score gnus-unread-mark))
8621
8622 (defun gnus-summary-tick-above (&optional score)
8623   "Tick all articles with score above SCORE."
8624   (interactive "P")
8625   (gnus-summary-mark-above score gnus-ticked-mark))
8626
8627 (defun gnus-summary-mark-above (score mark)
8628   "Mark articles with score over SCORE with MARK."
8629   (interactive "P\ncMark: ")
8630   (setq score (if score
8631                   (prefix-numeric-value score)
8632                 (or gnus-summary-default-score 0)))
8633   (save-excursion
8634     (set-buffer gnus-summary-buffer)
8635     (goto-char (point-min))
8636     (while (and (progn
8637                   (when (> (gnus-summary-article-score) score)
8638                     (gnus-summary-mark-article nil mark))
8639                   t)
8640                 (gnus-summary-find-next)))))
8641
8642 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8643 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8644 (defun gnus-summary-limit-include-expunged (&optional no-error)
8645   "Display all the hidden articles that were expunged for low scores."
8646   (interactive)
8647   (let ((buffer-read-only nil))
8648     (let ((scored gnus-newsgroup-scored)
8649           headers h)
8650       (while scored
8651         (unless (gnus-summary-goto-subject (caar scored))
8652           (and (setq h (gnus-summary-article-header (caar scored)))
8653                (< (cdar scored) gnus-summary-expunge-below)
8654                (push h headers)))
8655         (setq scored (cdr scored)))
8656       (if (not headers)
8657           (when (not no-error)
8658             (error "No expunged articles hidden"))
8659         (goto-char (point-min))
8660         (gnus-summary-prepare-unthreaded (nreverse headers))
8661         (goto-char (point-min))
8662         (gnus-summary-position-point)
8663         t))))
8664
8665 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8666   "Mark all unread articles in this newsgroup as read.
8667 If prefix argument ALL is non-nil, ticked and dormant articles will
8668 also be marked as read.
8669 If QUIETLY is non-nil, no questions will be asked.
8670 If TO-HERE is non-nil, it should be a point in the buffer.  All
8671 articles before this point will be marked as read.
8672 Note that this function will only catch up the unread article
8673 in the current summary buffer limitation.
8674 The number of articles marked as read is returned."
8675   (interactive "P")
8676   (prog1
8677       (save-excursion
8678         (when (or quietly
8679                   (not gnus-interactive-catchup) ;Without confirmation?
8680                   gnus-expert-user
8681                   (gnus-y-or-n-p
8682                    (if all
8683                        "Mark absolutely all articles as read? "
8684                      "Mark all unread articles as read? ")))
8685           (if (and not-mark
8686                    (not gnus-newsgroup-adaptive)
8687                    (not gnus-newsgroup-auto-expire)
8688                    (not gnus-suppress-duplicates)
8689                    (or (not gnus-use-cache)
8690                        (eq gnus-use-cache 'passive)))
8691               (progn
8692                 (when all
8693                   (setq gnus-newsgroup-marked nil
8694                         gnus-newsgroup-dormant nil))
8695                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8696             ;; We actually mark all articles as canceled, which we
8697             ;; have to do when using auto-expiry or adaptive scoring.
8698             (gnus-summary-show-all-threads)
8699             (when (gnus-summary-first-subject (not all) t)
8700               (while (and
8701                       (if to-here (< (point) to-here) t)
8702                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8703                       (gnus-summary-find-next (not all) nil nil t))))
8704             (gnus-set-mode-line 'summary))
8705           t))
8706     (gnus-summary-position-point)))
8707
8708 (defun gnus-summary-catchup-to-here (&optional all)
8709   "Mark all unticked articles before the current one as read.
8710 If ALL is non-nil, also mark ticked and dormant articles as read."
8711   (interactive "P")
8712   (save-excursion
8713     (gnus-save-hidden-threads
8714       (let ((beg (point)))
8715         ;; We check that there are unread articles.
8716         (when (or all (gnus-summary-find-prev))
8717           (gnus-summary-catchup all t beg)))))
8718   (gnus-summary-position-point))
8719
8720 (defun gnus-summary-catchup-all (&optional quietly)
8721   "Mark all articles in this newsgroup as read."
8722   (interactive "P")
8723   (gnus-summary-catchup t quietly))
8724
8725 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8726   "Mark all unread articles in this group as read, then exit.
8727 If prefix argument ALL is non-nil, all articles are marked as read."
8728   (interactive "P")
8729   (when (gnus-summary-catchup all quietly nil 'fast)
8730     ;; Select next newsgroup or exit.
8731     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8732              (eq gnus-auto-select-next 'quietly))
8733         (gnus-summary-next-group nil)
8734       (gnus-summary-exit))))
8735
8736 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8737   "Mark all articles in this newsgroup as read, and then exit."
8738   (interactive "P")
8739   (gnus-summary-catchup-and-exit t quietly))
8740
8741 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8742   "Mark all articles in this group as read and select the next group.
8743 If given a prefix, mark all articles, unread as well as ticked, as
8744 read."
8745   (interactive "P")
8746   (save-excursion
8747     (gnus-summary-catchup all))
8748   (gnus-summary-next-group))
8749
8750 ;;;
8751 ;;; with article
8752 ;;;
8753
8754 (defmacro gnus-with-article (article &rest forms)
8755   "Select ARTICLE and perform FORMS in the original article buffer.
8756 Then replace the article with the result."
8757   `(progn
8758      ;; We don't want the article to be marked as read.
8759      (let (gnus-mark-article-hook)
8760        (gnus-summary-select-article t t nil ,article))
8761      (set-buffer gnus-original-article-buffer)
8762      ,@forms
8763      (if (not (gnus-check-backend-function
8764                'request-replace-article (car gnus-article-current)))
8765          (gnus-message 5 "Read-only group; not replacing")
8766        (unless (gnus-request-replace-article
8767                 ,article (car gnus-article-current)
8768                 (current-buffer) t)
8769          (error "Couldn't replace article")))
8770      ;; The cache and backlog have to be flushed somewhat.
8771      (when gnus-keep-backlog
8772        (gnus-backlog-remove-article
8773         (car gnus-article-current) (cdr gnus-article-current)))
8774      (when gnus-use-cache
8775        (gnus-cache-update-article
8776         (car gnus-article-current) (cdr gnus-article-current)))))
8777
8778 (put 'gnus-with-article 'lisp-indent-function 1)
8779 (put 'gnus-with-article 'edebug-form-spec '(form body))
8780
8781 ;; Thread-based commands.
8782
8783 (defun gnus-summary-articles-in-thread (&optional article)
8784   "Return a list of all articles in the current thread.
8785 If ARTICLE is non-nil, return all articles in the thread that starts
8786 with that article."
8787   (let* ((article (or article (gnus-summary-article-number)))
8788          (data (gnus-data-find-list article))
8789          (top-level (gnus-data-level (car data)))
8790          (top-subject
8791           (cond ((null gnus-thread-operation-ignore-subject)
8792                  (gnus-simplify-subject-re
8793                   (mail-header-subject (gnus-data-header (car data)))))
8794                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8795                  (gnus-simplify-subject-fuzzy
8796                   (mail-header-subject (gnus-data-header (car data)))))
8797                 (t nil)))
8798          (end-point (save-excursion
8799                       (if (gnus-summary-go-to-next-thread)
8800                           (point) (point-max))))
8801          articles)
8802     (while (and data
8803                 (< (gnus-data-pos (car data)) end-point))
8804       (when (or (not top-subject)
8805                 (string= top-subject
8806                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8807                              (gnus-simplify-subject-fuzzy
8808                               (mail-header-subject
8809                                (gnus-data-header (car data))))
8810                            (gnus-simplify-subject-re
8811                             (mail-header-subject
8812                              (gnus-data-header (car data)))))))
8813         (push (gnus-data-number (car data)) articles))
8814       (unless (and (setq data (cdr data))
8815                    (> (gnus-data-level (car data)) top-level))
8816         (setq data nil)))
8817     ;; Return the list of articles.
8818     (nreverse articles)))
8819
8820 (defun gnus-summary-rethread-current ()
8821   "Rethread the thread the current article is part of."
8822   (interactive)
8823   (let* ((gnus-show-threads t)
8824          (article (gnus-summary-article-number))
8825          (id (mail-header-id (gnus-summary-article-header)))
8826          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8827     (unless id
8828       (error "No article on the current line"))
8829     (gnus-rebuild-thread id)
8830     (gnus-summary-goto-subject article)))
8831
8832 (defun gnus-summary-reparent-thread ()
8833   "Make the current article child of the marked (or previous) article.
8834
8835 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8836 is non-nil or the Subject: of both articles are the same."
8837   (interactive)
8838   (unless (not (gnus-group-read-only-p))
8839     (error "The current newsgroup does not support article editing"))
8840   (unless (<= (length gnus-newsgroup-processable) 1)
8841     (error "No more than one article may be marked"))
8842   (save-window-excursion
8843     (let ((gnus-article-buffer " *reparent*")
8844           (current-article (gnus-summary-article-number))
8845           ;; First grab the marked article, otherwise one line up.
8846           (parent-article (if (not (null gnus-newsgroup-processable))
8847                               (car gnus-newsgroup-processable)
8848                             (save-excursion
8849                               (if (eq (forward-line -1) 0)
8850                                   (gnus-summary-article-number)
8851                                 (error "Beginning of summary buffer"))))))
8852       (unless (not (eq current-article parent-article))
8853         (error "An article may not be self-referential"))
8854       (let ((message-id (mail-header-id
8855                          (gnus-summary-article-header parent-article))))
8856         (unless (and message-id (not (equal message-id "")))
8857           (error "No message-id in desired parent"))
8858         (gnus-with-article current-article
8859           (save-restriction
8860             (goto-char (point-min))
8861             (message-narrow-to-head)
8862             (if (re-search-forward "^References: " nil t)
8863                 (progn
8864                   (re-search-forward "^[^ \t]" nil t)
8865                   (forward-line -1)
8866                   (end-of-line)
8867                   (insert " " message-id))
8868               (insert "References: " message-id "\n"))))
8869         (set-buffer gnus-summary-buffer)
8870         (gnus-summary-unmark-all-processable)
8871         (gnus-summary-update-article current-article)
8872         (gnus-summary-rethread-current)
8873         (gnus-message 3 "Article %d is now the child of article %d"
8874                       current-article parent-article)))))
8875
8876 (defun gnus-summary-toggle-threads (&optional arg)
8877   "Toggle showing conversation threads.
8878 If ARG is positive number, turn showing conversation threads on."
8879   (interactive "P")
8880   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8881     (setq gnus-show-threads
8882           (if (null arg) (not gnus-show-threads)
8883             (> (prefix-numeric-value arg) 0)))
8884     (gnus-summary-prepare)
8885     (gnus-summary-goto-subject current)
8886     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8887     (gnus-summary-position-point)))
8888
8889 (defun gnus-summary-show-all-threads ()
8890   "Show all threads."
8891   (interactive)
8892   (save-excursion
8893     (let ((buffer-read-only nil))
8894       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8895   (gnus-summary-position-point))
8896
8897 (defun gnus-summary-show-thread ()
8898   "Show thread subtrees.
8899 Returns nil if no thread was there to be shown."
8900   (interactive)
8901   (let ((buffer-read-only nil)
8902         (orig (point))
8903         ;; first goto end then to beg, to have point at beg after let
8904         (end (progn (end-of-line) (point)))
8905         (beg (progn (beginning-of-line) (point))))
8906     (prog1
8907         ;; Any hidden lines here?
8908         (search-forward "\r" end t)
8909       (subst-char-in-region beg end ?\^M ?\n t)
8910       (goto-char orig)
8911       (gnus-summary-position-point))))
8912
8913 (defun gnus-summary-hide-all-threads ()
8914   "Hide all thread subtrees."
8915   (interactive)
8916   (save-excursion
8917     (goto-char (point-min))
8918     (gnus-summary-hide-thread)
8919     (while (zerop (gnus-summary-next-thread 1 t))
8920       (gnus-summary-hide-thread)))
8921   (gnus-summary-position-point))
8922
8923 (defun gnus-summary-hide-thread ()
8924   "Hide thread subtrees.
8925 Returns nil if no threads were there to be hidden."
8926   (interactive)
8927   (let ((buffer-read-only nil)
8928         (start (point))
8929         (article (gnus-summary-article-number)))
8930     (goto-char start)
8931     ;; Go forward until either the buffer ends or the subthread
8932     ;; ends.
8933     (when (and (not (eobp))
8934                (or (zerop (gnus-summary-next-thread 1 t))
8935                    (goto-char (point-max))))
8936       (prog1
8937           (if (and (> (point) start)
8938                    (search-backward "\n" start t))
8939               (progn
8940                 (subst-char-in-region start (point) ?\n ?\^M)
8941                 (gnus-summary-goto-subject article))
8942             (goto-char start)
8943             nil)))))
8944
8945 (defun gnus-summary-go-to-next-thread (&optional previous)
8946   "Go to the same level (or less) next thread.
8947 If PREVIOUS is non-nil, go to previous thread instead.
8948 Return the article number moved to, or nil if moving was impossible."
8949   (let ((level (gnus-summary-thread-level))
8950         (way (if previous -1 1))
8951         (beg (point)))
8952     (forward-line way)
8953     (while (and (not (eobp))
8954                 (< level (gnus-summary-thread-level)))
8955       (forward-line way))
8956     (if (eobp)
8957         (progn
8958           (goto-char beg)
8959           nil)
8960       (setq beg (point))
8961       (prog1
8962           (gnus-summary-article-number)
8963         (goto-char beg)))))
8964
8965 (defun gnus-summary-next-thread (n &optional silent)
8966   "Go to the same level next N'th thread.
8967 If N is negative, search backward instead.
8968 Returns the difference between N and the number of skips actually
8969 done.
8970
8971 If SILENT, don't output messages."
8972   (interactive "p")
8973   (let ((backward (< n 0))
8974         (n (abs n)))
8975     (while (and (> n 0)
8976                 (gnus-summary-go-to-next-thread backward))
8977       (decf n))
8978     (unless silent
8979       (gnus-summary-position-point))
8980     (when (and (not silent) (/= 0 n))
8981       (gnus-message 7 "No more threads"))
8982     n))
8983
8984 (defun gnus-summary-prev-thread (n)
8985   "Go to the same level previous N'th thread.
8986 Returns the difference between N and the number of skips actually
8987 done."
8988   (interactive "p")
8989   (gnus-summary-next-thread (- n)))
8990
8991 (defun gnus-summary-go-down-thread ()
8992   "Go down one level in the current thread."
8993   (let ((children (gnus-summary-article-children)))
8994     (when children
8995       (gnus-summary-goto-subject (car children)))))
8996
8997 (defun gnus-summary-go-up-thread ()
8998   "Go up one level in the current thread."
8999   (let ((parent (gnus-summary-article-parent)))
9000     (when parent
9001       (gnus-summary-goto-subject parent))))
9002
9003 (defun gnus-summary-down-thread (n)
9004   "Go down thread N steps.
9005 If N is negative, go up instead.
9006 Returns the difference between N and how many steps down that were
9007 taken."
9008   (interactive "p")
9009   (let ((up (< n 0))
9010         (n (abs n)))
9011     (while (and (> n 0)
9012                 (if up (gnus-summary-go-up-thread)
9013                   (gnus-summary-go-down-thread)))
9014       (setq n (1- n)))
9015     (gnus-summary-position-point)
9016     (when (/= 0 n)
9017       (gnus-message 7 "Can't go further"))
9018     n))
9019
9020 (defun gnus-summary-up-thread (n)
9021   "Go up thread N steps.
9022 If N is negative, go up instead.
9023 Returns the difference between N and how many steps down that were
9024 taken."
9025   (interactive "p")
9026   (gnus-summary-down-thread (- n)))
9027
9028 (defun gnus-summary-top-thread ()
9029   "Go to the top of the thread."
9030   (interactive)
9031   (while (gnus-summary-go-up-thread))
9032   (gnus-summary-article-number))
9033
9034 (defun gnus-summary-kill-thread (&optional unmark)
9035   "Mark articles under current thread as read.
9036 If the prefix argument is positive, remove any kinds of marks.
9037 If the prefix argument is negative, tick articles instead."
9038   (interactive "P")
9039   (when unmark
9040     (setq unmark (prefix-numeric-value unmark)))
9041   (let ((articles (gnus-summary-articles-in-thread)))
9042     (save-excursion
9043       ;; Expand the thread.
9044       (gnus-summary-show-thread)
9045       ;; Mark all the articles.
9046       (while articles
9047         (gnus-summary-goto-subject (car articles))
9048         (cond ((null unmark)
9049                (gnus-summary-mark-article-as-read gnus-killed-mark))
9050               ((> unmark 0)
9051                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9052               (t
9053                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9054         (setq articles (cdr articles))))
9055     ;; Hide killed subtrees.
9056     (and (null unmark)
9057          gnus-thread-hide-killed
9058          (gnus-summary-hide-thread))
9059     ;; If marked as read, go to next unread subject.
9060     (when (null unmark)
9061       ;; Go to next unread subject.
9062       (gnus-summary-next-subject 1 t)))
9063   (gnus-set-mode-line 'summary))
9064
9065 ;; Summary sorting commands
9066
9067 (defun gnus-summary-sort-by-number (&optional reverse)
9068   "Sort the summary buffer by article number.
9069 Argument REVERSE means reverse order."
9070   (interactive "P")
9071   (gnus-summary-sort 'number reverse))
9072
9073 (defun gnus-summary-sort-by-author (&optional reverse)
9074   "Sort the summary buffer by author name alphabetically.
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 'author reverse))
9079
9080 (defun gnus-summary-sort-by-subject (&optional reverse)
9081   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9082 If `case-fold-search' is non-nil, case of letters is ignored.
9083 Argument REVERSE means reverse order."
9084   (interactive "P")
9085   (gnus-summary-sort 'subject reverse))
9086
9087 (defun gnus-summary-sort-by-date (&optional reverse)
9088   "Sort the summary buffer by date.
9089 Argument REVERSE means reverse order."
9090   (interactive "P")
9091   (gnus-summary-sort 'date reverse))
9092
9093 (defun gnus-summary-sort-by-score (&optional reverse)
9094   "Sort the summary buffer by score.
9095 Argument REVERSE means reverse order."
9096   (interactive "P")
9097   (gnus-summary-sort 'score reverse))
9098
9099 (defun gnus-summary-sort-by-lines (&optional reverse)
9100   "Sort the summary buffer by the number of lines.
9101 Argument REVERSE means reverse order."
9102   (interactive "P")
9103   (gnus-summary-sort 'lines reverse))
9104
9105 (defun gnus-summary-sort-by-chars (&optional reverse)
9106   "Sort the summary buffer by article length.
9107 Argument REVERSE means reverse order."
9108   (interactive "P")
9109   (gnus-summary-sort 'chars reverse))
9110
9111 (defun gnus-summary-sort (predicate reverse)
9112   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9113   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9114          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9115          (gnus-thread-sort-functions
9116           (if (not reverse)
9117               thread
9118             `(lambda (t1 t2)
9119                (,thread t2 t1))))
9120          (gnus-sort-gathered-threads-function
9121           gnus-thread-sort-functions)
9122          (gnus-article-sort-functions
9123           (if (not reverse)
9124               article
9125             `(lambda (t1 t2)
9126                (,article t2 t1))))
9127          (buffer-read-only)
9128          (gnus-summary-prepare-hook nil))
9129     ;; We do the sorting by regenerating the threads.
9130     (gnus-summary-prepare)
9131     ;; Hide subthreads if needed.
9132     (when (and gnus-show-threads gnus-thread-hide-subtree)
9133       (gnus-summary-hide-all-threads))))
9134
9135 ;; Summary saving commands.
9136
9137 (defun gnus-summary-save-article (&optional n not-saved)
9138   "Save the current article using the default saver function.
9139 If N is a positive number, save the N next articles.
9140 If N is a negative number, save the N previous articles.
9141 If N is nil and any articles have been marked with the process mark,
9142 save those articles instead.
9143 The variable `gnus-default-article-saver' specifies the saver function."
9144   (interactive "P")
9145   (let* ((articles (gnus-summary-work-articles n))
9146          (save-buffer (save-excursion
9147                         (nnheader-set-temp-buffer " *Gnus Save*")))
9148          (num (length articles))
9149          header file)
9150     (dolist (article articles)
9151       (setq header (gnus-summary-article-header article))
9152       (if (not (vectorp header))
9153           ;; This is a pseudo-article.
9154           (if (assq 'name header)
9155               (gnus-copy-file (cdr (assq 'name header)))
9156             (gnus-message 1 "Article %d is unsaveable" article))
9157         ;; This is a real article.
9158         (save-window-excursion
9159           (gnus-summary-select-article t nil nil article))
9160         (save-excursion
9161           (set-buffer save-buffer)
9162           (erase-buffer)
9163           (insert-buffer-substring gnus-original-article-buffer))
9164         (setq file (gnus-article-save save-buffer file num))
9165         (gnus-summary-remove-process-mark article)
9166         (unless not-saved
9167           (gnus-summary-set-saved-mark article))))
9168     (gnus-kill-buffer save-buffer)
9169     (gnus-summary-position-point)
9170     (gnus-set-mode-line 'summary)
9171     n))
9172
9173 (defun gnus-summary-pipe-output (&optional arg)
9174   "Pipe the current article to a subprocess.
9175 If N is a positive number, pipe the N next articles.
9176 If N is a negative number, pipe the N previous articles.
9177 If N is nil and any articles have been marked with the process mark,
9178 pipe those articles instead."
9179   (interactive "P")
9180   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9181     (gnus-summary-save-article arg t))
9182   (gnus-configure-windows 'pipe))
9183
9184 (defun gnus-summary-save-article-mail (&optional arg)
9185   "Append the current article to an mail file.
9186 If N is a positive number, save the N next articles.
9187 If N is a negative number, save the N previous articles.
9188 If N is nil and any articles have been marked with the process mark,
9189 save those articles instead."
9190   (interactive "P")
9191   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9192     (gnus-summary-save-article arg)))
9193
9194 (defun gnus-summary-save-article-rmail (&optional arg)
9195   "Append the current article to an rmail file.
9196 If N is a positive number, save the N next articles.
9197 If N is a negative number, save the N previous articles.
9198 If N is nil and any articles have been marked with the process mark,
9199 save those articles instead."
9200   (interactive "P")
9201   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9202     (gnus-summary-save-article arg)))
9203
9204 (defun gnus-summary-save-article-file (&optional arg)
9205   "Append the current article to a file.
9206 If N is a positive number, save the N next articles.
9207 If N is a negative number, save the N previous articles.
9208 If N is nil and any articles have been marked with the process mark,
9209 save those articles instead."
9210   (interactive "P")
9211   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9212     (gnus-summary-save-article arg)))
9213
9214 (defun gnus-summary-write-article-file (&optional arg)
9215   "Write the current article to a file, deleting the previous file.
9216 If N is a positive number, save the N next articles.
9217 If N is a negative number, save the N previous articles.
9218 If N is nil and any articles have been marked with the process mark,
9219 save those articles instead."
9220   (interactive "P")
9221   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9222     (gnus-summary-save-article arg)))
9223
9224 (defun gnus-summary-save-article-body-file (&optional arg)
9225   "Append the current article body to a file.
9226 If N is a positive number, save the N next articles.
9227 If N is a negative number, save the N previous articles.
9228 If N is nil and any articles have been marked with the process mark,
9229 save those articles instead."
9230   (interactive "P")
9231   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9232     (gnus-summary-save-article arg)))
9233
9234 (defun gnus-summary-pipe-message (program)
9235   "Pipe the current article through PROGRAM."
9236   (interactive "sProgram: ")
9237   (gnus-summary-select-article)
9238   (let ((mail-header-separator ""))
9239     (gnus-eval-in-buffer-window gnus-article-buffer
9240       (save-restriction
9241         (widen)
9242         (let ((start (window-start))
9243               buffer-read-only)
9244           (message-pipe-buffer-body program)
9245           (set-window-start (get-buffer-window (current-buffer)) start))))))
9246
9247 (defun gnus-get-split-value (methods)
9248   "Return a value based on the split METHODS."
9249   (let (split-name method result match)
9250     (when methods
9251       (save-excursion
9252         (set-buffer gnus-original-article-buffer)
9253         (save-restriction
9254           (nnheader-narrow-to-headers)
9255           (while methods
9256             (goto-char (point-min))
9257             (setq method (pop methods))
9258             (setq match (car method))
9259             (when (cond
9260                    ((stringp match)
9261                     ;; Regular expression.
9262                     (ignore-errors
9263                       (re-search-forward match nil t)))
9264                    ((gnus-functionp match)
9265                     ;; Function.
9266                     (save-restriction
9267                       (widen)
9268                       (setq result (funcall match gnus-newsgroup-name))))
9269                    ((consp match)
9270                     ;; Form.
9271                     (save-restriction
9272                       (widen)
9273                       (setq result (eval match)))))
9274               (setq split-name (append (cdr method) split-name))
9275               (cond ((stringp result)
9276                      (push (expand-file-name
9277                             result gnus-article-save-directory)
9278                            split-name))
9279                     ((consp result)
9280                      (setq split-name (append result split-name)))))))))
9281     (nreverse split-name)))
9282
9283 (defun gnus-valid-move-group-p (group)
9284   (and (boundp group)
9285        (symbol-name group)
9286        (symbol-value group)
9287        (gnus-get-function (gnus-find-method-for-group
9288                            (symbol-name group)) 'request-accept-article t)))
9289
9290 (defun gnus-read-move-group-name (prompt default articles prefix)
9291   "Read a group name."
9292   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9293          (minibuffer-confirm-incomplete nil) ; XEmacs
9294          (prom
9295           (format "%s %s to:"
9296                   prompt
9297                   (if (> (length articles) 1)
9298                       (format "these %d articles" (length articles))
9299                     "this article")))
9300          (to-newsgroup
9301           (cond
9302            ((null split-name)
9303             (gnus-completing-read default prom
9304                                   gnus-active-hashtb
9305                                   'gnus-valid-move-group-p
9306                                   nil prefix
9307                                   'gnus-group-history))
9308            ((= 1 (length split-name))
9309             (gnus-completing-read (car split-name) prom
9310                                   gnus-active-hashtb
9311                                   'gnus-valid-move-group-p
9312                                   nil nil
9313                                   'gnus-group-history))
9314            (t
9315             (gnus-completing-read nil prom
9316                                   (mapcar (lambda (el) (list el))
9317                                           (nreverse split-name))
9318                                   nil nil nil
9319                                   'gnus-group-history))))
9320          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9321     (when to-newsgroup
9322       (if (or (string= to-newsgroup "")
9323               (string= to-newsgroup prefix))
9324           (setq to-newsgroup default))
9325       (unless to-newsgroup
9326         (error "No group name entered"))
9327       (or (gnus-active to-newsgroup)
9328           (gnus-activate-group to-newsgroup nil nil to-method)
9329           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9330                                      to-newsgroup))
9331               (or (and (gnus-request-create-group to-newsgroup to-method)
9332                        (gnus-activate-group
9333                         to-newsgroup nil nil to-method)
9334                        (gnus-subscribe-group to-newsgroup))
9335                   (error "Couldn't create group %s" to-newsgroup)))
9336           (error "No such group: %s" to-newsgroup)))
9337     to-newsgroup))
9338
9339 (defun gnus-summary-save-parts (type dir n &optional reverse)
9340   "Save parts matching TYPE to DIR.
9341 If REVERSE, save parts that do not match TYPE."
9342   (interactive
9343    (list (read-string "Save parts of type: " 
9344                       (or (car gnus-summary-save-parts-type-history)
9345                           gnus-summary-save-parts-default-mime)
9346                       'gnus-summary-save-parts-type-history)
9347          (setq gnus-summary-save-parts-last-directory
9348                (read-file-name "Save to directory: " 
9349                                gnus-summary-save-parts-last-directory
9350                                nil t))
9351          current-prefix-arg))
9352   (gnus-summary-iterate n
9353     (let ((gnus-display-mime-function nil)
9354           (gnus-inhibit-treatment t))
9355       (gnus-summary-select-article))
9356     (save-excursion
9357       (set-buffer gnus-article-buffer)
9358       (let ((handles (or gnus-article-mime-handles
9359                          (mm-dissect-buffer) (mm-uu-dissect))))
9360         (when handles
9361           (gnus-summary-save-parts-1 type dir handles reverse)
9362           (unless gnus-article-mime-handles ;; Don't destroy this case.
9363             (mm-destroy-parts handles)))))))
9364
9365 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9366   (if (stringp (car handle))
9367       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9368               (cdr handle))
9369     (when (if reverse
9370               (not (string-match type (mm-handle-media-type handle)))
9371             (string-match type (mm-handle-media-type handle)))
9372       (let ((file (expand-file-name
9373                    (file-name-nondirectory
9374                     (or
9375                      (mail-content-type-get
9376                       (mm-handle-disposition handle) 'filename)
9377                      (concat gnus-newsgroup-name
9378                              "." (number-to-string
9379                                   (cdr gnus-article-current)))))
9380                    dir)))
9381         (unless (file-exists-p file)
9382           (mm-save-part-to-file handle file))))))
9383
9384 ;; Summary extract commands
9385
9386 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9387   (let ((buffer-read-only nil)
9388         (article (gnus-summary-article-number))
9389         after-article b e)
9390     (unless (gnus-summary-goto-subject article)
9391       (error "No such article: %d" article))
9392     (gnus-summary-position-point)
9393     ;; If all commands are to be bunched up on one line, we collect
9394     ;; them here.
9395     (unless gnus-view-pseudos-separately
9396       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9397             files action)
9398         (while ps
9399           (setq action (cdr (assq 'action (car ps))))
9400           (setq files (list (cdr (assq 'name (car ps)))))
9401           (while (and ps (cdr ps)
9402                       (string= (or action "1")
9403                                (or (cdr (assq 'action (cadr ps))) "2")))
9404             (push (cdr (assq 'name (cadr ps))) files)
9405             (setcdr ps (cddr ps)))
9406           (when files
9407             (when (not (string-match "%s" action))
9408               (push " " files))
9409             (push " " files)
9410             (when (assq 'execute (car ps))
9411               (setcdr (assq 'execute (car ps))
9412                       (funcall (if (string-match "%s" action)
9413                                    'format 'concat)
9414                                action
9415                                (mapconcat
9416                                 (lambda (f)
9417                                   (if (equal f " ")
9418                                       f
9419                                     (gnus-quote-arg-for-sh-or-csh f)))
9420                                 files " ")))))
9421           (setq ps (cdr ps)))))
9422     (if (and gnus-view-pseudos (not not-view))
9423         (while pslist
9424           (when (assq 'execute (car pslist))
9425             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9426                                   (eq gnus-view-pseudos 'not-confirm)))
9427           (setq pslist (cdr pslist)))
9428       (save-excursion
9429         (while pslist
9430           (setq after-article (or (cdr (assq 'article (car pslist)))
9431                                   (gnus-summary-article-number)))
9432           (gnus-summary-goto-subject after-article)
9433           (forward-line 1)
9434           (setq b (point))
9435           (insert "    " (file-name-nondirectory
9436                           (cdr (assq 'name (car pslist))))
9437                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9438           (setq e (point))
9439           (forward-line -1)             ; back to `b'
9440           (gnus-add-text-properties
9441            b (1- e) (list 'gnus-number gnus-reffed-article-number
9442                           gnus-mouse-face-prop gnus-mouse-face))
9443           (gnus-data-enter
9444            after-article gnus-reffed-article-number
9445            gnus-unread-mark b (car pslist) 0 (- e b))
9446           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9447           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9448           (setq pslist (cdr pslist)))))))
9449
9450 (defun gnus-pseudos< (p1 p2)
9451   (let ((c1 (cdr (assq 'action p1)))
9452         (c2 (cdr (assq 'action p2))))
9453     (and c1 c2 (string< c1 c2))))
9454
9455 (defun gnus-request-pseudo-article (props)
9456   (cond ((assq 'execute props)
9457          (gnus-execute-command (cdr (assq 'execute props)))))
9458   (let ((gnus-current-article (gnus-summary-article-number)))
9459     (gnus-run-hooks 'gnus-mark-article-hook)))
9460
9461 (defun gnus-execute-command (command &optional automatic)
9462   (save-excursion
9463     (gnus-article-setup-buffer)
9464     (set-buffer gnus-article-buffer)
9465     (setq buffer-read-only nil)
9466     (let ((command (if automatic command
9467                      (read-string "Command: " (cons command 0)))))
9468       (erase-buffer)
9469       (insert "$ " command "\n\n")
9470       (if gnus-view-pseudo-asynchronously
9471           (start-process "gnus-execute" (current-buffer) shell-file-name
9472                          shell-command-switch command)
9473         (call-process shell-file-name nil t nil
9474                       shell-command-switch command)))))
9475
9476 ;; Summary kill commands.
9477
9478 (defun gnus-summary-edit-global-kill (article)
9479   "Edit the \"global\" kill file."
9480   (interactive (list (gnus-summary-article-number)))
9481   (gnus-group-edit-global-kill article))
9482
9483 (defun gnus-summary-edit-local-kill ()
9484   "Edit a local kill file applied to the current newsgroup."
9485   (interactive)
9486   (setq gnus-current-headers (gnus-summary-article-header))
9487   (gnus-group-edit-local-kill
9488    (gnus-summary-article-number) gnus-newsgroup-name))
9489
9490 ;;; Header reading.
9491
9492 (defun gnus-read-header (id &optional header)
9493   "Read the headers of article ID and enter them into the Gnus system."
9494   (let ((group gnus-newsgroup-name)
9495         (gnus-override-method
9496          (or
9497           gnus-override-method
9498           (and (gnus-news-group-p gnus-newsgroup-name)
9499                (car (gnus-refer-article-methods)))))
9500         where)
9501     ;; First we check to see whether the header in question is already
9502     ;; fetched.
9503     (if (stringp id)
9504         ;; This is a Message-ID.
9505         (setq header (or header (gnus-id-to-header id)))
9506       ;; This is an article number.
9507       (setq header (or header (gnus-summary-article-header id))))
9508     (if (and header
9509              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9510         ;; We have found the header.
9511         header
9512       ;; If this is a sparse article, we have to nix out its
9513       ;; previous entry in the thread hashtb.
9514       (when (and header
9515                  (gnus-summary-article-sparse-p (mail-header-number header)))
9516         (let* ((parent (gnus-parent-id (mail-header-references header)))
9517                (thread (and parent (gnus-id-to-thread parent))))
9518           (when thread
9519             (delq (assq header thread) thread))))
9520       ;; We have to really fetch the header to this article.
9521       (save-excursion
9522         (set-buffer nntp-server-buffer)
9523         (when (setq where (gnus-request-head id group))
9524           (nnheader-fold-continuation-lines)
9525           (goto-char (point-max))
9526           (insert ".\n")
9527           (goto-char (point-min))
9528           (insert "211 ")
9529           (princ (cond
9530                   ((numberp id) id)
9531                   ((cdr where) (cdr where))
9532                   (header (mail-header-number header))
9533                   (t gnus-reffed-article-number))
9534                  (current-buffer))
9535           (insert " Article retrieved.\n"))
9536         (if (or (not where)
9537                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9538             ()                          ; Malformed head.
9539           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9540             (when (and (stringp id)
9541                        (not (string= (gnus-group-real-name group)
9542                                      (car where))))
9543               ;; If we fetched by Message-ID and the article came
9544               ;; from a different group, we fudge some bogus article
9545               ;; numbers for this article.
9546               (mail-header-set-number header gnus-reffed-article-number))
9547             (save-excursion
9548               (set-buffer gnus-summary-buffer)
9549               (decf gnus-reffed-article-number)
9550               (gnus-remove-header (mail-header-number header))
9551               (push header gnus-newsgroup-headers)
9552               (setq gnus-current-headers header)
9553               (push (mail-header-number header) gnus-newsgroup-limit)))
9554           header)))))
9555
9556 (defun gnus-remove-header (number)
9557   "Remove header NUMBER from `gnus-newsgroup-headers'."
9558   (if (and gnus-newsgroup-headers
9559            (= number (mail-header-number (car gnus-newsgroup-headers))))
9560       (pop gnus-newsgroup-headers)
9561     (let ((headers gnus-newsgroup-headers))
9562       (while (and (cdr headers)
9563                   (not (= number (mail-header-number (cadr headers)))))
9564         (pop headers))
9565       (when (cdr headers)
9566         (setcdr headers (cddr headers))))))
9567
9568 ;;;
9569 ;;; summary highlights
9570 ;;;
9571
9572 (defun gnus-highlight-selected-summary ()
9573   "Highlight selected article in summary buffer."
9574   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9575   (when gnus-summary-selected-face
9576     (save-excursion
9577       (let* ((beg (progn (beginning-of-line) (point)))
9578              (end (progn (end-of-line) (point)))
9579              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9580              (from (if (get-text-property beg gnus-mouse-face-prop)
9581                        beg
9582                      (or (next-single-property-change
9583                           beg gnus-mouse-face-prop nil end)
9584                          beg)))
9585              (to
9586               (if (= from end)
9587                   (- from 2)
9588                 (or (next-single-property-change
9589                      from gnus-mouse-face-prop nil end)
9590                     end))))
9591         ;; If no mouse-face prop on line we will have to = from = end,
9592         ;; so we highlight the entire line instead.
9593         (when (= (+ to 2) from)
9594           (setq from beg)
9595           (setq to end))
9596         (if gnus-newsgroup-selected-overlay
9597             ;; Move old overlay.
9598             (gnus-move-overlay
9599              gnus-newsgroup-selected-overlay from to (current-buffer))
9600           ;; Create new overlay.
9601           (gnus-overlay-put
9602            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9603            'face gnus-summary-selected-face))))))
9604
9605 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9606 (defun gnus-summary-highlight-line ()
9607   "Highlight current line according to `gnus-summary-highlight'."
9608   (let* ((list gnus-summary-highlight)
9609          (p (point))
9610          (end (progn (end-of-line) (point)))
9611          ;; now find out where the line starts and leave point there.
9612          (beg (progn (beginning-of-line) (point)))
9613          (article (gnus-summary-article-number))
9614          (score (or (cdr (assq (or article gnus-current-article)
9615                                gnus-newsgroup-scored))
9616                     gnus-summary-default-score 0))
9617          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9618          (inhibit-read-only t))
9619     ;; Eval the cars of the lists until we find a match.
9620     (let ((default gnus-summary-default-score))
9621       (while (and list
9622                   (not (eval (caar list))))
9623         (setq list (cdr list))))
9624     (let ((face (cdar list)))
9625       (unless (eq face (get-text-property beg 'face))
9626         (gnus-put-text-property-excluding-characters-with-faces
9627          beg end 'face
9628          (setq face (if (boundp face) (symbol-value face) face)))
9629         (when gnus-summary-highlight-line-function
9630           (funcall gnus-summary-highlight-line-function article face))))
9631     (goto-char p)))
9632
9633 (defun gnus-update-read-articles (group unread &optional compute)
9634   "Update the list of read articles in GROUP."
9635   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9636          (entry (gnus-gethash group gnus-newsrc-hashtb))
9637          (info (nth 2 entry))
9638          (prev 1)
9639          (unread (sort (copy-sequence unread) '<))
9640          read)
9641     (if (or (not info) (not active))
9642         ;; There is no info on this group if it was, in fact,
9643         ;; killed.  Gnus stores no information on killed groups, so
9644         ;; there's nothing to be done.
9645         ;; One could store the information somewhere temporarily,
9646         ;; perhaps...  Hmmm...
9647         ()
9648       ;; Remove any negative articles numbers.
9649       (while (and unread (< (car unread) 0))
9650         (setq unread (cdr unread)))
9651       ;; Remove any expired article numbers
9652       (while (and unread (< (car unread) (car active)))
9653         (setq unread (cdr unread)))
9654       ;; Compute the ranges of read articles by looking at the list of
9655       ;; unread articles.
9656       (while unread
9657         (when (/= (car unread) prev)
9658           (push (if (= prev (1- (car unread))) prev
9659                   (cons prev (1- (car unread))))
9660                 read))
9661         (setq prev (1+ (car unread)))
9662         (setq unread (cdr unread)))
9663       (when (<= prev (cdr active))
9664         (push (cons prev (cdr active)) read))
9665       (setq read (if (> (length read) 1) (nreverse read) read))
9666       (if compute
9667           read
9668         (save-excursion
9669           (let (setmarkundo)
9670             ;; Propagate the read marks to the backend.
9671             (when (gnus-check-backend-function 'request-set-mark group)
9672               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9673                     (add (gnus-remove-from-range read (gnus-info-read info))))
9674                 (when (or add del)
9675                   (unless (gnus-check-group group)
9676                     (error "Can't open server for %s" group))
9677                   (gnus-request-set-mark
9678                    group (delq nil (list (if add (list add 'add '(read)))
9679                                          (if del (list del 'del '(read))))))
9680                   (setq setmarkundo
9681                         `(gnus-request-set-mark
9682                           ,group
9683                           ',(delq nil (list
9684                                        (if del (list del 'add '(read)))
9685                                        (if add (list add 'del '(read))))))))))
9686             (set-buffer gnus-group-buffer)
9687             (gnus-undo-register
9688               `(progn
9689                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9690                  (gnus-info-set-read ',info ',(gnus-info-read info))
9691                  (gnus-get-unread-articles-in-group ',info 
9692                                                     (gnus-active ,group))
9693                  (gnus-group-update-group ,group t)
9694                  ,setmarkundo))))
9695         ;; Enter this list into the group info.
9696         (gnus-info-set-read info read)
9697         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9698         (gnus-get-unread-articles-in-group info (gnus-active group))
9699         t))))
9700
9701 (defun gnus-offer-save-summaries ()
9702   "Offer to save all active summary buffers."
9703   (save-excursion
9704     (let ((buflist (buffer-list))
9705           buffers bufname)
9706       ;; Go through all buffers and find all summaries.
9707       (while buflist
9708         (and (setq bufname (buffer-name (car buflist)))
9709              (string-match "Summary" bufname)
9710              (save-excursion
9711                (set-buffer bufname)
9712                ;; We check that this is, indeed, a summary buffer.
9713                (and (eq major-mode 'gnus-summary-mode)
9714                     ;; Also make sure this isn't bogus.
9715                     gnus-newsgroup-prepared
9716                     ;; Also make sure that this isn't a dead summary buffer.
9717                     (not gnus-dead-summary-mode)))
9718              (push bufname buffers))
9719         (setq buflist (cdr buflist)))
9720       ;; Go through all these summary buffers and offer to save them.
9721       (when buffers
9722         (map-y-or-n-p
9723          "Update summary buffer %s? "
9724          (lambda (buf)
9725            (switch-to-buffer buf)
9726            (gnus-summary-exit))
9727          buffers)))))
9728
9729
9730 ;;; @ for mime-partial
9731 ;;;
9732
9733 (defun gnus-request-partial-message ()
9734   (save-excursion
9735     (let ((number (gnus-summary-article-number))
9736           (group gnus-newsgroup-name)
9737           (mother gnus-article-buffer))
9738       (set-buffer (get-buffer-create " *Partial Article*"))
9739       (erase-buffer)
9740       (setq mime-preview-buffer mother)
9741       (gnus-request-article-this-buffer number group)
9742       (mime-parse-buffer)
9743       )))
9744
9745 (autoload 'mime-combine-message/partial-pieces-automatically
9746   "mime-partial"
9747   "Internal method to combine message/partial messages automatically.")
9748
9749 (mime-add-condition
9750  'action '((type . message)(subtype . partial)
9751            (major-mode . gnus-original-article-mode)
9752            (method . mime-combine-message/partial-pieces-automatically)
9753            (summary-buffer-exp . gnus-summary-buffer)
9754            (request-partial-message-method . gnus-request-partial-message)
9755            ))
9756
9757
9758 ;;; @ for message/rfc822
9759 ;;;
9760
9761 (defun gnus-mime-extract-message/rfc822 (entity situation)
9762   (let (group article num cwin swin cur)
9763     (with-temp-buffer
9764       (mime-insert-entity-content entity)
9765       (setq group (or (cdr (assq 'group situation))
9766                       (completing-read "Group: "
9767                                        gnus-active-hashtb
9768                                        nil
9769                                        (gnus-read-active-file-p)
9770                                        gnus-newsgroup-name))
9771             article (gnus-request-accept-article group)))
9772     (when (and (consp article)
9773                (numberp (setq article (cdr article))))
9774       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9775             cwin (get-buffer-window (current-buffer) t))
9776       (save-window-excursion
9777         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9778             (select-window swin)
9779           (set-buffer gnus-summary-buffer))
9780         (setq cur gnus-current-article)
9781         (forward-line num)
9782         (let (gnus-show-threads)
9783           (gnus-summary-goto-subject article t))
9784         (gnus-summary-clear-mark-forward 1)
9785         (gnus-summary-goto-subject cur))
9786       (when (and cwin (window-frame cwin))
9787         (select-frame (window-frame cwin)))
9788       (when (boundp 'mime-acting-situation-to-override)
9789         (set-alist 'mime-acting-situation-to-override
9790                    'group
9791                    group)
9792         (set-alist 'mime-acting-situation-to-override
9793                    'after-method
9794                    `(progn
9795                       (save-current-buffer
9796                         (set-buffer gnus-group-buffer)
9797                         (gnus-activate-group ,group))
9798                       (gnus-summary-goto-article ,cur
9799                                                  gnus-show-all-headers)))
9800         (set-alist 'mime-acting-situation-to-override
9801                    'number num)))))
9802
9803 (mime-add-condition
9804  'action '((type . message)(subtype . rfc822)
9805            (major-mode . gnus-original-article-mode)
9806            (method . gnus-mime-extract-message/rfc822)
9807            (mode . "extract")
9808            ))
9809
9810 (mime-add-condition
9811  'action '((type . message)(subtype . news)
9812            (major-mode . gnus-original-article-mode)
9813            (method . gnus-mime-extract-message/rfc822)
9814            (mode . "extract")
9815            ))
9816
9817 (defun gnus-mime-extract-multipart (entity situation)
9818   (let ((children (mime-entity-children entity))
9819         mime-acting-situation-to-override
9820         f)
9821     (while children
9822       (mime-play-entity (car children)
9823                         (cons (assq 'mode situation)
9824                               mime-acting-situation-to-override))
9825       (setq children (cdr children)))
9826     (if (setq f (cdr (assq 'after-method
9827                            mime-acting-situation-to-override)))
9828         (eval f)
9829       )))
9830
9831 (mime-add-condition
9832  'action '((type . multipart)
9833            (method . gnus-mime-extract-multipart)
9834            (mode . "extract")
9835            )
9836  'with-default)
9837
9838
9839 ;;; @ end
9840 ;;;
9841
9842 (defun gnus-summary-setup-default-charset ()
9843   "Setup newsgroup default charset."
9844   (if (equal gnus-newsgroup-name "nndraft:drafts")
9845       (setq gnus-newsgroup-charset nil)
9846     (let* ((name (and gnus-newsgroup-name
9847                       (gnus-group-real-name gnus-newsgroup-name)))
9848            (ignored-charsets
9849             (or gnus-newsgroup-ephemeral-ignored-charsets
9850                 (append
9851                  (and gnus-newsgroup-name
9852                       (or (gnus-group-find-parameter gnus-newsgroup-name
9853                                                      'ignored-charsets t)
9854                           (let ((alist gnus-group-ignored-charsets-alist)
9855                                 elem (charsets nil))
9856                             (while (setq elem (pop alist))
9857                               (when (and name
9858                                          (string-match (car elem) name))
9859                                 (setq alist nil
9860                                       charsets (cdr elem))))
9861                             charsets)))
9862                  gnus-newsgroup-ignored-charsets))))
9863       (setq gnus-newsgroup-charset
9864             (or gnus-newsgroup-ephemeral-charset
9865                 (and gnus-newsgroup-name
9866                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9867                          (let ((alist gnus-group-charset-alist)
9868                                elem charset)
9869                            (while (setq elem (pop alist))
9870                              (when (and name
9871                                         (string-match (car elem) name))
9872                                (setq alist nil
9873                                      charset (cadr elem))))
9874                            charset)))
9875                 gnus-default-charset))
9876       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9877            ignored-charsets))))
9878
9879 ;;;
9880 ;;; Mime Commands
9881 ;;;
9882
9883 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9884   "Display the current article buffer fully MIME-buttonized.
9885 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9886 treated as multipart/mixed."
9887   (interactive "P")
9888   (require 'gnus-art)
9889   (let ((gnus-unbuttonized-mime-types nil)
9890         (gnus-mime-display-multipart-as-mixed show-all-parts))
9891     (gnus-summary-show-article)))
9892
9893 (defun gnus-summary-repair-multipart (article)
9894   "Add a Content-Type header to a multipart article without one."
9895   (interactive (list (gnus-summary-article-number)))
9896   (gnus-with-article article
9897     (message-narrow-to-head)
9898     (goto-char (point-max))
9899     (widen)
9900     (when (search-forward "\n--" nil t)
9901       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9902         (message-narrow-to-head)
9903         (message-remove-header "Mime-Version")
9904         (message-remove-header "Content-Type")
9905         (goto-char (point-max))
9906         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9907                         separator))
9908         (insert "Mime-Version: 1.0\n")
9909         (widen))))
9910   (let (gnus-mark-article-hook)
9911     (gnus-summary-select-article t t nil article)))
9912
9913 (defun gnus-summary-toggle-display-buttonized ()
9914   "Toggle the buttonizing of the article buffer."
9915   (interactive)
9916   (require 'gnus-art)
9917   (if (setq gnus-inhibit-mime-unbuttonizing
9918             (not gnus-inhibit-mime-unbuttonizing))
9919       (let ((gnus-unbuttonized-mime-types nil))
9920         (gnus-summary-show-article))
9921     (gnus-summary-show-article)))
9922
9923 ;;;
9924 ;;; Intelli-mouse commmands
9925 ;;;
9926
9927 (defun gnus-wheel-summary-scroll (event)
9928   (interactive "e")
9929   (let ((amount (if (memq 'shift (event-modifiers event))
9930                     (car gnus-wheel-scroll-amount)
9931                   (cdr gnus-wheel-scroll-amount)))
9932         (direction (- (* (static-if (featurep 'xemacs)
9933                              (event-button event)
9934                            (cond ((eq 'mouse-4 (event-basic-type event))
9935                                   4)
9936                                  ((eq 'mouse-5 (event-basic-type event))
9937                                   5)))
9938                          2) 9))
9939         edge)
9940     (gnus-summary-scroll-up (* amount direction))
9941     (when (gnus-eval-in-buffer-window gnus-article-buffer
9942             (save-restriction
9943               (widen)
9944               (and (if (< 0 direction)
9945                        (gnus-article-next-page 0)
9946                      (gnus-article-prev-page 0)
9947                      (bobp))
9948                    (if (setq edge (get-text-property
9949                                    (point-min) 'gnus-wheel-edge))
9950                        (setq edge (* edge direction))
9951                      (setq edge -1))
9952                    (or (plusp edge)
9953                        (let ((buffer-read-only nil)
9954                              (inhibit-read-only t))
9955                          (put-text-property (point-min) (point-max)
9956                                             'gnus-wheel-edge direction)
9957                          nil))
9958                    (or (> edge gnus-wheel-edge-resistance)
9959                        (let ((buffer-read-only nil)
9960                              (inhibit-read-only t))
9961                          (put-text-property (point-min) (point-max)
9962                                             'gnus-wheel-edge
9963                                             (* (1+ edge) direction))
9964                          nil))
9965                    (eq last-command 'gnus-wheel-summary-scroll))))
9966       (gnus-summary-next-article nil nil (minusp direction)))))
9967
9968 (defun gnus-wheel-install ()
9969   "Enable mouse wheel support on summary window."
9970   (when gnus-use-wheel
9971     (let ((keys
9972            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9973       (dolist (key keys)
9974         (define-key gnus-summary-mode-map key
9975           'gnus-wheel-summary-scroll)))))
9976
9977 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9978
9979 ;;;
9980 ;;; Traditional PGP commmands
9981 ;;;
9982
9983 (defun gnus-summary-decrypt-article (&optional force)
9984   "Decrypt the current article in traditional PGP way.
9985 This will have permanent effect only in mail groups.
9986 If FORCE is non-nil, allow editing of articles even in read-only
9987 groups."
9988   (interactive "P")
9989   (gnus-summary-select-article t)
9990   (gnus-eval-in-buffer-window gnus-article-buffer
9991     (save-excursion
9992       (save-restriction
9993         (widen)
9994         (goto-char (point-min))
9995         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9996           (error "Not a traditional PGP message!"))
9997         (let ((armor-start (match-beginning 0)))
9998           (if (and (pgg-decrypt-region armor-start (point-max))
9999                    (or force (not (gnus-group-read-only-p))))
10000               (let ((inhibit-read-only t)
10001                     buffer-read-only)
10002                 (delete-region armor-start
10003                                (progn
10004                                  (re-search-forward "^-+END PGP" nil t)
10005                                  (beginning-of-line 2)
10006                                  (point)))
10007                 (insert-buffer-substring pgg-output-buffer))))))))
10008
10009 (defun gnus-summary-verify-article ()
10010   "Verify the current article in traditional PGP way."
10011   (interactive)
10012   (save-excursion
10013     (set-buffer gnus-original-article-buffer)
10014     (goto-char (point-min))
10015     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10016       (error "Not a traditional PGP message!"))
10017     (re-search-forward "^-+END PGP" nil t)
10018     (beginning-of-line 2)
10019     (call-interactively (function pgg-verify-region))))
10020
10021 ;;;
10022 ;;; Generic summary marking commands
10023 ;;;
10024
10025 (defvar gnus-summary-marking-alist
10026   '((read gnus-del-mark "d")
10027     (unread gnus-unread-mark "u")
10028     (ticked gnus-ticked-mark "!")
10029     (dormant gnus-dormant-mark "?")
10030     (expirable gnus-expirable-mark "e"))
10031   "An alist of names/marks/keystrokes.")
10032
10033 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10034 (defvar gnus-summary-mark-map)
10035
10036 (defun gnus-summary-make-all-marking-commands ()
10037   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10038   (dolist (elem gnus-summary-marking-alist)
10039     (apply 'gnus-summary-make-marking-command elem)))
10040
10041 (defun gnus-summary-make-marking-command (name mark keystroke)
10042   (let ((map (make-sparse-keymap)))
10043     (define-key gnus-summary-generic-mark-map keystroke map)
10044     (dolist (lway `((next "next" next nil "n")
10045                     (next-unread "next unread" next t "N")
10046                     (prev "previous" prev nil "p")
10047                     (prev-unread "previous unread" prev t "P")
10048                     (nomove "" nil nil ,keystroke)))
10049       (let ((func (gnus-summary-make-marking-command-1
10050                    mark (car lway) lway name)))
10051         (setq func (eval func))
10052         (define-key map (nth 4 lway) func)))))
10053
10054 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10055   `(defun ,(intern
10056             (format "gnus-summary-put-mark-as-%s%s"
10057                     name (if (eq way 'nomove)
10058                              ""
10059                            (concat "-" (symbol-name way)))))
10060      (n)
10061      ,(format
10062        "Mark the current article as %s%s.
10063 If N, the prefix, then repeat N times.
10064 If N is negative, move in reverse order.
10065 The difference between N and the actual number of articles marked is
10066 returned."
10067        name (car (cdr lway)))
10068      (interactive "p")
10069      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10070
10071 (defun gnus-summary-generic-mark (n mark move unread)
10072   "Mark N articles with MARK."
10073   (unless (eq major-mode 'gnus-summary-mode)
10074     (error "This command can only be used in the summary buffer"))
10075   (gnus-summary-show-thread)
10076   (let ((nummove
10077          (cond
10078           ((eq move 'next) 1)
10079           ((eq move 'prev) -1)
10080           (t 0))))
10081     (if (zerop nummove)
10082         (setq n 1)
10083       (when (< n 0)
10084         (setq n (abs n)
10085               nummove (* -1 nummove))))
10086     (while (and (> n 0)
10087                 (gnus-summary-mark-article nil mark)
10088                 (zerop (gnus-summary-next-subject nummove unread t)))
10089       (setq n (1- n)))
10090     (when (/= 0 n)
10091       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10092     (gnus-summary-recenter)
10093     (gnus-summary-position-point)
10094     (gnus-set-mode-line 'summary)
10095     n))
10096
10097 (gnus-summary-make-all-marking-commands)
10098
10099 (gnus-ems-redefine)
10100
10101 (provide 'gnus-sum)
10102
10103 (run-hooks 'gnus-sum-load-hook)
10104
10105 ;;; gnus-sum.el ends here