Sync
[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 ;;; Internal variables
934
935 (defvar gnus-article-mime-handles nil)
936 (defvar gnus-article-decoded-p nil)
937 (defvar gnus-scores-exclude-files nil)
938 (defvar gnus-page-broken nil)
939 (defvar gnus-inhibit-mime-unbuttonizing nil)
940
941 (defvar gnus-original-article nil)
942 (defvar gnus-article-internal-prepare-hook nil)
943 (defvar gnus-newsgroup-process-stack nil)
944
945 (defvar gnus-thread-indent-array nil)
946 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
947 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
948   "Function called to sort the articles within a thread after it has been gathered together.")
949
950 ;; Avoid highlighting in kill files.
951 (defvar gnus-summary-inhibit-highlight nil)
952 (defvar gnus-newsgroup-selected-overlay nil)
953 (defvar gnus-inhibit-limiting nil)
954 (defvar gnus-newsgroup-adaptive-score-file nil)
955 (defvar gnus-current-score-file nil)
956 (defvar gnus-current-move-group nil)
957 (defvar gnus-current-copy-group nil)
958 (defvar gnus-current-crosspost-group nil)
959
960 (defvar gnus-newsgroup-dependencies nil)
961 (defvar gnus-newsgroup-adaptive nil)
962 (defvar gnus-summary-display-article-function nil)
963 (defvar gnus-summary-highlight-line-function nil
964   "Function called after highlighting a summary line.")
965
966 (defvar gnus-summary-line-format-alist
967   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
968     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
969     (?s gnus-tmp-subject-or-nil ?s)
970     (?n gnus-tmp-name ?s)
971     (?A (std11-address-string
972          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
973     (?a (or (std11-full-name-string
974              (car (mime-entity-read-field gnus-tmp-header 'From)))
975             gnus-tmp-from) ?s)
976     (?F gnus-tmp-from ?s)
977     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
978     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
979     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
980     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
981     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
982     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
983     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
984     (?L gnus-tmp-lines ?d)
985     (?I gnus-tmp-indentation ?s)
986     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
987     (?R gnus-tmp-replied ?c)
988     (?\[ gnus-tmp-opening-bracket ?c)
989     (?\] gnus-tmp-closing-bracket ?c)
990     (?\> (make-string gnus-tmp-level ? ) ?s)
991     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
992     (?i gnus-tmp-score ?d)
993     (?z gnus-tmp-score-char ?c)
994     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
995     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
996     (?U gnus-tmp-unread ?c)
997     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
998     (?t (gnus-summary-number-of-articles-in-thread
999          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1000         ?d)
1001     (?e (gnus-summary-number-of-articles-in-thread
1002          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1003         ?c)
1004     (?u gnus-tmp-user-defined ?s)
1005     (?P (gnus-pick-line-number) ?d))
1006   "An alist of format specifications that can appear in summary lines,
1007 and what variables they correspond with, along with the type of the
1008 variable (string, integer, character, etc).")
1009
1010 (defvar gnus-summary-dummy-line-format-alist
1011   `((?S gnus-tmp-subject ?s)
1012     (?N gnus-tmp-number ?d)
1013     (?u gnus-tmp-user-defined ?s)))
1014
1015 (defvar gnus-summary-mode-line-format-alist
1016   `((?G gnus-tmp-group-name ?s)
1017     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1018     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1019     (?A gnus-tmp-article-number ?d)
1020     (?Z gnus-tmp-unread-and-unselected ?s)
1021     (?V gnus-version ?s)
1022     (?U gnus-tmp-unread-and-unticked ?d)
1023     (?S gnus-tmp-subject ?s)
1024     (?e gnus-tmp-unselected ?d)
1025     (?u gnus-tmp-user-defined ?s)
1026     (?d (length gnus-newsgroup-dormant) ?d)
1027     (?t (length gnus-newsgroup-marked) ?d)
1028     (?r (length gnus-newsgroup-reads) ?d)
1029     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1030     (?E gnus-newsgroup-expunged-tally ?d)
1031     (?s (gnus-current-score-file-nondirectory) ?s)))
1032
1033 (defvar gnus-last-search-regexp nil
1034   "Default regexp for article search command.")
1035
1036 (defvar gnus-summary-search-article-matched-data nil
1037   "Last matched data of article search command.  It is the local variable
1038 in `gnus-article-buffer' which consists of the list of start position,
1039 end position and text.")
1040
1041 (defvar gnus-last-shell-command nil
1042   "Default shell command on article.")
1043
1044 (defvar gnus-newsgroup-begin nil)
1045 (defvar gnus-newsgroup-end nil)
1046 (defvar gnus-newsgroup-last-rmail nil)
1047 (defvar gnus-newsgroup-last-mail nil)
1048 (defvar gnus-newsgroup-last-folder nil)
1049 (defvar gnus-newsgroup-last-file nil)
1050 (defvar gnus-newsgroup-auto-expire nil)
1051 (defvar gnus-newsgroup-active nil)
1052
1053 (defvar gnus-newsgroup-data nil)
1054 (defvar gnus-newsgroup-data-reverse nil)
1055 (defvar gnus-newsgroup-limit nil)
1056 (defvar gnus-newsgroup-limits nil)
1057
1058 (defvar gnus-newsgroup-unreads nil
1059   "List of unread articles in the current newsgroup.")
1060
1061 (defvar gnus-newsgroup-unselected nil
1062   "List of unselected unread articles in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-reads nil
1065   "Alist of read articles and article marks in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-expunged-tally nil)
1068
1069 (defvar gnus-newsgroup-marked nil
1070   "List of ticked articles in the current newsgroup (a subset of unread art).")
1071
1072 (defvar gnus-newsgroup-killed nil
1073   "List of ranges of articles that have been through the scoring process.")
1074
1075 (defvar gnus-newsgroup-cached nil
1076   "List of articles that come from the article cache.")
1077
1078 (defvar gnus-newsgroup-saved nil
1079   "List of articles that have been saved.")
1080
1081 (defvar gnus-newsgroup-kill-headers nil)
1082
1083 (defvar gnus-newsgroup-replied nil
1084   "List of articles that have been replied to in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-expirable nil
1087   "List of articles in the current newsgroup that can be expired.")
1088
1089 (defvar gnus-newsgroup-processable nil
1090   "List of articles in the current newsgroup that can be processed.")
1091
1092 (defvar gnus-newsgroup-downloadable nil
1093   "List of articles in the current newsgroup that can be processed.")
1094
1095 (defvar gnus-newsgroup-undownloaded nil
1096   "List of articles in the current newsgroup that haven't been downloaded..")
1097
1098 (defvar gnus-newsgroup-unsendable nil
1099   "List of articles in the current newsgroup that won't be sent.")
1100
1101 (defvar gnus-newsgroup-bookmarks nil
1102   "List of articles in the current newsgroup that have bookmarks.")
1103
1104 (defvar gnus-newsgroup-dormant nil
1105   "List of dormant articles in the current newsgroup.")
1106
1107 (defvar gnus-newsgroup-scored nil
1108   "List of scored articles in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-incorporated nil
1111   "List of incorporated articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-headers nil
1114   "List of article headers in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-threads nil)
1117
1118 (defvar gnus-newsgroup-prepared nil
1119   "Whether the current group has been prepared properly.")
1120
1121 (defvar gnus-newsgroup-ancient nil
1122   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1123
1124 (defvar gnus-newsgroup-sparse nil)
1125
1126 (defvar gnus-current-article nil)
1127 (defvar gnus-article-current nil)
1128 (defvar gnus-current-headers nil)
1129 (defvar gnus-have-all-headers nil)
1130 (defvar gnus-last-article nil)
1131 (defvar gnus-newsgroup-history nil)
1132 (defvar gnus-newsgroup-charset nil)
1133 (defvar gnus-newsgroup-ephemeral-charset nil)
1134 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1135
1136 (defconst gnus-summary-local-variables
1137   '(gnus-newsgroup-name
1138     gnus-newsgroup-begin gnus-newsgroup-end
1139     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1140     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1141     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1142     gnus-newsgroup-unselected gnus-newsgroup-marked
1143     gnus-newsgroup-reads gnus-newsgroup-saved
1144     gnus-newsgroup-replied gnus-newsgroup-expirable
1145     gnus-newsgroup-processable gnus-newsgroup-killed
1146     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1147     gnus-newsgroup-unsendable
1148     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1149     gnus-newsgroup-headers gnus-newsgroup-threads
1150     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1151     gnus-current-article gnus-current-headers gnus-have-all-headers
1152     gnus-last-article gnus-article-internal-prepare-hook
1153     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1154     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1155     gnus-thread-expunge-below
1156     gnus-score-alist gnus-current-score-file
1157     (gnus-summary-expunge-below . global)
1158     (gnus-summary-mark-below . global)
1159     gnus-newsgroup-active gnus-scores-exclude-files
1160     gnus-newsgroup-history gnus-newsgroup-ancient
1161     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1162     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1163     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1164     (gnus-newsgroup-expunged-tally . 0)
1165     gnus-cache-removable-articles gnus-newsgroup-cached
1166     gnus-newsgroup-data gnus-newsgroup-data-reverse
1167     gnus-newsgroup-limit gnus-newsgroup-limits
1168     gnus-newsgroup-charset
1169     gnus-newsgroup-incorporated)
1170   "Variables that are buffer-local to the summary buffers.")
1171
1172 ;; Byte-compiler warning.
1173 (defvar gnus-article-mode-map)
1174
1175 ;; Subject simplification.
1176
1177 (defun gnus-simplify-whitespace (str)
1178   "Remove excessive whitespace."
1179   (let ((mystr str))
1180     ;; Multiple spaces.
1181     (while (string-match "[ \t][ \t]+" mystr)
1182       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1183                           " "
1184                           (substring mystr (match-end 0)))))
1185     ;; Leading spaces.
1186     (when (string-match "^[ \t]+" mystr)
1187       (setq mystr (substring mystr (match-end 0))))
1188     ;; Trailing spaces.
1189     (when (string-match "[ \t]+$" mystr)
1190       (setq mystr (substring mystr 0 (match-beginning 0))))
1191     mystr))
1192
1193 (defsubst gnus-simplify-subject-re (subject)
1194   "Remove \"Re:\" from subject lines."
1195   (if (string-match "^[Rr][Ee]: *" subject)
1196       (substring subject (match-end 0))
1197     subject))
1198
1199 (defun gnus-simplify-subject (subject &optional re-only)
1200   "Remove `Re:' and words in parentheses.
1201 If RE-ONLY is non-nil, strip leading `Re:'s only."
1202   (let ((case-fold-search t))           ;Ignore case.
1203     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1204     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1205       (setq subject (substring subject (match-end 0))))
1206     ;; Remove uninteresting prefixes.
1207     (when (and (not re-only)
1208                gnus-simplify-ignored-prefixes
1209                (string-match gnus-simplify-ignored-prefixes subject))
1210       (setq subject (substring subject (match-end 0))))
1211     ;; Remove words in parentheses from end.
1212     (unless re-only
1213       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1214         (setq subject (substring subject 0 (match-beginning 0)))))
1215     ;; Return subject string.
1216     subject))
1217
1218 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1219 ;; all whitespace.
1220 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1221   (goto-char (point-min))
1222   (while (re-search-forward regexp nil t)
1223     (replace-match (or newtext ""))))
1224
1225 (defun gnus-simplify-buffer-fuzzy ()
1226   "Simplify string in the buffer fuzzily.
1227 The string in the accessible portion of the current buffer is simplified.
1228 It is assumed to be a single-line subject.
1229 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1230 matter is removed.  Additional things can be deleted by setting
1231 gnus-simplify-subject-fuzzy-regexp."
1232   (let ((case-fold-search t)
1233         (modified-tick))
1234     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1235
1236     (while (not (eq modified-tick (buffer-modified-tick)))
1237       (setq modified-tick (buffer-modified-tick))
1238       (cond
1239        ((listp gnus-simplify-subject-fuzzy-regexp)
1240         (mapcar 'gnus-simplify-buffer-fuzzy-step
1241                 gnus-simplify-subject-fuzzy-regexp))
1242        (gnus-simplify-subject-fuzzy-regexp
1243         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1244       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1245       (gnus-simplify-buffer-fuzzy-step
1246        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1247       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1248
1249     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1250     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1251     (gnus-simplify-buffer-fuzzy-step " $")
1252     (gnus-simplify-buffer-fuzzy-step "^ +")))
1253
1254 (defun gnus-simplify-subject-fuzzy (subject)
1255   "Simplify a subject string fuzzily.
1256 See `gnus-simplify-buffer-fuzzy' for details."
1257   (save-excursion
1258     (gnus-set-work-buffer)
1259     (let ((case-fold-search t))
1260       ;; Remove uninteresting prefixes.
1261       (when (and gnus-simplify-ignored-prefixes
1262                  (string-match gnus-simplify-ignored-prefixes subject))
1263         (setq subject (substring subject (match-end 0))))
1264       (insert subject)
1265       (inline (gnus-simplify-buffer-fuzzy))
1266       (buffer-string))))
1267
1268 (defsubst gnus-simplify-subject-fully (subject)
1269   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1270   (cond
1271    (gnus-simplify-subject-functions
1272     (gnus-map-function gnus-simplify-subject-functions subject))
1273    ((null gnus-summary-gather-subject-limit)
1274     (gnus-simplify-subject-re subject))
1275    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1276     (gnus-simplify-subject-fuzzy subject))
1277    ((numberp gnus-summary-gather-subject-limit)
1278     (gnus-limit-string (gnus-simplify-subject-re subject)
1279                        gnus-summary-gather-subject-limit))
1280    (t
1281     subject)))
1282
1283 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1284   "Check whether two subjects are equal.
1285 If optional argument simple-first is t, first argument is already
1286 simplified."
1287   (cond
1288    ((null simple-first)
1289     (equal (gnus-simplify-subject-fully s1)
1290            (gnus-simplify-subject-fully s2)))
1291    (t
1292     (equal s1
1293            (gnus-simplify-subject-fully s2)))))
1294
1295 (defun gnus-summary-bubble-group ()
1296   "Increase the score of the current group.
1297 This is a handy function to add to `gnus-summary-exit-hook' to
1298 increase the score of each group you read."
1299   (gnus-group-add-score gnus-newsgroup-name))
1300
1301 \f
1302 ;;;
1303 ;;; Gnus summary mode
1304 ;;;
1305
1306 (put 'gnus-summary-mode 'mode-class 'special)
1307
1308 (when t
1309   ;; Non-orthogonal keys
1310
1311   (gnus-define-keys gnus-summary-mode-map
1312     " " gnus-summary-next-page
1313     "\177" gnus-summary-prev-page
1314     [delete] gnus-summary-prev-page
1315     [backspace] gnus-summary-prev-page
1316     "\r" gnus-summary-scroll-up
1317     "\M-\r" gnus-summary-scroll-down
1318     "n" gnus-summary-next-unread-article
1319     "p" gnus-summary-prev-unread-article
1320     "N" gnus-summary-next-article
1321     "P" gnus-summary-prev-article
1322     "\M-\C-n" gnus-summary-next-same-subject
1323     "\M-\C-p" gnus-summary-prev-same-subject
1324     "\M-n" gnus-summary-next-unread-subject
1325     "\M-p" gnus-summary-prev-unread-subject
1326     "." gnus-summary-first-unread-article
1327     "," gnus-summary-best-unread-article
1328     "\M-s" gnus-summary-search-article-forward
1329     "\M-r" gnus-summary-search-article-backward
1330     "<" gnus-summary-beginning-of-article
1331     ">" gnus-summary-end-of-article
1332     "j" gnus-summary-goto-article
1333     "^" gnus-summary-refer-parent-article
1334     "\M-^" gnus-summary-refer-article
1335     "u" gnus-summary-tick-article-forward
1336     "!" gnus-summary-tick-article-forward
1337     "U" gnus-summary-tick-article-backward
1338     "d" gnus-summary-mark-as-read-forward
1339     "D" gnus-summary-mark-as-read-backward
1340     "E" gnus-summary-mark-as-expirable
1341     "\M-u" gnus-summary-clear-mark-forward
1342     "\M-U" gnus-summary-clear-mark-backward
1343     "k" gnus-summary-kill-same-subject-and-select
1344     "\C-k" gnus-summary-kill-same-subject
1345     "\M-\C-k" gnus-summary-kill-thread
1346     "\M-\C-l" gnus-summary-lower-thread
1347     "e" gnus-summary-edit-article
1348     "#" gnus-summary-mark-as-processable
1349     "\M-#" gnus-summary-unmark-as-processable
1350     "\M-\C-t" gnus-summary-toggle-threads
1351     "\M-\C-s" gnus-summary-show-thread
1352     "\M-\C-h" gnus-summary-hide-thread
1353     "\M-\C-f" gnus-summary-next-thread
1354     "\M-\C-b" gnus-summary-prev-thread
1355     [(meta down)] gnus-summary-next-thread
1356     [(meta up)] gnus-summary-prev-thread
1357     "\M-\C-u" gnus-summary-up-thread
1358     "\M-\C-d" gnus-summary-down-thread
1359     "&" gnus-summary-execute-command
1360     "c" gnus-summary-catchup-and-exit
1361     "\C-w" gnus-summary-mark-region-as-read
1362     "\C-t" gnus-summary-toggle-truncation
1363     "?" gnus-summary-mark-as-dormant
1364     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1365     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1366     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1367     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1368     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1369     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1370     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1371     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1372     "=" gnus-summary-expand-window
1373     "\C-x\C-s" gnus-summary-reselect-current-group
1374     "\M-g" gnus-summary-rescan-group
1375     "w" gnus-summary-stop-page-breaking
1376     "\C-c\C-r" gnus-summary-caesar-message
1377     "\M-t" gnus-summary-toggle-mime
1378     "f" gnus-summary-followup
1379     "F" gnus-summary-followup-with-original
1380     "C" gnus-summary-cancel-article
1381     "r" gnus-summary-reply
1382     "R" gnus-summary-reply-with-original
1383     "\C-c\C-f" gnus-summary-mail-forward
1384     "o" gnus-summary-save-article
1385     "\C-o" gnus-summary-save-article-mail
1386     "|" gnus-summary-pipe-output
1387     "\M-k" gnus-summary-edit-local-kill
1388     "\M-K" gnus-summary-edit-global-kill
1389     ;; "V" gnus-version
1390     "\C-c\C-d" gnus-summary-describe-group
1391     "q" gnus-summary-exit
1392     "Q" gnus-summary-exit-no-update
1393     "\C-c\C-i" gnus-info-find-node
1394     gnus-mouse-2 gnus-mouse-pick-article
1395     "m" gnus-summary-mail-other-window
1396     "a" gnus-summary-post-news
1397     "x" gnus-summary-limit-to-unread
1398     "s" gnus-summary-isearch-article
1399     "t" gnus-article-toggle-headers
1400     "g" gnus-summary-show-article
1401     "l" gnus-summary-goto-last-article
1402     "v" gnus-summary-preview-mime-message
1403     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1404     "\C-d" gnus-summary-enter-digest-group
1405     "\M-\C-d" gnus-summary-read-document
1406     "\M-\C-e" gnus-summary-edit-parameters
1407     "\M-\C-a" gnus-summary-customize-parameters
1408     "\C-c\C-b" gnus-bug
1409     "*" gnus-cache-enter-article
1410     "\M-*" gnus-cache-remove-article
1411     "\M-&" gnus-summary-universal-argument
1412     "\C-l" gnus-recenter
1413     "I" gnus-summary-increase-score
1414     "L" gnus-summary-lower-score
1415     "\M-i" gnus-symbolic-argument
1416     "h" gnus-summary-select-article-buffer
1417
1418     "V" gnus-summary-score-map
1419     "X" gnus-uu-extract-map
1420     "S" gnus-summary-send-map)
1421
1422   ;; Sort of orthogonal keymap
1423   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1424     "t" gnus-summary-tick-article-forward
1425     "!" gnus-summary-tick-article-forward
1426     "d" gnus-summary-mark-as-read-forward
1427     "r" gnus-summary-mark-as-read-forward
1428     "c" gnus-summary-clear-mark-forward
1429     " " gnus-summary-clear-mark-forward
1430     "e" gnus-summary-mark-as-expirable
1431     "x" gnus-summary-mark-as-expirable
1432     "?" gnus-summary-mark-as-dormant
1433     "b" gnus-summary-set-bookmark
1434     "B" gnus-summary-remove-bookmark
1435     "#" gnus-summary-mark-as-processable
1436     "\M-#" gnus-summary-unmark-as-processable
1437     "S" gnus-summary-limit-include-expunged
1438     "C" gnus-summary-catchup
1439     "H" gnus-summary-catchup-to-here
1440     "\C-c" gnus-summary-catchup-all
1441     "k" gnus-summary-kill-same-subject-and-select
1442     "K" gnus-summary-kill-same-subject
1443     "P" gnus-uu-mark-map)
1444
1445   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1446     "c" gnus-summary-clear-above
1447     "u" gnus-summary-tick-above
1448     "m" gnus-summary-mark-above
1449     "k" gnus-summary-kill-below)
1450
1451   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1452     "/" gnus-summary-limit-to-subject
1453     "n" gnus-summary-limit-to-articles
1454     "w" gnus-summary-pop-limit
1455     "s" gnus-summary-limit-to-subject
1456     "a" gnus-summary-limit-to-author
1457     "u" gnus-summary-limit-to-unread
1458     "m" gnus-summary-limit-to-marks
1459     "M" gnus-summary-limit-exclude-marks
1460     "v" gnus-summary-limit-to-score
1461     "*" gnus-summary-limit-include-cached
1462     "D" gnus-summary-limit-include-dormant
1463     "T" gnus-summary-limit-include-thread
1464     "d" gnus-summary-limit-exclude-dormant
1465     "t" gnus-summary-limit-to-age
1466     "x" gnus-summary-limit-to-extra 
1467     "E" gnus-summary-limit-include-expunged
1468     "c" gnus-summary-limit-exclude-childless-dormant
1469     "C" gnus-summary-limit-mark-excluded-as-read)
1470
1471   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1472     "n" gnus-summary-next-unread-article
1473     "p" gnus-summary-prev-unread-article
1474     "N" gnus-summary-next-article
1475     "P" gnus-summary-prev-article
1476     "\C-n" gnus-summary-next-same-subject
1477     "\C-p" gnus-summary-prev-same-subject
1478     "\M-n" gnus-summary-next-unread-subject
1479     "\M-p" gnus-summary-prev-unread-subject
1480     "f" gnus-summary-first-unread-article
1481     "b" gnus-summary-best-unread-article
1482     "j" gnus-summary-goto-article
1483     "g" gnus-summary-goto-subject
1484     "l" gnus-summary-goto-last-article
1485     "o" gnus-summary-pop-article)
1486
1487   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1488     "k" gnus-summary-kill-thread
1489     "l" gnus-summary-lower-thread
1490     "i" gnus-summary-raise-thread
1491     "T" gnus-summary-toggle-threads
1492     "t" gnus-summary-rethread-current
1493     "^" gnus-summary-reparent-thread
1494     "s" gnus-summary-show-thread
1495     "S" gnus-summary-show-all-threads
1496     "h" gnus-summary-hide-thread
1497     "H" gnus-summary-hide-all-threads
1498     "n" gnus-summary-next-thread
1499     "p" gnus-summary-prev-thread
1500     "u" gnus-summary-up-thread
1501     "o" gnus-summary-top-thread
1502     "d" gnus-summary-down-thread
1503     "#" gnus-uu-mark-thread
1504     "\M-#" gnus-uu-unmark-thread)
1505
1506   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1507     "g" gnus-summary-prepare
1508     "c" gnus-summary-insert-cached-articles)
1509
1510   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1511     "c" gnus-summary-catchup-and-exit
1512     "C" gnus-summary-catchup-all-and-exit
1513     "E" gnus-summary-exit-no-update
1514     "J" gnus-summary-jump-to-other-group
1515     "Q" gnus-summary-exit
1516     "Z" gnus-summary-exit
1517     "n" gnus-summary-catchup-and-goto-next-group
1518     "R" gnus-summary-reselect-current-group
1519     "G" gnus-summary-rescan-group
1520     "N" gnus-summary-next-group
1521     "s" gnus-summary-save-newsrc
1522     "P" gnus-summary-prev-group)
1523
1524   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1525     " " gnus-summary-next-page
1526     "n" gnus-summary-next-page
1527     "\177" gnus-summary-prev-page
1528     [delete] gnus-summary-prev-page
1529     "p" gnus-summary-prev-page
1530     "\r" gnus-summary-scroll-up
1531     "\M-\r" gnus-summary-scroll-down
1532     "<" gnus-summary-beginning-of-article
1533     ">" gnus-summary-end-of-article
1534     "b" gnus-summary-beginning-of-article
1535     "e" gnus-summary-end-of-article
1536     "^" gnus-summary-refer-parent-article
1537     "r" gnus-summary-refer-parent-article
1538     "D" gnus-summary-enter-digest-group
1539     "R" gnus-summary-refer-references
1540     "T" gnus-summary-refer-thread
1541     "g" gnus-summary-show-article
1542     "s" gnus-summary-isearch-article
1543     "P" gnus-summary-print-article
1544     "t" gnus-article-babel
1545     "d" gnus-summary-decrypt-article
1546     "v" gnus-summary-verify-article)
1547
1548   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1549     "b" gnus-article-add-buttons
1550     "B" gnus-article-add-buttons-to-head
1551     "o" gnus-article-treat-overstrike
1552     "e" gnus-article-emphasize
1553     "w" gnus-article-fill-cited-article
1554     "Q" gnus-article-fill-long-lines
1555     "C" gnus-article-capitalize-sentences
1556     "c" gnus-article-remove-cr
1557     "Z" gnus-article-decode-HZ
1558     "f" gnus-article-display-x-face
1559     "l" gnus-summary-stop-page-breaking
1560     "r" gnus-summary-caesar-message
1561     "t" gnus-article-toggle-headers
1562     "v" gnus-summary-verbose-headers
1563     "m" gnus-summary-toggle-mime
1564     "H" gnus-article-strip-headers-in-body
1565     "d" gnus-article-treat-dumbquotes
1566     "s" gnus-smiley-display)
1567
1568   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1569     "a" gnus-article-hide
1570     "h" gnus-article-toggle-headers
1571     "b" gnus-article-hide-boring-headers
1572     "s" gnus-article-hide-signature
1573     "c" gnus-article-hide-citation
1574     "C" gnus-article-hide-citation-in-followups
1575     "l" gnus-article-hide-list-identifiers
1576     "p" gnus-article-hide-pgp
1577     "B" gnus-article-strip-banner
1578     "P" gnus-article-hide-pem
1579     "\C-c" gnus-article-hide-citation-maybe)
1580
1581   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1582     "a" gnus-article-highlight
1583     "h" gnus-article-highlight-headers
1584     "c" gnus-article-highlight-citation
1585     "s" gnus-article-highlight-signature)
1586
1587   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1588     "z" gnus-article-date-ut
1589     "u" gnus-article-date-ut
1590     "l" gnus-article-date-local
1591     "e" gnus-article-date-lapsed
1592     "o" gnus-article-date-original
1593     "i" gnus-article-date-iso8601
1594     "s" gnus-article-date-user)
1595
1596   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1597     "t" gnus-article-remove-trailing-blank-lines
1598     "l" gnus-article-strip-leading-blank-lines
1599     "m" gnus-article-strip-multiple-blank-lines
1600     "a" gnus-article-strip-blank-lines
1601     "A" gnus-article-strip-all-blank-lines
1602     "s" gnus-article-strip-leading-space
1603     "e" gnus-article-strip-trailing-space)
1604
1605   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1606     "v" gnus-version
1607     "f" gnus-summary-fetch-faq
1608     "d" gnus-summary-describe-group
1609     "h" gnus-summary-describe-briefly
1610     "i" gnus-info-find-node)
1611
1612   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1613     "e" gnus-summary-expire-articles
1614     "\M-\C-e" gnus-summary-expire-articles-now
1615     "\177" gnus-summary-delete-article
1616     [delete] gnus-summary-delete-article
1617     [backspace] gnus-summary-delete-article
1618     "m" gnus-summary-move-article
1619     "r" gnus-summary-respool-article
1620     "w" gnus-summary-edit-article
1621     "c" gnus-summary-copy-article
1622     "B" gnus-summary-crosspost-article
1623     "q" gnus-summary-respool-query
1624     "t" gnus-summary-respool-trace
1625     "i" gnus-summary-import-article
1626     "p" gnus-summary-article-posted-p)
1627
1628   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1629     "o" gnus-summary-save-article
1630     "m" gnus-summary-save-article-mail
1631     "F" gnus-summary-write-article-file
1632     "r" gnus-summary-save-article-rmail
1633     "f" gnus-summary-save-article-file
1634     "b" gnus-summary-save-article-body-file
1635     "h" gnus-summary-save-article-folder
1636     "v" gnus-summary-save-article-vm
1637     "p" gnus-summary-pipe-output
1638     "s" gnus-soup-add-article)
1639
1640   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1641     "b" gnus-summary-display-buttonized
1642     "m" gnus-summary-repair-multipart
1643     "v" gnus-article-view-part
1644     "o" gnus-article-save-part
1645     "c" gnus-article-copy-part
1646     "e" gnus-article-externalize-part
1647     "i" gnus-article-inline-part
1648     "|" gnus-article-pipe-part))
1649
1650 (defun gnus-summary-make-menu-bar ()
1651   (gnus-turn-off-edit-menu 'summary)
1652
1653   (unless (boundp 'gnus-summary-misc-menu)
1654
1655     (easy-menu-define
1656      gnus-summary-kill-menu gnus-summary-mode-map ""
1657      (cons
1658       "Score"
1659       (nconc
1660        (list
1661         ["Enter score..." gnus-summary-score-entry t]
1662         ["Customize" gnus-score-customize t])
1663        (gnus-make-score-map 'increase)
1664        (gnus-make-score-map 'lower)
1665        '(("Mark"
1666           ["Kill below" gnus-summary-kill-below t]
1667           ["Mark above" gnus-summary-mark-above t]
1668           ["Tick above" gnus-summary-tick-above t]
1669           ["Clear above" gnus-summary-clear-above t])
1670          ["Current score" gnus-summary-current-score t]
1671          ["Set score" gnus-summary-set-score t]
1672          ["Switch current score file..." gnus-score-change-score-file t]
1673          ["Set mark below..." gnus-score-set-mark-below t]
1674          ["Set expunge below..." gnus-score-set-expunge-below t]
1675          ["Edit current score file" gnus-score-edit-current-scores t]
1676          ["Edit score file" gnus-score-edit-file t]
1677          ["Trace score" gnus-score-find-trace t]
1678          ["Find words" gnus-score-find-favourite-words t]
1679          ["Rescore buffer" gnus-summary-rescore t]
1680          ["Increase score..." gnus-summary-increase-score t]
1681          ["Lower score..." gnus-summary-lower-score t]))))
1682
1683     ;; Define both the Article menu in the summary buffer and the equivalent
1684     ;; Commands menu in the article buffer here for consistency.
1685     (let ((innards
1686            '(("Hide"
1687               ["All" gnus-article-hide t]
1688               ["Headers" gnus-article-toggle-headers t]
1689               ["Signature" gnus-article-hide-signature t]
1690               ["Citation" gnus-article-hide-citation t]
1691               ["List identifiers" gnus-article-hide-list-identifiers t]
1692               ["PGP" gnus-article-hide-pgp t]
1693               ["Banner" gnus-article-strip-banner t]
1694               ["Boring headers" gnus-article-hide-boring-headers t])
1695              ("Highlight"
1696               ["All" gnus-article-highlight t]
1697               ["Headers" gnus-article-highlight-headers t]
1698               ["Signature" gnus-article-highlight-signature t]
1699               ["Citation" gnus-article-highlight-citation t])
1700              ("Date"
1701               ["Local" gnus-article-date-local t]
1702               ["ISO8601" gnus-article-date-iso8601 t]
1703               ["UT" gnus-article-date-ut t]
1704               ["Original" gnus-article-date-original t]
1705               ["Lapsed" gnus-article-date-lapsed t]
1706               ["User-defined" gnus-article-date-user t])
1707              ("Washing"
1708               ("Remove Blanks"
1709                ["Leading" gnus-article-strip-leading-blank-lines t]
1710                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1711                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1712                ["All of the above" gnus-article-strip-blank-lines t]
1713                ["All" gnus-article-strip-all-blank-lines t]
1714                ["Leading space" gnus-article-strip-leading-space t]
1715                ["Trailing space" gnus-article-strip-trailing-space t])
1716               ["Overstrike" gnus-article-treat-overstrike t]
1717               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1718               ["Emphasis" gnus-article-emphasize t]
1719               ["Word wrap" gnus-article-fill-cited-article t]
1720               ["Fill long lines" gnus-article-fill-long-lines t]
1721               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1722               ["CR" gnus-article-remove-cr t]
1723               ["Show X-Face" gnus-article-display-x-face t]
1724               ["Rot 13" gnus-summary-caesar-message t]
1725               ["Unix pipe" gnus-summary-pipe-message t]
1726               ["Add buttons" gnus-article-add-buttons t]
1727               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1728               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1729               ["Toggle MIME" gnus-summary-toggle-mime t]
1730               ["Verbose header" gnus-summary-verbose-headers t]
1731               ["Toggle header" gnus-summary-toggle-header t]
1732               ["Toggle smileys" gnus-smiley-display t]
1733               ["HZ" gnus-article-decode-HZ t])
1734              ("Output"
1735               ["Save in default format" gnus-summary-save-article t]
1736               ["Save in file" gnus-summary-save-article-file t]
1737               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1738               ["Save in MH folder" gnus-summary-save-article-folder t]
1739               ["Save in VM folder" gnus-summary-save-article-vm t]
1740               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1741               ["Save body in file" gnus-summary-save-article-body-file t]
1742               ["Pipe through a filter" gnus-summary-pipe-output t]
1743               ["Add to SOUP packet" gnus-soup-add-article t]
1744               ["Print" gnus-summary-print-article t])
1745              ("Backend"
1746               ["Respool article..." gnus-summary-respool-article t]
1747               ["Move article..." gnus-summary-move-article
1748                (gnus-check-backend-function
1749                 'request-move-article gnus-newsgroup-name)]
1750               ["Copy article..." gnus-summary-copy-article t]
1751               ["Crosspost article..." gnus-summary-crosspost-article
1752                (gnus-check-backend-function
1753                 'request-replace-article gnus-newsgroup-name)]
1754               ["Import file..." gnus-summary-import-article t]
1755               ["Check if posted" gnus-summary-article-posted-p t]
1756               ["Edit article" gnus-summary-edit-article
1757                (not (gnus-group-read-only-p))]
1758               ["Delete article" gnus-summary-delete-article
1759                (gnus-check-backend-function
1760                 'request-expire-articles gnus-newsgroup-name)]
1761               ["Query respool" gnus-summary-respool-query t]
1762               ["Trace respool" gnus-summary-respool-trace t]
1763               ["Delete expirable articles" gnus-summary-expire-articles-now
1764                (gnus-check-backend-function
1765                 'request-expire-articles gnus-newsgroup-name)])
1766              ("Extract"
1767               ["Uudecode" gnus-uu-decode-uu t]
1768               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1769               ["Unshar" gnus-uu-decode-unshar t]
1770               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1771               ["Save" gnus-uu-decode-save t]
1772               ["Binhex" gnus-uu-decode-binhex t]
1773               ["Postscript" gnus-uu-decode-postscript t])
1774              ("Cache"
1775               ["Enter article" gnus-cache-enter-article t]
1776               ["Remove article" gnus-cache-remove-article t])
1777              ["Translate" gnus-article-babel t]
1778              ["Select article buffer" gnus-summary-select-article-buffer t]
1779              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1780              ["Isearch article..." gnus-summary-isearch-article t]
1781              ["Beginning of the article" gnus-summary-beginning-of-article t]
1782              ["End of the article" gnus-summary-end-of-article t]
1783              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1784              ["Fetch referenced articles" gnus-summary-refer-references t]
1785              ["Fetch current thread" gnus-summary-refer-thread t]
1786              ["Fetch article with id..." gnus-summary-refer-article t]
1787              ["Redisplay" gnus-summary-show-article t])))
1788       (easy-menu-define
1789        gnus-summary-article-menu gnus-summary-mode-map ""
1790        (cons "Article" innards))
1791
1792       (easy-menu-define
1793        gnus-article-commands-menu gnus-article-mode-map ""
1794        (cons "Commands" innards)))
1795
1796     (easy-menu-define
1797      gnus-summary-thread-menu gnus-summary-mode-map ""
1798      '("Threads"
1799        ["Toggle threading" gnus-summary-toggle-threads t]
1800        ["Hide threads" gnus-summary-hide-all-threads t]
1801        ["Show threads" gnus-summary-show-all-threads t]
1802        ["Hide thread" gnus-summary-hide-thread t]
1803        ["Show thread" gnus-summary-show-thread t]
1804        ["Go to next thread" gnus-summary-next-thread t]
1805        ["Go to previous thread" gnus-summary-prev-thread t]
1806        ["Go down thread" gnus-summary-down-thread t]
1807        ["Go up thread" gnus-summary-up-thread t]
1808        ["Top of thread" gnus-summary-top-thread t]
1809        ["Mark thread as read" gnus-summary-kill-thread t]
1810        ["Lower thread score" gnus-summary-lower-thread t]
1811        ["Raise thread score" gnus-summary-raise-thread t]
1812        ["Rethread current" gnus-summary-rethread-current t]))
1813
1814     (easy-menu-define
1815      gnus-summary-post-menu gnus-summary-mode-map ""
1816      '("Post"
1817        ["Post an article" gnus-summary-post-news t]
1818        ["Followup" gnus-summary-followup t]
1819        ["Followup and yank" gnus-summary-followup-with-original t]
1820        ["Supersede article" gnus-summary-supersede-article t]
1821        ["Cancel article" gnus-summary-cancel-article t]
1822        ["Reply" gnus-summary-reply t]
1823        ["Reply and yank" gnus-summary-reply-with-original t]
1824        ["Wide reply" gnus-summary-wide-reply t]
1825        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1826        ["Mail forward" gnus-summary-mail-forward t]
1827        ["Post forward" gnus-summary-post-forward t]
1828        ["Digest and mail" gnus-summary-mail-digest t]
1829        ["Digest and post" gnus-summary-post-digest t]
1830        ["Resend message" gnus-summary-resend-message t]
1831        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1832        ["Send a mail" gnus-summary-mail-other-window t]
1833        ["Uuencode and post" gnus-uu-post-news t]
1834        ["Followup via news" gnus-summary-followup-to-mail t]
1835        ["Followup via news and yank"
1836         gnus-summary-followup-to-mail-with-original t]
1837        ;;("Draft"
1838        ;;["Send" gnus-summary-send-draft t]
1839        ;;["Send bounced" gnus-resend-bounced-mail t])
1840        ))
1841
1842     (easy-menu-define
1843      gnus-summary-misc-menu gnus-summary-mode-map ""
1844      '("Misc"
1845        ("Mark Read"
1846         ["Mark as read" gnus-summary-mark-as-read-forward t]
1847         ["Mark same subject and select"
1848          gnus-summary-kill-same-subject-and-select t]
1849         ["Mark same subject" gnus-summary-kill-same-subject t]
1850         ["Catchup" gnus-summary-catchup t]
1851         ["Catchup all" gnus-summary-catchup-all t]
1852         ["Catchup to here" gnus-summary-catchup-to-here t]
1853         ["Catchup region" gnus-summary-mark-region-as-read t]
1854         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1855        ("Mark Various"
1856         ["Tick" gnus-summary-tick-article-forward t]
1857         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1858         ["Remove marks" gnus-summary-clear-mark-forward t]
1859         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1860         ["Set bookmark" gnus-summary-set-bookmark t]
1861         ["Remove bookmark" gnus-summary-remove-bookmark t])
1862        ("Mark Limit"
1863         ["Marks..." gnus-summary-limit-to-marks t]
1864         ["Subject..." gnus-summary-limit-to-subject t]
1865         ["Author..." gnus-summary-limit-to-author t]
1866         ["Age..." gnus-summary-limit-to-age t]
1867         ["Extra..." gnus-summary-limit-to-extra t]
1868         ["Score" gnus-summary-limit-to-score t]
1869         ["Unread" gnus-summary-limit-to-unread t]
1870         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1871         ["Articles" gnus-summary-limit-to-articles t]
1872         ["Pop limit" gnus-summary-pop-limit t]
1873         ["Show dormant" gnus-summary-limit-include-dormant t]
1874         ["Hide childless dormant"
1875          gnus-summary-limit-exclude-childless-dormant t]
1876         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1877         ["Hide marked" gnus-summary-limit-exclude-marks t]
1878         ["Show expunged" gnus-summary-show-all-expunged t])
1879        ("Process Mark"
1880         ["Set mark" gnus-summary-mark-as-processable t]
1881         ["Remove mark" gnus-summary-unmark-as-processable t]
1882         ["Remove all marks" gnus-summary-unmark-all-processable t]
1883         ["Mark above" gnus-uu-mark-over t]
1884         ["Mark series" gnus-uu-mark-series t]
1885         ["Mark region" gnus-uu-mark-region t]
1886         ["Unmark region" gnus-uu-unmark-region t]
1887         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1888         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1889         ["Mark all" gnus-uu-mark-all t]
1890         ["Mark buffer" gnus-uu-mark-buffer t]
1891         ["Mark sparse" gnus-uu-mark-sparse t]
1892         ["Mark thread" gnus-uu-mark-thread t]
1893         ["Unmark thread" gnus-uu-unmark-thread t]
1894         ("Process Mark Sets"
1895          ["Kill" gnus-summary-kill-process-mark t]
1896          ["Yank" gnus-summary-yank-process-mark
1897           gnus-newsgroup-process-stack]
1898          ["Save" gnus-summary-save-process-mark t]))
1899        ("Scroll article"
1900         ["Page forward" gnus-summary-next-page t]
1901         ["Page backward" gnus-summary-prev-page t]
1902         ["Line forward" gnus-summary-scroll-up t])
1903        ("Move"
1904         ["Next unread article" gnus-summary-next-unread-article t]
1905         ["Previous unread article" gnus-summary-prev-unread-article t]
1906         ["Next article" gnus-summary-next-article t]
1907         ["Previous article" gnus-summary-prev-article t]
1908         ["Next unread subject" gnus-summary-next-unread-subject t]
1909         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1910         ["Next article same subject" gnus-summary-next-same-subject t]
1911         ["Previous article same subject" gnus-summary-prev-same-subject t]
1912         ["First unread article" gnus-summary-first-unread-article t]
1913         ["Best unread article" gnus-summary-best-unread-article t]
1914         ["Go to subject number..." gnus-summary-goto-subject t]
1915         ["Go to article number..." gnus-summary-goto-article t]
1916         ["Go to the last article" gnus-summary-goto-last-article t]
1917         ["Pop article off history" gnus-summary-pop-article t])
1918        ("Sort"
1919         ["Sort by number" gnus-summary-sort-by-number t]
1920         ["Sort by author" gnus-summary-sort-by-author t]
1921         ["Sort by subject" gnus-summary-sort-by-subject t]
1922         ["Sort by date" gnus-summary-sort-by-date t]
1923         ["Sort by score" gnus-summary-sort-by-score t]
1924         ["Sort by lines" gnus-summary-sort-by-lines t]
1925         ["Sort by characters" gnus-summary-sort-by-chars t])
1926        ("Help"
1927         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1928         ["Describe group" gnus-summary-describe-group t]
1929         ["Read manual" gnus-info-find-node t])
1930        ("Modes"
1931         ["Pick and read" gnus-pick-mode t]
1932         ["Binary" gnus-binary-mode t])
1933        ("Regeneration"
1934         ["Regenerate" gnus-summary-prepare t]
1935         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1936         ["Toggle threading" gnus-summary-toggle-threads t])
1937        ["Filter articles..." gnus-summary-execute-command t]
1938        ["Run command on subjects..." gnus-summary-universal-argument t]
1939        ["Search articles forward..." gnus-summary-search-article-forward t]
1940        ["Search articles backward..." gnus-summary-search-article-backward t]
1941        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1942        ["Expand window" gnus-summary-expand-window t]
1943        ["Expire expirable articles" gnus-summary-expire-articles
1944         (gnus-check-backend-function
1945          'request-expire-articles gnus-newsgroup-name)]
1946        ["Edit local kill file" gnus-summary-edit-local-kill t]
1947        ["Edit main kill file" gnus-summary-edit-global-kill t]
1948        ["Edit group parameters" gnus-summary-edit-parameters t]
1949        ["Customize group parameters" gnus-summary-customize-parameters t]
1950        ["Send a bug report" gnus-bug t]
1951        ("Exit"
1952         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1953         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1954         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1955         ["Exit group" gnus-summary-exit t]
1956         ["Exit group without updating" gnus-summary-exit-no-update t]
1957         ["Exit and goto next group" gnus-summary-next-group t]
1958         ["Exit and goto prev group" gnus-summary-prev-group t]
1959         ["Reselect group" gnus-summary-reselect-current-group t]
1960         ["Rescan group" gnus-summary-rescan-group t]
1961         ["Update dribble" gnus-summary-save-newsrc t])))
1962
1963     (gnus-run-hooks 'gnus-summary-menu-hook)))
1964
1965 (defun gnus-score-set-default (var value)
1966   "A version of set that updates the GNU Emacs menu-bar."
1967   (set var value)
1968   ;; It is the message that forces the active status to be updated.
1969   (message ""))
1970
1971 (defun gnus-make-score-map (type)
1972   "Make a summary score map of type TYPE."
1973   (if t
1974       nil
1975     (let ((headers '(("author" "from" string)
1976                      ("subject" "subject" string)
1977                      ("article body" "body" string)
1978                      ("article head" "head" string)
1979                      ("xref" "xref" string)
1980                      ("extra header" "extra" string)
1981                      ("lines" "lines" number)
1982                      ("followups to author" "followup" string)))
1983           (types '((number ("less than" <)
1984                            ("greater than" >)
1985                            ("equal" =))
1986                    (string ("substring" s)
1987                            ("exact string" e)
1988                            ("fuzzy string" f)
1989                            ("regexp" r))))
1990           (perms '(("temporary" (current-time-string))
1991                    ("permanent" nil)
1992                    ("immediate" now)))
1993           header)
1994       (list
1995        (apply
1996         'nconc
1997         (list
1998          (if (eq type 'lower)
1999              "Lower score"
2000            "Increase score"))
2001         (let (outh)
2002           (while headers
2003             (setq header (car headers))
2004             (setq outh
2005                   (cons
2006                    (apply
2007                     'nconc
2008                     (list (car header))
2009                     (let ((ts (cdr (assoc (nth 2 header) types)))
2010                           outt)
2011                       (while ts
2012                         (setq outt
2013                               (cons
2014                                (apply
2015                                 'nconc
2016                                 (list (caar ts))
2017                                 (let ((ps perms)
2018                                       outp)
2019                                   (while ps
2020                                     (setq outp
2021                                           (cons
2022                                            (vector
2023                                             (caar ps)
2024                                             (list
2025                                              'gnus-summary-score-entry
2026                                              (nth 1 header)
2027                                              (if (or (string= (nth 1 header)
2028                                                               "head")
2029                                                      (string= (nth 1 header)
2030                                                               "body"))
2031                                                  ""
2032                                                (list 'gnus-summary-header
2033                                                      (nth 1 header)))
2034                                              (list 'quote (nth 1 (car ts)))
2035                                              (list 'gnus-score-delta-default
2036                                                    nil)
2037                                              (nth 1 (car ps))
2038                                              t)
2039                                             t)
2040                                            outp))
2041                                     (setq ps (cdr ps)))
2042                                   (list (nreverse outp))))
2043                                outt))
2044                         (setq ts (cdr ts)))
2045                       (list (nreverse outt))))
2046                    outh))
2047             (setq headers (cdr headers)))
2048           (list (nreverse outh))))))))
2049
2050 \f
2051
2052 (defun gnus-summary-mode (&optional group)
2053   "Major mode for reading articles.
2054
2055 All normal editing commands are switched off.
2056 \\<gnus-summary-mode-map>
2057 Each line in this buffer represents one article.  To read an
2058 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2059 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2060 respectively.
2061
2062 You can also post articles and send mail from this buffer.  To
2063 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2064 of an article, type `\\[gnus-summary-reply]'.
2065
2066 There are approx. one gazillion commands you can execute in this
2067 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2068
2069 The following commands are available:
2070
2071 \\{gnus-summary-mode-map}"
2072   (interactive)
2073   (when (gnus-visual-p 'summary-menu 'menu)
2074     (gnus-summary-make-menu-bar))
2075   (kill-all-local-variables)
2076   (gnus-summary-make-local-variables)
2077   (gnus-make-thread-indent-array)
2078   (gnus-simplify-mode-line)
2079   (setq major-mode 'gnus-summary-mode)
2080   (setq mode-name "Summary")
2081   (make-local-variable 'minor-mode-alist)
2082   (use-local-map gnus-summary-mode-map)
2083   (buffer-disable-undo)
2084   (setq buffer-read-only t)             ;Disable modification
2085   (setq truncate-lines t)
2086   (setq selective-display t)
2087   (setq selective-display-ellipses t)   ;Display `...'
2088   (gnus-summary-set-display-table)
2089   (gnus-set-default-directory)
2090   (setq gnus-newsgroup-name group)
2091   (unless (gnus-news-group-p group)
2092     (setq gnus-newsgroup-incorporated
2093           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2094   (make-local-variable 'gnus-summary-line-format)
2095   (make-local-variable 'gnus-summary-line-format-spec)
2096   (make-local-variable 'gnus-summary-dummy-line-format)
2097   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2098   (make-local-variable 'gnus-summary-mark-positions)
2099   (make-local-hook 'pre-command-hook)
2100   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2101   (gnus-run-hooks 'gnus-summary-mode-hook)
2102   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2103   (gnus-update-summary-mark-positions))
2104
2105 (defun gnus-summary-make-local-variables ()
2106   "Make all the local summary buffer variables."
2107   (let (global)
2108     (dolist (local gnus-summary-local-variables)
2109       (if (consp local)
2110           (progn
2111             (if (eq (cdr local) 'global)
2112                 ;; Copy the global value of the variable.
2113                 (setq global (symbol-value (car local)))
2114               ;; Use the value from the list.
2115               (setq global (eval (cdr local))))
2116             (set (make-local-variable (car local)) global))
2117         ;; Simple nil-valued local variable.
2118         (set (make-local-variable local) nil)))))
2119
2120 (defun gnus-summary-clear-local-variables ()
2121   (let ((locals gnus-summary-local-variables))
2122     (while locals
2123       (if (consp (car locals))
2124           (and (vectorp (caar locals))
2125                (set (caar locals) nil))
2126         (and (vectorp (car locals))
2127              (set (car locals) nil)))
2128       (setq locals (cdr locals)))))
2129
2130 ;; Summary data functions.
2131
2132 (defmacro gnus-data-number (data)
2133   `(car ,data))
2134
2135 (defmacro gnus-data-set-number (data number)
2136   `(setcar ,data ,number))
2137
2138 (defmacro gnus-data-mark (data)
2139   `(nth 1 ,data))
2140
2141 (defmacro gnus-data-set-mark (data mark)
2142   `(setcar (nthcdr 1 ,data) ,mark))
2143
2144 (defmacro gnus-data-pos (data)
2145   `(nth 2 ,data))
2146
2147 (defmacro gnus-data-set-pos (data pos)
2148   `(setcar (nthcdr 2 ,data) ,pos))
2149
2150 (defmacro gnus-data-header (data)
2151   `(nth 3 ,data))
2152
2153 (defmacro gnus-data-set-header (data header)
2154   `(setcar (nthcdr 3 ,data) ,header))
2155
2156 (defmacro gnus-data-level (data)
2157   `(nth 4 ,data))
2158
2159 (defmacro gnus-data-unread-p (data)
2160   `(= (nth 1 ,data) gnus-unread-mark))
2161
2162 (defmacro gnus-data-read-p (data)
2163   `(/= (nth 1 ,data) gnus-unread-mark))
2164
2165 (defmacro gnus-data-pseudo-p (data)
2166   `(consp (nth 3 ,data)))
2167
2168 (defmacro gnus-data-find (number)
2169   `(assq ,number gnus-newsgroup-data))
2170
2171 (defmacro gnus-data-find-list (number &optional data)
2172   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2173      (memq (assq ,number bdata)
2174            bdata)))
2175
2176 (defmacro gnus-data-make (number mark pos header level)
2177   `(list ,number ,mark ,pos ,header ,level))
2178
2179 (defun gnus-data-enter (after-article number mark pos header level offset)
2180   (let ((data (gnus-data-find-list after-article)))
2181     (unless data
2182       (error "No such article: %d" after-article))
2183     (setcdr data (cons (gnus-data-make number mark pos header level)
2184                        (cdr data)))
2185     (setq gnus-newsgroup-data-reverse nil)
2186     (gnus-data-update-list (cddr data) offset)))
2187
2188 (defun gnus-data-enter-list (after-article list &optional offset)
2189   (when list
2190     (let ((data (and after-article (gnus-data-find-list after-article)))
2191           (ilist list))
2192       (if (not (or data
2193                    after-article))
2194           (let ((odata gnus-newsgroup-data))
2195             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2196             (when offset
2197               (gnus-data-update-list odata offset)))
2198         ;; Find the last element in the list to be spliced into the main
2199         ;; list.
2200         (while (cdr list)
2201           (setq list (cdr list)))
2202         (if (not data)
2203             (progn
2204               (setcdr list gnus-newsgroup-data)
2205               (setq gnus-newsgroup-data ilist)
2206               (when offset
2207                 (gnus-data-update-list (cdr list) offset)))
2208           (setcdr list (cdr data))
2209           (setcdr data ilist)
2210           (when offset
2211             (gnus-data-update-list (cdr list) offset))))
2212       (setq gnus-newsgroup-data-reverse nil))))
2213
2214 (defun gnus-data-remove (article &optional offset)
2215   (let ((data gnus-newsgroup-data))
2216     (if (= (gnus-data-number (car data)) article)
2217         (progn
2218           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2219                 gnus-newsgroup-data-reverse nil)
2220           (when offset
2221             (gnus-data-update-list gnus-newsgroup-data offset)))
2222       (while (cdr data)
2223         (when (= (gnus-data-number (cadr data)) article)
2224           (setcdr data (cddr data))
2225           (when offset
2226             (gnus-data-update-list (cdr data) offset))
2227           (setq data nil
2228                 gnus-newsgroup-data-reverse nil))
2229         (setq data (cdr data))))))
2230
2231 (defmacro gnus-data-list (backward)
2232   `(if ,backward
2233        (or gnus-newsgroup-data-reverse
2234            (setq gnus-newsgroup-data-reverse
2235                  (reverse gnus-newsgroup-data)))
2236      gnus-newsgroup-data))
2237
2238 (defun gnus-data-update-list (data offset)
2239   "Add OFFSET to the POS of all data entries in DATA."
2240   (setq gnus-newsgroup-data-reverse nil)
2241   (while data
2242     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2243     (setq data (cdr data))))
2244
2245 (defun gnus-summary-article-pseudo-p (article)
2246   "Say whether this article is a pseudo article or not."
2247   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2248
2249 (defmacro gnus-summary-article-sparse-p (article)
2250   "Say whether this article is a sparse article or not."
2251   `(memq ,article gnus-newsgroup-sparse))
2252
2253 (defmacro gnus-summary-article-ancient-p (article)
2254   "Say whether this article is a sparse article or not."
2255   `(memq ,article gnus-newsgroup-ancient))
2256
2257 (defun gnus-article-parent-p (number)
2258   "Say whether this article is a parent or not."
2259   (let ((data (gnus-data-find-list number)))
2260     (and (cdr data)                     ; There has to be an article after...
2261          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2262             (gnus-data-level (nth 1 data))))))
2263
2264 (defun gnus-article-children (number)
2265   "Return a list of all children to NUMBER."
2266   (let* ((data (gnus-data-find-list number))
2267          (level (gnus-data-level (car data)))
2268          children)
2269     (setq data (cdr data))
2270     (while (and data
2271                 (= (gnus-data-level (car data)) (1+ level)))
2272       (push (gnus-data-number (car data)) children)
2273       (setq data (cdr data)))
2274     children))
2275
2276 (defmacro gnus-summary-skip-intangible ()
2277   "If the current article is intangible, then jump to a different article."
2278   '(let ((to (get-text-property (point) 'gnus-intangible)))
2279      (and to (gnus-summary-goto-subject to))))
2280
2281 (defmacro gnus-summary-article-intangible-p ()
2282   "Say whether this article is intangible or not."
2283   '(get-text-property (point) 'gnus-intangible))
2284
2285 (defun gnus-article-read-p (article)
2286   "Say whether ARTICLE is read or not."
2287   (not (or (memq article gnus-newsgroup-marked)
2288            (memq article gnus-newsgroup-unreads)
2289            (memq article gnus-newsgroup-unselected)
2290            (memq article gnus-newsgroup-dormant))))
2291
2292 ;; Some summary mode macros.
2293
2294 (defmacro gnus-summary-article-number ()
2295   "The article number of the article on the current line.
2296 If there isn's an article number here, then we return the current
2297 article number."
2298   '(progn
2299      (gnus-summary-skip-intangible)
2300      (or (get-text-property (point) 'gnus-number)
2301          (gnus-summary-last-subject))))
2302
2303 (defmacro gnus-summary-article-header (&optional number)
2304   "Return the header of article NUMBER."
2305   `(gnus-data-header (gnus-data-find
2306                       ,(or number '(gnus-summary-article-number)))))
2307
2308 (defmacro gnus-summary-thread-level (&optional number)
2309   "Return the level of thread that starts with article NUMBER."
2310   `(if (and (eq gnus-summary-make-false-root 'dummy)
2311             (get-text-property (point) 'gnus-intangible))
2312        0
2313      (gnus-data-level (gnus-data-find
2314                        ,(or number '(gnus-summary-article-number))))))
2315
2316 (defmacro gnus-summary-article-mark (&optional number)
2317   "Return the mark of article NUMBER."
2318   `(gnus-data-mark (gnus-data-find
2319                     ,(or number '(gnus-summary-article-number)))))
2320
2321 (defmacro gnus-summary-article-pos (&optional number)
2322   "Return the position of the line of article NUMBER."
2323   `(gnus-data-pos (gnus-data-find
2324                    ,(or number '(gnus-summary-article-number)))))
2325
2326 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2327 (defmacro gnus-summary-article-subject (&optional number)
2328   "Return current subject string or nil if nothing."
2329   `(let ((headers
2330           ,(if number
2331                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2332              '(gnus-data-header (assq (gnus-summary-article-number)
2333                                       gnus-newsgroup-data)))))
2334      (and headers
2335           (vectorp headers)
2336           (mail-header-subject headers))))
2337
2338 (defmacro gnus-summary-article-score (&optional number)
2339   "Return current article score."
2340   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2341                   gnus-newsgroup-scored))
2342        gnus-summary-default-score 0))
2343
2344 (defun gnus-summary-article-children (&optional number)
2345   "Return a list of article numbers that are children of article NUMBER."
2346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2347          (level (gnus-data-level (car data)))
2348          l children)
2349     (while (and (setq data (cdr data))
2350                 (> (setq l (gnus-data-level (car data))) level))
2351       (and (= (1+ level) l)
2352            (push (gnus-data-number (car data))
2353                  children)))
2354     (nreverse children)))
2355
2356 (defun gnus-summary-article-parent (&optional number)
2357   "Return the article number of the parent of article NUMBER."
2358   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2359                                     (gnus-data-list t)))
2360          (level (gnus-data-level (car data))))
2361     (if (zerop level)
2362         ()                              ; This is a root.
2363       ;; We search until we find an article with a level less than
2364       ;; this one.  That function has to be the parent.
2365       (while (and (setq data (cdr data))
2366                   (not (< (gnus-data-level (car data)) level))))
2367       (and data (gnus-data-number (car data))))))
2368
2369 (defun gnus-unread-mark-p (mark)
2370   "Say whether MARK is the unread mark."
2371   (= mark gnus-unread-mark))
2372
2373 (defun gnus-read-mark-p (mark)
2374   "Say whether MARK is one of the marks that mark as read.
2375 This is all marks except unread, ticked, dormant, and expirable."
2376   (not (or (= mark gnus-unread-mark)
2377            (= mark gnus-ticked-mark)
2378            (= mark gnus-dormant-mark)
2379            (= mark gnus-expirable-mark))))
2380
2381 (defmacro gnus-article-mark (number)
2382   "Return the MARK of article NUMBER.
2383 This macro should only be used when computing the mark the \"first\"
2384 time; i.e., when generating the summary lines.  After that,
2385 `gnus-summary-article-mark' should be used to examine the
2386 marks of articles."
2387   `(cond
2388     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2389     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2390     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2391     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2392     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2393     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2394     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2395     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2396            gnus-ancient-mark))))
2397
2398 ;; Saving hidden threads.
2399
2400 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2401 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2402
2403 (defmacro gnus-save-hidden-threads (&rest forms)
2404   "Save hidden threads, eval FORMS, and restore the hidden threads."
2405   (let ((config (make-symbol "config")))
2406     `(let ((,config (gnus-hidden-threads-configuration)))
2407        (unwind-protect
2408            (save-excursion
2409              ,@forms)
2410          (gnus-restore-hidden-threads-configuration ,config)))))
2411
2412 (defun gnus-data-compute-positions ()
2413   "Compute the positions of all articles."
2414   (setq gnus-newsgroup-data-reverse nil)
2415   (let ((data gnus-newsgroup-data))
2416     (save-excursion
2417       (gnus-save-hidden-threads
2418         (gnus-summary-show-all-threads)
2419         (goto-char (point-min))
2420         (while data
2421           (while (get-text-property (point) 'gnus-intangible)
2422             (forward-line 1))
2423           (gnus-data-set-pos (car data) (+ (point) 3))
2424           (setq data (cdr data))
2425           (forward-line 1))))))
2426
2427 (defun gnus-hidden-threads-configuration ()
2428   "Return the current hidden threads configuration."
2429   (save-excursion
2430     (let (config)
2431       (goto-char (point-min))
2432       (while (search-forward "\r" nil t)
2433         (push (1- (point)) config))
2434       config)))
2435
2436 (defun gnus-restore-hidden-threads-configuration (config)
2437   "Restore hidden threads configuration from CONFIG."
2438   (let (point buffer-read-only)
2439     (while (setq point (pop config))
2440       (when (and (< point (point-max))
2441                  (goto-char point)
2442                  (eq (char-after) ?\n))
2443         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2444
2445 ;; Various summary mode internalish functions.
2446
2447 (defun gnus-mouse-pick-article (e)
2448   (interactive "e")
2449   (mouse-set-point e)
2450   (gnus-summary-next-page nil t))
2451
2452 (defun gnus-summary-set-display-table ()
2453   ;; Change the display table.  Odd characters have a tendency to mess
2454   ;; up nicely formatted displays - we make all possible glyphs
2455   ;; display only a single character.
2456
2457   ;; We start from the standard display table, if any.
2458   (let ((table (or (copy-sequence standard-display-table)
2459                    (make-display-table)))
2460         (i 32))
2461     ;; Nix out all the control chars...
2462     (while (>= (setq i (1- i)) 0)
2463       (aset table i [??]))
2464     ;; ... but not newline and cr, of course.  (cr is necessary for the
2465     ;; selective display).
2466     (aset table ?\n nil)
2467     (aset table ?\r nil)
2468     ;; We keep TAB as well.
2469     (aset table ?\t nil)
2470     ;; We nix out any glyphs over 126 that are not set already.
2471     (let ((i 256))
2472       (while (>= (setq i (1- i)) 127)
2473         ;; Only modify if the entry is nil.
2474         (unless (aref table i)
2475           (aset table i [??]))))
2476     (setq buffer-display-table table)))
2477
2478 (defun gnus-summary-setup-buffer (group)
2479   "Initialize summary buffer."
2480   (let ((buffer (concat "*Summary " group "*")))
2481     (if (get-buffer buffer)
2482         (progn
2483           (set-buffer buffer)
2484           (setq gnus-summary-buffer (current-buffer))
2485           (not gnus-newsgroup-prepared))
2486       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2487       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2488       (gnus-summary-mode group)
2489       (when gnus-carpal
2490         (gnus-carpal-setup-buffer 'summary))
2491       (unless gnus-single-article-buffer
2492         (make-local-variable 'gnus-article-buffer)
2493         (make-local-variable 'gnus-article-current)
2494         (make-local-variable 'gnus-original-article-buffer))
2495       (setq gnus-newsgroup-name group)
2496       t)))
2497
2498 (defun gnus-set-global-variables ()
2499   ;; Set the global equivalents of the summary buffer-local variables
2500   ;; to the latest values they had.  These reflect the summary buffer
2501   ;; that was in action when the last article was fetched.
2502   (when (eq major-mode 'gnus-summary-mode)
2503     (setq gnus-summary-buffer (current-buffer))
2504     (let ((name gnus-newsgroup-name)
2505           (marked gnus-newsgroup-marked)
2506           (unread gnus-newsgroup-unreads)
2507           (headers gnus-current-headers)
2508           (data gnus-newsgroup-data)
2509           (summary gnus-summary-buffer)
2510           (article-buffer gnus-article-buffer)
2511           (original gnus-original-article-buffer)
2512           (gac gnus-article-current)
2513           (reffed gnus-reffed-article-number)
2514           (score-file gnus-current-score-file)
2515           (default-charset gnus-newsgroup-charset))
2516       (save-excursion
2517         (set-buffer gnus-group-buffer)
2518         (setq gnus-newsgroup-name name
2519               gnus-newsgroup-marked marked
2520               gnus-newsgroup-unreads unread
2521               gnus-current-headers headers
2522               gnus-newsgroup-data data
2523               gnus-article-current gac
2524               gnus-summary-buffer summary
2525               gnus-article-buffer article-buffer
2526               gnus-original-article-buffer original
2527               gnus-reffed-article-number reffed
2528               gnus-current-score-file score-file
2529               gnus-newsgroup-charset default-charset)
2530         ;; The article buffer also has local variables.
2531         (when (gnus-buffer-live-p gnus-article-buffer)
2532           (set-buffer gnus-article-buffer)
2533           (setq gnus-summary-buffer summary))))))
2534
2535 (defun gnus-summary-article-unread-p (article)
2536   "Say whether ARTICLE is unread or not."
2537   (memq article gnus-newsgroup-unreads))
2538
2539 (defun gnus-summary-first-article-p (&optional article)
2540   "Return whether ARTICLE is the first article in the buffer."
2541   (if (not (setq article (or article (gnus-summary-article-number))))
2542       nil
2543     (eq article (caar gnus-newsgroup-data))))
2544
2545 (defun gnus-summary-last-article-p (&optional article)
2546   "Return whether ARTICLE is the last article in the buffer."
2547   (if (not (setq article (or article (gnus-summary-article-number))))
2548       ;; All non-existent numbers are the last article.  :-)
2549       t
2550     (not (cdr (gnus-data-find-list article)))))
2551
2552 (defun gnus-make-thread-indent-array ()
2553   (let ((n 200))
2554     (unless (and gnus-thread-indent-array
2555                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2556       (setq gnus-thread-indent-array (make-vector 201 "")
2557             gnus-thread-indent-array-level gnus-thread-indent-level)
2558       (while (>= n 0)
2559         (aset gnus-thread-indent-array n
2560               (make-string (* n gnus-thread-indent-level) ? ))
2561         (setq n (1- n))))))
2562
2563 (defun gnus-update-summary-mark-positions ()
2564   "Compute where the summary marks are to go."
2565   (save-excursion
2566     (when (gnus-buffer-exists-p gnus-summary-buffer)
2567       (set-buffer gnus-summary-buffer))
2568     (let ((gnus-replied-mark 129)
2569           (gnus-score-below-mark 130)
2570           (gnus-score-over-mark 130)
2571           (gnus-download-mark 131)
2572           (spec gnus-summary-line-format-spec)
2573           gnus-visual pos)
2574       (save-excursion
2575         (gnus-set-work-buffer)
2576         (let ((gnus-summary-line-format-spec spec)
2577               (gnus-newsgroup-downloadable '((0 . t))))
2578           (gnus-summary-insert-line
2579            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2580            0 nil 128 t nil "" nil 1)
2581           (goto-char (point-min))
2582           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2583                                              (- (point) 2)))))
2584           (goto-char (point-min))
2585           (push (cons 'replied (and (search-forward "\201" nil t)
2586                                     (- (point) 2)))
2587                 pos)
2588           (goto-char (point-min))
2589           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2590                 pos)
2591           (goto-char (point-min))
2592           (push (cons 'download
2593                       (and (search-forward "\203" nil t) (- (point) 2)))
2594                 pos)))
2595       (setq gnus-summary-mark-positions pos))))
2596
2597 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2598   "Insert a dummy root in the summary buffer."
2599   (beginning-of-line)
2600   (gnus-add-text-properties
2601    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2602    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2603
2604 (defun gnus-summary-from-or-to-or-newsgroups (header)
2605   (let ((to (cdr (assq 'To (mail-header-extra header))))
2606         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2607         (default-mime-charset (with-current-buffer gnus-summary-buffer
2608                                 default-mime-charset)))
2609     (cond
2610      ((and to
2611            gnus-ignored-from-addresses
2612            (string-match gnus-ignored-from-addresses
2613                          (mail-header-from header)))
2614       (concat "-> "
2615               (or (car (funcall gnus-extract-address-components
2616                                 (funcall
2617                                  gnus-decode-encoded-word-function to)))
2618                   (funcall gnus-decode-encoded-word-function to))))
2619      ((and newsgroups
2620            gnus-ignored-from-addresses
2621            (string-match gnus-ignored-from-addresses
2622                          (mail-header-from header)))
2623       (concat "=> " newsgroups))
2624      (t
2625       (or (car (funcall gnus-extract-address-components
2626                         (mail-header-from header)))
2627           (mail-header-from header))))))
2628
2629 (defun gnus-summary-insert-line (gnus-tmp-header
2630                                  gnus-tmp-level gnus-tmp-current
2631                                  gnus-tmp-unread gnus-tmp-replied
2632                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2633                                  &optional gnus-tmp-dummy gnus-tmp-score
2634                                  gnus-tmp-process)
2635   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2636          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2637          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2638          (gnus-tmp-score-char
2639           (if (or (null gnus-summary-default-score)
2640                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2641                       gnus-summary-zcore-fuzz))
2642               ?  ;Whitespace
2643             (if (< gnus-tmp-score gnus-summary-default-score)
2644                 gnus-score-below-mark gnus-score-over-mark)))
2645          (gnus-tmp-replied
2646           (cond (gnus-tmp-process gnus-process-mark)
2647                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2648                  gnus-cached-mark)
2649                 (gnus-tmp-replied gnus-replied-mark)
2650                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2651                  gnus-saved-mark)
2652                 (t gnus-unread-mark)))
2653          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2654          (gnus-tmp-name
2655           (cond
2656            ((string-match "<[^>]+> *$" gnus-tmp-from)
2657             (let ((beg (match-beginning 0)))
2658               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2659                        (substring gnus-tmp-from (1+ (match-beginning 0))
2660                                   (1- (match-end 0))))
2661                   (substring gnus-tmp-from 0 beg))))
2662            ((string-match "(.+)" gnus-tmp-from)
2663             (substring gnus-tmp-from
2664                        (1+ (match-beginning 0)) (1- (match-end 0))))
2665            (t gnus-tmp-from)))
2666          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2667          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2668          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2669          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2670          (buffer-read-only nil))
2671     (when (string= gnus-tmp-name "")
2672       (setq gnus-tmp-name gnus-tmp-from))
2673     (unless (numberp gnus-tmp-lines)
2674       (setq gnus-tmp-lines 0))
2675     (gnus-put-text-property-excluding-characters-with-faces
2676      (point)
2677      (progn (eval gnus-summary-line-format-spec) (point))
2678      'gnus-number gnus-tmp-number)
2679     (when (gnus-visual-p 'summary-highlight 'highlight)
2680       (forward-line -1)
2681       (gnus-run-hooks 'gnus-summary-update-hook)
2682       (forward-line 1))))
2683
2684 (defun gnus-summary-update-line (&optional dont-update)
2685   ;; Update summary line after change.
2686   (when (and gnus-summary-default-score
2687              (not gnus-summary-inhibit-highlight))
2688     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2689            (article (gnus-summary-article-number))
2690            (score (gnus-summary-article-score article)))
2691       (unless dont-update
2692         (if (and gnus-summary-mark-below
2693                  (< (gnus-summary-article-score)
2694                     gnus-summary-mark-below))
2695             ;; This article has a low score, so we mark it as read.
2696             (when (memq article gnus-newsgroup-unreads)
2697               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2698           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2699             ;; This article was previously marked as read on account
2700             ;; of a low score, but now it has risen, so we mark it as
2701             ;; unread.
2702             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2703         (gnus-summary-update-mark
2704          (if (or (null gnus-summary-default-score)
2705                  (<= (abs (- score gnus-summary-default-score))
2706                      gnus-summary-zcore-fuzz))
2707              ?  ;Whitespace
2708            (if (< score gnus-summary-default-score)
2709                gnus-score-below-mark gnus-score-over-mark))
2710          'score))
2711       ;; Do visual highlighting.
2712       (when (gnus-visual-p 'summary-highlight 'highlight)
2713         (gnus-run-hooks 'gnus-summary-update-hook)))))
2714
2715 (defvar gnus-tmp-new-adopts nil)
2716
2717 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2718   "Return the number of articles in THREAD.
2719 This may be 0 in some cases -- if none of the articles in
2720 the thread are to be displayed."
2721   (let* ((number
2722           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2723           (cond
2724            ((not (listp thread))
2725             1)
2726            ((and (consp thread) (cdr thread))
2727             (apply
2728              '+ 1 (mapcar
2729                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2730            ((null thread)
2731             1)
2732            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2733             1)
2734            (t 0))))
2735     (when (and level (zerop level) gnus-tmp-new-adopts)
2736       (incf number
2737             (apply '+ (mapcar
2738                        'gnus-summary-number-of-articles-in-thread
2739                        gnus-tmp-new-adopts))))
2740     (if char
2741         (if (> number 1) gnus-not-empty-thread-mark
2742           gnus-empty-thread-mark)
2743       number)))
2744
2745 (defun gnus-summary-set-local-parameters (group)
2746   "Go through the local params of GROUP and set all variable specs in that list."
2747   (let ((params (gnus-group-find-parameter group))
2748         elem)
2749     (while params
2750       (setq elem (car params)
2751             params (cdr params))
2752       (and (consp elem)                 ; Has to be a cons.
2753            (consp (cdr elem))           ; The cdr has to be a list.
2754            (symbolp (car elem))         ; Has to be a symbol in there.
2755            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2756            (ignore-errors               ; So we set it.
2757              (make-local-variable (car elem))
2758              (set (car elem) (eval (nth 1 elem))))))))
2759
2760 (defun gnus-summary-read-group (group &optional show-all no-article
2761                                       kill-buffer no-display backward
2762                                       select-articles)
2763   "Start reading news in newsgroup GROUP.
2764 If SHOW-ALL is non-nil, already read articles are also listed.
2765 If NO-ARTICLE is non-nil, no article is selected initially.
2766 If NO-DISPLAY, don't generate a summary buffer."
2767   (let (result)
2768     (while (and group
2769                 (null (setq result
2770                             (let ((gnus-auto-select-next nil))
2771                               (or (gnus-summary-read-group-1
2772                                    group show-all no-article
2773                                    kill-buffer no-display
2774                                    select-articles)
2775                                   (setq show-all nil
2776                                         select-articles nil)))))
2777                 (eq gnus-auto-select-next 'quietly))
2778       (set-buffer gnus-group-buffer)
2779       ;; The entry function called above goes to the next
2780       ;; group automatically, so we go two groups back
2781       ;; if we are searching for the previous group.
2782       (when backward
2783         (gnus-group-prev-unread-group 2))
2784       (if (not (equal group (gnus-group-group-name)))
2785           (setq group (gnus-group-group-name))
2786         (setq group nil)))
2787     result))
2788
2789 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2790   "Directly jump to the other GROUP from summary buffer.
2791 If SHOW-ALL is non-nil, already read articles are also listed."
2792   (interactive
2793    (if (eq gnus-summary-buffer (current-buffer))
2794        (list (completing-read
2795               "Group: " gnus-active-hashtb nil t
2796               (when (and gnus-newsgroup-name
2797                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2798                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2799               'gnus-group-history)
2800              current-prefix-arg)
2801      (error "%s must be invoked from a gnus summary buffer." this-command)))
2802   (unless (or (zerop (length group))
2803               (and gnus-newsgroup-name
2804                    (string-equal gnus-newsgroup-name group)))
2805     (gnus-summary-exit)
2806     (gnus-summary-read-group group show-all
2807                              gnus-dont-select-after-jump-to-other-group)))
2808
2809 (defun gnus-summary-read-group-1 (group show-all no-article
2810                                         kill-buffer no-display
2811                                         &optional select-articles)
2812   ;; Killed foreign groups can't be entered.
2813   (when (and (not (gnus-group-native-p group))
2814              (not (gnus-gethash group gnus-newsrc-hashtb)))
2815     (error "Dead non-native groups can't be entered"))
2816   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2817   (let* ((new-group (gnus-summary-setup-buffer group))
2818          (quit-config (gnus-group-quit-config group))
2819          (did-select (and new-group (gnus-select-newsgroup
2820                                      group show-all select-articles))))
2821     (cond
2822      ;; This summary buffer exists already, so we just select it.
2823      ((not new-group)
2824       (gnus-set-global-variables)
2825       (when kill-buffer
2826         (gnus-kill-or-deaden-summary kill-buffer))
2827       (gnus-configure-windows 'summary 'force)
2828       (gnus-set-mode-line 'summary)
2829       (gnus-summary-position-point)
2830       (message "")
2831       t)
2832      ;; We couldn't select this group.
2833      ((null did-select)
2834       (when (and (eq major-mode 'gnus-summary-mode)
2835                  (not (equal (current-buffer) kill-buffer)))
2836         (kill-buffer (current-buffer))
2837         (if (not quit-config)
2838             (progn
2839               ;; Update the info -- marks might need to be removed,
2840               ;; for instance.
2841               (gnus-summary-update-info)
2842               (set-buffer gnus-group-buffer)
2843               (gnus-group-jump-to-group group)
2844               (gnus-group-next-unread-group 1))
2845           (gnus-handle-ephemeral-exit quit-config)))
2846       (gnus-message 3 "Can't select group")
2847       nil)
2848      ;; The user did a `C-g' while prompting for number of articles,
2849      ;; so we exit this group.
2850      ((eq did-select 'quit)
2851       (and (eq major-mode 'gnus-summary-mode)
2852            (not (equal (current-buffer) kill-buffer))
2853            (kill-buffer (current-buffer)))
2854       (when kill-buffer
2855         (gnus-kill-or-deaden-summary kill-buffer))
2856       (if (not quit-config)
2857           (progn
2858             (set-buffer gnus-group-buffer)
2859             (gnus-group-jump-to-group group)
2860             (gnus-group-next-unread-group 1)
2861             (gnus-configure-windows 'group 'force))
2862         (gnus-handle-ephemeral-exit quit-config))
2863       ;; Finally signal the quit.
2864       (signal 'quit nil))
2865      ;; The group was successfully selected.
2866      (t
2867       (gnus-set-global-variables)
2868       ;; Save the active value in effect when the group was entered.
2869       (setq gnus-newsgroup-active
2870             (gnus-copy-sequence
2871              (gnus-active gnus-newsgroup-name)))
2872       ;; You can change the summary buffer in some way with this hook.
2873       (gnus-run-hooks 'gnus-select-group-hook)
2874       ;; Set any local variables in the group parameters.
2875       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2876       (gnus-update-format-specifications
2877        nil 'summary 'summary-mode 'summary-dummy)
2878       (gnus-update-summary-mark-positions)
2879       ;; Do score processing.
2880       (when gnus-use-scoring
2881         (gnus-possibly-score-headers))
2882       ;; Check whether to fill in the gaps in the threads.
2883       (when gnus-build-sparse-threads
2884         (gnus-build-sparse-threads))
2885       ;; Find the initial limit.
2886       (if gnus-show-threads
2887           (if show-all
2888               (let ((gnus-newsgroup-dormant nil))
2889                 (gnus-summary-initial-limit show-all))
2890             (gnus-summary-initial-limit show-all))
2891         ;; When untreaded, all articles are always shown.
2892         (setq gnus-newsgroup-limit
2893               (mapcar
2894                (lambda (header) (mail-header-number header))
2895                gnus-newsgroup-headers)))
2896       ;; Generate the summary buffer.
2897       (unless no-display
2898         (gnus-summary-prepare))
2899       (when gnus-use-trees
2900         (gnus-tree-open group)
2901         (setq gnus-summary-highlight-line-function
2902               'gnus-tree-highlight-article))
2903       ;; If the summary buffer is empty, but there are some low-scored
2904       ;; articles or some excluded dormants, we include these in the
2905       ;; buffer.
2906       (when (and (zerop (buffer-size))
2907                  (not no-display))
2908         (cond (gnus-newsgroup-dormant
2909                (gnus-summary-limit-include-dormant))
2910               ((and gnus-newsgroup-scored show-all)
2911                (gnus-summary-limit-include-expunged t))))
2912       ;; Function `gnus-apply-kill-file' must be called in this hook.
2913       (gnus-run-hooks 'gnus-apply-kill-hook)
2914       (if (and (zerop (buffer-size))
2915                (not no-display))
2916           (progn
2917             ;; This newsgroup is empty.
2918             (gnus-summary-catchup-and-exit nil t)
2919             (gnus-message 6 "No unread news")
2920             (when kill-buffer
2921               (gnus-kill-or-deaden-summary kill-buffer))
2922             ;; Return nil from this function.
2923             nil)
2924         ;; Hide conversation thread subtrees.  We cannot do this in
2925         ;; gnus-summary-prepare-hook since kill processing may not
2926         ;; work with hidden articles.
2927         (and gnus-show-threads
2928              gnus-thread-hide-subtree
2929              (gnus-summary-hide-all-threads))
2930         (when kill-buffer
2931           (gnus-kill-or-deaden-summary kill-buffer))
2932         ;; Show first unread article if requested.
2933         (if (and (not no-article)
2934                  (not no-display)
2935                  gnus-newsgroup-unreads
2936                  gnus-auto-select-first)
2937             (progn
2938               (gnus-configure-windows 'summary)
2939               (cond
2940                ((eq gnus-auto-select-first 'best)
2941                 (gnus-summary-best-unread-article))
2942                ((eq gnus-auto-select-first t)
2943                 (gnus-summary-first-unread-article))
2944                ((gnus-functionp gnus-auto-select-first)
2945                 (funcall gnus-auto-select-first))))
2946           ;; Don't select any articles, just move point to the first
2947           ;; article in the group.
2948           (goto-char (point-min))
2949           (gnus-summary-position-point)
2950           (gnus-configure-windows 'summary 'force)
2951           (gnus-set-mode-line 'summary))
2952         (when (get-buffer-window gnus-group-buffer t)
2953           ;; Gotta use windows, because recenter does weird stuff if
2954           ;; the current buffer ain't the displayed window.
2955           (let ((owin (selected-window)))
2956             (select-window (get-buffer-window gnus-group-buffer t))
2957             (when (gnus-group-goto-group group)
2958               (recenter))
2959             (select-window owin)))
2960         ;; Mark this buffer as "prepared".
2961         (setq gnus-newsgroup-prepared t)
2962         (gnus-run-hooks 'gnus-summary-prepared-hook)
2963         t)))))
2964
2965 (defun gnus-summary-prepare ()
2966   "Generate the summary buffer."
2967   (interactive)
2968   (let ((buffer-read-only nil))
2969     (erase-buffer)
2970     (setq gnus-newsgroup-data nil
2971           gnus-newsgroup-data-reverse nil)
2972     (gnus-run-hooks 'gnus-summary-generate-hook)
2973     ;; Generate the buffer, either with threads or without.
2974     (when gnus-newsgroup-headers
2975       (gnus-summary-prepare-threads
2976        (if gnus-show-threads
2977            (gnus-sort-gathered-threads
2978             (funcall gnus-summary-thread-gathering-function
2979                      (gnus-sort-threads
2980                       (gnus-cut-threads (gnus-make-threads)))))
2981          ;; Unthreaded display.
2982          (gnus-sort-articles gnus-newsgroup-headers))))
2983     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2984     ;; Call hooks for modifying summary buffer.
2985     (goto-char (point-min))
2986     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2987
2988 (defsubst gnus-general-simplify-subject (subject)
2989   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2990   (setq subject
2991         (cond
2992          ;; Truncate the subject.
2993          (gnus-simplify-subject-functions
2994           (gnus-map-function gnus-simplify-subject-functions subject))
2995          ((numberp gnus-summary-gather-subject-limit)
2996           (setq subject (gnus-simplify-subject-re subject))
2997           (if (> (length subject) gnus-summary-gather-subject-limit)
2998               (substring subject 0 gnus-summary-gather-subject-limit)
2999             subject))
3000          ;; Fuzzily simplify it.
3001          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3002           (gnus-simplify-subject-fuzzy subject))
3003          ;; Just remove the leading "Re:".
3004          (t
3005           (gnus-simplify-subject-re subject))))
3006
3007   (if (and gnus-summary-gather-exclude-subject
3008            (string-match gnus-summary-gather-exclude-subject subject))
3009       nil                               ; This article shouldn't be gathered
3010     subject))
3011
3012 (defun gnus-summary-simplify-subject-query ()
3013   "Query where the respool algorithm would put this article."
3014   (interactive)
3015   (gnus-summary-select-article)
3016   (message "%s"
3017            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3018
3019 (defun gnus-gather-threads-by-subject (threads)
3020   "Gather threads by looking at Subject headers."
3021   (if (not gnus-summary-make-false-root)
3022       threads
3023     (let ((hashtb (gnus-make-hashtable 1024))
3024           (prev threads)
3025           (result threads)
3026           subject hthread whole-subject)
3027       (while threads
3028         (setq subject (gnus-general-simplify-subject
3029                        (setq whole-subject (mail-header-subject
3030                                             (caar threads)))))
3031         (when subject
3032           (if (setq hthread (gnus-gethash subject hashtb))
3033               (progn
3034                 ;; We enter a dummy root into the thread, if we
3035                 ;; haven't done that already.
3036                 (unless (stringp (caar hthread))
3037                   (setcar hthread (list whole-subject (car hthread))))
3038                 ;; We add this new gathered thread to this gathered
3039                 ;; thread.
3040                 (setcdr (car hthread)
3041                         (nconc (cdar hthread) (list (car threads))))
3042                 ;; Remove it from the list of threads.
3043                 (setcdr prev (cdr threads))
3044                 (setq threads prev))
3045             ;; Enter this thread into the hash table.
3046             (gnus-sethash subject threads hashtb)))
3047         (setq prev threads)
3048         (setq threads (cdr threads)))
3049       result)))
3050
3051 (defun gnus-gather-threads-by-references (threads)
3052   "Gather threads by looking at References headers."
3053   (let ((idhashtb (gnus-make-hashtable 1024))
3054         (thhashtb (gnus-make-hashtable 1024))
3055         (prev threads)
3056         (result threads)
3057         ids references id gthread gid entered ref)
3058     (while threads
3059       (when (setq references (mail-header-references (caar threads)))
3060         (setq id (mail-header-id (caar threads))
3061               ids (gnus-split-references references)
3062               entered nil)
3063         (while (setq ref (pop ids))
3064           (setq ids (delete ref ids))
3065           (if (not (setq gid (gnus-gethash ref idhashtb)))
3066               (progn
3067                 (gnus-sethash ref id idhashtb)
3068                 (gnus-sethash id threads thhashtb))
3069             (setq gthread (gnus-gethash gid thhashtb))
3070             (unless entered
3071               ;; We enter a dummy root into the thread, if we
3072               ;; haven't done that already.
3073               (unless (stringp (caar gthread))
3074                 (setcar gthread (list (mail-header-subject (caar gthread))
3075                                       (car gthread))))
3076               ;; We add this new gathered thread to this gathered
3077               ;; thread.
3078               (setcdr (car gthread)
3079                       (nconc (cdar gthread) (list (car threads)))))
3080             ;; Add it into the thread hash table.
3081             (gnus-sethash id gthread thhashtb)
3082             (setq entered t)
3083             ;; Remove it from the list of threads.
3084             (setcdr prev (cdr threads))
3085             (setq threads prev))))
3086       (setq prev threads)
3087       (setq threads (cdr threads)))
3088     result))
3089
3090 (defun gnus-sort-gathered-threads (threads)
3091   "Sort subtreads inside each gathered thread by article number."
3092   (let ((result threads))
3093     (while threads
3094       (when (stringp (caar threads))
3095         (setcdr (car threads)
3096                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3097       (setq threads (cdr threads)))
3098     result))
3099
3100 (defun gnus-thread-loop-p (root thread)
3101   "Say whether ROOT is in THREAD."
3102   (let ((stack (list thread))
3103         (infloop 0)
3104         th)
3105     (while (setq thread (pop stack))
3106       (setq th (cdr thread))
3107       (while (and th
3108                   (not (eq (caar th) root)))
3109         (pop th))
3110       (if th
3111           ;; We have found a loop.
3112           (let (ref-dep)
3113             (setcdr thread (delq (car th) (cdr thread)))
3114             (if (boundp (setq ref-dep (intern "none"
3115                                               gnus-newsgroup-dependencies)))
3116                 (setcdr (symbol-value ref-dep)
3117                         (nconc (cdr (symbol-value ref-dep))
3118                                (list (car th))))
3119               (set ref-dep (list nil (car th))))
3120             (setq infloop 1
3121                   stack nil))
3122         ;; Push all the subthreads onto the stack.
3123         (push (cdr thread) stack)))
3124     infloop))
3125
3126 (defun gnus-make-threads ()
3127   "Go through the dependency hashtb and find the roots.  Return all threads."
3128   (let (threads)
3129     (while (catch 'infloop
3130              (mapatoms
3131               (lambda (refs)
3132                 ;; Deal with self-referencing References loops.
3133                 (when (and (car (symbol-value refs))
3134                            (not (zerop
3135                                  (apply
3136                                   '+
3137                                   (mapcar
3138                                    (lambda (thread)
3139                                      (gnus-thread-loop-p
3140                                       (car (symbol-value refs)) thread))
3141                                    (cdr (symbol-value refs)))))))
3142                   (setq threads nil)
3143                   (throw 'infloop t))
3144                 (unless (car (symbol-value refs))
3145                   ;; These threads do not refer back to any other articles,
3146                   ;; so they're roots.
3147                   (setq threads (append (cdr (symbol-value refs)) threads))))
3148               gnus-newsgroup-dependencies)))
3149     threads))
3150
3151 ;; Build the thread tree.
3152 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3153   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3154
3155 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3156 if it was already present.
3157
3158 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3159 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3160 Message-IDs will be renamed be renamed to a unique Message-ID before
3161 being entered.
3162
3163 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3164   (let* ((id (mail-header-id header))
3165          (id-dep (and id (intern id dependencies)))
3166          ref ref-dep ref-header)
3167     ;; Enter this `header' in the `dependencies' table.
3168     (cond
3169      ((not id-dep)
3170       (setq header nil))
3171      ;; The first two cases do the normal part: enter a new `header'
3172      ;; in the `dependencies' table.
3173      ((not (boundp id-dep))
3174       (set id-dep (list header)))
3175      ((null (car (symbol-value id-dep)))
3176       (setcar (symbol-value id-dep) header))
3177
3178      ;; From here the `header' was already present in the
3179      ;; `dependencies' table.
3180      (force-new
3181       ;; Overrides an existing entry;
3182       ;; just set the header part of the entry.
3183       (setcar (symbol-value id-dep) header))
3184
3185      ;; Renames the existing `header' to a unique Message-ID.
3186      ((not gnus-summary-ignore-duplicates)
3187       ;; An article with this Message-ID has already been seen.
3188       ;; We rename the Message-ID.
3189       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3190            (list header))
3191       (mail-header-set-id header id))
3192
3193      ;; The last case ignores an existing entry, except it adds any
3194      ;; additional Xrefs (in case the two articles came from different
3195      ;; servers.
3196      ;; Also sets `header' to `nil' meaning that the `dependencies'
3197      ;; table was *not* modified.
3198      (t
3199       (mail-header-set-xref
3200        (car (symbol-value id-dep))
3201        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3202                    "")
3203                (or (mail-header-xref header) "")))
3204       (setq header nil)))
3205
3206     (when header
3207       ;; First check if that we are not creating a References loop.
3208       (setq ref (gnus-parent-id (mail-header-references header)))
3209       (while (and ref
3210                   (setq ref-dep (intern-soft ref dependencies))
3211                   (boundp ref-dep)
3212                   (setq ref-header (car (symbol-value ref-dep))))
3213         (if (string= id ref)
3214             ;; Yuk!  This is a reference loop.  Make the article be a
3215             ;; root article.
3216             (progn
3217               (mail-header-set-references (car (symbol-value id-dep)) "none")
3218               (setq ref nil))
3219           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3220       (setq ref (gnus-parent-id (mail-header-references header)))
3221       (setq ref-dep (intern (or ref "none") dependencies))
3222       (if (boundp ref-dep)
3223           (setcdr (symbol-value ref-dep)
3224                   (nconc (cdr (symbol-value ref-dep))
3225                          (list (symbol-value id-dep))))
3226         (set ref-dep (list nil (symbol-value id-dep)))))
3227     header))
3228
3229 (defun gnus-build-sparse-threads ()
3230   (let ((headers gnus-newsgroup-headers)
3231         (mail-parse-charset gnus-newsgroup-charset)
3232         (gnus-summary-ignore-duplicates t)
3233         header references generation relations
3234         subject child end new-child date)
3235     ;; First we create an alist of generations/relations, where
3236     ;; generations is how much we trust the relation, and the relation
3237     ;; is parent/child.
3238     (gnus-message 7 "Making sparse threads...")
3239     (save-excursion
3240       (nnheader-set-temp-buffer " *gnus sparse threads*")
3241       (while (setq header (pop headers))
3242         (when (and (setq references (mail-header-references header))
3243                    (not (string= references "")))
3244           (insert references)
3245           (setq child (mail-header-id header)
3246                 subject (mail-header-subject header)
3247                 date (mail-header-date header)
3248                 generation 0)
3249           (while (search-backward ">" nil t)
3250             (setq end (1+ (point)))
3251             (when (search-backward "<" nil t)
3252               (setq new-child (buffer-substring (point) end))
3253               (push (list (incf generation)
3254                           child (setq child new-child)
3255                           subject date)
3256                     relations)))
3257           (when child
3258             (push (list (1+ generation) child nil subject) relations))
3259           (erase-buffer)))
3260       (kill-buffer (current-buffer)))
3261     ;; Sort over trustworthiness.
3262     (mapcar
3263      (lambda (relation)
3264        (when (gnus-dependencies-add-header
3265               (make-full-mail-header
3266                gnus-reffed-article-number
3267                (nth 3 relation) "" (or (nth 4 relation) "")
3268                (nth 1 relation)
3269                (or (nth 2 relation) "") 0 0 "")
3270               gnus-newsgroup-dependencies nil)
3271          (push gnus-reffed-article-number gnus-newsgroup-limit)
3272          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3273          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3274                gnus-newsgroup-reads)
3275          (decf gnus-reffed-article-number)))
3276      (sort relations 'car-less-than-car))
3277     (gnus-message 7 "Making sparse threads...done")))
3278
3279 (defun gnus-build-old-threads ()
3280   ;; Look at all the articles that refer back to old articles, and
3281   ;; fetch the headers for the articles that aren't there.  This will
3282   ;; build complete threads - if the roots haven't been expired by the
3283   ;; server, that is.
3284   (let ((mail-parse-charset gnus-newsgroup-charset)
3285         id heads)
3286     (mapatoms
3287      (lambda (refs)
3288        (when (not (car (symbol-value refs)))
3289          (setq heads (cdr (symbol-value refs)))
3290          (while heads
3291            (if (memq (mail-header-number (caar heads))
3292                      gnus-newsgroup-dormant)
3293                (setq heads (cdr heads))
3294              (setq id (symbol-name refs))
3295              (while (and (setq id (gnus-build-get-header id))
3296                          (not (car (gnus-id-to-thread id)))))
3297              (setq heads nil)))))
3298      gnus-newsgroup-dependencies)))
3299
3300 ;; This function has to be called with point after the article number
3301 ;; on the beginning of the line.
3302 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3303   (let ((eol (gnus-point-at-eol))
3304         (buffer (current-buffer))
3305         header)
3306
3307     ;; overview: [num subject from date id refs chars lines misc]
3308     (unless (eobp)
3309       (forward-char))
3310
3311     (setq header
3312           (make-full-mail-header
3313            number                               ; number
3314            (nnheader-nov-field)                 ; subject
3315            (nnheader-nov-field)                 ; from
3316            (nnheader-nov-field)                 ; date
3317            (nnheader-nov-read-message-id)       ; id
3318            (nnheader-nov-field)                 ; refs
3319            (nnheader-nov-read-integer)          ; chars
3320            (nnheader-nov-read-integer)          ; lines
3321            (unless (eobp)
3322              (nnheader-nov-field))              ; misc
3323            (nnheader-nov-parse-extra)))         ; extra
3324
3325     (when gnus-alter-header-function
3326       (funcall gnus-alter-header-function header))
3327     (gnus-dependencies-add-header header dependencies force-new)))
3328
3329 (defun gnus-build-get-header (id)
3330   ;; Look through the buffer of NOV lines and find the header to
3331   ;; ID.  Enter this line into the dependencies hash table, and return
3332   ;; the id of the parent article (if any).
3333   (let ((deps gnus-newsgroup-dependencies)
3334         found header)
3335     (prog1
3336         (save-excursion
3337           (set-buffer nntp-server-buffer)
3338           (let ((case-fold-search nil))
3339             (goto-char (point-min))
3340             (while (and (not found)
3341                         (search-forward id nil t))
3342               (beginning-of-line)
3343               (setq found (looking-at
3344                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3345                                    (regexp-quote id))))
3346               (or found (beginning-of-line 2)))
3347             (when found
3348               (beginning-of-line)
3349               (and
3350                (setq header (gnus-nov-parse-line
3351                              (read (current-buffer)) deps))
3352                (gnus-parent-id (mail-header-references header))))))
3353       (when header
3354         (let ((number (mail-header-number header)))
3355           (push number gnus-newsgroup-limit)
3356           (push header gnus-newsgroup-headers)
3357           (if (memq number gnus-newsgroup-unselected)
3358               (progn
3359                 (push number gnus-newsgroup-unreads)
3360                 (setq gnus-newsgroup-unselected
3361                       (delq number gnus-newsgroup-unselected)))
3362             (push number gnus-newsgroup-ancient)))))))
3363
3364 (defun gnus-build-all-threads ()
3365   "Read all the headers."
3366   (let ((gnus-summary-ignore-duplicates t)
3367         (mail-parse-charset gnus-newsgroup-charset)
3368         (dependencies gnus-newsgroup-dependencies)
3369         header article)
3370     (save-excursion
3371       (set-buffer nntp-server-buffer)
3372       (let ((case-fold-search nil))
3373         (goto-char (point-min))
3374         (while (not (eobp))
3375           (ignore-errors
3376             (setq article (read (current-buffer))
3377                   header (gnus-nov-parse-line article dependencies)))
3378           (when header
3379             (save-excursion
3380               (set-buffer gnus-summary-buffer)
3381               (push header gnus-newsgroup-headers)
3382               (if (memq (setq article (mail-header-number header))
3383                         gnus-newsgroup-unselected)
3384                   (progn
3385                     (push article gnus-newsgroup-unreads)
3386                     (setq gnus-newsgroup-unselected
3387                           (delq article gnus-newsgroup-unselected)))
3388                 (push article gnus-newsgroup-ancient)))
3389             (forward-line 1)))))))
3390
3391 (defun gnus-summary-update-article-line (article header)
3392   "Update the line for ARTICLE using HEADERS."
3393   (let* ((id (mail-header-id header))
3394          (thread (gnus-id-to-thread id)))
3395     (unless thread
3396       (error "Article in no thread"))
3397     ;; Update the thread.
3398     (setcar thread header)
3399     (gnus-summary-goto-subject article)
3400     (let* ((datal (gnus-data-find-list article))
3401            (data (car datal))
3402            (length (when (cdr datal)
3403                      (- (gnus-data-pos data)
3404                         (gnus-data-pos (cadr datal)))))
3405            (buffer-read-only nil)
3406            (level (gnus-summary-thread-level)))
3407       (gnus-delete-line)
3408       (gnus-summary-insert-line
3409        header level nil (gnus-article-mark article)
3410        (memq article gnus-newsgroup-replied)
3411        (memq article gnus-newsgroup-expirable)
3412        ;; Only insert the Subject string when it's different
3413        ;; from the previous Subject string.
3414        (if (and
3415             gnus-show-threads
3416             (gnus-subject-equal
3417              (condition-case ()
3418                  (mail-header-subject
3419                   (gnus-data-header
3420                    (cadr
3421                     (gnus-data-find-list
3422                      article
3423                      (gnus-data-list t)))))
3424                ;; Error on the side of excessive subjects.
3425                (error ""))
3426              (mail-header-subject header)))
3427            ""
3428          (mail-header-subject header))
3429        nil (cdr (assq article gnus-newsgroup-scored))
3430        (memq article gnus-newsgroup-processable))
3431       (when length
3432         (gnus-data-update-list
3433          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3434
3435 (defun gnus-summary-update-article (article &optional iheader)
3436   "Update ARTICLE in the summary buffer."
3437   (set-buffer gnus-summary-buffer)
3438   (let* ((header (gnus-summary-article-header article))
3439          (id (mail-header-id header))
3440          (data (gnus-data-find article))
3441          (thread (gnus-id-to-thread id))
3442          (references (mail-header-references header))
3443          (parent
3444           (gnus-id-to-thread
3445            (or (gnus-parent-id
3446                 (when (and references
3447                            (not (equal "" references)))
3448                   references))
3449                "none")))
3450          (buffer-read-only nil)
3451          (old (car thread)))
3452     (when thread
3453       (unless iheader
3454         (setcar thread nil)
3455         (when parent
3456           (delq thread parent)))
3457       (if (gnus-summary-insert-subject id header)
3458           ;; Set the (possibly) new article number in the data structure.
3459           (gnus-data-set-number data (gnus-id-to-article id))
3460         (setcar thread old)
3461         nil))))
3462
3463 (defun gnus-rebuild-thread (id &optional line)
3464   "Rebuild the thread containing ID.
3465 If LINE, insert the rebuilt thread starting on line LINE."
3466   (let ((buffer-read-only nil)
3467         old-pos current thread data)
3468     (if (not gnus-show-threads)
3469         (setq thread (list (car (gnus-id-to-thread id))))
3470       ;; Get the thread this article is part of.
3471       (setq thread (gnus-remove-thread id)))
3472     (setq old-pos (gnus-point-at-bol))
3473     (setq current (save-excursion
3474                     (and (zerop (forward-line -1))
3475                          (gnus-summary-article-number))))
3476     ;; If this is a gathered thread, we have to go some re-gathering.
3477     (when (stringp (car thread))
3478       (let ((subject (car thread))
3479             roots thr)
3480         (setq thread (cdr thread))
3481         (while thread
3482           (unless (memq (setq thr (gnus-id-to-thread
3483                                    (gnus-root-id
3484                                     (mail-header-id (caar thread)))))
3485                         roots)
3486             (push thr roots))
3487           (setq thread (cdr thread)))
3488         ;; We now have all (unique) roots.
3489         (if (= (length roots) 1)
3490             ;; All the loose roots are now one solid root.
3491             (setq thread (car roots))
3492           (setq thread (cons subject (gnus-sort-threads roots))))))
3493     (let (threads)
3494       ;; We then insert this thread into the summary buffer.
3495       (when line
3496         (goto-char (point-min))
3497         (forward-line (1- line)))
3498       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3499         (if gnus-show-threads
3500             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3501           (gnus-summary-prepare-unthreaded thread))
3502         (setq data (nreverse gnus-newsgroup-data))
3503         (setq threads gnus-newsgroup-threads))
3504       ;; We splice the new data into the data structure.
3505       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3506       ;;!!! then we want to insert at the beginning of the buffer.
3507       ;;!!! That happens to be true with Gnus now, but that may
3508       ;;!!! change in the future.  Perhaps.
3509       (gnus-data-enter-list
3510        (if line nil current) data (- (point) old-pos))
3511       (setq gnus-newsgroup-threads
3512             (nconc threads gnus-newsgroup-threads))
3513       (gnus-data-compute-positions))))
3514
3515 (defun gnus-number-to-header (number)
3516   "Return the header for article NUMBER."
3517   (let ((headers gnus-newsgroup-headers))
3518     (while (and headers
3519                 (not (= number (mail-header-number (car headers)))))
3520       (pop headers))
3521     (when headers
3522       (car headers))))
3523
3524 (defun gnus-parent-headers (in-headers &optional generation)
3525   "Return the headers of the GENERATIONeth parent of HEADERS."
3526   (unless generation
3527     (setq generation 1))
3528   (let ((parent t)
3529         (headers in-headers)
3530         references)
3531     (while (and parent
3532                 (not (zerop generation))
3533                 (setq references (mail-header-references headers)))
3534       (setq headers (if (and references
3535                              (setq parent (gnus-parent-id references)))
3536                         (car (gnus-id-to-thread parent))
3537                       nil))
3538       (decf generation))
3539     (and (not (eq headers in-headers))
3540          headers)))
3541
3542 (defun gnus-id-to-thread (id)
3543   "Return the (sub-)thread where ID appears."
3544   (gnus-gethash id gnus-newsgroup-dependencies))
3545
3546 (defun gnus-id-to-article (id)
3547   "Return the article number of ID."
3548   (let ((thread (gnus-id-to-thread id)))
3549     (when (and thread
3550                (car thread))
3551       (mail-header-number (car thread)))))
3552
3553 (defun gnus-id-to-header (id)
3554   "Return the article headers of ID."
3555   (car (gnus-id-to-thread id)))
3556
3557 (defun gnus-article-displayed-root-p (article)
3558   "Say whether ARTICLE is a root(ish) article."
3559   (let ((level (gnus-summary-thread-level article))
3560         (refs (mail-header-references  (gnus-summary-article-header article)))
3561         particle)
3562     (cond
3563      ((null level) nil)
3564      ((zerop level) t)
3565      ((null refs) t)
3566      ((null (gnus-parent-id refs)) t)
3567      ((and (= 1 level)
3568            (null (setq particle (gnus-id-to-article
3569                                  (gnus-parent-id refs))))
3570            (null (gnus-summary-thread-level particle)))))))
3571
3572 (defun gnus-root-id (id)
3573   "Return the id of the root of the thread where ID appears."
3574   (let (last-id prev)
3575     (while (and id (setq prev (car (gnus-id-to-thread id))))
3576       (setq last-id id
3577             id (gnus-parent-id (mail-header-references prev))))
3578     last-id))
3579
3580 (defun gnus-articles-in-thread (thread)
3581   "Return the list of articles in THREAD."
3582   (cons (mail-header-number (car thread))
3583         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3584
3585 (defun gnus-remove-thread (id &optional dont-remove)
3586   "Remove the thread that has ID in it."
3587   (let (headers thread last-id)
3588     ;; First go up in this thread until we find the root.
3589     (setq last-id (gnus-root-id id)
3590           headers (message-flatten-list (gnus-id-to-thread last-id)))
3591     ;; We have now found the real root of this thread.  It might have
3592     ;; been gathered into some loose thread, so we have to search
3593     ;; through the threads to find the thread we wanted.
3594     (let ((threads gnus-newsgroup-threads)
3595           sub)
3596       (while threads
3597         (setq sub (car threads))
3598         (if (stringp (car sub))
3599             ;; This is a gathered thread, so we look at the roots
3600             ;; below it to find whether this article is in this
3601             ;; gathered root.
3602             (progn
3603               (setq sub (cdr sub))
3604               (while sub
3605                 (when (member (caar sub) headers)
3606                   (setq thread (car threads)
3607                         threads nil
3608                         sub nil))
3609                 (setq sub (cdr sub))))
3610           ;; It's an ordinary thread, so we check it.
3611           (when (eq (car sub) (car headers))
3612             (setq thread sub
3613                   threads nil)))
3614         (setq threads (cdr threads)))
3615       ;; If this article is in no thread, then it's a root.
3616       (if thread
3617           (unless dont-remove
3618             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3619         (setq thread (gnus-id-to-thread last-id)))
3620       (when thread
3621         (prog1
3622             thread                      ; We return this thread.
3623           (unless dont-remove
3624             (if (stringp (car thread))
3625                 (progn
3626                   ;; If we use dummy roots, then we have to remove the
3627                   ;; dummy root as well.
3628                   (when (eq gnus-summary-make-false-root 'dummy)
3629                     ;; We go to the dummy root by going to
3630                     ;; the first sub-"thread", and then one line up.
3631                     (gnus-summary-goto-article
3632                      (mail-header-number (caadr thread)))
3633                     (forward-line -1)
3634                     (gnus-delete-line)
3635                     (gnus-data-compute-positions))
3636                   (setq thread (cdr thread))
3637                   (while thread
3638                     (gnus-remove-thread-1 (car thread))
3639                     (setq thread (cdr thread))))
3640               (gnus-remove-thread-1 thread))))))))
3641
3642 (defun gnus-remove-thread-1 (thread)
3643   "Remove the thread THREAD recursively."
3644   (let ((number (mail-header-number (pop thread)))
3645         d)
3646     (setq thread (reverse thread))
3647     (while thread
3648       (gnus-remove-thread-1 (pop thread)))
3649     (when (setq d (gnus-data-find number))
3650       (goto-char (gnus-data-pos d))
3651       (gnus-summary-show-thread)
3652       (gnus-data-remove
3653        number
3654        (- (gnus-point-at-bol)
3655           (prog1
3656               (1+ (gnus-point-at-eol))
3657             (gnus-delete-line)))))))
3658
3659 (defun gnus-sort-threads (threads)
3660   "Sort THREADS."
3661   (if (not gnus-thread-sort-functions)
3662       threads
3663     (gnus-message 8 "Sorting threads...")
3664     (prog1
3665         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3666       (gnus-message 8 "Sorting threads...done"))))
3667
3668 (defun gnus-sort-articles (articles)
3669   "Sort ARTICLES."
3670   (when gnus-article-sort-functions
3671     (gnus-message 7 "Sorting articles...")
3672     (prog1
3673         (setq gnus-newsgroup-headers
3674               (sort articles (gnus-make-sort-function
3675                               gnus-article-sort-functions)))
3676       (gnus-message 7 "Sorting articles...done"))))
3677
3678 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3679 (defmacro gnus-thread-header (thread)
3680   ;; Return header of first article in THREAD.
3681   ;; Note that THREAD must never, ever be anything else than a variable -
3682   ;; using some other form will lead to serious barfage.
3683   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3684   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3685   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3686         (vector thread) 2))
3687
3688 (defsubst gnus-article-sort-by-number (h1 h2)
3689   "Sort articles by article number."
3690   (< (mail-header-number h1)
3691      (mail-header-number h2)))
3692
3693 (defun gnus-thread-sort-by-number (h1 h2)
3694   "Sort threads by root article number."
3695   (gnus-article-sort-by-number
3696    (gnus-thread-header h1) (gnus-thread-header h2)))
3697
3698 (defsubst gnus-article-sort-by-lines (h1 h2)
3699   "Sort articles by article Lines header."
3700   (< (mail-header-lines h1)
3701      (mail-header-lines h2)))
3702
3703 (defun gnus-thread-sort-by-lines (h1 h2)
3704   "Sort threads by root article Lines header."
3705   (gnus-article-sort-by-lines
3706    (gnus-thread-header h1) (gnus-thread-header h2)))
3707
3708 (defsubst gnus-article-sort-by-chars (h1 h2)
3709   "Sort articles by octet length."
3710   (< (mail-header-chars h1)
3711      (mail-header-chars h2)))
3712
3713 (defun gnus-thread-sort-by-chars (h1 h2)
3714   "Sort threads by root article octet length."
3715   (gnus-article-sort-by-chars
3716    (gnus-thread-header h1) (gnus-thread-header h2)))
3717
3718 (defsubst gnus-article-sort-by-author (h1 h2)
3719   "Sort articles by root author."
3720   (string-lessp
3721    (let ((addr (car (mime-entity-read-field h1 'From))))
3722      (or (std11-full-name-string addr)
3723          (std11-address-string addr)
3724          ""))
3725    (let ((addr (car (mime-entity-read-field h2 'From))))
3726      (or (std11-full-name-string addr)
3727          (std11-address-string addr)
3728          ""))
3729    ))
3730
3731 (defun gnus-thread-sort-by-author (h1 h2)
3732   "Sort threads by root author."
3733   (gnus-article-sort-by-author
3734    (gnus-thread-header h1)  (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-subject (h1 h2)
3737   "Sort articles by root subject."
3738   (string-lessp
3739    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3740    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3741
3742 (defun gnus-thread-sort-by-subject (h1 h2)
3743   "Sort threads by root subject."
3744   (gnus-article-sort-by-subject
3745    (gnus-thread-header h1) (gnus-thread-header h2)))
3746
3747 (defsubst gnus-article-sort-by-date (h1 h2)
3748   "Sort articles by root article date."
3749   (time-less-p
3750    (gnus-date-get-time (mail-header-date h1))
3751    (gnus-date-get-time (mail-header-date h2))))
3752
3753 (defun gnus-thread-sort-by-date (h1 h2)
3754   "Sort threads by root article date."
3755   (gnus-article-sort-by-date
3756    (gnus-thread-header h1) (gnus-thread-header h2)))
3757
3758 (defsubst gnus-article-sort-by-score (h1 h2)
3759   "Sort articles by root article score.
3760 Unscored articles will be counted as having a score of zero."
3761   (> (or (cdr (assq (mail-header-number h1)
3762                     gnus-newsgroup-scored))
3763          gnus-summary-default-score 0)
3764      (or (cdr (assq (mail-header-number h2)
3765                     gnus-newsgroup-scored))
3766          gnus-summary-default-score 0)))
3767
3768 (defun gnus-thread-sort-by-score (h1 h2)
3769   "Sort threads by root article score."
3770   (gnus-article-sort-by-score
3771    (gnus-thread-header h1) (gnus-thread-header h2)))
3772
3773 (defun gnus-thread-sort-by-total-score (h1 h2)
3774   "Sort threads by the sum of all scores in the thread.
3775 Unscored articles will be counted as having a score of zero."
3776   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3777
3778 (defun gnus-thread-total-score (thread)
3779   ;; This function find the total score of THREAD.
3780   (cond ((null thread)
3781          0)
3782         ((consp thread)
3783          (if (stringp (car thread))
3784              (apply gnus-thread-score-function 0
3785                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3786            (gnus-thread-total-score-1 thread)))
3787         (t
3788          (gnus-thread-total-score-1 (list thread)))))
3789
3790 (defun gnus-thread-total-score-1 (root)
3791   ;; This function find the total score of the thread below ROOT.
3792   (setq root (car root))
3793   (apply gnus-thread-score-function
3794          (or (append
3795               (mapcar 'gnus-thread-total-score
3796                       (cdr (gnus-id-to-thread (mail-header-id root))))
3797               (when (> (mail-header-number root) 0)
3798                 (list (or (cdr (assq (mail-header-number root)
3799                                      gnus-newsgroup-scored))
3800                           gnus-summary-default-score 0))))
3801              (list gnus-summary-default-score)
3802              '(0))))
3803
3804 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3805 (defvar gnus-tmp-prev-subject nil)
3806 (defvar gnus-tmp-false-parent nil)
3807 (defvar gnus-tmp-root-expunged nil)
3808 (defvar gnus-tmp-dummy-line nil)
3809
3810 (defvar gnus-tmp-header)
3811 (defun gnus-extra-header (type &optional header)
3812   "Return the extra header of TYPE."
3813   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3814       ""))
3815
3816 (defun gnus-summary-prepare-threads (threads)
3817   "Prepare summary buffer from THREADS and indentation LEVEL.
3818 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3819 or a straight list of headers."
3820   (gnus-message 7 "Generating summary...")
3821
3822   (setq gnus-newsgroup-threads threads)
3823   (beginning-of-line)
3824
3825   (let ((gnus-tmp-level 0)
3826         (default-score (or gnus-summary-default-score 0))
3827         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3828         thread number subject stack state gnus-tmp-gathered beg-match
3829         new-roots gnus-tmp-new-adopts thread-end
3830         gnus-tmp-header gnus-tmp-unread
3831         gnus-tmp-replied gnus-tmp-subject-or-nil
3832         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3833         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3834         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3835
3836     (setq gnus-tmp-prev-subject nil)
3837
3838     (if (vectorp (car threads))
3839         ;; If this is a straight (sic) list of headers, then a
3840         ;; threaded summary display isn't required, so we just create
3841         ;; an unthreaded one.
3842         (gnus-summary-prepare-unthreaded threads)
3843
3844       ;; Do the threaded display.
3845
3846       (while (or threads stack gnus-tmp-new-adopts new-roots)
3847
3848         (if (and (= gnus-tmp-level 0)
3849                  (or (not stack)
3850                      (= (caar stack) 0))
3851                  (not gnus-tmp-false-parent)
3852                  (or gnus-tmp-new-adopts new-roots))
3853             (if gnus-tmp-new-adopts
3854                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3855                       thread (list (car gnus-tmp-new-adopts))
3856                       gnus-tmp-header (caar thread)
3857                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3858               (when new-roots
3859                 (setq thread (list (car new-roots))
3860                       gnus-tmp-header (caar thread)
3861                       new-roots (cdr new-roots))))
3862
3863           (if threads
3864               ;; If there are some threads, we do them before the
3865               ;; threads on the stack.
3866               (setq thread threads
3867                     gnus-tmp-header (caar thread))
3868             ;; There were no current threads, so we pop something off
3869             ;; the stack.
3870             (setq state (car stack)
3871                   gnus-tmp-level (car state)
3872                   thread (cdr state)
3873                   stack (cdr stack)
3874                   gnus-tmp-header (caar thread))))
3875
3876         (setq gnus-tmp-false-parent nil)
3877         (setq gnus-tmp-root-expunged nil)
3878         (setq thread-end nil)
3879
3880         (if (stringp gnus-tmp-header)
3881             ;; The header is a dummy root.
3882             (cond
3883              ((eq gnus-summary-make-false-root 'adopt)
3884               ;; We let the first article adopt the rest.
3885               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3886                                                (cddar thread)))
3887               (setq gnus-tmp-gathered
3888                     (nconc (mapcar
3889                             (lambda (h) (mail-header-number (car h)))
3890                             (cddar thread))
3891                            gnus-tmp-gathered))
3892               (setq thread (cons (list (caar thread)
3893                                        (cadar thread))
3894                                  (cdr thread)))
3895               (setq gnus-tmp-level -1
3896                     gnus-tmp-false-parent t))
3897              ((eq gnus-summary-make-false-root 'empty)
3898               ;; We print adopted articles with empty subject fields.
3899               (setq gnus-tmp-gathered
3900                     (nconc (mapcar
3901                             (lambda (h) (mail-header-number (car h)))
3902                             (cddar thread))
3903                            gnus-tmp-gathered))
3904               (setq gnus-tmp-level -1))
3905              ((eq gnus-summary-make-false-root 'dummy)
3906               ;; We remember that we probably want to output a dummy
3907               ;; root.
3908               (setq gnus-tmp-dummy-line gnus-tmp-header)
3909               (setq gnus-tmp-prev-subject gnus-tmp-header))
3910              (t
3911               ;; We do not make a root for the gathered
3912               ;; sub-threads at all.
3913               (setq gnus-tmp-level -1)))
3914
3915           (setq number (mail-header-number gnus-tmp-header)
3916                 subject (mail-header-subject gnus-tmp-header))
3917
3918           (cond
3919            ;; If the thread has changed subject, we might want to make
3920            ;; this subthread into a root.
3921            ((and (null gnus-thread-ignore-subject)
3922                  (not (zerop gnus-tmp-level))
3923                  gnus-tmp-prev-subject
3924                  (not (inline
3925                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3926             (setq new-roots (nconc new-roots (list (car thread)))
3927                   thread-end t
3928                   gnus-tmp-header nil))
3929            ;; If the article lies outside the current limit,
3930            ;; then we do not display it.
3931            ((not (memq number gnus-newsgroup-limit))
3932             (setq gnus-tmp-gathered
3933                   (nconc (mapcar
3934                           (lambda (h) (mail-header-number (car h)))
3935                           (cdar thread))
3936                          gnus-tmp-gathered))
3937             (setq gnus-tmp-new-adopts (if (cdar thread)
3938                                           (append gnus-tmp-new-adopts
3939                                                   (cdar thread))
3940                                         gnus-tmp-new-adopts)
3941                   thread-end t
3942                   gnus-tmp-header nil)
3943             (when (zerop gnus-tmp-level)
3944               (setq gnus-tmp-root-expunged t)))
3945            ;; Perhaps this article is to be marked as read?
3946            ((and gnus-summary-mark-below
3947                  (< (or (cdr (assq number gnus-newsgroup-scored))
3948                         default-score)
3949                     gnus-summary-mark-below)
3950                  ;; Don't touch sparse articles.
3951                  (not (gnus-summary-article-sparse-p number))
3952                  (not (gnus-summary-article-ancient-p number)))
3953             (setq gnus-newsgroup-unreads
3954                   (delq number gnus-newsgroup-unreads))
3955             (if gnus-newsgroup-auto-expire
3956                 (push number gnus-newsgroup-expirable)
3957               (push (cons number gnus-low-score-mark)
3958                     gnus-newsgroup-reads))))
3959
3960           (when gnus-tmp-header
3961             ;; We may have an old dummy line to output before this
3962             ;; article.
3963             (when (and gnus-tmp-dummy-line
3964                        (gnus-subject-equal
3965                         gnus-tmp-dummy-line
3966                         (mail-header-subject gnus-tmp-header)))
3967               (gnus-summary-insert-dummy-line
3968                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3969               (setq gnus-tmp-dummy-line nil))
3970
3971             ;; Compute the mark.
3972             (setq gnus-tmp-unread (gnus-article-mark number))
3973
3974             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3975                                   gnus-tmp-header gnus-tmp-level)
3976                   gnus-newsgroup-data)
3977
3978             ;; Actually insert the line.
3979             (setq
3980              gnus-tmp-subject-or-nil
3981              (cond
3982               ((and gnus-thread-ignore-subject
3983                     gnus-tmp-prev-subject
3984                     (not (inline (gnus-subject-equal
3985                                   gnus-tmp-prev-subject subject))))
3986                subject)
3987               ((zerop gnus-tmp-level)
3988                (if (and (eq gnus-summary-make-false-root 'empty)
3989                         (memq number gnus-tmp-gathered)
3990                         gnus-tmp-prev-subject
3991                         (inline (gnus-subject-equal
3992                                  gnus-tmp-prev-subject subject)))
3993                    gnus-summary-same-subject
3994                  subject))
3995               (t gnus-summary-same-subject)))
3996             (if (and (eq gnus-summary-make-false-root 'adopt)
3997                      (= gnus-tmp-level 1)
3998                      (memq number gnus-tmp-gathered))
3999                 (setq gnus-tmp-opening-bracket ?\<
4000                       gnus-tmp-closing-bracket ?\>)
4001               (setq gnus-tmp-opening-bracket ?\[
4002                     gnus-tmp-closing-bracket ?\]))
4003             (setq
4004              gnus-tmp-indentation
4005              (aref gnus-thread-indent-array gnus-tmp-level)
4006              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4007              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4008                                 gnus-summary-default-score 0)
4009              gnus-tmp-score-char
4010              (if (or (null gnus-summary-default-score)
4011                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4012                          gnus-summary-zcore-fuzz))
4013                  ?  ;Whitespace
4014                (if (< gnus-tmp-score gnus-summary-default-score)
4015                    gnus-score-below-mark gnus-score-over-mark))
4016              gnus-tmp-replied
4017              (cond ((memq number gnus-newsgroup-processable)
4018                     gnus-process-mark)
4019                    ((memq number gnus-newsgroup-cached)
4020                     gnus-cached-mark)
4021                    ((memq number gnus-newsgroup-replied)
4022                     gnus-replied-mark)
4023                    ((memq number gnus-newsgroup-saved)
4024                     gnus-saved-mark)
4025                    (t gnus-unread-mark))
4026              gnus-tmp-from (mail-header-from gnus-tmp-header)
4027              gnus-tmp-name
4028              (cond
4029               ((string-match "<[^>]+> *$" gnus-tmp-from)
4030                (setq beg-match (match-beginning 0))
4031                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4032                         (substring gnus-tmp-from (1+ (match-beginning 0))
4033                                    (1- (match-end 0))))
4034                    (substring gnus-tmp-from 0 beg-match)))
4035               ((string-match "(.+)" gnus-tmp-from)
4036                (substring gnus-tmp-from
4037                           (1+ (match-beginning 0)) (1- (match-end 0))))
4038               (t gnus-tmp-from)))
4039             (when (string= gnus-tmp-name "")
4040               (setq gnus-tmp-name gnus-tmp-from))
4041             (unless (numberp gnus-tmp-lines)
4042               (setq gnus-tmp-lines 0))
4043             (gnus-put-text-property
4044              (point)
4045              (progn (eval gnus-summary-line-format-spec) (point))
4046              'gnus-number number)
4047             (when gnus-visual-p
4048               (forward-line -1)
4049               (gnus-run-hooks 'gnus-summary-update-hook)
4050               (forward-line 1))
4051
4052             (setq gnus-tmp-prev-subject subject)))
4053
4054         (when (nth 1 thread)
4055           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4056         (incf gnus-tmp-level)
4057         (setq threads (if thread-end nil (cdar thread)))
4058         (unless threads
4059           (setq gnus-tmp-level 0)))))
4060   (gnus-message 7 "Generating summary...done"))
4061
4062 (defun gnus-summary-prepare-unthreaded (headers)
4063   "Generate an unthreaded summary buffer based on HEADERS."
4064   (let (header number mark)
4065
4066     (beginning-of-line)
4067
4068     (while headers
4069       ;; We may have to root out some bad articles...
4070       (when (memq (setq number (mail-header-number
4071                                 (setq header (pop headers))))
4072                   gnus-newsgroup-limit)
4073         ;; Mark article as read when it has a low score.
4074         (when (and gnus-summary-mark-below
4075                    (< (or (cdr (assq number gnus-newsgroup-scored))
4076                           gnus-summary-default-score 0)
4077                       gnus-summary-mark-below)
4078                    (not (gnus-summary-article-ancient-p number)))
4079           (setq gnus-newsgroup-unreads
4080                 (delq number gnus-newsgroup-unreads))
4081           (if gnus-newsgroup-auto-expire
4082               (push number gnus-newsgroup-expirable)
4083             (push (cons number gnus-low-score-mark)
4084                   gnus-newsgroup-reads)))
4085
4086         (setq mark (gnus-article-mark number))
4087         (push (gnus-data-make number mark (1+ (point)) header 0)
4088               gnus-newsgroup-data)
4089         (gnus-summary-insert-line
4090          header 0 number
4091          mark (memq number gnus-newsgroup-replied)
4092          (memq number gnus-newsgroup-expirable)
4093          (mail-header-subject header) nil
4094          (cdr (assq number gnus-newsgroup-scored))
4095          (memq number gnus-newsgroup-processable))))))
4096
4097 (defun gnus-summary-remove-list-identifiers ()
4098   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4099   (let ((regexp (if (stringp gnus-list-identifiers)
4100                     gnus-list-identifiers
4101                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4102     (dolist (header gnus-newsgroup-headers)
4103       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4104                           (mail-header-subject header))
4105         (mail-header-set-subject
4106          header (concat (substring (mail-header-subject header)
4107                                    0 (match-beginning 2))
4108                         (substring (mail-header-subject header)
4109                                    (match-end 2))))))))
4110
4111 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4112   "Select newsgroup GROUP.
4113 If READ-ALL is non-nil, all articles in the group are selected.
4114 If SELECT-ARTICLES, only select those articles from GROUP."
4115   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4116          ;;!!! Dirty hack; should be removed.
4117          (gnus-summary-ignore-duplicates
4118           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4119               t
4120             gnus-summary-ignore-duplicates))
4121          (info (nth 2 entry))
4122          articles fetched-articles cached)
4123
4124     (unless (gnus-check-server
4125              (setq gnus-current-select-method
4126                    (gnus-find-method-for-group group)))
4127       (error "Couldn't open server"))
4128
4129     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4130         (gnus-activate-group group)     ; Or we can activate it...
4131         (progn                          ; Or we bug out.
4132           (when (equal major-mode 'gnus-summary-mode)
4133             (kill-buffer (current-buffer)))
4134           (error "Couldn't request group %s: %s"
4135                  group (gnus-status-message group))))
4136
4137     (unless (gnus-request-group group t)
4138       (when (equal major-mode 'gnus-summary-mode)
4139         (kill-buffer (current-buffer)))
4140       (error "Couldn't request group %s: %s"
4141              group (gnus-status-message group)))
4142
4143     (setq gnus-newsgroup-name group)
4144     (setq gnus-newsgroup-unselected nil)
4145     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4146     (gnus-summary-setup-default-charset)
4147
4148     ;; Adjust and set lists of article marks.
4149     (when info
4150       (gnus-adjust-marked-articles info))
4151
4152     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4153     (setq cached
4154           (if (gnus-virtual-group-p group)
4155               gnus-newsgroup-cached
4156             (gnus-cache-articles-in-group group)))
4157
4158     (setq gnus-newsgroup-unreads
4159           (gnus-set-difference
4160            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4161            gnus-newsgroup-dormant))
4162
4163     (setq gnus-newsgroup-processable nil)
4164
4165     (gnus-update-read-articles group gnus-newsgroup-unreads)
4166
4167     (if (setq articles select-articles)
4168         (setq gnus-newsgroup-unselected
4169               (gnus-sorted-intersection
4170                gnus-newsgroup-unreads
4171                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4172       (setq articles (gnus-articles-to-read group read-all)))
4173
4174     (cond
4175      ((null articles)
4176       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4177       'quit)
4178      ((eq articles 0) nil)
4179      (t
4180       ;; Init the dependencies hash table.
4181       (setq gnus-newsgroup-dependencies
4182             (gnus-make-hashtable (length articles)))
4183       (gnus-set-global-variables)
4184       ;; Retrieve the headers and read them in.
4185       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4186       (setq gnus-newsgroup-headers
4187             (gnus-retrieve-parsed-headers
4188              articles gnus-newsgroup-name
4189              ;; We might want to fetch old headers, but
4190              ;; not if there is only 1 article.
4191              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4192                            (not (numberp gnus-fetch-old-headers)))
4193                       (> (length articles) 1))
4194                   gnus-fetch-old-headers)))
4195       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4196
4197       ;; Suppress duplicates?
4198       (when gnus-suppress-duplicates
4199         (gnus-dup-suppress-articles))
4200
4201       ;; Set the initial limit.
4202       (setq gnus-newsgroup-limit (copy-sequence articles))
4203       ;; Remove canceled articles from the list of unread articles.
4204       (setq gnus-newsgroup-unreads
4205             (gnus-set-sorted-intersection
4206              gnus-newsgroup-unreads
4207              (setq fetched-articles
4208                    (mapcar (lambda (headers) (mail-header-number headers))
4209                            gnus-newsgroup-headers))))
4210       ;; Removed marked articles that do not exist.
4211       (gnus-update-missing-marks
4212        (gnus-sorted-complement fetched-articles articles))
4213
4214       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4215       (when cached
4216         (setq gnus-newsgroup-cached cached))
4217
4218       ;; We might want to build some more threads first.
4219       (when (and gnus-fetch-old-headers
4220                  (eq gnus-headers-retrieved-by 'nov))
4221         (if (eq gnus-fetch-old-headers 'invisible)
4222             (gnus-build-all-threads)
4223           (gnus-build-old-threads)))
4224       ;; Let the Gnus agent mark articles as read.
4225       (when gnus-agent
4226         (gnus-agent-get-undownloaded-list))
4227       ;; Remove list identifiers from subject
4228       (when gnus-list-identifiers
4229         (gnus-summary-remove-list-identifiers))
4230       ;; Check whether auto-expire is to be done in this group.
4231       (setq gnus-newsgroup-auto-expire
4232             (gnus-group-auto-expirable-p group))
4233       ;; Set up the article buffer now, if necessary.
4234       (unless gnus-single-article-buffer
4235         (gnus-article-setup-buffer))
4236       ;; First and last article in this newsgroup.
4237       (when gnus-newsgroup-headers
4238         (setq gnus-newsgroup-begin
4239               (mail-header-number (car gnus-newsgroup-headers))
4240               gnus-newsgroup-end
4241               (mail-header-number
4242                (gnus-last-element gnus-newsgroup-headers))))
4243       ;; GROUP is successfully selected.
4244       (or gnus-newsgroup-headers t)))))
4245
4246 (defun gnus-articles-to-read (group &optional read-all)
4247   ;; Find out what articles the user wants to read.
4248   (let* ((articles
4249           ;; Select all articles if `read-all' is non-nil, or if there
4250           ;; are no unread articles.
4251           (if (or read-all
4252                   (and (zerop (length gnus-newsgroup-marked))
4253                        (zerop (length gnus-newsgroup-unreads)))
4254                   (eq (gnus-group-find-parameter group 'display)
4255                       'all))
4256               (or
4257                (gnus-uncompress-range (gnus-active group))
4258                (gnus-cache-articles-in-group group))
4259             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4260                           (copy-sequence gnus-newsgroup-unreads))
4261                   '<)))
4262          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4263          (scored (length scored-list))
4264          (number (length articles))
4265          (marked (+ (length gnus-newsgroup-marked)
4266                     (length gnus-newsgroup-dormant)))
4267          (select
4268           (cond
4269            ((numberp read-all)
4270             read-all)
4271            (t
4272             (condition-case ()
4273                 (cond
4274                  ((and (or (<= scored marked) (= scored number))
4275                        (natnump gnus-large-newsgroup)
4276                        (> number gnus-large-newsgroup))
4277                   (let* ((cursor-in-echo-area nil)
4278                          (input (read-from-minibuffer
4279                                  (format
4280                                   "How many articles from %s (max %d): "
4281                                   (gnus-limit-string gnus-newsgroup-name 35)
4282                                   number)
4283                                  (cons (number-to-string gnus-large-newsgroup)
4284                                        0))))
4285                     (if (string-match "^[ \t]*$" input)
4286                         number
4287                       input)))
4288                  ((and (> scored marked) (< scored number)
4289                        (> (- scored number) 20))
4290                   (let ((input
4291                          (read-string
4292                           (format "%s %s (%d scored, %d total): "
4293                                   "How many articles from"
4294                                   group scored number))))
4295                     (if (string-match "^[ \t]*$" input)
4296                         number input)))
4297                  (t number))
4298               (quit nil))))))
4299     (setq select (if (stringp select) (string-to-number select) select))
4300     (if (or (null select) (zerop select))
4301         select
4302       (if (and (not (zerop scored)) (<= (abs select) scored))
4303           (progn
4304             (setq articles (sort scored-list '<))
4305             (setq number (length articles)))
4306         (setq articles (copy-sequence articles)))
4307
4308       (when (< (abs select) number)
4309         (if (< select 0)
4310             ;; Select the N oldest articles.
4311             (setcdr (nthcdr (1- (abs select)) articles) nil)
4312           ;; Select the N most recent articles.
4313           (setq articles (nthcdr (- number select) articles))))
4314       (setq gnus-newsgroup-unselected
4315             (gnus-sorted-intersection
4316              gnus-newsgroup-unreads
4317              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4318       (when gnus-alter-articles-to-read-function
4319         (setq gnus-newsgroup-unreads
4320               (sort 
4321                (funcall gnus-alter-articles-to-read-function
4322                         gnus-newsgroup-name gnus-newsgroup-unreads)
4323                '<)))
4324       articles)))
4325
4326 (defun gnus-killed-articles (killed articles)
4327   (let (out)
4328     (while articles
4329       (when (inline (gnus-member-of-range (car articles) killed))
4330         (push (car articles) out))
4331       (setq articles (cdr articles)))
4332     out))
4333
4334 (defun gnus-uncompress-marks (marks)
4335   "Uncompress the mark ranges in MARKS."
4336   (let ((uncompressed '(score bookmark))
4337         out)
4338     (while marks
4339       (if (memq (caar marks) uncompressed)
4340           (push (car marks) out)
4341         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4342       (setq marks (cdr marks)))
4343     out))
4344
4345 (defun gnus-adjust-marked-articles (info)
4346   "Set all article lists and remove all marks that are no longer valid."
4347   (let* ((marked-lists (gnus-info-marks info))
4348          (active (gnus-active (gnus-info-group info)))
4349          (min (car active))
4350          (max (cdr active))
4351          (types gnus-article-mark-lists)
4352          (uncompressed '(score bookmark killed))
4353          marks var articles article mark)
4354
4355     (while marked-lists
4356       (setq marks (pop marked-lists))
4357       (set (setq var (intern (format "gnus-newsgroup-%s"
4358                                      (car (rassq (setq mark (car marks))
4359                                                  types)))))
4360            (if (memq (car marks) uncompressed) (cdr marks)
4361              (gnus-uncompress-range (cdr marks))))
4362
4363       (setq articles (symbol-value var))
4364
4365       ;; All articles have to be subsets of the active articles.
4366       (cond
4367        ;; Adjust "simple" lists.
4368        ((memq mark '(tick dormant expire reply save))
4369         (while articles
4370           (when (or (< (setq article (pop articles)) min) (> article max))
4371             (set var (delq article (symbol-value var))))))
4372        ;; Adjust assocs.
4373        ((memq mark uncompressed)
4374         (when (not (listp (cdr (symbol-value var))))
4375           (set var (list (symbol-value var))))
4376         (when (not (listp (cdr articles)))
4377           (setq articles (list articles)))
4378         (while articles
4379           (when (or (not (consp (setq article (pop articles))))
4380                     (< (car article) min)
4381                     (> (car article) max))
4382             (set var (delq article (symbol-value var))))))))))
4383
4384 (defun gnus-update-missing-marks (missing)
4385   "Go through the list of MISSING articles and remove them from the mark lists."
4386   (when missing
4387     (let ((types gnus-article-mark-lists)
4388           var m)
4389       ;; Go through all types.
4390       (while types
4391         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4392         (when (symbol-value var)
4393           ;; This list has articles.  So we delete all missing articles
4394           ;; from it.
4395           (setq m missing)
4396           (while m
4397             (set var (delq (pop m) (symbol-value var)))))))))
4398
4399 (defun gnus-update-marks ()
4400   "Enter the various lists of marked articles into the newsgroup info list."
4401   (let ((types gnus-article-mark-lists)
4402         (info (gnus-get-info gnus-newsgroup-name))
4403         (uncompressed '(score bookmark killed))
4404         type list newmarked symbol delta-marks)
4405     (when info
4406       ;; Add all marks lists to the list of marks lists.
4407       (while (setq type (pop types))
4408         (setq list (symbol-value
4409                     (setq symbol
4410                           (intern (format "gnus-newsgroup-%s"
4411                                           (car type))))))
4412
4413         (when list
4414           ;; Get rid of the entries of the articles that have the
4415           ;; default score.
4416           (when (and (eq (cdr type) 'score)
4417                      gnus-save-score
4418                      list)
4419             (let* ((arts list)
4420                    (prev (cons nil list))
4421                    (all prev))
4422               (while arts
4423                 (if (or (not (consp (car arts)))
4424                         (= (cdar arts) gnus-summary-default-score))
4425                     (setcdr prev (cdr arts))
4426                   (setq prev arts))
4427                 (setq arts (cdr arts)))
4428               (setq list (cdr all)))))
4429
4430         (unless (memq (cdr type) uncompressed)
4431           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4432        
4433         (when (gnus-check-backend-function
4434                'request-set-mark gnus-newsgroup-name)
4435           ;; uncompressed:s are not proper flags (they are cons cells)
4436           ;; cache is a internal gnus flag
4437           (unless (memq (cdr type) (cons 'cache uncompressed))
4438             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4439                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4440                    (add (gnus-remove-from-range
4441                          (gnus-copy-sequence list) old)))
4442               (when add
4443                 (push (list add 'add (list (cdr type))) delta-marks))
4444               (when del
4445                 (push (list del 'del (list (cdr type))) delta-marks)))))
4446           
4447         (when list
4448           (push (cons (cdr type) list) newmarked)))
4449
4450       (when delta-marks
4451         (unless (gnus-check-group gnus-newsgroup-name)
4452           (error "Can't open server for %s" gnus-newsgroup-name))
4453         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4454           
4455       ;; Enter these new marks into the info of the group.
4456       (if (nthcdr 3 info)
4457           (setcar (nthcdr 3 info) newmarked)
4458         ;; Add the marks lists to the end of the info.
4459         (when newmarked
4460           (setcdr (nthcdr 2 info) (list newmarked))))
4461
4462       ;; Cut off the end of the info if there's nothing else there.
4463       (let ((i 5))
4464         (while (and (> i 2)
4465                     (not (nth i info)))
4466           (when (nthcdr (decf i) info)
4467             (setcdr (nthcdr i info) nil)))))))
4468
4469 (defun gnus-set-mode-line (where)
4470   "This function sets the mode line of the article or summary buffers.
4471 If WHERE is `summary', the summary mode line format will be used."
4472   ;; Is this mode line one we keep updated?
4473   (when (and (memq where gnus-updated-mode-lines)
4474              (symbol-value
4475               (intern (format "gnus-%s-mode-line-format-spec" where))))
4476     (let (mode-string)
4477       (save-excursion
4478         ;; We evaluate this in the summary buffer since these
4479         ;; variables are buffer-local to that buffer.
4480         (set-buffer gnus-summary-buffer)
4481         ;; We bind all these variables that are used in the `eval' form
4482         ;; below.
4483         (let* ((mformat (symbol-value
4484                          (intern
4485                           (format "gnus-%s-mode-line-format-spec" where))))
4486                (gnus-tmp-group-name gnus-newsgroup-name)
4487                (gnus-tmp-article-number (or gnus-current-article 0))
4488                (gnus-tmp-unread gnus-newsgroup-unreads)
4489                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4490                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4491                (gnus-tmp-unread-and-unselected
4492                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4493                             (zerop gnus-tmp-unselected))
4494                        "")
4495                       ((zerop gnus-tmp-unselected)
4496                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4497                       (t (format "{%d(+%d) more}"
4498                                  gnus-tmp-unread-and-unticked
4499                                  gnus-tmp-unselected))))
4500                (gnus-tmp-subject
4501                 (if (and gnus-current-headers
4502                          (vectorp gnus-current-headers))
4503                     (gnus-mode-string-quote
4504                      (mail-header-subject gnus-current-headers))
4505                   ""))
4506                bufname-length max-len
4507                gnus-tmp-header);; passed as argument to any user-format-funcs
4508           (setq mode-string (eval mformat))
4509           (setq bufname-length (if (string-match "%b" mode-string)
4510                                    (- (length
4511                                        (buffer-name
4512                                         (if (eq where 'summary)
4513                                             nil
4514                                           (get-buffer gnus-article-buffer))))
4515                                       2)
4516                                  0))
4517           (setq max-len (max 4 (if gnus-mode-non-string-length
4518                                    (- (window-width)
4519                                       gnus-mode-non-string-length
4520                                       bufname-length)
4521                                  (length mode-string))))
4522           ;; We might have to chop a bit of the string off...
4523           (when (> (length mode-string) max-len)
4524             (setq mode-string
4525                   (concat (gnus-truncate-string mode-string (- max-len 3))
4526                           "...")))
4527           ;; Pad the mode string a bit.
4528           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4529       ;; Update the mode line.
4530       (setq mode-line-buffer-identification
4531             (gnus-mode-line-buffer-identification (list mode-string)))
4532       (set-buffer-modified-p t))))
4533
4534 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4535   "Go through the HEADERS list and add all Xrefs to a hash table.
4536 The resulting hash table is returned, or nil if no Xrefs were found."
4537   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4538          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4539          (xref-hashtb (gnus-make-hashtable))
4540          start group entry number xrefs header)
4541     (while headers
4542       (setq header (pop headers))
4543       (when (and (setq xrefs (mail-header-xref header))
4544                  (not (memq (setq number (mail-header-number header))
4545                             unreads)))
4546         (setq start 0)
4547         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4548           (setq start (match-end 0))
4549           (setq group (if prefix
4550                           (concat prefix (substring xrefs (match-beginning 1)
4551                                                     (match-end 1)))
4552                         (substring xrefs (match-beginning 1) (match-end 1))))
4553           (setq number
4554                 (string-to-int (substring xrefs (match-beginning 2)
4555                                           (match-end 2))))
4556           (if (setq entry (gnus-gethash group xref-hashtb))
4557               (setcdr entry (cons number (cdr entry)))
4558             (gnus-sethash group (cons number nil) xref-hashtb)))))
4559     (and start xref-hashtb)))
4560
4561 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4562   "Look through all the headers and mark the Xrefs as read."
4563   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4564         name entry info xref-hashtb idlist method nth4)
4565     (save-excursion
4566       (set-buffer gnus-group-buffer)
4567       (when (setq xref-hashtb
4568                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4569         (mapatoms
4570          (lambda (group)
4571            (unless (string= from-newsgroup (setq name (symbol-name group)))
4572              (setq idlist (symbol-value group))
4573              ;; Dead groups are not updated.
4574              (and (prog1
4575                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4576                             info (nth 2 entry))
4577                     (when (stringp (setq nth4 (gnus-info-method info)))
4578                       (setq nth4 (gnus-server-to-method nth4))))
4579                   ;; Only do the xrefs if the group has the same
4580                   ;; select method as the group we have just read.
4581                   (or (gnus-methods-equal-p
4582                        nth4 (gnus-find-method-for-group from-newsgroup))
4583                       virtual
4584                       (equal nth4 (setq method (gnus-find-method-for-group
4585                                                 from-newsgroup)))
4586                       (and (equal (car nth4) (car method))
4587                            (equal (nth 1 nth4) (nth 1 method))))
4588                   gnus-use-cross-reference
4589                   (or (not (eq gnus-use-cross-reference t))
4590                       virtual
4591                       ;; Only do cross-references on subscribed
4592                       ;; groups, if that is what is wanted.
4593                       (<= (gnus-info-level info) gnus-level-subscribed))
4594                   (gnus-group-make-articles-read name idlist))))
4595          xref-hashtb)))))
4596
4597 (defun gnus-compute-read-articles (group articles)
4598   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4599          (info (nth 2 entry))
4600          (active (gnus-active group))
4601          ninfo)
4602     (when entry
4603       ;; First peel off all invalid article numbers.
4604       (when active
4605         (let ((ids articles)
4606               id first)
4607           (while (setq id (pop ids))
4608             (when (and first (> id (cdr active)))
4609               ;; We'll end up in this situation in one particular
4610               ;; obscure situation.  If you re-scan a group and get
4611               ;; a new article that is cross-posted to a different
4612               ;; group that has not been re-scanned, you might get
4613               ;; crossposted article that has a higher number than
4614               ;; Gnus believes possible.  So we re-activate this
4615               ;; group as well.  This might mean doing the
4616               ;; crossposting thingy will *increase* the number
4617               ;; of articles in some groups.  Tsk, tsk.
4618               (setq active (or (gnus-activate-group group) active)))
4619             (when (or (> id (cdr active))
4620                       (< id (car active)))
4621               (setq articles (delq id articles))))))
4622       ;; If the read list is nil, we init it.
4623       (if (and active
4624                (null (gnus-info-read info))
4625                (> (car active) 1))
4626           (setq ninfo (cons 1 (1- (car active))))
4627         (setq ninfo (gnus-info-read info)))
4628       ;; Then we add the read articles to the range.
4629       (gnus-add-to-range
4630        ninfo (setq articles (sort articles '<))))))
4631
4632 (defun gnus-group-make-articles-read (group articles)
4633   "Update the info of GROUP to say that ARTICLES are read."
4634   (let* ((num 0)
4635          (entry (gnus-gethash group gnus-newsrc-hashtb))
4636          (info (nth 2 entry))
4637          (active (gnus-active group))
4638          range)
4639     (when entry
4640       (setq range (gnus-compute-read-articles group articles))
4641       (save-excursion
4642         (set-buffer gnus-group-buffer)
4643         (gnus-undo-register
4644           `(progn
4645              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4646              (gnus-info-set-read ',info ',(gnus-info-read info))
4647              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4648              (gnus-group-update-group ,group t))))
4649       ;; Add the read articles to the range.
4650       (gnus-info-set-read info range)
4651       ;; Then we have to re-compute how many unread
4652       ;; articles there are in this group.
4653       (when active
4654         (cond
4655          ((not range)
4656           (setq num (- (1+ (cdr active)) (car active))))
4657          ((not (listp (cdr range)))
4658           (setq num (- (cdr active) (- (1+ (cdr range))
4659                                        (car range)))))
4660          (t
4661           (while range
4662             (if (numberp (car range))
4663                 (setq num (1+ num))
4664               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4665             (setq range (cdr range)))
4666           (setq num (- (cdr active) num))))
4667         ;; Update the number of unread articles.
4668         (setcar entry num)
4669         ;; Update the group buffer.
4670         (gnus-group-update-group group t)))))
4671
4672 (defvar gnus-newsgroup-none-id 0)
4673
4674 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4675   (let ((cur nntp-server-buffer)
4676         (dependencies
4677          (or dependencies
4678              (save-excursion (set-buffer gnus-summary-buffer)
4679                              gnus-newsgroup-dependencies)))
4680         headers id end ref
4681         (mail-parse-charset gnus-newsgroup-charset)
4682         (mail-parse-ignored-charsets
4683          (save-excursion (condition-case nil
4684                              (set-buffer gnus-summary-buffer)
4685                            (error))
4686                          gnus-newsgroup-ignored-charsets)))
4687     (save-excursion
4688       (set-buffer nntp-server-buffer)
4689       ;; Translate all TAB characters into SPACE characters.
4690       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4691       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4692       (gnus-run-hooks 'gnus-parse-headers-hook)
4693       (let ((case-fold-search t)
4694             in-reply-to header p lines chars ctype)
4695         (goto-char (point-min))
4696         ;; Search to the beginning of the next header.  Error messages
4697         ;; do not begin with 2 or 3.
4698         (while (re-search-forward "^[23][0-9]+ " nil t)
4699           (setq id nil
4700                 ref nil)
4701           ;; This implementation of this function, with nine
4702           ;; search-forwards instead of the one re-search-forward and
4703           ;; a case (which basically was the old function) is actually
4704           ;; about twice as fast, even though it looks messier.  You
4705           ;; can't have everything, I guess.  Speed and elegance
4706           ;; doesn't always go hand in hand.
4707           (setq
4708            header
4709            (make-full-mail-header
4710             ;; Number.
4711             (prog1
4712                 (read cur)
4713               (end-of-line)
4714               (setq p (point))
4715               (narrow-to-region (point)
4716                                 (or (and (search-forward "\n.\n" nil t)
4717                                          (- (point) 2))
4718                                     (point))))
4719             ;; Subject.
4720             (progn
4721               (goto-char p)
4722               (if (search-forward "\nsubject: " nil t)
4723                   (buffer-substring (match-end 0) (std11-field-end))
4724                 "(none)"))
4725             ;; From.
4726             (progn
4727               (goto-char p)
4728               (if (search-forward "\nfrom: " nil t)
4729                   (buffer-substring (match-end 0) (std11-field-end))
4730                 "(nobody)"))
4731             ;; Date.
4732             (progn
4733               (goto-char p)
4734               (if (search-forward "\ndate: " nil t)
4735                   (buffer-substring (match-end 0) (std11-field-end))
4736                 ""))
4737             ;; Message-ID.
4738             (progn
4739               (goto-char p)
4740               (setq id (if (re-search-forward
4741                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4742                            ;; We do it this way to make sure the Message-ID
4743                            ;; is (somewhat) syntactically valid.
4744                            (buffer-substring (match-beginning 1)
4745                                              (match-end 1))
4746                          ;; If there was no message-id, we just fake one
4747                          ;; to make subsequent routines simpler.
4748                          (nnheader-generate-fake-message-id))))
4749             ;; References.
4750             (progn
4751               (goto-char p)
4752               (if (search-forward "\nreferences: " nil t)
4753                   (progn
4754                     (setq end (point))
4755                     (prog1
4756                         (buffer-substring (match-end 0) (std11-field-end))
4757                       (setq ref
4758                             (buffer-substring
4759                              (progn
4760                                ;; (end-of-line)
4761                                (search-backward ">" end t)
4762                                (1+ (point)))
4763                              (progn
4764                                (search-backward "<" end t)
4765                                (point))))))
4766                 ;; Get the references from the in-reply-to header if there
4767                 ;; were no references and the in-reply-to header looks
4768                 ;; promising.
4769                 (if (and (search-forward "\nin-reply-to: " nil t)
4770                          (setq in-reply-to
4771                                (buffer-substring (match-end 0)
4772                                                  (std11-field-end)))
4773                          (string-match "<[^>]+>" in-reply-to))
4774                     (let (ref2)
4775                       (setq ref (substring in-reply-to (match-beginning 0)
4776                                            (match-end 0)))
4777                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4778                         (setq ref2 (substring in-reply-to (match-beginning 0)
4779                                               (match-end 0)))
4780                         (when (> (length ref2) (length ref))
4781                           (setq ref ref2)))
4782                       ref)
4783                   (setq ref nil))))
4784             ;; Chars.
4785             (progn
4786               (goto-char p)
4787               (if (search-forward "\nchars: " nil t)
4788                   (if (numberp (setq chars (ignore-errors (read cur))))
4789                       chars 0)
4790                 0))
4791             ;; Lines.
4792             (progn
4793               (goto-char p)
4794               (if (search-forward "\nlines: " nil t)
4795                   (if (numberp (setq lines (ignore-errors (read cur))))
4796                       lines 0)
4797                 0))
4798             ;; Xref.
4799             (progn
4800               (goto-char p)
4801               (and (search-forward "\nxref: " nil t)
4802                    (buffer-substring (match-end 0) (std11-field-end))))
4803             ;; Extra.
4804             (when gnus-extra-headers
4805               (let ((extra gnus-extra-headers)
4806                     out)
4807                 (while extra
4808                   (goto-char p)
4809                   (when (search-forward
4810                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4811                     (push (cons (car extra)
4812                                 (buffer-substring (match-end 0)
4813                                                   (std11-field-end)))
4814                           out))
4815                   (pop extra))
4816                 out))))
4817           (goto-char p)
4818           (if (and (search-forward "\ncontent-type: " nil t)
4819                    (setq ctype
4820                          (buffer-substring (match-end 0) (std11-field-end))))
4821               (mime-entity-set-content-type-internal
4822                header (mime-parse-Content-Type ctype)))
4823           (when (equal id ref)
4824             (setq ref nil))
4825
4826           (when gnus-alter-header-function
4827             (funcall gnus-alter-header-function header)
4828             (setq id (mail-header-id header)
4829                   ref (gnus-parent-id (mail-header-references header))))
4830
4831           (when (setq header
4832                       (gnus-dependencies-add-header
4833                        header dependencies force-new))
4834             (push header headers))
4835           (goto-char (point-max))
4836           (widen))
4837         (nreverse headers)))))
4838
4839 ;; Goes through the xover lines and returns a list of vectors
4840 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4841                                                   force-new dependencies
4842                                                   group also-fetch-heads)
4843   "Parse the news overview data in the server buffer, and return a
4844 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4845   ;; Get the Xref when the users reads the articles since most/some
4846   ;; NNTP servers do not include Xrefs when using XOVER.
4847   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4848   (let ((mail-parse-charset gnus-newsgroup-charset)
4849         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4850         (cur nntp-server-buffer)
4851         (dependencies (or dependencies gnus-newsgroup-dependencies))
4852         number headers header)
4853     (save-excursion
4854       (set-buffer nntp-server-buffer)
4855       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4856       ;; Allow the user to mangle the headers before parsing them.
4857       (gnus-run-hooks 'gnus-parse-headers-hook)
4858       (goto-char (point-min))
4859       (while (not (eobp))
4860         (condition-case ()
4861             (while (and sequence (not (eobp)))
4862               (setq number (read cur))
4863               (while (and sequence
4864                           (< (car sequence) number))
4865                 (setq sequence (cdr sequence)))
4866               (and sequence
4867                    (eq number (car sequence))
4868                    (progn
4869                      (setq sequence (cdr sequence))
4870                      (setq header (inline
4871                                     (gnus-nov-parse-line
4872                                      number dependencies force-new))))
4873                    (push header headers))
4874               (forward-line 1))
4875           (error
4876            (gnus-error 4 "Strange nov line (%d)"
4877                        (count-lines (point-min) (point)))))
4878         (forward-line 1))
4879       ;; A common bug in inn is that if you have posted an article and
4880       ;; then retrieves the active file, it will answer correctly --
4881       ;; the new article is included.  However, a NOV entry for the
4882       ;; article may not have been generated yet, so this may fail.
4883       ;; We work around this problem by retrieving the last few
4884       ;; headers using HEAD.
4885       (if (or (not also-fetch-heads)
4886               (not sequence))
4887           ;; We (probably) got all the headers.
4888           (nreverse headers)
4889         (let ((gnus-nov-is-evil t))
4890           (nconc
4891            (nreverse headers)
4892            (gnus-retrieve-parsed-headers sequence group)
4893            ))))))
4894
4895 (defun gnus-article-get-xrefs ()
4896   "Fill in the Xref value in `gnus-current-headers', if necessary.
4897 This is meant to be called in `gnus-article-internal-prepare-hook'."
4898   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4899                                  gnus-current-headers)))
4900     (or (not gnus-use-cross-reference)
4901         (not headers)
4902         (and (mail-header-xref headers)
4903              (not (string= (mail-header-xref headers) "")))
4904         (let ((case-fold-search t)
4905               xref)
4906           (save-restriction
4907             (nnheader-narrow-to-headers)
4908             (goto-char (point-min))
4909             (when (or (and (not (eobp))
4910                            (eq (downcase (char-after)) ?x)
4911                            (looking-at "Xref:"))
4912                       (search-forward "\nXref:" nil t))
4913               (goto-char (1+ (match-end 0)))
4914               (setq xref (buffer-substring (point)
4915                                            (progn (end-of-line) (point))))
4916               (mail-header-set-xref headers xref)))))))
4917
4918 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4919   "Find article ID and insert the summary line for that article.
4920 OLD-HEADER can either be a header or a line number to insert
4921 the subject line on."
4922   (let* ((line (and (numberp old-header) old-header))
4923          (old-header (and (vectorp old-header) old-header))
4924          (header (cond ((and old-header use-old-header)
4925                         old-header)
4926                        ((and (numberp id)
4927                              (gnus-number-to-header id))
4928                         (gnus-number-to-header id))
4929                        (t
4930                         (gnus-read-header id))))
4931          (number (and (numberp id) id))
4932          d)
4933     (when header
4934       ;; Rebuild the thread that this article is part of and go to the
4935       ;; article we have fetched.
4936       (when (and (not gnus-show-threads)
4937                  old-header)
4938         (when (and number
4939                    (setq d (gnus-data-find (mail-header-number old-header))))
4940           (goto-char (gnus-data-pos d))
4941           (gnus-data-remove
4942            number
4943            (- (gnus-point-at-bol)
4944               (prog1
4945                   (1+ (gnus-point-at-eol))
4946                 (gnus-delete-line))))))
4947       (when old-header
4948         (mail-header-set-number header (mail-header-number old-header)))
4949       (setq gnus-newsgroup-sparse
4950             (delq (setq number (mail-header-number header))
4951                   gnus-newsgroup-sparse))
4952       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4953       (push number gnus-newsgroup-limit)
4954       (gnus-rebuild-thread (mail-header-id header) line)
4955       (gnus-summary-goto-subject number nil t))
4956     (when (and (numberp number)
4957                (> number 0))
4958       ;; We have to update the boundaries even if we can't fetch the
4959       ;; article if ID is a number -- so that the next `P' or `N'
4960       ;; command will fetch the previous (or next) article even
4961       ;; if the one we tried to fetch this time has been canceled.
4962       (when (> number gnus-newsgroup-end)
4963         (setq gnus-newsgroup-end number))
4964       (when (< number gnus-newsgroup-begin)
4965         (setq gnus-newsgroup-begin number))
4966       (setq gnus-newsgroup-unselected
4967             (delq number gnus-newsgroup-unselected)))
4968     ;; Report back a success?
4969     (and header (mail-header-number header))))
4970
4971 ;;; Process/prefix in the summary buffer
4972
4973 (defun gnus-summary-work-articles (n)
4974   "Return a list of articles to be worked upon.
4975 The prefix argument, the list of process marked articles, and the
4976 current article will be taken into consideration."
4977   (save-excursion
4978     (set-buffer gnus-summary-buffer)
4979     (cond
4980      (n
4981       ;; A numerical prefix has been given.
4982       (setq n (prefix-numeric-value n))
4983       (let ((backward (< n 0))
4984             (n (abs (prefix-numeric-value n)))
4985             articles article)
4986         (save-excursion
4987           (while
4988               (and (> n 0)
4989                    (push (setq article (gnus-summary-article-number))
4990                          articles)
4991                    (if backward
4992                        (gnus-summary-find-prev nil article)
4993                      (gnus-summary-find-next nil article)))
4994             (decf n)))
4995         (nreverse articles)))
4996      ((and (gnus-region-active-p) (mark))
4997       (message "region active")
4998       ;; Work on the region between point and mark.
4999       (let ((max (max (point) (mark)))
5000             articles article)
5001         (save-excursion
5002           (goto-char (min (point) (mark)))
5003           (while
5004               (and
5005                (push (setq article (gnus-summary-article-number)) articles)
5006                (gnus-summary-find-next nil article)
5007                (< (point) max)))
5008           (nreverse articles))))
5009      (gnus-newsgroup-processable
5010       ;; There are process-marked articles present.
5011       ;; Save current state.
5012       (gnus-summary-save-process-mark)
5013       ;; Return the list.
5014       (reverse gnus-newsgroup-processable))
5015      (t
5016       ;; Just return the current article.
5017       (list (gnus-summary-article-number))))))
5018
5019 (defmacro gnus-summary-iterate (arg &rest forms)
5020   "Iterate over the process/prefixed articles and do FORMS.
5021 ARG is the interactive prefix given to the command.  FORMS will be
5022 executed with point over the summary line of the articles."
5023   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5024     `(let ((,articles (gnus-summary-work-articles ,arg)))
5025        (while ,articles
5026          (gnus-summary-goto-subject (car ,articles))
5027          ,@forms
5028          (pop ,articles)))))
5029
5030 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5031 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5032
5033 (defun gnus-summary-save-process-mark ()
5034   "Push the current set of process marked articles on the stack."
5035   (interactive)
5036   (push (copy-sequence gnus-newsgroup-processable)
5037         gnus-newsgroup-process-stack))
5038
5039 (defun gnus-summary-kill-process-mark ()
5040   "Push the current set of process marked articles on the stack and unmark."
5041   (interactive)
5042   (gnus-summary-save-process-mark)
5043   (gnus-summary-unmark-all-processable))
5044
5045 (defun gnus-summary-yank-process-mark ()
5046   "Pop the last process mark state off the stack and restore it."
5047   (interactive)
5048   (unless gnus-newsgroup-process-stack
5049     (error "Empty mark stack"))
5050   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5051
5052 (defun gnus-summary-process-mark-set (set)
5053   "Make SET into the current process marked articles."
5054   (gnus-summary-unmark-all-processable)
5055   (while set
5056     (gnus-summary-set-process-mark (pop set))))
5057
5058 ;;; Searching and stuff
5059
5060 (defun gnus-summary-search-group (&optional backward use-level)
5061   "Search for next unread newsgroup.
5062 If optional argument BACKWARD is non-nil, search backward instead."
5063   (save-excursion
5064     (set-buffer gnus-group-buffer)
5065     (when (gnus-group-search-forward
5066            backward nil (if use-level (gnus-group-group-level) nil))
5067       (gnus-group-group-name))))
5068
5069 (defun gnus-summary-best-group (&optional exclude-group)
5070   "Find the name of the best unread group.
5071 If EXCLUDE-GROUP, do not go to this group."
5072   (save-excursion
5073     (set-buffer gnus-group-buffer)
5074     (save-excursion
5075       (gnus-group-best-unread-group exclude-group))))
5076
5077 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5078   (if backward (gnus-summary-find-prev)
5079     (let* ((dummy (gnus-summary-article-intangible-p))
5080            (article (or article (gnus-summary-article-number)))
5081            (arts (gnus-data-find-list article))
5082            result)
5083       (when (and (not dummy)
5084                  (or (not gnus-summary-check-current)
5085                      (not unread)
5086                      (not (gnus-data-unread-p (car arts)))))
5087         (setq arts (cdr arts)))
5088       (when (setq result
5089                   (if unread
5090                       (progn
5091                         (while arts
5092                           (when (or (and undownloaded
5093                                          (eq gnus-undownloaded-mark
5094                                              (gnus-data-mark (car arts))))
5095                                     (gnus-data-unread-p (car arts)))
5096                             (setq result (car arts)
5097                                   arts nil))
5098                           (setq arts (cdr arts)))
5099                         result)
5100                     (car arts)))
5101         (goto-char (gnus-data-pos result))
5102         (gnus-data-number result)))))
5103
5104 (defun gnus-summary-find-prev (&optional unread article)
5105   (let* ((eobp (eobp))
5106          (article (or article (gnus-summary-article-number)))
5107          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5108          result)
5109     (when (and (not eobp)
5110                (or (not gnus-summary-check-current)
5111                    (not unread)
5112                    (not (gnus-data-unread-p (car arts)))))
5113       (setq arts (cdr arts)))
5114     (when (setq result
5115                 (if unread
5116                     (progn
5117                       (while arts
5118                         (when (gnus-data-unread-p (car arts))
5119                           (setq result (car arts)
5120                                 arts nil))
5121                         (setq arts (cdr arts)))
5122                       result)
5123                   (car arts)))
5124       (goto-char (gnus-data-pos result))
5125       (gnus-data-number result))))
5126
5127 (defun gnus-summary-find-subject (subject &optional unread backward article)
5128   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5129          (article (or article (gnus-summary-article-number)))
5130          (articles (gnus-data-list backward))
5131          (arts (gnus-data-find-list article articles))
5132          result)
5133     (when (or (not gnus-summary-check-current)
5134               (not unread)
5135               (not (gnus-data-unread-p (car arts))))
5136       (setq arts (cdr arts)))
5137     (while arts
5138       (and (or (not unread)
5139                (gnus-data-unread-p (car arts)))
5140            (vectorp (gnus-data-header (car arts)))
5141            (gnus-subject-equal
5142             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5143            (setq result (car arts)
5144                  arts nil))
5145       (setq arts (cdr arts)))
5146     (and result
5147          (goto-char (gnus-data-pos result))
5148          (gnus-data-number result))))
5149
5150 (defun gnus-summary-search-forward (&optional unread subject backward)
5151   "Search forward for an article.
5152 If UNREAD, look for unread articles.  If SUBJECT, look for
5153 articles with that subject.  If BACKWARD, search backward instead."
5154   (cond (subject (gnus-summary-find-subject subject unread backward))
5155         (backward (gnus-summary-find-prev unread))
5156         (t (gnus-summary-find-next unread))))
5157
5158 (defun gnus-recenter (&optional n)
5159   "Center point in window and redisplay frame.
5160 Also do horizontal recentering."
5161   (interactive "P")
5162   (when (and nil
5163              gnus-auto-center-summary
5164              (not (eq gnus-auto-center-summary 'vertical)))
5165     (gnus-horizontal-recenter))
5166   (recenter n))
5167
5168 (defun gnus-summary-recenter ()
5169   "Center point in the summary window.
5170 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5171 displayed, no centering will be performed."
5172   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5173   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5174   (interactive)
5175   (let* ((top (cond ((< (window-height) 4) 0)
5176                     ((< (window-height) 7) 1)
5177                     (t (if (numberp gnus-auto-center-summary)
5178                            gnus-auto-center-summary
5179                          2))))
5180          (height (1- (window-height)))
5181          (bottom (save-excursion (goto-char (point-max))
5182                                  (forward-line (- height))
5183                                  (point)))
5184          (window (get-buffer-window (current-buffer))))
5185     ;; The user has to want it.
5186     (when gnus-auto-center-summary
5187       (when (get-buffer-window gnus-article-buffer)
5188         ;; Only do recentering when the article buffer is displayed,
5189         ;; Set the window start to either `bottom', which is the biggest
5190         ;; possible valid number, or the second line from the top,
5191         ;; whichever is the least.
5192         (set-window-start
5193          window (min bottom (save-excursion
5194                               (forward-line (- top)) (point)))
5195          t))
5196       ;; Do horizontal recentering while we're at it.
5197       (when (and (get-buffer-window (current-buffer) t)
5198                  (not (eq gnus-auto-center-summary 'vertical)))
5199         (let ((selected (selected-window)))
5200           (select-window (get-buffer-window (current-buffer) t))
5201           (gnus-summary-position-point)
5202           (gnus-horizontal-recenter)
5203           (select-window selected))))))
5204
5205 (defun gnus-summary-jump-to-group (newsgroup)
5206   "Move point to NEWSGROUP in group mode buffer."
5207   ;; Keep update point of group mode buffer if visible.
5208   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5209       (save-window-excursion
5210         ;; Take care of tree window mode.
5211         (when (get-buffer-window gnus-group-buffer)
5212           (pop-to-buffer gnus-group-buffer))
5213         (gnus-group-jump-to-group newsgroup))
5214     (save-excursion
5215       ;; Take care of tree window mode.
5216       (if (get-buffer-window gnus-group-buffer)
5217           (pop-to-buffer gnus-group-buffer)
5218         (set-buffer gnus-group-buffer))
5219       (gnus-group-jump-to-group newsgroup))))
5220
5221 ;; This function returns a list of article numbers based on the
5222 ;; difference between the ranges of read articles in this group and
5223 ;; the range of active articles.
5224 (defun gnus-list-of-unread-articles (group)
5225   (let* ((read (gnus-info-read (gnus-get-info group)))
5226          (active (or (gnus-active group) (gnus-activate-group group)))
5227          (last (cdr active))
5228          first nlast unread)
5229     ;; If none are read, then all are unread.
5230     (if (not read)
5231         (setq first (car active))
5232       ;; If the range of read articles is a single range, then the
5233       ;; first unread article is the article after the last read
5234       ;; article.  Sounds logical, doesn't it?
5235       (if (and (not (listp (cdr read)))
5236                (or (< (car read) (car active))
5237                    (progn (setq read (list read))
5238                           nil)))
5239           (setq first (max (car active) (1+ (cdr read))))
5240         ;; `read' is a list of ranges.
5241         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5242                                   (caar read)))
5243                   1)
5244           (setq first (car active)))
5245         (while read
5246           (when first
5247             (while (< first nlast)
5248               (push first unread)
5249               (setq first (1+ first))))
5250           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5251           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5252           (setq read (cdr read)))))
5253     ;; And add the last unread articles.
5254     (while (<= first last)
5255       (push first unread)
5256       (setq first (1+ first)))
5257     ;; Return the list of unread articles.
5258     (delq 0 (nreverse unread))))
5259
5260 (defun gnus-list-of-read-articles (group)
5261   "Return a list of unread, unticked and non-dormant articles."
5262   (let* ((info (gnus-get-info group))
5263          (marked (gnus-info-marks info))
5264          (active (gnus-active group)))
5265     (and info active
5266          (gnus-set-difference
5267           (gnus-sorted-complement
5268            (gnus-uncompress-range active)
5269            (gnus-list-of-unread-articles group))
5270           (append
5271            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5272            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5273
5274 ;; Various summary commands
5275
5276 (defun gnus-summary-select-article-buffer ()
5277   "Reconfigure windows to show article buffer."
5278   (interactive)
5279   (if (not (gnus-buffer-live-p gnus-article-buffer))
5280       (error "There is no article buffer for this summary buffer")
5281     (gnus-configure-windows 'article)
5282     (select-window (get-buffer-window gnus-article-buffer))))
5283
5284 (defun gnus-summary-universal-argument (arg)
5285   "Perform any operation on all articles that are process/prefixed."
5286   (interactive "P")
5287   (let ((articles (gnus-summary-work-articles arg))
5288         func article)
5289     (if (eq
5290          (setq
5291           func
5292           (key-binding
5293            (read-key-sequence
5294             (substitute-command-keys
5295              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5296          'undefined)
5297         (gnus-error 1 "Undefined key")
5298       (save-excursion
5299         (while articles
5300           (gnus-summary-goto-subject (setq article (pop articles)))
5301           (let (gnus-newsgroup-processable)
5302             (command-execute func))
5303           (gnus-summary-remove-process-mark article)))))
5304   (gnus-summary-position-point))
5305
5306 (defun gnus-summary-toggle-truncation (&optional arg)
5307   "Toggle truncation of summary lines.
5308 With arg, turn line truncation on iff arg is positive."
5309   (interactive "P")
5310   (setq truncate-lines
5311         (if (null arg) (not truncate-lines)
5312           (> (prefix-numeric-value arg) 0)))
5313   (redraw-display))
5314
5315 (defun gnus-summary-reselect-current-group (&optional all rescan)
5316   "Exit and then reselect the current newsgroup.
5317 The prefix argument ALL means to select all articles."
5318   (interactive "P")
5319   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5320     (error "Ephemeral groups can't be reselected"))
5321   (let ((current-subject (gnus-summary-article-number))
5322         (group gnus-newsgroup-name))
5323     (setq gnus-newsgroup-begin nil)
5324     (gnus-summary-exit)
5325     ;; We have to adjust the point of group mode buffer because
5326     ;; point was moved to the next unread newsgroup by exiting.
5327     (gnus-summary-jump-to-group group)
5328     (when rescan
5329       (save-excursion
5330         (save-window-excursion
5331           ;; Don't show group contents.
5332           (set-window-start (selected-window) (point-max))
5333           (gnus-group-get-new-news-this-group 1))))
5334     (gnus-group-read-group all t)
5335     (gnus-summary-goto-subject current-subject nil t)))
5336
5337 (defun gnus-summary-rescan-group (&optional all)
5338   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5339   (interactive "P")
5340   (gnus-summary-reselect-current-group all t))
5341
5342 (defun gnus-summary-update-info (&optional non-destructive)
5343   (save-excursion
5344     (let ((group gnus-newsgroup-name))
5345       (when group
5346         (when gnus-newsgroup-kill-headers
5347           (setq gnus-newsgroup-killed
5348                 (gnus-compress-sequence
5349                  (nconc
5350                   (gnus-set-sorted-intersection
5351                    (gnus-uncompress-range gnus-newsgroup-killed)
5352                    (setq gnus-newsgroup-unselected
5353                          (sort gnus-newsgroup-unselected '<)))
5354                   (setq gnus-newsgroup-unreads
5355                         (sort gnus-newsgroup-unreads '<)))
5356                  t)))
5357         (unless (listp (cdr gnus-newsgroup-killed))
5358           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5359         (let ((headers gnus-newsgroup-headers))
5360           ;; Set the new ranges of read articles.
5361           (save-excursion
5362             (set-buffer gnus-group-buffer)
5363             (gnus-undo-force-boundary))
5364           (gnus-update-read-articles
5365            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5366           ;; Set the current article marks.
5367           (let ((gnus-newsgroup-scored
5368                  (if (and (not gnus-save-score)
5369                           (not non-destructive))
5370                      nil
5371                    gnus-newsgroup-scored)))
5372             (save-excursion
5373               (gnus-update-marks)))
5374           ;; Do the cross-ref thing.
5375           (when gnus-use-cross-reference
5376             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5377           ;; Do not switch windows but change the buffer to work.
5378           (set-buffer gnus-group-buffer)
5379           (unless (gnus-ephemeral-group-p group)
5380             (gnus-group-update-group group)))))))
5381
5382 (defun gnus-summary-save-newsrc (&optional force)
5383   "Save the current number of read/marked articles in the dribble buffer.
5384 The dribble buffer will then be saved.
5385 If FORCE (the prefix), also save the .newsrc file(s)."
5386   (interactive "P")
5387   (gnus-summary-update-info t)
5388   (if force
5389       (gnus-save-newsrc-file)
5390     (gnus-dribble-save)))
5391
5392 (defun gnus-summary-exit (&optional temporary)
5393   "Exit reading current newsgroup, and then return to group selection mode.
5394 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5395   (interactive)
5396   (gnus-set-global-variables)
5397   (gnus-kill-save-kill-buffer)
5398   (gnus-async-halt-prefetch)
5399   (let* ((group gnus-newsgroup-name)
5400          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5401          (mode major-mode)
5402          (group-point nil)
5403          (buf (current-buffer)))
5404     (unless quit-config
5405       ;; Do adaptive scoring, and possibly save score files.
5406       (when gnus-newsgroup-adaptive
5407         (gnus-score-adaptive))
5408       (when gnus-use-scoring
5409         (gnus-score-save)))
5410     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5411     ;; If we have several article buffers, we kill them at exit.
5412     (unless gnus-single-article-buffer
5413       (gnus-kill-buffer gnus-original-article-buffer)
5414       (setq gnus-article-current nil))
5415     (when gnus-use-cache
5416       (gnus-cache-possibly-remove-articles)
5417       (gnus-cache-save-buffers))
5418     (gnus-async-prefetch-remove-group group)
5419     (when gnus-suppress-duplicates
5420       (gnus-dup-enter-articles))
5421     (when gnus-use-trees
5422       (gnus-tree-close group))
5423     (when gnus-use-cache
5424       (gnus-cache-write-active))
5425     ;; Remove entries for this group.
5426     (nnmail-purge-split-history (gnus-group-real-name group))
5427     ;; Make all changes in this group permanent.
5428     (unless quit-config
5429       (gnus-run-hooks 'gnus-exit-group-hook)
5430       (gnus-summary-update-info))
5431     (gnus-close-group group)
5432     ;; Make sure where we were, and go to next newsgroup.
5433     (set-buffer gnus-group-buffer)
5434     (unless quit-config
5435       (gnus-group-jump-to-group group))
5436     (gnus-run-hooks 'gnus-summary-exit-hook)
5437     (unless (or quit-config
5438                 ;; If this group has disappeared from the summary
5439                 ;; buffer, don't skip forwards.
5440                 (not (string= group (gnus-group-group-name))))
5441       (gnus-group-next-unread-group 1))
5442     (setq group-point (point))
5443     (if temporary
5444         nil                             ;Nothing to do.
5445       ;; If we have several article buffers, we kill them at exit.
5446       (unless gnus-single-article-buffer
5447         (gnus-kill-buffer gnus-article-buffer)
5448         (gnus-kill-buffer gnus-original-article-buffer)
5449         (setq gnus-article-current nil))
5450       (set-buffer buf)
5451       (if (not gnus-kill-summary-on-exit)
5452           (gnus-deaden-summary)
5453         ;; We set all buffer-local variables to nil.  It is unclear why
5454         ;; this is needed, but if we don't, buffer-local variables are
5455         ;; not garbage-collected, it seems.  This would the lead to en
5456         ;; ever-growing Emacs.
5457         (gnus-summary-clear-local-variables)
5458         (when (get-buffer gnus-article-buffer)
5459           (bury-buffer gnus-article-buffer))
5460         ;; We clear the global counterparts of the buffer-local
5461         ;; variables as well, just to be on the safe side.
5462         (set-buffer gnus-group-buffer)
5463         (gnus-summary-clear-local-variables)
5464         ;; Return to group mode buffer.
5465         (when (eq mode 'gnus-summary-mode)
5466           (gnus-kill-buffer buf)))
5467       (setq gnus-current-select-method gnus-select-method)
5468       (pop-to-buffer gnus-group-buffer)
5469       (if (not quit-config)
5470           (progn
5471             (goto-char group-point)
5472             (gnus-configure-windows 'group 'force)
5473             (unless (pos-visible-in-window-p)
5474               (forward-line (/ (static-if (featurep 'xemacs)
5475                                    (window-displayed-height)
5476                                  (1- (window-height)))
5477                                -2))
5478               (set-window-start (selected-window) (point))
5479               (goto-char group-point)))
5480         (gnus-handle-ephemeral-exit quit-config))
5481       ;; Clear the current group name.
5482       (unless quit-config
5483         (setq gnus-newsgroup-name nil)))))
5484
5485 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5486 (defun gnus-summary-exit-no-update (&optional no-questions)
5487   "Quit reading current newsgroup without updating read article info."
5488   (interactive)
5489   (let* ((group gnus-newsgroup-name)
5490          (quit-config (gnus-group-quit-config group)))
5491     (when (or no-questions
5492               gnus-expert-user
5493               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5494       (gnus-async-halt-prefetch)
5495       (mapcar 'funcall
5496               (delq 'gnus-summary-expire-articles
5497                     (copy-sequence gnus-summary-prepare-exit-hook)))
5498       ;; If we have several article buffers, we kill them at exit.
5499       (unless gnus-single-article-buffer
5500         (gnus-kill-buffer gnus-article-buffer)
5501         (gnus-kill-buffer gnus-original-article-buffer)
5502         (setq gnus-article-current nil))
5503       (if (not gnus-kill-summary-on-exit)
5504           (gnus-deaden-summary)
5505         (gnus-close-group group)
5506         (gnus-summary-clear-local-variables)
5507         (set-buffer gnus-group-buffer)
5508         (gnus-summary-clear-local-variables)
5509         (when (get-buffer gnus-summary-buffer)
5510           (kill-buffer gnus-summary-buffer)))
5511       (unless gnus-single-article-buffer
5512         (setq gnus-article-current nil))
5513       (when gnus-use-trees
5514         (gnus-tree-close group))
5515       (gnus-async-prefetch-remove-group group)
5516       (when (get-buffer gnus-article-buffer)
5517         (bury-buffer gnus-article-buffer))
5518       ;; Return to the group buffer.
5519       (gnus-configure-windows 'group 'force)
5520       ;; Clear the current group name.
5521       (setq gnus-newsgroup-name nil)
5522       (when (equal (gnus-group-group-name) group)
5523         (gnus-group-next-unread-group 1))
5524       (when quit-config
5525         (gnus-handle-ephemeral-exit quit-config)))))
5526
5527 (defun gnus-handle-ephemeral-exit (quit-config)
5528   "Handle movement when leaving an ephemeral group.
5529 The state which existed when entering the ephemeral is reset."
5530   (if (not (buffer-name (car quit-config)))
5531       (gnus-configure-windows 'group 'force)
5532     (set-buffer (car quit-config))
5533     (cond ((eq major-mode 'gnus-summary-mode)
5534            (gnus-set-global-variables))
5535           ((eq major-mode 'gnus-article-mode)
5536            (save-excursion
5537              ;; The `gnus-summary-buffer' variable may point
5538              ;; to the old summary buffer when using a single
5539              ;; article buffer.
5540              (unless (gnus-buffer-live-p gnus-summary-buffer)
5541                (set-buffer gnus-group-buffer))
5542              (set-buffer gnus-summary-buffer)
5543              (gnus-set-global-variables))))
5544     (if (or (eq (cdr quit-config) 'article)
5545             (eq (cdr quit-config) 'pick))
5546         (progn
5547           ;; The current article may be from the ephemeral group
5548           ;; thus it is best that we reload this article
5549           (gnus-summary-show-article)
5550           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5551               (gnus-configure-windows 'pick 'force)
5552             (gnus-configure-windows (cdr quit-config) 'force)))
5553       (gnus-configure-windows (cdr quit-config) 'force))
5554     (when (eq major-mode 'gnus-summary-mode)
5555       (gnus-summary-next-subject 1 nil t)
5556       (gnus-summary-recenter)
5557       (gnus-summary-position-point))))
5558
5559 (defun gnus-summary-preview-mime-message ()
5560   "MIME decode and play this message."
5561   (interactive)
5562   (let ((gnus-break-pages nil)
5563         (gnus-show-mime t))
5564     (gnus-summary-select-article gnus-show-all-headers t))
5565   (select-window (get-buffer-window gnus-article-buffer)))
5566
5567 ;;; Dead summaries.
5568
5569 (defvar gnus-dead-summary-mode-map nil)
5570
5571 (unless gnus-dead-summary-mode-map
5572   (setq gnus-dead-summary-mode-map (make-keymap))
5573   (suppress-keymap gnus-dead-summary-mode-map)
5574   (substitute-key-definition
5575    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5576   (let ((keys '("\C-d" "\r" "\177" [delete])))
5577     (while keys
5578       (define-key gnus-dead-summary-mode-map
5579         (pop keys) 'gnus-summary-wake-up-the-dead))))
5580
5581 (defvar gnus-dead-summary-mode nil
5582   "Minor mode for Gnus summary buffers.")
5583
5584 (defun gnus-dead-summary-mode (&optional arg)
5585   "Minor mode for Gnus summary buffers."
5586   (interactive "P")
5587   (when (eq major-mode 'gnus-summary-mode)
5588     (make-local-variable 'gnus-dead-summary-mode)
5589     (setq gnus-dead-summary-mode
5590           (if (null arg) (not gnus-dead-summary-mode)
5591             (> (prefix-numeric-value arg) 0)))
5592     (when gnus-dead-summary-mode
5593       (gnus-add-minor-mode
5594        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5595
5596 (defun gnus-deaden-summary ()
5597   "Make the current summary buffer into a dead summary buffer."
5598   ;; Kill any previous dead summary buffer.
5599   (when (and gnus-dead-summary
5600              (buffer-name gnus-dead-summary))
5601     (save-excursion
5602       (set-buffer gnus-dead-summary)
5603       (when gnus-dead-summary-mode
5604         (kill-buffer (current-buffer)))))
5605   ;; Make this the current dead summary.
5606   (setq gnus-dead-summary (current-buffer))
5607   (gnus-dead-summary-mode 1)
5608   (let ((name (buffer-name)))
5609     (when (string-match "Summary" name)
5610       (rename-buffer
5611        (concat (substring name 0 (match-beginning 0)) "Dead "
5612                (substring name (match-beginning 0)))
5613        t)
5614       (bury-buffer))))
5615
5616 (defun gnus-kill-or-deaden-summary (buffer)
5617   "Kill or deaden the summary BUFFER."
5618   (save-excursion
5619     (when (and (buffer-name buffer)
5620                (not gnus-single-article-buffer))
5621       (save-excursion
5622         (set-buffer buffer)
5623         (gnus-kill-buffer gnus-article-buffer)
5624         (gnus-kill-buffer gnus-original-article-buffer)))
5625     (cond (gnus-kill-summary-on-exit
5626            (when (and gnus-use-trees
5627                       (gnus-buffer-exists-p buffer))
5628              (save-excursion
5629                (set-buffer buffer)
5630                (gnus-tree-close gnus-newsgroup-name)))
5631            (gnus-kill-buffer buffer))
5632           ((gnus-buffer-exists-p buffer)
5633            (save-excursion
5634              (set-buffer buffer)
5635              (gnus-deaden-summary))))))
5636
5637 (defun gnus-summary-wake-up-the-dead (&rest args)
5638   "Wake up the dead summary buffer."
5639   (interactive)
5640   (gnus-dead-summary-mode -1)
5641   (let ((name (buffer-name)))
5642     (when (string-match "Dead " name)
5643       (rename-buffer
5644        (concat (substring name 0 (match-beginning 0))
5645                (substring name (match-end 0)))
5646        t)))
5647   (gnus-message 3 "This dead summary is now alive again"))
5648
5649 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5650 (defun gnus-summary-fetch-faq (&optional faq-dir)
5651   "Fetch the FAQ for the current group.
5652 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5653 in."
5654   (interactive
5655    (list
5656     (when current-prefix-arg
5657       (completing-read
5658        "Faq dir: " (and (listp gnus-group-faq-directory)
5659                         (mapcar (lambda (file) (list file))
5660                                 gnus-group-faq-directory))))))
5661   (let (gnus-faq-buffer)
5662     (when (setq gnus-faq-buffer
5663                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5664       (gnus-configure-windows 'summary-faq))))
5665
5666 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5667 (defun gnus-summary-describe-group (&optional force)
5668   "Describe the current newsgroup."
5669   (interactive "P")
5670   (gnus-group-describe-group force gnus-newsgroup-name))
5671
5672 (defun gnus-summary-describe-briefly ()
5673   "Describe summary mode commands briefly."
5674   (interactive)
5675   (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")))
5676
5677 ;; Walking around group mode buffer from summary mode.
5678
5679 (defun gnus-summary-next-group (&optional no-article target-group backward)
5680   "Exit current newsgroup and then select next unread newsgroup.
5681 If prefix argument NO-ARTICLE is non-nil, no article is selected
5682 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5683 previous group instead."
5684   (interactive "P")
5685   ;; Stop pre-fetching.
5686   (gnus-async-halt-prefetch)
5687   (let ((current-group gnus-newsgroup-name)
5688         (current-buffer (current-buffer))
5689         entered)
5690     ;; First we semi-exit this group to update Xrefs and all variables.
5691     ;; We can't do a real exit, because the window conf must remain
5692     ;; the same in case the user is prompted for info, and we don't
5693     ;; want the window conf to change before that...
5694     (gnus-summary-exit t)
5695     (while (not entered)
5696       ;; Then we find what group we are supposed to enter.
5697       (set-buffer gnus-group-buffer)
5698       (gnus-group-jump-to-group current-group)
5699       (setq target-group
5700             (or target-group
5701                 (if (eq gnus-keep-same-level 'best)
5702                     (gnus-summary-best-group gnus-newsgroup-name)
5703                   (gnus-summary-search-group backward gnus-keep-same-level))))
5704       (if (not target-group)
5705           ;; There are no further groups, so we return to the group
5706           ;; buffer.
5707           (progn
5708             (gnus-message 5 "Returning to the group buffer")
5709             (setq entered t)
5710             (when (gnus-buffer-live-p current-buffer)
5711               (set-buffer current-buffer)
5712               (gnus-summary-exit))
5713             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5714         ;; We try to enter the target group.
5715         (gnus-group-jump-to-group target-group)
5716         (let ((unreads (gnus-group-group-unread)))
5717           (if (and (or (eq t unreads)
5718                        (and unreads (not (zerop unreads))))
5719                    (gnus-summary-read-group
5720                     target-group nil no-article
5721                     (and (buffer-name current-buffer) current-buffer)
5722                     nil backward))
5723               (setq entered t)
5724             (setq current-group target-group
5725                   target-group nil)))))))
5726
5727 (defun gnus-summary-prev-group (&optional no-article)
5728   "Exit current newsgroup and then select previous unread newsgroup.
5729 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5730   (interactive "P")
5731   (gnus-summary-next-group no-article nil t))
5732
5733 ;; Walking around summary lines.
5734
5735 (defun gnus-summary-first-subject (&optional unread undownloaded)
5736   "Go to the first unread subject.
5737 If UNREAD is non-nil, go to the first unread article.
5738 Returns the article selected or nil if there are no unread articles."
5739   (interactive "P")
5740   (prog1
5741       (cond
5742        ;; Empty summary.
5743        ((null gnus-newsgroup-data)
5744         (gnus-message 3 "No articles in the group")
5745         nil)
5746        ;; Pick the first article.
5747        ((not unread)
5748         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5749         (gnus-data-number (car gnus-newsgroup-data)))
5750        ;; No unread articles.
5751        ((null gnus-newsgroup-unreads)
5752         (gnus-message 3 "No more unread articles")
5753         nil)
5754        ;; Find the first unread article.
5755        (t
5756         (let ((data gnus-newsgroup-data))
5757           (while (and data
5758                       (and (not (and undownloaded
5759                                      (eq gnus-undownloaded-mark
5760                                          (gnus-data-mark (car data)))))
5761                            (not (gnus-data-unread-p (car data)))))
5762             (setq data (cdr data)))
5763           (when data
5764             (goto-char (gnus-data-pos (car data)))
5765             (gnus-data-number (car data))))))
5766     (gnus-summary-position-point)))
5767
5768 (defun gnus-summary-next-subject (n &optional unread dont-display)
5769   "Go to next N'th summary line.
5770 If N is negative, go to the previous N'th subject line.
5771 If UNREAD is non-nil, only unread articles are selected.
5772 The difference between N and the actual number of steps taken is
5773 returned."
5774   (interactive "p")
5775   (let ((backward (< n 0))
5776         (n (abs n)))
5777     (while (and (> n 0)
5778                 (if backward
5779                     (gnus-summary-find-prev unread)
5780                   (gnus-summary-find-next unread)))
5781       (unless (zerop (setq n (1- n)))
5782         (gnus-summary-show-thread)))
5783     (when (/= 0 n)
5784       (gnus-message 7 "No more%s articles"
5785                     (if unread " unread" "")))
5786     (unless dont-display
5787       (gnus-summary-recenter)
5788       (gnus-summary-position-point))
5789     n))
5790
5791 (defun gnus-summary-next-unread-subject (n)
5792   "Go to next N'th unread summary line."
5793   (interactive "p")
5794   (gnus-summary-next-subject n t))
5795
5796 (defun gnus-summary-prev-subject (n &optional unread)
5797   "Go to previous N'th summary line.
5798 If optional argument UNREAD is non-nil, only unread article is selected."
5799   (interactive "p")
5800   (gnus-summary-next-subject (- n) unread))
5801
5802 (defun gnus-summary-prev-unread-subject (n)
5803   "Go to previous N'th unread summary line."
5804   (interactive "p")
5805   (gnus-summary-next-subject (- n) t))
5806
5807 (defun gnus-summary-goto-subject (article &optional force silent)
5808   "Go the subject line of ARTICLE.
5809 If FORCE, also allow jumping to articles not currently shown."
5810   (interactive "nArticle number: ")
5811   (let ((b (point))
5812         (data (gnus-data-find article)))
5813     ;; We read in the article if we have to.
5814     (and (not data)
5815          force
5816          (gnus-summary-insert-subject
5817           article
5818           (if (or (numberp force) (vectorp force)) force)
5819           t)
5820          (setq data (gnus-data-find article)))
5821     (goto-char b)
5822     (if (not data)
5823         (progn
5824           (unless silent
5825             (gnus-message 3 "Can't find article %d" article))
5826           nil)
5827       (goto-char (gnus-data-pos data))
5828       (gnus-summary-position-point)
5829       article)))
5830
5831 ;; Walking around summary lines with displaying articles.
5832
5833 (defun gnus-summary-expand-window (&optional arg)
5834   "Make the summary buffer take up the entire Emacs frame.
5835 Given a prefix, will force an `article' buffer configuration."
5836   (interactive "P")
5837   (if arg
5838       (gnus-configure-windows 'article 'force)
5839     (gnus-configure-windows 'summary 'force)))
5840
5841 (defun gnus-summary-display-article (article &optional all-header)
5842   "Display ARTICLE in article buffer."
5843   (gnus-set-global-variables)
5844   (if (null article)
5845       nil
5846     (prog1
5847         (if gnus-summary-display-article-function
5848             (funcall gnus-summary-display-article-function article all-header)
5849           (gnus-article-prepare article all-header))
5850       (with-current-buffer gnus-article-buffer
5851         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5852              nil))
5853       (gnus-run-hooks 'gnus-select-article-hook)
5854       (when (and gnus-current-article
5855                  (not (zerop gnus-current-article)))
5856         (gnus-summary-goto-subject gnus-current-article))
5857       (gnus-summary-recenter)
5858       (when (and gnus-use-trees gnus-show-threads)
5859         (gnus-possibly-generate-tree article)
5860         (gnus-highlight-selected-tree article))
5861       ;; Successfully display article.
5862       (gnus-article-set-window-start
5863        (cdr (assq article gnus-newsgroup-bookmarks))))))
5864
5865 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5866   "Select the current article.
5867 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5868 non-nil, the article will be re-fetched even if it already present in
5869 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5870 be displayed."
5871   ;; Make sure we are in the summary buffer to work around bbdb bug.
5872   (unless (eq major-mode 'gnus-summary-mode)
5873     (set-buffer gnus-summary-buffer))
5874   (let ((article (or article (gnus-summary-article-number)))
5875         (all-headers (not (not all-headers))) ;Must be T or NIL.
5876         gnus-summary-display-article-function)
5877     (and (not pseudo)
5878          (gnus-summary-article-pseudo-p article)
5879          (error "This is a pseudo-article"))
5880     (save-excursion
5881       (set-buffer gnus-summary-buffer)
5882       (if (or (and gnus-single-article-buffer
5883                    (or (null gnus-current-article)
5884                        (null gnus-article-current)
5885                        (null (get-buffer gnus-article-buffer))
5886                        (not (eq article (cdr gnus-article-current)))
5887                        (not (equal (car gnus-article-current)
5888                                    gnus-newsgroup-name))))
5889               (and (not gnus-single-article-buffer)
5890                    (or (null gnus-current-article)
5891                        (not (eq gnus-current-article article))))
5892               force)
5893           ;; The requested article is different from the current article.
5894           (progn
5895             (when (gnus-buffer-live-p gnus-article-buffer)
5896               (with-current-buffer gnus-article-buffer
5897                 (mm-enable-multibyte)))
5898             (gnus-summary-display-article article all-headers)
5899             (when (gnus-buffer-live-p gnus-article-buffer)
5900               (with-current-buffer gnus-article-buffer
5901                 (if (not gnus-article-decoded-p) ;; a local variable
5902                     (mm-disable-multibyte))))
5903             (when (or all-headers gnus-show-all-headers)
5904               (gnus-article-show-all-headers))
5905             (gnus-article-set-window-start
5906              (cdr (assq article gnus-newsgroup-bookmarks)))
5907             article)
5908         (when (or all-headers gnus-show-all-headers)
5909           (gnus-article-show-all-headers))
5910         'old))))
5911
5912 (defun gnus-summary-set-current-mark (&optional current-mark)
5913   "Obsolete function."
5914   nil)
5915
5916 (defun gnus-summary-next-article (&optional unread subject backward push)
5917   "Select the next article.
5918 If UNREAD, only unread articles are selected.
5919 If SUBJECT, only articles with SUBJECT are selected.
5920 If BACKWARD, the previous article is selected instead of the next."
5921   (interactive "P")
5922   (cond
5923    ;; Is there such an article?
5924    ((and (gnus-summary-search-forward unread subject backward)
5925          (or (gnus-summary-display-article (gnus-summary-article-number))
5926              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5927     (gnus-summary-position-point))
5928    ;; If not, we try the first unread, if that is wanted.
5929    ((and subject
5930          gnus-auto-select-same
5931          (gnus-summary-first-unread-article))
5932     (gnus-summary-position-point)
5933     (gnus-message 6 "Wrapped"))
5934    ;; Try to get next/previous article not displayed in this group.
5935    ((and gnus-auto-extend-newsgroup
5936          (not unread) (not subject))
5937     (gnus-summary-goto-article
5938      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5939      nil (count-lines (point-min) (point))))
5940    ;; Go to next/previous group.
5941    (t
5942     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5943       (gnus-summary-jump-to-group gnus-newsgroup-name))
5944     (let ((cmd last-command-char)
5945           (point
5946            (save-excursion
5947              (set-buffer gnus-group-buffer)
5948              (point)))
5949           (group
5950            (if (eq gnus-keep-same-level 'best)
5951                (gnus-summary-best-group gnus-newsgroup-name)
5952              (gnus-summary-search-group backward gnus-keep-same-level))))
5953       ;; For some reason, the group window gets selected.  We change
5954       ;; it back.
5955       (select-window (get-buffer-window (current-buffer)))
5956       ;; Select next unread newsgroup automagically.
5957       (cond
5958        ((or (not gnus-auto-select-next)
5959             (not cmd))
5960         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5961        ((or (eq gnus-auto-select-next 'quietly)
5962             (and (eq gnus-auto-select-next 'slightly-quietly)
5963                  push)
5964             (and (eq gnus-auto-select-next 'almost-quietly)
5965                  (gnus-summary-last-article-p)))
5966         ;; Select quietly.
5967         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5968             (gnus-summary-exit)
5969           (gnus-message 7 "No more%s articles (%s)..."
5970                         (if unread " unread" "")
5971                         (if group (concat "selecting " group)
5972                           "exiting"))
5973           (gnus-summary-next-group nil group backward)))
5974        (t
5975         (when (gnus-key-press-event-p last-input-event)
5976           (gnus-summary-walk-group-buffer
5977            gnus-newsgroup-name cmd unread backward point))))))))
5978
5979 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5980   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5981                       (?\C-p (gnus-group-prev-unread-group 1))))
5982         (cursor-in-echo-area t)
5983         keve key group ended)
5984     (save-excursion
5985       (set-buffer gnus-group-buffer)
5986       (goto-char start)
5987       (setq group
5988             (if (eq gnus-keep-same-level 'best)
5989                 (gnus-summary-best-group gnus-newsgroup-name)
5990               (gnus-summary-search-group backward gnus-keep-same-level))))
5991     (while (not ended)
5992       (gnus-message
5993        5 "No more%s articles%s" (if unread " unread" "")
5994        (if (and group
5995                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5996            (format " (Type %s for %s [%s])"
5997                    (single-key-description cmd) group
5998                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5999          (format " (Type %s to exit %s)"
6000                  (single-key-description cmd)
6001                  gnus-newsgroup-name)))
6002       ;; Confirm auto selection.
6003       (setq key (car (setq keve (gnus-read-event-char))))
6004       (setq ended t)
6005       (cond
6006        ((assq key keystrokes)
6007         (let ((obuf (current-buffer)))
6008           (switch-to-buffer gnus-group-buffer)
6009           (when group
6010             (gnus-group-jump-to-group group))
6011           (eval (cadr (assq key keystrokes)))
6012           (setq group (gnus-group-group-name))
6013           (switch-to-buffer obuf))
6014         (setq ended nil))
6015        ((equal key cmd)
6016         (if (or (not group)
6017                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6018             (gnus-summary-exit)
6019           (gnus-summary-next-group nil group backward)))
6020        (t
6021         (push (cdr keve) unread-command-events))))))
6022
6023 (defun gnus-summary-next-unread-article ()
6024   "Select unread article after current one."
6025   (interactive)
6026   (gnus-summary-next-article
6027    (or (not (eq gnus-summary-goto-unread 'never))
6028        (gnus-summary-last-article-p (gnus-summary-article-number)))
6029    (and gnus-auto-select-same
6030         (gnus-summary-article-subject))))
6031
6032 (defun gnus-summary-prev-article (&optional unread subject)
6033   "Select the article after the current one.
6034 If UNREAD is non-nil, only unread articles are selected."
6035   (interactive "P")
6036   (gnus-summary-next-article unread subject t))
6037
6038 (defun gnus-summary-prev-unread-article ()
6039   "Select unread article before current one."
6040   (interactive)
6041   (gnus-summary-prev-article
6042    (or (not (eq gnus-summary-goto-unread 'never))
6043        (gnus-summary-first-article-p (gnus-summary-article-number)))
6044    (and gnus-auto-select-same
6045         (gnus-summary-article-subject))))
6046
6047 (defun gnus-summary-next-page (&optional lines circular)
6048   "Show next page of the selected article.
6049 If at the end of the current article, select the next article.
6050 LINES says how many lines should be scrolled up.
6051
6052 If CIRCULAR is non-nil, go to the start of the article instead of
6053 selecting the next article when reaching the end of the current
6054 article."
6055   (interactive "P")
6056   (setq gnus-summary-buffer (current-buffer))
6057   (gnus-set-global-variables)
6058   (let ((article (gnus-summary-article-number))
6059         (article-window (get-buffer-window gnus-article-buffer t))
6060         endp)
6061     ;; If the buffer is empty, we have no article.
6062     (unless article
6063       (error "No article to select"))
6064     (gnus-configure-windows 'article)
6065     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6066         (if (and (eq gnus-summary-goto-unread 'never)
6067                  (not (gnus-summary-last-article-p article)))
6068             (gnus-summary-next-article)
6069           (gnus-summary-next-unread-article))
6070       (if (or (null gnus-current-article)
6071               (null gnus-article-current)
6072               (/= article (cdr gnus-article-current))
6073               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6074           ;; Selected subject is different from current article's.
6075           (gnus-summary-display-article article)
6076         (when article-window
6077           (gnus-eval-in-buffer-window gnus-article-buffer
6078             (setq endp (gnus-article-next-page lines)))
6079           (when endp
6080             (cond (circular
6081                    (gnus-summary-beginning-of-article))
6082                   (lines
6083                    (gnus-message 3 "End of message"))
6084                   ((null lines)
6085                    (if (and (eq gnus-summary-goto-unread 'never)
6086                             (not (gnus-summary-last-article-p article)))
6087                        (gnus-summary-next-article)
6088                      (gnus-summary-next-unread-article))))))))
6089     (gnus-summary-recenter)
6090     (gnus-summary-position-point)))
6091
6092 (defun gnus-summary-prev-page (&optional lines move)
6093   "Show previous page of selected article.
6094 Argument LINES specifies lines to be scrolled down.
6095 If MOVE, move to the previous unread article if point is at
6096 the beginning of the buffer."
6097   (interactive "P")
6098   (let ((article (gnus-summary-article-number))
6099         (article-window (get-buffer-window gnus-article-buffer t))
6100         endp)
6101     (gnus-configure-windows 'article)
6102     (if (or (null gnus-current-article)
6103             (null gnus-article-current)
6104             (/= article (cdr gnus-article-current))
6105             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6106         ;; Selected subject is different from current article's.
6107         (gnus-summary-display-article article)
6108       (gnus-summary-recenter)
6109       (when article-window
6110         (gnus-eval-in-buffer-window gnus-article-buffer
6111           (setq endp (gnus-article-prev-page lines)))
6112         (when (and move endp)
6113           (cond (lines
6114                  (gnus-message 3 "Beginning of message"))
6115                 ((null lines)
6116                  (if (and (eq gnus-summary-goto-unread 'never)
6117                           (not (gnus-summary-first-article-p article)))
6118                      (gnus-summary-prev-article)
6119                    (gnus-summary-prev-unread-article))))))))
6120   (gnus-summary-position-point))
6121
6122 (defun gnus-summary-prev-page-or-article (&optional lines)
6123   "Show previous page of selected article.
6124 Argument LINES specifies lines to be scrolled down.
6125 If at the beginning of the article, go to the next article."
6126   (interactive "P")
6127   (gnus-summary-prev-page lines t))
6128
6129 (defun gnus-summary-scroll-up (lines)
6130   "Scroll up (or down) one line current article.
6131 Argument LINES specifies lines to be scrolled up (or down if negative)."
6132   (interactive "p")
6133   (gnus-configure-windows 'article)
6134   (gnus-summary-show-thread)
6135   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6136     (gnus-eval-in-buffer-window gnus-article-buffer
6137       (cond ((> lines 0)
6138              (when (gnus-article-next-page lines)
6139                (gnus-message 3 "End of message")))
6140             ((< lines 0)
6141              (gnus-article-prev-page (- lines))))))
6142   (gnus-summary-recenter)
6143   (gnus-summary-position-point))
6144
6145 (defun gnus-summary-scroll-down (lines)
6146   "Scroll down (or up) one line current article.
6147 Argument LINES specifies lines to be scrolled down (or up if negative)."
6148   (interactive "p")
6149   (gnus-summary-scroll-up (- lines)))
6150
6151 (defun gnus-summary-next-same-subject ()
6152   "Select next article which has the same subject as current one."
6153   (interactive)
6154   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6155
6156 (defun gnus-summary-prev-same-subject ()
6157   "Select previous article which has the same subject as current one."
6158   (interactive)
6159   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6160
6161 (defun gnus-summary-next-unread-same-subject ()
6162   "Select next unread article which has the same subject as current one."
6163   (interactive)
6164   (gnus-summary-next-article t (gnus-summary-article-subject)))
6165
6166 (defun gnus-summary-prev-unread-same-subject ()
6167   "Select previous unread article which has the same subject as current one."
6168   (interactive)
6169   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6170
6171 (defun gnus-summary-first-unread-article ()
6172   "Select the first unread article.
6173 Return nil if there are no unread articles."
6174   (interactive)
6175   (prog1
6176       (when (gnus-summary-first-subject t)
6177         (gnus-summary-show-thread)
6178         (gnus-summary-first-subject t)
6179         (gnus-summary-display-article (gnus-summary-article-number)))
6180     (gnus-summary-position-point)))
6181
6182 (defun gnus-summary-first-unread-subject ()
6183   "Place the point on the subject line of the first unread article.
6184 Return nil if there are no unread articles."
6185   (interactive)
6186   (prog1
6187       (when (gnus-summary-first-subject t)
6188         (gnus-summary-show-thread)
6189         (gnus-summary-first-subject t))
6190     (gnus-summary-position-point)))
6191
6192 (defun gnus-summary-first-article ()
6193   "Select the first article.
6194 Return nil if there are no articles."
6195   (interactive)
6196   (prog1
6197       (when (gnus-summary-first-subject)
6198         (gnus-summary-show-thread)
6199         (gnus-summary-first-subject)
6200         (gnus-summary-display-article (gnus-summary-article-number)))
6201     (gnus-summary-position-point)))
6202
6203 (defun gnus-summary-best-unread-article ()
6204   "Select the unread article with the highest score."
6205   (interactive)
6206   (let ((best -1000000)
6207         (data gnus-newsgroup-data)
6208         article score)
6209     (while data
6210       (and (gnus-data-unread-p (car data))
6211            (> (setq score
6212                     (gnus-summary-article-score (gnus-data-number (car data))))
6213               best)
6214            (setq best score
6215                  article (gnus-data-number (car data))))
6216       (setq data (cdr data)))
6217     (prog1
6218         (if article
6219             (gnus-summary-goto-article article)
6220           (error "No unread articles"))
6221       (gnus-summary-position-point))))
6222
6223 (defun gnus-summary-last-subject ()
6224   "Go to the last displayed subject line in the group."
6225   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6226     (when article
6227       (gnus-summary-goto-subject article))))
6228
6229 (defun gnus-summary-goto-article (article &optional all-headers force)
6230   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6231 If ALL-HEADERS is non-nil, no header lines are hidden.
6232 If FORCE, go to the article even if it isn't displayed.  If FORCE
6233 is a number, it is the line the article is to be displayed on."
6234   (interactive
6235    (list
6236     (completing-read
6237      "Article number or Message-ID: "
6238      (mapcar (lambda (number) (list (int-to-string number)))
6239              gnus-newsgroup-limit))
6240     current-prefix-arg
6241     t))
6242   (prog1
6243       (if (and (stringp article)
6244                (string-match "@" article))
6245           (gnus-summary-refer-article article)
6246         (when (stringp article)
6247           (setq article (string-to-number article)))
6248         (if (gnus-summary-goto-subject article force)
6249             (gnus-summary-display-article article all-headers)
6250           (gnus-message 4 "Couldn't go to article %s" article) nil))
6251     (gnus-summary-position-point)))
6252
6253 (defun gnus-summary-goto-last-article ()
6254   "Go to the previously read article."
6255   (interactive)
6256   (prog1
6257       (when gnus-last-article
6258         (gnus-summary-goto-article gnus-last-article nil t))
6259     (gnus-summary-position-point)))
6260
6261 (defun gnus-summary-pop-article (number)
6262   "Pop one article off the history and go to the previous.
6263 NUMBER articles will be popped off."
6264   (interactive "p")
6265   (let (to)
6266     (setq gnus-newsgroup-history
6267           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6268     (if to
6269         (gnus-summary-goto-article (car to) nil t)
6270       (error "Article history empty")))
6271   (gnus-summary-position-point))
6272
6273 ;; Summary commands and functions for limiting the summary buffer.
6274
6275 (defun gnus-summary-limit-to-articles (n)
6276   "Limit the summary buffer to the next N articles.
6277 If not given a prefix, use the process marked articles instead."
6278   (interactive "P")
6279   (prog1
6280       (let ((articles (gnus-summary-work-articles n)))
6281         (setq gnus-newsgroup-processable nil)
6282         (gnus-summary-limit articles))
6283     (gnus-summary-position-point)))
6284
6285 (defun gnus-summary-pop-limit (&optional total)
6286   "Restore the previous limit.
6287 If given a prefix, remove all limits."
6288   (interactive "P")
6289   (when total
6290     (setq gnus-newsgroup-limits
6291           (list (mapcar (lambda (h) (mail-header-number h))
6292                         gnus-newsgroup-headers))))
6293   (unless gnus-newsgroup-limits
6294     (error "No limit to pop"))
6295   (prog1
6296       (gnus-summary-limit nil 'pop)
6297     (gnus-summary-position-point)))
6298
6299 (defun gnus-summary-limit-to-subject (subject &optional header)
6300   "Limit the summary buffer to articles that have subjects that match a regexp."
6301   (interactive "sLimit to subject (regexp): ")
6302   (unless header
6303     (setq header "subject"))
6304   (when (not (equal "" subject))
6305     (prog1
6306         (let ((articles (gnus-summary-find-matching
6307                          (or header "subject") subject 'all)))
6308           (unless articles
6309             (error "Found no matches for \"%s\"" subject))
6310           (gnus-summary-limit articles))
6311       (gnus-summary-position-point))))
6312
6313 (defun gnus-summary-limit-to-author (from)
6314   "Limit the summary buffer to articles that have authors that match a regexp."
6315   (interactive "sLimit to author (regexp): ")
6316   (gnus-summary-limit-to-subject from "from"))
6317
6318 (defun gnus-summary-limit-to-age (age &optional younger-p)
6319   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6320 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6321 articles that are younger than AGE days."
6322   (interactive
6323    (let ((younger current-prefix-arg)
6324          (days-got nil)
6325          days)
6326      (while (not days-got)
6327        (setq days (if younger
6328                       (read-string "Limit to articles within (in days): ")
6329                     (read-string "Limit to articles old than (in days): ")))
6330        (when (> (length days) 0)
6331          (setq days (read days)))
6332        (if (numberp days)
6333            (setq days-got t)
6334          (message "Please enter a number.")
6335          (sleep-for 1)))
6336      (list days younger)))
6337   (prog1
6338       (let ((data gnus-newsgroup-data)
6339             (cutoff (days-to-time age))
6340             articles d date is-younger)
6341         (while (setq d (pop data))
6342           (when (and (vectorp (gnus-data-header d))
6343                      (setq date (mail-header-date (gnus-data-header d))))
6344             (setq is-younger (time-less-p
6345                               (time-since (condition-case ()
6346                                               (date-to-time date)
6347                                             (error '(0 0))))
6348                               cutoff))
6349             (when (if younger-p
6350                       is-younger
6351                     (not is-younger))
6352               (push (gnus-data-number d) articles))))
6353         (gnus-summary-limit (nreverse articles)))
6354     (gnus-summary-position-point)))
6355
6356 (defun gnus-summary-limit-to-extra (header regexp)
6357   "Limit the summary buffer to articles that match an 'extra' header."
6358   (interactive
6359    (let ((header
6360           (intern
6361            (gnus-completing-read
6362             (symbol-name (car gnus-extra-headers))      
6363             "Limit extra header:"       
6364             (mapcar (lambda (x) 
6365                       (cons (symbol-name x) x))
6366                     gnus-extra-headers)
6367             nil                 
6368             t))))
6369      (list header
6370            (read-string (format "Limit to header %s (regexp): " header)))))
6371   (when (not (equal "" regexp))
6372     (prog1
6373         (let ((articles (gnus-summary-find-matching
6374                          (cons 'extra header) regexp 'all)))
6375           (unless articles
6376             (error "Found no matches for \"%s\"" regexp))
6377           (gnus-summary-limit articles))
6378       (gnus-summary-position-point))))
6379
6380 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6381 (make-obsolete
6382  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6383
6384 (defun gnus-summary-limit-to-unread (&optional all)
6385   "Limit the summary buffer to articles that are not marked as read.
6386 If ALL is non-nil, limit strictly to unread articles."
6387   (interactive "P")
6388   (if all
6389       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6390     (gnus-summary-limit-to-marks
6391      ;; Concat all the marks that say that an article is read and have
6392      ;; those removed.
6393      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6394            gnus-killed-mark gnus-kill-file-mark
6395            gnus-low-score-mark gnus-expirable-mark
6396            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6397            gnus-duplicate-mark gnus-souped-mark)
6398      'reverse)))
6399
6400 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6401 (make-obsolete 'gnus-summary-delete-marked-with
6402                'gnus-summary-limit-exlude-marks)
6403
6404 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6405   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6406 If REVERSE, limit the summary buffer to articles that are marked
6407 with MARKS.  MARKS can either be a string of marks or a list of marks.
6408 Returns how many articles were removed."
6409   (interactive "sMarks: ")
6410   (gnus-summary-limit-to-marks marks t))
6411
6412 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6413   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6414 If REVERSE (the prefix), limit the summary buffer to articles that are
6415 not marked with MARKS.  MARKS can either be a string of marks or a
6416 list of marks.
6417 Returns how many articles were removed."
6418   (interactive "sMarks: \nP")
6419   (prog1
6420       (let ((data gnus-newsgroup-data)
6421             (marks (if (listp marks) marks
6422                      (append marks nil))) ; Transform to list.
6423             articles)
6424         (while data
6425           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6426                   (memq (gnus-data-mark (car data)) marks))
6427             (push (gnus-data-number (car data)) articles))
6428           (setq data (cdr data)))
6429         (gnus-summary-limit articles))
6430     (gnus-summary-position-point)))
6431
6432 (defun gnus-summary-limit-to-score (&optional score)
6433   "Limit to articles with score at or above SCORE."
6434   (interactive "P")
6435   (setq score (if score
6436                   (prefix-numeric-value score)
6437                 (or gnus-summary-default-score 0)))
6438   (let ((data gnus-newsgroup-data)
6439         articles)
6440     (while data
6441       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6442                 score)
6443         (push (gnus-data-number (car data)) articles))
6444       (setq data (cdr data)))
6445     (prog1
6446         (gnus-summary-limit articles)
6447       (gnus-summary-position-point))))
6448
6449 (defun gnus-summary-limit-include-thread (id)
6450   "Display all the hidden articles that in the current thread."
6451   (interactive (list (mail-header-id (gnus-summary-article-header))))
6452   (let ((articles (gnus-articles-in-thread
6453                    (gnus-id-to-thread (gnus-root-id id)))))
6454     (prog1
6455         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6456       (gnus-summary-position-point))))
6457
6458 (defun gnus-summary-limit-include-dormant ()
6459   "Display all the hidden articles that are marked as dormant.
6460 Note that this command only works on a subset of the articles currently
6461 fetched for this group."
6462   (interactive)
6463   (unless gnus-newsgroup-dormant
6464     (error "There are no dormant articles in this group"))
6465   (prog1
6466       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6467     (gnus-summary-position-point)))
6468
6469 (defun gnus-summary-limit-exclude-dormant ()
6470   "Hide all dormant articles."
6471   (interactive)
6472   (prog1
6473       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6474     (gnus-summary-position-point)))
6475
6476 (defun gnus-summary-limit-exclude-childless-dormant ()
6477   "Hide all dormant articles that have no children."
6478   (interactive)
6479   (let ((data (gnus-data-list t))
6480         articles d children)
6481     ;; Find all articles that are either not dormant or have
6482     ;; children.
6483     (while (setq d (pop data))
6484       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6485                 (and (setq children
6486                            (gnus-article-children (gnus-data-number d)))
6487                      (let (found)
6488                        (while children
6489                          (when (memq (car children) articles)
6490                            (setq children nil
6491                                  found t))
6492                          (pop children))
6493                        found)))
6494         (push (gnus-data-number d) articles)))
6495     ;; Do the limiting.
6496     (prog1
6497         (gnus-summary-limit articles)
6498       (gnus-summary-position-point))))
6499
6500 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6501   "Mark all unread excluded articles as read.
6502 If ALL, mark even excluded ticked and dormants as read."
6503   (interactive "P")
6504   (let ((articles (gnus-sorted-complement
6505                    (sort
6506                     (mapcar (lambda (h) (mail-header-number h))
6507                             gnus-newsgroup-headers)
6508                     '<)
6509                    (sort gnus-newsgroup-limit '<)))
6510         article)
6511     (setq gnus-newsgroup-unreads
6512           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6513     (if all
6514         (setq gnus-newsgroup-dormant nil
6515               gnus-newsgroup-marked nil
6516               gnus-newsgroup-reads
6517               (nconc
6518                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6519                gnus-newsgroup-reads))
6520       (while (setq article (pop articles))
6521         (unless (or (memq article gnus-newsgroup-dormant)
6522                     (memq article gnus-newsgroup-marked))
6523           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6524
6525 (defun gnus-summary-limit (articles &optional pop)
6526   (if pop
6527       ;; We pop the previous limit off the stack and use that.
6528       (setq articles (car gnus-newsgroup-limits)
6529             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6530     ;; We use the new limit, so we push the old limit on the stack.
6531     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6532   ;; Set the limit.
6533   (setq gnus-newsgroup-limit articles)
6534   (let ((total (length gnus-newsgroup-data))
6535         (data (gnus-data-find-list (gnus-summary-article-number)))
6536         (gnus-summary-mark-below nil)   ; Inhibit this.
6537         found)
6538     ;; This will do all the work of generating the new summary buffer
6539     ;; according to the new limit.
6540     (gnus-summary-prepare)
6541     ;; Hide any threads, possibly.
6542     (and gnus-show-threads
6543          gnus-thread-hide-subtree
6544          (gnus-summary-hide-all-threads))
6545     ;; Try to return to the article you were at, or one in the
6546     ;; neighborhood.
6547     (when data
6548       ;; We try to find some article after the current one.
6549       (while data
6550         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6551           (setq data nil
6552                 found t))
6553         (setq data (cdr data))))
6554     (unless found
6555       ;; If there is no data, that means that we were after the last
6556       ;; article.  The same goes when we can't find any articles
6557       ;; after the current one.
6558       (goto-char (point-max))
6559       (gnus-summary-find-prev))
6560     (gnus-set-mode-line 'summary)
6561     ;; We return how many articles were removed from the summary
6562     ;; buffer as a result of the new limit.
6563     (- total (length gnus-newsgroup-data))))
6564
6565 (defsubst gnus-invisible-cut-children (threads)
6566   (let ((num 0))
6567     (while threads
6568       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6569         (incf num))
6570       (pop threads))
6571     (< num 2)))
6572
6573 (defsubst gnus-cut-thread (thread)
6574   "Go forwards in the thread until we find an article that we want to display."
6575   (when (or (eq gnus-fetch-old-headers 'some)
6576             (eq gnus-fetch-old-headers 'invisible)
6577             (numberp gnus-fetch-old-headers)
6578             (eq gnus-build-sparse-threads 'some)
6579             (eq gnus-build-sparse-threads 'more))
6580     ;; Deal with old-fetched headers and sparse threads.
6581     (while (and
6582             thread
6583             (or
6584              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6585              (gnus-summary-article-ancient-p
6586               (mail-header-number (car thread))))
6587             (if (or (<= (length (cdr thread)) 1)
6588                     (eq gnus-fetch-old-headers 'invisible))
6589                 (setq gnus-newsgroup-limit
6590                       (delq (mail-header-number (car thread))
6591                             gnus-newsgroup-limit)
6592                       thread (cadr thread))
6593               (when (gnus-invisible-cut-children (cdr thread))
6594                 (let ((th (cdr thread)))
6595                   (while th
6596                     (if (memq (mail-header-number (caar th))
6597                               gnus-newsgroup-limit)
6598                         (setq thread (car th)
6599                               th nil)
6600                       (setq th (cdr th))))))))))
6601   thread)
6602
6603 (defun gnus-cut-threads (threads)
6604   "Cut off all uninteresting articles from the beginning of threads."
6605   (when (or (eq gnus-fetch-old-headers 'some)
6606             (eq gnus-fetch-old-headers 'invisible)
6607             (numberp gnus-fetch-old-headers)
6608             (eq gnus-build-sparse-threads 'some)
6609             (eq gnus-build-sparse-threads 'more))
6610     (let ((th threads))
6611       (while th
6612         (setcar th (gnus-cut-thread (car th)))
6613         (setq th (cdr th)))))
6614   ;; Remove nixed out threads.
6615   (delq nil threads))
6616
6617 (defun gnus-summary-initial-limit (&optional show-if-empty)
6618   "Figure out what the initial limit is supposed to be on group entry.
6619 This entails weeding out unwanted dormants, low-scored articles,
6620 fetch-old-headers verbiage, and so on."
6621   ;; Most groups have nothing to remove.
6622   (if (or gnus-inhibit-limiting
6623           (and (null gnus-newsgroup-dormant)
6624                (not (eq gnus-fetch-old-headers 'some))
6625                (not (numberp gnus-fetch-old-headers))
6626                (not (eq gnus-fetch-old-headers 'invisible))
6627                (null gnus-summary-expunge-below)
6628                (not (eq gnus-build-sparse-threads 'some))
6629                (not (eq gnus-build-sparse-threads 'more))
6630                (null gnus-thread-expunge-below)
6631                (not gnus-use-nocem)))
6632       ()                                ; Do nothing.
6633     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6634     (setq gnus-newsgroup-limit nil)
6635     (mapatoms
6636      (lambda (node)
6637        (unless (car (symbol-value node))
6638          ;; These threads have no parents -- they are roots.
6639          (let ((nodes (cdr (symbol-value node)))
6640                thread)
6641            (while nodes
6642              (if (and gnus-thread-expunge-below
6643                       (< (gnus-thread-total-score (car nodes))
6644                          gnus-thread-expunge-below))
6645                  (gnus-expunge-thread (pop nodes))
6646                (setq thread (pop nodes))
6647                (gnus-summary-limit-children thread))))))
6648      gnus-newsgroup-dependencies)
6649     ;; If this limitation resulted in an empty group, we might
6650     ;; pop the previous limit and use it instead.
6651     (when (and (not gnus-newsgroup-limit)
6652                show-if-empty)
6653       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6654     gnus-newsgroup-limit))
6655
6656 (defun gnus-summary-limit-children (thread)
6657   "Return 1 if this subthread is visible and 0 if it is not."
6658   ;; First we get the number of visible children to this thread.  This
6659   ;; is done by recursing down the thread using this function, so this
6660   ;; will really go down to a leaf article first, before slowly
6661   ;; working its way up towards the root.
6662   (when thread
6663     (let ((children
6664            (if (cdr thread)
6665                (apply '+ (mapcar 'gnus-summary-limit-children
6666                                  (cdr thread)))
6667              0))
6668           (number (mail-header-number (car thread)))
6669           score)
6670       (if (and
6671            (not (memq number gnus-newsgroup-marked))
6672            (or
6673             ;; If this article is dormant and has absolutely no visible
6674             ;; children, then this article isn't visible.
6675             (and (memq number gnus-newsgroup-dormant)
6676                  (zerop children))
6677             ;; If this is "fetch-old-headered" and there is no
6678             ;; visible children, then we don't want this article.
6679             (and (or (eq gnus-fetch-old-headers 'some)
6680                      (numberp gnus-fetch-old-headers))
6681                  (gnus-summary-article-ancient-p number)
6682                  (zerop children))
6683             ;; If this is "fetch-old-headered" and `invisible', then
6684             ;; we don't want this article.
6685             (and (eq gnus-fetch-old-headers 'invisible)
6686                  (gnus-summary-article-ancient-p number))
6687             ;; If this is a sparsely inserted article with no children,
6688             ;; we don't want it.
6689             (and (eq gnus-build-sparse-threads 'some)
6690                  (gnus-summary-article-sparse-p number)
6691                  (zerop children))
6692             ;; If we use expunging, and this article is really
6693             ;; low-scored, then we don't want this article.
6694             (when (and gnus-summary-expunge-below
6695                        (< (setq score
6696                                 (or (cdr (assq number gnus-newsgroup-scored))
6697                                     gnus-summary-default-score))
6698                           gnus-summary-expunge-below))
6699               ;; We increase the expunge-tally here, but that has
6700               ;; nothing to do with the limits, really.
6701               (incf gnus-newsgroup-expunged-tally)
6702               ;; We also mark as read here, if that's wanted.
6703               (when (and gnus-summary-mark-below
6704                          (< score gnus-summary-mark-below))
6705                 (setq gnus-newsgroup-unreads
6706                       (delq number gnus-newsgroup-unreads))
6707                 (if gnus-newsgroup-auto-expire
6708                     (push number gnus-newsgroup-expirable)
6709                   (push (cons number gnus-low-score-mark)
6710                         gnus-newsgroup-reads)))
6711               t)
6712             ;; Check NoCeM things.
6713             (if (and gnus-use-nocem
6714                      (gnus-nocem-unwanted-article-p
6715                       (mail-header-id (car thread))))
6716                 (progn
6717                   (setq gnus-newsgroup-unreads
6718                         (delq number gnus-newsgroup-unreads))
6719                   t))))
6720           ;; Nope, invisible article.
6721           0
6722         ;; Ok, this article is to be visible, so we add it to the limit
6723         ;; and return 1.
6724         (push number gnus-newsgroup-limit)
6725         1))))
6726
6727 (defun gnus-expunge-thread (thread)
6728   "Mark all articles in THREAD as read."
6729   (let* ((number (mail-header-number (car thread))))
6730     (incf gnus-newsgroup-expunged-tally)
6731     ;; We also mark as read here, if that's wanted.
6732     (setq gnus-newsgroup-unreads
6733           (delq number gnus-newsgroup-unreads))
6734     (if gnus-newsgroup-auto-expire
6735         (push number gnus-newsgroup-expirable)
6736       (push (cons number gnus-low-score-mark)
6737             gnus-newsgroup-reads)))
6738   ;; Go recursively through all subthreads.
6739   (mapcar 'gnus-expunge-thread (cdr thread)))
6740
6741 ;; Summary article oriented commands
6742
6743 (defun gnus-summary-refer-parent-article (n)
6744   "Refer parent article N times.
6745 If N is negative, go to ancestor -N instead.
6746 The difference between N and the number of articles fetched is returned."
6747   (interactive "p")
6748   (let ((skip 1)
6749         error header ref)
6750     (when (not (natnump n))
6751       (setq skip (abs n)
6752             n 1))
6753     (while (and (> n 0)
6754                 (not error))
6755       (setq header (gnus-summary-article-header))
6756       (if (and (eq (mail-header-number header)
6757                    (cdr gnus-article-current))
6758                (equal gnus-newsgroup-name
6759                       (car gnus-article-current)))
6760           ;; If we try to find the parent of the currently
6761           ;; displayed article, then we take a look at the actual
6762           ;; References header, since this is slightly more
6763           ;; reliable than the References field we got from the
6764           ;; server.
6765           (save-excursion
6766             (set-buffer gnus-original-article-buffer)
6767             (nnheader-narrow-to-headers)
6768             (unless (setq ref (message-fetch-field "references"))
6769               (setq ref (message-fetch-field "in-reply-to")))
6770             (widen))
6771         (setq ref
6772               ;; It's not the current article, so we take a bet on
6773               ;; the value we got from the server.
6774               (mail-header-references header)))
6775       (if (and ref
6776                (not (equal ref "")))
6777           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6778             (gnus-message 1 "Couldn't find parent"))
6779         (gnus-message 1 "No references in article %d"
6780                       (gnus-summary-article-number))
6781         (setq error t))
6782       (decf n))
6783     (gnus-summary-position-point)
6784     n))
6785
6786 (defun gnus-summary-refer-references ()
6787   "Fetch all articles mentioned in the References header.
6788 Return the number of articles fetched."
6789   (interactive)
6790   (let ((ref (mail-header-references (gnus-summary-article-header)))
6791         (current (gnus-summary-article-number))
6792         (n 0))
6793     (if (or (not ref)
6794             (equal ref ""))
6795         (error "No References in the current article")
6796       ;; For each Message-ID in the References header...
6797       (while (string-match "<[^>]*>" ref)
6798         (incf n)
6799         ;; ... fetch that article.
6800         (gnus-summary-refer-article
6801          (prog1 (match-string 0 ref)
6802            (setq ref (substring ref (match-end 0))))))
6803       (gnus-summary-goto-subject current)
6804       (gnus-summary-position-point)
6805       n)))
6806
6807 (defun gnus-summary-refer-thread (&optional limit)
6808   "Fetch all articles in the current thread.
6809 If LIMIT (the numerical prefix), fetch that many old headers instead
6810 of what's specified by the `gnus-refer-thread-limit' variable."
6811   (interactive "P")
6812   (let ((id (mail-header-id (gnus-summary-article-header)))
6813         (limit (if limit (prefix-numeric-value limit)
6814                  gnus-refer-thread-limit)))
6815     ;; We want to fetch LIMIT *old* headers, but we also have to
6816     ;; re-fetch all the headers in the current buffer, because many of
6817     ;; them may be undisplayed.  So we adjust LIMIT.
6818     (when (numberp limit)
6819       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6820     (unless (eq gnus-fetch-old-headers 'invisible)
6821       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6822       ;; Retrieve the headers and read them in.
6823       (if (eq (gnus-retrieve-headers
6824                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6825               'nov)
6826           (gnus-build-all-threads)
6827         (error "Can't fetch thread from backends that don't support NOV"))
6828       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6829     (gnus-summary-limit-include-thread id)))
6830
6831 (defun gnus-summary-refer-article (message-id)
6832   "Fetch an article specified by MESSAGE-ID."
6833   (interactive "sMessage-ID: ")
6834   (when (and (stringp message-id)
6835              (not (zerop (length message-id))))
6836     ;; Construct the correct Message-ID if necessary.
6837     ;; Suggested by tale@pawl.rpi.edu.
6838     (unless (string-match "^<" message-id)
6839       (setq message-id (concat "<" message-id)))
6840     (unless (string-match ">$" message-id)
6841       (setq message-id (concat message-id ">")))
6842     (let* ((header (gnus-id-to-header message-id))
6843            (sparse (and header
6844                         (gnus-summary-article-sparse-p
6845                          (mail-header-number header))
6846                         (memq (mail-header-number header)
6847                               gnus-newsgroup-limit)))
6848            number)
6849       (cond
6850        ;; If the article is present in the buffer we just go to it.
6851        ((and header
6852              (or (not (gnus-summary-article-sparse-p
6853                        (mail-header-number header)))
6854                  sparse))
6855         (prog1
6856             (gnus-summary-goto-article
6857              (mail-header-number header) nil t)
6858           (when sparse
6859             (gnus-summary-update-article (mail-header-number header)))))
6860        (t
6861         ;; We fetch the article.
6862         (catch 'found
6863           (dolist (gnus-override-method (gnus-refer-article-methods))
6864             (gnus-check-server gnus-override-method)
6865             ;; Fetch the header, and display the article.
6866             (when (setq number (gnus-summary-insert-subject message-id))
6867               (gnus-summary-select-article nil nil nil number)
6868               (throw 'found t)))
6869           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6870
6871 (defun gnus-refer-article-methods ()
6872   "Return a list of referrable methods."
6873   (cond
6874    ;; No method, so we default to current and native.
6875    ((null gnus-refer-article-method)
6876     (list gnus-current-select-method gnus-select-method))
6877    ;; Current.
6878    ((eq 'current gnus-refer-article-method)
6879     (list gnus-current-select-method))
6880    ;; List of select methods.
6881    ((not (stringp (cadr gnus-refer-article-method)))
6882     (let (out)
6883       (dolist (method gnus-refer-article-method)
6884         (push (if (eq 'current method)
6885                   gnus-current-select-method
6886                 method)
6887               out))
6888       (nreverse out)))
6889    ;; One single select method.
6890    (t
6891     (list gnus-refer-article-method))))
6892
6893 (defun gnus-summary-edit-parameters ()
6894   "Edit the group parameters of the current group."
6895   (interactive)
6896   (gnus-group-edit-group gnus-newsgroup-name 'params))
6897
6898 (defun gnus-summary-customize-parameters ()
6899   "Customize the group parameters of the current group."
6900   (interactive)
6901   (gnus-group-customize gnus-newsgroup-name))
6902
6903 (defun gnus-summary-enter-digest-group (&optional force)
6904   "Enter an nndoc group based on the current article.
6905 If FORCE, force a digest interpretation.  If not, try
6906 to guess what the document format is."
6907   (interactive "P")
6908   (let ((conf gnus-current-window-configuration))
6909     (save-excursion
6910       (gnus-summary-select-article))
6911     (setq gnus-current-window-configuration conf)
6912     (let* ((name (format "%s-%d"
6913                          (gnus-group-prefixed-name
6914                           gnus-newsgroup-name (list 'nndoc ""))
6915                          (save-excursion
6916                            (set-buffer gnus-summary-buffer)
6917                            gnus-current-article)))
6918            (ogroup gnus-newsgroup-name)
6919            (params (append (gnus-info-params (gnus-get-info ogroup))
6920                            (list (cons 'to-group ogroup))
6921                            (list (cons 'save-article-group ogroup))))
6922            (case-fold-search t)
6923            (buf (current-buffer))
6924            dig to-address)
6925       (save-excursion
6926         (set-buffer gnus-original-article-buffer)
6927         ;; Have the digest group inherit the main mail address of
6928         ;; the parent article.
6929         (when (setq to-address (or (message-fetch-field "reply-to")
6930                                    (message-fetch-field "from")))
6931           (setq params (append (list (cons 'to-address to-address)))))
6932         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6933         (insert-buffer-substring gnus-original-article-buffer)
6934         ;; Remove lines that may lead nndoc to misinterpret the
6935         ;; document type.
6936         (narrow-to-region
6937          (goto-char (point-min))
6938          (or (search-forward "\n\n" nil t) (point)))
6939         (goto-char (point-min))
6940         (delete-matching-lines "^Path:\\|^From ")
6941         (widen))
6942       (unwind-protect
6943           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6944                     (gnus-newsgroup-ephemeral-ignored-charsets
6945                      gnus-newsgroup-ignored-charsets))
6946                 (gnus-group-read-ephemeral-group
6947                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6948                               (nndoc-article-type
6949                                ,(if force 'mbox 'guess))) t))
6950               ;; Make all postings to this group go to the parent group.
6951               (nconc (gnus-info-params (gnus-get-info name))
6952                      params)
6953             ;; Couldn't select this doc group.
6954             (switch-to-buffer buf)
6955             (gnus-set-global-variables)
6956             (gnus-configure-windows 'summary)
6957             (gnus-message 3 "Article couldn't be entered?"))
6958         (kill-buffer dig)))))
6959
6960 (defun gnus-summary-read-document (n)
6961   "Open a new group based on the current article(s).
6962 This will allow you to read digests and other similar
6963 documents as newsgroups.
6964 Obeys the standard process/prefix convention."
6965   (interactive "P")
6966   (let* ((articles (gnus-summary-work-articles n))
6967          (ogroup gnus-newsgroup-name)
6968          (params (append (gnus-info-params (gnus-get-info ogroup))
6969                          (list (cons 'to-group ogroup))))
6970          article group egroup groups vgroup)
6971     (while (setq article (pop articles))
6972       (setq group (format "%s-%d" gnus-newsgroup-name article))
6973       (gnus-summary-remove-process-mark article)
6974       (when (gnus-summary-display-article article)
6975         (save-excursion
6976           (with-temp-buffer
6977             (insert-buffer-substring gnus-original-article-buffer)
6978             ;; Remove some headers that may lead nndoc to make
6979             ;; the wrong guess.
6980             (message-narrow-to-head)
6981             (goto-char (point-min))
6982             (delete-matching-lines "^\\(Path\\):\\|^From ")
6983             (widen)
6984             (if (setq egroup
6985                       (gnus-group-read-ephemeral-group
6986                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6987                                      (nndoc-article-type guess))
6988                        t nil t))
6989                 (progn
6990                   ;; Make all postings to this group go to the parent group.
6991                   (nconc (gnus-info-params (gnus-get-info egroup))
6992                          params)
6993                   (push egroup groups))
6994               ;; Couldn't select this doc group.
6995               (gnus-error 3 "Article couldn't be entered"))))))
6996     ;; Now we have selected all the documents.
6997     (cond
6998      ((not groups)
6999       (error "None of the articles could be interpreted as documents"))
7000      ((gnus-group-read-ephemeral-group
7001        (setq vgroup (format
7002                      "nnvirtual:%s-%s" gnus-newsgroup-name
7003                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7004        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7005        t
7006        (cons (current-buffer) 'summary)))
7007      (t
7008       (error "Couldn't select virtual nndoc group")))))
7009
7010 (defun gnus-summary-isearch-article (&optional regexp-p)
7011   "Do incremental search forward on the current article.
7012 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7013   (interactive "P")
7014   (let* ((gnus-inhibit-treatment t)
7015          (old (gnus-summary-select-article)))
7016     (gnus-configure-windows 'article)
7017     (gnus-eval-in-buffer-window gnus-article-buffer
7018       (save-restriction
7019         (widen)
7020         (when (eq 'old old)
7021           (gnus-article-show-all-headers))
7022         (goto-char (point-min))
7023         (isearch-forward regexp-p)))))
7024
7025 (defun gnus-summary-search-article-forward (regexp &optional backward)
7026   "Search for an article containing REGEXP forward.
7027 If BACKWARD, search backward instead."
7028   (interactive
7029    (list (read-string
7030           (format "Search article %s (regexp%s): "
7031                   (if current-prefix-arg "backward" "forward")
7032                   (if gnus-last-search-regexp
7033                       (concat ", default " gnus-last-search-regexp)
7034                     "")))
7035          current-prefix-arg))
7036   (if (string-equal regexp "")
7037       (setq regexp (or gnus-last-search-regexp ""))
7038     (setq gnus-last-search-regexp regexp))
7039   (if (gnus-summary-search-article regexp backward)
7040       (gnus-summary-show-thread)
7041     (error "Search failed: \"%s\"" regexp)))
7042
7043 (defun gnus-summary-search-article-backward (regexp)
7044   "Search for an article containing REGEXP backward."
7045   (interactive
7046    (list (read-string
7047           (format "Search article backward (regexp%s): "
7048                   (if gnus-last-search-regexp
7049                       (concat ", default " gnus-last-search-regexp)
7050                     "")))))
7051   (gnus-summary-search-article-forward regexp 'backward))
7052
7053 (eval-when-compile
7054   (defmacro gnus-summary-search-article-position-point (regexp backward)
7055     "Dehighlight the last matched text and goto the beginning position."
7056     (` (if (and gnus-summary-search-article-matched-data
7057                 (let ((text (caddr gnus-summary-search-article-matched-data))
7058                       (inhibit-read-only t)
7059                       buffer-read-only)
7060                   (delete-region
7061                    (goto-char (car gnus-summary-search-article-matched-data))
7062                    (cadr gnus-summary-search-article-matched-data))
7063                   (insert text)
7064                   (string-match (, regexp) text)))
7065            (if (, backward) (beginning-of-line) (end-of-line))
7066          (goto-char (if (, backward) (point-max) (point-min))))))
7067
7068   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7069     "Place point where X-Face image is displayed."
7070     (if (featurep 'xemacs)
7071         (` (let ((end (if (search-forward "\n\n" nil t)
7072                           (goto-char (1- (point)))
7073                         (point-min)))
7074                  extent)
7075              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7076              (unless (and (re-search-forward "^From:" end t)
7077                           (setq extent (extent-at (point)))
7078                           (extent-begin-glyph extent))
7079                (goto-char (, opoint)))))
7080       (` (let ((end (if (search-forward "\n\n" nil t)
7081                         (goto-char (1- (point)))
7082                       (point-min))))
7083            (goto-char
7084             (or (text-property-any (or (search-backward "\n\n" nil t)
7085                                        (point-min))
7086                                    end 'x-face-mule-bitmap-image t)
7087                 (, opoint)))))))
7088
7089   (defmacro gnus-summary-search-article-highlight-matched-text
7090     (backward treated x-face)
7091     "Highlight matched text in the function `gnus-summary-search-article'."
7092     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7093              (end (set-marker (make-marker) (match-end 0)))
7094              (inhibit-read-only t)
7095              buffer-read-only)
7096          (unless treated
7097            (let ((,@
7098                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7099                     (mapcar
7100                      (lambda (item) (setq items (delq item items)))
7101                      '(gnus-treat-buttonize
7102                        gnus-treat-fill-article
7103                        gnus-treat-fill-long-lines
7104                        gnus-treat-emphasize
7105                        gnus-treat-highlight-headers
7106                        gnus-treat-highlight-citation
7107                        gnus-treat-highlight-signature
7108                        gnus-treat-overstrike
7109                        gnus-treat-display-xface
7110                        gnus-treat-buttonize-head
7111                        gnus-treat-decode-article-as-default-mime-charset))
7112                     (static-if (featurep 'xemacs)
7113                         items
7114                       (cons '(x-face-mule-delete-x-face-field
7115                               (quote never))
7116                             items))))
7117                  (gnus-treat-display-xface
7118                   (when (, x-face) gnus-treat-display-xface)))
7119              (gnus-article-prepare-mime-display)))
7120          (goto-char (if (, backward) start end))
7121          (when (, x-face)
7122            (gnus-summary-search-article-highlight-goto-x-face (point)))
7123          (setq gnus-summary-search-article-matched-data
7124                (list start end (buffer-substring start end)))
7125          (unless (eq start end);; matched text has been deleted. :-<
7126            (put-text-property start end 'face
7127                               (or (find-face 'isearch)
7128                                   'secondary-selection))))))
7129   )
7130
7131 (defun gnus-summary-search-article (regexp &optional backward)
7132   "Search for an article containing REGEXP.
7133 Optional argument BACKWARD means do search for backward.
7134 `gnus-select-article-hook' is not called during the search."
7135   ;; We have to require this here to make sure that the following
7136   ;; dynamic binding isn't shadowed by autoloading.
7137   (require 'gnus-async)
7138   (require 'gnus-art)
7139   (let ((gnus-select-article-hook nil)  ;Disable hook.
7140         (gnus-article-display-hook nil)
7141         (gnus-article-prepare-hook nil)
7142         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7143         (gnus-use-article-prefetch nil)
7144         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7145         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7146         (sum (current-buffer))
7147         (found nil)
7148         point treated)
7149     (gnus-save-hidden-threads
7150       (static-if (featurep 'xemacs)
7151           (let ((gnus-inhibit-treatment t))
7152             (setq treated (eq 'old (gnus-summary-select-article)))
7153             (when (and treated
7154                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7155                                  (window-live-p (get-buffer-window
7156                                                  gnus-article-buffer t)))))
7157               (gnus-summary-select-article nil t)
7158               (setq treated nil)))
7159         (let ((gnus-inhibit-treatment t)
7160               (x-face-mule-delete-x-face-field 'never))
7161           (setq treated (eq 'old (gnus-summary-select-article)))
7162           (when (and treated
7163                      (not
7164                       (and (gnus-buffer-live-p gnus-article-buffer)
7165                            (window-live-p (get-buffer-window
7166                                            gnus-article-buffer t))
7167                            (or (not (string-match "^\\^X-Face:" regexp))
7168                                (with-current-buffer gnus-article-buffer
7169                                  gnus-summary-search-article-matched-data)))))
7170             (gnus-summary-select-article nil t)
7171             (setq treated nil))))
7172       (set-buffer gnus-article-buffer)
7173       (widen)
7174       (if treated
7175           (progn
7176             (gnus-article-show-all-headers)
7177             (gnus-summary-search-article-position-point regexp backward))
7178         (goto-char (if backward (point-max) (point-min))))
7179       (while (not found)
7180         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7181         (if (if backward
7182                 (re-search-backward regexp nil t)
7183               (re-search-forward regexp nil t))
7184             ;; We found the regexp.
7185             (progn
7186               (gnus-summary-search-article-highlight-matched-text
7187                backward treated (string-match "^\\^X-Face:" regexp))
7188               (setq found 'found)
7189               (forward-line
7190                (/ (- 2 (window-height
7191                         (get-buffer-window gnus-article-buffer t)))
7192                   2))
7193               (set-window-start
7194                (get-buffer-window (current-buffer))
7195                (point))
7196               (set-buffer sum)
7197               (setq point (point)))
7198           ;; We didn't find it, so we go to the next article.
7199           (set-buffer sum)
7200           (setq found 'not)
7201           (while (eq found 'not)
7202             (if (not (if backward (gnus-summary-find-prev)
7203                        (gnus-summary-find-next)))
7204                 ;; No more articles.
7205                 (setq found t)
7206               ;; Select the next article and adjust point.
7207               (unless (gnus-summary-article-sparse-p
7208                        (gnus-summary-article-number))
7209                 (setq found nil)
7210                 (let ((gnus-inhibit-treatment t))
7211                   (gnus-summary-select-article))
7212                 (setq treated nil)
7213                 (set-buffer gnus-article-buffer)
7214                 (widen)
7215                 (goto-char (if backward (point-max) (point-min))))))))
7216       (gnus-message 7 ""))
7217     ;; Return whether we found the regexp.
7218     (when (eq found 'found)
7219       (goto-char point)
7220       (gnus-summary-show-thread)
7221       (gnus-summary-goto-subject gnus-current-article)
7222       (gnus-summary-position-point)
7223       t)))
7224
7225 (defun gnus-summary-find-matching (header regexp &optional backward unread
7226                                           not-case-fold)
7227   "Return a list of all articles that match REGEXP on HEADER.
7228 The search stars on the current article and goes forwards unless
7229 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7230 If UNREAD is non-nil, only unread articles will
7231 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7232 in the comparisons."
7233   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7234                 (gnus-data-find-list
7235                  (gnus-summary-article-number) (gnus-data-list backward))))
7236         (case-fold-search (not not-case-fold))
7237         articles d func)
7238     (if (consp header)
7239         (if (eq (car header) 'extra)
7240             (setq func
7241                   `(lambda (h)
7242                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7243                          "")))
7244           (error "%s is an invalid header" header))
7245       (unless (fboundp (intern (concat "mail-header-" header)))
7246         (error "%s is not a valid header" header))
7247       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7248     (while data
7249       (setq d (car data))
7250       (and (or (not unread)             ; We want all articles...
7251                (gnus-data-unread-p d))  ; Or just unreads.
7252            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7253            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7254            (push (gnus-data-number d) articles)) ; Success!
7255       (setq data (cdr data)))
7256     (nreverse articles)))
7257
7258 (defun gnus-summary-execute-command (header regexp command &optional backward)
7259   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7260 If HEADER is an empty string (or nil), the match is done on the entire
7261 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7262   (interactive
7263    (list (let ((completion-ignore-case t))
7264            (completing-read
7265             "Header name: "
7266             (mapcar (lambda (string) (list string))
7267                     '("Number" "Subject" "From" "Lines" "Date"
7268                       "Message-ID" "Xref" "References" "Body"))
7269             nil 'require-match))
7270          (read-string "Regexp: ")
7271          (read-key-sequence "Command: ")
7272          current-prefix-arg))
7273   (when (equal header "Body")
7274     (setq header ""))
7275   ;; Hidden thread subtrees must be searched as well.
7276   (gnus-summary-show-all-threads)
7277   ;; We don't want to change current point nor window configuration.
7278   (save-excursion
7279     (save-window-excursion
7280       (gnus-message 6 "Executing %s..." (key-description command))
7281       ;; We'd like to execute COMMAND interactively so as to give arguments.
7282       (gnus-execute header regexp
7283                     `(call-interactively ',(key-binding command))
7284                     backward)
7285       (gnus-message 6 "Executing %s...done" (key-description command)))))
7286
7287 (defun gnus-summary-beginning-of-article ()
7288   "Scroll the article back to the beginning."
7289   (interactive)
7290   (gnus-summary-select-article)
7291   (gnus-configure-windows 'article)
7292   (gnus-eval-in-buffer-window gnus-article-buffer
7293     (widen)
7294     (goto-char (point-min))
7295     (when gnus-page-broken
7296       (gnus-narrow-to-page))))
7297
7298 (defun gnus-summary-end-of-article ()
7299   "Scroll to the end of the article."
7300   (interactive)
7301   (gnus-summary-select-article)
7302   (gnus-configure-windows 'article)
7303   (gnus-eval-in-buffer-window gnus-article-buffer
7304     (widen)
7305     (goto-char (point-max))
7306     (recenter -3)
7307     (when gnus-page-broken
7308       (gnus-narrow-to-page))))
7309
7310 (defun gnus-summary-print-article (&optional filename n)
7311   "Generate and print a PostScript image of the N next (mail) articles.
7312
7313 If N is negative, print the N previous articles.  If N is nil and articles
7314 have been marked with the process mark, print these instead.
7315
7316 If the optional first argument FILENAME is nil, send the image to the
7317 printer.  If FILENAME is a string, save the PostScript image in a file with
7318 that name.  If FILENAME is a number, prompt the user for the name of the file
7319 to save in."
7320   (interactive (list (ps-print-preprint current-prefix-arg)
7321                      current-prefix-arg))
7322   (dolist (article (gnus-summary-work-articles n))
7323     (gnus-summary-select-article nil nil 'pseudo article)
7324     (gnus-eval-in-buffer-window gnus-article-buffer
7325       (let ((buffer (generate-new-buffer " *print*")))
7326         (unwind-protect
7327             (progn
7328               (copy-to-buffer buffer (point-min) (point-max))
7329               (set-buffer buffer)
7330               (gnus-article-delete-invisible-text)
7331               (let ((ps-left-header
7332                      (list
7333                       (concat "("
7334                               (mail-header-subject gnus-current-headers) ")")
7335                       (concat "("
7336                               (mail-header-from gnus-current-headers) ")")))
7337                     (ps-right-header
7338                      (list
7339                       "/pagenumberstring load"
7340                       (concat "("
7341                               (mail-header-date gnus-current-headers) ")"))))
7342                 (gnus-run-hooks 'gnus-ps-print-hook)
7343                 (save-excursion
7344                   (ps-print-buffer-with-faces filename))))
7345           (kill-buffer buffer))))))
7346
7347 (defun gnus-summary-show-article (&optional arg)
7348   "Force re-fetching of the current article.
7349 If ARG (the prefix) is a number, show the article with the charset 
7350 defined in `gnus-summary-show-article-charset-alist', or the charset
7351 inputed.
7352 If ARG (the prefix) is non-nil and not a number, show the raw article 
7353 without any article massaging functions being run."
7354   (interactive "P")
7355   (cond 
7356    ((numberp arg)
7357     (let ((gnus-newsgroup-charset 
7358            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7359                (read-coding-system "Charset: ")))
7360           (gnus-newsgroup-ignored-charsets 'gnus-all))
7361       (gnus-summary-select-article nil 'force)))
7362    ((not arg)
7363     ;; Select the article the normal way.
7364     (gnus-summary-select-article nil 'force))
7365    (t
7366     ;; We have to require this here to make sure that the following
7367     ;; dynamic binding isn't shadowed by autoloading.
7368     (require 'gnus-async)
7369     (require 'gnus-art)
7370     ;; Bind the article treatment functions to nil.
7371     (let ((gnus-have-all-headers t)
7372           gnus-article-display-hook
7373           gnus-article-prepare-hook
7374           gnus-article-decode-hook
7375           gnus-break-pages
7376           gnus-show-mime)
7377       (gnus-summary-select-article nil 'force))))
7378   (gnus-summary-goto-subject gnus-current-article)
7379   (gnus-summary-position-point))
7380
7381 (defun gnus-summary-verbose-headers (&optional arg)
7382   "Toggle permanent full header display.
7383 If ARG is a positive number, turn header display on.
7384 If ARG is a negative number, turn header display off."
7385   (interactive "P")
7386   (setq gnus-show-all-headers
7387         (cond ((or (not (numberp arg))
7388                    (zerop arg))
7389                (not gnus-show-all-headers))
7390               ((natnump arg)
7391                t)))
7392   (gnus-summary-show-article))
7393
7394 (defun gnus-summary-toggle-header (&optional arg)
7395   "Show the headers if they are hidden, or hide them if they are shown.
7396 If ARG is a positive number, show the entire header.
7397 If ARG is a negative number, hide the unwanted header lines."
7398   (interactive "P")
7399   (save-excursion
7400     (set-buffer gnus-article-buffer)
7401     (save-restriction
7402       (let* ((buffer-read-only nil)
7403              (inhibit-point-motion-hooks t)
7404              hidden e)
7405         (setq hidden
7406               (if (numberp arg)
7407                   (>= arg 0)
7408                 (save-restriction 
7409                   (article-narrow-to-head)
7410                   (gnus-article-hidden-text-p 'headers))))
7411         (goto-char (point-min))
7412         (when (search-forward "\n\n" nil t)
7413           (delete-region (point-min) (1- (point))))
7414         (goto-char (point-min))
7415         (save-excursion
7416           (set-buffer gnus-original-article-buffer)
7417           (goto-char (point-min))
7418           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7419         (insert-buffer-substring gnus-original-article-buffer 1 e)
7420         (save-restriction
7421           (narrow-to-region (point-min) (point))
7422           (article-decode-encoded-words)
7423           (if  hidden
7424               (let ((gnus-treat-hide-headers nil)
7425                     (gnus-treat-hide-boring-headers nil))
7426                 (setq gnus-article-wash-types
7427                       (delq 'headers gnus-article-wash-types))
7428                 (gnus-treat-article 'head))
7429             (gnus-treat-article 'head)))
7430         (gnus-set-mode-line 'article)))))
7431
7432 (defun gnus-summary-show-all-headers ()
7433   "Make all header lines visible."
7434   (interactive)
7435   (gnus-article-show-all-headers))
7436
7437 (defun gnus-summary-toggle-mime (&optional arg)
7438   "Toggle MIME processing.
7439 If ARG is a positive number, turn MIME processing on."
7440   (interactive "P")
7441   (setq gnus-show-mime
7442         (if (null arg)
7443             (not gnus-show-mime)
7444           (> (prefix-numeric-value arg) 0)))
7445   (gnus-summary-select-article t 'force))
7446
7447 (defun gnus-summary-caesar-message (&optional arg)
7448   "Caesar rotate the current article by 13.
7449 The numerical prefix specifies how many places to rotate each letter
7450 forward."
7451   (interactive "P")
7452   (gnus-summary-select-article)
7453   (let ((mail-header-separator ""))
7454     (gnus-eval-in-buffer-window gnus-article-buffer
7455       (save-restriction
7456         (widen)
7457         (let ((start (window-start))
7458               buffer-read-only)
7459           (message-caesar-buffer-body arg)
7460           (set-window-start (get-buffer-window (current-buffer)) start))))))
7461
7462 (defun gnus-summary-stop-page-breaking ()
7463   "Stop page breaking in the current article."
7464   (interactive)
7465   (gnus-summary-select-article)
7466   (gnus-eval-in-buffer-window gnus-article-buffer
7467     (widen)
7468     (when (gnus-visual-p 'page-marker)
7469       (let ((buffer-read-only nil))
7470         (gnus-remove-text-with-property 'gnus-prev)
7471         (gnus-remove-text-with-property 'gnus-next))
7472       (setq gnus-page-broken nil))))
7473
7474 (defun gnus-summary-move-article (&optional n to-newsgroup
7475                                             select-method action)
7476   "Move the current article to a different newsgroup.
7477 If N is a positive number, move the N next articles.
7478 If N is a negative number, move the N previous articles.
7479 If N is nil and any articles have been marked with the process mark,
7480 move those articles instead.
7481 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7482 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7483 re-spool using this method.
7484
7485 For this function to work, both the current newsgroup and the
7486 newsgroup that you want to move to have to support the `request-move'
7487 and `request-accept' functions.
7488
7489 ACTION can be either `move' (the default), `crosspost' or `copy'."
7490   (interactive "P")
7491   (unless action
7492     (setq action 'move))
7493   ;; Disable marking as read.
7494   (let (gnus-mark-article-hook)
7495     (save-window-excursion
7496       (gnus-summary-select-article)))
7497   ;; Check whether the source group supports the required functions.
7498   (cond ((and (eq action 'move)
7499               (not (gnus-check-backend-function
7500                     'request-move-article gnus-newsgroup-name)))
7501          (error "The current group does not support article moving"))
7502         ((and (eq action 'crosspost)
7503               (not (gnus-check-backend-function
7504                     'request-replace-article gnus-newsgroup-name)))
7505          (error "The current group does not support article editing")))
7506   (let ((articles (gnus-summary-work-articles n))
7507         (prefix (if (gnus-check-backend-function
7508                     'request-move-article gnus-newsgroup-name)
7509                     (gnus-group-real-prefix gnus-newsgroup-name)
7510                   ""))
7511         (names '((move "Move" "Moving")
7512                  (copy "Copy" "Copying")
7513                  (crosspost "Crosspost" "Crossposting")))
7514         (copy-buf (save-excursion
7515                     (nnheader-set-temp-buffer " *copy article*")))
7516         (default-marks gnus-article-mark-lists)
7517         (no-expire-marks (delete '(expirable . expire)
7518                                  (copy-sequence gnus-article-mark-lists)))
7519         art-group to-method new-xref article to-groups)
7520     (unless (assq action names)
7521       (error "Unknown action %s" action))
7522     ;; Read the newsgroup name.
7523     (when (and (not to-newsgroup)
7524                (not select-method))
7525       (setq to-newsgroup
7526             (gnus-read-move-group-name
7527              (cadr (assq action names))
7528              (symbol-value (intern (format "gnus-current-%s-group" action)))
7529              articles prefix))
7530       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7531     (setq to-method (or select-method
7532                         (gnus-server-to-method
7533                          (gnus-group-method to-newsgroup))))
7534     ;; Check the method we are to move this article to...
7535     (unless (gnus-check-backend-function
7536              'request-accept-article (car to-method))
7537       (error "%s does not support article copying" (car to-method)))
7538     (unless (gnus-check-server to-method)
7539       (error "Can't open server %s" (car to-method)))
7540     (gnus-message 6 "%s to %s: %s..."
7541                   (caddr (assq action names))
7542                   (or (car select-method) to-newsgroup) articles)
7543     (while articles
7544       (setq article (pop articles))
7545       (setq
7546        art-group
7547        (cond
7548         ;; Move the article.
7549         ((eq action 'move)
7550          ;; Remove this article from future suppression.
7551          (gnus-dup-unsuppress-article article)
7552          (gnus-request-move-article
7553           article                       ; Article to move
7554           gnus-newsgroup-name           ; From newsgroup
7555           (nth 1 (gnus-find-method-for-group
7556                   gnus-newsgroup-name)) ; Server
7557           (list 'gnus-request-accept-article
7558                 to-newsgroup (list 'quote select-method)
7559                 (not articles) t)       ; Accept form
7560           (not articles)))              ; Only save nov last time
7561         ;; Copy the article.
7562         ((eq action 'copy)
7563          (save-excursion
7564            (set-buffer copy-buf)
7565            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7566              (gnus-request-accept-article
7567               to-newsgroup select-method (not articles) t))))
7568         ;; Crosspost the article.
7569         ((eq action 'crosspost)
7570          (let ((xref (message-tokenize-header
7571                       (mail-header-xref (gnus-summary-article-header article))
7572                       " ")))
7573            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7574                                   ":" article))
7575            (unless xref
7576              (setq xref (list (system-name))))
7577            (setq new-xref
7578                  (concat
7579                   (mapconcat 'identity
7580                              (delete "Xref:" (delete new-xref xref))
7581                              " ")
7582                   " " new-xref))
7583            (save-excursion
7584              (set-buffer copy-buf)
7585              ;; First put the article in the destination group.
7586              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7587              (when (consp (setq art-group
7588                                 (gnus-request-accept-article
7589                                  to-newsgroup select-method (not articles))))
7590                (setq new-xref (concat new-xref " " (car art-group)
7591                                       ":" (cdr art-group)))
7592                ;; Now we have the new Xrefs header, so we insert
7593                ;; it and replace the new article.
7594                (nnheader-replace-header "Xref" new-xref)
7595                (gnus-request-replace-article
7596                 (cdr art-group) to-newsgroup (current-buffer))
7597                art-group))))))
7598       (cond
7599        ((not art-group)
7600         (gnus-message 1 "Couldn't %s article %s: %s"
7601                       (cadr (assq action names)) article
7602                       (nnheader-get-report (car to-method))))
7603        ((eq art-group 'junk)
7604         (when (eq action 'move)
7605           (gnus-summary-mark-article article gnus-canceled-mark)
7606           (gnus-message 4 "Deleted article %s" article)))
7607        (t
7608         (let* ((pto-group (gnus-group-prefixed-name
7609                            (car art-group) to-method))
7610                (entry
7611                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7612                (info (nth 2 entry))
7613                (to-group (gnus-info-group info))
7614                to-marks)
7615           ;; Update the group that has been moved to.
7616           (when (and info
7617                      (memq action '(move copy)))
7618             (unless (member to-group to-groups)
7619               (push to-group to-groups))
7620
7621             (unless (memq article gnus-newsgroup-unreads)
7622               (push 'read to-marks)
7623               (gnus-info-set-read
7624                info (gnus-add-to-range (gnus-info-read info)
7625                                        (list (cdr art-group)))))
7626
7627             ;; See whether the article is to be put in the cache.
7628             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7629                              default-marks
7630                            no-expire-marks))
7631                   (to-article (cdr art-group)))
7632
7633               ;; Enter the article into the cache in the new group,
7634               ;; if that is required.
7635               (when gnus-use-cache
7636                 (gnus-cache-possibly-enter-article
7637                  to-group to-article
7638                  (let ((header (copy-sequence
7639                                 (gnus-summary-article-header article))))
7640                    (mail-header-set-number header to-article)
7641                    header)
7642                  (memq article gnus-newsgroup-marked)
7643                  (memq article gnus-newsgroup-dormant)
7644                  (memq article gnus-newsgroup-unreads)))
7645
7646               (when gnus-preserve-marks 
7647                 ;; Copy any marks over to the new group.
7648                 (when (and (equal to-group gnus-newsgroup-name)
7649                            (not (memq article gnus-newsgroup-unreads)))
7650                   ;; Mark this article as read in this group.
7651                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7652                   (setcdr (gnus-active to-group) to-article)
7653                   (setcdr gnus-newsgroup-active to-article))
7654
7655                 (while marks
7656                   (when (memq article (symbol-value
7657                                        (intern (format "gnus-newsgroup-%s"
7658                                                        (caar marks)))))
7659                     (push (cdar marks) to-marks)
7660                     ;; If the other group is the same as this group,
7661                     ;; then we have to add the mark to the list.
7662                     (when (equal to-group gnus-newsgroup-name)
7663                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7664                            (cons to-article
7665                                  (symbol-value
7666                                   (intern (format "gnus-newsgroup-%s"
7667                                                   (caar marks)))))))
7668                     ;; Copy the marks to other group.
7669                     (gnus-add-marked-articles
7670                      to-group (cdar marks) (list to-article) info))
7671                   (setq marks (cdr marks)))
7672
7673                 (gnus-request-set-mark to-group (list (list (list to-article)
7674                                                             'set
7675                                                             to-marks))))
7676
7677               (gnus-dribble-enter
7678                (concat "(gnus-group-set-info '"
7679                        (gnus-prin1-to-string (gnus-get-info to-group))
7680                        ")"))))
7681
7682           ;; Update the Xref header in this article to point to
7683           ;; the new crossposted article we have just created.
7684           (when (eq action 'crosspost)
7685             (save-excursion
7686               (set-buffer copy-buf)
7687               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7688               (nnheader-replace-header "Xref" new-xref)
7689               (gnus-request-replace-article
7690                article gnus-newsgroup-name (current-buffer)))))
7691
7692         ;;;!!!Why is this necessary?
7693         (set-buffer gnus-summary-buffer)
7694
7695         (gnus-summary-goto-subject article)
7696         (when (eq action 'move)
7697           (gnus-summary-mark-article article gnus-canceled-mark))))
7698       (gnus-summary-remove-process-mark article))
7699     ;; Re-activate all groups that have been moved to.
7700     (while to-groups
7701       (save-excursion
7702         (set-buffer gnus-group-buffer)
7703         (when (gnus-group-goto-group (car to-groups) t)
7704           (gnus-group-get-new-news-this-group 1 t))
7705         (pop to-groups)))
7706
7707     (gnus-kill-buffer copy-buf)
7708     (gnus-summary-position-point)
7709     (gnus-set-mode-line 'summary)))
7710
7711 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7712   "Move the current article to a different newsgroup.
7713 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7714 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7715 re-spool using this method."
7716   (interactive "P")
7717   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7718
7719 (defun gnus-summary-crosspost-article (&optional n)
7720   "Crosspost the current article to some other group."
7721   (interactive "P")
7722   (gnus-summary-move-article n nil nil 'crosspost))
7723
7724 (defcustom gnus-summary-respool-default-method nil
7725   "Default method for respooling an article.
7726 If nil, use to the current newsgroup method."
7727   :type '(choice (gnus-select-method :value (nnml ""))
7728                  (const nil))
7729   :group 'gnus-summary-mail)
7730
7731 (defun gnus-summary-respool-article (&optional n method)
7732   "Respool the current article.
7733 The article will be squeezed through the mail spooling process again,
7734 which means that it will be put in some mail newsgroup or other
7735 depending on `nnmail-split-methods'.
7736 If N is a positive number, respool the N next articles.
7737 If N is a negative number, respool the N previous articles.
7738 If N is nil and any articles have been marked with the process mark,
7739 respool those articles instead.
7740
7741 Respooling can be done both from mail groups and \"real\" newsgroups.
7742 In the former case, the articles in question will be moved from the
7743 current group into whatever groups they are destined to.  In the
7744 latter case, they will be copied into the relevant groups."
7745   (interactive
7746    (list current-prefix-arg
7747          (let* ((methods (gnus-methods-using 'respool))
7748                 (methname
7749                  (symbol-name (or gnus-summary-respool-default-method
7750                                   (car (gnus-find-method-for-group
7751                                         gnus-newsgroup-name)))))
7752                 (method
7753                  (gnus-completing-read
7754                   methname "What backend do you want to use when respooling?"
7755                   methods nil t nil 'gnus-mail-method-history))
7756                 ms)
7757            (cond
7758             ((zerop (length (setq ms (gnus-servers-using-backend
7759                                       (intern method)))))
7760              (list (intern method) ""))
7761             ((= 1 (length ms))
7762              (car ms))
7763             (t
7764              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7765                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7766                            ms-alist))))))))
7767   (unless method
7768     (error "No method given for respooling"))
7769   (if (assoc (symbol-name
7770               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7771              (gnus-methods-using 'respool))
7772       (gnus-summary-move-article n nil method)
7773     (gnus-summary-copy-article n nil method)))
7774
7775 (defun gnus-summary-import-article (file)
7776   "Import an arbitrary file into a mail newsgroup."
7777   (interactive "fImport file: ")
7778   (let ((group gnus-newsgroup-name)
7779         (now (current-time))
7780         atts lines)
7781     (unless (gnus-check-backend-function 'request-accept-article group)
7782       (error "%s does not support article importing" group))
7783     (or (file-readable-p file)
7784         (not (file-regular-p file))
7785         (error "Can't read %s" file))
7786     (save-excursion
7787       (set-buffer (gnus-get-buffer-create " *import file*"))
7788       (erase-buffer)
7789       (nnheader-insert-file-contents file)
7790       (goto-char (point-min))
7791       (unless (nnheader-article-p)
7792         ;; This doesn't look like an article, so we fudge some headers.
7793         (setq atts (file-attributes file)
7794               lines (count-lines (point-min) (point-max)))
7795         (insert "From: " (read-string "From: ") "\n"
7796                 "Subject: " (read-string "Subject: ") "\n"
7797                 "Date: " (message-make-date (nth 5 atts))
7798                 "\n"
7799                 "Message-ID: " (message-make-message-id) "\n"
7800                 "Lines: " (int-to-string lines) "\n"
7801                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7802       (gnus-request-accept-article group nil t)
7803       (kill-buffer (current-buffer)))))
7804
7805 (defun gnus-summary-article-posted-p ()
7806   "Say whether the current (mail) article is available from news as well.
7807 This will be the case if the article has both been mailed and posted."
7808   (interactive)
7809   (let ((id (mail-header-references (gnus-summary-article-header)))
7810         (gnus-override-method (car (gnus-refer-article-methods))))
7811     (if (gnus-request-head id "")
7812         (gnus-message 2 "The current message was found on %s"
7813                       gnus-override-method)
7814       (gnus-message 2 "The current message couldn't be found on %s"
7815                     gnus-override-method)
7816       nil)))
7817
7818 (defun gnus-summary-expire-articles (&optional now)
7819   "Expire all articles that are marked as expirable in the current group."
7820   (interactive)
7821   (when (gnus-check-backend-function
7822          'request-expire-articles gnus-newsgroup-name)
7823     ;; This backend supports expiry.
7824     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7825            (expirable (if total
7826                           (progn
7827                             ;; We need to update the info for
7828                             ;; this group for `gnus-list-of-read-articles'
7829                             ;; to give us the right answer.
7830                             (gnus-run-hooks 'gnus-exit-group-hook)
7831                             (gnus-summary-update-info)
7832                             (gnus-list-of-read-articles gnus-newsgroup-name))
7833                         (setq gnus-newsgroup-expirable
7834                               (sort gnus-newsgroup-expirable '<))))
7835            (expiry-wait (if now 'immediate
7836                           (gnus-group-find-parameter
7837                            gnus-newsgroup-name 'expiry-wait)))
7838            es)
7839       (when expirable
7840         ;; There are expirable articles in this group, so we run them
7841         ;; through the expiry process.
7842         (gnus-message 6 "Expiring articles...")
7843         (unless (gnus-check-group gnus-newsgroup-name)
7844           (error "Can't open server for %s" gnus-newsgroup-name))
7845         ;; The list of articles that weren't expired is returned.
7846         (save-excursion
7847           (if expiry-wait
7848               (let ((nnmail-expiry-wait-function nil)
7849                     (nnmail-expiry-wait expiry-wait))
7850                 (setq es (gnus-request-expire-articles
7851                           expirable gnus-newsgroup-name)))
7852             (setq es (gnus-request-expire-articles
7853                       expirable gnus-newsgroup-name))))
7854         (unless total
7855           (setq gnus-newsgroup-expirable es))
7856         ;; We go through the old list of expirable, and mark all
7857         ;; really expired articles as nonexistent.
7858         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7859           (let ((gnus-use-cache nil))
7860             (while expirable
7861               (unless (memq (car expirable) es)
7862                 (when (gnus-data-find (car expirable))
7863                   (gnus-summary-mark-article
7864                    (car expirable) gnus-canceled-mark)))
7865               (setq expirable (cdr expirable)))))
7866         (gnus-message 6 "Expiring articles...done")))))
7867
7868 (defun gnus-summary-expire-articles-now ()
7869   "Expunge all expirable articles in the current group.
7870 This means that *all* articles that are marked as expirable will be
7871 deleted forever, right now."
7872   (interactive)
7873   (or gnus-expert-user
7874       (gnus-yes-or-no-p
7875        "Are you really, really, really sure you want to delete all these messages? ")
7876       (error "Phew!"))
7877   (gnus-summary-expire-articles t))
7878
7879 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7880 (defun gnus-summary-delete-article (&optional n)
7881   "Delete the N next (mail) articles.
7882 This command actually deletes articles.  This is not a marking
7883 command.  The article will disappear forever from your life, never to
7884 return.
7885 If N is negative, delete backwards.
7886 If N is nil and articles have been marked with the process mark,
7887 delete these instead."
7888   (interactive "P")
7889   (unless (gnus-check-backend-function 'request-expire-articles
7890                                        gnus-newsgroup-name)
7891     (error "The current newsgroup does not support article deletion"))
7892   ;; Compute the list of articles to delete.
7893   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7894         not-deleted)
7895     (if (and gnus-novice-user
7896              (not (gnus-yes-or-no-p
7897                    (format "Do you really want to delete %s forever? "
7898                            (if (> (length articles) 1)
7899                                (format "these %s articles" (length articles))
7900                              "this article")))))
7901         ()
7902       ;; Delete the articles.
7903       (setq not-deleted (gnus-request-expire-articles
7904                          articles gnus-newsgroup-name 'force))
7905       (while articles
7906         (gnus-summary-remove-process-mark (car articles))
7907         ;; The backend might not have been able to delete the article
7908         ;; after all.
7909         (unless (memq (car articles) not-deleted)
7910           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7911         (setq articles (cdr articles)))
7912       (when not-deleted
7913         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7914     (gnus-summary-position-point)
7915     (gnus-set-mode-line 'summary)
7916     not-deleted))
7917
7918 (defun gnus-summary-edit-article (&optional force)
7919   "Edit the current article.
7920 This will have permanent effect only in mail groups.
7921 If FORCE is non-nil, allow editing of articles even in read-only
7922 groups."
7923   (interactive "P")
7924   (save-excursion
7925     (set-buffer gnus-summary-buffer)
7926     (let ((mail-parse-charset gnus-newsgroup-charset)
7927           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7928       (gnus-set-global-variables)
7929       (when (and (not force)
7930                  (gnus-group-read-only-p))
7931         (error "The current newsgroup does not support article editing"))
7932       (gnus-summary-show-article t)
7933       (gnus-article-edit-article
7934        'ignore
7935        `(lambda (no-highlight)
7936           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7937                 (mail-parse-ignored-charsets
7938                  ',gnus-newsgroup-ignored-charsets))
7939             (gnus-summary-edit-article-done
7940              ,(or (mail-header-references gnus-current-headers) "")
7941              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7942
7943 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7944
7945 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7946                                                  no-highlight)
7947   "Make edits to the current article permanent."
7948   (interactive)
7949   ;; Replace the article.
7950   (let ((buf (current-buffer)))
7951     (with-temp-buffer
7952       (insert-buffer-substring buf)
7953       (if (and (not read-only)
7954                (not (gnus-request-replace-article
7955                      (cdr gnus-article-current) (car gnus-article-current)
7956                      (current-buffer) t)))
7957           (error "Couldn't replace article")
7958         ;; Update the summary buffer.
7959         (if (and references
7960                  (equal (message-tokenize-header references " ")
7961                         (message-tokenize-header
7962                          (or (message-fetch-field "references") "") " ")))
7963             ;; We only have to update this line.
7964             (save-excursion
7965               (save-restriction
7966                 (message-narrow-to-head)
7967                 (let ((head (buffer-string))
7968                       header)
7969                   (with-temp-buffer
7970                     (insert (format "211 %d Article retrieved.\n"
7971                                     (cdr gnus-article-current)))
7972                     (insert head)
7973                     (insert ".\n")
7974                     (let ((nntp-server-buffer (current-buffer)))
7975                       (setq header (car (gnus-get-newsgroup-headers
7976                                          (save-excursion
7977                                            (set-buffer gnus-summary-buffer)
7978                                            gnus-newsgroup-dependencies)
7979                                          t))))
7980                     (save-excursion
7981                       (set-buffer gnus-summary-buffer)
7982                       (gnus-data-set-header
7983                        (gnus-data-find (cdr gnus-article-current))
7984                        header)
7985                       (gnus-summary-update-article-line
7986                        (cdr gnus-article-current) header))))))
7987           ;; Update threads.
7988           (set-buffer (or buffer gnus-summary-buffer))
7989           (gnus-summary-update-article (cdr gnus-article-current)))
7990         ;; Prettify the article buffer again.
7991         (unless no-highlight
7992           (save-excursion
7993             (set-buffer gnus-article-buffer)
7994             ;;;!!! Fix this -- article should be rehighlighted.
7995             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7996             (set-buffer gnus-original-article-buffer)
7997             (gnus-request-article
7998              (cdr gnus-article-current)
7999              (car gnus-article-current) (current-buffer))))
8000         ;; Prettify the summary buffer line.
8001         (when (gnus-visual-p 'summary-highlight 'highlight)
8002           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8003
8004 (defun gnus-summary-edit-wash (key)
8005   "Perform editing command KEY in the article buffer."
8006   (interactive
8007    (list
8008     (progn
8009       (message "%s" (concat (this-command-keys) "- "))
8010       (read-char))))
8011   (message "")
8012   (gnus-summary-edit-article)
8013   (execute-kbd-macro (concat (this-command-keys) key))
8014   (gnus-article-edit-done))
8015
8016 ;;; Respooling
8017
8018 (defun gnus-summary-respool-query (&optional silent trace)
8019   "Query where the respool algorithm would put this article."
8020   (interactive)
8021   (let (gnus-mark-article-hook)
8022     (gnus-summary-select-article)
8023     (save-excursion
8024       (set-buffer gnus-original-article-buffer)
8025       (save-restriction
8026         (message-narrow-to-head)
8027         (let ((groups (nnmail-article-group 'identity trace)))
8028           (unless silent
8029             (if groups
8030                 (message "This message would go to %s"
8031                          (mapconcat 'car groups ", "))
8032               (message "This message would go to no groups"))
8033             groups))))))
8034
8035 (defun gnus-summary-respool-trace ()
8036   "Trace where the respool algorithm would put this article.
8037 Display a buffer showing all fancy splitting patterns which matched."
8038   (interactive)
8039   (gnus-summary-respool-query nil t))
8040
8041 ;; Summary marking commands.
8042
8043 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8044   "Mark articles which has the same subject as read, and then select the next.
8045 If UNMARK is positive, remove any kind of mark.
8046 If UNMARK is negative, tick articles."
8047   (interactive "P")
8048   (when unmark
8049     (setq unmark (prefix-numeric-value unmark)))
8050   (let ((count
8051          (gnus-summary-mark-same-subject
8052           (gnus-summary-article-subject) unmark)))
8053     ;; Select next unread article.  If auto-select-same mode, should
8054     ;; select the first unread article.
8055     (gnus-summary-next-article t (and gnus-auto-select-same
8056                                       (gnus-summary-article-subject)))
8057     (gnus-message 7 "%d article%s marked as %s"
8058                   count (if (= count 1) " is" "s are")
8059                   (if unmark "unread" "read"))))
8060
8061 (defun gnus-summary-kill-same-subject (&optional unmark)
8062   "Mark articles which has the same subject as read.
8063 If UNMARK is positive, remove any kind of mark.
8064 If UNMARK is negative, tick articles."
8065   (interactive "P")
8066   (when unmark
8067     (setq unmark (prefix-numeric-value unmark)))
8068   (let ((count
8069          (gnus-summary-mark-same-subject
8070           (gnus-summary-article-subject) unmark)))
8071     ;; If marked as read, go to next unread subject.
8072     (when (null unmark)
8073       ;; Go to next unread subject.
8074       (gnus-summary-next-subject 1 t))
8075     (gnus-message 7 "%d articles are marked as %s"
8076                   count (if unmark "unread" "read"))))
8077
8078 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8079   "Mark articles with same SUBJECT as read, and return marked number.
8080 If optional argument UNMARK is positive, remove any kinds of marks.
8081 If optional argument UNMARK is negative, mark articles as unread instead."
8082   (let ((count 1))
8083     (save-excursion
8084       (cond
8085        ((null unmark)                   ; Mark as read.
8086         (while (and
8087                 (progn
8088                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8089                   (gnus-summary-show-thread) t)
8090                 (gnus-summary-find-subject subject))
8091           (setq count (1+ count))))
8092        ((> unmark 0)                    ; Tick.
8093         (while (and
8094                 (progn
8095                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8096                   (gnus-summary-show-thread) t)
8097                 (gnus-summary-find-subject subject))
8098           (setq count (1+ count))))
8099        (t                               ; Mark as unread.
8100         (while (and
8101                 (progn
8102                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8103                   (gnus-summary-show-thread) t)
8104                 (gnus-summary-find-subject subject))
8105           (setq count (1+ count)))))
8106       (gnus-set-mode-line 'summary)
8107       ;; Return the number of marked articles.
8108       count)))
8109
8110 (defun gnus-summary-mark-as-processable (n &optional unmark)
8111   "Set the process mark on the next N articles.
8112 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8113 the process mark instead.  The difference between N and the actual
8114 number of articles marked is returned."
8115   (interactive "p")
8116   (let ((backward (< n 0))
8117         (n (abs n)))
8118     (while (and
8119             (> n 0)
8120             (if unmark
8121                 (gnus-summary-remove-process-mark
8122                  (gnus-summary-article-number))
8123               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8124             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8125       (setq n (1- n)))
8126     (when (/= 0 n)
8127       (gnus-message 7 "No more articles"))
8128     (gnus-summary-recenter)
8129     (gnus-summary-position-point)
8130     n))
8131
8132 (defun gnus-summary-unmark-as-processable (n)
8133   "Remove the process mark from the next N articles.
8134 If N is negative, unmark backward instead.  The difference between N and
8135 the actual number of articles unmarked is returned."
8136   (interactive "p")
8137   (gnus-summary-mark-as-processable n t))
8138
8139 (defun gnus-summary-unmark-all-processable ()
8140   "Remove the process mark from all articles."
8141   (interactive)
8142   (save-excursion
8143     (while gnus-newsgroup-processable
8144       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8145   (gnus-summary-position-point))
8146
8147 (defun gnus-summary-mark-as-expirable (n)
8148   "Mark N articles forward as expirable.
8149 If N is negative, mark backward instead.  The difference between N and
8150 the actual number of articles marked is returned."
8151   (interactive "p")
8152   (gnus-summary-mark-forward n gnus-expirable-mark))
8153
8154 (defun gnus-summary-mark-article-as-replied (article)
8155   "Mark ARTICLE replied and update the summary line."
8156   (push article gnus-newsgroup-replied)
8157   (let ((buffer-read-only nil))
8158     (when (gnus-summary-goto-subject article nil t)
8159       (gnus-summary-update-secondary-mark article))))
8160
8161 (defun gnus-summary-set-bookmark (article)
8162   "Set a bookmark in current article."
8163   (interactive (list (gnus-summary-article-number)))
8164   (when (or (not (get-buffer gnus-article-buffer))
8165             (not gnus-current-article)
8166             (not gnus-article-current)
8167             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8168     (error "No current article selected"))
8169   ;; Remove old bookmark, if one exists.
8170   (let ((old (assq article gnus-newsgroup-bookmarks)))
8171     (when old
8172       (setq gnus-newsgroup-bookmarks
8173             (delq old gnus-newsgroup-bookmarks))))
8174   ;; Set the new bookmark, which is on the form
8175   ;; (article-number . line-number-in-body).
8176   (push
8177    (cons article
8178          (save-excursion
8179            (set-buffer gnus-article-buffer)
8180            (count-lines
8181             (min (point)
8182                  (save-excursion
8183                    (goto-char (point-min))
8184                    (search-forward "\n\n" nil t)
8185                    (point)))
8186             (point))))
8187    gnus-newsgroup-bookmarks)
8188   (gnus-message 6 "A bookmark has been added to the current article."))
8189
8190 (defun gnus-summary-remove-bookmark (article)
8191   "Remove the bookmark from the current article."
8192   (interactive (list (gnus-summary-article-number)))
8193   ;; Remove old bookmark, if one exists.
8194   (let ((old (assq article gnus-newsgroup-bookmarks)))
8195     (if old
8196         (progn
8197           (setq gnus-newsgroup-bookmarks
8198                 (delq old gnus-newsgroup-bookmarks))
8199           (gnus-message 6 "Removed bookmark."))
8200       (gnus-message 6 "No bookmark in current article."))))
8201
8202 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8203 (defun gnus-summary-mark-as-dormant (n)
8204   "Mark N articles forward as dormant.
8205 If N is negative, mark backward instead.  The difference between N and
8206 the actual number of articles marked is returned."
8207   (interactive "p")
8208   (gnus-summary-mark-forward n gnus-dormant-mark))
8209
8210 (defun gnus-summary-set-process-mark (article)
8211   "Set the process mark on ARTICLE and update the summary line."
8212   (setq gnus-newsgroup-processable
8213         (cons article
8214               (delq article gnus-newsgroup-processable)))
8215   (when (gnus-summary-goto-subject article)
8216     (gnus-summary-show-thread)
8217     (gnus-summary-goto-subject article)
8218     (gnus-summary-update-secondary-mark article)))
8219
8220 (defun gnus-summary-remove-process-mark (article)
8221   "Remove the process mark from ARTICLE and update the summary line."
8222   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8223   (when (gnus-summary-goto-subject article)
8224     (gnus-summary-show-thread)
8225     (gnus-summary-goto-subject article)
8226     (gnus-summary-update-secondary-mark article)))
8227
8228 (defun gnus-summary-set-saved-mark (article)
8229   "Set the process mark on ARTICLE and update the summary line."
8230   (push article gnus-newsgroup-saved)
8231   (when (gnus-summary-goto-subject article)
8232     (gnus-summary-update-secondary-mark article)))
8233
8234 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8235   "Mark N articles as read forwards.
8236 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8237 The difference between N and the actual number of articles marked is
8238 returned.
8239 Iff NO-EXPIRE, auto-expiry will be inhibited."
8240   (interactive "p")
8241   (gnus-summary-show-thread)
8242   (let ((backward (< n 0))
8243         (gnus-summary-goto-unread
8244          (and gnus-summary-goto-unread
8245               (not (eq gnus-summary-goto-unread 'never))
8246               (not (memq mark (list gnus-unread-mark
8247                                     gnus-ticked-mark gnus-dormant-mark)))))
8248         (n (abs n))
8249         (mark (or mark gnus-del-mark)))
8250     (while (and (> n 0)
8251                 (gnus-summary-mark-article nil mark no-expire)
8252                 (zerop (gnus-summary-next-subject
8253                         (if backward -1 1)
8254                         (and gnus-summary-goto-unread
8255                              (not (eq gnus-summary-goto-unread 'never)))
8256                         t)))
8257       (setq n (1- n)))
8258     (when (/= 0 n)
8259       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8260     (gnus-summary-recenter)
8261     (gnus-summary-position-point)
8262     (gnus-set-mode-line 'summary)
8263     n))
8264
8265 (defun gnus-summary-mark-article-as-read (mark)
8266   "Mark the current article quickly as read with MARK."
8267   (let ((article (gnus-summary-article-number)))
8268     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8269     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8270     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8271     (push (cons article mark) gnus-newsgroup-reads)
8272     ;; Possibly remove from cache, if that is used.
8273     (when gnus-use-cache
8274       (gnus-cache-enter-remove-article article))
8275     ;; Allow the backend to change the mark.
8276     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8277     ;; Check for auto-expiry.
8278     (when (and gnus-newsgroup-auto-expire
8279                (memq mark gnus-auto-expirable-marks))
8280       (setq mark gnus-expirable-mark)
8281       ;; Let the backend know about the mark change.
8282       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8283       (push article gnus-newsgroup-expirable))
8284     ;; Set the mark in the buffer.
8285     (gnus-summary-update-mark mark 'unread)
8286     t))
8287
8288 (defun gnus-summary-mark-article-as-unread (mark)
8289   "Mark the current article quickly as unread with MARK."
8290   (let* ((article (gnus-summary-article-number))
8291          (old-mark (gnus-summary-article-mark article)))
8292     ;; Allow the backend to change the mark.
8293     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8294     (if (eq mark old-mark)
8295         t
8296       (if (<= article 0)
8297           (progn
8298             (gnus-error 1 "Can't mark negative article numbers")
8299             nil)
8300         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8301         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8302         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8303         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8304         (cond ((= mark gnus-ticked-mark)
8305                (push article gnus-newsgroup-marked))
8306               ((= mark gnus-dormant-mark)
8307                (push article gnus-newsgroup-dormant))
8308               (t
8309                (push article gnus-newsgroup-unreads)))
8310         (gnus-pull article gnus-newsgroup-reads)
8311
8312         ;; See whether the article is to be put in the cache.
8313         (and gnus-use-cache
8314              (vectorp (gnus-summary-article-header article))
8315              (save-excursion
8316                (gnus-cache-possibly-enter-article
8317                 gnus-newsgroup-name article
8318                 (gnus-summary-article-header article)
8319                 (= mark gnus-ticked-mark)
8320                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8321
8322         ;; Fix the mark.
8323         (gnus-summary-update-mark mark 'unread)
8324         t))))
8325
8326 (defun gnus-summary-mark-article (&optional article mark no-expire)
8327   "Mark ARTICLE with MARK.  MARK can be any character.
8328 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8329 `??' (dormant) and `?E' (expirable).
8330 If MARK is nil, then the default character `?r' is used.
8331 If ARTICLE is nil, then the article on the current line will be
8332 marked.
8333 Iff NO-EXPIRE, auto-expiry will be inhibited."
8334   ;; The mark might be a string.
8335   (when (stringp mark)
8336     (setq mark (aref mark 0)))
8337   ;; If no mark is given, then we check auto-expiring.
8338   (when (null mark)
8339     (setq mark gnus-del-mark))
8340   (when (and (not no-expire)
8341              gnus-newsgroup-auto-expire
8342              (memq mark gnus-auto-expirable-marks))
8343     (setq mark gnus-expirable-mark))
8344   (let ((article (or article (gnus-summary-article-number)))
8345         (old-mark (gnus-summary-article-mark article)))
8346     ;; Allow the backend to change the mark.
8347     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8348     (if (eq mark old-mark)
8349         t
8350       (unless article
8351         (error "No article on current line"))
8352       (if (not (if (or (= mark gnus-unread-mark)
8353                        (= mark gnus-ticked-mark)
8354                        (= mark gnus-dormant-mark))
8355                    (gnus-mark-article-as-unread article mark)
8356                  (gnus-mark-article-as-read article mark)))
8357           t
8358         ;; See whether the article is to be put in the cache.
8359         (and gnus-use-cache
8360              (not (= mark gnus-canceled-mark))
8361              (vectorp (gnus-summary-article-header article))
8362              (save-excursion
8363                (gnus-cache-possibly-enter-article
8364                 gnus-newsgroup-name article
8365                 (gnus-summary-article-header article)
8366                 (= mark gnus-ticked-mark)
8367                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8368
8369         (when (gnus-summary-goto-subject article nil t)
8370           (let ((buffer-read-only nil))
8371             (gnus-summary-show-thread)
8372             ;; Fix the mark.
8373             (gnus-summary-update-mark mark 'unread)
8374             t))))))
8375
8376 (defun gnus-summary-update-secondary-mark (article)
8377   "Update the secondary (read, process, cache) mark."
8378   (gnus-summary-update-mark
8379    (cond ((memq article gnus-newsgroup-processable)
8380           gnus-process-mark)
8381          ((memq article gnus-newsgroup-cached)
8382           gnus-cached-mark)
8383          ((memq article gnus-newsgroup-replied)
8384           gnus-replied-mark)
8385          ((memq article gnus-newsgroup-saved)
8386           gnus-saved-mark)
8387          (t gnus-unread-mark))
8388    'replied)
8389   (when (gnus-visual-p 'summary-highlight 'highlight)
8390     (gnus-run-hooks 'gnus-summary-update-hook))
8391   t)
8392
8393 (defun gnus-summary-update-mark (mark type)
8394   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8395         (buffer-read-only nil))
8396     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8397     (when forward
8398       (when (looking-at "\r")
8399         (incf forward))
8400       (when (<= (+ forward (point)) (point-max))
8401         ;; Go to the right position on the line.
8402         (goto-char (+ forward (point)))
8403         ;; Replace the old mark with the new mark.
8404         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8405         ;; Optionally update the marks by some user rule.
8406         (when (eq type 'unread)
8407           (gnus-data-set-mark
8408            (gnus-data-find (gnus-summary-article-number)) mark)
8409           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8410
8411 (defun gnus-mark-article-as-read (article &optional mark)
8412   "Enter ARTICLE in the pertinent lists and remove it from others."
8413   ;; Make the article expirable.
8414   (let ((mark (or mark gnus-del-mark)))
8415     (if (= mark gnus-expirable-mark)
8416         (push article gnus-newsgroup-expirable)
8417       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8418     ;; Remove from unread and marked lists.
8419     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8420     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8421     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8422     (push (cons article mark) gnus-newsgroup-reads)
8423     ;; Possibly remove from cache, if that is used.
8424     (when gnus-use-cache
8425       (gnus-cache-enter-remove-article article))
8426     t))
8427
8428 (defun gnus-mark-article-as-unread (article &optional mark)
8429   "Enter ARTICLE in the pertinent lists and remove it from others."
8430   (let ((mark (or mark gnus-ticked-mark)))
8431     (if (<= article 0)
8432         (progn
8433           (gnus-error 1 "Can't mark negative article numbers")
8434           nil)
8435       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8436             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8437             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8438             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8439
8440       ;; Unsuppress duplicates?
8441       (when gnus-suppress-duplicates
8442         (gnus-dup-unsuppress-article article))
8443
8444       (cond ((= mark gnus-ticked-mark)
8445              (push article gnus-newsgroup-marked))
8446             ((= mark gnus-dormant-mark)
8447              (push article gnus-newsgroup-dormant))
8448             (t
8449              (push article gnus-newsgroup-unreads)))
8450       (gnus-pull article gnus-newsgroup-reads)
8451       t)))
8452
8453 (defalias 'gnus-summary-mark-as-unread-forward
8454   'gnus-summary-tick-article-forward)
8455 (make-obsolete 'gnus-summary-mark-as-unread-forward
8456                'gnus-summary-tick-article-forward)
8457 (defun gnus-summary-tick-article-forward (n)
8458   "Tick N articles forwards.
8459 If N is negative, tick backwards instead.
8460 The difference between N and the number of articles ticked is returned."
8461   (interactive "p")
8462   (gnus-summary-mark-forward n gnus-ticked-mark))
8463
8464 (defalias 'gnus-summary-mark-as-unread-backward
8465   'gnus-summary-tick-article-backward)
8466 (make-obsolete 'gnus-summary-mark-as-unread-backward
8467                'gnus-summary-tick-article-backward)
8468 (defun gnus-summary-tick-article-backward (n)
8469   "Tick N articles backwards.
8470 The difference between N and the number of articles ticked is returned."
8471   (interactive "p")
8472   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8473
8474 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8475 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8476 (defun gnus-summary-tick-article (&optional article clear-mark)
8477   "Mark current article as unread.
8478 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8479 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8480   (interactive)
8481   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8482                                        gnus-ticked-mark)))
8483
8484 (defun gnus-summary-mark-as-read-forward (n)
8485   "Mark N articles as read forwards.
8486 If N is negative, mark backwards instead.
8487 The difference between N and the actual number of articles marked is
8488 returned."
8489   (interactive "p")
8490   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8491
8492 (defun gnus-summary-mark-as-read-backward (n)
8493   "Mark the N articles as read backwards.
8494 The difference between N and the actual number of articles marked is
8495 returned."
8496   (interactive "p")
8497   (gnus-summary-mark-forward
8498    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8499
8500 (defun gnus-summary-mark-as-read (&optional article mark)
8501   "Mark current article as read.
8502 ARTICLE specifies the article to be marked as read.
8503 MARK specifies a string to be inserted at the beginning of the line."
8504   (gnus-summary-mark-article article mark))
8505
8506 (defun gnus-summary-clear-mark-forward (n)
8507   "Clear marks from N articles forward.
8508 If N is negative, clear backward instead.
8509 The difference between N and the number of marks cleared is returned."
8510   (interactive "p")
8511   (gnus-summary-mark-forward n gnus-unread-mark))
8512
8513 (defun gnus-summary-clear-mark-backward (n)
8514   "Clear marks from N articles backward.
8515 The difference between N and the number of marks cleared is returned."
8516   (interactive "p")
8517   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8518
8519 (defun gnus-summary-mark-unread-as-read ()
8520   "Intended to be used by `gnus-summary-mark-article-hook'."
8521   (when (memq gnus-current-article gnus-newsgroup-unreads)
8522     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8523
8524 (defun gnus-summary-mark-read-and-unread-as-read ()
8525   "Intended to be used by `gnus-summary-mark-article-hook'."
8526   (let ((mark (gnus-summary-article-mark)))
8527     (when (or (gnus-unread-mark-p mark)
8528               (gnus-read-mark-p mark))
8529       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8530
8531 (defun gnus-summary-mark-region-as-read (point mark all)
8532   "Mark all unread articles between point and mark as read.
8533 If given a prefix, mark all articles between point and mark as read,
8534 even ticked and dormant ones."
8535   (interactive "r\nP")
8536   (save-excursion
8537     (let (article)
8538       (goto-char point)
8539       (beginning-of-line)
8540       (while (and
8541               (< (point) mark)
8542               (progn
8543                 (when (or all
8544                           (memq (setq article (gnus-summary-article-number))
8545                                 gnus-newsgroup-unreads))
8546                   (gnus-summary-mark-article article gnus-del-mark))
8547                 t)
8548               (gnus-summary-find-next))))))
8549
8550 (defun gnus-summary-mark-below (score mark)
8551   "Mark articles with score less than SCORE with MARK."
8552   (interactive "P\ncMark: ")
8553   (setq score (if score
8554                   (prefix-numeric-value score)
8555                 (or gnus-summary-default-score 0)))
8556   (save-excursion
8557     (set-buffer gnus-summary-buffer)
8558     (goto-char (point-min))
8559     (while
8560         (progn
8561           (and (< (gnus-summary-article-score) score)
8562                (gnus-summary-mark-article nil mark))
8563           (gnus-summary-find-next)))))
8564
8565 (defun gnus-summary-kill-below (&optional score)
8566   "Mark articles with score below SCORE as read."
8567   (interactive "P")
8568   (gnus-summary-mark-below score gnus-killed-mark))
8569
8570 (defun gnus-summary-clear-above (&optional score)
8571   "Clear all marks from articles with score above SCORE."
8572   (interactive "P")
8573   (gnus-summary-mark-above score gnus-unread-mark))
8574
8575 (defun gnus-summary-tick-above (&optional score)
8576   "Tick all articles with score above SCORE."
8577   (interactive "P")
8578   (gnus-summary-mark-above score gnus-ticked-mark))
8579
8580 (defun gnus-summary-mark-above (score mark)
8581   "Mark articles with score over SCORE with MARK."
8582   (interactive "P\ncMark: ")
8583   (setq score (if score
8584                   (prefix-numeric-value score)
8585                 (or gnus-summary-default-score 0)))
8586   (save-excursion
8587     (set-buffer gnus-summary-buffer)
8588     (goto-char (point-min))
8589     (while (and (progn
8590                   (when (> (gnus-summary-article-score) score)
8591                     (gnus-summary-mark-article nil mark))
8592                   t)
8593                 (gnus-summary-find-next)))))
8594
8595 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8596 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8597 (defun gnus-summary-limit-include-expunged (&optional no-error)
8598   "Display all the hidden articles that were expunged for low scores."
8599   (interactive)
8600   (let ((buffer-read-only nil))
8601     (let ((scored gnus-newsgroup-scored)
8602           headers h)
8603       (while scored
8604         (unless (gnus-summary-goto-subject (caar scored))
8605           (and (setq h (gnus-summary-article-header (caar scored)))
8606                (< (cdar scored) gnus-summary-expunge-below)
8607                (push h headers)))
8608         (setq scored (cdr scored)))
8609       (if (not headers)
8610           (when (not no-error)
8611             (error "No expunged articles hidden"))
8612         (goto-char (point-min))
8613         (gnus-summary-prepare-unthreaded (nreverse headers))
8614         (goto-char (point-min))
8615         (gnus-summary-position-point)
8616         t))))
8617
8618 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8619   "Mark all unread articles in this newsgroup as read.
8620 If prefix argument ALL is non-nil, ticked and dormant articles will
8621 also be marked as read.
8622 If QUIETLY is non-nil, no questions will be asked.
8623 If TO-HERE is non-nil, it should be a point in the buffer.  All
8624 articles before this point will be marked as read.
8625 Note that this function will only catch up the unread article
8626 in the current summary buffer limitation.
8627 The number of articles marked as read is returned."
8628   (interactive "P")
8629   (prog1
8630       (save-excursion
8631         (when (or quietly
8632                   (not gnus-interactive-catchup) ;Without confirmation?
8633                   gnus-expert-user
8634                   (gnus-y-or-n-p
8635                    (if all
8636                        "Mark absolutely all articles as read? "
8637                      "Mark all unread articles as read? ")))
8638           (if (and not-mark
8639                    (not gnus-newsgroup-adaptive)
8640                    (not gnus-newsgroup-auto-expire)
8641                    (not gnus-suppress-duplicates)
8642                    (or (not gnus-use-cache)
8643                        (eq gnus-use-cache 'passive)))
8644               (progn
8645                 (when all
8646                   (setq gnus-newsgroup-marked nil
8647                         gnus-newsgroup-dormant nil))
8648                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8649             ;; We actually mark all articles as canceled, which we
8650             ;; have to do when using auto-expiry or adaptive scoring.
8651             (gnus-summary-show-all-threads)
8652             (when (gnus-summary-first-subject (not all) t)
8653               (while (and
8654                       (if to-here (< (point) to-here) t)
8655                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8656                       (gnus-summary-find-next (not all) nil nil t))))
8657             (gnus-set-mode-line 'summary))
8658           t))
8659     (gnus-summary-position-point)))
8660
8661 (defun gnus-summary-catchup-to-here (&optional all)
8662   "Mark all unticked articles before the current one as read.
8663 If ALL is non-nil, also mark ticked and dormant articles as read."
8664   (interactive "P")
8665   (save-excursion
8666     (gnus-save-hidden-threads
8667       (let ((beg (point)))
8668         ;; We check that there are unread articles.
8669         (when (or all (gnus-summary-find-prev))
8670           (gnus-summary-catchup all t beg)))))
8671   (gnus-summary-position-point))
8672
8673 (defun gnus-summary-catchup-all (&optional quietly)
8674   "Mark all articles in this newsgroup as read."
8675   (interactive "P")
8676   (gnus-summary-catchup t quietly))
8677
8678 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8679   "Mark all unread articles in this group as read, then exit.
8680 If prefix argument ALL is non-nil, all articles are marked as read."
8681   (interactive "P")
8682   (when (gnus-summary-catchup all quietly nil 'fast)
8683     ;; Select next newsgroup or exit.
8684     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8685              (eq gnus-auto-select-next 'quietly))
8686         (gnus-summary-next-group nil)
8687       (gnus-summary-exit))))
8688
8689 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8690   "Mark all articles in this newsgroup as read, and then exit."
8691   (interactive "P")
8692   (gnus-summary-catchup-and-exit t quietly))
8693
8694 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8695   "Mark all articles in this group as read and select the next group.
8696 If given a prefix, mark all articles, unread as well as ticked, as
8697 read."
8698   (interactive "P")
8699   (save-excursion
8700     (gnus-summary-catchup all))
8701   (gnus-summary-next-group))
8702
8703 ;; Thread-based commands.
8704
8705 (defun gnus-summary-articles-in-thread (&optional article)
8706   "Return a list of all articles in the current thread.
8707 If ARTICLE is non-nil, return all articles in the thread that starts
8708 with that article."
8709   (let* ((article (or article (gnus-summary-article-number)))
8710          (data (gnus-data-find-list article))
8711          (top-level (gnus-data-level (car data)))
8712          (top-subject
8713           (cond ((null gnus-thread-operation-ignore-subject)
8714                  (gnus-simplify-subject-re
8715                   (mail-header-subject (gnus-data-header (car data)))))
8716                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8717                  (gnus-simplify-subject-fuzzy
8718                   (mail-header-subject (gnus-data-header (car data)))))
8719                 (t nil)))
8720          (end-point (save-excursion
8721                       (if (gnus-summary-go-to-next-thread)
8722                           (point) (point-max))))
8723          articles)
8724     (while (and data
8725                 (< (gnus-data-pos (car data)) end-point))
8726       (when (or (not top-subject)
8727                 (string= top-subject
8728                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8729                              (gnus-simplify-subject-fuzzy
8730                               (mail-header-subject
8731                                (gnus-data-header (car data))))
8732                            (gnus-simplify-subject-re
8733                             (mail-header-subject
8734                              (gnus-data-header (car data)))))))
8735         (push (gnus-data-number (car data)) articles))
8736       (unless (and (setq data (cdr data))
8737                    (> (gnus-data-level (car data)) top-level))
8738         (setq data nil)))
8739     ;; Return the list of articles.
8740     (nreverse articles)))
8741
8742 (defun gnus-summary-rethread-current ()
8743   "Rethread the thread the current article is part of."
8744   (interactive)
8745   (let* ((gnus-show-threads t)
8746          (article (gnus-summary-article-number))
8747          (id (mail-header-id (gnus-summary-article-header)))
8748          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8749     (unless id
8750       (error "No article on the current line"))
8751     (gnus-rebuild-thread id)
8752     (gnus-summary-goto-subject article)))
8753
8754 (defun gnus-summary-reparent-thread ()
8755   "Make the current article child of the marked (or previous) article.
8756
8757 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8758 is non-nil or the Subject: of both articles are the same."
8759   (interactive)
8760   (unless (not (gnus-group-read-only-p))
8761     (error "The current newsgroup does not support article editing"))
8762   (unless (<= (length gnus-newsgroup-processable) 1)
8763     (error "No more than one article may be marked"))
8764   (save-window-excursion
8765     (let ((gnus-article-buffer " *reparent*")
8766           (current-article (gnus-summary-article-number))
8767           ;; First grab the marked article, otherwise one line up.
8768           (parent-article (if (not (null gnus-newsgroup-processable))
8769                               (car gnus-newsgroup-processable)
8770                             (save-excursion
8771                               (if (eq (forward-line -1) 0)
8772                                   (gnus-summary-article-number)
8773                                 (error "Beginning of summary buffer"))))))
8774       (unless (not (eq current-article parent-article))
8775         (error "An article may not be self-referential"))
8776       (let ((message-id (mail-header-id
8777                          (gnus-summary-article-header parent-article))))
8778         (unless (and message-id (not (equal message-id "")))
8779           (error "No message-id in desired parent"))
8780         (gnus-with-article current-article
8781           (goto-char (point-min))
8782           (if (re-search-forward "^References: " nil t)
8783               (progn
8784                 (re-search-forward "^[^ \t]" nil t)
8785                 (forward-line -1)
8786                 (end-of-line)
8787                 (insert " " message-id))
8788             (insert "References: " message-id "\n")))
8789         (set-buffer gnus-summary-buffer)
8790         (gnus-summary-unmark-all-processable)
8791         (gnus-summary-update-article current-article)
8792         (gnus-summary-rethread-current)
8793         (gnus-message 3 "Article %d is now the child of article %d"
8794                       current-article parent-article)))))
8795
8796 (defun gnus-summary-toggle-threads (&optional arg)
8797   "Toggle showing conversation threads.
8798 If ARG is positive number, turn showing conversation threads on."
8799   (interactive "P")
8800   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8801     (setq gnus-show-threads
8802           (if (null arg) (not gnus-show-threads)
8803             (> (prefix-numeric-value arg) 0)))
8804     (gnus-summary-prepare)
8805     (gnus-summary-goto-subject current)
8806     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8807     (gnus-summary-position-point)))
8808
8809 (defun gnus-summary-show-all-threads ()
8810   "Show all threads."
8811   (interactive)
8812   (save-excursion
8813     (let ((buffer-read-only nil))
8814       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8815   (gnus-summary-position-point))
8816
8817 (defun gnus-summary-show-thread ()
8818   "Show thread subtrees.
8819 Returns nil if no thread was there to be shown."
8820   (interactive)
8821   (let ((buffer-read-only nil)
8822         (orig (point))
8823         ;; first goto end then to beg, to have point at beg after let
8824         (end (progn (end-of-line) (point)))
8825         (beg (progn (beginning-of-line) (point))))
8826     (prog1
8827         ;; Any hidden lines here?
8828         (search-forward "\r" end t)
8829       (subst-char-in-region beg end ?\^M ?\n t)
8830       (goto-char orig)
8831       (gnus-summary-position-point))))
8832
8833 (defun gnus-summary-hide-all-threads ()
8834   "Hide all thread subtrees."
8835   (interactive)
8836   (save-excursion
8837     (goto-char (point-min))
8838     (gnus-summary-hide-thread)
8839     (while (zerop (gnus-summary-next-thread 1 t))
8840       (gnus-summary-hide-thread)))
8841   (gnus-summary-position-point))
8842
8843 (defun gnus-summary-hide-thread ()
8844   "Hide thread subtrees.
8845 Returns nil if no threads were there to be hidden."
8846   (interactive)
8847   (let ((buffer-read-only nil)
8848         (start (point))
8849         (article (gnus-summary-article-number)))
8850     (goto-char start)
8851     ;; Go forward until either the buffer ends or the subthread
8852     ;; ends.
8853     (when (and (not (eobp))
8854                (or (zerop (gnus-summary-next-thread 1 t))
8855                    (goto-char (point-max))))
8856       (prog1
8857           (if (and (> (point) start)
8858                    (search-backward "\n" start t))
8859               (progn
8860                 (subst-char-in-region start (point) ?\n ?\^M)
8861                 (gnus-summary-goto-subject article))
8862             (goto-char start)
8863             nil)))))
8864
8865 (defun gnus-summary-go-to-next-thread (&optional previous)
8866   "Go to the same level (or less) next thread.
8867 If PREVIOUS is non-nil, go to previous thread instead.
8868 Return the article number moved to, or nil if moving was impossible."
8869   (let ((level (gnus-summary-thread-level))
8870         (way (if previous -1 1))
8871         (beg (point)))
8872     (forward-line way)
8873     (while (and (not (eobp))
8874                 (< level (gnus-summary-thread-level)))
8875       (forward-line way))
8876     (if (eobp)
8877         (progn
8878           (goto-char beg)
8879           nil)
8880       (setq beg (point))
8881       (prog1
8882           (gnus-summary-article-number)
8883         (goto-char beg)))))
8884
8885 (defun gnus-summary-next-thread (n &optional silent)
8886   "Go to the same level next N'th thread.
8887 If N is negative, search backward instead.
8888 Returns the difference between N and the number of skips actually
8889 done.
8890
8891 If SILENT, don't output messages."
8892   (interactive "p")
8893   (let ((backward (< n 0))
8894         (n (abs n)))
8895     (while (and (> n 0)
8896                 (gnus-summary-go-to-next-thread backward))
8897       (decf n))
8898     (unless silent
8899       (gnus-summary-position-point))
8900     (when (and (not silent) (/= 0 n))
8901       (gnus-message 7 "No more threads"))
8902     n))
8903
8904 (defun gnus-summary-prev-thread (n)
8905   "Go to the same level previous N'th thread.
8906 Returns the difference between N and the number of skips actually
8907 done."
8908   (interactive "p")
8909   (gnus-summary-next-thread (- n)))
8910
8911 (defun gnus-summary-go-down-thread ()
8912   "Go down one level in the current thread."
8913   (let ((children (gnus-summary-article-children)))
8914     (when children
8915       (gnus-summary-goto-subject (car children)))))
8916
8917 (defun gnus-summary-go-up-thread ()
8918   "Go up one level in the current thread."
8919   (let ((parent (gnus-summary-article-parent)))
8920     (when parent
8921       (gnus-summary-goto-subject parent))))
8922
8923 (defun gnus-summary-down-thread (n)
8924   "Go down thread N steps.
8925 If N is negative, go up instead.
8926 Returns the difference between N and how many steps down that were
8927 taken."
8928   (interactive "p")
8929   (let ((up (< n 0))
8930         (n (abs n)))
8931     (while (and (> n 0)
8932                 (if up (gnus-summary-go-up-thread)
8933                   (gnus-summary-go-down-thread)))
8934       (setq n (1- n)))
8935     (gnus-summary-position-point)
8936     (when (/= 0 n)
8937       (gnus-message 7 "Can't go further"))
8938     n))
8939
8940 (defun gnus-summary-up-thread (n)
8941   "Go up thread N steps.
8942 If N is negative, go up instead.
8943 Returns the difference between N and how many steps down that were
8944 taken."
8945   (interactive "p")
8946   (gnus-summary-down-thread (- n)))
8947
8948 (defun gnus-summary-top-thread ()
8949   "Go to the top of the thread."
8950   (interactive)
8951   (while (gnus-summary-go-up-thread))
8952   (gnus-summary-article-number))
8953
8954 (defun gnus-summary-kill-thread (&optional unmark)
8955   "Mark articles under current thread as read.
8956 If the prefix argument is positive, remove any kinds of marks.
8957 If the prefix argument is negative, tick articles instead."
8958   (interactive "P")
8959   (when unmark
8960     (setq unmark (prefix-numeric-value unmark)))
8961   (let ((articles (gnus-summary-articles-in-thread)))
8962     (save-excursion
8963       ;; Expand the thread.
8964       (gnus-summary-show-thread)
8965       ;; Mark all the articles.
8966       (while articles
8967         (gnus-summary-goto-subject (car articles))
8968         (cond ((null unmark)
8969                (gnus-summary-mark-article-as-read gnus-killed-mark))
8970               ((> unmark 0)
8971                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8972               (t
8973                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8974         (setq articles (cdr articles))))
8975     ;; Hide killed subtrees.
8976     (and (null unmark)
8977          gnus-thread-hide-killed
8978          (gnus-summary-hide-thread))
8979     ;; If marked as read, go to next unread subject.
8980     (when (null unmark)
8981       ;; Go to next unread subject.
8982       (gnus-summary-next-subject 1 t)))
8983   (gnus-set-mode-line 'summary))
8984
8985 ;; Summary sorting commands
8986
8987 (defun gnus-summary-sort-by-number (&optional reverse)
8988   "Sort the summary buffer by article number.
8989 Argument REVERSE means reverse order."
8990   (interactive "P")
8991   (gnus-summary-sort 'number reverse))
8992
8993 (defun gnus-summary-sort-by-author (&optional reverse)
8994   "Sort the summary buffer by author name alphabetically.
8995 If case-fold-search is non-nil, case of letters is ignored.
8996 Argument REVERSE means reverse order."
8997   (interactive "P")
8998   (gnus-summary-sort 'author reverse))
8999
9000 (defun gnus-summary-sort-by-subject (&optional reverse)
9001   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9002 If case-fold-search is non-nil, case of letters is ignored.
9003 Argument REVERSE means reverse order."
9004   (interactive "P")
9005   (gnus-summary-sort 'subject reverse))
9006
9007 (defun gnus-summary-sort-by-date (&optional reverse)
9008   "Sort the summary buffer by date.
9009 Argument REVERSE means reverse order."
9010   (interactive "P")
9011   (gnus-summary-sort 'date reverse))
9012
9013 (defun gnus-summary-sort-by-score (&optional reverse)
9014   "Sort the summary buffer by score.
9015 Argument REVERSE means reverse order."
9016   (interactive "P")
9017   (gnus-summary-sort 'score reverse))
9018
9019 (defun gnus-summary-sort-by-lines (&optional reverse)
9020   "Sort the summary buffer by the number of lines.
9021 Argument REVERSE means reverse order."
9022   (interactive "P")
9023   (gnus-summary-sort 'lines reverse))
9024
9025 (defun gnus-summary-sort-by-chars (&optional reverse)
9026   "Sort the summary buffer by article length.
9027 Argument REVERSE means reverse order."
9028   (interactive "P")
9029   (gnus-summary-sort 'chars reverse))   
9030
9031 (defun gnus-summary-sort (predicate reverse)
9032   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9033   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9034          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9035          (gnus-thread-sort-functions
9036           (if (not reverse)
9037               thread
9038             `(lambda (t1 t2)
9039                (,thread t2 t1))))
9040          (gnus-article-sort-functions
9041           (if (not reverse)
9042               article
9043             `(lambda (t1 t2)
9044                (,article t2 t1))))
9045          (buffer-read-only)
9046          (gnus-summary-prepare-hook nil))
9047     ;; We do the sorting by regenerating the threads.
9048     (gnus-summary-prepare)
9049     ;; Hide subthreads if needed.
9050     (when (and gnus-show-threads gnus-thread-hide-subtree)
9051       (gnus-summary-hide-all-threads))))
9052
9053 ;; Summary saving commands.
9054
9055 (defun gnus-summary-save-article (&optional n not-saved)
9056   "Save the current article using the default saver function.
9057 If N is a positive number, save the N next articles.
9058 If N is a negative number, save the N previous articles.
9059 If N is nil and any articles have been marked with the process mark,
9060 save those articles instead.
9061 The variable `gnus-default-article-saver' specifies the saver function."
9062   (interactive "P")
9063   (let* ((articles (gnus-summary-work-articles n))
9064          (save-buffer (save-excursion
9065                         (nnheader-set-temp-buffer " *Gnus Save*")))
9066          (num (length articles))
9067          header file)
9068     (dolist (article articles)
9069       (setq header (gnus-summary-article-header article))
9070       (if (not (vectorp header))
9071           ;; This is a pseudo-article.
9072           (if (assq 'name header)
9073               (gnus-copy-file (cdr (assq 'name header)))
9074             (gnus-message 1 "Article %d is unsaveable" article))
9075         ;; This is a real article.
9076         (save-window-excursion
9077           (gnus-summary-select-article t nil nil article))
9078         (save-excursion
9079           (set-buffer save-buffer)
9080           (erase-buffer)
9081           (insert-buffer-substring gnus-original-article-buffer))
9082         (setq file (gnus-article-save save-buffer file num))
9083         (gnus-summary-remove-process-mark article)
9084         (unless not-saved
9085           (gnus-summary-set-saved-mark article))))
9086     (gnus-kill-buffer save-buffer)
9087     (gnus-summary-position-point)
9088     (gnus-set-mode-line 'summary)
9089     n))
9090
9091 (defun gnus-summary-pipe-output (&optional arg)
9092   "Pipe the current article to a subprocess.
9093 If N is a positive number, pipe the N next articles.
9094 If N is a negative number, pipe the N previous articles.
9095 If N is nil and any articles have been marked with the process mark,
9096 pipe those articles instead."
9097   (interactive "P")
9098   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9099     (gnus-summary-save-article arg t))
9100   (gnus-configure-windows 'pipe))
9101
9102 (defun gnus-summary-save-article-mail (&optional arg)
9103   "Append the current article to an mail file.
9104 If N is a positive number, save the N next articles.
9105 If N is a negative number, save the N previous articles.
9106 If N is nil and any articles have been marked with the process mark,
9107 save those articles instead."
9108   (interactive "P")
9109   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9110     (gnus-summary-save-article arg)))
9111
9112 (defun gnus-summary-save-article-rmail (&optional arg)
9113   "Append the current article to an rmail file.
9114 If N is a positive number, save the N next articles.
9115 If N is a negative number, save the N previous articles.
9116 If N is nil and any articles have been marked with the process mark,
9117 save those articles instead."
9118   (interactive "P")
9119   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9120     (gnus-summary-save-article arg)))
9121
9122 (defun gnus-summary-save-article-file (&optional arg)
9123   "Append the current article to a file.
9124 If N is a positive number, save the N next articles.
9125 If N is a negative number, save the N previous articles.
9126 If N is nil and any articles have been marked with the process mark,
9127 save those articles instead."
9128   (interactive "P")
9129   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9130     (gnus-summary-save-article arg)))
9131
9132 (defun gnus-summary-write-article-file (&optional arg)
9133   "Write the current article to a file, deleting the previous file.
9134 If N is a positive number, save the N next articles.
9135 If N is a negative number, save the N previous articles.
9136 If N is nil and any articles have been marked with the process mark,
9137 save those articles instead."
9138   (interactive "P")
9139   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9140     (gnus-summary-save-article arg)))
9141
9142 (defun gnus-summary-save-article-body-file (&optional arg)
9143   "Append the current article body to a file.
9144 If N is a positive number, save the N next articles.
9145 If N is a negative number, save the N previous articles.
9146 If N is nil and any articles have been marked with the process mark,
9147 save those articles instead."
9148   (interactive "P")
9149   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9150     (gnus-summary-save-article arg)))
9151
9152 (defun gnus-summary-pipe-message (program)
9153   "Pipe the current article through PROGRAM."
9154   (interactive "sProgram: ")
9155   (gnus-summary-select-article)
9156   (let ((mail-header-separator ""))
9157     (gnus-eval-in-buffer-window gnus-article-buffer
9158       (save-restriction
9159         (widen)
9160         (let ((start (window-start))
9161               buffer-read-only)
9162           (message-pipe-buffer-body program)
9163           (set-window-start (get-buffer-window (current-buffer)) start))))))
9164
9165 (defun gnus-get-split-value (methods)
9166   "Return a value based on the split METHODS."
9167   (let (split-name method result match)
9168     (when methods
9169       (save-excursion
9170         (set-buffer gnus-original-article-buffer)
9171         (save-restriction
9172           (nnheader-narrow-to-headers)
9173           (while methods
9174             (goto-char (point-min))
9175             (setq method (pop methods))
9176             (setq match (car method))
9177             (when (cond
9178                    ((stringp match)
9179                     ;; Regular expression.
9180                     (ignore-errors
9181                       (re-search-forward match nil t)))
9182                    ((gnus-functionp match)
9183                     ;; Function.
9184                     (save-restriction
9185                       (widen)
9186                       (setq result (funcall match gnus-newsgroup-name))))
9187                    ((consp match)
9188                     ;; Form.
9189                     (save-restriction
9190                       (widen)
9191                       (setq result (eval match)))))
9192               (setq split-name (append (cdr method) split-name))
9193               (cond ((stringp result)
9194                      (push (expand-file-name
9195                             result gnus-article-save-directory)
9196                            split-name))
9197                     ((consp result)
9198                      (setq split-name (append result split-name)))))))))
9199     (nreverse split-name)))
9200
9201 (defun gnus-valid-move-group-p (group)
9202   (and (boundp group)
9203        (symbol-name group)
9204        (symbol-value group)
9205        (gnus-get-function (gnus-find-method-for-group
9206                            (symbol-name group)) 'request-accept-article t)))
9207
9208 (defun gnus-read-move-group-name (prompt default articles prefix)
9209   "Read a group name."
9210   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9211          (minibuffer-confirm-incomplete nil) ; XEmacs
9212          (prom
9213           (format "%s %s to:"
9214                   prompt
9215                   (if (> (length articles) 1)
9216                       (format "these %d articles" (length articles))
9217                     "this article")))
9218          (to-newsgroup
9219           (cond
9220            ((null split-name)
9221             (gnus-completing-read default prom
9222                                   gnus-active-hashtb
9223                                   'gnus-valid-move-group-p
9224                                   nil prefix
9225                                   'gnus-group-history))
9226            ((= 1 (length split-name))
9227             (gnus-completing-read (car split-name) prom
9228                                   gnus-active-hashtb
9229                                   'gnus-valid-move-group-p
9230                                   nil nil
9231                                   'gnus-group-history))
9232            (t
9233             (gnus-completing-read nil prom
9234                                   (mapcar (lambda (el) (list el))
9235                                           (nreverse split-name))
9236                                   nil nil nil
9237                                   'gnus-group-history))))
9238          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9239     (when to-newsgroup
9240       (if (or (string= to-newsgroup "")
9241               (string= to-newsgroup prefix))
9242           (setq to-newsgroup default))
9243       (unless to-newsgroup
9244         (error "No group name entered"))
9245       (or (gnus-active to-newsgroup)
9246           (gnus-activate-group to-newsgroup nil nil to-method)
9247           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9248                                      to-newsgroup))
9249               (or (and (gnus-request-create-group to-newsgroup to-method)
9250                        (gnus-activate-group
9251                         to-newsgroup nil nil to-method)
9252                        (gnus-subscribe-group to-newsgroup))
9253                   (error "Couldn't create group %s" to-newsgroup)))
9254           (error "No such group: %s" to-newsgroup)))
9255     to-newsgroup))
9256
9257 (defun gnus-summary-save-parts (type dir n &optional reverse)
9258   "Save parts matching TYPE to DIR.
9259 If REVERSE, save parts that do not match TYPE."
9260   (interactive
9261    (list (read-string "Save parts of type: " "image/.*")
9262          (read-file-name "Save to directory: " nil nil t)
9263          current-prefix-arg))
9264   (gnus-summary-iterate n
9265     (let ((gnus-display-mime-function nil)
9266           (gnus-inhibit-treatment t))
9267       (gnus-summary-select-article))
9268     (save-excursion
9269       (set-buffer gnus-article-buffer)
9270       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9271         (when handles
9272           (gnus-summary-save-parts-1 type dir handles reverse)
9273           (mm-destroy-parts handles))))))
9274
9275 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9276   (if (stringp (car handle))
9277       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9278               (cdr handle))
9279     (when (if reverse
9280               (not (string-match type (mm-handle-media-type handle)))
9281             (string-match type (mm-handle-media-type handle)))
9282       (let ((file (expand-file-name
9283                    (file-name-nondirectory
9284                     (or
9285                      (mail-content-type-get
9286                       (mm-handle-disposition handle) 'filename)
9287                      (concat gnus-newsgroup-name "." gnus-current-article)))
9288                    dir)))
9289         (unless (file-exists-p file)
9290           (mm-save-part-to-file handle file))))))
9291
9292 ;; Summary extract commands
9293
9294 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9295   (let ((buffer-read-only nil)
9296         (article (gnus-summary-article-number))
9297         after-article b e)
9298     (unless (gnus-summary-goto-subject article)
9299       (error "No such article: %d" article))
9300     (gnus-summary-position-point)
9301     ;; If all commands are to be bunched up on one line, we collect
9302     ;; them here.
9303     (unless gnus-view-pseudos-separately
9304       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9305             files action)
9306         (while ps
9307           (setq action (cdr (assq 'action (car ps))))
9308           (setq files (list (cdr (assq 'name (car ps)))))
9309           (while (and ps (cdr ps)
9310                       (string= (or action "1")
9311                                (or (cdr (assq 'action (cadr ps))) "2")))
9312             (push (cdr (assq 'name (cadr ps))) files)
9313             (setcdr ps (cddr ps)))
9314           (when files
9315             (when (not (string-match "%s" action))
9316               (push " " files))
9317             (push " " files)
9318             (when (assq 'execute (car ps))
9319               (setcdr (assq 'execute (car ps))
9320                       (funcall (if (string-match "%s" action)
9321                                    'format 'concat)
9322                                action
9323                                (mapconcat
9324                                 (lambda (f)
9325                                   (if (equal f " ")
9326                                       f
9327                                     (gnus-quote-arg-for-sh-or-csh f)))
9328                                 files " ")))))
9329           (setq ps (cdr ps)))))
9330     (if (and gnus-view-pseudos (not not-view))
9331         (while pslist
9332           (when (assq 'execute (car pslist))
9333             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9334                                   (eq gnus-view-pseudos 'not-confirm)))
9335           (setq pslist (cdr pslist)))
9336       (save-excursion
9337         (while pslist
9338           (setq after-article (or (cdr (assq 'article (car pslist)))
9339                                   (gnus-summary-article-number)))
9340           (gnus-summary-goto-subject after-article)
9341           (forward-line 1)
9342           (setq b (point))
9343           (insert "    " (file-name-nondirectory
9344                           (cdr (assq 'name (car pslist))))
9345                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9346           (setq e (point))
9347           (forward-line -1)             ; back to `b'
9348           (gnus-add-text-properties
9349            b (1- e) (list 'gnus-number gnus-reffed-article-number
9350                           gnus-mouse-face-prop gnus-mouse-face))
9351           (gnus-data-enter
9352            after-article gnus-reffed-article-number
9353            gnus-unread-mark b (car pslist) 0 (- e b))
9354           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9355           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9356           (setq pslist (cdr pslist)))))))
9357
9358 (defun gnus-pseudos< (p1 p2)
9359   (let ((c1 (cdr (assq 'action p1)))
9360         (c2 (cdr (assq 'action p2))))
9361     (and c1 c2 (string< c1 c2))))
9362
9363 (defun gnus-request-pseudo-article (props)
9364   (cond ((assq 'execute props)
9365          (gnus-execute-command (cdr (assq 'execute props)))))
9366   (let ((gnus-current-article (gnus-summary-article-number)))
9367     (gnus-run-hooks 'gnus-mark-article-hook)))
9368
9369 (defun gnus-execute-command (command &optional automatic)
9370   (save-excursion
9371     (gnus-article-setup-buffer)
9372     (set-buffer gnus-article-buffer)
9373     (setq buffer-read-only nil)
9374     (let ((command (if automatic command
9375                      (read-string "Command: " (cons command 0)))))
9376       (erase-buffer)
9377       (insert "$ " command "\n\n")
9378       (if gnus-view-pseudo-asynchronously
9379           (start-process "gnus-execute" (current-buffer) shell-file-name
9380                          shell-command-switch command)
9381         (call-process shell-file-name nil t nil
9382                       shell-command-switch command)))))
9383
9384 ;; Summary kill commands.
9385
9386 (defun gnus-summary-edit-global-kill (article)
9387   "Edit the \"global\" kill file."
9388   (interactive (list (gnus-summary-article-number)))
9389   (gnus-group-edit-global-kill article))
9390
9391 (defun gnus-summary-edit-local-kill ()
9392   "Edit a local kill file applied to the current newsgroup."
9393   (interactive)
9394   (setq gnus-current-headers (gnus-summary-article-header))
9395   (gnus-group-edit-local-kill
9396    (gnus-summary-article-number) gnus-newsgroup-name))
9397
9398 ;;; Header reading.
9399
9400 (defun gnus-read-header (id &optional header)
9401   "Read the headers of article ID and enter them into the Gnus system."
9402   (let ((group gnus-newsgroup-name)
9403         (gnus-override-method
9404          (or
9405           gnus-override-method
9406           (and (gnus-news-group-p gnus-newsgroup-name)
9407                (car (gnus-refer-article-methods)))))
9408         where)
9409     ;; First we check to see whether the header in question is already
9410     ;; fetched.
9411     (if (stringp id)
9412         ;; This is a Message-ID.
9413         (setq header (or header (gnus-id-to-header id)))
9414       ;; This is an article number.
9415       (setq header (or header (gnus-summary-article-header id))))
9416     (if (and header
9417              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9418         ;; We have found the header.
9419         header
9420       ;; If this is a sparse article, we have to nix out its
9421       ;; previous entry in the thread hashtb.
9422       (when (and header
9423                  (gnus-summary-article-sparse-p (mail-header-number header)))
9424         (let* ((parent (gnus-parent-id (mail-header-references header)))
9425                (thread (and parent (gnus-id-to-thread parent))))
9426           (when thread
9427             (delq (assq header thread) thread))))
9428       ;; We have to really fetch the header to this article.
9429       (save-excursion
9430         (set-buffer nntp-server-buffer)
9431         (when (setq where (gnus-request-head id group))
9432           (nnheader-fold-continuation-lines)
9433           (goto-char (point-max))
9434           (insert ".\n")
9435           (goto-char (point-min))
9436           (insert "211 ")
9437           (princ (cond
9438                   ((numberp id) id)
9439                   ((cdr where) (cdr where))
9440                   (header (mail-header-number header))
9441                   (t gnus-reffed-article-number))
9442                  (current-buffer))
9443           (insert " Article retrieved.\n"))
9444         (if (or (not where)
9445                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9446             ()                          ; Malformed head.
9447           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9448             (when (and (stringp id)
9449                        (not (string= (gnus-group-real-name group)
9450                                      (car where))))
9451               ;; If we fetched by Message-ID and the article came
9452               ;; from a different group, we fudge some bogus article
9453               ;; numbers for this article.
9454               (mail-header-set-number header gnus-reffed-article-number))
9455             (save-excursion
9456               (set-buffer gnus-summary-buffer)
9457               (decf gnus-reffed-article-number)
9458               (gnus-remove-header (mail-header-number header))
9459               (push header gnus-newsgroup-headers)
9460               (setq gnus-current-headers header)
9461               (push (mail-header-number header) gnus-newsgroup-limit)))
9462           header)))))
9463
9464 (defun gnus-remove-header (number)
9465   "Remove header NUMBER from `gnus-newsgroup-headers'."
9466   (if (and gnus-newsgroup-headers
9467            (= number (mail-header-number (car gnus-newsgroup-headers))))
9468       (pop gnus-newsgroup-headers)
9469     (let ((headers gnus-newsgroup-headers))
9470       (while (and (cdr headers)
9471                   (not (= number (mail-header-number (cadr headers)))))
9472         (pop headers))
9473       (when (cdr headers)
9474         (setcdr headers (cddr headers))))))
9475
9476 ;;;
9477 ;;; summary highlights
9478 ;;;
9479
9480 (defun gnus-highlight-selected-summary ()
9481   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9482   ;; Highlight selected article in summary buffer
9483   (when gnus-summary-selected-face
9484     (save-excursion
9485       (let* ((beg (progn (beginning-of-line) (point)))
9486              (end (progn (end-of-line) (point)))
9487              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9488              (from (if (get-text-property beg gnus-mouse-face-prop)
9489                        beg
9490                      (or (next-single-property-change
9491                           beg gnus-mouse-face-prop nil end)
9492                          beg)))
9493              (to
9494               (if (= from end)
9495                   (- from 2)
9496                 (or (next-single-property-change
9497                      from gnus-mouse-face-prop nil end)
9498                     end))))
9499         ;; If no mouse-face prop on line we will have to = from = end,
9500         ;; so we highlight the entire line instead.
9501         (when (= (+ to 2) from)
9502           (setq from beg)
9503           (setq to end))
9504         (if gnus-newsgroup-selected-overlay
9505             ;; Move old overlay.
9506             (gnus-move-overlay
9507              gnus-newsgroup-selected-overlay from to (current-buffer))
9508           ;; Create new overlay.
9509           (gnus-overlay-put
9510            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9511            'face gnus-summary-selected-face))))))
9512
9513 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9514 (defun gnus-summary-highlight-line ()
9515   "Highlight current line according to `gnus-summary-highlight'."
9516   (let* ((list gnus-summary-highlight)
9517          (p (point))
9518          (end (progn (end-of-line) (point)))
9519          ;; now find out where the line starts and leave point there.
9520          (beg (progn (beginning-of-line) (point)))
9521          (article (gnus-summary-article-number))
9522          (score (or (cdr (assq (or article gnus-current-article)
9523                                gnus-newsgroup-scored))
9524                     gnus-summary-default-score 0))
9525          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9526          (inhibit-read-only t))
9527     ;; Eval the cars of the lists until we find a match.
9528     (let ((default gnus-summary-default-score))
9529       (while (and list
9530                   (not (eval (caar list))))
9531         (setq list (cdr list))))
9532     (let ((face (cdar list)))
9533       (unless (eq face (get-text-property beg 'face))
9534         (gnus-put-text-property-excluding-characters-with-faces
9535          beg end 'face
9536          (setq face (if (boundp face) (symbol-value face) face)))
9537         (when gnus-summary-highlight-line-function
9538           (funcall gnus-summary-highlight-line-function article face))))
9539     (goto-char p)))
9540
9541 (defun gnus-update-read-articles (group unread &optional compute)
9542   "Update the list of read articles in GROUP."
9543   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9544          (entry (gnus-gethash group gnus-newsrc-hashtb))
9545          (info (nth 2 entry))
9546          (prev 1)
9547          (unread (sort (copy-sequence unread) '<))
9548          read)
9549     (if (or (not info) (not active))
9550         ;; There is no info on this group if it was, in fact,
9551         ;; killed.  Gnus stores no information on killed groups, so
9552         ;; there's nothing to be done.
9553         ;; One could store the information somewhere temporarily,
9554         ;; perhaps...  Hmmm...
9555         ()
9556       ;; Remove any negative articles numbers.
9557       (while (and unread (< (car unread) 0))
9558         (setq unread (cdr unread)))
9559       ;; Remove any expired article numbers
9560       (while (and unread (< (car unread) (car active)))
9561         (setq unread (cdr unread)))
9562       ;; Compute the ranges of read articles by looking at the list of
9563       ;; unread articles.
9564       (while unread
9565         (when (/= (car unread) prev)
9566           (push (if (= prev (1- (car unread))) prev
9567                   (cons prev (1- (car unread))))
9568                 read))
9569         (setq prev (1+ (car unread)))
9570         (setq unread (cdr unread)))
9571       (when (<= prev (cdr active))
9572         (push (cons prev (cdr active)) read))
9573       (setq read (if (> (length read) 1) (nreverse read) read))
9574       (if compute
9575           read
9576         (save-excursion
9577           (set-buffer gnus-group-buffer)
9578           (gnus-undo-register
9579             `(progn
9580                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9581                (gnus-info-set-read ',info ',(gnus-info-read info))
9582                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9583                (gnus-group-update-group ,group t))))
9584         ;; Propagate the read marks to the backend.
9585         (if (gnus-check-backend-function 'request-set-mark group)
9586             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9587                   (add (gnus-remove-from-range read (gnus-info-read info))))
9588               (when (or add del)
9589                 (unless (gnus-check-group group)
9590                   (error "Can't open server for %s" group))
9591                 (gnus-request-set-mark
9592                  group (delq nil (list (if add (list add 'add '(read)))
9593                                        (if del (list del 'del '(read)))))))))
9594         ;; Enter this list into the group info.
9595         (gnus-info-set-read info read)
9596         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9597         (gnus-get-unread-articles-in-group info (gnus-active group))
9598         t))))
9599
9600 (defun gnus-offer-save-summaries ()
9601   "Offer to save all active summary buffers."
9602   (save-excursion
9603     (let ((buflist (buffer-list))
9604           buffers bufname)
9605       ;; Go through all buffers and find all summaries.
9606       (while buflist
9607         (and (setq bufname (buffer-name (car buflist)))
9608              (string-match "Summary" bufname)
9609              (save-excursion
9610                (set-buffer bufname)
9611                ;; We check that this is, indeed, a summary buffer.
9612                (and (eq major-mode 'gnus-summary-mode)
9613                     ;; Also make sure this isn't bogus.
9614                     gnus-newsgroup-prepared
9615                     ;; Also make sure that this isn't a dead summary buffer.
9616                     (not gnus-dead-summary-mode)))
9617              (push bufname buffers))
9618         (setq buflist (cdr buflist)))
9619       ;; Go through all these summary buffers and offer to save them.
9620       (when buffers
9621         (map-y-or-n-p
9622          "Update summary buffer %s? "
9623          (lambda (buf)
9624            (switch-to-buffer buf)
9625            (gnus-summary-exit))
9626          buffers)))))
9627
9628
9629 ;;; @ for mime-partial
9630 ;;;
9631
9632 (defun gnus-request-partial-message ()
9633   (save-excursion
9634     (let ((number (gnus-summary-article-number))
9635           (group gnus-newsgroup-name)
9636           (mother gnus-article-buffer))
9637       (set-buffer (get-buffer-create " *Partial Article*"))
9638       (erase-buffer)
9639       (setq mime-preview-buffer mother)
9640       (gnus-request-article-this-buffer number group)
9641       (mime-parse-buffer)
9642       )))
9643
9644 (autoload 'mime-combine-message/partial-pieces-automatically
9645   "mime-partial"
9646   "Internal method to combine message/partial messages automatically.")
9647
9648 (mime-add-condition
9649  'action '((type . message)(subtype . partial)
9650            (major-mode . gnus-original-article-mode)
9651            (method . mime-combine-message/partial-pieces-automatically)
9652            (summary-buffer-exp . gnus-summary-buffer)
9653            (request-partial-message-method . gnus-request-partial-message)
9654            ))
9655
9656
9657 ;;; @ for message/rfc822
9658 ;;;
9659
9660 (defun gnus-mime-extract-message/rfc822 (entity situation)
9661   (let (group article num cwin swin cur)
9662     (with-current-buffer (mime-entity-buffer entity)
9663       (save-restriction
9664         (narrow-to-region (mime-entity-body-start entity)
9665                           (mime-entity-body-end entity))
9666         (setq group (or (cdr (assq 'group situation))
9667                         (completing-read "Group: "
9668                                          gnus-active-hashtb
9669                                          nil
9670                                          (gnus-read-active-file-p)
9671                                          gnus-newsgroup-name))
9672               article (gnus-request-accept-article group)
9673               )
9674         ))
9675     (when (and (consp article)
9676                (numberp (setq article (cdr article))))
9677       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9678             cwin (get-buffer-window (current-buffer) t)
9679             )
9680       (save-window-excursion
9681         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9682             (select-window swin)
9683           (set-buffer gnus-summary-buffer)
9684           )
9685         (setq cur gnus-current-article)
9686         (forward-line num)
9687         (let (gnus-show-threads)
9688           (gnus-summary-goto-subject article t)
9689           )
9690         (gnus-summary-clear-mark-forward 1)
9691         (gnus-summary-goto-subject cur)
9692         )
9693       (when (and cwin (window-frame cwin))
9694         (select-frame (window-frame cwin))
9695         )
9696       (when (boundp 'mime-acting-situation-to-override)
9697         (set-alist 'mime-acting-situation-to-override
9698                    'group
9699                    group)
9700         (set-alist 'mime-acting-situation-to-override
9701                    'after-method
9702                    `(progn
9703                       (save-current-buffer
9704                         (set-buffer gnus-group-buffer)
9705                         (gnus-activate-group ,group)
9706                         )
9707                       (gnus-summary-goto-article ,cur
9708                                                  gnus-show-all-headers)
9709                       ))
9710         (set-alist 'mime-acting-situation-to-override
9711                    'number num)
9712         )
9713       )))
9714
9715 (mime-add-condition
9716  'action '((type . message)(subtype . rfc822)
9717            (major-mode . gnus-original-article-mode)
9718            (method . gnus-mime-extract-message/rfc822)
9719            (mode . "extract")
9720            ))
9721
9722 (mime-add-condition
9723  'action '((type . message)(subtype . news)
9724            (major-mode . gnus-original-article-mode)
9725            (method . gnus-mime-extract-message/rfc822)
9726            (mode . "extract")
9727            ))
9728
9729 (defun gnus-mime-extract-multipart (entity situation)
9730   (let ((children (mime-entity-children entity))
9731         mime-acting-situation-to-override
9732         f)
9733     (while children
9734       (mime-play-entity (car children)
9735                         (cons (assq 'mode situation)
9736                               mime-acting-situation-to-override))
9737       (setq children (cdr children)))
9738     (if (setq f (cdr (assq 'after-method
9739                            mime-acting-situation-to-override)))
9740         (eval f)
9741       )))  
9742
9743 (mime-add-condition
9744  'action '((type . multipart)
9745            (method . gnus-mime-extract-multipart)
9746            (mode . "extract")
9747            )
9748  'with-default)
9749
9750
9751 ;;; @ end
9752 ;;;
9753
9754 (defun gnus-summary-setup-default-charset ()
9755   "Setup newsgroup default charset."
9756   (if (equal gnus-newsgroup-name "nndraft:drafts")
9757       (setq gnus-newsgroup-charset nil)
9758     (let* ((name (and gnus-newsgroup-name
9759                       (gnus-group-real-name gnus-newsgroup-name)))
9760            (ignored-charsets 
9761             (or gnus-newsgroup-ephemeral-ignored-charsets
9762                 (append
9763                  (and gnus-newsgroup-name
9764                       (or (gnus-group-find-parameter gnus-newsgroup-name
9765                                                      'ignored-charsets t)
9766                           (let ((alist gnus-group-ignored-charsets-alist)
9767                                 elem (charsets nil))
9768                             (while (setq elem (pop alist))
9769                               (when (and name
9770                                          (string-match (car elem) name))
9771                                 (setq alist nil
9772                                       charsets (cdr elem))))
9773                             charsets)))
9774                  gnus-newsgroup-ignored-charsets))))
9775       (setq gnus-newsgroup-charset
9776             (or gnus-newsgroup-ephemeral-charset
9777                 (and gnus-newsgroup-name
9778                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9779                          (let ((alist gnus-group-charset-alist)
9780                                elem charset)
9781                            (while (setq elem (pop alist))
9782                              (when (and name
9783                                         (string-match (car elem) name))
9784                                (setq alist nil
9785                                      charset (cadr elem))))
9786                            charset)))
9787                 gnus-default-charset))
9788       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9789            ignored-charsets))))
9790
9791 ;;;
9792 ;;; Mime Commands
9793 ;;;
9794
9795 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9796   "Display the current article buffer fully MIME-buttonized.
9797 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9798 treated as multipart/mixed."
9799   (interactive "P")
9800   (require 'gnus-art)
9801   (let ((gnus-unbuttonized-mime-types nil)
9802         (gnus-mime-display-multipart-as-mixed show-all-parts))
9803     (gnus-summary-show-article)))
9804
9805 (defun gnus-summary-repair-multipart (article)
9806   "Add a Content-Type header to a multipart article without one."
9807   (interactive (list (gnus-summary-article-number)))
9808   (gnus-with-article article
9809     (message-narrow-to-head)
9810     (goto-char (point-max))
9811     (widen)
9812     (when (search-forward "\n--" nil t)
9813       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9814         (message-narrow-to-head)
9815         (message-remove-header "Mime-Version")
9816         (message-remove-header "Content-Type")
9817         (goto-char (point-max))
9818         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9819                         separator))
9820         (insert "Mime-Version: 1.0\n")
9821         (widen))))
9822   (let (gnus-mark-article-hook)
9823     (gnus-summary-select-article t t nil article)))
9824
9825 (defun gnus-summary-toggle-display-buttonized ()
9826   "Toggle the buttonizing of the article buffer."
9827   (interactive)
9828   (require 'gnus-art)
9829   (if (setq gnus-inhibit-mime-unbuttonizing
9830             (not gnus-inhibit-mime-unbuttonizing))
9831       (let ((gnus-unbuttonized-mime-types nil))
9832         (gnus-summary-show-article))
9833     (gnus-summary-show-article)))
9834
9835 ;;;
9836 ;;; Intelli-mouse commmands
9837 ;;;
9838
9839 (defun gnus-wheel-summary-scroll (event)
9840   (interactive "e")
9841   (let ((amount (if (memq 'shift (event-modifiers event))
9842                     (car gnus-wheel-scroll-amount)
9843                   (cdr gnus-wheel-scroll-amount)))
9844         (direction (- (* (static-if (featurep 'xemacs)
9845                              (event-button event)
9846                            (cond ((eq 'mouse-4 (event-basic-type event))
9847                                   4)
9848                                  ((eq 'mouse-5 (event-basic-type event))
9849                                   5)))
9850                          2) 9))
9851         edge)
9852     (gnus-summary-scroll-up (* amount direction))
9853     (when (gnus-eval-in-buffer-window gnus-article-buffer
9854             (save-restriction
9855               (widen)
9856               (and (if (< 0 direction)
9857                        (gnus-article-next-page 0)
9858                      (gnus-article-prev-page 0)
9859                      (bobp))
9860                    (if (setq edge (get-text-property
9861                                    (point-min) 'gnus-wheel-edge))
9862                        (setq edge (* edge direction))
9863                      (setq edge -1))
9864                    (or (plusp edge)
9865                        (let ((buffer-read-only nil)
9866                              (inhibit-read-only t))
9867                          (put-text-property (point-min) (point-max)
9868                                             'gnus-wheel-edge direction)
9869                          nil))
9870                    (or (> edge gnus-wheel-edge-resistance)
9871                        (let ((buffer-read-only nil)
9872                              (inhibit-read-only t))
9873                          (put-text-property (point-min) (point-max)
9874                                             'gnus-wheel-edge
9875                                             (* (1+ edge) direction))
9876                          nil))
9877                    (eq last-command 'gnus-wheel-summary-scroll))))
9878       (gnus-summary-next-article nil nil (minusp direction)))))
9879
9880 (defun gnus-wheel-install ()
9881   "Enable mouse wheel support on summary window."
9882   (when gnus-use-wheel
9883     (let ((keys 
9884            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9885       (dolist (key keys)
9886         (define-key gnus-summary-mode-map key
9887           'gnus-wheel-summary-scroll)))))
9888
9889 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9890
9891 ;;;
9892 ;;; Traditional PGP commmands
9893 ;;;
9894
9895 (defun gnus-summary-decrypt-article (&optional force)
9896   "Decrypt the current article in traditional PGP way.
9897 This will have permanent effect only in mail groups.
9898 If FORCE is non-nil, allow editing of articles even in read-only
9899 groups."
9900   (interactive "P")
9901   (gnus-summary-select-article t)
9902   (gnus-eval-in-buffer-window gnus-article-buffer
9903     (save-excursion
9904       (save-restriction
9905         (widen)
9906         (goto-char (point-min))
9907         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9908           (error "Not a traditional PGP message!"))
9909         (let ((armor-start (match-beginning 0)))
9910           (if (and (pgg-decrypt-region armor-start (point-max))
9911                    (or force (not (gnus-group-read-only-p))))
9912               (let ((inhibit-read-only t) 
9913                     buffer-read-only)
9914                 (delete-region armor-start
9915                                (progn 
9916                                  (re-search-forward "^-+END PGP" nil t)
9917                                  (beginning-of-line 2)
9918                                  (point)))
9919                 (insert-buffer-substring pgg-output-buffer))))))))
9920
9921 (defun gnus-summary-verify-article ()
9922   "Verify the current article in traditional PGP way."
9923   (interactive)
9924   (save-excursion
9925     (set-buffer gnus-original-article-buffer)
9926     (goto-char (point-min))
9927     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9928       (error "Not a traditional PGP message!"))
9929     (re-search-forward "^-+END PGP" nil t)
9930     (beginning-of-line 2)
9931     (call-interactively (function pgg-verify-region))))
9932
9933 ;;;
9934 ;;; with article
9935 ;;;
9936
9937 (defmacro gnus-with-article (article &rest forms)
9938   "Select ARTICLE and perform FORMS in the original article buffer.
9939 Then replace the article with the result."
9940   `(progn
9941      ;; We don't want the article to be marked as read.
9942      (let (gnus-mark-article-hook)
9943        (gnus-summary-select-article t t nil ,article))
9944      (set-buffer gnus-original-article-buffer)
9945      ,@forms
9946      (if (not (gnus-check-backend-function
9947                'request-replace-article (car gnus-article-current)))
9948          (gnus-message 5 "Read-only group; not replacing")
9949        (unless (gnus-request-replace-article
9950                 ,article (car gnus-article-current)
9951                 (current-buffer) t)
9952          (error "Couldn't replace article")))
9953      ;; The cache and backlog have to be flushed somewhat.
9954      (when gnus-keep-backlog
9955        (gnus-backlog-remove-article
9956         (car gnus-article-current) (cdr gnus-article-current)))
9957      (when gnus-use-cache
9958        (gnus-cache-update-article
9959         (car gnus-article-current) (cdr gnus-article-current)))))
9960
9961 (put 'gnus-with-article 'lisp-indent-function 1)
9962 (put 'gnus-with-article 'edebug-form-spec '(form body))
9963
9964 ;;;
9965 ;;; Generic summary marking commands
9966 ;;;
9967
9968 (defvar gnus-summary-marking-alist
9969   '((read gnus-del-mark "d")
9970     (unread gnus-unread-mark "u")
9971     (ticked gnus-ticked-mark "!")
9972     (dormant gnus-dormant-mark "?")
9973     (expirable gnus-expirable-mark "e"))
9974   "An alist of names/marks/keystrokes.")
9975
9976 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9977 (defvar gnus-summary-mark-map)
9978
9979 (defun gnus-summary-make-all-marking-commands ()
9980   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9981   (dolist (elem gnus-summary-marking-alist)
9982     (apply 'gnus-summary-make-marking-command elem)))
9983
9984 (defun gnus-summary-make-marking-command (name mark keystroke)
9985   (let ((map (make-sparse-keymap)))
9986     (define-key gnus-summary-generic-mark-map keystroke map)
9987     (dolist (lway `((next "next" next nil "n")
9988                     (next-unread "next unread" next t "N")
9989                     (prev "previous" prev nil "p")
9990                     (prev-unread "previous unread" prev t "P")
9991                     (nomove "" nil nil ,keystroke)))
9992       (let ((func (gnus-summary-make-marking-command-1
9993                    mark (car lway) lway name)))
9994         (setq func (eval func))
9995         (define-key map (nth 4 lway) func)))))
9996       
9997 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9998   `(defun ,(intern
9999             (format "gnus-summary-put-mark-as-%s%s"
10000                     name (if (eq way 'nomove)
10001                              ""
10002                            (concat "-" (symbol-name way)))))
10003      (n)
10004      ,(format
10005        "Mark the current article as %s%s.
10006 If N, the prefix, then repeat N times.
10007 If N is negative, move in reverse order.
10008 The difference between N and the actual number of articles marked is
10009 returned."
10010        name (car (cdr lway)))
10011      (interactive "p")
10012      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10013     
10014 (defun gnus-summary-generic-mark (n mark move unread)
10015   "Mark N articles with MARK."
10016   (unless (eq major-mode 'gnus-summary-mode)
10017     (error "This command can only be used in the summary buffer"))
10018   (gnus-summary-show-thread)
10019   (let ((nummove
10020          (cond
10021           ((eq move 'next) 1)
10022           ((eq move 'prev) -1)
10023           (t 0))))
10024     (if (zerop nummove)
10025         (setq n 1)
10026       (when (< n 0)
10027         (setq n (abs n)
10028               nummove (* -1 nummove))))
10029     (while (and (> n 0)
10030                 (gnus-summary-mark-article nil mark)
10031                 (zerop (gnus-summary-next-subject nummove unread t)))
10032       (setq n (1- n)))
10033     (when (/= 0 n)
10034       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10035     (gnus-summary-recenter)
10036     (gnus-summary-position-point)
10037     (gnus-set-mode-line 'summary)
10038     n))
10039
10040 (gnus-summary-make-all-marking-commands)
10041
10042 (gnus-ems-redefine)
10043
10044 (provide 'gnus-sum)
10045
10046 (run-hooks 'gnus-sum-load-hook)
10047
10048 ;;; gnus-sum.el ends here