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 
6974                         (list (cons 'to-address 
6975                                     (funcall gnus-decode-encoded-word-function
6976                                              to-address))))))
6977         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6978         (insert-buffer-substring gnus-original-article-buffer)
6979         ;; Remove lines that may lead nndoc to misinterpret the
6980         ;; document type.
6981         (narrow-to-region
6982          (goto-char (point-min))
6983          (or (search-forward "\n\n" nil t) (point)))
6984         (goto-char (point-min))
6985         (delete-matching-lines "^Path:\\|^From ")
6986         (widen))
6987       (unwind-protect
6988           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6989                     (gnus-newsgroup-ephemeral-ignored-charsets
6990                      gnus-newsgroup-ignored-charsets))
6991                 (gnus-group-read-ephemeral-group
6992                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6993                               (nndoc-article-type
6994                                ,(if force 'mbox 'guess))) t))
6995               ;; Make all postings to this group go to the parent group.
6996               (nconc (gnus-info-params (gnus-get-info name))
6997                      params)
6998             ;; Couldn't select this doc group.
6999             (switch-to-buffer buf)
7000             (gnus-set-global-variables)
7001             (gnus-configure-windows 'summary)
7002             (gnus-message 3 "Article couldn't be entered?"))
7003         (kill-buffer dig)))))
7004
7005 (defun gnus-summary-read-document (n)
7006   "Open a new group based on the current article(s).
7007 This will allow you to read digests and other similar
7008 documents as newsgroups.
7009 Obeys the standard process/prefix convention."
7010   (interactive "P")
7011   (let* ((articles (gnus-summary-work-articles n))
7012          (ogroup gnus-newsgroup-name)
7013          (params (append (gnus-info-params (gnus-get-info ogroup))
7014                          (list (cons 'to-group ogroup))))
7015          article group egroup groups vgroup)
7016     (while (setq article (pop articles))
7017       (setq group (format "%s-%d" gnus-newsgroup-name article))
7018       (gnus-summary-remove-process-mark article)
7019       (when (gnus-summary-display-article article)
7020         (save-excursion
7021           (with-temp-buffer
7022             (insert-buffer-substring gnus-original-article-buffer)
7023             ;; Remove some headers that may lead nndoc to make
7024             ;; the wrong guess.
7025             (message-narrow-to-head)
7026             (goto-char (point-min))
7027             (delete-matching-lines "^\\(Path\\):\\|^From ")
7028             (widen)
7029             (if (setq egroup
7030                       (gnus-group-read-ephemeral-group
7031                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7032                                      (nndoc-article-type guess))
7033                        t nil t))
7034                 (progn
7035                   ;; Make all postings to this group go to the parent group.
7036                   (nconc (gnus-info-params (gnus-get-info egroup))
7037                          params)
7038                   (push egroup groups))
7039               ;; Couldn't select this doc group.
7040               (gnus-error 3 "Article couldn't be entered"))))))
7041     ;; Now we have selected all the documents.
7042     (cond
7043      ((not groups)
7044       (error "None of the articles could be interpreted as documents"))
7045      ((gnus-group-read-ephemeral-group
7046        (setq vgroup (format
7047                      "nnvirtual:%s-%s" gnus-newsgroup-name
7048                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7049        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7050        t
7051        (cons (current-buffer) 'summary)))
7052      (t
7053       (error "Couldn't select virtual nndoc group")))))
7054
7055 (defun gnus-summary-isearch-article (&optional regexp-p)
7056   "Do incremental search forward on the current article.
7057 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7058   (interactive "P")
7059   (let* ((gnus-inhibit-treatment t)
7060          (old (gnus-summary-select-article)))
7061     (gnus-configure-windows 'article)
7062     (gnus-eval-in-buffer-window gnus-article-buffer
7063       (save-restriction
7064         (widen)
7065         (when (eq 'old old)
7066           (gnus-article-show-all-headers))
7067         (goto-char (point-min))
7068         (isearch-forward regexp-p)))))
7069
7070 (defun gnus-summary-search-article-forward (regexp &optional backward)
7071   "Search for an article containing REGEXP forward.
7072 If BACKWARD, search backward instead."
7073   (interactive
7074    (list (read-string
7075           (format "Search article %s (regexp%s): "
7076                   (if current-prefix-arg "backward" "forward")
7077                   (if gnus-last-search-regexp
7078                       (concat ", default " gnus-last-search-regexp)
7079                     "")))
7080          current-prefix-arg))
7081   (if (string-equal regexp "")
7082       (setq regexp (or gnus-last-search-regexp ""))
7083     (setq gnus-last-search-regexp regexp))
7084   (if (gnus-summary-search-article regexp backward)
7085       (gnus-summary-show-thread)
7086     (error "Search failed: \"%s\"" regexp)))
7087
7088 (defun gnus-summary-search-article-backward (regexp)
7089   "Search for an article containing REGEXP backward."
7090   (interactive
7091    (list (read-string
7092           (format "Search article backward (regexp%s): "
7093                   (if gnus-last-search-regexp
7094                       (concat ", default " gnus-last-search-regexp)
7095                     "")))))
7096   (gnus-summary-search-article-forward regexp 'backward))
7097
7098 (eval-when-compile
7099   (defmacro gnus-summary-search-article-position-point (regexp backward)
7100     "Dehighlight the last matched text and goto the beginning position."
7101     (` (if (and gnus-summary-search-article-matched-data
7102                 (let ((text (caddr gnus-summary-search-article-matched-data))
7103                       (inhibit-read-only t)
7104                       buffer-read-only)
7105                   (delete-region
7106                    (goto-char (car gnus-summary-search-article-matched-data))
7107                    (cadr gnus-summary-search-article-matched-data))
7108                   (insert text)
7109                   (string-match (, regexp) text)))
7110            (if (, backward) (beginning-of-line) (end-of-line))
7111          (goto-char (if (, backward) (point-max) (point-min))))))
7112
7113   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7114     "Place point where X-Face image is displayed."
7115     (if (featurep 'xemacs)
7116         (` (let ((end (if (search-forward "\n\n" nil t)
7117                           (goto-char (1- (point)))
7118                         (point-min)))
7119                  extent)
7120              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7121              (unless (and (re-search-forward "^From:" end t)
7122                           (setq extent (extent-at (point)))
7123                           (extent-begin-glyph extent))
7124                (goto-char (, opoint)))))
7125       (` (let ((end (if (search-forward "\n\n" nil t)
7126                         (goto-char (1- (point)))
7127                       (point-min))))
7128            (goto-char
7129             (or (text-property-any (or (search-backward "\n\n" nil t)
7130                                        (point-min))
7131                                    end 'x-face-mule-bitmap-image t)
7132                 (, opoint)))))))
7133
7134   (defmacro gnus-summary-search-article-highlight-matched-text
7135     (backward treated x-face)
7136     "Highlight matched text in the function `gnus-summary-search-article'."
7137     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7138              (end (set-marker (make-marker) (match-end 0)))
7139              (inhibit-read-only t)
7140              buffer-read-only)
7141          (unless treated
7142            (let ((,@
7143                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7144                     (mapcar
7145                      (lambda (item) (setq items (delq item items)))
7146                      '(gnus-treat-buttonize
7147                        gnus-treat-fill-article
7148                        gnus-treat-fill-long-lines
7149                        gnus-treat-emphasize
7150                        gnus-treat-highlight-headers
7151                        gnus-treat-highlight-citation
7152                        gnus-treat-highlight-signature
7153                        gnus-treat-overstrike
7154                        gnus-treat-display-xface
7155                        gnus-treat-buttonize-head
7156                        gnus-treat-decode-article-as-default-mime-charset))
7157                     (static-if (featurep 'xemacs)
7158                         items
7159                       (cons '(x-face-mule-delete-x-face-field
7160                               (quote never))
7161                             items))))
7162                  (gnus-treat-display-xface
7163                   (when (, x-face) gnus-treat-display-xface)))
7164              (gnus-article-prepare-mime-display)))
7165          (goto-char (if (, backward) start end))
7166          (when (, x-face)
7167            (gnus-summary-search-article-highlight-goto-x-face (point)))
7168          (setq gnus-summary-search-article-matched-data
7169                (list start end (buffer-substring start end)))
7170          (unless (eq start end);; matched text has been deleted. :-<
7171            (put-text-property start end 'face
7172                               (or (find-face 'isearch)
7173                                   'secondary-selection))))))
7174   )
7175
7176 (defun gnus-summary-search-article (regexp &optional backward)
7177   "Search for an article containing REGEXP.
7178 Optional argument BACKWARD means do search for backward.
7179 `gnus-select-article-hook' is not called during the search."
7180   ;; We have to require this here to make sure that the following
7181   ;; dynamic binding isn't shadowed by autoloading.
7182   (require 'gnus-async)
7183   (require 'gnus-art)
7184   (let ((gnus-select-article-hook nil)  ;Disable hook.
7185         (gnus-article-display-hook nil)
7186         (gnus-article-prepare-hook nil)
7187         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7188         (gnus-use-article-prefetch nil)
7189         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7190         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7191         (sum (current-buffer))
7192         (found nil)
7193         point treated)
7194     (gnus-save-hidden-threads
7195       (static-if (featurep 'xemacs)
7196           (let ((gnus-inhibit-treatment t))
7197             (setq treated (eq 'old (gnus-summary-select-article)))
7198             (when (and treated
7199                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7200                                  (window-live-p (get-buffer-window
7201                                                  gnus-article-buffer t)))))
7202               (gnus-summary-select-article nil t)
7203               (setq treated nil)))
7204         (let ((gnus-inhibit-treatment t)
7205               (x-face-mule-delete-x-face-field 'never))
7206           (setq treated (eq 'old (gnus-summary-select-article)))
7207           (when (and treated
7208                      (not
7209                       (and (gnus-buffer-live-p gnus-article-buffer)
7210                            (window-live-p (get-buffer-window
7211                                            gnus-article-buffer t))
7212                            (or (not (string-match "^\\^X-Face:" regexp))
7213                                (with-current-buffer gnus-article-buffer
7214                                  gnus-summary-search-article-matched-data)))))
7215             (gnus-summary-select-article nil t)
7216             (setq treated nil))))
7217       (set-buffer gnus-article-buffer)
7218       (widen)
7219       (if treated
7220           (progn
7221             (gnus-article-show-all-headers)
7222             (gnus-summary-search-article-position-point regexp backward))
7223         (goto-char (if backward (point-max) (point-min))))
7224       (while (not found)
7225         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7226         (if (if backward
7227                 (re-search-backward regexp nil t)
7228               (re-search-forward regexp nil t))
7229             ;; We found the regexp.
7230             (progn
7231               (gnus-summary-search-article-highlight-matched-text
7232                backward treated (string-match "^\\^X-Face:" regexp))
7233               (setq found 'found)
7234               (forward-line
7235                (/ (- 2 (window-height
7236                         (get-buffer-window gnus-article-buffer t)))
7237                   2))
7238               (set-window-start
7239                (get-buffer-window (current-buffer))
7240                (point))
7241               (set-buffer sum)
7242               (setq point (point)))
7243           ;; We didn't find it, so we go to the next article.
7244           (set-buffer sum)
7245           (setq found 'not)
7246           (while (eq found 'not)
7247             (if (not (if backward (gnus-summary-find-prev)
7248                        (gnus-summary-find-next)))
7249                 ;; No more articles.
7250                 (setq found t)
7251               ;; Select the next article and adjust point.
7252               (unless (gnus-summary-article-sparse-p
7253                        (gnus-summary-article-number))
7254                 (setq found nil)
7255                 (let ((gnus-inhibit-treatment t))
7256                   (gnus-summary-select-article))
7257                 (setq treated nil)
7258                 (set-buffer gnus-article-buffer)
7259                 (widen)
7260                 (goto-char (if backward (point-max) (point-min))))))))
7261       (gnus-message 7 ""))
7262     ;; Return whether we found the regexp.
7263     (when (eq found 'found)
7264       (goto-char point)
7265       (gnus-summary-show-thread)
7266       (gnus-summary-goto-subject gnus-current-article)
7267       (gnus-summary-position-point)
7268       t)))
7269
7270 (defun gnus-summary-find-matching (header regexp &optional backward unread
7271                                           not-case-fold)
7272   "Return a list of all articles that match REGEXP on HEADER.
7273 The search stars on the current article and goes forwards unless
7274 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7275 If UNREAD is non-nil, only unread articles will
7276 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7277 in the comparisons."
7278   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7279                 (gnus-data-find-list
7280                  (gnus-summary-article-number) (gnus-data-list backward))))
7281         (case-fold-search (not not-case-fold))
7282         articles d func)
7283     (if (consp header)
7284         (if (eq (car header) 'extra)
7285             (setq func
7286                   `(lambda (h)
7287                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7288                          "")))
7289           (error "%s is an invalid header" header))
7290       (unless (fboundp (intern (concat "mail-header-" header)))
7291         (error "%s is not a valid header" header))
7292       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7293     (while data
7294       (setq d (car data))
7295       (and (or (not unread)             ; We want all articles...
7296                (gnus-data-unread-p d))  ; Or just unreads.
7297            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7298            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7299            (push (gnus-data-number d) articles)) ; Success!
7300       (setq data (cdr data)))
7301     (nreverse articles)))
7302
7303 (defun gnus-summary-execute-command (header regexp command &optional backward)
7304   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7305 If HEADER is an empty string (or nil), the match is done on the entire
7306 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7307   (interactive
7308    (list (let ((completion-ignore-case t))
7309            (completing-read
7310             "Header name: "
7311             (mapcar (lambda (string) (list string))
7312                     '("Number" "Subject" "From" "Lines" "Date"
7313                       "Message-ID" "Xref" "References" "Body"))
7314             nil 'require-match))
7315          (read-string "Regexp: ")
7316          (read-key-sequence "Command: ")
7317          current-prefix-arg))
7318   (when (equal header "Body")
7319     (setq header ""))
7320   ;; Hidden thread subtrees must be searched as well.
7321   (gnus-summary-show-all-threads)
7322   ;; We don't want to change current point nor window configuration.
7323   (save-excursion
7324     (save-window-excursion
7325       (gnus-message 6 "Executing %s..." (key-description command))
7326       ;; We'd like to execute COMMAND interactively so as to give arguments.
7327       (gnus-execute header regexp
7328                     `(call-interactively ',(key-binding command))
7329                     backward)
7330       (gnus-message 6 "Executing %s...done" (key-description command)))))
7331
7332 (defun gnus-summary-beginning-of-article ()
7333   "Scroll the article back to the beginning."
7334   (interactive)
7335   (gnus-summary-select-article)
7336   (gnus-configure-windows 'article)
7337   (gnus-eval-in-buffer-window gnus-article-buffer
7338     (widen)
7339     (goto-char (point-min))
7340     (when gnus-page-broken
7341       (gnus-narrow-to-page))))
7342
7343 (defun gnus-summary-end-of-article ()
7344   "Scroll to the end of the article."
7345   (interactive)
7346   (gnus-summary-select-article)
7347   (gnus-configure-windows 'article)
7348   (gnus-eval-in-buffer-window gnus-article-buffer
7349     (widen)
7350     (goto-char (point-max))
7351     (recenter -3)
7352     (when gnus-page-broken
7353       (gnus-narrow-to-page))))
7354
7355 (defun gnus-summary-print-article (&optional filename n)
7356   "Generate and print a PostScript image of the N next (mail) articles.
7357
7358 If N is negative, print the N previous articles.  If N is nil and articles
7359 have been marked with the process mark, print these instead.
7360
7361 If the optional first argument FILENAME is nil, send the image to the
7362 printer.  If FILENAME is a string, save the PostScript image in a file with
7363 that name.  If FILENAME is a number, prompt the user for the name of the file
7364 to save in."
7365   (interactive (list (ps-print-preprint current-prefix-arg)
7366                      current-prefix-arg))
7367   (dolist (article (gnus-summary-work-articles n))
7368     (gnus-summary-select-article nil nil 'pseudo article)
7369     (gnus-eval-in-buffer-window gnus-article-buffer
7370       (let ((buffer (generate-new-buffer " *print*")))
7371         (unwind-protect
7372             (progn
7373               (copy-to-buffer buffer (point-min) (point-max))
7374               (set-buffer buffer)
7375               (gnus-article-delete-invisible-text)
7376               (let ((ps-left-header
7377                      (list
7378                       (concat "("
7379                               (mail-header-subject gnus-current-headers) ")")
7380                       (concat "("
7381                               (mail-header-from gnus-current-headers) ")")))
7382                     (ps-right-header
7383                      (list
7384                       "/pagenumberstring load"
7385                       (concat "("
7386                               (mail-header-date gnus-current-headers) ")"))))
7387                 (gnus-run-hooks 'gnus-ps-print-hook)
7388                 (save-excursion
7389                   (ps-print-buffer-with-faces filename))))
7390           (kill-buffer buffer))))))
7391
7392 (defun gnus-summary-show-article (&optional arg)
7393   "Force re-fetching of the current article.
7394 If ARG (the prefix) is a number, show the article with the charset
7395 defined in `gnus-summary-show-article-charset-alist', or the charset
7396 inputed.
7397 If ARG (the prefix) is non-nil and not a number, show the raw article
7398 without any article massaging functions being run."
7399   (interactive "P")
7400   (cond
7401    ((numberp arg)
7402     (let ((gnus-newsgroup-charset
7403            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7404                (read-coding-system "Charset: ")))
7405           (gnus-newsgroup-ignored-charsets 'gnus-all))
7406       (gnus-summary-select-article nil 'force)))
7407    ((not arg)
7408     ;; Select the article the normal way.
7409     (gnus-summary-select-article nil 'force))
7410    (t
7411     ;; We have to require this here to make sure that the following
7412     ;; dynamic binding isn't shadowed by autoloading.
7413     (require 'gnus-async)
7414     (require 'gnus-art)
7415     ;; Bind the article treatment functions to nil.
7416     (let ((gnus-have-all-headers t)
7417           gnus-article-display-hook
7418           gnus-article-prepare-hook
7419           gnus-article-decode-hook
7420           gnus-break-pages
7421           gnus-show-mime)
7422       (gnus-summary-select-article nil 'force))))
7423   (gnus-summary-goto-subject gnus-current-article)
7424   (gnus-summary-position-point))
7425
7426 (defun gnus-summary-verbose-headers (&optional arg)
7427   "Toggle permanent full header display.
7428 If ARG is a positive number, turn header display on.
7429 If ARG is a negative number, turn header display off."
7430   (interactive "P")
7431   (setq gnus-show-all-headers
7432         (cond ((or (not (numberp arg))
7433                    (zerop arg))
7434                (not gnus-show-all-headers))
7435               ((natnump arg)
7436                t)))
7437   (gnus-summary-show-article))
7438
7439 (defun gnus-summary-toggle-header (&optional arg)
7440   "Show the headers if they are hidden, or hide them if they are shown.
7441 If ARG is a positive number, show the entire header.
7442 If ARG is a negative number, hide the unwanted header lines."
7443   (interactive "P")
7444   (save-excursion
7445     (set-buffer gnus-article-buffer)
7446     (save-restriction
7447       (let* ((buffer-read-only nil)
7448              (inhibit-point-motion-hooks t)
7449              hidden e)
7450         (setq hidden
7451               (if (numberp arg)
7452                   (>= arg 0)
7453                 (save-restriction
7454                   (article-narrow-to-head)
7455                   (gnus-article-hidden-text-p 'headers))))
7456         (goto-char (point-min))
7457         (when (search-forward "\n\n" nil t)
7458           (delete-region (point-min) (1- (point))))
7459         (goto-char (point-min))
7460         (save-excursion
7461           (set-buffer gnus-original-article-buffer)
7462           (goto-char (point-min))
7463           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7464         (insert-buffer-substring gnus-original-article-buffer 1 e)
7465         (save-restriction
7466           (narrow-to-region (point-min) (point))
7467           (article-decode-encoded-words)
7468           (if  hidden
7469               (let ((gnus-treat-hide-headers nil)
7470                     (gnus-treat-hide-boring-headers nil))
7471                 (setq gnus-article-wash-types
7472                       (delq 'headers gnus-article-wash-types))
7473                 (gnus-treat-article 'head))
7474             (gnus-treat-article 'head)))
7475         (gnus-set-mode-line 'article)))))
7476
7477 (defun gnus-summary-show-all-headers ()
7478   "Make all header lines visible."
7479   (interactive)
7480   (gnus-article-show-all-headers))
7481
7482 (defun gnus-summary-toggle-mime (&optional arg)
7483   "Toggle MIME processing.
7484 If ARG is a positive number, turn MIME processing on."
7485   (interactive "P")
7486   (setq gnus-show-mime
7487         (if (null arg)
7488             (not gnus-show-mime)
7489           (> (prefix-numeric-value arg) 0)))
7490   (gnus-summary-select-article t 'force))
7491
7492 (defun gnus-summary-caesar-message (&optional arg)
7493   "Caesar rotate the current article by 13.
7494 The numerical prefix specifies how many places to rotate each letter
7495 forward."
7496   (interactive "P")
7497   (gnus-summary-select-article)
7498   (let ((mail-header-separator ""))
7499     (gnus-eval-in-buffer-window gnus-article-buffer
7500       (save-restriction
7501         (widen)
7502         (let ((start (window-start))
7503               buffer-read-only)
7504           (message-caesar-buffer-body arg)
7505           (set-window-start (get-buffer-window (current-buffer)) start))))))
7506
7507 (defun gnus-summary-stop-page-breaking ()
7508   "Stop page breaking in the current article."
7509   (interactive)
7510   (gnus-summary-select-article)
7511   (gnus-eval-in-buffer-window gnus-article-buffer
7512     (widen)
7513     (when (gnus-visual-p 'page-marker)
7514       (let ((buffer-read-only nil))
7515         (gnus-remove-text-with-property 'gnus-prev)
7516         (gnus-remove-text-with-property 'gnus-next))
7517       (setq gnus-page-broken nil))))
7518
7519 (defun gnus-summary-move-article (&optional n to-newsgroup
7520                                             select-method action)
7521   "Move the current article to a different newsgroup.
7522 If N is a positive number, move the N next articles.
7523 If N is a negative number, move the N previous articles.
7524 If N is nil and any articles have been marked with the process mark,
7525 move those articles instead.
7526 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7527 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7528 re-spool using this method.
7529
7530 For this function to work, both the current newsgroup and the
7531 newsgroup that you want to move to have to support the `request-move'
7532 and `request-accept' functions.
7533
7534 ACTION can be either `move' (the default), `crosspost' or `copy'."
7535   (interactive "P")
7536   (unless action
7537     (setq action 'move))
7538   ;; Disable marking as read.
7539   (let (gnus-mark-article-hook)
7540     (save-window-excursion
7541       (gnus-summary-select-article)))
7542   ;; Check whether the source group supports the required functions.
7543   (cond ((and (eq action 'move)
7544               (not (gnus-check-backend-function
7545                     'request-move-article gnus-newsgroup-name)))
7546          (error "The current group does not support article moving"))
7547         ((and (eq action 'crosspost)
7548               (not (gnus-check-backend-function
7549                     'request-replace-article gnus-newsgroup-name)))
7550          (error "The current group does not support article editing")))
7551   (let ((articles (gnus-summary-work-articles n))
7552         (prefix (if (gnus-check-backend-function
7553                     'request-move-article gnus-newsgroup-name)
7554                     (gnus-group-real-prefix gnus-newsgroup-name)
7555                   ""))
7556         (names '((move "Move" "Moving")
7557                  (copy "Copy" "Copying")
7558                  (crosspost "Crosspost" "Crossposting")))
7559         (copy-buf (save-excursion
7560                     (nnheader-set-temp-buffer " *copy article*")))
7561         (default-marks gnus-article-mark-lists)
7562         (no-expire-marks (delete '(expirable . expire)
7563                                  (copy-sequence gnus-article-mark-lists)))
7564         art-group to-method new-xref article to-groups)
7565     (unless (assq action names)
7566       (error "Unknown action %s" action))
7567     ;; Read the newsgroup name.
7568     (when (and (not to-newsgroup)
7569                (not select-method))
7570       (setq to-newsgroup
7571             (gnus-read-move-group-name
7572              (cadr (assq action names))
7573              (symbol-value (intern (format "gnus-current-%s-group" action)))
7574              articles prefix))
7575       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7576     (setq to-method (or select-method
7577                         (gnus-server-to-method
7578                          (gnus-group-method to-newsgroup))))
7579     ;; Check the method we are to move this article to...
7580     (unless (gnus-check-backend-function
7581              'request-accept-article (car to-method))
7582       (error "%s does not support article copying" (car to-method)))
7583     (unless (gnus-check-server to-method)
7584       (error "Can't open server %s" (car to-method)))
7585     (gnus-message 6 "%s to %s: %s..."
7586                   (caddr (assq action names))
7587                   (or (car select-method) to-newsgroup) articles)
7588     (while articles
7589       (setq article (pop articles))
7590       (setq
7591        art-group
7592        (cond
7593         ;; Move the article.
7594         ((eq action 'move)
7595          ;; Remove this article from future suppression.
7596          (gnus-dup-unsuppress-article article)
7597          (gnus-request-move-article
7598           article                       ; Article to move
7599           gnus-newsgroup-name           ; From newsgroup
7600           (nth 1 (gnus-find-method-for-group
7601                   gnus-newsgroup-name)) ; Server
7602           (list 'gnus-request-accept-article
7603                 to-newsgroup (list 'quote select-method)
7604                 (not articles) t)       ; Accept form
7605           (not articles)))              ; Only save nov last time
7606         ;; Copy the article.
7607         ((eq action 'copy)
7608          (save-excursion
7609            (set-buffer copy-buf)
7610            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7611              (gnus-request-accept-article
7612               to-newsgroup select-method (not articles) t))))
7613         ;; Crosspost the article.
7614         ((eq action 'crosspost)
7615          (let ((xref (message-tokenize-header
7616                       (mail-header-xref (gnus-summary-article-header article))
7617                       " ")))
7618            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7619                                   ":" article))
7620            (unless xref
7621              (setq xref (list (system-name))))
7622            (setq new-xref
7623                  (concat
7624                   (mapconcat 'identity
7625                              (delete "Xref:" (delete new-xref xref))
7626                              " ")
7627                   " " new-xref))
7628            (save-excursion
7629              (set-buffer copy-buf)
7630              ;; First put the article in the destination group.
7631              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7632              (when (consp (setq art-group
7633                                 (gnus-request-accept-article
7634                                  to-newsgroup select-method (not articles))))
7635                (setq new-xref (concat new-xref " " (car art-group)
7636                                       ":" (cdr art-group)))
7637                ;; Now we have the new Xrefs header, so we insert
7638                ;; it and replace the new article.
7639                (nnheader-replace-header "Xref" new-xref)
7640                (gnus-request-replace-article
7641                 (cdr art-group) to-newsgroup (current-buffer))
7642                art-group))))))
7643       (cond
7644        ((not art-group)
7645         (gnus-message 1 "Couldn't %s article %s: %s"
7646                       (cadr (assq action names)) article
7647                       (nnheader-get-report (car to-method))))
7648        ((eq art-group 'junk)
7649         (when (eq action 'move)
7650           (gnus-summary-mark-article article gnus-canceled-mark)
7651           (gnus-message 4 "Deleted article %s" article)))
7652        (t
7653         (let* ((pto-group (gnus-group-prefixed-name
7654                            (car art-group) to-method))
7655                (entry
7656                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7657                (info (nth 2 entry))
7658                (to-group (gnus-info-group info))
7659                to-marks)
7660           ;; Update the group that has been moved to.
7661           (when (and info
7662                      (memq action '(move copy)))
7663             (unless (member to-group to-groups)
7664               (push to-group to-groups))
7665
7666             (unless (memq article gnus-newsgroup-unreads)
7667               (push 'read to-marks)
7668               (gnus-info-set-read
7669                info (gnus-add-to-range (gnus-info-read info)
7670                                        (list (cdr art-group)))))
7671
7672             ;; See whether the article is to be put in the cache.
7673             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7674                              default-marks
7675                            no-expire-marks))
7676                   (to-article (cdr art-group)))
7677
7678               ;; Enter the article into the cache in the new group,
7679               ;; if that is required.
7680               (when gnus-use-cache
7681                 (gnus-cache-possibly-enter-article
7682                  to-group to-article
7683                  (let ((header (copy-sequence
7684                                 (gnus-summary-article-header article))))
7685                    (mail-header-set-number header to-article)
7686                    header)
7687                  (memq article gnus-newsgroup-marked)
7688                  (memq article gnus-newsgroup-dormant)
7689                  (memq article gnus-newsgroup-unreads)))
7690
7691               (when gnus-preserve-marks
7692                 ;; Copy any marks over to the new group.
7693                 (when (and (equal to-group gnus-newsgroup-name)
7694                            (not (memq article gnus-newsgroup-unreads)))
7695                   ;; Mark this article as read in this group.
7696                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7697                   (setcdr (gnus-active to-group) to-article)
7698                   (setcdr gnus-newsgroup-active to-article))
7699
7700                 (while marks
7701                   (when (memq article (symbol-value
7702                                        (intern (format "gnus-newsgroup-%s"
7703                                                        (caar marks)))))
7704                     (push (cdar marks) to-marks)
7705                     ;; If the other group is the same as this group,
7706                     ;; then we have to add the mark to the list.
7707                     (when (equal to-group gnus-newsgroup-name)
7708                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7709                            (cons to-article
7710                                  (symbol-value
7711                                   (intern (format "gnus-newsgroup-%s"
7712                                                   (caar marks)))))))
7713                     ;; Copy the marks to other group.
7714                     (gnus-add-marked-articles
7715                      to-group (cdar marks) (list to-article) info))
7716                   (setq marks (cdr marks)))
7717
7718                 (gnus-request-set-mark to-group (list (list (list to-article)
7719                                                             'set
7720                                                             to-marks))))
7721
7722               (gnus-dribble-enter
7723                (concat "(gnus-group-set-info '"
7724                        (gnus-prin1-to-string (gnus-get-info to-group))
7725                        ")"))))
7726
7727           ;; Update the Xref header in this article to point to
7728           ;; the new crossposted article we have just created.
7729           (when (eq action 'crosspost)
7730             (save-excursion
7731               (set-buffer copy-buf)
7732               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7733               (nnheader-replace-header "Xref" new-xref)
7734               (gnus-request-replace-article
7735                article gnus-newsgroup-name (current-buffer)))))
7736
7737         ;;;!!!Why is this necessary?
7738         (set-buffer gnus-summary-buffer)
7739
7740         (gnus-summary-goto-subject article)
7741         (when (eq action 'move)
7742           (gnus-summary-mark-article article gnus-canceled-mark))))
7743       (gnus-summary-remove-process-mark article))
7744     ;; Re-activate all groups that have been moved to.
7745     (while to-groups
7746       (save-excursion
7747         (set-buffer gnus-group-buffer)
7748         (when (gnus-group-goto-group (car to-groups) t)
7749           (gnus-group-get-new-news-this-group 1 t))
7750         (pop to-groups)))
7751
7752     (gnus-kill-buffer copy-buf)
7753     (gnus-summary-position-point)
7754     (gnus-set-mode-line 'summary)))
7755
7756 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7757   "Move the current article to a different newsgroup.
7758 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7759 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7760 re-spool using this method."
7761   (interactive "P")
7762   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7763
7764 (defun gnus-summary-crosspost-article (&optional n)
7765   "Crosspost the current article to some other group."
7766   (interactive "P")
7767   (gnus-summary-move-article n nil nil 'crosspost))
7768
7769 (defcustom gnus-summary-respool-default-method nil
7770   "Default method for respooling an article.
7771 If nil, use to the current newsgroup method."
7772   :type '(choice (gnus-select-method :value (nnml ""))
7773                  (const nil))
7774   :group 'gnus-summary-mail)
7775
7776 (defun gnus-summary-respool-article (&optional n method)
7777   "Respool the current article.
7778 The article will be squeezed through the mail spooling process again,
7779 which means that it will be put in some mail newsgroup or other
7780 depending on `nnmail-split-methods'.
7781 If N is a positive number, respool the N next articles.
7782 If N is a negative number, respool the N previous articles.
7783 If N is nil and any articles have been marked with the process mark,
7784 respool those articles instead.
7785
7786 Respooling can be done both from mail groups and \"real\" newsgroups.
7787 In the former case, the articles in question will be moved from the
7788 current group into whatever groups they are destined to.  In the
7789 latter case, they will be copied into the relevant groups."
7790   (interactive
7791    (list current-prefix-arg
7792          (let* ((methods (gnus-methods-using 'respool))
7793                 (methname
7794                  (symbol-name (or gnus-summary-respool-default-method
7795                                   (car (gnus-find-method-for-group
7796                                         gnus-newsgroup-name)))))
7797                 (method
7798                  (gnus-completing-read
7799                   methname "What backend do you want to use when respooling?"
7800                   methods nil t nil 'gnus-mail-method-history))
7801                 ms)
7802            (cond
7803             ((zerop (length (setq ms (gnus-servers-using-backend
7804                                       (intern method)))))
7805              (list (intern method) ""))
7806             ((= 1 (length ms))
7807              (car ms))
7808             (t
7809              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7810                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7811                            ms-alist))))))))
7812   (unless method
7813     (error "No method given for respooling"))
7814   (if (assoc (symbol-name
7815               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7816              (gnus-methods-using 'respool))
7817       (gnus-summary-move-article n nil method)
7818     (gnus-summary-copy-article n nil method)))
7819
7820 (defun gnus-summary-import-article (file)
7821   "Import an arbitrary file into a mail newsgroup."
7822   (interactive "fImport file: ")
7823   (let ((group gnus-newsgroup-name)
7824         (now (current-time))
7825         atts lines)
7826     (unless (gnus-check-backend-function 'request-accept-article group)
7827       (error "%s does not support article importing" group))
7828     (or (file-readable-p file)
7829         (not (file-regular-p file))
7830         (error "Can't read %s" file))
7831     (save-excursion
7832       (set-buffer (gnus-get-buffer-create " *import file*"))
7833       (erase-buffer)
7834       (nnheader-insert-file-contents file)
7835       (goto-char (point-min))
7836       (unless (nnheader-article-p)
7837         ;; This doesn't look like an article, so we fudge some headers.
7838         (setq atts (file-attributes file)
7839               lines (count-lines (point-min) (point-max)))
7840         (insert "From: " (read-string "From: ") "\n"
7841                 "Subject: " (read-string "Subject: ") "\n"
7842                 "Date: " (message-make-date (nth 5 atts))
7843                 "\n"
7844                 "Message-ID: " (message-make-message-id) "\n"
7845                 "Lines: " (int-to-string lines) "\n"
7846                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7847       (gnus-request-accept-article group nil t)
7848       (kill-buffer (current-buffer)))))
7849
7850 (defun gnus-summary-article-posted-p ()
7851   "Say whether the current (mail) article is available from news as well.
7852 This will be the case if the article has both been mailed and posted."
7853   (interactive)
7854   (let ((id (mail-header-references (gnus-summary-article-header)))
7855         (gnus-override-method (car (gnus-refer-article-methods))))
7856     (if (gnus-request-head id "")
7857         (gnus-message 2 "The current message was found on %s"
7858                       gnus-override-method)
7859       (gnus-message 2 "The current message couldn't be found on %s"
7860                     gnus-override-method)
7861       nil)))
7862
7863 (defun gnus-summary-expire-articles (&optional now)
7864   "Expire all articles that are marked as expirable in the current group."
7865   (interactive)
7866   (when (gnus-check-backend-function
7867          'request-expire-articles gnus-newsgroup-name)
7868     ;; This backend supports expiry.
7869     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7870            (expirable (if total
7871                           (progn
7872                             ;; We need to update the info for
7873                             ;; this group for `gnus-list-of-read-articles'
7874                             ;; to give us the right answer.
7875                             (gnus-run-hooks 'gnus-exit-group-hook)
7876                             (gnus-summary-update-info)
7877                             (gnus-list-of-read-articles gnus-newsgroup-name))
7878                         (setq gnus-newsgroup-expirable
7879                               (sort gnus-newsgroup-expirable '<))))
7880            (expiry-wait (if now 'immediate
7881                           (gnus-group-find-parameter
7882                            gnus-newsgroup-name 'expiry-wait)))
7883            (nnmail-expiry-target
7884             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7885                 nnmail-expiry-target))
7886            es)
7887       (when expirable
7888         ;; There are expirable articles in this group, so we run them
7889         ;; through the expiry process.
7890         (gnus-message 6 "Expiring articles...")
7891         (unless (gnus-check-group gnus-newsgroup-name)
7892           (error "Can't open server for %s" gnus-newsgroup-name))
7893         ;; The list of articles that weren't expired is returned.
7894         (save-excursion
7895           (if expiry-wait
7896               (let ((nnmail-expiry-wait-function nil)
7897                     (nnmail-expiry-wait expiry-wait))
7898                 (setq es (gnus-request-expire-articles
7899                           expirable gnus-newsgroup-name)))
7900             (setq es (gnus-request-expire-articles
7901                       expirable gnus-newsgroup-name))))
7902         (unless total
7903           (setq gnus-newsgroup-expirable es))
7904         ;; We go through the old list of expirable, and mark all
7905         ;; really expired articles as nonexistent.
7906         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7907           (let ((gnus-use-cache nil))
7908             (while expirable
7909               (unless (memq (car expirable) es)
7910                 (when (gnus-data-find (car expirable))
7911                   (gnus-summary-mark-article
7912                    (car expirable) gnus-canceled-mark)))
7913               (setq expirable (cdr expirable)))))
7914         (gnus-message 6 "Expiring articles...done")))))
7915
7916 (defun gnus-summary-expire-articles-now ()
7917   "Expunge all expirable articles in the current group.
7918 This means that *all* articles that are marked as expirable will be
7919 deleted forever, right now."
7920   (interactive)
7921   (or gnus-expert-user
7922       (gnus-yes-or-no-p
7923        "Are you really, really, really sure you want to delete all these messages? ")
7924       (error "Phew!"))
7925   (gnus-summary-expire-articles t))
7926
7927 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7928 (defun gnus-summary-delete-article (&optional n)
7929   "Delete the N next (mail) articles.
7930 This command actually deletes articles.  This is not a marking
7931 command.  The article will disappear forever from your life, never to
7932 return.
7933 If N is negative, delete backwards.
7934 If N is nil and articles have been marked with the process mark,
7935 delete these instead."
7936   (interactive "P")
7937   (unless (gnus-check-backend-function 'request-expire-articles
7938                                        gnus-newsgroup-name)
7939     (error "The current newsgroup does not support article deletion"))
7940   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7941     (error "Couldn't open server"))
7942   ;; Compute the list of articles to delete.
7943   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7944         not-deleted)
7945     (if (and gnus-novice-user
7946              (not (gnus-yes-or-no-p
7947                    (format "Do you really want to delete %s forever? "
7948                            (if (> (length articles) 1)
7949                                (format "these %s articles" (length articles))
7950                              "this article")))))
7951         ()
7952       ;; Delete the articles.
7953       (setq not-deleted (gnus-request-expire-articles
7954                          articles gnus-newsgroup-name 'force))
7955       (while articles
7956         (gnus-summary-remove-process-mark (car articles))
7957         ;; The backend might not have been able to delete the article
7958         ;; after all.
7959         (unless (memq (car articles) not-deleted)
7960           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7961         (setq articles (cdr articles)))
7962       (when not-deleted
7963         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7964     (gnus-summary-position-point)
7965     (gnus-set-mode-line 'summary)
7966     not-deleted))
7967
7968 (defun gnus-summary-edit-article (&optional force)
7969   "Edit the current article.
7970 This will have permanent effect only in mail groups.
7971 If FORCE is non-nil, allow editing of articles even in read-only
7972 groups."
7973   (interactive "P")
7974   (save-excursion
7975     (set-buffer gnus-summary-buffer)
7976     (let ((mail-parse-charset gnus-newsgroup-charset)
7977           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7978       (gnus-set-global-variables)
7979       (when (and (not force)
7980                  (gnus-group-read-only-p))
7981         (error "The current newsgroup does not support article editing"))
7982       (gnus-summary-show-article t)
7983       (gnus-article-edit-article
7984        'ignore
7985        `(lambda (no-highlight)
7986           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7987                 (mail-parse-ignored-charsets
7988                  ',gnus-newsgroup-ignored-charsets))
7989             (gnus-summary-edit-article-done
7990              ,(or (mail-header-references gnus-current-headers) "")
7991              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7992
7993 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7994
7995 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7996                                                  no-highlight)
7997   "Make edits to the current article permanent."
7998   (interactive)
7999   ;; Replace the article.
8000   (let ((buf (current-buffer)))
8001     (with-temp-buffer
8002       (insert-buffer-substring buf)
8003       (if (and (not read-only)
8004                (not (gnus-request-replace-article
8005                      (cdr gnus-article-current) (car gnus-article-current)
8006                      (current-buffer) t)))
8007           (error "Couldn't replace article")
8008         ;; Update the summary buffer.
8009         (if (and references
8010                  (equal (message-tokenize-header references " ")
8011                         (message-tokenize-header
8012                          (or (message-fetch-field "references") "") " ")))
8013             ;; We only have to update this line.
8014             (save-excursion
8015               (save-restriction
8016                 (message-narrow-to-head)
8017                 (let ((head (buffer-string))
8018                       header)
8019                   (with-temp-buffer
8020                     (insert (format "211 %d Article retrieved.\n"
8021                                     (cdr gnus-article-current)))
8022                     (insert head)
8023                     (insert ".\n")
8024                     (let ((nntp-server-buffer (current-buffer)))
8025                       (setq header (car (gnus-get-newsgroup-headers
8026                                          (save-excursion
8027                                            (set-buffer gnus-summary-buffer)
8028                                            gnus-newsgroup-dependencies)
8029                                          t))))
8030                     (save-excursion
8031                       (set-buffer gnus-summary-buffer)
8032                       (gnus-data-set-header
8033                        (gnus-data-find (cdr gnus-article-current))
8034                        header)
8035                       (gnus-summary-update-article-line
8036                        (cdr gnus-article-current) header))))))
8037           ;; Update threads.
8038           (set-buffer (or buffer gnus-summary-buffer))
8039           (gnus-summary-update-article (cdr gnus-article-current)))
8040         ;; Prettify the article buffer again.
8041         (unless no-highlight
8042           (save-excursion
8043             (set-buffer gnus-article-buffer)
8044             ;;;!!! Fix this -- article should be rehighlighted.
8045             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8046             (set-buffer gnus-original-article-buffer)
8047             (gnus-request-article
8048              (cdr gnus-article-current)
8049              (car gnus-article-current) (current-buffer))))
8050         ;; Prettify the summary buffer line.
8051         (when (gnus-visual-p 'summary-highlight 'highlight)
8052           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8053
8054 (defun gnus-summary-edit-wash (key)
8055   "Perform editing command KEY in the article buffer."
8056   (interactive
8057    (list
8058     (progn
8059       (message "%s" (concat (this-command-keys) "- "))
8060       (read-char))))
8061   (message "")
8062   (gnus-summary-edit-article)
8063   (execute-kbd-macro (concat (this-command-keys) key))
8064   (gnus-article-edit-done))
8065
8066 ;;; Respooling
8067
8068 (defun gnus-summary-respool-query (&optional silent trace)
8069   "Query where the respool algorithm would put this article."
8070   (interactive)
8071   (let (gnus-mark-article-hook)
8072     (gnus-summary-select-article)
8073     (save-excursion
8074       (set-buffer gnus-original-article-buffer)
8075       (save-restriction
8076         (message-narrow-to-head)
8077         (let ((groups (nnmail-article-group 'identity trace)))
8078           (unless silent
8079             (if groups
8080                 (message "This message would go to %s"
8081                          (mapconcat 'car groups ", "))
8082               (message "This message would go to no groups"))
8083             groups))))))
8084
8085 (defun gnus-summary-respool-trace ()
8086   "Trace where the respool algorithm would put this article.
8087 Display a buffer showing all fancy splitting patterns which matched."
8088   (interactive)
8089   (gnus-summary-respool-query nil t))
8090
8091 ;; Summary marking commands.
8092
8093 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8094   "Mark articles which has the same subject as read, and then select the next.
8095 If UNMARK is positive, remove any kind of mark.
8096 If UNMARK is negative, tick articles."
8097   (interactive "P")
8098   (when unmark
8099     (setq unmark (prefix-numeric-value unmark)))
8100   (let ((count
8101          (gnus-summary-mark-same-subject
8102           (gnus-summary-article-subject) unmark)))
8103     ;; Select next unread article.  If auto-select-same mode, should
8104     ;; select the first unread article.
8105     (gnus-summary-next-article t (and gnus-auto-select-same
8106                                       (gnus-summary-article-subject)))
8107     (gnus-message 7 "%d article%s marked as %s"
8108                   count (if (= count 1) " is" "s are")
8109                   (if unmark "unread" "read"))))
8110
8111 (defun gnus-summary-kill-same-subject (&optional unmark)
8112   "Mark articles which has the same subject as read.
8113 If UNMARK is positive, remove any kind of mark.
8114 If UNMARK is negative, tick articles."
8115   (interactive "P")
8116   (when unmark
8117     (setq unmark (prefix-numeric-value unmark)))
8118   (let ((count
8119          (gnus-summary-mark-same-subject
8120           (gnus-summary-article-subject) unmark)))
8121     ;; If marked as read, go to next unread subject.
8122     (when (null unmark)
8123       ;; Go to next unread subject.
8124       (gnus-summary-next-subject 1 t))
8125     (gnus-message 7 "%d articles are marked as %s"
8126                   count (if unmark "unread" "read"))))
8127
8128 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8129   "Mark articles with same SUBJECT as read, and return marked number.
8130 If optional argument UNMARK is positive, remove any kinds of marks.
8131 If optional argument UNMARK is negative, mark articles as unread instead."
8132   (let ((count 1))
8133     (save-excursion
8134       (cond
8135        ((null unmark)                   ; Mark as read.
8136         (while (and
8137                 (progn
8138                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8139                   (gnus-summary-show-thread) t)
8140                 (gnus-summary-find-subject subject))
8141           (setq count (1+ count))))
8142        ((> unmark 0)                    ; Tick.
8143         (while (and
8144                 (progn
8145                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8146                   (gnus-summary-show-thread) t)
8147                 (gnus-summary-find-subject subject))
8148           (setq count (1+ count))))
8149        (t                               ; Mark as unread.
8150         (while (and
8151                 (progn
8152                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8153                   (gnus-summary-show-thread) t)
8154                 (gnus-summary-find-subject subject))
8155           (setq count (1+ count)))))
8156       (gnus-set-mode-line 'summary)
8157       ;; Return the number of marked articles.
8158       count)))
8159
8160 (defun gnus-summary-mark-as-processable (n &optional unmark)
8161   "Set the process mark on the next N articles.
8162 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8163 the process mark instead.  The difference between N and the actual
8164 number of articles marked is returned."
8165   (interactive "p")
8166   (let ((backward (< n 0))
8167         (n (abs n)))
8168     (while (and
8169             (> n 0)
8170             (if unmark
8171                 (gnus-summary-remove-process-mark
8172                  (gnus-summary-article-number))
8173               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8174             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8175       (setq n (1- n)))
8176     (when (/= 0 n)
8177       (gnus-message 7 "No more articles"))
8178     (gnus-summary-recenter)
8179     (gnus-summary-position-point)
8180     n))
8181
8182 (defun gnus-summary-unmark-as-processable (n)
8183   "Remove the process mark from the next N articles.
8184 If N is negative, unmark backward instead.  The difference between N and
8185 the actual number of articles unmarked is returned."
8186   (interactive "p")
8187   (gnus-summary-mark-as-processable n t))
8188
8189 (defun gnus-summary-unmark-all-processable ()
8190   "Remove the process mark from all articles."
8191   (interactive)
8192   (save-excursion
8193     (while gnus-newsgroup-processable
8194       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8195   (gnus-summary-position-point))
8196
8197 (defun gnus-summary-mark-as-expirable (n)
8198   "Mark N articles forward as expirable.
8199 If N is negative, mark backward instead.  The difference between N and
8200 the actual number of articles marked is returned."
8201   (interactive "p")
8202   (gnus-summary-mark-forward n gnus-expirable-mark))
8203
8204 (defun gnus-summary-mark-article-as-replied (article)
8205   "Mark ARTICLE replied and update the summary line."
8206   (push article gnus-newsgroup-replied)
8207   (let ((buffer-read-only nil))
8208     (when (gnus-summary-goto-subject article nil t)
8209       (gnus-summary-update-secondary-mark article))))
8210
8211 (defun gnus-summary-set-bookmark (article)
8212   "Set a bookmark in current article."
8213   (interactive (list (gnus-summary-article-number)))
8214   (when (or (not (get-buffer gnus-article-buffer))
8215             (not gnus-current-article)
8216             (not gnus-article-current)
8217             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8218     (error "No current article selected"))
8219   ;; Remove old bookmark, if one exists.
8220   (let ((old (assq article gnus-newsgroup-bookmarks)))
8221     (when old
8222       (setq gnus-newsgroup-bookmarks
8223             (delq old gnus-newsgroup-bookmarks))))
8224   ;; Set the new bookmark, which is on the form
8225   ;; (article-number . line-number-in-body).
8226   (push
8227    (cons article
8228          (save-excursion
8229            (set-buffer gnus-article-buffer)
8230            (count-lines
8231             (min (point)
8232                  (save-excursion
8233                    (goto-char (point-min))
8234                    (search-forward "\n\n" nil t)
8235                    (point)))
8236             (point))))
8237    gnus-newsgroup-bookmarks)
8238   (gnus-message 6 "A bookmark has been added to the current article."))
8239
8240 (defun gnus-summary-remove-bookmark (article)
8241   "Remove the bookmark from the current article."
8242   (interactive (list (gnus-summary-article-number)))
8243   ;; Remove old bookmark, if one exists.
8244   (let ((old (assq article gnus-newsgroup-bookmarks)))
8245     (if old
8246         (progn
8247           (setq gnus-newsgroup-bookmarks
8248                 (delq old gnus-newsgroup-bookmarks))
8249           (gnus-message 6 "Removed bookmark."))
8250       (gnus-message 6 "No bookmark in current article."))))
8251
8252 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8253 (defun gnus-summary-mark-as-dormant (n)
8254   "Mark N articles forward as dormant.
8255 If N is negative, mark backward instead.  The difference between N and
8256 the actual number of articles marked is returned."
8257   (interactive "p")
8258   (gnus-summary-mark-forward n gnus-dormant-mark))
8259
8260 (defun gnus-summary-set-process-mark (article)
8261   "Set the process mark on ARTICLE and update the summary line."
8262   (setq gnus-newsgroup-processable
8263         (cons article
8264               (delq article gnus-newsgroup-processable)))
8265   (when (gnus-summary-goto-subject article)
8266     (gnus-summary-show-thread)
8267     (gnus-summary-goto-subject article)
8268     (gnus-summary-update-secondary-mark article)))
8269
8270 (defun gnus-summary-remove-process-mark (article)
8271   "Remove the process mark from ARTICLE and update the summary line."
8272   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8273   (when (gnus-summary-goto-subject article)
8274     (gnus-summary-show-thread)
8275     (gnus-summary-goto-subject article)
8276     (gnus-summary-update-secondary-mark article)))
8277
8278 (defun gnus-summary-set-saved-mark (article)
8279   "Set the process mark on ARTICLE and update the summary line."
8280   (push article gnus-newsgroup-saved)
8281   (when (gnus-summary-goto-subject article)
8282     (gnus-summary-update-secondary-mark article)))
8283
8284 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8285   "Mark N articles as read forwards.
8286 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8287 The difference between N and the actual number of articles marked is
8288 returned.
8289 Iff NO-EXPIRE, auto-expiry will be inhibited."
8290   (interactive "p")
8291   (gnus-summary-show-thread)
8292   (let ((backward (< n 0))
8293         (gnus-summary-goto-unread
8294          (and gnus-summary-goto-unread
8295               (not (eq gnus-summary-goto-unread 'never))
8296               (not (memq mark (list gnus-unread-mark
8297                                     gnus-ticked-mark gnus-dormant-mark)))))
8298         (n (abs n))
8299         (mark (or mark gnus-del-mark)))
8300     (while (and (> n 0)
8301                 (gnus-summary-mark-article nil mark no-expire)
8302                 (zerop (gnus-summary-next-subject
8303                         (if backward -1 1)
8304                         (and gnus-summary-goto-unread
8305                              (not (eq gnus-summary-goto-unread 'never)))
8306                         t)))
8307       (setq n (1- n)))
8308     (when (/= 0 n)
8309       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8310     (gnus-summary-recenter)
8311     (gnus-summary-position-point)
8312     (gnus-set-mode-line 'summary)
8313     n))
8314
8315 (defun gnus-summary-mark-article-as-read (mark)
8316   "Mark the current article quickly as read with MARK."
8317   (let ((article (gnus-summary-article-number)))
8318     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8319     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8320     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8321     (push (cons article mark) gnus-newsgroup-reads)
8322     ;; Possibly remove from cache, if that is used.
8323     (when gnus-use-cache
8324       (gnus-cache-enter-remove-article article))
8325     ;; Allow the backend to change the mark.
8326     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8327     ;; Check for auto-expiry.
8328     (when (and gnus-newsgroup-auto-expire
8329                (memq mark gnus-auto-expirable-marks))
8330       (setq mark gnus-expirable-mark)
8331       ;; Let the backend know about the mark change.
8332       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8333       (push article gnus-newsgroup-expirable))
8334     ;; Set the mark in the buffer.
8335     (gnus-summary-update-mark mark 'unread)
8336     t))
8337
8338 (defun gnus-summary-mark-article-as-unread (mark)
8339   "Mark the current article quickly as unread with MARK."
8340   (let* ((article (gnus-summary-article-number))
8341          (old-mark (gnus-summary-article-mark article)))
8342     ;; Allow the backend to change the mark.
8343     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8344     (if (eq mark old-mark)
8345         t
8346       (if (<= article 0)
8347           (progn
8348             (gnus-error 1 "Can't mark negative article numbers")
8349             nil)
8350         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8351         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8352         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8353         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8354         (cond ((= mark gnus-ticked-mark)
8355                (push article gnus-newsgroup-marked))
8356               ((= mark gnus-dormant-mark)
8357                (push article gnus-newsgroup-dormant))
8358               (t
8359                (push article gnus-newsgroup-unreads)))
8360         (gnus-pull article gnus-newsgroup-reads)
8361
8362         ;; See whether the article is to be put in the cache.
8363         (and gnus-use-cache
8364              (vectorp (gnus-summary-article-header article))
8365              (save-excursion
8366                (gnus-cache-possibly-enter-article
8367                 gnus-newsgroup-name article
8368                 (gnus-summary-article-header article)
8369                 (= mark gnus-ticked-mark)
8370                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8371
8372         ;; Fix the mark.
8373         (gnus-summary-update-mark mark 'unread)
8374         t))))
8375
8376 (defun gnus-summary-mark-article (&optional article mark no-expire)
8377   "Mark ARTICLE with MARK.  MARK can be any character.
8378 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8379 `??' (dormant) and `?E' (expirable).
8380 If MARK is nil, then the default character `?r' is used.
8381 If ARTICLE is nil, then the article on the current line will be
8382 marked.
8383 Iff NO-EXPIRE, auto-expiry will be inhibited."
8384   ;; The mark might be a string.
8385   (when (stringp mark)
8386     (setq mark (aref mark 0)))
8387   ;; If no mark is given, then we check auto-expiring.
8388   (when (null mark)
8389     (setq mark gnus-del-mark))
8390   (when (and (not no-expire)
8391              gnus-newsgroup-auto-expire
8392              (memq mark gnus-auto-expirable-marks))
8393     (setq mark gnus-expirable-mark))
8394   (let ((article (or article (gnus-summary-article-number)))
8395         (old-mark (gnus-summary-article-mark article)))
8396     ;; Allow the backend to change the mark.
8397     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8398     (if (eq mark old-mark)
8399         t
8400       (unless article
8401         (error "No article on current line"))
8402       (if (not (if (or (= mark gnus-unread-mark)
8403                        (= mark gnus-ticked-mark)
8404                        (= mark gnus-dormant-mark))
8405                    (gnus-mark-article-as-unread article mark)
8406                  (gnus-mark-article-as-read article mark)))
8407           t
8408         ;; See whether the article is to be put in the cache.
8409         (and gnus-use-cache
8410              (not (= mark gnus-canceled-mark))
8411              (vectorp (gnus-summary-article-header article))
8412              (save-excursion
8413                (gnus-cache-possibly-enter-article
8414                 gnus-newsgroup-name article
8415                 (gnus-summary-article-header article)
8416                 (= mark gnus-ticked-mark)
8417                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8418
8419         (when (gnus-summary-goto-subject article nil t)
8420           (let ((buffer-read-only nil))
8421             (gnus-summary-show-thread)
8422             ;; Fix the mark.
8423             (gnus-summary-update-mark mark 'unread)
8424             t))))))
8425
8426 (defun gnus-summary-update-secondary-mark (article)
8427   "Update the secondary (read, process, cache) mark."
8428   (gnus-summary-update-mark
8429    (cond ((memq article gnus-newsgroup-processable)
8430           gnus-process-mark)
8431          ((memq article gnus-newsgroup-cached)
8432           gnus-cached-mark)
8433          ((memq article gnus-newsgroup-replied)
8434           gnus-replied-mark)
8435          ((memq article gnus-newsgroup-saved)
8436           gnus-saved-mark)
8437          (t gnus-unread-mark))
8438    'replied)
8439   (when (gnus-visual-p 'summary-highlight 'highlight)
8440     (gnus-run-hooks 'gnus-summary-update-hook))
8441   t)
8442
8443 (defun gnus-summary-update-mark (mark type)
8444   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8445         (buffer-read-only nil))
8446     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8447     (when forward
8448       (when (looking-at "\r")
8449         (incf forward))
8450       (when (<= (+ forward (point)) (point-max))
8451         ;; Go to the right position on the line.
8452         (goto-char (+ forward (point)))
8453         ;; Replace the old mark with the new mark.
8454         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8455         ;; Optionally update the marks by some user rule.
8456         (when (eq type 'unread)
8457           (gnus-data-set-mark
8458            (gnus-data-find (gnus-summary-article-number)) mark)
8459           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8460
8461 (defun gnus-mark-article-as-read (article &optional mark)
8462   "Enter ARTICLE in the pertinent lists and remove it from others."
8463   ;; Make the article expirable.
8464   (let ((mark (or mark gnus-del-mark)))
8465     (if (= mark gnus-expirable-mark)
8466         (push article gnus-newsgroup-expirable)
8467       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8468     ;; Remove from unread and marked lists.
8469     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8470     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8471     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8472     (push (cons article mark) gnus-newsgroup-reads)
8473     ;; Possibly remove from cache, if that is used.
8474     (when gnus-use-cache
8475       (gnus-cache-enter-remove-article article))
8476     t))
8477
8478 (defun gnus-mark-article-as-unread (article &optional mark)
8479   "Enter ARTICLE in the pertinent lists and remove it from others."
8480   (let ((mark (or mark gnus-ticked-mark)))
8481     (if (<= article 0)
8482         (progn
8483           (gnus-error 1 "Can't mark negative article numbers")
8484           nil)
8485       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8486             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8487             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8488             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8489
8490       ;; Unsuppress duplicates?
8491       (when gnus-suppress-duplicates
8492         (gnus-dup-unsuppress-article article))
8493
8494       (cond ((= mark gnus-ticked-mark)
8495              (push article gnus-newsgroup-marked))
8496             ((= mark gnus-dormant-mark)
8497              (push article gnus-newsgroup-dormant))
8498             (t
8499              (push article gnus-newsgroup-unreads)))
8500       (gnus-pull article gnus-newsgroup-reads)
8501       t)))
8502
8503 (defalias 'gnus-summary-mark-as-unread-forward
8504   'gnus-summary-tick-article-forward)
8505 (make-obsolete 'gnus-summary-mark-as-unread-forward
8506                'gnus-summary-tick-article-forward)
8507 (defun gnus-summary-tick-article-forward (n)
8508   "Tick N articles forwards.
8509 If N is negative, tick backwards instead.
8510 The difference between N and the number of articles ticked is returned."
8511   (interactive "p")
8512   (gnus-summary-mark-forward n gnus-ticked-mark))
8513
8514 (defalias 'gnus-summary-mark-as-unread-backward
8515   'gnus-summary-tick-article-backward)
8516 (make-obsolete 'gnus-summary-mark-as-unread-backward
8517                'gnus-summary-tick-article-backward)
8518 (defun gnus-summary-tick-article-backward (n)
8519   "Tick N articles backwards.
8520 The difference between N and the number of articles ticked is returned."
8521   (interactive "p")
8522   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8523
8524 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8525 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8526 (defun gnus-summary-tick-article (&optional article clear-mark)
8527   "Mark current article as unread.
8528 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8529 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8530   (interactive)
8531   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8532                                        gnus-ticked-mark)))
8533
8534 (defun gnus-summary-mark-as-read-forward (n)
8535   "Mark N articles as read forwards.
8536 If N is negative, mark backwards instead.
8537 The difference between N and the actual number of articles marked is
8538 returned."
8539   (interactive "p")
8540   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8541
8542 (defun gnus-summary-mark-as-read-backward (n)
8543   "Mark the N articles as read backwards.
8544 The difference between N and the actual number of articles marked is
8545 returned."
8546   (interactive "p")
8547   (gnus-summary-mark-forward
8548    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8549
8550 (defun gnus-summary-mark-as-read (&optional article mark)
8551   "Mark current article as read.
8552 ARTICLE specifies the article to be marked as read.
8553 MARK specifies a string to be inserted at the beginning of the line."
8554   (gnus-summary-mark-article article mark))
8555
8556 (defun gnus-summary-clear-mark-forward (n)
8557   "Clear marks from N articles forward.
8558 If N is negative, clear backward instead.
8559 The difference between N and the number of marks cleared is returned."
8560   (interactive "p")
8561   (gnus-summary-mark-forward n gnus-unread-mark))
8562
8563 (defun gnus-summary-clear-mark-backward (n)
8564   "Clear marks from N articles backward.
8565 The difference between N and the number of marks cleared is returned."
8566   (interactive "p")
8567   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8568
8569 (defun gnus-summary-mark-unread-as-read ()
8570   "Intended to be used by `gnus-summary-mark-article-hook'."
8571   (when (memq gnus-current-article gnus-newsgroup-unreads)
8572     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8573
8574 (defun gnus-summary-mark-read-and-unread-as-read ()
8575   "Intended to be used by `gnus-summary-mark-article-hook'."
8576   (let ((mark (gnus-summary-article-mark)))
8577     (when (or (gnus-unread-mark-p mark)
8578               (gnus-read-mark-p mark))
8579       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8580
8581 (defun gnus-summary-mark-region-as-read (point mark all)
8582   "Mark all unread articles between point and mark as read.
8583 If given a prefix, mark all articles between point and mark as read,
8584 even ticked and dormant ones."
8585   (interactive "r\nP")
8586   (save-excursion
8587     (let (article)
8588       (goto-char point)
8589       (beginning-of-line)
8590       (while (and
8591               (< (point) mark)
8592               (progn
8593                 (when (or all
8594                           (memq (setq article (gnus-summary-article-number))
8595                                 gnus-newsgroup-unreads))
8596                   (gnus-summary-mark-article article gnus-del-mark))
8597                 t)
8598               (gnus-summary-find-next))))))
8599
8600 (defun gnus-summary-mark-below (score mark)
8601   "Mark articles with score less than SCORE with MARK."
8602   (interactive "P\ncMark: ")
8603   (setq score (if score
8604                   (prefix-numeric-value score)
8605                 (or gnus-summary-default-score 0)))
8606   (save-excursion
8607     (set-buffer gnus-summary-buffer)
8608     (goto-char (point-min))
8609     (while
8610         (progn
8611           (and (< (gnus-summary-article-score) score)
8612                (gnus-summary-mark-article nil mark))
8613           (gnus-summary-find-next)))))
8614
8615 (defun gnus-summary-kill-below (&optional score)
8616   "Mark articles with score below SCORE as read."
8617   (interactive "P")
8618   (gnus-summary-mark-below score gnus-killed-mark))
8619
8620 (defun gnus-summary-clear-above (&optional score)
8621   "Clear all marks from articles with score above SCORE."
8622   (interactive "P")
8623   (gnus-summary-mark-above score gnus-unread-mark))
8624
8625 (defun gnus-summary-tick-above (&optional score)
8626   "Tick all articles with score above SCORE."
8627   (interactive "P")
8628   (gnus-summary-mark-above score gnus-ticked-mark))
8629
8630 (defun gnus-summary-mark-above (score mark)
8631   "Mark articles with score over SCORE with MARK."
8632   (interactive "P\ncMark: ")
8633   (setq score (if score
8634                   (prefix-numeric-value score)
8635                 (or gnus-summary-default-score 0)))
8636   (save-excursion
8637     (set-buffer gnus-summary-buffer)
8638     (goto-char (point-min))
8639     (while (and (progn
8640                   (when (> (gnus-summary-article-score) score)
8641                     (gnus-summary-mark-article nil mark))
8642                   t)
8643                 (gnus-summary-find-next)))))
8644
8645 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8646 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8647 (defun gnus-summary-limit-include-expunged (&optional no-error)
8648   "Display all the hidden articles that were expunged for low scores."
8649   (interactive)
8650   (let ((buffer-read-only nil))
8651     (let ((scored gnus-newsgroup-scored)
8652           headers h)
8653       (while scored
8654         (unless (gnus-summary-goto-subject (caar scored))
8655           (and (setq h (gnus-summary-article-header (caar scored)))
8656                (< (cdar scored) gnus-summary-expunge-below)
8657                (push h headers)))
8658         (setq scored (cdr scored)))
8659       (if (not headers)
8660           (when (not no-error)
8661             (error "No expunged articles hidden"))
8662         (goto-char (point-min))
8663         (gnus-summary-prepare-unthreaded (nreverse headers))
8664         (goto-char (point-min))
8665         (gnus-summary-position-point)
8666         t))))
8667
8668 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8669   "Mark all unread articles in this newsgroup as read.
8670 If prefix argument ALL is non-nil, ticked and dormant articles will
8671 also be marked as read.
8672 If QUIETLY is non-nil, no questions will be asked.
8673 If TO-HERE is non-nil, it should be a point in the buffer.  All
8674 articles before this point will be marked as read.
8675 Note that this function will only catch up the unread article
8676 in the current summary buffer limitation.
8677 The number of articles marked as read is returned."
8678   (interactive "P")
8679   (prog1
8680       (save-excursion
8681         (when (or quietly
8682                   (not gnus-interactive-catchup) ;Without confirmation?
8683                   gnus-expert-user
8684                   (gnus-y-or-n-p
8685                    (if all
8686                        "Mark absolutely all articles as read? "
8687                      "Mark all unread articles as read? ")))
8688           (if (and not-mark
8689                    (not gnus-newsgroup-adaptive)
8690                    (not gnus-newsgroup-auto-expire)
8691                    (not gnus-suppress-duplicates)
8692                    (or (not gnus-use-cache)
8693                        (eq gnus-use-cache 'passive)))
8694               (progn
8695                 (when all
8696                   (setq gnus-newsgroup-marked nil
8697                         gnus-newsgroup-dormant nil))
8698                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8699             ;; We actually mark all articles as canceled, which we
8700             ;; have to do when using auto-expiry or adaptive scoring.
8701             (gnus-summary-show-all-threads)
8702             (when (gnus-summary-first-subject (not all) t)
8703               (while (and
8704                       (if to-here (< (point) to-here) t)
8705                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8706                       (gnus-summary-find-next (not all) nil nil t))))
8707             (gnus-set-mode-line 'summary))
8708           t))
8709     (gnus-summary-position-point)))
8710
8711 (defun gnus-summary-catchup-to-here (&optional all)
8712   "Mark all unticked articles before the current one as read.
8713 If ALL is non-nil, also mark ticked and dormant articles as read."
8714   (interactive "P")
8715   (save-excursion
8716     (gnus-save-hidden-threads
8717       (let ((beg (point)))
8718         ;; We check that there are unread articles.
8719         (when (or all (gnus-summary-find-prev))
8720           (gnus-summary-catchup all t beg)))))
8721   (gnus-summary-position-point))
8722
8723 (defun gnus-summary-catchup-all (&optional quietly)
8724   "Mark all articles in this newsgroup as read."
8725   (interactive "P")
8726   (gnus-summary-catchup t quietly))
8727
8728 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8729   "Mark all unread articles in this group as read, then exit.
8730 If prefix argument ALL is non-nil, all articles are marked as read."
8731   (interactive "P")
8732   (when (gnus-summary-catchup all quietly nil 'fast)
8733     ;; Select next newsgroup or exit.
8734     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8735              (eq gnus-auto-select-next 'quietly))
8736         (gnus-summary-next-group nil)
8737       (gnus-summary-exit))))
8738
8739 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8740   "Mark all articles in this newsgroup as read, and then exit."
8741   (interactive "P")
8742   (gnus-summary-catchup-and-exit t quietly))
8743
8744 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8745   "Mark all articles in this group as read and select the next group.
8746 If given a prefix, mark all articles, unread as well as ticked, as
8747 read."
8748   (interactive "P")
8749   (save-excursion
8750     (gnus-summary-catchup all))
8751   (gnus-summary-next-group))
8752
8753 ;;;
8754 ;;; with article
8755 ;;;
8756
8757 (defmacro gnus-with-article (article &rest forms)
8758   "Select ARTICLE and perform FORMS in the original article buffer.
8759 Then replace the article with the result."
8760   `(progn
8761      ;; We don't want the article to be marked as read.
8762      (let (gnus-mark-article-hook)
8763        (gnus-summary-select-article t t nil ,article))
8764      (set-buffer gnus-original-article-buffer)
8765      ,@forms
8766      (if (not (gnus-check-backend-function
8767                'request-replace-article (car gnus-article-current)))
8768          (gnus-message 5 "Read-only group; not replacing")
8769        (unless (gnus-request-replace-article
8770                 ,article (car gnus-article-current)
8771                 (current-buffer) t)
8772          (error "Couldn't replace article")))
8773      ;; The cache and backlog have to be flushed somewhat.
8774      (when gnus-keep-backlog
8775        (gnus-backlog-remove-article
8776         (car gnus-article-current) (cdr gnus-article-current)))
8777      (when gnus-use-cache
8778        (gnus-cache-update-article
8779         (car gnus-article-current) (cdr gnus-article-current)))))
8780
8781 (put 'gnus-with-article 'lisp-indent-function 1)
8782 (put 'gnus-with-article 'edebug-form-spec '(form body))
8783
8784 ;; Thread-based commands.
8785
8786 (defun gnus-summary-articles-in-thread (&optional article)
8787   "Return a list of all articles in the current thread.
8788 If ARTICLE is non-nil, return all articles in the thread that starts
8789 with that article."
8790   (let* ((article (or article (gnus-summary-article-number)))
8791          (data (gnus-data-find-list article))
8792          (top-level (gnus-data-level (car data)))
8793          (top-subject
8794           (cond ((null gnus-thread-operation-ignore-subject)
8795                  (gnus-simplify-subject-re
8796                   (mail-header-subject (gnus-data-header (car data)))))
8797                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8798                  (gnus-simplify-subject-fuzzy
8799                   (mail-header-subject (gnus-data-header (car data)))))
8800                 (t nil)))
8801          (end-point (save-excursion
8802                       (if (gnus-summary-go-to-next-thread)
8803                           (point) (point-max))))
8804          articles)
8805     (while (and data
8806                 (< (gnus-data-pos (car data)) end-point))
8807       (when (or (not top-subject)
8808                 (string= top-subject
8809                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8810                              (gnus-simplify-subject-fuzzy
8811                               (mail-header-subject
8812                                (gnus-data-header (car data))))
8813                            (gnus-simplify-subject-re
8814                             (mail-header-subject
8815                              (gnus-data-header (car data)))))))
8816         (push (gnus-data-number (car data)) articles))
8817       (unless (and (setq data (cdr data))
8818                    (> (gnus-data-level (car data)) top-level))
8819         (setq data nil)))
8820     ;; Return the list of articles.
8821     (nreverse articles)))
8822
8823 (defun gnus-summary-rethread-current ()
8824   "Rethread the thread the current article is part of."
8825   (interactive)
8826   (let* ((gnus-show-threads t)
8827          (article (gnus-summary-article-number))
8828          (id (mail-header-id (gnus-summary-article-header)))
8829          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8830     (unless id
8831       (error "No article on the current line"))
8832     (gnus-rebuild-thread id)
8833     (gnus-summary-goto-subject article)))
8834
8835 (defun gnus-summary-reparent-thread ()
8836   "Make the current article child of the marked (or previous) article.
8837
8838 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8839 is non-nil or the Subject: of both articles are the same."
8840   (interactive)
8841   (unless (not (gnus-group-read-only-p))
8842     (error "The current newsgroup does not support article editing"))
8843   (unless (<= (length gnus-newsgroup-processable) 1)
8844     (error "No more than one article may be marked"))
8845   (save-window-excursion
8846     (let ((gnus-article-buffer " *reparent*")
8847           (current-article (gnus-summary-article-number))
8848           ;; First grab the marked article, otherwise one line up.
8849           (parent-article (if (not (null gnus-newsgroup-processable))
8850                               (car gnus-newsgroup-processable)
8851                             (save-excursion
8852                               (if (eq (forward-line -1) 0)
8853                                   (gnus-summary-article-number)
8854                                 (error "Beginning of summary buffer"))))))
8855       (unless (not (eq current-article parent-article))
8856         (error "An article may not be self-referential"))
8857       (let ((message-id (mail-header-id
8858                          (gnus-summary-article-header parent-article))))
8859         (unless (and message-id (not (equal message-id "")))
8860           (error "No message-id in desired parent"))
8861         (gnus-with-article current-article
8862           (save-restriction
8863             (goto-char (point-min))
8864             (message-narrow-to-head)
8865             (if (re-search-forward "^References: " nil t)
8866                 (progn
8867                   (re-search-forward "^[^ \t]" nil t)
8868                   (forward-line -1)
8869                   (end-of-line)
8870                   (insert " " message-id))
8871               (insert "References: " message-id "\n"))))
8872         (set-buffer gnus-summary-buffer)
8873         (gnus-summary-unmark-all-processable)
8874         (gnus-summary-update-article current-article)
8875         (gnus-summary-rethread-current)
8876         (gnus-message 3 "Article %d is now the child of article %d"
8877                       current-article parent-article)))))
8878
8879 (defun gnus-summary-toggle-threads (&optional arg)
8880   "Toggle showing conversation threads.
8881 If ARG is positive number, turn showing conversation threads on."
8882   (interactive "P")
8883   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8884     (setq gnus-show-threads
8885           (if (null arg) (not gnus-show-threads)
8886             (> (prefix-numeric-value arg) 0)))
8887     (gnus-summary-prepare)
8888     (gnus-summary-goto-subject current)
8889     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8890     (gnus-summary-position-point)))
8891
8892 (defun gnus-summary-show-all-threads ()
8893   "Show all threads."
8894   (interactive)
8895   (save-excursion
8896     (let ((buffer-read-only nil))
8897       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8898   (gnus-summary-position-point))
8899
8900 (defun gnus-summary-show-thread ()
8901   "Show thread subtrees.
8902 Returns nil if no thread was there to be shown."
8903   (interactive)
8904   (let ((buffer-read-only nil)
8905         (orig (point))
8906         ;; first goto end then to beg, to have point at beg after let
8907         (end (progn (end-of-line) (point)))
8908         (beg (progn (beginning-of-line) (point))))
8909     (prog1
8910         ;; Any hidden lines here?
8911         (search-forward "\r" end t)
8912       (subst-char-in-region beg end ?\^M ?\n t)
8913       (goto-char orig)
8914       (gnus-summary-position-point))))
8915
8916 (defun gnus-summary-hide-all-threads ()
8917   "Hide all thread subtrees."
8918   (interactive)
8919   (save-excursion
8920     (goto-char (point-min))
8921     (gnus-summary-hide-thread)
8922     (while (zerop (gnus-summary-next-thread 1 t))
8923       (gnus-summary-hide-thread)))
8924   (gnus-summary-position-point))
8925
8926 (defun gnus-summary-hide-thread ()
8927   "Hide thread subtrees.
8928 Returns nil if no threads were there to be hidden."
8929   (interactive)
8930   (let ((buffer-read-only nil)
8931         (start (point))
8932         (article (gnus-summary-article-number)))
8933     (goto-char start)
8934     ;; Go forward until either the buffer ends or the subthread
8935     ;; ends.
8936     (when (and (not (eobp))
8937                (or (zerop (gnus-summary-next-thread 1 t))
8938                    (goto-char (point-max))))
8939       (prog1
8940           (if (and (> (point) start)
8941                    (search-backward "\n" start t))
8942               (progn
8943                 (subst-char-in-region start (point) ?\n ?\^M)
8944                 (gnus-summary-goto-subject article))
8945             (goto-char start)
8946             nil)))))
8947
8948 (defun gnus-summary-go-to-next-thread (&optional previous)
8949   "Go to the same level (or less) next thread.
8950 If PREVIOUS is non-nil, go to previous thread instead.
8951 Return the article number moved to, or nil if moving was impossible."
8952   (let ((level (gnus-summary-thread-level))
8953         (way (if previous -1 1))
8954         (beg (point)))
8955     (forward-line way)
8956     (while (and (not (eobp))
8957                 (< level (gnus-summary-thread-level)))
8958       (forward-line way))
8959     (if (eobp)
8960         (progn
8961           (goto-char beg)
8962           nil)
8963       (setq beg (point))
8964       (prog1
8965           (gnus-summary-article-number)
8966         (goto-char beg)))))
8967
8968 (defun gnus-summary-next-thread (n &optional silent)
8969   "Go to the same level next N'th thread.
8970 If N is negative, search backward instead.
8971 Returns the difference between N and the number of skips actually
8972 done.
8973
8974 If SILENT, don't output messages."
8975   (interactive "p")
8976   (let ((backward (< n 0))
8977         (n (abs n)))
8978     (while (and (> n 0)
8979                 (gnus-summary-go-to-next-thread backward))
8980       (decf n))
8981     (unless silent
8982       (gnus-summary-position-point))
8983     (when (and (not silent) (/= 0 n))
8984       (gnus-message 7 "No more threads"))
8985     n))
8986
8987 (defun gnus-summary-prev-thread (n)
8988   "Go to the same level previous N'th thread.
8989 Returns the difference between N and the number of skips actually
8990 done."
8991   (interactive "p")
8992   (gnus-summary-next-thread (- n)))
8993
8994 (defun gnus-summary-go-down-thread ()
8995   "Go down one level in the current thread."
8996   (let ((children (gnus-summary-article-children)))
8997     (when children
8998       (gnus-summary-goto-subject (car children)))))
8999
9000 (defun gnus-summary-go-up-thread ()
9001   "Go up one level in the current thread."
9002   (let ((parent (gnus-summary-article-parent)))
9003     (when parent
9004       (gnus-summary-goto-subject parent))))
9005
9006 (defun gnus-summary-down-thread (n)
9007   "Go down thread N steps.
9008 If N is negative, go up instead.
9009 Returns the difference between N and how many steps down that were
9010 taken."
9011   (interactive "p")
9012   (let ((up (< n 0))
9013         (n (abs n)))
9014     (while (and (> n 0)
9015                 (if up (gnus-summary-go-up-thread)
9016                   (gnus-summary-go-down-thread)))
9017       (setq n (1- n)))
9018     (gnus-summary-position-point)
9019     (when (/= 0 n)
9020       (gnus-message 7 "Can't go further"))
9021     n))
9022
9023 (defun gnus-summary-up-thread (n)
9024   "Go up thread N steps.
9025 If N is negative, go up instead.
9026 Returns the difference between N and how many steps down that were
9027 taken."
9028   (interactive "p")
9029   (gnus-summary-down-thread (- n)))
9030
9031 (defun gnus-summary-top-thread ()
9032   "Go to the top of the thread."
9033   (interactive)
9034   (while (gnus-summary-go-up-thread))
9035   (gnus-summary-article-number))
9036
9037 (defun gnus-summary-kill-thread (&optional unmark)
9038   "Mark articles under current thread as read.
9039 If the prefix argument is positive, remove any kinds of marks.
9040 If the prefix argument is negative, tick articles instead."
9041   (interactive "P")
9042   (when unmark
9043     (setq unmark (prefix-numeric-value unmark)))
9044   (let ((articles (gnus-summary-articles-in-thread)))
9045     (save-excursion
9046       ;; Expand the thread.
9047       (gnus-summary-show-thread)
9048       ;; Mark all the articles.
9049       (while articles
9050         (gnus-summary-goto-subject (car articles))
9051         (cond ((null unmark)
9052                (gnus-summary-mark-article-as-read gnus-killed-mark))
9053               ((> unmark 0)
9054                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9055               (t
9056                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9057         (setq articles (cdr articles))))
9058     ;; Hide killed subtrees.
9059     (and (null unmark)
9060          gnus-thread-hide-killed
9061          (gnus-summary-hide-thread))
9062     ;; If marked as read, go to next unread subject.
9063     (when (null unmark)
9064       ;; Go to next unread subject.
9065       (gnus-summary-next-subject 1 t)))
9066   (gnus-set-mode-line 'summary))
9067
9068 ;; Summary sorting commands
9069
9070 (defun gnus-summary-sort-by-number (&optional reverse)
9071   "Sort the summary buffer by article number.
9072 Argument REVERSE means reverse order."
9073   (interactive "P")
9074   (gnus-summary-sort 'number reverse))
9075
9076 (defun gnus-summary-sort-by-author (&optional reverse)
9077   "Sort the summary buffer by author name alphabetically.
9078 If `case-fold-search' is non-nil, case of letters is ignored.
9079 Argument REVERSE means reverse order."
9080   (interactive "P")
9081   (gnus-summary-sort 'author reverse))
9082
9083 (defun gnus-summary-sort-by-subject (&optional reverse)
9084   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9085 If `case-fold-search' is non-nil, case of letters is ignored.
9086 Argument REVERSE means reverse order."
9087   (interactive "P")
9088   (gnus-summary-sort 'subject reverse))
9089
9090 (defun gnus-summary-sort-by-date (&optional reverse)
9091   "Sort the summary buffer by date.
9092 Argument REVERSE means reverse order."
9093   (interactive "P")
9094   (gnus-summary-sort 'date reverse))
9095
9096 (defun gnus-summary-sort-by-score (&optional reverse)
9097   "Sort the summary buffer by score.
9098 Argument REVERSE means reverse order."
9099   (interactive "P")
9100   (gnus-summary-sort 'score reverse))
9101
9102 (defun gnus-summary-sort-by-lines (&optional reverse)
9103   "Sort the summary buffer by the number of lines.
9104 Argument REVERSE means reverse order."
9105   (interactive "P")
9106   (gnus-summary-sort 'lines reverse))
9107
9108 (defun gnus-summary-sort-by-chars (&optional reverse)
9109   "Sort the summary buffer by article length.
9110 Argument REVERSE means reverse order."
9111   (interactive "P")
9112   (gnus-summary-sort 'chars reverse))
9113
9114 (defun gnus-summary-sort (predicate reverse)
9115   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9116   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9117          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9118          (gnus-thread-sort-functions
9119           (if (not reverse)
9120               thread
9121             `(lambda (t1 t2)
9122                (,thread t2 t1))))
9123          (gnus-sort-gathered-threads-function
9124           gnus-thread-sort-functions)
9125          (gnus-article-sort-functions
9126           (if (not reverse)
9127               article
9128             `(lambda (t1 t2)
9129                (,article t2 t1))))
9130          (buffer-read-only)
9131          (gnus-summary-prepare-hook nil))
9132     ;; We do the sorting by regenerating the threads.
9133     (gnus-summary-prepare)
9134     ;; Hide subthreads if needed.
9135     (when (and gnus-show-threads gnus-thread-hide-subtree)
9136       (gnus-summary-hide-all-threads))))
9137
9138 ;; Summary saving commands.
9139
9140 (defun gnus-summary-save-article (&optional n not-saved)
9141   "Save the current article using the default saver function.
9142 If N is a positive number, save the N next articles.
9143 If N is a negative number, save the N previous articles.
9144 If N is nil and any articles have been marked with the process mark,
9145 save those articles instead.
9146 The variable `gnus-default-article-saver' specifies the saver function."
9147   (interactive "P")
9148   (let* ((articles (gnus-summary-work-articles n))
9149          (save-buffer (save-excursion
9150                         (nnheader-set-temp-buffer " *Gnus Save*")))
9151          (num (length articles))
9152          header file)
9153     (dolist (article articles)
9154       (setq header (gnus-summary-article-header article))
9155       (if (not (vectorp header))
9156           ;; This is a pseudo-article.
9157           (if (assq 'name header)
9158               (gnus-copy-file (cdr (assq 'name header)))
9159             (gnus-message 1 "Article %d is unsaveable" article))
9160         ;; This is a real article.
9161         (save-window-excursion
9162           (gnus-summary-select-article t nil nil article))
9163         (save-excursion
9164           (set-buffer save-buffer)
9165           (erase-buffer)
9166           (insert-buffer-substring gnus-original-article-buffer))
9167         (setq file (gnus-article-save save-buffer file num))
9168         (gnus-summary-remove-process-mark article)
9169         (unless not-saved
9170           (gnus-summary-set-saved-mark article))))
9171     (gnus-kill-buffer save-buffer)
9172     (gnus-summary-position-point)
9173     (gnus-set-mode-line 'summary)
9174     n))
9175
9176 (defun gnus-summary-pipe-output (&optional arg)
9177   "Pipe the current article to a subprocess.
9178 If N is a positive number, pipe the N next articles.
9179 If N is a negative number, pipe the N previous articles.
9180 If N is nil and any articles have been marked with the process mark,
9181 pipe those articles instead."
9182   (interactive "P")
9183   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9184     (gnus-summary-save-article arg t))
9185   (gnus-configure-windows 'pipe))
9186
9187 (defun gnus-summary-save-article-mail (&optional arg)
9188   "Append the current article to an mail file.
9189 If N is a positive number, save the N next articles.
9190 If N is a negative number, save the N previous articles.
9191 If N is nil and any articles have been marked with the process mark,
9192 save those articles instead."
9193   (interactive "P")
9194   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9195     (gnus-summary-save-article arg)))
9196
9197 (defun gnus-summary-save-article-rmail (&optional arg)
9198   "Append the current article to an rmail file.
9199 If N is a positive number, save the N next articles.
9200 If N is a negative number, save the N previous articles.
9201 If N is nil and any articles have been marked with the process mark,
9202 save those articles instead."
9203   (interactive "P")
9204   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9205     (gnus-summary-save-article arg)))
9206
9207 (defun gnus-summary-save-article-file (&optional arg)
9208   "Append the current article to a file.
9209 If N is a positive number, save the N next articles.
9210 If N is a negative number, save the N previous articles.
9211 If N is nil and any articles have been marked with the process mark,
9212 save those articles instead."
9213   (interactive "P")
9214   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9215     (gnus-summary-save-article arg)))
9216
9217 (defun gnus-summary-write-article-file (&optional arg)
9218   "Write the current article to a file, deleting the previous file.
9219 If N is a positive number, save the N next articles.
9220 If N is a negative number, save the N previous articles.
9221 If N is nil and any articles have been marked with the process mark,
9222 save those articles instead."
9223   (interactive "P")
9224   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9225     (gnus-summary-save-article arg)))
9226
9227 (defun gnus-summary-save-article-body-file (&optional arg)
9228   "Append the current article body to a file.
9229 If N is a positive number, save the N next articles.
9230 If N is a negative number, save the N previous articles.
9231 If N is nil and any articles have been marked with the process mark,
9232 save those articles instead."
9233   (interactive "P")
9234   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9235     (gnus-summary-save-article arg)))
9236
9237 (defun gnus-summary-pipe-message (program)
9238   "Pipe the current article through PROGRAM."
9239   (interactive "sProgram: ")
9240   (gnus-summary-select-article)
9241   (let ((mail-header-separator ""))
9242     (gnus-eval-in-buffer-window gnus-article-buffer
9243       (save-restriction
9244         (widen)
9245         (let ((start (window-start))
9246               buffer-read-only)
9247           (message-pipe-buffer-body program)
9248           (set-window-start (get-buffer-window (current-buffer)) start))))))
9249
9250 (defun gnus-get-split-value (methods)
9251   "Return a value based on the split METHODS."
9252   (let (split-name method result match)
9253     (when methods
9254       (save-excursion
9255         (set-buffer gnus-original-article-buffer)
9256         (save-restriction
9257           (nnheader-narrow-to-headers)
9258           (while methods
9259             (goto-char (point-min))
9260             (setq method (pop methods))
9261             (setq match (car method))
9262             (when (cond
9263                    ((stringp match)
9264                     ;; Regular expression.
9265                     (ignore-errors
9266                       (re-search-forward match nil t)))
9267                    ((gnus-functionp match)
9268                     ;; Function.
9269                     (save-restriction
9270                       (widen)
9271                       (setq result (funcall match gnus-newsgroup-name))))
9272                    ((consp match)
9273                     ;; Form.
9274                     (save-restriction
9275                       (widen)
9276                       (setq result (eval match)))))
9277               (setq split-name (append (cdr method) split-name))
9278               (cond ((stringp result)
9279                      (push (expand-file-name
9280                             result gnus-article-save-directory)
9281                            split-name))
9282                     ((consp result)
9283                      (setq split-name (append result split-name)))))))))
9284     (nreverse split-name)))
9285
9286 (defun gnus-valid-move-group-p (group)
9287   (and (boundp group)
9288        (symbol-name group)
9289        (symbol-value group)
9290        (gnus-get-function (gnus-find-method-for-group
9291                            (symbol-name group)) 'request-accept-article t)))
9292
9293 (defun gnus-read-move-group-name (prompt default articles prefix)
9294   "Read a group name."
9295   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9296          (minibuffer-confirm-incomplete nil) ; XEmacs
9297          (prom
9298           (format "%s %s to:"
9299                   prompt
9300                   (if (> (length articles) 1)
9301                       (format "these %d articles" (length articles))
9302                     "this article")))
9303          (to-newsgroup
9304           (cond
9305            ((null split-name)
9306             (gnus-completing-read default prom
9307                                   gnus-active-hashtb
9308                                   'gnus-valid-move-group-p
9309                                   nil prefix
9310                                   'gnus-group-history))
9311            ((= 1 (length split-name))
9312             (gnus-completing-read (car split-name) prom
9313                                   gnus-active-hashtb
9314                                   'gnus-valid-move-group-p
9315                                   nil nil
9316                                   'gnus-group-history))
9317            (t
9318             (gnus-completing-read nil prom
9319                                   (mapcar (lambda (el) (list el))
9320                                           (nreverse split-name))
9321                                   nil nil nil
9322                                   'gnus-group-history))))
9323          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9324     (when to-newsgroup
9325       (if (or (string= to-newsgroup "")
9326               (string= to-newsgroup prefix))
9327           (setq to-newsgroup default))
9328       (unless to-newsgroup
9329         (error "No group name entered"))
9330       (or (gnus-active to-newsgroup)
9331           (gnus-activate-group to-newsgroup nil nil to-method)
9332           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9333                                      to-newsgroup))
9334               (or (and (gnus-request-create-group to-newsgroup to-method)
9335                        (gnus-activate-group
9336                         to-newsgroup nil nil to-method)
9337                        (gnus-subscribe-group to-newsgroup))
9338                   (error "Couldn't create group %s" to-newsgroup)))
9339           (error "No such group: %s" to-newsgroup)))
9340     to-newsgroup))
9341
9342 (defun gnus-summary-save-parts (type dir n &optional reverse)
9343   "Save parts matching TYPE to DIR.
9344 If REVERSE, save parts that do not match TYPE."
9345   (interactive
9346    (list (read-string "Save parts of type: " 
9347                       (or (car gnus-summary-save-parts-type-history)
9348                           gnus-summary-save-parts-default-mime)
9349                       'gnus-summary-save-parts-type-history)
9350          (setq gnus-summary-save-parts-last-directory
9351                (read-file-name "Save to directory: " 
9352                                gnus-summary-save-parts-last-directory
9353                                nil t))
9354          current-prefix-arg))
9355   (gnus-summary-iterate n
9356     (let ((gnus-display-mime-function nil)
9357           (gnus-inhibit-treatment t))
9358       (gnus-summary-select-article))
9359     (save-excursion
9360       (set-buffer gnus-article-buffer)
9361       (let ((handles (or gnus-article-mime-handles
9362                          (mm-dissect-buffer) (mm-uu-dissect))))
9363         (when handles
9364           (gnus-summary-save-parts-1 type dir handles reverse)
9365           (unless gnus-article-mime-handles ;; Don't destroy this case.
9366             (mm-destroy-parts handles)))))))
9367
9368 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9369   (if (stringp (car handle))
9370       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9371               (cdr handle))
9372     (when (if reverse
9373               (not (string-match type (mm-handle-media-type handle)))
9374             (string-match type (mm-handle-media-type handle)))
9375       (let ((file (expand-file-name
9376                    (file-name-nondirectory
9377                     (or
9378                      (mail-content-type-get
9379                       (mm-handle-disposition handle) 'filename)
9380                      (concat gnus-newsgroup-name
9381                              "." (number-to-string
9382                                   (cdr gnus-article-current)))))
9383                    dir)))
9384         (unless (file-exists-p file)
9385           (mm-save-part-to-file handle file))))))
9386
9387 ;; Summary extract commands
9388
9389 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9390   (let ((buffer-read-only nil)
9391         (article (gnus-summary-article-number))
9392         after-article b e)
9393     (unless (gnus-summary-goto-subject article)
9394       (error "No such article: %d" article))
9395     (gnus-summary-position-point)
9396     ;; If all commands are to be bunched up on one line, we collect
9397     ;; them here.
9398     (unless gnus-view-pseudos-separately
9399       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9400             files action)
9401         (while ps
9402           (setq action (cdr (assq 'action (car ps))))
9403           (setq files (list (cdr (assq 'name (car ps)))))
9404           (while (and ps (cdr ps)
9405                       (string= (or action "1")
9406                                (or (cdr (assq 'action (cadr ps))) "2")))
9407             (push (cdr (assq 'name (cadr ps))) files)
9408             (setcdr ps (cddr ps)))
9409           (when files
9410             (when (not (string-match "%s" action))
9411               (push " " files))
9412             (push " " files)
9413             (when (assq 'execute (car ps))
9414               (setcdr (assq 'execute (car ps))
9415                       (funcall (if (string-match "%s" action)
9416                                    'format 'concat)
9417                                action
9418                                (mapconcat
9419                                 (lambda (f)
9420                                   (if (equal f " ")
9421                                       f
9422                                     (gnus-quote-arg-for-sh-or-csh f)))
9423                                 files " ")))))
9424           (setq ps (cdr ps)))))
9425     (if (and gnus-view-pseudos (not not-view))
9426         (while pslist
9427           (when (assq 'execute (car pslist))
9428             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9429                                   (eq gnus-view-pseudos 'not-confirm)))
9430           (setq pslist (cdr pslist)))
9431       (save-excursion
9432         (while pslist
9433           (setq after-article (or (cdr (assq 'article (car pslist)))
9434                                   (gnus-summary-article-number)))
9435           (gnus-summary-goto-subject after-article)
9436           (forward-line 1)
9437           (setq b (point))
9438           (insert "    " (file-name-nondirectory
9439                           (cdr (assq 'name (car pslist))))
9440                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9441           (setq e (point))
9442           (forward-line -1)             ; back to `b'
9443           (gnus-add-text-properties
9444            b (1- e) (list 'gnus-number gnus-reffed-article-number
9445                           gnus-mouse-face-prop gnus-mouse-face))
9446           (gnus-data-enter
9447            after-article gnus-reffed-article-number
9448            gnus-unread-mark b (car pslist) 0 (- e b))
9449           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9450           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9451           (setq pslist (cdr pslist)))))))
9452
9453 (defun gnus-pseudos< (p1 p2)
9454   (let ((c1 (cdr (assq 'action p1)))
9455         (c2 (cdr (assq 'action p2))))
9456     (and c1 c2 (string< c1 c2))))
9457
9458 (defun gnus-request-pseudo-article (props)
9459   (cond ((assq 'execute props)
9460          (gnus-execute-command (cdr (assq 'execute props)))))
9461   (let ((gnus-current-article (gnus-summary-article-number)))
9462     (gnus-run-hooks 'gnus-mark-article-hook)))
9463
9464 (defun gnus-execute-command (command &optional automatic)
9465   (save-excursion
9466     (gnus-article-setup-buffer)
9467     (set-buffer gnus-article-buffer)
9468     (setq buffer-read-only nil)
9469     (let ((command (if automatic command
9470                      (read-string "Command: " (cons command 0)))))
9471       (erase-buffer)
9472       (insert "$ " command "\n\n")
9473       (if gnus-view-pseudo-asynchronously
9474           (start-process "gnus-execute" (current-buffer) shell-file-name
9475                          shell-command-switch command)
9476         (call-process shell-file-name nil t nil
9477                       shell-command-switch command)))))
9478
9479 ;; Summary kill commands.
9480
9481 (defun gnus-summary-edit-global-kill (article)
9482   "Edit the \"global\" kill file."
9483   (interactive (list (gnus-summary-article-number)))
9484   (gnus-group-edit-global-kill article))
9485
9486 (defun gnus-summary-edit-local-kill ()
9487   "Edit a local kill file applied to the current newsgroup."
9488   (interactive)
9489   (setq gnus-current-headers (gnus-summary-article-header))
9490   (gnus-group-edit-local-kill
9491    (gnus-summary-article-number) gnus-newsgroup-name))
9492
9493 ;;; Header reading.
9494
9495 (defun gnus-read-header (id &optional header)
9496   "Read the headers of article ID and enter them into the Gnus system."
9497   (let ((group gnus-newsgroup-name)
9498         (gnus-override-method
9499          (or
9500           gnus-override-method
9501           (and (gnus-news-group-p gnus-newsgroup-name)
9502                (car (gnus-refer-article-methods)))))
9503         where)
9504     ;; First we check to see whether the header in question is already
9505     ;; fetched.
9506     (if (stringp id)
9507         ;; This is a Message-ID.
9508         (setq header (or header (gnus-id-to-header id)))
9509       ;; This is an article number.
9510       (setq header (or header (gnus-summary-article-header id))))
9511     (if (and header
9512              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9513         ;; We have found the header.
9514         header
9515       ;; If this is a sparse article, we have to nix out its
9516       ;; previous entry in the thread hashtb.
9517       (when (and header
9518                  (gnus-summary-article-sparse-p (mail-header-number header)))
9519         (let* ((parent (gnus-parent-id (mail-header-references header)))
9520                (thread (and parent (gnus-id-to-thread parent))))
9521           (when thread
9522             (delq (assq header thread) thread))))
9523       ;; We have to really fetch the header to this article.
9524       (save-excursion
9525         (set-buffer nntp-server-buffer)
9526         (when (setq where (gnus-request-head id group))
9527           (nnheader-fold-continuation-lines)
9528           (goto-char (point-max))
9529           (insert ".\n")
9530           (goto-char (point-min))
9531           (insert "211 ")
9532           (princ (cond
9533                   ((numberp id) id)
9534                   ((cdr where) (cdr where))
9535                   (header (mail-header-number header))
9536                   (t gnus-reffed-article-number))
9537                  (current-buffer))
9538           (insert " Article retrieved.\n"))
9539         (if (or (not where)
9540                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9541             ()                          ; Malformed head.
9542           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9543             (when (and (stringp id)
9544                        (not (string= (gnus-group-real-name group)
9545                                      (car where))))
9546               ;; If we fetched by Message-ID and the article came
9547               ;; from a different group, we fudge some bogus article
9548               ;; numbers for this article.
9549               (mail-header-set-number header gnus-reffed-article-number))
9550             (save-excursion
9551               (set-buffer gnus-summary-buffer)
9552               (decf gnus-reffed-article-number)
9553               (gnus-remove-header (mail-header-number header))
9554               (push header gnus-newsgroup-headers)
9555               (setq gnus-current-headers header)
9556               (push (mail-header-number header) gnus-newsgroup-limit)))
9557           header)))))
9558
9559 (defun gnus-remove-header (number)
9560   "Remove header NUMBER from `gnus-newsgroup-headers'."
9561   (if (and gnus-newsgroup-headers
9562            (= number (mail-header-number (car gnus-newsgroup-headers))))
9563       (pop gnus-newsgroup-headers)
9564     (let ((headers gnus-newsgroup-headers))
9565       (while (and (cdr headers)
9566                   (not (= number (mail-header-number (cadr headers)))))
9567         (pop headers))
9568       (when (cdr headers)
9569         (setcdr headers (cddr headers))))))
9570
9571 ;;;
9572 ;;; summary highlights
9573 ;;;
9574
9575 (defun gnus-highlight-selected-summary ()
9576   "Highlight selected article in summary buffer."
9577   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9578   (when gnus-summary-selected-face
9579     (save-excursion
9580       (let* ((beg (progn (beginning-of-line) (point)))
9581              (end (progn (end-of-line) (point)))
9582              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9583              (from (if (get-text-property beg gnus-mouse-face-prop)
9584                        beg
9585                      (or (next-single-property-change
9586                           beg gnus-mouse-face-prop nil end)
9587                          beg)))
9588              (to
9589               (if (= from end)
9590                   (- from 2)
9591                 (or (next-single-property-change
9592                      from gnus-mouse-face-prop nil end)
9593                     end))))
9594         ;; If no mouse-face prop on line we will have to = from = end,
9595         ;; so we highlight the entire line instead.
9596         (when (= (+ to 2) from)
9597           (setq from beg)
9598           (setq to end))
9599         (if gnus-newsgroup-selected-overlay
9600             ;; Move old overlay.
9601             (gnus-move-overlay
9602              gnus-newsgroup-selected-overlay from to (current-buffer))
9603           ;; Create new overlay.
9604           (gnus-overlay-put
9605            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9606            'face gnus-summary-selected-face))))))
9607
9608 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9609 (defun gnus-summary-highlight-line ()
9610   "Highlight current line according to `gnus-summary-highlight'."
9611   (let* ((list gnus-summary-highlight)
9612          (p (point))
9613          (end (progn (end-of-line) (point)))
9614          ;; now find out where the line starts and leave point there.
9615          (beg (progn (beginning-of-line) (point)))
9616          (article (gnus-summary-article-number))
9617          (score (or (cdr (assq (or article gnus-current-article)
9618                                gnus-newsgroup-scored))
9619                     gnus-summary-default-score 0))
9620          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9621          (inhibit-read-only t))
9622     ;; Eval the cars of the lists until we find a match.
9623     (let ((default gnus-summary-default-score))
9624       (while (and list
9625                   (not (eval (caar list))))
9626         (setq list (cdr list))))
9627     (let ((face (cdar list)))
9628       (unless (eq face (get-text-property beg 'face))
9629         (gnus-put-text-property-excluding-characters-with-faces
9630          beg end 'face
9631          (setq face (if (boundp face) (symbol-value face) face)))
9632         (when gnus-summary-highlight-line-function
9633           (funcall gnus-summary-highlight-line-function article face))))
9634     (goto-char p)))
9635
9636 (defun gnus-update-read-articles (group unread &optional compute)
9637   "Update the list of read articles in GROUP."
9638   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9639          (entry (gnus-gethash group gnus-newsrc-hashtb))
9640          (info (nth 2 entry))
9641          (prev 1)
9642          (unread (sort (copy-sequence unread) '<))
9643          read)
9644     (if (or (not info) (not active))
9645         ;; There is no info on this group if it was, in fact,
9646         ;; killed.  Gnus stores no information on killed groups, so
9647         ;; there's nothing to be done.
9648         ;; One could store the information somewhere temporarily,
9649         ;; perhaps...  Hmmm...
9650         ()
9651       ;; Remove any negative articles numbers.
9652       (while (and unread (< (car unread) 0))
9653         (setq unread (cdr unread)))
9654       ;; Remove any expired article numbers
9655       (while (and unread (< (car unread) (car active)))
9656         (setq unread (cdr unread)))
9657       ;; Compute the ranges of read articles by looking at the list of
9658       ;; unread articles.
9659       (while unread
9660         (when (/= (car unread) prev)
9661           (push (if (= prev (1- (car unread))) prev
9662                   (cons prev (1- (car unread))))
9663                 read))
9664         (setq prev (1+ (car unread)))
9665         (setq unread (cdr unread)))
9666       (when (<= prev (cdr active))
9667         (push (cons prev (cdr active)) read))
9668       (setq read (if (> (length read) 1) (nreverse read) read))
9669       (if compute
9670           read
9671         (save-excursion
9672           (let (setmarkundo)
9673             ;; Propagate the read marks to the backend.
9674             (when (gnus-check-backend-function 'request-set-mark group)
9675               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9676                     (add (gnus-remove-from-range read (gnus-info-read info))))
9677                 (when (or add del)
9678                   (unless (gnus-check-group group)
9679                     (error "Can't open server for %s" group))
9680                   (gnus-request-set-mark
9681                    group (delq nil (list (if add (list add 'add '(read)))
9682                                          (if del (list del 'del '(read))))))
9683                   (setq setmarkundo
9684                         `(gnus-request-set-mark
9685                           ,group
9686                           ',(delq nil (list
9687                                        (if del (list del 'add '(read)))
9688                                        (if add (list add 'del '(read))))))))))
9689             (set-buffer gnus-group-buffer)
9690             (gnus-undo-register
9691               `(progn
9692                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9693                  (gnus-info-set-read ',info ',(gnus-info-read info))
9694                  (gnus-get-unread-articles-in-group ',info 
9695                                                     (gnus-active ,group))
9696                  (gnus-group-update-group ,group t)
9697                  ,setmarkundo))))
9698         ;; Enter this list into the group info.
9699         (gnus-info-set-read info read)
9700         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9701         (gnus-get-unread-articles-in-group info (gnus-active group))
9702         t))))
9703
9704 (defun gnus-offer-save-summaries ()
9705   "Offer to save all active summary buffers."
9706   (save-excursion
9707     (let ((buflist (buffer-list))
9708           buffers bufname)
9709       ;; Go through all buffers and find all summaries.
9710       (while buflist
9711         (and (setq bufname (buffer-name (car buflist)))
9712              (string-match "Summary" bufname)
9713              (save-excursion
9714                (set-buffer bufname)
9715                ;; We check that this is, indeed, a summary buffer.
9716                (and (eq major-mode 'gnus-summary-mode)
9717                     ;; Also make sure this isn't bogus.
9718                     gnus-newsgroup-prepared
9719                     ;; Also make sure that this isn't a dead summary buffer.
9720                     (not gnus-dead-summary-mode)))
9721              (push bufname buffers))
9722         (setq buflist (cdr buflist)))
9723       ;; Go through all these summary buffers and offer to save them.
9724       (when buffers
9725         (map-y-or-n-p
9726          "Update summary buffer %s? "
9727          (lambda (buf)
9728            (switch-to-buffer buf)
9729            (gnus-summary-exit))
9730          buffers)))))
9731
9732
9733 ;;; @ for mime-partial
9734 ;;;
9735
9736 (defun gnus-request-partial-message ()
9737   (save-excursion
9738     (let ((number (gnus-summary-article-number))
9739           (group gnus-newsgroup-name)
9740           (mother gnus-article-buffer))
9741       (set-buffer (get-buffer-create " *Partial Article*"))
9742       (erase-buffer)
9743       (setq mime-preview-buffer mother)
9744       (gnus-request-article-this-buffer number group)
9745       (mime-parse-buffer)
9746       )))
9747
9748 (autoload 'mime-combine-message/partial-pieces-automatically
9749   "mime-partial"
9750   "Internal method to combine message/partial messages automatically.")
9751
9752 (mime-add-condition
9753  'action '((type . message)(subtype . partial)
9754            (major-mode . gnus-original-article-mode)
9755            (method . mime-combine-message/partial-pieces-automatically)
9756            (summary-buffer-exp . gnus-summary-buffer)
9757            (request-partial-message-method . gnus-request-partial-message)
9758            ))
9759
9760
9761 ;;; @ for message/rfc822
9762 ;;;
9763
9764 (defun gnus-mime-extract-message/rfc822 (entity situation)
9765   (let (group article num cwin swin cur)
9766     (with-temp-buffer
9767       (mime-insert-entity-content entity)
9768       (setq group (or (cdr (assq 'group situation))
9769                       (completing-read "Group: "
9770                                        gnus-active-hashtb
9771                                        nil
9772                                        (gnus-read-active-file-p)
9773                                        gnus-newsgroup-name))
9774             article (gnus-request-accept-article group)))
9775     (when (and (consp article)
9776                (numberp (setq article (cdr article))))
9777       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9778             cwin (get-buffer-window (current-buffer) t))
9779       (save-window-excursion
9780         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9781             (select-window swin)
9782           (set-buffer gnus-summary-buffer))
9783         (setq cur gnus-current-article)
9784         (forward-line num)
9785         (let (gnus-show-threads)
9786           (gnus-summary-goto-subject article t))
9787         (gnus-summary-clear-mark-forward 1)
9788         (gnus-summary-goto-subject cur))
9789       (when (and cwin (window-frame cwin))
9790         (select-frame (window-frame cwin)))
9791       (when (boundp 'mime-acting-situation-to-override)
9792         (set-alist 'mime-acting-situation-to-override
9793                    'group
9794                    group)
9795         (set-alist 'mime-acting-situation-to-override
9796                    'after-method
9797                    `(progn
9798                       (save-current-buffer
9799                         (set-buffer gnus-group-buffer)
9800                         (gnus-activate-group ,group))
9801                       (gnus-summary-goto-article ,cur
9802                                                  gnus-show-all-headers)))
9803         (set-alist 'mime-acting-situation-to-override
9804                    'number num)))))
9805
9806 (mime-add-condition
9807  'action '((type . message)(subtype . rfc822)
9808            (major-mode . gnus-original-article-mode)
9809            (method . gnus-mime-extract-message/rfc822)
9810            (mode . "extract")
9811            ))
9812
9813 (mime-add-condition
9814  'action '((type . message)(subtype . news)
9815            (major-mode . gnus-original-article-mode)
9816            (method . gnus-mime-extract-message/rfc822)
9817            (mode . "extract")
9818            ))
9819
9820 (defun gnus-mime-extract-multipart (entity situation)
9821   (let ((children (mime-entity-children entity))
9822         mime-acting-situation-to-override
9823         f)
9824     (while children
9825       (mime-play-entity (car children)
9826                         (cons (assq 'mode situation)
9827                               mime-acting-situation-to-override))
9828       (setq children (cdr children)))
9829     (if (setq f (cdr (assq 'after-method
9830                            mime-acting-situation-to-override)))
9831         (eval f)
9832       )))
9833
9834 (mime-add-condition
9835  'action '((type . multipart)
9836            (method . gnus-mime-extract-multipart)
9837            (mode . "extract")
9838            )
9839  'with-default)
9840
9841
9842 ;;; @ end
9843 ;;;
9844
9845 (defun gnus-summary-setup-default-charset ()
9846   "Setup newsgroup default charset."
9847   (if (equal gnus-newsgroup-name "nndraft:drafts")
9848       (setq gnus-newsgroup-charset nil)
9849     (let* ((name (and gnus-newsgroup-name
9850                       (gnus-group-real-name gnus-newsgroup-name)))
9851            (ignored-charsets
9852             (or gnus-newsgroup-ephemeral-ignored-charsets
9853                 (append
9854                  (and gnus-newsgroup-name
9855                       (or (gnus-group-find-parameter gnus-newsgroup-name
9856                                                      'ignored-charsets t)
9857                           (let ((alist gnus-group-ignored-charsets-alist)
9858                                 elem (charsets nil))
9859                             (while (setq elem (pop alist))
9860                               (when (and name
9861                                          (string-match (car elem) name))
9862                                 (setq alist nil
9863                                       charsets (cdr elem))))
9864                             charsets)))
9865                  gnus-newsgroup-ignored-charsets))))
9866       (setq gnus-newsgroup-charset
9867             (or gnus-newsgroup-ephemeral-charset
9868                 (and gnus-newsgroup-name
9869                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9870                          (let ((alist gnus-group-charset-alist)
9871                                elem charset)
9872                            (while (setq elem (pop alist))
9873                              (when (and name
9874                                         (string-match (car elem) name))
9875                                (setq alist nil
9876                                      charset (cadr elem))))
9877                            charset)))
9878                 gnus-default-charset))
9879       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9880            ignored-charsets))))
9881
9882 ;;;
9883 ;;; Mime Commands
9884 ;;;
9885
9886 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9887   "Display the current article buffer fully MIME-buttonized.
9888 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9889 treated as multipart/mixed."
9890   (interactive "P")
9891   (require 'gnus-art)
9892   (let ((gnus-unbuttonized-mime-types nil)
9893         (gnus-mime-display-multipart-as-mixed show-all-parts))
9894     (gnus-summary-show-article)))
9895
9896 (defun gnus-summary-repair-multipart (article)
9897   "Add a Content-Type header to a multipart article without one."
9898   (interactive (list (gnus-summary-article-number)))
9899   (gnus-with-article article
9900     (message-narrow-to-head)
9901     (goto-char (point-max))
9902     (widen)
9903     (when (search-forward "\n--" nil t)
9904       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9905         (message-narrow-to-head)
9906         (message-remove-header "Mime-Version")
9907         (message-remove-header "Content-Type")
9908         (goto-char (point-max))
9909         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9910                         separator))
9911         (insert "Mime-Version: 1.0\n")
9912         (widen))))
9913   (let (gnus-mark-article-hook)
9914     (gnus-summary-select-article t t nil article)))
9915
9916 (defun gnus-summary-toggle-display-buttonized ()
9917   "Toggle the buttonizing of the article buffer."
9918   (interactive)
9919   (require 'gnus-art)
9920   (if (setq gnus-inhibit-mime-unbuttonizing
9921             (not gnus-inhibit-mime-unbuttonizing))
9922       (let ((gnus-unbuttonized-mime-types nil))
9923         (gnus-summary-show-article))
9924     (gnus-summary-show-article)))
9925
9926 ;;;
9927 ;;; Intelli-mouse commmands
9928 ;;;
9929
9930 (defun gnus-wheel-summary-scroll (event)
9931   (interactive "e")
9932   (let ((amount (if (memq 'shift (event-modifiers event))
9933                     (car gnus-wheel-scroll-amount)
9934                   (cdr gnus-wheel-scroll-amount)))
9935         (direction (- (* (static-if (featurep 'xemacs)
9936                              (event-button event)
9937                            (cond ((eq 'mouse-4 (event-basic-type event))
9938                                   4)
9939                                  ((eq 'mouse-5 (event-basic-type event))
9940                                   5)))
9941                          2) 9))
9942         edge)
9943     (gnus-summary-scroll-up (* amount direction))
9944     (when (gnus-eval-in-buffer-window gnus-article-buffer
9945             (save-restriction
9946               (widen)
9947               (and (if (< 0 direction)
9948                        (gnus-article-next-page 0)
9949                      (gnus-article-prev-page 0)
9950                      (bobp))
9951                    (if (setq edge (get-text-property
9952                                    (point-min) 'gnus-wheel-edge))
9953                        (setq edge (* edge direction))
9954                      (setq edge -1))
9955                    (or (plusp edge)
9956                        (let ((buffer-read-only nil)
9957                              (inhibit-read-only t))
9958                          (put-text-property (point-min) (point-max)
9959                                             'gnus-wheel-edge direction)
9960                          nil))
9961                    (or (> edge gnus-wheel-edge-resistance)
9962                        (let ((buffer-read-only nil)
9963                              (inhibit-read-only t))
9964                          (put-text-property (point-min) (point-max)
9965                                             'gnus-wheel-edge
9966                                             (* (1+ edge) direction))
9967                          nil))
9968                    (eq last-command 'gnus-wheel-summary-scroll))))
9969       (gnus-summary-next-article nil nil (minusp direction)))))
9970
9971 (defun gnus-wheel-install ()
9972   "Enable mouse wheel support on summary window."
9973   (when gnus-use-wheel
9974     (let ((keys
9975            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9976       (dolist (key keys)
9977         (define-key gnus-summary-mode-map key
9978           'gnus-wheel-summary-scroll)))))
9979
9980 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9981
9982 ;;;
9983 ;;; Traditional PGP commmands
9984 ;;;
9985
9986 (defun gnus-summary-decrypt-article (&optional force)
9987   "Decrypt the current article in traditional PGP way.
9988 This will have permanent effect only in mail groups.
9989 If FORCE is non-nil, allow editing of articles even in read-only
9990 groups."
9991   (interactive "P")
9992   (gnus-summary-select-article t)
9993   (gnus-eval-in-buffer-window gnus-article-buffer
9994     (save-excursion
9995       (save-restriction
9996         (widen)
9997         (goto-char (point-min))
9998         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9999           (error "Not a traditional PGP message!"))
10000         (let ((armor-start (match-beginning 0)))
10001           (if (and (pgg-decrypt-region armor-start (point-max))
10002                    (or force (not (gnus-group-read-only-p))))
10003               (let ((inhibit-read-only t)
10004                     buffer-read-only)
10005                 (delete-region armor-start
10006                                (progn
10007                                  (re-search-forward "^-+END PGP" nil t)
10008                                  (beginning-of-line 2)
10009                                  (point)))
10010                 (insert-buffer-substring pgg-output-buffer))))))))
10011
10012 (defun gnus-summary-verify-article ()
10013   "Verify the current article in traditional PGP way."
10014   (interactive)
10015   (save-excursion
10016     (set-buffer gnus-original-article-buffer)
10017     (goto-char (point-min))
10018     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10019       (error "Not a traditional PGP message!"))
10020     (re-search-forward "^-+END PGP" nil t)
10021     (beginning-of-line 2)
10022     (call-interactively (function pgg-verify-region))))
10023
10024 ;;;
10025 ;;; Generic summary marking commands
10026 ;;;
10027
10028 (defvar gnus-summary-marking-alist
10029   '((read gnus-del-mark "d")
10030     (unread gnus-unread-mark "u")
10031     (ticked gnus-ticked-mark "!")
10032     (dormant gnus-dormant-mark "?")
10033     (expirable gnus-expirable-mark "e"))
10034   "An alist of names/marks/keystrokes.")
10035
10036 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10037 (defvar gnus-summary-mark-map)
10038
10039 (defun gnus-summary-make-all-marking-commands ()
10040   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10041   (dolist (elem gnus-summary-marking-alist)
10042     (apply 'gnus-summary-make-marking-command elem)))
10043
10044 (defun gnus-summary-make-marking-command (name mark keystroke)
10045   (let ((map (make-sparse-keymap)))
10046     (define-key gnus-summary-generic-mark-map keystroke map)
10047     (dolist (lway `((next "next" next nil "n")
10048                     (next-unread "next unread" next t "N")
10049                     (prev "previous" prev nil "p")
10050                     (prev-unread "previous unread" prev t "P")
10051                     (nomove "" nil nil ,keystroke)))
10052       (let ((func (gnus-summary-make-marking-command-1
10053                    mark (car lway) lway name)))
10054         (setq func (eval func))
10055         (define-key map (nth 4 lway) func)))))
10056
10057 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10058   `(defun ,(intern
10059             (format "gnus-summary-put-mark-as-%s%s"
10060                     name (if (eq way 'nomove)
10061                              ""
10062                            (concat "-" (symbol-name way)))))
10063      (n)
10064      ,(format
10065        "Mark the current article as %s%s.
10066 If N, the prefix, then repeat N times.
10067 If N is negative, move in reverse order.
10068 The difference between N and the actual number of articles marked is
10069 returned."
10070        name (car (cdr lway)))
10071      (interactive "p")
10072      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10073
10074 (defun gnus-summary-generic-mark (n mark move unread)
10075   "Mark N articles with MARK."
10076   (unless (eq major-mode 'gnus-summary-mode)
10077     (error "This command can only be used in the summary buffer"))
10078   (gnus-summary-show-thread)
10079   (let ((nummove
10080          (cond
10081           ((eq move 'next) 1)
10082           ((eq move 'prev) -1)
10083           (t 0))))
10084     (if (zerop nummove)
10085         (setq n 1)
10086       (when (< n 0)
10087         (setq n (abs n)
10088               nummove (* -1 nummove))))
10089     (while (and (> n 0)
10090                 (gnus-summary-mark-article nil mark)
10091                 (zerop (gnus-summary-next-subject nummove unread t)))
10092       (setq n (1- n)))
10093     (when (/= 0 n)
10094       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10095     (gnus-summary-recenter)
10096     (gnus-summary-position-point)
10097     (gnus-set-mode-line 'summary)
10098     n))
10099
10100 (gnus-summary-make-all-marking-commands)
10101
10102 (gnus-ems-redefine)
10103
10104 (provide 'gnus-sum)
10105
10106 (run-hooks 'gnus-sum-load-hook)
10107
10108 ;;; gnus-sum.el ends here