* lisp/gnus-sum.el: Add autoload setting for `pgg-decrypt-region'
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   (require 'static))
44
45 (eval-and-compile
46   (autoload 'gnus-cache-articles-in-group "gnus-cache")
47   (autoload 'pgg-decrypt-region "pgg" nil t)
48   (autoload 'pgg-verify-region "pgg" nil t))
49
50 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
51 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
116   "*A regexp to match subjects to be excluded from loose thread gathering.
117 As loose thread gathering is done on subjects only, that means that
118 there can be many false gatherings performed.  By rooting out certain
119 common subjects, gathering might become saner."
120   :group 'gnus-thread
121   :type 'regexp)
122
123 (defcustom gnus-summary-gather-subject-limit nil
124   "*Maximum length of subject comparisons when gathering loose threads.
125 Use nil to compare full subjects.  Setting this variable to a low
126 number will help gather threads that have been corrupted by
127 newsreaders chopping off subject lines, but it might also mean that
128 unrelated articles that have subject that happen to begin with the
129 same few characters will be incorrectly gathered.
130
131 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
132 comparing subjects."
133   :group 'gnus-thread
134   :type '(choice (const :tag "off" nil)
135                  (const fuzzy)
136                  (sexp :menu-tag "on" t)))
137
138 (defcustom gnus-simplify-subject-functions nil
139   "List of functions taking a string argument that simplify subjects.
140 The functions are applied recursively.
141
142 Useful functions to put in this list include: `gnus-simplify-subject-re',
143 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
144   :group 'gnus-thread
145   :type '(repeat function))
146
147 (defcustom gnus-simplify-ignored-prefixes nil
148   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
149   :group 'gnus-thread
150   :type '(choice (const :tag "off" nil)
151                  regexp))
152
153 (defcustom gnus-build-sparse-threads nil
154   "*If non-nil, fill in the gaps in threads.
155 If `some', only fill in the gaps that are needed to tie loose threads
156 together.  If `more', fill in all leaf nodes that Gnus can find.  If
157 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  (const some)
161                  (const more)
162                  (sexp :menu-tag "all" t)))
163
164 (defcustom gnus-summary-thread-gathering-function
165   'gnus-gather-threads-by-subject
166   "*Function used for gathering loose threads.
167 There are two pre-defined functions: `gnus-gather-threads-by-subject',
168 which only takes Subjects into consideration; and
169 `gnus-gather-threads-by-references', which compared the References
170 headers of the articles to find matches."
171   :group 'gnus-thread
172   :type '(radio (function-item gnus-gather-threads-by-subject)
173                 (function-item gnus-gather-threads-by-references)
174                 (function :tag "other")))
175
176 (defcustom gnus-summary-same-subject ""
177   "*String indicating that the current article has the same subject as the previous.
178 This variable will only be used if the value of
179 `gnus-summary-make-false-root' is `empty'."
180   :group 'gnus-summary-format
181   :type 'string)
182
183 (defcustom gnus-summary-goto-unread t
184   "*If t, many commands will go to the next unread article.
185 This applies to marking commands as well as other commands that
186 \"naturally\" select the next article, like, for instance, `SPC' at
187 the end of an article.
188 If nil, only the marking commands will go to the next (un)read article.
189 If `never', commands that usually go to the next unread article, will
190 go to the next article, whether it is read or not."
191   :group 'gnus-summary-marks
192   :link '(custom-manual "(gnus)Setting Marks")
193   :type '(choice (const :tag "off" nil)
194                  (const never)
195                  (sexp :menu-tag "on" t)))
196
197 (defcustom gnus-summary-default-score 0
198   "*Default article score level.
199 All scores generated by the score files will be added to this score.
200 If this variable is nil, scoring will be disabled."
201   :group 'gnus-score-default
202   :type '(choice (const :tag "disable")
203                  integer))
204
205 (defcustom gnus-summary-zcore-fuzz 0
206   "*Fuzziness factor for the zcore in the summary buffer.
207 Articles with scores closer than this to `gnus-summary-default-score'
208 will not be marked."
209   :group 'gnus-summary-format
210   :type 'integer)
211
212 (defcustom gnus-simplify-subject-fuzzy-regexp nil
213   "*Strings to be removed when doing fuzzy matches.
214 This can either be a regular expression or list of regular expressions
215 that will be removed from subject strings if fuzzy subject
216 simplification is selected."
217   :group 'gnus-thread
218   :type '(repeat regexp))
219
220 (defcustom gnus-show-threads t
221   "*If non-nil, display threads in summary mode."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-hide-subtree nil
226   "*If non-nil, hide all threads initially.
227 If threads are hidden, you have to run the command
228 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
229 to expose hidden threads."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-hide-killed t
234   "*If non-nil, hide killed threads automatically."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-ignore-subject t
239   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
240 If nil, articles that have different subjects from their parents will
241 start separate threads."
242   :group 'gnus-thread
243   :type 'boolean)
244
245 (defcustom gnus-thread-operation-ignore-subject t
246   "*If non-nil, subjects will be ignored when doing thread commands.
247 This affects commands like `gnus-summary-kill-thread' and
248 `gnus-summary-lower-thread'.
249
250 If this variable is nil, articles in the same thread with different
251 subjects will not be included in the operation in question.  If this
252 variable is `fuzzy', only articles that have subjects that are fuzzily
253 equal will be included."
254   :group 'gnus-thread
255   :type '(choice (const :tag "off" nil)
256                  (const fuzzy)
257                  (sexp :tag "on" t)))
258
259 (defcustom gnus-thread-indent-level 4
260   "*Number that says how much each sub-thread should be indented."
261   :group 'gnus-thread
262   :type 'integer)
263
264 (defcustom gnus-auto-extend-newsgroup t
265   "*If non-nil, extend newsgroup forward and backward when requested."
266   :group 'gnus-summary-choose
267   :type 'boolean)
268
269 (defcustom gnus-auto-select-first t
270   "*If nil, don't select the first unread article when entering a group.
271 If this variable is `best', select the highest-scored unread article
272 in the group.  If t, select the first unread article.
273
274 This variable can also be a function to place point on a likely
275 subject line.  Useful values include `gnus-summary-first-unread-subject',
276 `gnus-summary-first-unread-article' and
277 `gnus-summary-best-unread-article'.
278
279 If you want to prevent automatic selection of the first unread article
280 in some newsgroups, set the variable to nil in
281 `gnus-select-group-hook'."
282   :group 'gnus-group-select
283   :type '(choice (const :tag "none" nil)
284                  (const best)
285                  (sexp :menu-tag "first" t)
286                  (function-item gnus-summary-first-unread-subject)
287                  (function-item gnus-summary-first-unread-article)
288                  (function-item gnus-summary-best-unread-article)))
289
290 (defcustom gnus-dont-select-after-jump-to-other-group nil
291   "If non-nil, don't select the first unread article after entering the
292 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
293 it is depend on the value of `gnus-auto-select-first' whether to select
294 or not."
295   :group 'gnus-group-select
296   :type 'boolean)
297
298 (defcustom gnus-auto-select-next t
299   "*If non-nil, offer to go to the next group from the end of the previous.
300 If the value is t and the next newsgroup is empty, Gnus will exit
301 summary mode and go back to group mode.  If the value is neither nil
302 nor t, Gnus will select the following unread newsgroup.  In
303 particular, if the value is the symbol `quietly', the next unread
304 newsgroup will be selected without any confirmation, and if it is
305 `almost-quietly', the next group will be selected without any
306 confirmation if you are located on the last article in the group.
307 Finally, if this variable is `slightly-quietly', the `Z n' command
308 will go to the next group without confirmation."
309   :group 'gnus-summary-maneuvering
310   :type '(choice (const :tag "off" nil)
311                  (const quietly)
312                  (const almost-quietly)
313                  (const slightly-quietly)
314                  (sexp :menu-tag "on" t)))
315
316 (defcustom gnus-auto-select-same nil
317   "*If non-nil, select the next article with the same subject.
318 If there are no more articles with the same subject, go to
319 the first unread article."
320   :group 'gnus-summary-maneuvering
321   :type 'boolean)
322
323 (defcustom gnus-summary-check-current nil
324   "*If non-nil, consider the current article when moving.
325 The \"unread\" movement commands will stay on the same line if the
326 current article is unread."
327   :group 'gnus-summary-maneuvering
328   :type 'boolean)
329
330 (defcustom gnus-auto-center-summary t
331   "*If non-nil, always center the current summary buffer.
332 In particular, if `vertical' do only vertical recentering.  If non-nil
333 and non-`vertical', do both horizontal and vertical recentering."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "none" nil)
336                  (const vertical)
337                  (integer :tag "height")
338                  (sexp :menu-tag "both" t)))
339
340 (defcustom gnus-show-all-headers nil
341   "*If non-nil, don't hide any headers."
342   :group 'gnus-article-hiding
343   :group 'gnus-article-headers
344   :type 'boolean)
345
346 (defcustom gnus-summary-ignore-duplicates nil
347   "*If non-nil, ignore articles with identical Message-ID headers."
348   :group 'gnus-summary
349   :type 'boolean)
350
351 (defcustom gnus-single-article-buffer t
352   "*If non-nil, display all articles in the same buffer.
353 If nil, each group will get its own article buffer."
354   :group 'gnus-article-various
355   :type 'boolean)
356
357 (defcustom gnus-break-pages t
358   "*If non-nil, do page breaking on articles.
359 The page delimiter is specified by the `gnus-page-delimiter'
360 variable."
361   :group 'gnus-article-various
362   :type 'boolean)
363
364 (defcustom gnus-show-mime t
365   "*If non-nil, do mime processing of articles.
366 The articles will simply be fed to the function given by
367 `gnus-article-display-method-for-mime'."
368   :group 'gnus-article-mime
369   :type 'boolean)
370
371 (defcustom gnus-move-split-methods nil
372   "*Variable used to suggest where articles are to be moved to.
373 It uses the same syntax as the `gnus-split-methods' variable."
374   :group 'gnus-summary-mail
375   :type '(repeat (choice (list :value (fun) function)
376                          (cons :value ("" "") regexp (repeat string))
377                          (sexp :value nil))))
378
379 (defcustom gnus-unread-mark ?  ;Whitespace
380   "*Mark used for unread articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-ticked-mark ?!
385   "*Mark used for ticked articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-dormant-mark ??
390   "*Mark used for dormant articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-del-mark ?r
395   "*Mark used for del'd articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-read-mark ?R
400   "*Mark used for read articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-expirable-mark ?E
405   "*Mark used for expirable articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-killed-mark ?K
410   "*Mark used for killed articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-souped-mark ?F
415   "*Mark used for killed articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-kill-file-mark ?X
420   "*Mark used for articles killed by kill files."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-low-score-mark ?Y
425   "*Mark used for articles with a low score."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-catchup-mark ?C
430   "*Mark used for articles that are caught up."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-replied-mark ?A
435   "*Mark used for articles that have been replied to."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-cached-mark ?*
440   "*Mark used for articles that are in the cache."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-saved-mark ?S
445   "*Mark used for articles that have been saved to."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-ancient-mark ?O
450   "*Mark used for ancient articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-sparse-mark ?Q
455   "*Mark used for sparsely reffed articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-canceled-mark ?G
460   "*Mark used for canceled articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-duplicate-mark ?M
465   "*Mark used for duplicate articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-undownloaded-mark ?@
470   "*Mark used for articles that weren't downloaded."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-downloadable-mark ?%
475   "*Mark used for articles that are to be downloaded."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unsendable-mark ?=
480   "*Mark used for articles that won't be sent."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-score-over-mark ?+
485   "*Score mark used for articles with high scores."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-score-below-mark ?-
490   "*Score mark used for articles with low scores."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-empty-thread-mark ?  ;Whitespace
495   "*There is no thread under the article."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-not-empty-thread-mark ?=
500   "*There is a thread under the article."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-view-pseudo-asynchronously nil
505   "*If non-nil, Gnus will view pseudo-articles asynchronously."
506   :group 'gnus-extract-view
507   :type 'boolean)
508
509 (defcustom gnus-auto-expirable-marks
510   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
511         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
512         gnus-souped-mark gnus-duplicate-mark)
513   "*The list of marks converted into expiration if a group is auto-expirable."
514   :group 'gnus-summary
515   :type '(repeat character))
516
517 (defcustom gnus-inhibit-user-auto-expire t
518   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
519   :group 'gnus-summary
520   :type 'boolean)
521
522 (defcustom gnus-view-pseudos nil
523   "*If `automatic', pseudo-articles will be viewed automatically.
524 If `not-confirm', pseudos will be viewed automatically, and the user
525 will not be asked to confirm the command."
526   :group 'gnus-extract-view
527   :type '(choice (const :tag "off" nil)
528                  (const automatic)
529                  (const not-confirm)))
530
531 (defcustom gnus-view-pseudos-separately t
532   "*If non-nil, one pseudo-article will be created for each file to be viewed.
533 If nil, all files that use the same viewing command will be given as a
534 list of parameters to that command."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-insert-pseudo-articles t
539   "*If non-nil, insert pseudo-articles when decoding articles."
540   :group 'gnus-extract-view
541   :type 'boolean)
542
543 (defcustom gnus-summary-dummy-line-format
544   "  %(:                          :%) %S\n"
545   "*The format specification for the dummy roots in the summary buffer.
546 It works along the same lines as a normal formatting string,
547 with some simple extensions.
548
549 %S  The subject"
550   :group 'gnus-threading
551   :type 'string)
552
553 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
554   "*The format specification for the summary mode line.
555 It works along the same lines as a normal formatting string,
556 with some simple extensions:
557
558 %G  Group name
559 %p  Unprefixed group name
560 %A  Current article number
561 %z  Current article score
562 %V  Gnus version
563 %U  Number of unread articles in the group
564 %e  Number of unselected articles in the group
565 %Z  A string with unread/unselected article counts
566 %g  Shortish group name
567 %S  Subject of the current article
568 %u  User-defined spec
569 %s  Current score file name
570 %d  Number of dormant articles
571 %r  Number of articles that have been marked as read in this session
572 %E  Number of articles expunged by the score files"
573   :group 'gnus-summary-format
574   :type 'string)
575
576 (defcustom gnus-list-identifiers nil
577   "Regexp that matches list identifiers to be removed from subject.
578 This can also be a list of regexps."
579   :group 'gnus-summary-format
580   :group 'gnus-article-hiding
581   :type '(choice (const :tag "none" nil)
582                  (regexp :value ".*")
583                  (repeat :value (".*") regexp)))
584
585 (defcustom gnus-summary-mark-below 0
586   "*Mark all articles with a score below this variable as read.
587 This variable is local to each summary buffer and usually set by the
588 score file."
589   :group 'gnus-score-default
590   :type 'integer)
591
592 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
593   "*List of functions used for sorting articles in the summary buffer.
594 This variable is only used when not using a threaded display."
595   :group 'gnus-summary-sort
596   :type '(repeat (choice (function-item gnus-article-sort-by-number)
597                          (function-item gnus-article-sort-by-author)
598                          (function-item gnus-article-sort-by-subject)
599                          (function-item gnus-article-sort-by-date)
600                          (function-item gnus-article-sort-by-score)
601                          (function :tag "other"))))
602
603 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
604   "*List of functions used for sorting threads in the summary buffer.
605 By default, threads are sorted by article number.
606
607 Each function takes two threads and return non-nil if the first thread
608 should be sorted before the other.  If you use more than one function,
609 the primary sort function should be the last.  You should probably
610 always include `gnus-thread-sort-by-number' in the list of sorting
611 functions -- preferably first.
612
613 Ready-made functions include `gnus-thread-sort-by-number',
614 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
615 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
616 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
617   :group 'gnus-summary-sort
618   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
619                          (function-item gnus-thread-sort-by-author)
620                          (function-item gnus-thread-sort-by-subject)
621                          (function-item gnus-thread-sort-by-date)
622                          (function-item gnus-thread-sort-by-score)
623                          (function-item gnus-thread-sort-by-total-score)
624                          (function :tag "other"))))
625
626 (defcustom gnus-thread-score-function '+
627   "*Function used for calculating the total score of a thread.
628
629 The function is called with the scores of the article and each
630 subthread and should then return the score of the thread.
631
632 Some functions you can use are `+', `max', or `min'."
633   :group 'gnus-summary-sort
634   :type 'function)
635
636 (defcustom gnus-summary-expunge-below nil
637   "All articles that have a score less than this variable will be expunged.
638 This variable is local to the summary buffers."
639   :group 'gnus-score-default
640   :type '(choice (const :tag "off" nil)
641                  integer))
642
643 (defcustom gnus-thread-expunge-below nil
644   "All threads that have a total score less than this variable will be expunged.
645 See `gnus-thread-score-function' for en explanation of what a
646 \"thread score\" is.
647
648 This variable is local to the summary buffers."
649   :group 'gnus-threading
650   :group 'gnus-score-default
651   :type '(choice (const :tag "off" nil)
652                  integer))
653
654 (defcustom gnus-summary-mode-hook nil
655   "*A hook for Gnus summary mode.
656 This hook is run before any variables are set in the summary buffer."
657   :group 'gnus-summary-various
658   :type 'hook)
659
660 (defcustom gnus-summary-menu-hook nil
661   "*Hook run after the creation of the summary mode menu."
662   :group 'gnus-summary-visual
663   :type 'hook)
664
665 (defcustom gnus-summary-exit-hook nil
666   "*A hook called on exit from the summary buffer.
667 It will be called with point in the group buffer."
668   :group 'gnus-summary-exit
669   :type 'hook)
670
671 (defcustom gnus-summary-prepare-hook nil
672   "*A hook called after the summary buffer has been generated.
673 If you want to modify the summary buffer, you can use this hook."
674   :group 'gnus-summary-various
675   :type 'hook)
676
677 (defcustom gnus-summary-prepared-hook nil
678   "*A hook called as the last thing after the summary buffer has been generated."
679   :group 'gnus-summary-various
680   :type 'hook)
681
682 (defcustom gnus-summary-generate-hook nil
683   "*A hook run just before generating the summary buffer.
684 This hook is commonly used to customize threading variables and the
685 like."
686   :group 'gnus-summary-various
687   :type 'hook)
688
689 (defcustom gnus-select-group-hook nil
690   "*A hook called when a newsgroup is selected.
691
692 If you'd like to simplify subjects like the
693 `gnus-summary-next-same-subject' command does, you can use the
694 following hook:
695
696  (setq gnus-select-group-hook
697       (list
698         (lambda ()
699           (mapcar (lambda (header)
700                      (mail-header-set-subject
701                       header
702                       (gnus-simplify-subject
703                        (mail-header-subject header) 're-only)))
704                   gnus-newsgroup-headers))))"
705   :group 'gnus-group-select
706   :type 'hook)
707
708 (defcustom gnus-select-article-hook nil
709   "*A hook called when an article is selected."
710   :group 'gnus-summary-choose
711   :type 'hook)
712
713 (defcustom gnus-visual-mark-article-hook
714   (list 'gnus-highlight-selected-summary)
715   "*Hook run after selecting an article in the summary buffer.
716 It is meant to be used for highlighting the article in some way.  It
717 is not run if `gnus-visual' is nil."
718   :group 'gnus-summary-visual
719   :type 'hook)
720
721 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
722   "*A hook called before parsing the headers."
723   :group 'gnus-various
724   :type 'hook)
725
726 (defcustom gnus-exit-group-hook nil
727   "*A hook called when exiting (not quitting) summary mode."
728   :group 'gnus-various
729   :type 'hook)
730
731 (defcustom gnus-summary-update-hook
732   (list 'gnus-summary-highlight-line)
733   "*A hook called when a summary line is changed.
734 The hook will not be called if `gnus-visual' is nil.
735
736 The default function `gnus-summary-highlight-line' will
737 highlight the line according to the `gnus-summary-highlight'
738 variable."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
743   "*A hook called when an article is selected for the first time.
744 The hook is intended to mark an article as read (or unread)
745 automatically when it is selected."
746   :group 'gnus-summary-choose
747   :type 'hook)
748
749 (defcustom gnus-group-no-more-groups-hook nil
750   "*A hook run when returning to group mode having no more (unread) groups."
751   :group 'gnus-group-select
752   :type 'hook)
753
754 (defcustom gnus-ps-print-hook nil
755   "*A hook run before ps-printing something from Gnus."
756   :group 'gnus-summary
757   :type 'hook)
758
759 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
760   "Face used for highlighting the current article in the summary buffer."
761   :group 'gnus-summary-visual
762   :type 'face)
763
764 (defcustom gnus-summary-highlight
765   '(((= mark gnus-canceled-mark)
766      . gnus-summary-cancelled-face)
767     ((and (> score default)
768           (or (= mark gnus-dormant-mark)
769               (= mark gnus-ticked-mark)))
770      . gnus-summary-high-ticked-face)
771     ((and (< score default)
772           (or (= mark gnus-dormant-mark)
773               (= mark gnus-ticked-mark)))
774      . gnus-summary-low-ticked-face)
775     ((or (= mark gnus-dormant-mark)
776          (= mark gnus-ticked-mark))
777      . gnus-summary-normal-ticked-face)
778     ((and (> score default) (= mark gnus-ancient-mark))
779      . gnus-summary-high-ancient-face)
780     ((and (< score default) (= mark gnus-ancient-mark))
781      . gnus-summary-low-ancient-face)
782     ((= mark gnus-ancient-mark)
783      . gnus-summary-normal-ancient-face)
784     ((and (> score default) (= mark gnus-unread-mark))
785      . gnus-summary-high-unread-face)
786     ((and (< score default) (= mark gnus-unread-mark))
787      . gnus-summary-low-unread-face)
788     ((and (memq article gnus-newsgroup-incorporated) 
789           (= mark gnus-unread-mark))
790      . gnus-summary-incorporated-face)
791     ((= mark gnus-unread-mark)
792      . gnus-summary-normal-unread-face)
793     ((and (> score default) (memq mark (list gnus-downloadable-mark
794                                              gnus-undownloaded-mark)))
795      . gnus-summary-high-unread-face)
796     ((and (< score default) (memq mark (list gnus-downloadable-mark
797                                              gnus-undownloaded-mark)))
798      . gnus-summary-low-unread-face)
799     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
800      . gnus-summary-normal-unread-face)
801     ((> score default)
802      . gnus-summary-high-read-face)
803     ((< score default)
804      . gnus-summary-low-read-face)
805     (t
806      . gnus-summary-normal-read-face))
807   "*Controls the highlighting of summary buffer lines.
808
809 A list of (FORM . FACE) pairs.  When deciding how a a particular
810 summary line should be displayed, each form is evaluated.  The content
811 of the face field after the first true form is used.  You can change
812 how those summary lines are displayed, by editing the face field.
813
814 You can use the following variables in the FORM field.
815
816 score:   The articles score
817 default: The default article score.
818 below:   The score below which articles are automatically marked as read.
819 mark:    The articles mark."
820   :group 'gnus-summary-visual
821   :type '(repeat (cons (sexp :tag "Form" nil)
822                        face)))
823
824 (defcustom gnus-alter-header-function nil
825   "Function called to allow alteration of article header structures.
826 The function is called with one parameter, the article header vector,
827 which it may alter in any way.")
828
829 (defvar gnus-decode-encoded-word-function
830   (mime-find-field-decoder 'From 'nov)
831   "Variable that says which function should be used to decode a string with encoded words.")
832
833 (defcustom gnus-extra-headers nil
834   "*Extra headers to parse."
835   :group 'gnus-summary
836   :type '(repeat symbol))
837
838 (defcustom gnus-ignored-from-addresses
839   (and user-mail-address (regexp-quote user-mail-address))
840   "*Regexp of From headers that may be suppressed in favor of To headers."
841   :group 'gnus-summary
842   :type 'regexp)
843
844 (defcustom gnus-group-charset-alist
845   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
846     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
847     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
848     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
849     ("^relcom\\>" koi8-r)
850     ("^fido7\\>" koi8-r)
851     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
852     ("^israel\\>" iso-8859-1)
853     ("^han\\>" euc-kr)
854     ("^alt.chinese.text.big5\\>" chinese-big5)
855     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
856     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
857     (".*" iso-8859-1))
858   "Alist of regexps (to match group names) and default charsets to be used when reading."
859   :type '(repeat (list (regexp :tag "Group")
860                        (symbol :tag "Charset")))
861   :group 'gnus-charset)
862
863 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
864   "List of charsets that should be ignored.
865 When these charsets are used in the \"charset\" parameter, the
866 default charset will be used instead."
867   :type '(repeat symbol)
868   :group 'gnus-charset)
869
870 (defcustom gnus-group-ignored-charsets-alist 
871   '(("alt\\.chinese\\.text" iso-8859-1))
872   "Alist of regexps (to match group names) and charsets that should be ignored.
873 When these charsets are used in the \"charset\" parameter, the
874 default charset will be used instead."
875   :type '(repeat (cons (regexp :tag "Group")
876                        (repeat symbol)))
877   :group 'gnus-charset)
878
879 (defcustom gnus-group-highlight-words-alist nil
880   "Alist of group regexps and highlight regexps.
881 This variable uses the same syntax as `gnus-emphasis-alist'."
882   :type '(repeat (cons (regexp :tag "Group")
883                        (repeat (list (regexp :tag "Highlight regexp")
884                                      (number :tag "Group for entire word" 0)
885                                      (number :tag "Group for displayed part" 0)
886                                      (symbol :tag "Face" 
887                                              gnus-emphasis-highlight-words)))))
888   :group 'gnus-summary-visual)
889
890 (defcustom gnus-use-wheel nil
891   "Use Intelli-mouse on summary movement"
892   :type 'boolean
893   :group 'gnus-summary-maneuvering)
894
895 (defcustom gnus-wheel-scroll-amount '(5 . 1)
896   "Amount to scroll messages by spinning the mouse wheel.
897 This is actually a cons cell, where the first item is the amount to scroll
898 on a normal wheel event, and the second is the amount to scroll when the
899 wheel is moved with the shift key depressed."
900   :type '(cons (integer :tag "Shift") integer)
901   :group 'gnus-summary-maneuvering)
902
903 (defcustom gnus-wheel-edge-resistance 2
904   "How hard it should be to change the current article
905 by moving the mouse over the edge of the article window."
906   :type 'integer
907   :group 'gnus-summary-maneuvering)
908
909 (defcustom gnus-summary-show-article-charset-alist
910   nil
911   "Alist of number and charset.
912 The article will be shown with the charset corresponding to the
913 numbered argument.
914 For example: ((1 . cn-gb-2312) (2 . big5))."
915   :type '(repeat (cons (number :tag "Argument" 1)
916                        (symbol :tag "Charset")))
917   :group 'gnus-charset)
918
919 (defcustom gnus-preserve-marks t
920   "Whether marks are preserved when moving, copying and respooling messages."
921   :type 'boolean
922   :group 'gnus-summary-marks)
923
924 ;;; Internal variables
925
926 (defvar gnus-scores-exclude-files nil)
927 (defvar gnus-page-broken nil)
928 (defvar gnus-inhibit-mime-unbuttonizing nil)
929
930 (defvar gnus-original-article nil)
931 (defvar gnus-article-internal-prepare-hook nil)
932 (defvar gnus-newsgroup-process-stack nil)
933
934 (defvar gnus-thread-indent-array nil)
935 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
936 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
937   "Function called to sort the articles within a thread after it has been gathered together.")
938
939 ;; Avoid highlighting in kill files.
940 (defvar gnus-summary-inhibit-highlight nil)
941 (defvar gnus-newsgroup-selected-overlay nil)
942 (defvar gnus-inhibit-limiting nil)
943 (defvar gnus-newsgroup-adaptive-score-file nil)
944 (defvar gnus-current-score-file nil)
945 (defvar gnus-current-move-group nil)
946 (defvar gnus-current-copy-group nil)
947 (defvar gnus-current-crosspost-group nil)
948
949 (defvar gnus-newsgroup-dependencies nil)
950 (defvar gnus-newsgroup-adaptive nil)
951 (defvar gnus-summary-display-article-function nil)
952 (defvar gnus-summary-highlight-line-function nil
953   "Function called after highlighting a summary line.")
954
955 (defvar gnus-summary-line-format-alist
956   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
957     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
958     (?s gnus-tmp-subject-or-nil ?s)
959     (?n gnus-tmp-name ?s)
960     (?A (std11-address-string
961          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
962     (?a (or (std11-full-name-string
963              (car (mime-entity-read-field gnus-tmp-header 'From)))
964             gnus-tmp-from) ?s)
965     (?F gnus-tmp-from ?s)
966     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
967     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
968     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
969     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
970     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
971     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
972     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
973     (?L gnus-tmp-lines ?d)
974     (?I gnus-tmp-indentation ?s)
975     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
976     (?R gnus-tmp-replied ?c)
977     (?\[ gnus-tmp-opening-bracket ?c)
978     (?\] gnus-tmp-closing-bracket ?c)
979     (?\> (make-string gnus-tmp-level ? ) ?s)
980     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
981     (?i gnus-tmp-score ?d)
982     (?z gnus-tmp-score-char ?c)
983     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
984     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
985     (?U gnus-tmp-unread ?c)
986     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
987     (?t (gnus-summary-number-of-articles-in-thread
988          (and (boundp 'thread) (car thread)) gnus-tmp-level)
989         ?d)
990     (?e (gnus-summary-number-of-articles-in-thread
991          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
992         ?c)
993     (?u gnus-tmp-user-defined ?s)
994     (?P (gnus-pick-line-number) ?d))
995   "An alist of format specifications that can appear in summary lines,
996 and what variables they correspond with, along with the type of the
997 variable (string, integer, character, etc).")
998
999 (defvar gnus-summary-dummy-line-format-alist
1000   `((?S gnus-tmp-subject ?s)
1001     (?N gnus-tmp-number ?d)
1002     (?u gnus-tmp-user-defined ?s)))
1003
1004 (defvar gnus-summary-mode-line-format-alist
1005   `((?G gnus-tmp-group-name ?s)
1006     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1007     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1008     (?A gnus-tmp-article-number ?d)
1009     (?Z gnus-tmp-unread-and-unselected ?s)
1010     (?V gnus-version ?s)
1011     (?U gnus-tmp-unread-and-unticked ?d)
1012     (?S gnus-tmp-subject ?s)
1013     (?e gnus-tmp-unselected ?d)
1014     (?u gnus-tmp-user-defined ?s)
1015     (?d (length gnus-newsgroup-dormant) ?d)
1016     (?t (length gnus-newsgroup-marked) ?d)
1017     (?r (length gnus-newsgroup-reads) ?d)
1018     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1019     (?E gnus-newsgroup-expunged-tally ?d)
1020     (?s (gnus-current-score-file-nondirectory) ?s)))
1021
1022 (defvar gnus-last-search-regexp nil
1023   "Default regexp for article search command.")
1024
1025 (defvar gnus-summary-search-article-matched-data nil
1026   "Last matched data of article search command.  It is the local variable
1027 in `gnus-article-buffer' which consists of the list of start position,
1028 end position and text.")
1029
1030 (defvar gnus-last-shell-command nil
1031   "Default shell command on article.")
1032
1033 (defvar gnus-newsgroup-begin nil)
1034 (defvar gnus-newsgroup-end nil)
1035 (defvar gnus-newsgroup-last-rmail nil)
1036 (defvar gnus-newsgroup-last-mail nil)
1037 (defvar gnus-newsgroup-last-folder nil)
1038 (defvar gnus-newsgroup-last-file nil)
1039 (defvar gnus-newsgroup-auto-expire nil)
1040 (defvar gnus-newsgroup-active nil)
1041
1042 (defvar gnus-newsgroup-data nil)
1043 (defvar gnus-newsgroup-data-reverse nil)
1044 (defvar gnus-newsgroup-limit nil)
1045 (defvar gnus-newsgroup-limits nil)
1046
1047 (defvar gnus-newsgroup-unreads nil
1048   "List of unread articles in the current newsgroup.")
1049
1050 (defvar gnus-newsgroup-unselected nil
1051   "List of unselected unread articles in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-reads nil
1054   "Alist of read articles and article marks in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-expunged-tally nil)
1057
1058 (defvar gnus-newsgroup-marked nil
1059   "List of ticked articles in the current newsgroup (a subset of unread art).")
1060
1061 (defvar gnus-newsgroup-killed nil
1062   "List of ranges of articles that have been through the scoring process.")
1063
1064 (defvar gnus-newsgroup-cached nil
1065   "List of articles that come from the article cache.")
1066
1067 (defvar gnus-newsgroup-saved nil
1068   "List of articles that have been saved.")
1069
1070 (defvar gnus-newsgroup-kill-headers nil)
1071
1072 (defvar gnus-newsgroup-replied nil
1073   "List of articles that have been replied to in the current newsgroup.")
1074
1075 (defvar gnus-newsgroup-expirable nil
1076   "List of articles in the current newsgroup that can be expired.")
1077
1078 (defvar gnus-newsgroup-processable nil
1079   "List of articles in the current newsgroup that can be processed.")
1080
1081 (defvar gnus-newsgroup-downloadable nil
1082   "List of articles in the current newsgroup that can be processed.")
1083
1084 (defvar gnus-newsgroup-undownloaded nil
1085   "List of articles in the current newsgroup that haven't been downloaded..")
1086
1087 (defvar gnus-newsgroup-unsendable nil
1088   "List of articles in the current newsgroup that won't be sent.")
1089
1090 (defvar gnus-newsgroup-bookmarks nil
1091   "List of articles in the current newsgroup that have bookmarks.")
1092
1093 (defvar gnus-newsgroup-dormant nil
1094   "List of dormant articles in the current newsgroup.")
1095
1096 (defvar gnus-newsgroup-scored nil
1097   "List of scored articles in the current newsgroup.")
1098
1099 (defvar gnus-newsgroup-incorporated nil
1100   "List of incorporated articles in the current newsgroup.")
1101
1102 (defvar gnus-newsgroup-headers nil
1103   "List of article headers in the current newsgroup.")
1104
1105 (defvar gnus-newsgroup-threads nil)
1106
1107 (defvar gnus-newsgroup-prepared nil
1108   "Whether the current group has been prepared properly.")
1109
1110 (defvar gnus-newsgroup-ancient nil
1111   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-sparse nil)
1114
1115 (defvar gnus-current-article nil)
1116 (defvar gnus-article-current nil)
1117 (defvar gnus-current-headers nil)
1118 (defvar gnus-have-all-headers nil)
1119 (defvar gnus-last-article nil)
1120 (defvar gnus-newsgroup-history nil)
1121 (defvar gnus-newsgroup-charset nil)
1122 (defvar gnus-newsgroup-ephemeral-charset nil)
1123 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1124
1125 (defconst gnus-summary-local-variables
1126   '(gnus-newsgroup-name
1127     gnus-newsgroup-begin gnus-newsgroup-end
1128     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1129     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1130     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1131     gnus-newsgroup-unselected gnus-newsgroup-marked
1132     gnus-newsgroup-reads gnus-newsgroup-saved
1133     gnus-newsgroup-replied gnus-newsgroup-expirable
1134     gnus-newsgroup-processable gnus-newsgroup-killed
1135     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1136     gnus-newsgroup-unsendable
1137     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1138     gnus-newsgroup-headers gnus-newsgroup-threads
1139     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1140     gnus-current-article gnus-current-headers gnus-have-all-headers
1141     gnus-last-article gnus-article-internal-prepare-hook
1142     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1143     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1144     gnus-thread-expunge-below
1145     gnus-score-alist gnus-current-score-file
1146     (gnus-summary-expunge-below . global)
1147     (gnus-summary-mark-below . global)
1148     gnus-newsgroup-active gnus-scores-exclude-files
1149     gnus-newsgroup-history gnus-newsgroup-ancient
1150     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1151     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1152     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1153     (gnus-newsgroup-expunged-tally . 0)
1154     gnus-cache-removable-articles gnus-newsgroup-cached
1155     gnus-newsgroup-data gnus-newsgroup-data-reverse
1156     gnus-newsgroup-limit gnus-newsgroup-limits
1157     gnus-newsgroup-charset
1158     gnus-newsgroup-incorporated)
1159   "Variables that are buffer-local to the summary buffers.")
1160
1161 ;; Byte-compiler warning.
1162 (defvar gnus-article-mode-map)
1163
1164 ;; Subject simplification.
1165
1166 (defun gnus-simplify-whitespace (str)
1167   "Remove excessive whitespace."
1168   (let ((mystr str))
1169     ;; Multiple spaces.
1170     (while (string-match "[ \t][ \t]+" mystr)
1171       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1172                           " "
1173                           (substring mystr (match-end 0)))))
1174     ;; Leading spaces.
1175     (when (string-match "^[ \t]+" mystr)
1176       (setq mystr (substring mystr (match-end 0))))
1177     ;; Trailing spaces.
1178     (when (string-match "[ \t]+$" mystr)
1179       (setq mystr (substring mystr 0 (match-beginning 0))))
1180     mystr))
1181
1182 (defsubst gnus-simplify-subject-re (subject)
1183   "Remove \"Re:\" from subject lines."
1184   (if (string-match "^[Rr][Ee]: *" subject)
1185       (substring subject (match-end 0))
1186     subject))
1187
1188 (defun gnus-simplify-subject (subject &optional re-only)
1189   "Remove `Re:' and words in parentheses.
1190 If RE-ONLY is non-nil, strip leading `Re:'s only."
1191   (let ((case-fold-search t))           ;Ignore case.
1192     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1193     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1194       (setq subject (substring subject (match-end 0))))
1195     ;; Remove uninteresting prefixes.
1196     (when (and (not re-only)
1197                gnus-simplify-ignored-prefixes
1198                (string-match gnus-simplify-ignored-prefixes subject))
1199       (setq subject (substring subject (match-end 0))))
1200     ;; Remove words in parentheses from end.
1201     (unless re-only
1202       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1203         (setq subject (substring subject 0 (match-beginning 0)))))
1204     ;; Return subject string.
1205     subject))
1206
1207 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1208 ;; all whitespace.
1209 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1210   (goto-char (point-min))
1211   (while (re-search-forward regexp nil t)
1212     (replace-match (or newtext ""))))
1213
1214 (defun gnus-simplify-buffer-fuzzy ()
1215   "Simplify string in the buffer fuzzily.
1216 The string in the accessible portion of the current buffer is simplified.
1217 It is assumed to be a single-line subject.
1218 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1219 matter is removed.  Additional things can be deleted by setting
1220 gnus-simplify-subject-fuzzy-regexp."
1221   (let ((case-fold-search t)
1222         (modified-tick))
1223     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1224
1225     (while (not (eq modified-tick (buffer-modified-tick)))
1226       (setq modified-tick (buffer-modified-tick))
1227       (cond
1228        ((listp gnus-simplify-subject-fuzzy-regexp)
1229         (mapcar 'gnus-simplify-buffer-fuzzy-step
1230                 gnus-simplify-subject-fuzzy-regexp))
1231        (gnus-simplify-subject-fuzzy-regexp
1232         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1233       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1234       (gnus-simplify-buffer-fuzzy-step
1235        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1236       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1237
1238     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1239     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1240     (gnus-simplify-buffer-fuzzy-step " $")
1241     (gnus-simplify-buffer-fuzzy-step "^ +")))
1242
1243 (defun gnus-simplify-subject-fuzzy (subject)
1244   "Simplify a subject string fuzzily.
1245 See `gnus-simplify-buffer-fuzzy' for details."
1246   (save-excursion
1247     (gnus-set-work-buffer)
1248     (let ((case-fold-search t))
1249       ;; Remove uninteresting prefixes.
1250       (when (and gnus-simplify-ignored-prefixes
1251                  (string-match gnus-simplify-ignored-prefixes subject))
1252         (setq subject (substring subject (match-end 0))))
1253       (insert subject)
1254       (inline (gnus-simplify-buffer-fuzzy))
1255       (buffer-string))))
1256
1257 (defsubst gnus-simplify-subject-fully (subject)
1258   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1259   (cond
1260    (gnus-simplify-subject-functions
1261     (gnus-map-function gnus-simplify-subject-functions subject))
1262    ((null gnus-summary-gather-subject-limit)
1263     (gnus-simplify-subject-re subject))
1264    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1265     (gnus-simplify-subject-fuzzy subject))
1266    ((numberp gnus-summary-gather-subject-limit)
1267     (gnus-limit-string (gnus-simplify-subject-re subject)
1268                        gnus-summary-gather-subject-limit))
1269    (t
1270     subject)))
1271
1272 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1273   "Check whether two subjects are equal.
1274 If optional argument simple-first is t, first argument is already
1275 simplified."
1276   (cond
1277    ((null simple-first)
1278     (equal (gnus-simplify-subject-fully s1)
1279            (gnus-simplify-subject-fully s2)))
1280    (t
1281     (equal s1
1282            (gnus-simplify-subject-fully s2)))))
1283
1284 (defun gnus-summary-bubble-group ()
1285   "Increase the score of the current group.
1286 This is a handy function to add to `gnus-summary-exit-hook' to
1287 increase the score of each group you read."
1288   (gnus-group-add-score gnus-newsgroup-name))
1289
1290 \f
1291 ;;;
1292 ;;; Gnus summary mode
1293 ;;;
1294
1295 (put 'gnus-summary-mode 'mode-class 'special)
1296
1297 (when t
1298   ;; Non-orthogonal keys
1299
1300   (gnus-define-keys gnus-summary-mode-map
1301     " " gnus-summary-next-page
1302     "\177" gnus-summary-prev-page
1303     [delete] gnus-summary-prev-page
1304     [backspace] gnus-summary-prev-page
1305     "\r" gnus-summary-scroll-up
1306     "\M-\r" gnus-summary-scroll-down
1307     "n" gnus-summary-next-unread-article
1308     "p" gnus-summary-prev-unread-article
1309     "N" gnus-summary-next-article
1310     "P" gnus-summary-prev-article
1311     "\M-\C-n" gnus-summary-next-same-subject
1312     "\M-\C-p" gnus-summary-prev-same-subject
1313     "\M-n" gnus-summary-next-unread-subject
1314     "\M-p" gnus-summary-prev-unread-subject
1315     "." gnus-summary-first-unread-article
1316     "," gnus-summary-best-unread-article
1317     "\M-s" gnus-summary-search-article-forward
1318     "\M-r" gnus-summary-search-article-backward
1319     "<" gnus-summary-beginning-of-article
1320     ">" gnus-summary-end-of-article
1321     "j" gnus-summary-goto-article
1322     "^" gnus-summary-refer-parent-article
1323     "\M-^" gnus-summary-refer-article
1324     "u" gnus-summary-tick-article-forward
1325     "!" gnus-summary-tick-article-forward
1326     "U" gnus-summary-tick-article-backward
1327     "d" gnus-summary-mark-as-read-forward
1328     "D" gnus-summary-mark-as-read-backward
1329     "E" gnus-summary-mark-as-expirable
1330     "\M-u" gnus-summary-clear-mark-forward
1331     "\M-U" gnus-summary-clear-mark-backward
1332     "k" gnus-summary-kill-same-subject-and-select
1333     "\C-k" gnus-summary-kill-same-subject
1334     "\M-\C-k" gnus-summary-kill-thread
1335     "\M-\C-l" gnus-summary-lower-thread
1336     "e" gnus-summary-edit-article
1337     "#" gnus-summary-mark-as-processable
1338     "\M-#" gnus-summary-unmark-as-processable
1339     "\M-\C-t" gnus-summary-toggle-threads
1340     "\M-\C-s" gnus-summary-show-thread
1341     "\M-\C-h" gnus-summary-hide-thread
1342     "\M-\C-f" gnus-summary-next-thread
1343     "\M-\C-b" gnus-summary-prev-thread
1344     "\M-\C-u" gnus-summary-up-thread
1345     "\M-\C-d" gnus-summary-down-thread
1346     "&" gnus-summary-execute-command
1347     "c" gnus-summary-catchup-and-exit
1348     "\C-w" gnus-summary-mark-region-as-read
1349     "\C-t" gnus-summary-toggle-truncation
1350     "?" gnus-summary-mark-as-dormant
1351     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1352     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1353     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1354     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1355     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1356     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1357     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1358     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1359     "=" gnus-summary-expand-window
1360     "\C-x\C-s" gnus-summary-reselect-current-group
1361     "\M-g" gnus-summary-rescan-group
1362     "w" gnus-summary-stop-page-breaking
1363     "\C-c\C-r" gnus-summary-caesar-message
1364     "\M-t" gnus-summary-toggle-mime
1365     "f" gnus-summary-followup
1366     "F" gnus-summary-followup-with-original
1367     "C" gnus-summary-cancel-article
1368     "r" gnus-summary-reply
1369     "R" gnus-summary-reply-with-original
1370     "\C-c\C-f" gnus-summary-mail-forward
1371     "o" gnus-summary-save-article
1372     "\C-o" gnus-summary-save-article-mail
1373     "|" gnus-summary-pipe-output
1374     "\M-k" gnus-summary-edit-local-kill
1375     "\M-K" gnus-summary-edit-global-kill
1376     ;; "V" gnus-version
1377     "\C-c\C-d" gnus-summary-describe-group
1378     "q" gnus-summary-exit
1379     "Q" gnus-summary-exit-no-update
1380     "\C-c\C-i" gnus-info-find-node
1381     gnus-mouse-2 gnus-mouse-pick-article
1382     "m" gnus-summary-mail-other-window
1383     "a" gnus-summary-post-news
1384     "x" gnus-summary-limit-to-unread
1385     "s" gnus-summary-isearch-article
1386     "t" gnus-article-toggle-headers
1387     "g" gnus-summary-show-article
1388     "l" gnus-summary-goto-last-article
1389     "v" gnus-summary-preview-mime-message
1390     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1391     "\C-d" gnus-summary-enter-digest-group
1392     "\M-\C-d" gnus-summary-read-document
1393     "\M-\C-e" gnus-summary-edit-parameters
1394     "\M-\C-a" gnus-summary-customize-parameters
1395     "\C-c\C-b" gnus-bug
1396     "*" gnus-cache-enter-article
1397     "\M-*" gnus-cache-remove-article
1398     "\M-&" gnus-summary-universal-argument
1399     "\C-l" gnus-recenter
1400     "I" gnus-summary-increase-score
1401     "L" gnus-summary-lower-score
1402     "\M-i" gnus-symbolic-argument
1403     "h" gnus-summary-select-article-buffer
1404
1405     "V" gnus-summary-score-map
1406     "X" gnus-uu-extract-map
1407     "S" gnus-summary-send-map)
1408
1409   ;; Sort of orthogonal keymap
1410   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1411     "t" gnus-summary-tick-article-forward
1412     "!" gnus-summary-tick-article-forward
1413     "d" gnus-summary-mark-as-read-forward
1414     "r" gnus-summary-mark-as-read-forward
1415     "c" gnus-summary-clear-mark-forward
1416     " " gnus-summary-clear-mark-forward
1417     "e" gnus-summary-mark-as-expirable
1418     "x" gnus-summary-mark-as-expirable
1419     "?" gnus-summary-mark-as-dormant
1420     "b" gnus-summary-set-bookmark
1421     "B" gnus-summary-remove-bookmark
1422     "#" gnus-summary-mark-as-processable
1423     "\M-#" gnus-summary-unmark-as-processable
1424     "S" gnus-summary-limit-include-expunged
1425     "C" gnus-summary-catchup
1426     "H" gnus-summary-catchup-to-here
1427     "\C-c" gnus-summary-catchup-all
1428     "k" gnus-summary-kill-same-subject-and-select
1429     "K" gnus-summary-kill-same-subject
1430     "P" gnus-uu-mark-map)
1431
1432   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1433     "c" gnus-summary-clear-above
1434     "u" gnus-summary-tick-above
1435     "m" gnus-summary-mark-above
1436     "k" gnus-summary-kill-below)
1437
1438   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1439     "/" gnus-summary-limit-to-subject
1440     "n" gnus-summary-limit-to-articles
1441     "w" gnus-summary-pop-limit
1442     "s" gnus-summary-limit-to-subject
1443     "a" gnus-summary-limit-to-author
1444     "u" gnus-summary-limit-to-unread
1445     "m" gnus-summary-limit-to-marks
1446     "M" gnus-summary-limit-exclude-marks
1447     "v" gnus-summary-limit-to-score
1448     "*" gnus-summary-limit-include-cached
1449     "D" gnus-summary-limit-include-dormant
1450     "T" gnus-summary-limit-include-thread
1451     "d" gnus-summary-limit-exclude-dormant
1452     "t" gnus-summary-limit-to-age
1453     "x" gnus-summary-limit-to-extra 
1454     "E" gnus-summary-limit-include-expunged
1455     "c" gnus-summary-limit-exclude-childless-dormant
1456     "C" gnus-summary-limit-mark-excluded-as-read)
1457
1458   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1459     "n" gnus-summary-next-unread-article
1460     "p" gnus-summary-prev-unread-article
1461     "N" gnus-summary-next-article
1462     "P" gnus-summary-prev-article
1463     "\C-n" gnus-summary-next-same-subject
1464     "\C-p" gnus-summary-prev-same-subject
1465     "\M-n" gnus-summary-next-unread-subject
1466     "\M-p" gnus-summary-prev-unread-subject
1467     "f" gnus-summary-first-unread-article
1468     "b" gnus-summary-best-unread-article
1469     "j" gnus-summary-goto-article
1470     "g" gnus-summary-goto-subject
1471     "l" gnus-summary-goto-last-article
1472     "o" gnus-summary-pop-article)
1473
1474   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1475     "k" gnus-summary-kill-thread
1476     "l" gnus-summary-lower-thread
1477     "i" gnus-summary-raise-thread
1478     "T" gnus-summary-toggle-threads
1479     "t" gnus-summary-rethread-current
1480     "^" gnus-summary-reparent-thread
1481     "s" gnus-summary-show-thread
1482     "S" gnus-summary-show-all-threads
1483     "h" gnus-summary-hide-thread
1484     "H" gnus-summary-hide-all-threads
1485     "n" gnus-summary-next-thread
1486     "p" gnus-summary-prev-thread
1487     "u" gnus-summary-up-thread
1488     "o" gnus-summary-top-thread
1489     "d" gnus-summary-down-thread
1490     "#" gnus-uu-mark-thread
1491     "\M-#" gnus-uu-unmark-thread)
1492
1493   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1494     "g" gnus-summary-prepare
1495     "c" gnus-summary-insert-cached-articles)
1496
1497   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1498     "c" gnus-summary-catchup-and-exit
1499     "C" gnus-summary-catchup-all-and-exit
1500     "E" gnus-summary-exit-no-update
1501     "J" gnus-summary-jump-to-other-group
1502     "Q" gnus-summary-exit
1503     "Z" gnus-summary-exit
1504     "n" gnus-summary-catchup-and-goto-next-group
1505     "R" gnus-summary-reselect-current-group
1506     "G" gnus-summary-rescan-group
1507     "N" gnus-summary-next-group
1508     "s" gnus-summary-save-newsrc
1509     "P" gnus-summary-prev-group)
1510
1511   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1512     " " gnus-summary-next-page
1513     "n" gnus-summary-next-page
1514     "\177" gnus-summary-prev-page
1515     [delete] gnus-summary-prev-page
1516     "p" gnus-summary-prev-page
1517     "\r" gnus-summary-scroll-up
1518     "\M-\r" gnus-summary-scroll-down
1519     "<" gnus-summary-beginning-of-article
1520     ">" gnus-summary-end-of-article
1521     "b" gnus-summary-beginning-of-article
1522     "e" gnus-summary-end-of-article
1523     "^" gnus-summary-refer-parent-article
1524     "r" gnus-summary-refer-parent-article
1525     "D" gnus-summary-enter-digest-group
1526     "R" gnus-summary-refer-references
1527     "T" gnus-summary-refer-thread
1528     "g" gnus-summary-show-article
1529     "s" gnus-summary-isearch-article
1530     "P" gnus-summary-print-article
1531     "t" gnus-article-babel
1532     "d" gnus-summary-decrypt-article
1533     "v" gnus-summary-verify-article)
1534
1535   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1536     "b" gnus-article-add-buttons
1537     "B" gnus-article-add-buttons-to-head
1538     "o" gnus-article-treat-overstrike
1539     "e" gnus-article-emphasize
1540     "w" gnus-article-fill-cited-article
1541     "Q" gnus-article-fill-long-lines
1542     "C" gnus-article-capitalize-sentences
1543     "c" gnus-article-remove-cr
1544     "f" gnus-article-display-x-face
1545     "l" gnus-summary-stop-page-breaking
1546     "r" gnus-summary-caesar-message
1547     "t" gnus-article-toggle-headers
1548     "v" gnus-summary-verbose-headers
1549     "m" gnus-summary-toggle-mime
1550     "H" gnus-article-strip-headers-in-body
1551     "d" gnus-article-treat-dumbquotes
1552     "s" gnus-smiley-display)
1553
1554   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1555     "a" gnus-article-hide
1556     "h" gnus-article-toggle-headers
1557     "b" gnus-article-hide-boring-headers
1558     "s" gnus-article-hide-signature
1559     "c" gnus-article-hide-citation
1560     "C" gnus-article-hide-citation-in-followups
1561     "l" gnus-article-hide-list-identifiers
1562     "p" gnus-article-hide-pgp
1563     "B" gnus-article-strip-banner
1564     "P" gnus-article-hide-pem
1565     "\C-c" gnus-article-hide-citation-maybe)
1566
1567   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1568     "a" gnus-article-highlight
1569     "h" gnus-article-highlight-headers
1570     "c" gnus-article-highlight-citation
1571     "s" gnus-article-highlight-signature)
1572
1573   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1574     "z" gnus-article-date-ut
1575     "u" gnus-article-date-ut
1576     "l" gnus-article-date-local
1577     "e" gnus-article-date-lapsed
1578     "o" gnus-article-date-original
1579     "i" gnus-article-date-iso8601
1580     "s" gnus-article-date-user)
1581
1582   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1583     "t" gnus-article-remove-trailing-blank-lines
1584     "l" gnus-article-strip-leading-blank-lines
1585     "m" gnus-article-strip-multiple-blank-lines
1586     "a" gnus-article-strip-blank-lines
1587     "A" gnus-article-strip-all-blank-lines
1588     "s" gnus-article-strip-leading-space
1589     "e" gnus-article-strip-trailing-space)
1590
1591   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1592     "v" gnus-version
1593     "f" gnus-summary-fetch-faq
1594     "d" gnus-summary-describe-group
1595     "h" gnus-summary-describe-briefly
1596     "i" gnus-info-find-node)
1597
1598   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1599     "e" gnus-summary-expire-articles
1600     "\M-\C-e" gnus-summary-expire-articles-now
1601     "\177" gnus-summary-delete-article
1602     [delete] gnus-summary-delete-article
1603     [backspace] gnus-summary-delete-article
1604     "m" gnus-summary-move-article
1605     "r" gnus-summary-respool-article
1606     "w" gnus-summary-edit-article
1607     "c" gnus-summary-copy-article
1608     "B" gnus-summary-crosspost-article
1609     "q" gnus-summary-respool-query
1610     "t" gnus-summary-respool-trace
1611     "i" gnus-summary-import-article
1612     "p" gnus-summary-article-posted-p)
1613
1614   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1615     "o" gnus-summary-save-article
1616     "m" gnus-summary-save-article-mail
1617     "F" gnus-summary-write-article-file
1618     "r" gnus-summary-save-article-rmail
1619     "f" gnus-summary-save-article-file
1620     "b" gnus-summary-save-article-body-file
1621     "h" gnus-summary-save-article-folder
1622     "v" gnus-summary-save-article-vm
1623     "p" gnus-summary-pipe-output
1624     "s" gnus-soup-add-article)
1625
1626   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1627     "b" gnus-summary-display-buttonized
1628     "m" gnus-summary-repair-multipart
1629     "v" gnus-article-view-part
1630     "o" gnus-article-save-part
1631     "c" gnus-article-copy-part
1632     "e" gnus-article-externalize-part
1633     "i" gnus-article-inline-part
1634     "|" gnus-article-pipe-part))
1635
1636 (defun gnus-summary-make-menu-bar ()
1637   (gnus-turn-off-edit-menu 'summary)
1638
1639   (unless (boundp 'gnus-summary-misc-menu)
1640
1641     (easy-menu-define
1642      gnus-summary-kill-menu gnus-summary-mode-map ""
1643      (cons
1644       "Score"
1645       (nconc
1646        (list
1647         ["Enter score..." gnus-summary-score-entry t]
1648         ["Customize" gnus-score-customize t])
1649        (gnus-make-score-map 'increase)
1650        (gnus-make-score-map 'lower)
1651        '(("Mark"
1652           ["Kill below" gnus-summary-kill-below t]
1653           ["Mark above" gnus-summary-mark-above t]
1654           ["Tick above" gnus-summary-tick-above t]
1655           ["Clear above" gnus-summary-clear-above t])
1656          ["Current score" gnus-summary-current-score t]
1657          ["Set score" gnus-summary-set-score t]
1658          ["Switch current score file..." gnus-score-change-score-file t]
1659          ["Set mark below..." gnus-score-set-mark-below t]
1660          ["Set expunge below..." gnus-score-set-expunge-below t]
1661          ["Edit current score file" gnus-score-edit-current-scores t]
1662          ["Edit score file" gnus-score-edit-file t]
1663          ["Trace score" gnus-score-find-trace t]
1664          ["Find words" gnus-score-find-favourite-words t]
1665          ["Rescore buffer" gnus-summary-rescore t]
1666          ["Increase score..." gnus-summary-increase-score t]
1667          ["Lower score..." gnus-summary-lower-score t]))))
1668
1669     ;; Define both the Article menu in the summary buffer and the equivalent
1670     ;; Commands menu in the article buffer here for consistency.
1671     (let ((innards
1672            '(("Hide"
1673               ["All" gnus-article-hide t]
1674               ["Headers" gnus-article-toggle-headers t]
1675               ["Signature" gnus-article-hide-signature t]
1676               ["Citation" gnus-article-hide-citation t]
1677               ["List identifiers" gnus-article-hide-list-identifiers t]
1678               ["PGP" gnus-article-hide-pgp t]
1679               ["Banner" gnus-article-strip-banner t]
1680               ["Boring headers" gnus-article-hide-boring-headers t])
1681              ("Highlight"
1682               ["All" gnus-article-highlight t]
1683               ["Headers" gnus-article-highlight-headers t]
1684               ["Signature" gnus-article-highlight-signature t]
1685               ["Citation" gnus-article-highlight-citation t])
1686              ("Date"
1687               ["Local" gnus-article-date-local t]
1688               ["ISO8601" gnus-article-date-iso8601 t]
1689               ["UT" gnus-article-date-ut t]
1690               ["Original" gnus-article-date-original t]
1691               ["Lapsed" gnus-article-date-lapsed t]
1692               ["User-defined" gnus-article-date-user t])
1693              ("Washing"
1694               ("Remove Blanks"
1695                ["Leading" gnus-article-strip-leading-blank-lines t]
1696                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1697                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1698                ["All of the above" gnus-article-strip-blank-lines t]
1699                ["All" gnus-article-strip-all-blank-lines t]
1700                ["Leading space" gnus-article-strip-leading-space t]
1701                ["Trailing space" gnus-article-strip-trailing-space t])
1702               ["Overstrike" gnus-article-treat-overstrike t]
1703               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1704               ["Emphasis" gnus-article-emphasize t]
1705               ["Word wrap" gnus-article-fill-cited-article t]
1706               ["Fill long lines" gnus-article-fill-long-lines t]
1707               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1708               ["CR" gnus-article-remove-cr t]
1709               ["Show X-Face" gnus-article-display-x-face t]
1710               ["Rot 13" gnus-summary-caesar-message t]
1711               ["Unix pipe" gnus-summary-pipe-message t]
1712               ["Add buttons" gnus-article-add-buttons t]
1713               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1714               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1715               ["Toggle MIME" gnus-summary-toggle-mime t]
1716               ["Verbose header" gnus-summary-verbose-headers t]
1717               ["Toggle header" gnus-summary-toggle-header t]
1718               ["Toggle smileys" gnus-smiley-display t]
1719               ["HZ" gnus-article-decode-HZ t])
1720              ("Output"
1721               ["Save in default format" gnus-summary-save-article t]
1722               ["Save in file" gnus-summary-save-article-file t]
1723               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1724               ["Save in MH folder" gnus-summary-save-article-folder t]
1725               ["Save in VM folder" gnus-summary-save-article-vm t]
1726               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1727               ["Save body in file" gnus-summary-save-article-body-file t]
1728               ["Pipe through a filter" gnus-summary-pipe-output t]
1729               ["Add to SOUP packet" gnus-soup-add-article t]
1730               ["Print" gnus-summary-print-article t])
1731              ("Backend"
1732               ["Respool article..." gnus-summary-respool-article t]
1733               ["Move article..." gnus-summary-move-article
1734                (gnus-check-backend-function
1735                 'request-move-article gnus-newsgroup-name)]
1736               ["Copy article..." gnus-summary-copy-article t]
1737               ["Crosspost article..." gnus-summary-crosspost-article
1738                (gnus-check-backend-function
1739                 'request-replace-article gnus-newsgroup-name)]
1740               ["Import file..." gnus-summary-import-article t]
1741               ["Check if posted" gnus-summary-article-posted-p t]
1742               ["Edit article" gnus-summary-edit-article
1743                (not (gnus-group-read-only-p))]
1744               ["Delete article" gnus-summary-delete-article
1745                (gnus-check-backend-function
1746                 'request-expire-articles gnus-newsgroup-name)]
1747               ["Query respool" gnus-summary-respool-query t]
1748               ["Trace respool" gnus-summary-respool-trace t]
1749               ["Delete expirable articles" gnus-summary-expire-articles-now
1750                (gnus-check-backend-function
1751                 'request-expire-articles gnus-newsgroup-name)])
1752              ("Extract"
1753               ["Uudecode" gnus-uu-decode-uu t]
1754               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1755               ["Unshar" gnus-uu-decode-unshar t]
1756               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1757               ["Save" gnus-uu-decode-save t]
1758               ["Binhex" gnus-uu-decode-binhex t]
1759               ["Postscript" gnus-uu-decode-postscript t])
1760              ("Cache"
1761               ["Enter article" gnus-cache-enter-article t]
1762               ["Remove article" gnus-cache-remove-article t])
1763              ["Translate" gnus-article-babel t]
1764              ["Select article buffer" gnus-summary-select-article-buffer t]
1765              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1766              ["Isearch article..." gnus-summary-isearch-article t]
1767              ["Beginning of the article" gnus-summary-beginning-of-article t]
1768              ["End of the article" gnus-summary-end-of-article t]
1769              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1770              ["Fetch referenced articles" gnus-summary-refer-references t]
1771              ["Fetch current thread" gnus-summary-refer-thread t]
1772              ["Fetch article with id..." gnus-summary-refer-article t]
1773              ["Redisplay" gnus-summary-show-article t])))
1774       (easy-menu-define
1775        gnus-summary-article-menu gnus-summary-mode-map ""
1776        (cons "Article" innards))
1777
1778       (easy-menu-define
1779        gnus-article-commands-menu gnus-article-mode-map ""
1780        (cons "Commands" innards)))
1781
1782     (easy-menu-define
1783      gnus-summary-thread-menu gnus-summary-mode-map ""
1784      '("Threads"
1785        ["Toggle threading" gnus-summary-toggle-threads t]
1786        ["Hide threads" gnus-summary-hide-all-threads t]
1787        ["Show threads" gnus-summary-show-all-threads t]
1788        ["Hide thread" gnus-summary-hide-thread t]
1789        ["Show thread" gnus-summary-show-thread t]
1790        ["Go to next thread" gnus-summary-next-thread t]
1791        ["Go to previous thread" gnus-summary-prev-thread t]
1792        ["Go down thread" gnus-summary-down-thread t]
1793        ["Go up thread" gnus-summary-up-thread t]
1794        ["Top of thread" gnus-summary-top-thread t]
1795        ["Mark thread as read" gnus-summary-kill-thread t]
1796        ["Lower thread score" gnus-summary-lower-thread t]
1797        ["Raise thread score" gnus-summary-raise-thread t]
1798        ["Rethread current" gnus-summary-rethread-current t]))
1799
1800     (easy-menu-define
1801      gnus-summary-post-menu gnus-summary-mode-map ""
1802      '("Post"
1803        ["Post an article" gnus-summary-post-news t]
1804        ["Followup" gnus-summary-followup t]
1805        ["Followup and yank" gnus-summary-followup-with-original t]
1806        ["Supersede article" gnus-summary-supersede-article t]
1807        ["Cancel article" gnus-summary-cancel-article t]
1808        ["Reply" gnus-summary-reply t]
1809        ["Reply and yank" gnus-summary-reply-with-original t]
1810        ["Wide reply" gnus-summary-wide-reply t]
1811        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1812        ["Mail forward" gnus-summary-mail-forward t]
1813        ["Post forward" gnus-summary-post-forward t]
1814        ["Digest and mail" gnus-summary-mail-digest t]
1815        ["Digest and post" gnus-summary-post-digest t]
1816        ["Resend message" gnus-summary-resend-message t]
1817        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1818        ["Send a mail" gnus-summary-mail-other-window t]
1819        ["Uuencode and post" gnus-uu-post-news t]
1820        ["Followup via news" gnus-summary-followup-to-mail t]
1821        ["Followup via news and yank"
1822         gnus-summary-followup-to-mail-with-original t]
1823        ;;("Draft"
1824        ;;["Send" gnus-summary-send-draft t]
1825        ;;["Send bounced" gnus-resend-bounced-mail t])
1826        ))
1827
1828     (easy-menu-define
1829      gnus-summary-misc-menu gnus-summary-mode-map ""
1830      '("Misc"
1831        ("Mark Read"
1832         ["Mark as read" gnus-summary-mark-as-read-forward t]
1833         ["Mark same subject and select"
1834          gnus-summary-kill-same-subject-and-select t]
1835         ["Mark same subject" gnus-summary-kill-same-subject t]
1836         ["Catchup" gnus-summary-catchup t]
1837         ["Catchup all" gnus-summary-catchup-all t]
1838         ["Catchup to here" gnus-summary-catchup-to-here t]
1839         ["Catchup region" gnus-summary-mark-region-as-read t]
1840         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1841        ("Mark Various"
1842         ["Tick" gnus-summary-tick-article-forward t]
1843         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1844         ["Remove marks" gnus-summary-clear-mark-forward t]
1845         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1846         ["Set bookmark" gnus-summary-set-bookmark t]
1847         ["Remove bookmark" gnus-summary-remove-bookmark t])
1848        ("Mark Limit"
1849         ["Marks..." gnus-summary-limit-to-marks t]
1850         ["Subject..." gnus-summary-limit-to-subject t]
1851         ["Author..." gnus-summary-limit-to-author t]
1852         ["Age..." gnus-summary-limit-to-age t]
1853         ["Extra..." gnus-summary-limit-to-extra t]
1854         ["Score" gnus-summary-limit-to-score t]
1855         ["Unread" gnus-summary-limit-to-unread t]
1856         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1857         ["Articles" gnus-summary-limit-to-articles t]
1858         ["Pop limit" gnus-summary-pop-limit t]
1859         ["Show dormant" gnus-summary-limit-include-dormant t]
1860         ["Hide childless dormant"
1861          gnus-summary-limit-exclude-childless-dormant t]
1862         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1863         ["Hide marked" gnus-summary-limit-exclude-marks t]
1864         ["Show expunged" gnus-summary-show-all-expunged t])
1865        ("Process Mark"
1866         ["Set mark" gnus-summary-mark-as-processable t]
1867         ["Remove mark" gnus-summary-unmark-as-processable t]
1868         ["Remove all marks" gnus-summary-unmark-all-processable t]
1869         ["Mark above" gnus-uu-mark-over t]
1870         ["Mark series" gnus-uu-mark-series t]
1871         ["Mark region" gnus-uu-mark-region t]
1872         ["Unmark region" gnus-uu-unmark-region t]
1873         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1874         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1875         ["Mark all" gnus-uu-mark-all t]
1876         ["Mark buffer" gnus-uu-mark-buffer t]
1877         ["Mark sparse" gnus-uu-mark-sparse t]
1878         ["Mark thread" gnus-uu-mark-thread t]
1879         ["Unmark thread" gnus-uu-unmark-thread t]
1880         ("Process Mark Sets"
1881          ["Kill" gnus-summary-kill-process-mark t]
1882          ["Yank" gnus-summary-yank-process-mark
1883           gnus-newsgroup-process-stack]
1884          ["Save" gnus-summary-save-process-mark t]))
1885        ("Scroll article"
1886         ["Page forward" gnus-summary-next-page t]
1887         ["Page backward" gnus-summary-prev-page t]
1888         ["Line forward" gnus-summary-scroll-up t])
1889        ("Move"
1890         ["Next unread article" gnus-summary-next-unread-article t]
1891         ["Previous unread article" gnus-summary-prev-unread-article t]
1892         ["Next article" gnus-summary-next-article t]
1893         ["Previous article" gnus-summary-prev-article t]
1894         ["Next unread subject" gnus-summary-next-unread-subject t]
1895         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1896         ["Next article same subject" gnus-summary-next-same-subject t]
1897         ["Previous article same subject" gnus-summary-prev-same-subject t]
1898         ["First unread article" gnus-summary-first-unread-article t]
1899         ["Best unread article" gnus-summary-best-unread-article t]
1900         ["Go to subject number..." gnus-summary-goto-subject t]
1901         ["Go to article number..." gnus-summary-goto-article t]
1902         ["Go to the last article" gnus-summary-goto-last-article t]
1903         ["Pop article off history" gnus-summary-pop-article t])
1904        ("Sort"
1905         ["Sort by number" gnus-summary-sort-by-number t]
1906         ["Sort by author" gnus-summary-sort-by-author t]
1907         ["Sort by subject" gnus-summary-sort-by-subject t]
1908         ["Sort by date" gnus-summary-sort-by-date t]
1909         ["Sort by score" gnus-summary-sort-by-score t]
1910         ["Sort by lines" gnus-summary-sort-by-lines t]
1911         ["Sort by characters" gnus-summary-sort-by-chars t])
1912        ("Help"
1913         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1914         ["Describe group" gnus-summary-describe-group t]
1915         ["Read manual" gnus-info-find-node t])
1916        ("Modes"
1917         ["Pick and read" gnus-pick-mode t]
1918         ["Binary" gnus-binary-mode t])
1919        ("Regeneration"
1920         ["Regenerate" gnus-summary-prepare t]
1921         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1922         ["Toggle threading" gnus-summary-toggle-threads t])
1923        ["Filter articles..." gnus-summary-execute-command t]
1924        ["Run command on subjects..." gnus-summary-universal-argument t]
1925        ["Search articles forward..." gnus-summary-search-article-forward t]
1926        ["Search articles backward..." gnus-summary-search-article-backward t]
1927        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1928        ["Expand window" gnus-summary-expand-window t]
1929        ["Expire expirable articles" gnus-summary-expire-articles
1930         (gnus-check-backend-function
1931          'request-expire-articles gnus-newsgroup-name)]
1932        ["Edit local kill file" gnus-summary-edit-local-kill t]
1933        ["Edit main kill file" gnus-summary-edit-global-kill t]
1934        ["Edit group parameters" gnus-summary-edit-parameters t]
1935        ["Customize group parameters" gnus-summary-customize-parameters t]
1936        ["Send a bug report" gnus-bug t]
1937        ("Exit"
1938         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1939         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1940         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1941         ["Exit group" gnus-summary-exit t]
1942         ["Exit group without updating" gnus-summary-exit-no-update t]
1943         ["Exit and goto next group" gnus-summary-next-group t]
1944         ["Exit and goto prev group" gnus-summary-prev-group t]
1945         ["Reselect group" gnus-summary-reselect-current-group t]
1946         ["Rescan group" gnus-summary-rescan-group t]
1947         ["Update dribble" gnus-summary-save-newsrc t])))
1948
1949     (gnus-run-hooks 'gnus-summary-menu-hook)))
1950
1951 (defun gnus-score-set-default (var value)
1952   "A version of set that updates the GNU Emacs menu-bar."
1953   (set var value)
1954   ;; It is the message that forces the active status to be updated.
1955   (message ""))
1956
1957 (defun gnus-make-score-map (type)
1958   "Make a summary score map of type TYPE."
1959   (if t
1960       nil
1961     (let ((headers '(("author" "from" string)
1962                      ("subject" "subject" string)
1963                      ("article body" "body" string)
1964                      ("article head" "head" string)
1965                      ("xref" "xref" string)
1966                      ("extra header" "extra" string)
1967                      ("lines" "lines" number)
1968                      ("followups to author" "followup" string)))
1969           (types '((number ("less than" <)
1970                            ("greater than" >)
1971                            ("equal" =))
1972                    (string ("substring" s)
1973                            ("exact string" e)
1974                            ("fuzzy string" f)
1975                            ("regexp" r))))
1976           (perms '(("temporary" (current-time-string))
1977                    ("permanent" nil)
1978                    ("immediate" now)))
1979           header)
1980       (list
1981        (apply
1982         'nconc
1983         (list
1984          (if (eq type 'lower)
1985              "Lower score"
1986            "Increase score"))
1987         (let (outh)
1988           (while headers
1989             (setq header (car headers))
1990             (setq outh
1991                   (cons
1992                    (apply
1993                     'nconc
1994                     (list (car header))
1995                     (let ((ts (cdr (assoc (nth 2 header) types)))
1996                           outt)
1997                       (while ts
1998                         (setq outt
1999                               (cons
2000                                (apply
2001                                 'nconc
2002                                 (list (caar ts))
2003                                 (let ((ps perms)
2004                                       outp)
2005                                   (while ps
2006                                     (setq outp
2007                                           (cons
2008                                            (vector
2009                                             (caar ps)
2010                                             (list
2011                                              'gnus-summary-score-entry
2012                                              (nth 1 header)
2013                                              (if (or (string= (nth 1 header)
2014                                                               "head")
2015                                                      (string= (nth 1 header)
2016                                                               "body"))
2017                                                  ""
2018                                                (list 'gnus-summary-header
2019                                                      (nth 1 header)))
2020                                              (list 'quote (nth 1 (car ts)))
2021                                              (list 'gnus-score-delta-default
2022                                                    nil)
2023                                              (nth 1 (car ps))
2024                                              t)
2025                                             t)
2026                                            outp))
2027                                     (setq ps (cdr ps)))
2028                                   (list (nreverse outp))))
2029                                outt))
2030                         (setq ts (cdr ts)))
2031                       (list (nreverse outt))))
2032                    outh))
2033             (setq headers (cdr headers)))
2034           (list (nreverse outh))))))))
2035
2036 \f
2037
2038 (defun gnus-summary-mode (&optional group)
2039   "Major mode for reading articles.
2040
2041 All normal editing commands are switched off.
2042 \\<gnus-summary-mode-map>
2043 Each line in this buffer represents one article.  To read an
2044 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2045 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2046 respectively.
2047
2048 You can also post articles and send mail from this buffer.  To
2049 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2050 of an article, type `\\[gnus-summary-reply]'.
2051
2052 There are approx. one gazillion commands you can execute in this
2053 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2054
2055 The following commands are available:
2056
2057 \\{gnus-summary-mode-map}"
2058   (interactive)
2059   (when (gnus-visual-p 'summary-menu 'menu)
2060     (gnus-summary-make-menu-bar))
2061   (kill-all-local-variables)
2062   (gnus-summary-make-local-variables)
2063   (gnus-make-thread-indent-array)
2064   (gnus-simplify-mode-line)
2065   (setq major-mode 'gnus-summary-mode)
2066   (setq mode-name "Summary")
2067   (make-local-variable 'minor-mode-alist)
2068   (use-local-map gnus-summary-mode-map)
2069   (buffer-disable-undo)
2070   (setq buffer-read-only t)             ;Disable modification
2071   (setq truncate-lines t)
2072   (setq selective-display t)
2073   (setq selective-display-ellipses t)   ;Display `...'
2074   (gnus-summary-set-display-table)
2075   (gnus-set-default-directory)
2076   (setq gnus-newsgroup-name group)
2077   (unless (gnus-news-group-p group)
2078     (setq gnus-newsgroup-incorporated
2079           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2080   (make-local-variable 'gnus-summary-line-format)
2081   (make-local-variable 'gnus-summary-line-format-spec)
2082   (make-local-variable 'gnus-summary-dummy-line-format)
2083   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2084   (make-local-variable 'gnus-summary-mark-positions)
2085   (make-local-hook 'pre-command-hook)
2086   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2087   (gnus-run-hooks 'gnus-summary-mode-hook)
2088   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2089   (gnus-update-summary-mark-positions))
2090
2091 (defun gnus-summary-make-local-variables ()
2092   "Make all the local summary buffer variables."
2093   (let (global)
2094     (dolist (local gnus-summary-local-variables)
2095       (if (consp local)
2096           (progn
2097             (if (eq (cdr local) 'global)
2098                 ;; Copy the global value of the variable.
2099                 (setq global (symbol-value (car local)))
2100               ;; Use the value from the list.
2101               (setq global (eval (cdr local))))
2102             (set (make-local-variable (car local)) global))
2103         ;; Simple nil-valued local variable.
2104         (set (make-local-variable local) nil)))))
2105
2106 (defun gnus-summary-clear-local-variables ()
2107   (let ((locals gnus-summary-local-variables))
2108     (while locals
2109       (if (consp (car locals))
2110           (and (vectorp (caar locals))
2111                (set (caar locals) nil))
2112         (and (vectorp (car locals))
2113              (set (car locals) nil)))
2114       (setq locals (cdr locals)))))
2115
2116 ;; Summary data functions.
2117
2118 (defmacro gnus-data-number (data)
2119   `(car ,data))
2120
2121 (defmacro gnus-data-set-number (data number)
2122   `(setcar ,data ,number))
2123
2124 (defmacro gnus-data-mark (data)
2125   `(nth 1 ,data))
2126
2127 (defmacro gnus-data-set-mark (data mark)
2128   `(setcar (nthcdr 1 ,data) ,mark))
2129
2130 (defmacro gnus-data-pos (data)
2131   `(nth 2 ,data))
2132
2133 (defmacro gnus-data-set-pos (data pos)
2134   `(setcar (nthcdr 2 ,data) ,pos))
2135
2136 (defmacro gnus-data-header (data)
2137   `(nth 3 ,data))
2138
2139 (defmacro gnus-data-set-header (data header)
2140   `(setcar (nthcdr 3 ,data) ,header))
2141
2142 (defmacro gnus-data-level (data)
2143   `(nth 4 ,data))
2144
2145 (defmacro gnus-data-unread-p (data)
2146   `(= (nth 1 ,data) gnus-unread-mark))
2147
2148 (defmacro gnus-data-read-p (data)
2149   `(/= (nth 1 ,data) gnus-unread-mark))
2150
2151 (defmacro gnus-data-pseudo-p (data)
2152   `(consp (nth 3 ,data)))
2153
2154 (defmacro gnus-data-find (number)
2155   `(assq ,number gnus-newsgroup-data))
2156
2157 (defmacro gnus-data-find-list (number &optional data)
2158   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2159      (memq (assq ,number bdata)
2160            bdata)))
2161
2162 (defmacro gnus-data-make (number mark pos header level)
2163   `(list ,number ,mark ,pos ,header ,level))
2164
2165 (defun gnus-data-enter (after-article number mark pos header level offset)
2166   (let ((data (gnus-data-find-list after-article)))
2167     (unless data
2168       (error "No such article: %d" after-article))
2169     (setcdr data (cons (gnus-data-make number mark pos header level)
2170                        (cdr data)))
2171     (setq gnus-newsgroup-data-reverse nil)
2172     (gnus-data-update-list (cddr data) offset)))
2173
2174 (defun gnus-data-enter-list (after-article list &optional offset)
2175   (when list
2176     (let ((data (and after-article (gnus-data-find-list after-article)))
2177           (ilist list))
2178       (if (not (or data
2179                    after-article))
2180           (let ((odata gnus-newsgroup-data))
2181             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2182             (when offset
2183               (gnus-data-update-list odata offset)))
2184         ;; Find the last element in the list to be spliced into the main
2185         ;; list.
2186         (while (cdr list)
2187           (setq list (cdr list)))
2188         (if (not data)
2189             (progn
2190               (setcdr list gnus-newsgroup-data)
2191               (setq gnus-newsgroup-data ilist)
2192               (when offset
2193                 (gnus-data-update-list (cdr list) offset)))
2194           (setcdr list (cdr data))
2195           (setcdr data ilist)
2196           (when offset
2197             (gnus-data-update-list (cdr list) offset))))
2198       (setq gnus-newsgroup-data-reverse nil))))
2199
2200 (defun gnus-data-remove (article &optional offset)
2201   (let ((data gnus-newsgroup-data))
2202     (if (= (gnus-data-number (car data)) article)
2203         (progn
2204           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2205                 gnus-newsgroup-data-reverse nil)
2206           (when offset
2207             (gnus-data-update-list gnus-newsgroup-data offset)))
2208       (while (cdr data)
2209         (when (= (gnus-data-number (cadr data)) article)
2210           (setcdr data (cddr data))
2211           (when offset
2212             (gnus-data-update-list (cdr data) offset))
2213           (setq data nil
2214                 gnus-newsgroup-data-reverse nil))
2215         (setq data (cdr data))))))
2216
2217 (defmacro gnus-data-list (backward)
2218   `(if ,backward
2219        (or gnus-newsgroup-data-reverse
2220            (setq gnus-newsgroup-data-reverse
2221                  (reverse gnus-newsgroup-data)))
2222      gnus-newsgroup-data))
2223
2224 (defun gnus-data-update-list (data offset)
2225   "Add OFFSET to the POS of all data entries in DATA."
2226   (setq gnus-newsgroup-data-reverse nil)
2227   (while data
2228     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2229     (setq data (cdr data))))
2230
2231 (defun gnus-summary-article-pseudo-p (article)
2232   "Say whether this article is a pseudo article or not."
2233   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2234
2235 (defmacro gnus-summary-article-sparse-p (article)
2236   "Say whether this article is a sparse article or not."
2237   `(memq ,article gnus-newsgroup-sparse))
2238
2239 (defmacro gnus-summary-article-ancient-p (article)
2240   "Say whether this article is a sparse article or not."
2241   `(memq ,article gnus-newsgroup-ancient))
2242
2243 (defun gnus-article-parent-p (number)
2244   "Say whether this article is a parent or not."
2245   (let ((data (gnus-data-find-list number)))
2246     (and (cdr data)                     ; There has to be an article after...
2247          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2248             (gnus-data-level (nth 1 data))))))
2249
2250 (defun gnus-article-children (number)
2251   "Return a list of all children to NUMBER."
2252   (let* ((data (gnus-data-find-list number))
2253          (level (gnus-data-level (car data)))
2254          children)
2255     (setq data (cdr data))
2256     (while (and data
2257                 (= (gnus-data-level (car data)) (1+ level)))
2258       (push (gnus-data-number (car data)) children)
2259       (setq data (cdr data)))
2260     children))
2261
2262 (defmacro gnus-summary-skip-intangible ()
2263   "If the current article is intangible, then jump to a different article."
2264   '(let ((to (get-text-property (point) 'gnus-intangible)))
2265      (and to (gnus-summary-goto-subject to))))
2266
2267 (defmacro gnus-summary-article-intangible-p ()
2268   "Say whether this article is intangible or not."
2269   '(get-text-property (point) 'gnus-intangible))
2270
2271 (defun gnus-article-read-p (article)
2272   "Say whether ARTICLE is read or not."
2273   (not (or (memq article gnus-newsgroup-marked)
2274            (memq article gnus-newsgroup-unreads)
2275            (memq article gnus-newsgroup-unselected)
2276            (memq article gnus-newsgroup-dormant))))
2277
2278 ;; Some summary mode macros.
2279
2280 (defmacro gnus-summary-article-number ()
2281   "The article number of the article on the current line.
2282 If there isn's an article number here, then we return the current
2283 article number."
2284   '(progn
2285      (gnus-summary-skip-intangible)
2286      (or (get-text-property (point) 'gnus-number)
2287          (gnus-summary-last-subject))))
2288
2289 (defmacro gnus-summary-article-header (&optional number)
2290   "Return the header of article NUMBER."
2291   `(gnus-data-header (gnus-data-find
2292                       ,(or number '(gnus-summary-article-number)))))
2293
2294 (defmacro gnus-summary-thread-level (&optional number)
2295   "Return the level of thread that starts with article NUMBER."
2296   `(if (and (eq gnus-summary-make-false-root 'dummy)
2297             (get-text-property (point) 'gnus-intangible))
2298        0
2299      (gnus-data-level (gnus-data-find
2300                        ,(or number '(gnus-summary-article-number))))))
2301
2302 (defmacro gnus-summary-article-mark (&optional number)
2303   "Return the mark of article NUMBER."
2304   `(gnus-data-mark (gnus-data-find
2305                     ,(or number '(gnus-summary-article-number)))))
2306
2307 (defmacro gnus-summary-article-pos (&optional number)
2308   "Return the position of the line of article NUMBER."
2309   `(gnus-data-pos (gnus-data-find
2310                    ,(or number '(gnus-summary-article-number)))))
2311
2312 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2313 (defmacro gnus-summary-article-subject (&optional number)
2314   "Return current subject string or nil if nothing."
2315   `(let ((headers
2316           ,(if number
2317                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2318              '(gnus-data-header (assq (gnus-summary-article-number)
2319                                       gnus-newsgroup-data)))))
2320      (and headers
2321           (vectorp headers)
2322           (mail-header-subject headers))))
2323
2324 (defmacro gnus-summary-article-score (&optional number)
2325   "Return current article score."
2326   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2327                   gnus-newsgroup-scored))
2328        gnus-summary-default-score 0))
2329
2330 (defun gnus-summary-article-children (&optional number)
2331   "Return a list of article numbers that are children of article NUMBER."
2332   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2333          (level (gnus-data-level (car data)))
2334          l children)
2335     (while (and (setq data (cdr data))
2336                 (> (setq l (gnus-data-level (car data))) level))
2337       (and (= (1+ level) l)
2338            (push (gnus-data-number (car data))
2339                  children)))
2340     (nreverse children)))
2341
2342 (defun gnus-summary-article-parent (&optional number)
2343   "Return the article number of the parent of article NUMBER."
2344   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2345                                     (gnus-data-list t)))
2346          (level (gnus-data-level (car data))))
2347     (if (zerop level)
2348         ()                              ; This is a root.
2349       ;; We search until we find an article with a level less than
2350       ;; this one.  That function has to be the parent.
2351       (while (and (setq data (cdr data))
2352                   (not (< (gnus-data-level (car data)) level))))
2353       (and data (gnus-data-number (car data))))))
2354
2355 (defun gnus-unread-mark-p (mark)
2356   "Say whether MARK is the unread mark."
2357   (= mark gnus-unread-mark))
2358
2359 (defun gnus-read-mark-p (mark)
2360   "Say whether MARK is one of the marks that mark as read.
2361 This is all marks except unread, ticked, dormant, and expirable."
2362   (not (or (= mark gnus-unread-mark)
2363            (= mark gnus-ticked-mark)
2364            (= mark gnus-dormant-mark)
2365            (= mark gnus-expirable-mark))))
2366
2367 (defmacro gnus-article-mark (number)
2368   "Return the MARK of article NUMBER.
2369 This macro should only be used when computing the mark the \"first\"
2370 time; i.e., when generating the summary lines.  After that,
2371 `gnus-summary-article-mark' should be used to examine the
2372 marks of articles."
2373   `(cond
2374     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2375     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2376     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2377     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2378     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2379     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2380     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2381     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2382            gnus-ancient-mark))))
2383
2384 ;; Saving hidden threads.
2385
2386 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2387 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2388
2389 (defmacro gnus-save-hidden-threads (&rest forms)
2390   "Save hidden threads, eval FORMS, and restore the hidden threads."
2391   (let ((config (make-symbol "config")))
2392     `(let ((,config (gnus-hidden-threads-configuration)))
2393        (unwind-protect
2394            (save-excursion
2395              ,@forms)
2396          (gnus-restore-hidden-threads-configuration ,config)))))
2397
2398 (defun gnus-data-compute-positions ()
2399   "Compute the positions of all articles."
2400   (setq gnus-newsgroup-data-reverse nil)
2401   (let ((data gnus-newsgroup-data))
2402     (save-excursion
2403       (gnus-save-hidden-threads
2404         (gnus-summary-show-all-threads)
2405         (goto-char (point-min))
2406         (while data
2407           (while (get-text-property (point) 'gnus-intangible)
2408             (forward-line 1))
2409           (gnus-data-set-pos (car data) (+ (point) 3))
2410           (setq data (cdr data))
2411           (forward-line 1))))))
2412
2413 (defun gnus-hidden-threads-configuration ()
2414   "Return the current hidden threads configuration."
2415   (save-excursion
2416     (let (config)
2417       (goto-char (point-min))
2418       (while (search-forward "\r" nil t)
2419         (push (1- (point)) config))
2420       config)))
2421
2422 (defun gnus-restore-hidden-threads-configuration (config)
2423   "Restore hidden threads configuration from CONFIG."
2424   (let (point buffer-read-only)
2425     (while (setq point (pop config))
2426       (when (and (< point (point-max))
2427                  (goto-char point)
2428                  (eq (char-after) ?\n))
2429         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2430
2431 ;; Various summary mode internalish functions.
2432
2433 (defun gnus-mouse-pick-article (e)
2434   (interactive "e")
2435   (mouse-set-point e)
2436   (gnus-summary-next-page nil t))
2437
2438 (defun gnus-summary-set-display-table ()
2439   ;; Change the display table.  Odd characters have a tendency to mess
2440   ;; up nicely formatted displays - we make all possible glyphs
2441   ;; display only a single character.
2442
2443   ;; We start from the standard display table, if any.
2444   (let ((table (or (copy-sequence standard-display-table)
2445                    (make-display-table)))
2446         (i 32))
2447     ;; Nix out all the control chars...
2448     (while (>= (setq i (1- i)) 0)
2449       (aset table i [??]))
2450     ;; ... but not newline and cr, of course.  (cr is necessary for the
2451     ;; selective display).
2452     (aset table ?\n nil)
2453     (aset table ?\r nil)
2454     ;; We keep TAB as well.
2455     (aset table ?\t nil)
2456     ;; We nix out any glyphs over 126 that are not set already.
2457     (let ((i 256))
2458       (while (>= (setq i (1- i)) 127)
2459         ;; Only modify if the entry is nil.
2460         (unless (aref table i)
2461           (aset table i [??]))))
2462     (setq buffer-display-table table)))
2463
2464 (defun gnus-summary-setup-buffer (group)
2465   "Initialize summary buffer."
2466   (let ((buffer (concat "*Summary " group "*")))
2467     (if (get-buffer buffer)
2468         (progn
2469           (set-buffer buffer)
2470           (setq gnus-summary-buffer (current-buffer))
2471           (not gnus-newsgroup-prepared))
2472       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2473       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2474       (gnus-summary-mode group)
2475       (when gnus-carpal
2476         (gnus-carpal-setup-buffer 'summary))
2477       (unless gnus-single-article-buffer
2478         (make-local-variable 'gnus-article-buffer)
2479         (make-local-variable 'gnus-article-current)
2480         (make-local-variable 'gnus-original-article-buffer))
2481       (setq gnus-newsgroup-name group)
2482       t)))
2483
2484 (defun gnus-set-global-variables ()
2485   ;; Set the global equivalents of the summary buffer-local variables
2486   ;; to the latest values they had.  These reflect the summary buffer
2487   ;; that was in action when the last article was fetched.
2488   (when (eq major-mode 'gnus-summary-mode)
2489     (setq gnus-summary-buffer (current-buffer))
2490     (let ((name gnus-newsgroup-name)
2491           (marked gnus-newsgroup-marked)
2492           (unread gnus-newsgroup-unreads)
2493           (headers gnus-current-headers)
2494           (data gnus-newsgroup-data)
2495           (summary gnus-summary-buffer)
2496           (article-buffer gnus-article-buffer)
2497           (original gnus-original-article-buffer)
2498           (gac gnus-article-current)
2499           (reffed gnus-reffed-article-number)
2500           (score-file gnus-current-score-file)
2501           (default-charset gnus-newsgroup-charset))
2502       (save-excursion
2503         (set-buffer gnus-group-buffer)
2504         (setq gnus-newsgroup-name name
2505               gnus-newsgroup-marked marked
2506               gnus-newsgroup-unreads unread
2507               gnus-current-headers headers
2508               gnus-newsgroup-data data
2509               gnus-article-current gac
2510               gnus-summary-buffer summary
2511               gnus-article-buffer article-buffer
2512               gnus-original-article-buffer original
2513               gnus-reffed-article-number reffed
2514               gnus-current-score-file score-file
2515               gnus-newsgroup-charset default-charset)
2516         ;; The article buffer also has local variables.
2517         (when (gnus-buffer-live-p gnus-article-buffer)
2518           (set-buffer gnus-article-buffer)
2519           (setq gnus-summary-buffer summary))))))
2520
2521 (defun gnus-summary-article-unread-p (article)
2522   "Say whether ARTICLE is unread or not."
2523   (memq article gnus-newsgroup-unreads))
2524
2525 (defun gnus-summary-first-article-p (&optional article)
2526   "Return whether ARTICLE is the first article in the buffer."
2527   (if (not (setq article (or article (gnus-summary-article-number))))
2528       nil
2529     (eq article (caar gnus-newsgroup-data))))
2530
2531 (defun gnus-summary-last-article-p (&optional article)
2532   "Return whether ARTICLE is the last article in the buffer."
2533   (if (not (setq article (or article (gnus-summary-article-number))))
2534       ;; All non-existent numbers are the last article.  :-)
2535       t
2536     (not (cdr (gnus-data-find-list article)))))
2537
2538 (defun gnus-make-thread-indent-array ()
2539   (let ((n 200))
2540     (unless (and gnus-thread-indent-array
2541                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2542       (setq gnus-thread-indent-array (make-vector 201 "")
2543             gnus-thread-indent-array-level gnus-thread-indent-level)
2544       (while (>= n 0)
2545         (aset gnus-thread-indent-array n
2546               (make-string (* n gnus-thread-indent-level) ? ))
2547         (setq n (1- n))))))
2548
2549 (defun gnus-update-summary-mark-positions ()
2550   "Compute where the summary marks are to go."
2551   (save-excursion
2552     (when (gnus-buffer-exists-p gnus-summary-buffer)
2553       (set-buffer gnus-summary-buffer))
2554     (let ((gnus-replied-mark 129)
2555           (gnus-score-below-mark 130)
2556           (gnus-score-over-mark 130)
2557           (gnus-download-mark 131)
2558           (spec gnus-summary-line-format-spec)
2559           gnus-visual pos)
2560       (save-excursion
2561         (gnus-set-work-buffer)
2562         (let ((gnus-summary-line-format-spec spec)
2563               (gnus-newsgroup-downloadable '((0 . t))))
2564           (gnus-summary-insert-line
2565            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2566            0 nil 128 t nil "" nil 1)
2567           (goto-char (point-min))
2568           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2569                                              (- (point) 2)))))
2570           (goto-char (point-min))
2571           (push (cons 'replied (and (search-forward "\201" nil t)
2572                                     (- (point) 2)))
2573                 pos)
2574           (goto-char (point-min))
2575           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2576                 pos)
2577           (goto-char (point-min))
2578           (push (cons 'download
2579                       (and (search-forward "\203" nil t) (- (point) 2)))
2580                 pos)))
2581       (setq gnus-summary-mark-positions pos))))
2582
2583 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2584   "Insert a dummy root in the summary buffer."
2585   (beginning-of-line)
2586   (gnus-add-text-properties
2587    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2588    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2589
2590 (defun gnus-summary-from-or-to-or-newsgroups (header)
2591   (let ((to (cdr (assq 'To (mail-header-extra header))))
2592         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2593         (default-mime-charset (with-current-buffer gnus-summary-buffer
2594                                 default-mime-charset)))
2595     (cond
2596      ((and to
2597            gnus-ignored-from-addresses
2598            (string-match gnus-ignored-from-addresses
2599                          (mail-header-from header)))
2600       (concat "-> "
2601               (or (car (funcall gnus-extract-address-components
2602                                 (funcall
2603                                  gnus-decode-encoded-word-function to)))
2604                   (funcall gnus-decode-encoded-word-function to))))
2605      ((and newsgroups
2606            gnus-ignored-from-addresses
2607            (string-match gnus-ignored-from-addresses
2608                          (mail-header-from header)))
2609       (concat "=> " newsgroups))
2610      (t
2611       (or (car (funcall gnus-extract-address-components
2612                         (mail-header-from header)))
2613           (mail-header-from header))))))
2614
2615 (defun gnus-summary-insert-line (gnus-tmp-header
2616                                  gnus-tmp-level gnus-tmp-current
2617                                  gnus-tmp-unread gnus-tmp-replied
2618                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2619                                  &optional gnus-tmp-dummy gnus-tmp-score
2620                                  gnus-tmp-process)
2621   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2622          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2623          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2624          (gnus-tmp-score-char
2625           (if (or (null gnus-summary-default-score)
2626                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2627                       gnus-summary-zcore-fuzz))
2628               ?  ;Whitespace
2629             (if (< gnus-tmp-score gnus-summary-default-score)
2630                 gnus-score-below-mark gnus-score-over-mark)))
2631          (gnus-tmp-replied
2632           (cond (gnus-tmp-process gnus-process-mark)
2633                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2634                  gnus-cached-mark)
2635                 (gnus-tmp-replied gnus-replied-mark)
2636                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2637                  gnus-saved-mark)
2638                 (t gnus-unread-mark)))
2639          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2640          (gnus-tmp-name
2641           (cond
2642            ((string-match "<[^>]+> *$" gnus-tmp-from)
2643             (let ((beg (match-beginning 0)))
2644               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2645                        (substring gnus-tmp-from (1+ (match-beginning 0))
2646                                   (1- (match-end 0))))
2647                   (substring gnus-tmp-from 0 beg))))
2648            ((string-match "(.+)" gnus-tmp-from)
2649             (substring gnus-tmp-from
2650                        (1+ (match-beginning 0)) (1- (match-end 0))))
2651            (t gnus-tmp-from)))
2652          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2653          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2654          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2655          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2656          (buffer-read-only nil))
2657     (when (string= gnus-tmp-name "")
2658       (setq gnus-tmp-name gnus-tmp-from))
2659     (unless (numberp gnus-tmp-lines)
2660       (setq gnus-tmp-lines 0))
2661     (gnus-put-text-property-excluding-characters-with-faces
2662      (point)
2663      (progn (eval gnus-summary-line-format-spec) (point))
2664      'gnus-number gnus-tmp-number)
2665     (when (gnus-visual-p 'summary-highlight 'highlight)
2666       (forward-line -1)
2667       (gnus-run-hooks 'gnus-summary-update-hook)
2668       (forward-line 1))))
2669
2670 (defun gnus-summary-update-line (&optional dont-update)
2671   ;; Update summary line after change.
2672   (when (and gnus-summary-default-score
2673              (not gnus-summary-inhibit-highlight))
2674     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2675            (article (gnus-summary-article-number))
2676            (score (gnus-summary-article-score article)))
2677       (unless dont-update
2678         (if (and gnus-summary-mark-below
2679                  (< (gnus-summary-article-score)
2680                     gnus-summary-mark-below))
2681             ;; This article has a low score, so we mark it as read.
2682             (when (memq article gnus-newsgroup-unreads)
2683               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2684           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2685             ;; This article was previously marked as read on account
2686             ;; of a low score, but now it has risen, so we mark it as
2687             ;; unread.
2688             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2689         (gnus-summary-update-mark
2690          (if (or (null gnus-summary-default-score)
2691                  (<= (abs (- score gnus-summary-default-score))
2692                      gnus-summary-zcore-fuzz))
2693              ?  ;Whitespace
2694            (if (< score gnus-summary-default-score)
2695                gnus-score-below-mark gnus-score-over-mark))
2696          'score))
2697       ;; Do visual highlighting.
2698       (when (gnus-visual-p 'summary-highlight 'highlight)
2699         (gnus-run-hooks 'gnus-summary-update-hook)))))
2700
2701 (defvar gnus-tmp-new-adopts nil)
2702
2703 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2704   "Return the number of articles in THREAD.
2705 This may be 0 in some cases -- if none of the articles in
2706 the thread are to be displayed."
2707   (let* ((number
2708           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2709           (cond
2710            ((not (listp thread))
2711             1)
2712            ((and (consp thread) (cdr thread))
2713             (apply
2714              '+ 1 (mapcar
2715                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2716            ((null thread)
2717             1)
2718            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2719             1)
2720            (t 0))))
2721     (when (and level (zerop level) gnus-tmp-new-adopts)
2722       (incf number
2723             (apply '+ (mapcar
2724                        'gnus-summary-number-of-articles-in-thread
2725                        gnus-tmp-new-adopts))))
2726     (if char
2727         (if (> number 1) gnus-not-empty-thread-mark
2728           gnus-empty-thread-mark)
2729       number)))
2730
2731 (defun gnus-summary-set-local-parameters (group)
2732   "Go through the local params of GROUP and set all variable specs in that list."
2733   (let ((params (gnus-group-find-parameter group))
2734         elem)
2735     (while params
2736       (setq elem (car params)
2737             params (cdr params))
2738       (and (consp elem)                 ; Has to be a cons.
2739            (consp (cdr elem))           ; The cdr has to be a list.
2740            (symbolp (car elem))         ; Has to be a symbol in there.
2741            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2742            (ignore-errors               ; So we set it.
2743              (make-local-variable (car elem))
2744              (set (car elem) (eval (nth 1 elem))))))))
2745
2746 (defun gnus-summary-read-group (group &optional show-all no-article
2747                                       kill-buffer no-display backward
2748                                       select-articles)
2749   "Start reading news in newsgroup GROUP.
2750 If SHOW-ALL is non-nil, already read articles are also listed.
2751 If NO-ARTICLE is non-nil, no article is selected initially.
2752 If NO-DISPLAY, don't generate a summary buffer."
2753   (let (result)
2754     (while (and group
2755                 (null (setq result
2756                             (let ((gnus-auto-select-next nil))
2757                               (or (gnus-summary-read-group-1
2758                                    group show-all no-article
2759                                    kill-buffer no-display
2760                                    select-articles)
2761                                   (setq show-all nil
2762                                         select-articles nil)))))
2763                 (eq gnus-auto-select-next 'quietly))
2764       (set-buffer gnus-group-buffer)
2765       ;; The entry function called above goes to the next
2766       ;; group automatically, so we go two groups back
2767       ;; if we are searching for the previous group.
2768       (when backward
2769         (gnus-group-prev-unread-group 2))
2770       (if (not (equal group (gnus-group-group-name)))
2771           (setq group (gnus-group-group-name))
2772         (setq group nil)))
2773     result))
2774
2775 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2776   "Directly jump to the other GROUP from summary buffer.
2777 If SHOW-ALL is non-nil, already read articles are also listed."
2778   (interactive
2779    (if (eq gnus-summary-buffer (current-buffer))
2780        (list (completing-read
2781               "Group: " gnus-active-hashtb nil t
2782               (when (and gnus-newsgroup-name
2783                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2784                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2785               'gnus-group-history)
2786              current-prefix-arg)
2787      (error "%s must be invoked from a gnus summary buffer." this-command)))
2788   (unless (or (zerop (length group))
2789               (and gnus-newsgroup-name
2790                    (string-equal gnus-newsgroup-name group)))
2791     (gnus-summary-exit)
2792     (gnus-summary-read-group group show-all
2793                              gnus-dont-select-after-jump-to-other-group)))
2794
2795 (defun gnus-summary-read-group-1 (group show-all no-article
2796                                         kill-buffer no-display
2797                                         &optional select-articles)
2798   ;; Killed foreign groups can't be entered.
2799   (when (and (not (gnus-group-native-p group))
2800              (not (gnus-gethash group gnus-newsrc-hashtb)))
2801     (error "Dead non-native groups can't be entered"))
2802   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2803   (let* ((new-group (gnus-summary-setup-buffer group))
2804          (quit-config (gnus-group-quit-config group))
2805          (did-select (and new-group (gnus-select-newsgroup
2806                                      group show-all select-articles))))
2807     (cond
2808      ;; This summary buffer exists already, so we just select it.
2809      ((not new-group)
2810       (gnus-set-global-variables)
2811       (when kill-buffer
2812         (gnus-kill-or-deaden-summary kill-buffer))
2813       (gnus-configure-windows 'summary 'force)
2814       (gnus-set-mode-line 'summary)
2815       (gnus-summary-position-point)
2816       (message "")
2817       t)
2818      ;; We couldn't select this group.
2819      ((null did-select)
2820       (when (and (eq major-mode 'gnus-summary-mode)
2821                  (not (equal (current-buffer) kill-buffer)))
2822         (kill-buffer (current-buffer))
2823         (if (not quit-config)
2824             (progn
2825               ;; Update the info -- marks might need to be removed,
2826               ;; for instance.
2827               (gnus-summary-update-info)
2828               (set-buffer gnus-group-buffer)
2829               (gnus-group-jump-to-group group)
2830               (gnus-group-next-unread-group 1))
2831           (gnus-handle-ephemeral-exit quit-config)))
2832       (gnus-message 3 "Can't select group")
2833       nil)
2834      ;; The user did a `C-g' while prompting for number of articles,
2835      ;; so we exit this group.
2836      ((eq did-select 'quit)
2837       (and (eq major-mode 'gnus-summary-mode)
2838            (not (equal (current-buffer) kill-buffer))
2839            (kill-buffer (current-buffer)))
2840       (when kill-buffer
2841         (gnus-kill-or-deaden-summary kill-buffer))
2842       (if (not quit-config)
2843           (progn
2844             (set-buffer gnus-group-buffer)
2845             (gnus-group-jump-to-group group)
2846             (gnus-group-next-unread-group 1)
2847             (gnus-configure-windows 'group 'force))
2848         (gnus-handle-ephemeral-exit quit-config))
2849       ;; Finally signal the quit.
2850       (signal 'quit nil))
2851      ;; The group was successfully selected.
2852      (t
2853       (gnus-set-global-variables)
2854       ;; Save the active value in effect when the group was entered.
2855       (setq gnus-newsgroup-active
2856             (gnus-copy-sequence
2857              (gnus-active gnus-newsgroup-name)))
2858       ;; You can change the summary buffer in some way with this hook.
2859       (gnus-run-hooks 'gnus-select-group-hook)
2860       ;; Set any local variables in the group parameters.
2861       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2862       (gnus-update-format-specifications
2863        nil 'summary 'summary-mode 'summary-dummy)
2864       (gnus-update-summary-mark-positions)
2865       ;; Do score processing.
2866       (when gnus-use-scoring
2867         (gnus-possibly-score-headers))
2868       ;; Check whether to fill in the gaps in the threads.
2869       (when gnus-build-sparse-threads
2870         (gnus-build-sparse-threads))
2871       ;; Find the initial limit.
2872       (if gnus-show-threads
2873           (if show-all
2874               (let ((gnus-newsgroup-dormant nil))
2875                 (gnus-summary-initial-limit show-all))
2876             (gnus-summary-initial-limit show-all))
2877         ;; When untreaded, all articles are always shown.
2878         (setq gnus-newsgroup-limit
2879               (mapcar
2880                (lambda (header) (mail-header-number header))
2881                gnus-newsgroup-headers)))
2882       ;; Generate the summary buffer.
2883       (unless no-display
2884         (gnus-summary-prepare))
2885       (when gnus-use-trees
2886         (gnus-tree-open group)
2887         (setq gnus-summary-highlight-line-function
2888               'gnus-tree-highlight-article))
2889       ;; If the summary buffer is empty, but there are some low-scored
2890       ;; articles or some excluded dormants, we include these in the
2891       ;; buffer.
2892       (when (and (zerop (buffer-size))
2893                  (not no-display))
2894         (cond (gnus-newsgroup-dormant
2895                (gnus-summary-limit-include-dormant))
2896               ((and gnus-newsgroup-scored show-all)
2897                (gnus-summary-limit-include-expunged t))))
2898       ;; Function `gnus-apply-kill-file' must be called in this hook.
2899       (gnus-run-hooks 'gnus-apply-kill-hook)
2900       (if (and (zerop (buffer-size))
2901                (not no-display))
2902           (progn
2903             ;; This newsgroup is empty.
2904             (gnus-summary-catchup-and-exit nil t)
2905             (gnus-message 6 "No unread news")
2906             (when kill-buffer
2907               (gnus-kill-or-deaden-summary kill-buffer))
2908             ;; Return nil from this function.
2909             nil)
2910         ;; Hide conversation thread subtrees.  We cannot do this in
2911         ;; gnus-summary-prepare-hook since kill processing may not
2912         ;; work with hidden articles.
2913         (and gnus-show-threads
2914              gnus-thread-hide-subtree
2915              (gnus-summary-hide-all-threads))
2916         (when kill-buffer
2917           (gnus-kill-or-deaden-summary kill-buffer))
2918         ;; Show first unread article if requested.
2919         (if (and (not no-article)
2920                  (not no-display)
2921                  gnus-newsgroup-unreads
2922                  gnus-auto-select-first)
2923             (progn
2924               (gnus-configure-windows 'summary)
2925               (cond
2926                ((eq gnus-auto-select-first 'best)
2927                 (gnus-summary-best-unread-article))
2928                ((eq gnus-auto-select-first t)
2929                 (gnus-summary-first-unread-article))
2930                ((gnus-functionp gnus-auto-select-first)
2931                 (funcall gnus-auto-select-first))))
2932           ;; Don't select any articles, just move point to the first
2933           ;; article in the group.
2934           (goto-char (point-min))
2935           (gnus-summary-position-point)
2936           (gnus-configure-windows 'summary 'force)
2937           (gnus-set-mode-line 'summary))
2938         (when (get-buffer-window gnus-group-buffer t)
2939           ;; Gotta use windows, because recenter does weird stuff if
2940           ;; the current buffer ain't the displayed window.
2941           (let ((owin (selected-window)))
2942             (select-window (get-buffer-window gnus-group-buffer t))
2943             (when (gnus-group-goto-group group)
2944               (recenter))
2945             (select-window owin)))
2946         ;; Mark this buffer as "prepared".
2947         (setq gnus-newsgroup-prepared t)
2948         (gnus-run-hooks 'gnus-summary-prepared-hook)
2949         t)))))
2950
2951 (defun gnus-summary-prepare ()
2952   "Generate the summary buffer."
2953   (interactive)
2954   (let ((buffer-read-only nil))
2955     (erase-buffer)
2956     (setq gnus-newsgroup-data nil
2957           gnus-newsgroup-data-reverse nil)
2958     (gnus-run-hooks 'gnus-summary-generate-hook)
2959     ;; Generate the buffer, either with threads or without.
2960     (when gnus-newsgroup-headers
2961       (gnus-summary-prepare-threads
2962        (if gnus-show-threads
2963            (gnus-sort-gathered-threads
2964             (funcall gnus-summary-thread-gathering-function
2965                      (gnus-sort-threads
2966                       (gnus-cut-threads (gnus-make-threads)))))
2967          ;; Unthreaded display.
2968          (gnus-sort-articles gnus-newsgroup-headers))))
2969     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2970     ;; Call hooks for modifying summary buffer.
2971     (goto-char (point-min))
2972     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2973
2974 (defsubst gnus-general-simplify-subject (subject)
2975   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2976   (setq subject
2977         (cond
2978          ;; Truncate the subject.
2979          (gnus-simplify-subject-functions
2980           (gnus-map-function gnus-simplify-subject-functions subject))
2981          ((numberp gnus-summary-gather-subject-limit)
2982           (setq subject (gnus-simplify-subject-re subject))
2983           (if (> (length subject) gnus-summary-gather-subject-limit)
2984               (substring subject 0 gnus-summary-gather-subject-limit)
2985             subject))
2986          ;; Fuzzily simplify it.
2987          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2988           (gnus-simplify-subject-fuzzy subject))
2989          ;; Just remove the leading "Re:".
2990          (t
2991           (gnus-simplify-subject-re subject))))
2992
2993   (if (and gnus-summary-gather-exclude-subject
2994            (string-match gnus-summary-gather-exclude-subject subject))
2995       nil                               ; This article shouldn't be gathered
2996     subject))
2997
2998 (defun gnus-summary-simplify-subject-query ()
2999   "Query where the respool algorithm would put this article."
3000   (interactive)
3001   (gnus-summary-select-article)
3002   (message "%s"
3003            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3004
3005 (defun gnus-gather-threads-by-subject (threads)
3006   "Gather threads by looking at Subject headers."
3007   (if (not gnus-summary-make-false-root)
3008       threads
3009     (let ((hashtb (gnus-make-hashtable 1024))
3010           (prev threads)
3011           (result threads)
3012           subject hthread whole-subject)
3013       (while threads
3014         (setq subject (gnus-general-simplify-subject
3015                        (setq whole-subject (mail-header-subject
3016                                             (caar threads)))))
3017         (when subject
3018           (if (setq hthread (gnus-gethash subject hashtb))
3019               (progn
3020                 ;; We enter a dummy root into the thread, if we
3021                 ;; haven't done that already.
3022                 (unless (stringp (caar hthread))
3023                   (setcar hthread (list whole-subject (car hthread))))
3024                 ;; We add this new gathered thread to this gathered
3025                 ;; thread.
3026                 (setcdr (car hthread)
3027                         (nconc (cdar hthread) (list (car threads))))
3028                 ;; Remove it from the list of threads.
3029                 (setcdr prev (cdr threads))
3030                 (setq threads prev))
3031             ;; Enter this thread into the hash table.
3032             (gnus-sethash subject threads hashtb)))
3033         (setq prev threads)
3034         (setq threads (cdr threads)))
3035       result)))
3036
3037 (defun gnus-gather-threads-by-references (threads)
3038   "Gather threads by looking at References headers."
3039   (let ((idhashtb (gnus-make-hashtable 1024))
3040         (thhashtb (gnus-make-hashtable 1024))
3041         (prev threads)
3042         (result threads)
3043         ids references id gthread gid entered ref)
3044     (while threads
3045       (when (setq references (mail-header-references (caar threads)))
3046         (setq id (mail-header-id (caar threads))
3047               ids (gnus-split-references references)
3048               entered nil)
3049         (while (setq ref (pop ids))
3050           (setq ids (delete ref ids))
3051           (if (not (setq gid (gnus-gethash ref idhashtb)))
3052               (progn
3053                 (gnus-sethash ref id idhashtb)
3054                 (gnus-sethash id threads thhashtb))
3055             (setq gthread (gnus-gethash gid thhashtb))
3056             (unless entered
3057               ;; We enter a dummy root into the thread, if we
3058               ;; haven't done that already.
3059               (unless (stringp (caar gthread))
3060                 (setcar gthread (list (mail-header-subject (caar gthread))
3061                                       (car gthread))))
3062               ;; We add this new gathered thread to this gathered
3063               ;; thread.
3064               (setcdr (car gthread)
3065                       (nconc (cdar gthread) (list (car threads)))))
3066             ;; Add it into the thread hash table.
3067             (gnus-sethash id gthread thhashtb)
3068             (setq entered t)
3069             ;; Remove it from the list of threads.
3070             (setcdr prev (cdr threads))
3071             (setq threads prev))))
3072       (setq prev threads)
3073       (setq threads (cdr threads)))
3074     result))
3075
3076 (defun gnus-sort-gathered-threads (threads)
3077   "Sort subtreads inside each gathered thread by article number."
3078   (let ((result threads))
3079     (while threads
3080       (when (stringp (caar threads))
3081         (setcdr (car threads)
3082                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3083       (setq threads (cdr threads)))
3084     result))
3085
3086 (defun gnus-thread-loop-p (root thread)
3087   "Say whether ROOT is in THREAD."
3088   (let ((stack (list thread))
3089         (infloop 0)
3090         th)
3091     (while (setq thread (pop stack))
3092       (setq th (cdr thread))
3093       (while (and th
3094                   (not (eq (caar th) root)))
3095         (pop th))
3096       (if th
3097           ;; We have found a loop.
3098           (let (ref-dep)
3099             (setcdr thread (delq (car th) (cdr thread)))
3100             (if (boundp (setq ref-dep (intern "none"
3101                                               gnus-newsgroup-dependencies)))
3102                 (setcdr (symbol-value ref-dep)
3103                         (nconc (cdr (symbol-value ref-dep))
3104                                (list (car th))))
3105               (set ref-dep (list nil (car th))))
3106             (setq infloop 1
3107                   stack nil))
3108         ;; Push all the subthreads onto the stack.
3109         (push (cdr thread) stack)))
3110     infloop))
3111
3112 (defun gnus-make-threads ()
3113   "Go through the dependency hashtb and find the roots.  Return all threads."
3114   (let (threads)
3115     (while (catch 'infloop
3116              (mapatoms
3117               (lambda (refs)
3118                 ;; Deal with self-referencing References loops.
3119                 (when (and (car (symbol-value refs))
3120                            (not (zerop
3121                                  (apply
3122                                   '+
3123                                   (mapcar
3124                                    (lambda (thread)
3125                                      (gnus-thread-loop-p
3126                                       (car (symbol-value refs)) thread))
3127                                    (cdr (symbol-value refs)))))))
3128                   (setq threads nil)
3129                   (throw 'infloop t))
3130                 (unless (car (symbol-value refs))
3131                   ;; These threads do not refer back to any other articles,
3132                   ;; so they're roots.
3133                   (setq threads (append (cdr (symbol-value refs)) threads))))
3134               gnus-newsgroup-dependencies)))
3135     threads))
3136
3137 ;; Build the thread tree.
3138 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3139   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3140
3141 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3142 if it was already present.
3143
3144 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3145 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3146 Message-IDs will be renamed be renamed to a unique Message-ID before
3147 being entered.
3148
3149 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3150   (let* ((id (mail-header-id header))
3151          (id-dep (and id (intern id dependencies)))
3152          ref ref-dep ref-header)
3153     ;; Enter this `header' in the `dependencies' table.
3154     (cond
3155      ((not id-dep)
3156       (setq header nil))
3157      ;; The first two cases do the normal part: enter a new `header'
3158      ;; in the `dependencies' table.
3159      ((not (boundp id-dep))
3160       (set id-dep (list header)))
3161      ((null (car (symbol-value id-dep)))
3162       (setcar (symbol-value id-dep) header))
3163
3164      ;; From here the `header' was already present in the
3165      ;; `dependencies' table.
3166      (force-new
3167       ;; Overrides an existing entry;
3168       ;; just set the header part of the entry.
3169       (setcar (symbol-value id-dep) header))
3170
3171      ;; Renames the existing `header' to a unique Message-ID.
3172      ((not gnus-summary-ignore-duplicates)
3173       ;; An article with this Message-ID has already been seen.
3174       ;; We rename the Message-ID.
3175       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3176            (list header))
3177       (mail-header-set-id header id))
3178
3179      ;; The last case ignores an existing entry, except it adds any
3180      ;; additional Xrefs (in case the two articles came from different
3181      ;; servers.
3182      ;; Also sets `header' to `nil' meaning that the `dependencies'
3183      ;; table was *not* modified.
3184      (t
3185       (mail-header-set-xref
3186        (car (symbol-value id-dep))
3187        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3188                    "")
3189                (or (mail-header-xref header) "")))
3190       (setq header nil)))
3191
3192     (when header
3193       ;; First check if that we are not creating a References loop.
3194       (setq ref (gnus-parent-id (mail-header-references header)))
3195       (while (and ref
3196                   (setq ref-dep (intern-soft ref dependencies))
3197                   (boundp ref-dep)
3198                   (setq ref-header (car (symbol-value ref-dep))))
3199         (if (string= id ref)
3200             ;; Yuk!  This is a reference loop.  Make the article be a
3201             ;; root article.
3202             (progn
3203               (mail-header-set-references (car (symbol-value id-dep)) "none")
3204               (setq ref nil))
3205           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3206       (setq ref (gnus-parent-id (mail-header-references header)))
3207       (setq ref-dep (intern (or ref "none") dependencies))
3208       (if (boundp ref-dep)
3209           (setcdr (symbol-value ref-dep)
3210                   (nconc (cdr (symbol-value ref-dep))
3211                          (list (symbol-value id-dep))))
3212         (set ref-dep (list nil (symbol-value id-dep)))))
3213     header))
3214
3215 (defun gnus-build-sparse-threads ()
3216   (let ((headers gnus-newsgroup-headers)
3217         (mail-parse-charset gnus-newsgroup-charset)
3218         (gnus-summary-ignore-duplicates t)
3219         header references generation relations
3220         subject child end new-child date)
3221     ;; First we create an alist of generations/relations, where
3222     ;; generations is how much we trust the relation, and the relation
3223     ;; is parent/child.
3224     (gnus-message 7 "Making sparse threads...")
3225     (save-excursion
3226       (nnheader-set-temp-buffer " *gnus sparse threads*")
3227       (while (setq header (pop headers))
3228         (when (and (setq references (mail-header-references header))
3229                    (not (string= references "")))
3230           (insert references)
3231           (setq child (mail-header-id header)
3232                 subject (mail-header-subject header)
3233                 date (mail-header-date header)
3234                 generation 0)
3235           (while (search-backward ">" nil t)
3236             (setq end (1+ (point)))
3237             (when (search-backward "<" nil t)
3238               (setq new-child (buffer-substring (point) end))
3239               (push (list (incf generation)
3240                           child (setq child new-child)
3241                           subject date)
3242                     relations)))
3243           (when child
3244             (push (list (1+ generation) child nil subject) relations))
3245           (erase-buffer)))
3246       (kill-buffer (current-buffer)))
3247     ;; Sort over trustworthiness.
3248     (mapcar
3249      (lambda (relation)
3250        (when (gnus-dependencies-add-header
3251               (make-full-mail-header
3252                gnus-reffed-article-number
3253                (nth 3 relation) "" (or (nth 4 relation) "")
3254                (nth 1 relation)
3255                (or (nth 2 relation) "") 0 0 "")
3256               gnus-newsgroup-dependencies nil)
3257          (push gnus-reffed-article-number gnus-newsgroup-limit)
3258          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3259          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3260                gnus-newsgroup-reads)
3261          (decf gnus-reffed-article-number)))
3262      (sort relations 'car-less-than-car))
3263     (gnus-message 7 "Making sparse threads...done")))
3264
3265 (defun gnus-build-old-threads ()
3266   ;; Look at all the articles that refer back to old articles, and
3267   ;; fetch the headers for the articles that aren't there.  This will
3268   ;; build complete threads - if the roots haven't been expired by the
3269   ;; server, that is.
3270   (let ((mail-parse-charset gnus-newsgroup-charset)
3271         id heads)
3272     (mapatoms
3273      (lambda (refs)
3274        (when (not (car (symbol-value refs)))
3275          (setq heads (cdr (symbol-value refs)))
3276          (while heads
3277            (if (memq (mail-header-number (caar heads))
3278                      gnus-newsgroup-dormant)
3279                (setq heads (cdr heads))
3280              (setq id (symbol-name refs))
3281              (while (and (setq id (gnus-build-get-header id))
3282                          (not (car (gnus-id-to-thread id)))))
3283              (setq heads nil)))))
3284      gnus-newsgroup-dependencies)))
3285
3286 ;; This function has to be called with point after the article number
3287 ;; on the beginning of the line.
3288 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3289   (let ((eol (gnus-point-at-eol))
3290         (buffer (current-buffer))
3291         header)
3292
3293     ;; overview: [num subject from date id refs chars lines misc]
3294     (unless (eobp)
3295       (forward-char))
3296
3297     (setq header
3298           (make-full-mail-header
3299            number                               ; number
3300            (nnheader-nov-field)                 ; subject
3301            (nnheader-nov-field)                 ; from
3302            (nnheader-nov-field)                 ; date
3303            (nnheader-nov-read-message-id)       ; id
3304            (nnheader-nov-field)                 ; refs
3305            (nnheader-nov-read-integer)          ; chars
3306            (nnheader-nov-read-integer)          ; lines
3307            (unless (eobp)
3308              (nnheader-nov-field))              ; misc
3309            (nnheader-nov-parse-extra)))         ; extra
3310
3311     (when gnus-alter-header-function
3312       (funcall gnus-alter-header-function header))
3313     (gnus-dependencies-add-header header dependencies force-new)))
3314
3315 (defun gnus-build-get-header (id)
3316   ;; Look through the buffer of NOV lines and find the header to
3317   ;; ID.  Enter this line into the dependencies hash table, and return
3318   ;; the id of the parent article (if any).
3319   (let ((deps gnus-newsgroup-dependencies)
3320         found header)
3321     (prog1
3322         (save-excursion
3323           (set-buffer nntp-server-buffer)
3324           (let ((case-fold-search nil))
3325             (goto-char (point-min))
3326             (while (and (not found)
3327                         (search-forward id nil t))
3328               (beginning-of-line)
3329               (setq found (looking-at
3330                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3331                                    (regexp-quote id))))
3332               (or found (beginning-of-line 2)))
3333             (when found
3334               (beginning-of-line)
3335               (and
3336                (setq header (gnus-nov-parse-line
3337                              (read (current-buffer)) deps))
3338                (gnus-parent-id (mail-header-references header))))))
3339       (when header
3340         (let ((number (mail-header-number header)))
3341           (push number gnus-newsgroup-limit)
3342           (push header gnus-newsgroup-headers)
3343           (if (memq number gnus-newsgroup-unselected)
3344               (progn
3345                 (push number gnus-newsgroup-unreads)
3346                 (setq gnus-newsgroup-unselected
3347                       (delq number gnus-newsgroup-unselected)))
3348             (push number gnus-newsgroup-ancient)))))))
3349
3350 (defun gnus-build-all-threads ()
3351   "Read all the headers."
3352   (let ((gnus-summary-ignore-duplicates t)
3353         (mail-parse-charset gnus-newsgroup-charset)
3354         (dependencies gnus-newsgroup-dependencies)
3355         header article)
3356     (save-excursion
3357       (set-buffer nntp-server-buffer)
3358       (let ((case-fold-search nil))
3359         (goto-char (point-min))
3360         (while (not (eobp))
3361           (ignore-errors
3362             (setq article (read (current-buffer))
3363                   header (gnus-nov-parse-line article dependencies)))
3364           (when header
3365             (save-excursion
3366               (set-buffer gnus-summary-buffer)
3367               (push header gnus-newsgroup-headers)
3368               (if (memq (setq article (mail-header-number header))
3369                         gnus-newsgroup-unselected)
3370                   (progn
3371                     (push article gnus-newsgroup-unreads)
3372                     (setq gnus-newsgroup-unselected
3373                           (delq article gnus-newsgroup-unselected)))
3374                 (push article gnus-newsgroup-ancient)))
3375             (forward-line 1)))))))
3376
3377 (defun gnus-summary-update-article-line (article header)
3378   "Update the line for ARTICLE using HEADERS."
3379   (let* ((id (mail-header-id header))
3380          (thread (gnus-id-to-thread id)))
3381     (unless thread
3382       (error "Article in no thread"))
3383     ;; Update the thread.
3384     (setcar thread header)
3385     (gnus-summary-goto-subject article)
3386     (let* ((datal (gnus-data-find-list article))
3387            (data (car datal))
3388            (length (when (cdr datal)
3389                      (- (gnus-data-pos data)
3390                         (gnus-data-pos (cadr datal)))))
3391            (buffer-read-only nil)
3392            (level (gnus-summary-thread-level)))
3393       (gnus-delete-line)
3394       (gnus-summary-insert-line
3395        header level nil (gnus-article-mark article)
3396        (memq article gnus-newsgroup-replied)
3397        (memq article gnus-newsgroup-expirable)
3398        ;; Only insert the Subject string when it's different
3399        ;; from the previous Subject string.
3400        (if (gnus-subject-equal
3401             (condition-case ()
3402                 (mail-header-subject
3403                  (gnus-data-header
3404                   (cadr
3405                    (gnus-data-find-list
3406                     article
3407                     (gnus-data-list t)))))
3408               ;; Error on the side of excessive subjects.
3409               (error ""))
3410             (mail-header-subject header))
3411            ""
3412          (mail-header-subject header))
3413        nil (cdr (assq article gnus-newsgroup-scored))
3414        (memq article gnus-newsgroup-processable))
3415       (when length
3416         (gnus-data-update-list
3417          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3418
3419 (defun gnus-summary-update-article (article &optional iheader)
3420   "Update ARTICLE in the summary buffer."
3421   (set-buffer gnus-summary-buffer)
3422   (let* ((header (gnus-summary-article-header article))
3423          (id (mail-header-id header))
3424          (data (gnus-data-find article))
3425          (thread (gnus-id-to-thread id))
3426          (references (mail-header-references header))
3427          (parent
3428           (gnus-id-to-thread
3429            (or (gnus-parent-id
3430                 (when (and references
3431                            (not (equal "" references)))
3432                   references))
3433                "none")))
3434          (buffer-read-only nil)
3435          (old (car thread)))
3436     (when thread
3437       (unless iheader
3438         (setcar thread nil)
3439         (when parent
3440           (delq thread parent)))
3441       (if (gnus-summary-insert-subject id header)
3442           ;; Set the (possibly) new article number in the data structure.
3443           (gnus-data-set-number data (gnus-id-to-article id))
3444         (setcar thread old)
3445         nil))))
3446
3447 (defun gnus-rebuild-thread (id &optional line)
3448   "Rebuild the thread containing ID.
3449 If LINE, insert the rebuilt thread starting on line LINE."
3450   (let ((buffer-read-only nil)
3451         old-pos current thread data)
3452     (if (not gnus-show-threads)
3453         (setq thread (list (car (gnus-id-to-thread id))))
3454       ;; Get the thread this article is part of.
3455       (setq thread (gnus-remove-thread id)))
3456     (setq old-pos (gnus-point-at-bol))
3457     (setq current (save-excursion
3458                     (and (zerop (forward-line -1))
3459                          (gnus-summary-article-number))))
3460     ;; If this is a gathered thread, we have to go some re-gathering.
3461     (when (stringp (car thread))
3462       (let ((subject (car thread))
3463             roots thr)
3464         (setq thread (cdr thread))
3465         (while thread
3466           (unless (memq (setq thr (gnus-id-to-thread
3467                                    (gnus-root-id
3468                                     (mail-header-id (caar thread)))))
3469                         roots)
3470             (push thr roots))
3471           (setq thread (cdr thread)))
3472         ;; We now have all (unique) roots.
3473         (if (= (length roots) 1)
3474             ;; All the loose roots are now one solid root.
3475             (setq thread (car roots))
3476           (setq thread (cons subject (gnus-sort-threads roots))))))
3477     (let (threads)
3478       ;; We then insert this thread into the summary buffer.
3479       (when line
3480         (goto-char (point-min))
3481         (forward-line (1- line)))
3482       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3483         (if gnus-show-threads
3484             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3485           (gnus-summary-prepare-unthreaded thread))
3486         (setq data (nreverse gnus-newsgroup-data))
3487         (setq threads gnus-newsgroup-threads))
3488       ;; We splice the new data into the data structure.
3489       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3490       ;;!!! then we want to insert at the beginning of the buffer.
3491       ;;!!! That happens to be true with Gnus now, but that may
3492       ;;!!! change in the future.  Perhaps.
3493       (gnus-data-enter-list
3494        (if line nil current) data (- (point) old-pos))
3495       (setq gnus-newsgroup-threads
3496             (nconc threads gnus-newsgroup-threads))
3497       (gnus-data-compute-positions))))
3498
3499 (defun gnus-number-to-header (number)
3500   "Return the header for article NUMBER."
3501   (let ((headers gnus-newsgroup-headers))
3502     (while (and headers
3503                 (not (= number (mail-header-number (car headers)))))
3504       (pop headers))
3505     (when headers
3506       (car headers))))
3507
3508 (defun gnus-parent-headers (in-headers &optional generation)
3509   "Return the headers of the GENERATIONeth parent of HEADERS."
3510   (unless generation
3511     (setq generation 1))
3512   (let ((parent t)
3513         (headers in-headers)
3514         references)
3515     (while (and parent
3516                 (not (zerop generation))
3517                 (setq references (mail-header-references headers)))
3518       (setq headers (if (and references
3519                              (setq parent (gnus-parent-id references)))
3520                         (car (gnus-id-to-thread parent))
3521                       nil))
3522       (decf generation))
3523     (and (not (eq headers in-headers))
3524          headers)))
3525
3526 (defun gnus-id-to-thread (id)
3527   "Return the (sub-)thread where ID appears."
3528   (gnus-gethash id gnus-newsgroup-dependencies))
3529
3530 (defun gnus-id-to-article (id)
3531   "Return the article number of ID."
3532   (let ((thread (gnus-id-to-thread id)))
3533     (when (and thread
3534                (car thread))
3535       (mail-header-number (car thread)))))
3536
3537 (defun gnus-id-to-header (id)
3538   "Return the article headers of ID."
3539   (car (gnus-id-to-thread id)))
3540
3541 (defun gnus-article-displayed-root-p (article)
3542   "Say whether ARTICLE is a root(ish) article."
3543   (let ((level (gnus-summary-thread-level article))
3544         (refs (mail-header-references  (gnus-summary-article-header article)))
3545         particle)
3546     (cond
3547      ((null level) nil)
3548      ((zerop level) t)
3549      ((null refs) t)
3550      ((null (gnus-parent-id refs)) t)
3551      ((and (= 1 level)
3552            (null (setq particle (gnus-id-to-article
3553                                  (gnus-parent-id refs))))
3554            (null (gnus-summary-thread-level particle)))))))
3555
3556 (defun gnus-root-id (id)
3557   "Return the id of the root of the thread where ID appears."
3558   (let (last-id prev)
3559     (while (and id (setq prev (car (gnus-id-to-thread id))))
3560       (setq last-id id
3561             id (gnus-parent-id (mail-header-references prev))))
3562     last-id))
3563
3564 (defun gnus-articles-in-thread (thread)
3565   "Return the list of articles in THREAD."
3566   (cons (mail-header-number (car thread))
3567         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3568
3569 (defun gnus-remove-thread (id &optional dont-remove)
3570   "Remove the thread that has ID in it."
3571   (let (headers thread last-id)
3572     ;; First go up in this thread until we find the root.
3573     (setq last-id (gnus-root-id id)
3574           headers (message-flatten-list (gnus-id-to-thread last-id)))
3575     ;; We have now found the real root of this thread.  It might have
3576     ;; been gathered into some loose thread, so we have to search
3577     ;; through the threads to find the thread we wanted.
3578     (let ((threads gnus-newsgroup-threads)
3579           sub)
3580       (while threads
3581         (setq sub (car threads))
3582         (if (stringp (car sub))
3583             ;; This is a gathered thread, so we look at the roots
3584             ;; below it to find whether this article is in this
3585             ;; gathered root.
3586             (progn
3587               (setq sub (cdr sub))
3588               (while sub
3589                 (when (member (caar sub) headers)
3590                   (setq thread (car threads)
3591                         threads nil
3592                         sub nil))
3593                 (setq sub (cdr sub))))
3594           ;; It's an ordinary thread, so we check it.
3595           (when (eq (car sub) (car headers))
3596             (setq thread sub
3597                   threads nil)))
3598         (setq threads (cdr threads)))
3599       ;; If this article is in no thread, then it's a root.
3600       (if thread
3601           (unless dont-remove
3602             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3603         (setq thread (gnus-id-to-thread last-id)))
3604       (when thread
3605         (prog1
3606             thread                      ; We return this thread.
3607           (unless dont-remove
3608             (if (stringp (car thread))
3609                 (progn
3610                   ;; If we use dummy roots, then we have to remove the
3611                   ;; dummy root as well.
3612                   (when (eq gnus-summary-make-false-root 'dummy)
3613                     ;; We go to the dummy root by going to
3614                     ;; the first sub-"thread", and then one line up.
3615                     (gnus-summary-goto-article
3616                      (mail-header-number (caadr thread)))
3617                     (forward-line -1)
3618                     (gnus-delete-line)
3619                     (gnus-data-compute-positions))
3620                   (setq thread (cdr thread))
3621                   (while thread
3622                     (gnus-remove-thread-1 (car thread))
3623                     (setq thread (cdr thread))))
3624               (gnus-summary-show-all-threads)
3625               (gnus-remove-thread-1 thread))))))))
3626
3627 (defun gnus-remove-thread-1 (thread)
3628   "Remove the thread THREAD recursively."
3629   (let ((number (mail-header-number (pop thread)))
3630         d)
3631     (setq thread (reverse thread))
3632     (while thread
3633       (gnus-remove-thread-1 (pop thread)))
3634     (when (setq d (gnus-data-find number))
3635       (goto-char (gnus-data-pos d))
3636       (gnus-data-remove
3637        number
3638        (- (gnus-point-at-bol)
3639           (prog1
3640               (1+ (gnus-point-at-eol))
3641             (gnus-delete-line)))))))
3642
3643 (defun gnus-sort-threads (threads)
3644   "Sort THREADS."
3645   (if (not gnus-thread-sort-functions)
3646       threads
3647     (gnus-message 8 "Sorting threads...")
3648     (prog1
3649         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3650       (gnus-message 8 "Sorting threads...done"))))
3651
3652 (defun gnus-sort-articles (articles)
3653   "Sort ARTICLES."
3654   (when gnus-article-sort-functions
3655     (gnus-message 7 "Sorting articles...")
3656     (prog1
3657         (setq gnus-newsgroup-headers
3658               (sort articles (gnus-make-sort-function
3659                               gnus-article-sort-functions)))
3660       (gnus-message 7 "Sorting articles...done"))))
3661
3662 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3663 (defmacro gnus-thread-header (thread)
3664   ;; Return header of first article in THREAD.
3665   ;; Note that THREAD must never, ever be anything else than a variable -
3666   ;; using some other form will lead to serious barfage.
3667   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3668   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3669   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3670         (vector thread) 2))
3671
3672 (defsubst gnus-article-sort-by-number (h1 h2)
3673   "Sort articles by article number."
3674   (< (mail-header-number h1)
3675      (mail-header-number h2)))
3676
3677 (defun gnus-thread-sort-by-number (h1 h2)
3678   "Sort threads by root article number."
3679   (gnus-article-sort-by-number
3680    (gnus-thread-header h1) (gnus-thread-header h2)))
3681
3682 (defsubst gnus-article-sort-by-lines (h1 h2)
3683   "Sort articles by article Lines header."
3684   (< (mail-header-lines h1)
3685      (mail-header-lines h2)))
3686
3687 (defun gnus-thread-sort-by-lines (h1 h2)
3688   "Sort threads by root article Lines header."
3689   (gnus-article-sort-by-lines
3690    (gnus-thread-header h1) (gnus-thread-header h2)))
3691
3692 (defsubst gnus-article-sort-by-chars (h1 h2)
3693   "Sort articles by octet length."
3694   (< (mail-header-chars h1)
3695      (mail-header-chars h2)))
3696
3697 (defun gnus-thread-sort-by-chars (h1 h2)
3698   "Sort threads by root article octet length."
3699   (gnus-article-sort-by-chars
3700    (gnus-thread-header h1) (gnus-thread-header h2)))
3701
3702 (defsubst gnus-article-sort-by-author (h1 h2)
3703   "Sort articles by root author."
3704   (string-lessp
3705    (let ((addr (car (mime-entity-read-field h1 'From))))
3706      (or (std11-full-name-string addr)
3707          (std11-address-string addr)
3708          ""))
3709    (let ((addr (car (mime-entity-read-field h2 'From))))
3710      (or (std11-full-name-string addr)
3711          (std11-address-string addr)
3712          ""))
3713    ))
3714
3715 (defun gnus-thread-sort-by-author (h1 h2)
3716   "Sort threads by root author."
3717   (gnus-article-sort-by-author
3718    (gnus-thread-header h1)  (gnus-thread-header h2)))
3719
3720 (defsubst gnus-article-sort-by-subject (h1 h2)
3721   "Sort articles by root subject."
3722   (string-lessp
3723    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3724    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3725
3726 (defun gnus-thread-sort-by-subject (h1 h2)
3727   "Sort threads by root subject."
3728   (gnus-article-sort-by-subject
3729    (gnus-thread-header h1) (gnus-thread-header h2)))
3730
3731 (defsubst gnus-article-sort-by-date (h1 h2)
3732   "Sort articles by root article date."
3733   (time-less-p
3734    (gnus-date-get-time (mail-header-date h1))
3735    (gnus-date-get-time (mail-header-date h2))))
3736
3737 (defun gnus-thread-sort-by-date (h1 h2)
3738   "Sort threads by root article date."
3739   (gnus-article-sort-by-date
3740    (gnus-thread-header h1) (gnus-thread-header h2)))
3741
3742 (defsubst gnus-article-sort-by-score (h1 h2)
3743   "Sort articles by root article score.
3744 Unscored articles will be counted as having a score of zero."
3745   (> (or (cdr (assq (mail-header-number h1)
3746                     gnus-newsgroup-scored))
3747          gnus-summary-default-score 0)
3748      (or (cdr (assq (mail-header-number h2)
3749                     gnus-newsgroup-scored))
3750          gnus-summary-default-score 0)))
3751
3752 (defun gnus-thread-sort-by-score (h1 h2)
3753   "Sort threads by root article score."
3754   (gnus-article-sort-by-score
3755    (gnus-thread-header h1) (gnus-thread-header h2)))
3756
3757 (defun gnus-thread-sort-by-total-score (h1 h2)
3758   "Sort threads by the sum of all scores in the thread.
3759 Unscored articles will be counted as having a score of zero."
3760   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3761
3762 (defun gnus-thread-total-score (thread)
3763   ;; This function find the total score of THREAD.
3764   (cond ((null thread)
3765          0)
3766         ((consp thread)
3767          (if (stringp (car thread))
3768              (apply gnus-thread-score-function 0
3769                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3770            (gnus-thread-total-score-1 thread)))
3771         (t
3772          (gnus-thread-total-score-1 (list thread)))))
3773
3774 (defun gnus-thread-total-score-1 (root)
3775   ;; This function find the total score of the thread below ROOT.
3776   (setq root (car root))
3777   (apply gnus-thread-score-function
3778          (or (append
3779               (mapcar 'gnus-thread-total-score
3780                       (cdr (gnus-id-to-thread (mail-header-id root))))
3781               (when (> (mail-header-number root) 0)
3782                 (list (or (cdr (assq (mail-header-number root)
3783                                      gnus-newsgroup-scored))
3784                           gnus-summary-default-score 0))))
3785              (list gnus-summary-default-score)
3786              '(0))))
3787
3788 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3789 (defvar gnus-tmp-prev-subject nil)
3790 (defvar gnus-tmp-false-parent nil)
3791 (defvar gnus-tmp-root-expunged nil)
3792 (defvar gnus-tmp-dummy-line nil)
3793
3794 (defvar gnus-tmp-header)
3795 (defun gnus-extra-header (type &optional header)
3796   "Return the extra header of TYPE."
3797   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3798       ""))
3799
3800 (defun gnus-summary-prepare-threads (threads)
3801   "Prepare summary buffer from THREADS and indentation LEVEL.
3802 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3803 or a straight list of headers."
3804   (gnus-message 7 "Generating summary...")
3805
3806   (setq gnus-newsgroup-threads threads)
3807   (beginning-of-line)
3808
3809   (let ((gnus-tmp-level 0)
3810         (default-score (or gnus-summary-default-score 0))
3811         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3812         thread number subject stack state gnus-tmp-gathered beg-match
3813         new-roots gnus-tmp-new-adopts thread-end
3814         gnus-tmp-header gnus-tmp-unread
3815         gnus-tmp-replied gnus-tmp-subject-or-nil
3816         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3817         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3818         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3819
3820     (setq gnus-tmp-prev-subject nil)
3821
3822     (if (vectorp (car threads))
3823         ;; If this is a straight (sic) list of headers, then a
3824         ;; threaded summary display isn't required, so we just create
3825         ;; an unthreaded one.
3826         (gnus-summary-prepare-unthreaded threads)
3827
3828       ;; Do the threaded display.
3829
3830       (while (or threads stack gnus-tmp-new-adopts new-roots)
3831
3832         (if (and (= gnus-tmp-level 0)
3833                  (or (not stack)
3834                      (= (caar stack) 0))
3835                  (not gnus-tmp-false-parent)
3836                  (or gnus-tmp-new-adopts new-roots))
3837             (if gnus-tmp-new-adopts
3838                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3839                       thread (list (car gnus-tmp-new-adopts))
3840                       gnus-tmp-header (caar thread)
3841                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3842               (when new-roots
3843                 (setq thread (list (car new-roots))
3844                       gnus-tmp-header (caar thread)
3845                       new-roots (cdr new-roots))))
3846
3847           (if threads
3848               ;; If there are some threads, we do them before the
3849               ;; threads on the stack.
3850               (setq thread threads
3851                     gnus-tmp-header (caar thread))
3852             ;; There were no current threads, so we pop something off
3853             ;; the stack.
3854             (setq state (car stack)
3855                   gnus-tmp-level (car state)
3856                   thread (cdr state)
3857                   stack (cdr stack)
3858                   gnus-tmp-header (caar thread))))
3859
3860         (setq gnus-tmp-false-parent nil)
3861         (setq gnus-tmp-root-expunged nil)
3862         (setq thread-end nil)
3863
3864         (if (stringp gnus-tmp-header)
3865             ;; The header is a dummy root.
3866             (cond
3867              ((eq gnus-summary-make-false-root 'adopt)
3868               ;; We let the first article adopt the rest.
3869               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3870                                                (cddar thread)))
3871               (setq gnus-tmp-gathered
3872                     (nconc (mapcar
3873                             (lambda (h) (mail-header-number (car h)))
3874                             (cddar thread))
3875                            gnus-tmp-gathered))
3876               (setq thread (cons (list (caar thread)
3877                                        (cadar thread))
3878                                  (cdr thread)))
3879               (setq gnus-tmp-level -1
3880                     gnus-tmp-false-parent t))
3881              ((eq gnus-summary-make-false-root 'empty)
3882               ;; We print adopted articles with empty subject fields.
3883               (setq gnus-tmp-gathered
3884                     (nconc (mapcar
3885                             (lambda (h) (mail-header-number (car h)))
3886                             (cddar thread))
3887                            gnus-tmp-gathered))
3888               (setq gnus-tmp-level -1))
3889              ((eq gnus-summary-make-false-root 'dummy)
3890               ;; We remember that we probably want to output a dummy
3891               ;; root.
3892               (setq gnus-tmp-dummy-line gnus-tmp-header)
3893               (setq gnus-tmp-prev-subject gnus-tmp-header))
3894              (t
3895               ;; We do not make a root for the gathered
3896               ;; sub-threads at all.
3897               (setq gnus-tmp-level -1)))
3898
3899           (setq number (mail-header-number gnus-tmp-header)
3900                 subject (mail-header-subject gnus-tmp-header))
3901
3902           (cond
3903            ;; If the thread has changed subject, we might want to make
3904            ;; this subthread into a root.
3905            ((and (null gnus-thread-ignore-subject)
3906                  (not (zerop gnus-tmp-level))
3907                  gnus-tmp-prev-subject
3908                  (not (inline
3909                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3910             (setq new-roots (nconc new-roots (list (car thread)))
3911                   thread-end t
3912                   gnus-tmp-header nil))
3913            ;; If the article lies outside the current limit,
3914            ;; then we do not display it.
3915            ((not (memq number gnus-newsgroup-limit))
3916             (setq gnus-tmp-gathered
3917                   (nconc (mapcar
3918                           (lambda (h) (mail-header-number (car h)))
3919                           (cdar thread))
3920                          gnus-tmp-gathered))
3921             (setq gnus-tmp-new-adopts (if (cdar thread)
3922                                           (append gnus-tmp-new-adopts
3923                                                   (cdar thread))
3924                                         gnus-tmp-new-adopts)
3925                   thread-end t
3926                   gnus-tmp-header nil)
3927             (when (zerop gnus-tmp-level)
3928               (setq gnus-tmp-root-expunged t)))
3929            ;; Perhaps this article is to be marked as read?
3930            ((and gnus-summary-mark-below
3931                  (< (or (cdr (assq number gnus-newsgroup-scored))
3932                         default-score)
3933                     gnus-summary-mark-below)
3934                  ;; Don't touch sparse articles.
3935                  (not (gnus-summary-article-sparse-p number))
3936                  (not (gnus-summary-article-ancient-p number)))
3937             (setq gnus-newsgroup-unreads
3938                   (delq number gnus-newsgroup-unreads))
3939             (if gnus-newsgroup-auto-expire
3940                 (push number gnus-newsgroup-expirable)
3941               (push (cons number gnus-low-score-mark)
3942                     gnus-newsgroup-reads))))
3943
3944           (when gnus-tmp-header
3945             ;; We may have an old dummy line to output before this
3946             ;; article.
3947             (when (and gnus-tmp-dummy-line
3948                        (gnus-subject-equal
3949                         gnus-tmp-dummy-line
3950                         (mail-header-subject gnus-tmp-header)))
3951               (gnus-summary-insert-dummy-line
3952                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3953               (setq gnus-tmp-dummy-line nil))
3954
3955             ;; Compute the mark.
3956             (setq gnus-tmp-unread (gnus-article-mark number))
3957
3958             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3959                                   gnus-tmp-header gnus-tmp-level)
3960                   gnus-newsgroup-data)
3961
3962             ;; Actually insert the line.
3963             (setq
3964              gnus-tmp-subject-or-nil
3965              (cond
3966               ((and gnus-thread-ignore-subject
3967                     gnus-tmp-prev-subject
3968                     (not (inline (gnus-subject-equal
3969                                   gnus-tmp-prev-subject subject))))
3970                subject)
3971               ((zerop gnus-tmp-level)
3972                (if (and (eq gnus-summary-make-false-root 'empty)
3973                         (memq number gnus-tmp-gathered)
3974                         gnus-tmp-prev-subject
3975                         (inline (gnus-subject-equal
3976                                  gnus-tmp-prev-subject subject)))
3977                    gnus-summary-same-subject
3978                  subject))
3979               (t gnus-summary-same-subject)))
3980             (if (and (eq gnus-summary-make-false-root 'adopt)
3981                      (= gnus-tmp-level 1)
3982                      (memq number gnus-tmp-gathered))
3983                 (setq gnus-tmp-opening-bracket ?\<
3984                       gnus-tmp-closing-bracket ?\>)
3985               (setq gnus-tmp-opening-bracket ?\[
3986                     gnus-tmp-closing-bracket ?\]))
3987             (setq
3988              gnus-tmp-indentation
3989              (aref gnus-thread-indent-array gnus-tmp-level)
3990              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3991              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3992                                 gnus-summary-default-score 0)
3993              gnus-tmp-score-char
3994              (if (or (null gnus-summary-default-score)
3995                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3996                          gnus-summary-zcore-fuzz))
3997                  ?  ;Whitespace
3998                (if (< gnus-tmp-score gnus-summary-default-score)
3999                    gnus-score-below-mark gnus-score-over-mark))
4000              gnus-tmp-replied
4001              (cond ((memq number gnus-newsgroup-processable)
4002                     gnus-process-mark)
4003                    ((memq number gnus-newsgroup-cached)
4004                     gnus-cached-mark)
4005                    ((memq number gnus-newsgroup-replied)
4006                     gnus-replied-mark)
4007                    ((memq number gnus-newsgroup-saved)
4008                     gnus-saved-mark)
4009                    (t gnus-unread-mark))
4010              gnus-tmp-from (mail-header-from gnus-tmp-header)
4011              gnus-tmp-name
4012              (cond
4013               ((string-match "<[^>]+> *$" gnus-tmp-from)
4014                (setq beg-match (match-beginning 0))
4015                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4016                         (substring gnus-tmp-from (1+ (match-beginning 0))
4017                                    (1- (match-end 0))))
4018                    (substring gnus-tmp-from 0 beg-match)))
4019               ((string-match "(.+)" gnus-tmp-from)
4020                (substring gnus-tmp-from
4021                           (1+ (match-beginning 0)) (1- (match-end 0))))
4022               (t gnus-tmp-from)))
4023             (when (string= gnus-tmp-name "")
4024               (setq gnus-tmp-name gnus-tmp-from))
4025             (unless (numberp gnus-tmp-lines)
4026               (setq gnus-tmp-lines 0))
4027             (gnus-put-text-property
4028              (point)
4029              (progn (eval gnus-summary-line-format-spec) (point))
4030              'gnus-number number)
4031             (when gnus-visual-p
4032               (forward-line -1)
4033               (gnus-run-hooks 'gnus-summary-update-hook)
4034               (forward-line 1))
4035
4036             (setq gnus-tmp-prev-subject subject)))
4037
4038         (when (nth 1 thread)
4039           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4040         (incf gnus-tmp-level)
4041         (setq threads (if thread-end nil (cdar thread)))
4042         (unless threads
4043           (setq gnus-tmp-level 0)))))
4044   (gnus-message 7 "Generating summary...done"))
4045
4046 (defun gnus-summary-prepare-unthreaded (headers)
4047   "Generate an unthreaded summary buffer based on HEADERS."
4048   (let (header number mark)
4049
4050     (beginning-of-line)
4051
4052     (while headers
4053       ;; We may have to root out some bad articles...
4054       (when (memq (setq number (mail-header-number
4055                                 (setq header (pop headers))))
4056                   gnus-newsgroup-limit)
4057         ;; Mark article as read when it has a low score.
4058         (when (and gnus-summary-mark-below
4059                    (< (or (cdr (assq number gnus-newsgroup-scored))
4060                           gnus-summary-default-score 0)
4061                       gnus-summary-mark-below)
4062                    (not (gnus-summary-article-ancient-p number)))
4063           (setq gnus-newsgroup-unreads
4064                 (delq number gnus-newsgroup-unreads))
4065           (if gnus-newsgroup-auto-expire
4066               (push number gnus-newsgroup-expirable)
4067             (push (cons number gnus-low-score-mark)
4068                   gnus-newsgroup-reads)))
4069
4070         (setq mark (gnus-article-mark number))
4071         (push (gnus-data-make number mark (1+ (point)) header 0)
4072               gnus-newsgroup-data)
4073         (gnus-summary-insert-line
4074          header 0 number
4075          mark (memq number gnus-newsgroup-replied)
4076          (memq number gnus-newsgroup-expirable)
4077          (mail-header-subject header) nil
4078          (cdr (assq number gnus-newsgroup-scored))
4079          (memq number gnus-newsgroup-processable))))))
4080
4081 (defun gnus-summary-remove-list-identifiers ()
4082   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4083   (let ((regexp (if (stringp gnus-list-identifiers)
4084                     gnus-list-identifiers
4085                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4086     (dolist (header gnus-newsgroup-headers)
4087       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4088                           (mail-header-subject header))
4089         (mail-header-set-subject
4090          header (concat (substring (mail-header-subject header)
4091                                    0 (match-beginning 2))
4092                         (substring (mail-header-subject header)
4093                                    (match-end 2))))))))
4094
4095 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4096   "Select newsgroup GROUP.
4097 If READ-ALL is non-nil, all articles in the group are selected.
4098 If SELECT-ARTICLES, only select those articles from GROUP."
4099   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4100          ;;!!! Dirty hack; should be removed.
4101          (gnus-summary-ignore-duplicates
4102           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4103               t
4104             gnus-summary-ignore-duplicates))
4105          (info (nth 2 entry))
4106          articles fetched-articles cached)
4107
4108     (unless (gnus-check-server
4109              (setq gnus-current-select-method
4110                    (gnus-find-method-for-group group)))
4111       (error "Couldn't open server"))
4112
4113     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4114         (gnus-activate-group group)     ; Or we can activate it...
4115         (progn                          ; Or we bug out.
4116           (when (equal major-mode 'gnus-summary-mode)
4117             (kill-buffer (current-buffer)))
4118           (error "Couldn't request group %s: %s"
4119                  group (gnus-status-message group))))
4120
4121     (unless (gnus-request-group group t)
4122       (when (equal major-mode 'gnus-summary-mode)
4123         (kill-buffer (current-buffer)))
4124       (error "Couldn't request group %s: %s"
4125              group (gnus-status-message group)))
4126
4127     (setq gnus-newsgroup-name group)
4128     (setq gnus-newsgroup-unselected nil)
4129     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4130     (gnus-summary-setup-default-charset)
4131
4132     ;; Adjust and set lists of article marks.
4133     (when info
4134       (gnus-adjust-marked-articles info))
4135
4136     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4137     (setq cached
4138           (if (gnus-virtual-group-p group)
4139               gnus-newsgroup-cached
4140             (gnus-cache-articles-in-group group)))
4141
4142     (setq gnus-newsgroup-unreads
4143           (gnus-set-difference
4144            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4145            gnus-newsgroup-dormant))
4146
4147     (setq gnus-newsgroup-processable nil)
4148
4149     (gnus-update-read-articles group gnus-newsgroup-unreads)
4150
4151     (if (setq articles select-articles)
4152         (setq gnus-newsgroup-unselected
4153               (gnus-sorted-intersection
4154                gnus-newsgroup-unreads
4155                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4156       (setq articles (gnus-articles-to-read group read-all)))
4157
4158     (cond
4159      ((null articles)
4160       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4161       'quit)
4162      ((eq articles 0) nil)
4163      (t
4164       ;; Init the dependencies hash table.
4165       (setq gnus-newsgroup-dependencies
4166             (gnus-make-hashtable (length articles)))
4167       (gnus-set-global-variables)
4168       ;; Retrieve the headers and read them in.
4169       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4170       (setq gnus-newsgroup-headers
4171             (gnus-retrieve-parsed-headers
4172              articles gnus-newsgroup-name
4173              ;; We might want to fetch old headers, but
4174              ;; not if there is only 1 article.
4175              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4176                            (not (numberp gnus-fetch-old-headers)))
4177                       (> (length articles) 1))
4178                   gnus-fetch-old-headers)))
4179       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4180
4181       ;; Suppress duplicates?
4182       (when gnus-suppress-duplicates
4183         (gnus-dup-suppress-articles))
4184
4185       ;; Set the initial limit.
4186       (setq gnus-newsgroup-limit (copy-sequence articles))
4187       ;; Remove canceled articles from the list of unread articles.
4188       (setq gnus-newsgroup-unreads
4189             (gnus-set-sorted-intersection
4190              gnus-newsgroup-unreads
4191              (setq fetched-articles
4192                    (mapcar (lambda (headers) (mail-header-number headers))
4193                            gnus-newsgroup-headers))))
4194       ;; Removed marked articles that do not exist.
4195       (gnus-update-missing-marks
4196        (gnus-sorted-complement fetched-articles articles))
4197
4198       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4199       (when cached
4200         (setq gnus-newsgroup-cached cached))
4201
4202       ;; We might want to build some more threads first.
4203       (when (and gnus-fetch-old-headers
4204                  (eq gnus-headers-retrieved-by 'nov))
4205         (if (eq gnus-fetch-old-headers 'invisible)
4206             (gnus-build-all-threads)
4207           (gnus-build-old-threads)))
4208       ;; Let the Gnus agent mark articles as read.
4209       (when gnus-agent
4210         (gnus-agent-get-undownloaded-list))
4211       ;; Remove list identifiers from subject
4212       (when gnus-list-identifiers
4213         (gnus-summary-remove-list-identifiers))
4214       ;; Check whether auto-expire is to be done in this group.
4215       (setq gnus-newsgroup-auto-expire
4216             (gnus-group-auto-expirable-p group))
4217       ;; Set up the article buffer now, if necessary.
4218       (unless gnus-single-article-buffer
4219         (gnus-article-setup-buffer))
4220       ;; First and last article in this newsgroup.
4221       (when gnus-newsgroup-headers
4222         (setq gnus-newsgroup-begin
4223               (mail-header-number (car gnus-newsgroup-headers))
4224               gnus-newsgroup-end
4225               (mail-header-number
4226                (gnus-last-element gnus-newsgroup-headers))))
4227       ;; GROUP is successfully selected.
4228       (or gnus-newsgroup-headers t)))))
4229
4230 (defun gnus-articles-to-read (group &optional read-all)
4231   ;; Find out what articles the user wants to read.
4232   (let* ((articles
4233           ;; Select all articles if `read-all' is non-nil, or if there
4234           ;; are no unread articles.
4235           (if (or read-all
4236                   (and (zerop (length gnus-newsgroup-marked))
4237                        (zerop (length gnus-newsgroup-unreads)))
4238                   (eq (gnus-group-find-parameter group 'display)
4239                       'all))
4240               (or
4241                (gnus-uncompress-range (gnus-active group))
4242                (gnus-cache-articles-in-group group))
4243             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4244                           (copy-sequence gnus-newsgroup-unreads))
4245                   '<)))
4246          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4247          (scored (length scored-list))
4248          (number (length articles))
4249          (marked (+ (length gnus-newsgroup-marked)
4250                     (length gnus-newsgroup-dormant)))
4251          (select
4252           (cond
4253            ((numberp read-all)
4254             read-all)
4255            (t
4256             (condition-case ()
4257                 (cond
4258                  ((and (or (<= scored marked) (= scored number))
4259                        (natnump gnus-large-newsgroup)
4260                        (> number gnus-large-newsgroup))
4261                   (let* ((cursor-in-echo-area nil)
4262                          (input (read-from-minibuffer
4263                                  (format
4264                                   "How many articles from %s (max %d): "
4265                                   (gnus-limit-string gnus-newsgroup-name 35)
4266                                   number)
4267                                  (cons (number-to-string gnus-large-newsgroup)
4268                                        0))))
4269                     (if (string-match "^[ \t]*$" input)
4270                         number
4271                       input)))
4272                  ((and (> scored marked) (< scored number)
4273                        (> (- scored number) 20))
4274                   (let ((input
4275                          (read-string
4276                           (format "%s %s (%d scored, %d total): "
4277                                   "How many articles from"
4278                                   group scored number))))
4279                     (if (string-match "^[ \t]*$" input)
4280                         number input)))
4281                  (t number))
4282               (quit nil))))))
4283     (setq select (if (stringp select) (string-to-number select) select))
4284     (if (or (null select) (zerop select))
4285         select
4286       (if (and (not (zerop scored)) (<= (abs select) scored))
4287           (progn
4288             (setq articles (sort scored-list '<))
4289             (setq number (length articles)))
4290         (setq articles (copy-sequence articles)))
4291
4292       (when (< (abs select) number)
4293         (if (< select 0)
4294             ;; Select the N oldest articles.
4295             (setcdr (nthcdr (1- (abs select)) articles) nil)
4296           ;; Select the N most recent articles.
4297           (setq articles (nthcdr (- number select) articles))))
4298       (setq gnus-newsgroup-unselected
4299             (gnus-sorted-intersection
4300              gnus-newsgroup-unreads
4301              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4302       articles)))
4303
4304 (defun gnus-killed-articles (killed articles)
4305   (let (out)
4306     (while articles
4307       (when (inline (gnus-member-of-range (car articles) killed))
4308         (push (car articles) out))
4309       (setq articles (cdr articles)))
4310     out))
4311
4312 (defun gnus-uncompress-marks (marks)
4313   "Uncompress the mark ranges in MARKS."
4314   (let ((uncompressed '(score bookmark))
4315         out)
4316     (while marks
4317       (if (memq (caar marks) uncompressed)
4318           (push (car marks) out)
4319         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4320       (setq marks (cdr marks)))
4321     out))
4322
4323 (defun gnus-adjust-marked-articles (info)
4324   "Set all article lists and remove all marks that are no longer valid."
4325   (let* ((marked-lists (gnus-info-marks info))
4326          (active (gnus-active (gnus-info-group info)))
4327          (min (car active))
4328          (max (cdr active))
4329          (types gnus-article-mark-lists)
4330          (uncompressed '(score bookmark killed))
4331          marks var articles article mark)
4332
4333     (while marked-lists
4334       (setq marks (pop marked-lists))
4335       (set (setq var (intern (format "gnus-newsgroup-%s"
4336                                      (car (rassq (setq mark (car marks))
4337                                                  types)))))
4338            (if (memq (car marks) uncompressed) (cdr marks)
4339              (gnus-uncompress-range (cdr marks))))
4340
4341       (setq articles (symbol-value var))
4342
4343       ;; All articles have to be subsets of the active articles.
4344       (cond
4345        ;; Adjust "simple" lists.
4346        ((memq mark '(tick dormant expire reply save))
4347         (while articles
4348           (when (or (< (setq article (pop articles)) min) (> article max))
4349             (set var (delq article (symbol-value var))))))
4350        ;; Adjust assocs.
4351        ((memq mark uncompressed)
4352         (when (not (listp (cdr (symbol-value var))))
4353           (set var (list (symbol-value var))))
4354         (when (not (listp (cdr articles)))
4355           (setq articles (list articles)))
4356         (while articles
4357           (when (or (not (consp (setq article (pop articles))))
4358                     (< (car article) min)
4359                     (> (car article) max))
4360             (set var (delq article (symbol-value var))))))))))
4361
4362 (defun gnus-update-missing-marks (missing)
4363   "Go through the list of MISSING articles and remove them from the mark lists."
4364   (when missing
4365     (let ((types gnus-article-mark-lists)
4366           var m)
4367       ;; Go through all types.
4368       (while types
4369         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4370         (when (symbol-value var)
4371           ;; This list has articles.  So we delete all missing articles
4372           ;; from it.
4373           (setq m missing)
4374           (while m
4375             (set var (delq (pop m) (symbol-value var)))))))))
4376
4377 (defun gnus-update-marks ()
4378   "Enter the various lists of marked articles into the newsgroup info list."
4379   (let ((types gnus-article-mark-lists)
4380         (info (gnus-get-info gnus-newsgroup-name))
4381         (uncompressed '(score bookmark killed))
4382         type list newmarked symbol delta-marks)
4383     (when info
4384       ;; Add all marks lists to the list of marks lists.
4385       (while (setq type (pop types))
4386         (setq list (symbol-value
4387                     (setq symbol
4388                           (intern (format "gnus-newsgroup-%s"
4389                                           (car type))))))
4390
4391         (when list
4392           ;; Get rid of the entries of the articles that have the
4393           ;; default score.
4394           (when (and (eq (cdr type) 'score)
4395                      gnus-save-score
4396                      list)
4397             (let* ((arts list)
4398                    (prev (cons nil list))
4399                    (all prev))
4400               (while arts
4401                 (if (or (not (consp (car arts)))
4402                         (= (cdar arts) gnus-summary-default-score))
4403                     (setcdr prev (cdr arts))
4404                   (setq prev arts))
4405                 (setq arts (cdr arts)))
4406               (setq list (cdr all)))))
4407
4408         (unless (memq (cdr type) uncompressed)
4409           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4410        
4411         (when (gnus-check-backend-function
4412                'request-set-mark gnus-newsgroup-name)
4413           ;; uncompressed:s are not proper flags (they are cons cells)
4414           ;; cache is a internal gnus flag
4415           (unless (memq (cdr type) (cons 'cache uncompressed))
4416             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4417                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4418                    (add (gnus-remove-from-range
4419                          (gnus-copy-sequence list) old)))
4420               (when add
4421                 (push (list add 'add (list (cdr type))) delta-marks))
4422               (when del
4423                 (push (list del 'del (list (cdr type))) delta-marks)))))
4424           
4425         (when list
4426           (push (cons (cdr type) list) newmarked)))
4427
4428       (when delta-marks
4429         (unless (gnus-check-group gnus-newsgroup-name)
4430           (error "Can't open server for %s" gnus-newsgroup-name))
4431         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4432           
4433       ;; Enter these new marks into the info of the group.
4434       (if (nthcdr 3 info)
4435           (setcar (nthcdr 3 info) newmarked)
4436         ;; Add the marks lists to the end of the info.
4437         (when newmarked
4438           (setcdr (nthcdr 2 info) (list newmarked))))
4439
4440       ;; Cut off the end of the info if there's nothing else there.
4441       (let ((i 5))
4442         (while (and (> i 2)
4443                     (not (nth i info)))
4444           (when (nthcdr (decf i) info)
4445             (setcdr (nthcdr i info) nil)))))))
4446
4447 (defun gnus-set-mode-line (where)
4448   "This function sets the mode line of the article or summary buffers.
4449 If WHERE is `summary', the summary mode line format will be used."
4450   ;; Is this mode line one we keep updated?
4451   (when (and (memq where gnus-updated-mode-lines)
4452              (symbol-value
4453               (intern (format "gnus-%s-mode-line-format-spec" where))))
4454     (let (mode-string)
4455       (save-excursion
4456         ;; We evaluate this in the summary buffer since these
4457         ;; variables are buffer-local to that buffer.
4458         (set-buffer gnus-summary-buffer)
4459         ;; We bind all these variables that are used in the `eval' form
4460         ;; below.
4461         (let* ((mformat (symbol-value
4462                          (intern
4463                           (format "gnus-%s-mode-line-format-spec" where))))
4464                (gnus-tmp-group-name gnus-newsgroup-name)
4465                (gnus-tmp-article-number (or gnus-current-article 0))
4466                (gnus-tmp-unread gnus-newsgroup-unreads)
4467                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4468                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4469                (gnus-tmp-unread-and-unselected
4470                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4471                             (zerop gnus-tmp-unselected))
4472                        "")
4473                       ((zerop gnus-tmp-unselected)
4474                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4475                       (t (format "{%d(+%d) more}"
4476                                  gnus-tmp-unread-and-unticked
4477                                  gnus-tmp-unselected))))
4478                (gnus-tmp-subject
4479                 (if (and gnus-current-headers
4480                          (vectorp gnus-current-headers))
4481                     (gnus-mode-string-quote
4482                      (mail-header-subject gnus-current-headers))
4483                   ""))
4484                bufname-length max-len
4485                gnus-tmp-header);; passed as argument to any user-format-funcs
4486           (setq mode-string (eval mformat))
4487           (setq bufname-length (if (string-match "%b" mode-string)
4488                                    (- (length
4489                                        (buffer-name
4490                                         (if (eq where 'summary)
4491                                             nil
4492                                           (get-buffer gnus-article-buffer))))
4493                                       2)
4494                                  0))
4495           (setq max-len (max 4 (if gnus-mode-non-string-length
4496                                    (- (window-width)
4497                                       gnus-mode-non-string-length
4498                                       bufname-length)
4499                                  (length mode-string))))
4500           ;; We might have to chop a bit of the string off...
4501           (when (> (length mode-string) max-len)
4502             (setq mode-string
4503                   (concat (gnus-truncate-string mode-string (- max-len 3))
4504                           "...")))
4505           ;; Pad the mode string a bit.
4506           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4507       ;; Update the mode line.
4508       (setq mode-line-buffer-identification
4509             (gnus-mode-line-buffer-identification (list mode-string)))
4510       (set-buffer-modified-p t))))
4511
4512 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4513   "Go through the HEADERS list and add all Xrefs to a hash table.
4514 The resulting hash table is returned, or nil if no Xrefs were found."
4515   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4516          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4517          (xref-hashtb (gnus-make-hashtable))
4518          start group entry number xrefs header)
4519     (while headers
4520       (setq header (pop headers))
4521       (when (and (setq xrefs (mail-header-xref header))
4522                  (not (memq (setq number (mail-header-number header))
4523                             unreads)))
4524         (setq start 0)
4525         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4526           (setq start (match-end 0))
4527           (setq group (if prefix
4528                           (concat prefix (substring xrefs (match-beginning 1)
4529                                                     (match-end 1)))
4530                         (substring xrefs (match-beginning 1) (match-end 1))))
4531           (setq number
4532                 (string-to-int (substring xrefs (match-beginning 2)
4533                                           (match-end 2))))
4534           (if (setq entry (gnus-gethash group xref-hashtb))
4535               (setcdr entry (cons number (cdr entry)))
4536             (gnus-sethash group (cons number nil) xref-hashtb)))))
4537     (and start xref-hashtb)))
4538
4539 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4540   "Look through all the headers and mark the Xrefs as read."
4541   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4542         name entry info xref-hashtb idlist method nth4)
4543     (save-excursion
4544       (set-buffer gnus-group-buffer)
4545       (when (setq xref-hashtb
4546                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4547         (mapatoms
4548          (lambda (group)
4549            (unless (string= from-newsgroup (setq name (symbol-name group)))
4550              (setq idlist (symbol-value group))
4551              ;; Dead groups are not updated.
4552              (and (prog1
4553                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4554                             info (nth 2 entry))
4555                     (when (stringp (setq nth4 (gnus-info-method info)))
4556                       (setq nth4 (gnus-server-to-method nth4))))
4557                   ;; Only do the xrefs if the group has the same
4558                   ;; select method as the group we have just read.
4559                   (or (gnus-methods-equal-p
4560                        nth4 (gnus-find-method-for-group from-newsgroup))
4561                       virtual
4562                       (equal nth4 (setq method (gnus-find-method-for-group
4563                                                 from-newsgroup)))
4564                       (and (equal (car nth4) (car method))
4565                            (equal (nth 1 nth4) (nth 1 method))))
4566                   gnus-use-cross-reference
4567                   (or (not (eq gnus-use-cross-reference t))
4568                       virtual
4569                       ;; Only do cross-references on subscribed
4570                       ;; groups, if that is what is wanted.
4571                       (<= (gnus-info-level info) gnus-level-subscribed))
4572                   (gnus-group-make-articles-read name idlist))))
4573          xref-hashtb)))))
4574
4575 (defun gnus-compute-read-articles (group articles)
4576   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4577          (info (nth 2 entry))
4578          (active (gnus-active group))
4579          ninfo)
4580     (when entry
4581       ;; First peel off all invalid article numbers.
4582       (when active
4583         (let ((ids articles)
4584               id first)
4585           (while (setq id (pop ids))
4586             (when (and first (> id (cdr active)))
4587               ;; We'll end up in this situation in one particular
4588               ;; obscure situation.  If you re-scan a group and get
4589               ;; a new article that is cross-posted to a different
4590               ;; group that has not been re-scanned, you might get
4591               ;; crossposted article that has a higher number than
4592               ;; Gnus believes possible.  So we re-activate this
4593               ;; group as well.  This might mean doing the
4594               ;; crossposting thingy will *increase* the number
4595               ;; of articles in some groups.  Tsk, tsk.
4596               (setq active (or (gnus-activate-group group) active)))
4597             (when (or (> id (cdr active))
4598                       (< id (car active)))
4599               (setq articles (delq id articles))))))
4600       ;; If the read list is nil, we init it.
4601       (if (and active
4602                (null (gnus-info-read info))
4603                (> (car active) 1))
4604           (setq ninfo (cons 1 (1- (car active))))
4605         (setq ninfo (gnus-info-read info)))
4606       ;; Then we add the read articles to the range.
4607       (gnus-add-to-range
4608        ninfo (setq articles (sort articles '<))))))
4609
4610 (defun gnus-group-make-articles-read (group articles)
4611   "Update the info of GROUP to say that ARTICLES are read."
4612   (let* ((num 0)
4613          (entry (gnus-gethash group gnus-newsrc-hashtb))
4614          (info (nth 2 entry))
4615          (active (gnus-active group))
4616          range)
4617     (when entry
4618       (setq range (gnus-compute-read-articles group articles))
4619       (save-excursion
4620         (set-buffer gnus-group-buffer)
4621         (gnus-undo-register
4622           `(progn
4623              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4624              (gnus-info-set-read ',info ',(gnus-info-read info))
4625              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4626              (gnus-group-update-group ,group t))))
4627       ;; Add the read articles to the range.
4628       (gnus-info-set-read info range)
4629       ;; Then we have to re-compute how many unread
4630       ;; articles there are in this group.
4631       (when active
4632         (cond
4633          ((not range)
4634           (setq num (- (1+ (cdr active)) (car active))))
4635          ((not (listp (cdr range)))
4636           (setq num (- (cdr active) (- (1+ (cdr range))
4637                                        (car range)))))
4638          (t
4639           (while range
4640             (if (numberp (car range))
4641                 (setq num (1+ num))
4642               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4643             (setq range (cdr range)))
4644           (setq num (- (cdr active) num))))
4645         ;; Update the number of unread articles.
4646         (setcar entry num)
4647         ;; Update the group buffer.
4648         (gnus-group-update-group group t)))))
4649
4650 (defvar gnus-newsgroup-none-id 0)
4651
4652 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4653   (let ((cur nntp-server-buffer)
4654         (dependencies
4655          (or dependencies
4656              (save-excursion (set-buffer gnus-summary-buffer)
4657                              gnus-newsgroup-dependencies)))
4658         headers id end ref
4659         (mail-parse-charset gnus-newsgroup-charset)
4660         (mail-parse-ignored-charsets
4661          (save-excursion (condition-case nil
4662                              (set-buffer gnus-summary-buffer)
4663                            (error))
4664                          gnus-newsgroup-ignored-charsets)))
4665     (save-excursion
4666       (set-buffer nntp-server-buffer)
4667       ;; Translate all TAB characters into SPACE characters.
4668       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4669       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4670       (gnus-run-hooks 'gnus-parse-headers-hook)
4671       (let ((case-fold-search t)
4672             in-reply-to header p lines chars ctype)
4673         (goto-char (point-min))
4674         ;; Search to the beginning of the next header.  Error messages
4675         ;; do not begin with 2 or 3.
4676         (while (re-search-forward "^[23][0-9]+ " nil t)
4677           (setq id nil
4678                 ref nil)
4679           ;; This implementation of this function, with nine
4680           ;; search-forwards instead of the one re-search-forward and
4681           ;; a case (which basically was the old function) is actually
4682           ;; about twice as fast, even though it looks messier.  You
4683           ;; can't have everything, I guess.  Speed and elegance
4684           ;; doesn't always go hand in hand.
4685           (setq
4686            header
4687            (make-full-mail-header
4688             ;; Number.
4689             (prog1
4690                 (read cur)
4691               (end-of-line)
4692               (setq p (point))
4693               (narrow-to-region (point)
4694                                 (or (and (search-forward "\n.\n" nil t)
4695                                          (- (point) 2))
4696                                     (point))))
4697             ;; Subject.
4698             (progn
4699               (goto-char p)
4700               (if (search-forward "\nsubject: " nil t)
4701                   (buffer-substring (match-end 0) (std11-field-end))
4702                 "(none)"))
4703             ;; From.
4704             (progn
4705               (goto-char p)
4706               (if (search-forward "\nfrom: " nil t)
4707                   (buffer-substring (match-end 0) (std11-field-end))
4708                 "(nobody)"))
4709             ;; Date.
4710             (progn
4711               (goto-char p)
4712               (if (search-forward "\ndate: " nil t)
4713                   (buffer-substring (match-end 0) (std11-field-end))
4714                 ""))
4715             ;; Message-ID.
4716             (progn
4717               (goto-char p)
4718               (setq id (if (re-search-forward
4719                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4720                            ;; We do it this way to make sure the Message-ID
4721                            ;; is (somewhat) syntactically valid.
4722                            (buffer-substring (match-beginning 1)
4723                                              (match-end 1))
4724                          ;; If there was no message-id, we just fake one
4725                          ;; to make subsequent routines simpler.
4726                          (nnheader-generate-fake-message-id))))
4727             ;; References.
4728             (progn
4729               (goto-char p)
4730               (if (search-forward "\nreferences: " nil t)
4731                   (progn
4732                     (setq end (point))
4733                     (prog1
4734                         (buffer-substring (match-end 0) (std11-field-end))
4735                       (setq ref
4736                             (buffer-substring
4737                              (progn
4738                                ;; (end-of-line)
4739                                (search-backward ">" end t)
4740                                (1+ (point)))
4741                              (progn
4742                                (search-backward "<" end t)
4743                                (point))))))
4744                 ;; Get the references from the in-reply-to header if there
4745                 ;; were no references and the in-reply-to header looks
4746                 ;; promising.
4747                 (if (and (search-forward "\nin-reply-to: " nil t)
4748                          (setq in-reply-to
4749                                (buffer-substring (match-end 0)
4750                                                  (std11-field-end)))
4751                          (string-match "<[^>]+>" in-reply-to))
4752                     (let (ref2)
4753                       (setq ref (substring in-reply-to (match-beginning 0)
4754                                            (match-end 0)))
4755                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4756                         (setq ref2 (substring in-reply-to (match-beginning 0)
4757                                               (match-end 0)))
4758                         (when (> (length ref2) (length ref))
4759                           (setq ref ref2)))
4760                       ref)
4761                   (setq ref nil))))
4762             ;; Chars.
4763             (progn
4764               (goto-char p)
4765               (if (search-forward "\nchars: " nil t)
4766                   (if (numberp (setq chars (ignore-errors (read cur))))
4767                       chars 0)
4768                 0))
4769             ;; Lines.
4770             (progn
4771               (goto-char p)
4772               (if (search-forward "\nlines: " nil t)
4773                   (if (numberp (setq lines (ignore-errors (read cur))))
4774                       lines 0)
4775                 0))
4776             ;; Xref.
4777             (progn
4778               (goto-char p)
4779               (and (search-forward "\nxref: " nil t)
4780                    (buffer-substring (match-end 0) (std11-field-end))))
4781             ;; Extra.
4782             (when gnus-extra-headers
4783               (let ((extra gnus-extra-headers)
4784                     out)
4785                 (while extra
4786                   (goto-char p)
4787                   (when (search-forward
4788                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4789                     (push (cons (car extra)
4790                                 (buffer-substring (match-end 0)
4791                                                   (std11-field-end)))
4792                           out))
4793                   (pop extra))
4794                 out))))
4795           (goto-char p)
4796           (if (and (search-forward "\ncontent-type: " nil t)
4797                    (setq ctype
4798                          (buffer-substring (match-end 0) (std11-field-end))))
4799               (mime-entity-set-content-type-internal
4800                header (mime-parse-Content-Type ctype)))
4801           (when (equal id ref)
4802             (setq ref nil))
4803
4804           (when gnus-alter-header-function
4805             (funcall gnus-alter-header-function header)
4806             (setq id (mail-header-id header)
4807                   ref (gnus-parent-id (mail-header-references header))))
4808
4809           (when (setq header
4810                       (gnus-dependencies-add-header
4811                        header dependencies force-new))
4812             (push header headers))
4813           (goto-char (point-max))
4814           (widen))
4815         (nreverse headers)))))
4816
4817 ;; Goes through the xover lines and returns a list of vectors
4818 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4819                                                   force-new dependencies
4820                                                   group also-fetch-heads)
4821   "Parse the news overview data in the server buffer, and return a
4822 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4823   ;; Get the Xref when the users reads the articles since most/some
4824   ;; NNTP servers do not include Xrefs when using XOVER.
4825   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4826   (let ((mail-parse-charset gnus-newsgroup-charset)
4827         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4828         (cur nntp-server-buffer)
4829         (dependencies (or dependencies gnus-newsgroup-dependencies))
4830         number headers header)
4831     (save-excursion
4832       (set-buffer nntp-server-buffer)
4833       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4834       ;; Allow the user to mangle the headers before parsing them.
4835       (gnus-run-hooks 'gnus-parse-headers-hook)
4836       (goto-char (point-min))
4837       (while (not (eobp))
4838         (condition-case ()
4839             (while (and sequence (not (eobp)))
4840               (setq number (read cur))
4841               (while (and sequence
4842                           (< (car sequence) number))
4843                 (setq sequence (cdr sequence)))
4844               (and sequence
4845                    (eq number (car sequence))
4846                    (progn
4847                      (setq sequence (cdr sequence))
4848                      (setq header (inline
4849                                     (gnus-nov-parse-line
4850                                      number dependencies force-new))))
4851                    (push header headers))
4852               (forward-line 1))
4853           (error
4854            (gnus-error 4 "Strange nov line (%d)"
4855                        (count-lines (point-min) (point)))))
4856         (forward-line 1))
4857       ;; A common bug in inn is that if you have posted an article and
4858       ;; then retrieves the active file, it will answer correctly --
4859       ;; the new article is included.  However, a NOV entry for the
4860       ;; article may not have been generated yet, so this may fail.
4861       ;; We work around this problem by retrieving the last few
4862       ;; headers using HEAD.
4863       (if (or (not also-fetch-heads)
4864               (not sequence))
4865           ;; We (probably) got all the headers.
4866           (nreverse headers)
4867         (let ((gnus-nov-is-evil t))
4868           (nconc
4869            (nreverse headers)
4870            (gnus-retrieve-parsed-headers sequence group)
4871            ))))))
4872
4873 (defun gnus-article-get-xrefs ()
4874   "Fill in the Xref value in `gnus-current-headers', if necessary.
4875 This is meant to be called in `gnus-article-internal-prepare-hook'."
4876   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4877                                  gnus-current-headers)))
4878     (or (not gnus-use-cross-reference)
4879         (not headers)
4880         (and (mail-header-xref headers)
4881              (not (string= (mail-header-xref headers) "")))
4882         (let ((case-fold-search t)
4883               xref)
4884           (save-restriction
4885             (nnheader-narrow-to-headers)
4886             (goto-char (point-min))
4887             (when (or (and (not (eobp))
4888                            (eq (downcase (char-after)) ?x)
4889                            (looking-at "Xref:"))
4890                       (search-forward "\nXref:" nil t))
4891               (goto-char (1+ (match-end 0)))
4892               (setq xref (buffer-substring (point)
4893                                            (progn (end-of-line) (point))))
4894               (mail-header-set-xref headers xref)))))))
4895
4896 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4897   "Find article ID and insert the summary line for that article.
4898 OLD-HEADER can either be a header or a line number to insert
4899 the subject line on."
4900   (let* ((line (and (numberp old-header) old-header))
4901          (old-header (and (vectorp old-header) old-header))
4902          (header (cond ((and old-header use-old-header)
4903                         old-header)
4904                        ((and (numberp id)
4905                              (gnus-number-to-header id))
4906                         (gnus-number-to-header id))
4907                        (t
4908                         (gnus-read-header id))))
4909          (number (and (numberp id) id))
4910          d)
4911     (when header
4912       ;; Rebuild the thread that this article is part of and go to the
4913       ;; article we have fetched.
4914       (when (and (not gnus-show-threads)
4915                  old-header)
4916         (when (and number
4917                    (setq d (gnus-data-find (mail-header-number old-header))))
4918           (goto-char (gnus-data-pos d))
4919           (gnus-data-remove
4920            number
4921            (- (gnus-point-at-bol)
4922               (prog1
4923                   (1+ (gnus-point-at-eol))
4924                 (gnus-delete-line))))))
4925       (when old-header
4926         (mail-header-set-number header (mail-header-number old-header)))
4927       (setq gnus-newsgroup-sparse
4928             (delq (setq number (mail-header-number header))
4929                   gnus-newsgroup-sparse))
4930       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4931       (push number gnus-newsgroup-limit)
4932       (gnus-rebuild-thread (mail-header-id header) line)
4933       (gnus-summary-goto-subject number nil t))
4934     (when (and (numberp number)
4935                (> number 0))
4936       ;; We have to update the boundaries even if we can't fetch the
4937       ;; article if ID is a number -- so that the next `P' or `N'
4938       ;; command will fetch the previous (or next) article even
4939       ;; if the one we tried to fetch this time has been canceled.
4940       (when (> number gnus-newsgroup-end)
4941         (setq gnus-newsgroup-end number))
4942       (when (< number gnus-newsgroup-begin)
4943         (setq gnus-newsgroup-begin number))
4944       (setq gnus-newsgroup-unselected
4945             (delq number gnus-newsgroup-unselected)))
4946     ;; Report back a success?
4947     (and header (mail-header-number header))))
4948
4949 ;;; Process/prefix in the summary buffer
4950
4951 (defun gnus-summary-work-articles (n)
4952   "Return a list of articles to be worked upon.
4953 The prefix argument, the list of process marked articles, and the
4954 current article will be taken into consideration."
4955   (save-excursion
4956     (set-buffer gnus-summary-buffer)
4957     (cond
4958      (n
4959       ;; A numerical prefix has been given.
4960       (setq n (prefix-numeric-value n))
4961       (let ((backward (< n 0))
4962             (n (abs (prefix-numeric-value n)))
4963             articles article)
4964         (save-excursion
4965           (while
4966               (and (> n 0)
4967                    (push (setq article (gnus-summary-article-number))
4968                          articles)
4969                    (if backward
4970                        (gnus-summary-find-prev nil article)
4971                      (gnus-summary-find-next nil article)))
4972             (decf n)))
4973         (nreverse articles)))
4974      ((and (gnus-region-active-p) (mark))
4975       (message "region active")
4976       ;; Work on the region between point and mark.
4977       (let ((max (max (point) (mark)))
4978             articles article)
4979         (save-excursion
4980           (goto-char (min (point) (mark)))
4981           (while
4982               (and
4983                (push (setq article (gnus-summary-article-number)) articles)
4984                (gnus-summary-find-next nil article)
4985                (< (point) max)))
4986           (nreverse articles))))
4987      (gnus-newsgroup-processable
4988       ;; There are process-marked articles present.
4989       ;; Save current state.
4990       (gnus-summary-save-process-mark)
4991       ;; Return the list.
4992       (reverse gnus-newsgroup-processable))
4993      (t
4994       ;; Just return the current article.
4995       (list (gnus-summary-article-number))))))
4996
4997 (defmacro gnus-summary-iterate (arg &rest forms)
4998   "Iterate over the process/prefixed articles and do FORMS.
4999 ARG is the interactive prefix given to the command.  FORMS will be
5000 executed with point over the summary line of the articles."
5001   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5002     `(let ((,articles (gnus-summary-work-articles ,arg)))
5003        (while ,articles
5004          (gnus-summary-goto-subject (car ,articles))
5005          ,@forms
5006          (pop ,articles)))))
5007
5008 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5009 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5010
5011 (defun gnus-summary-save-process-mark ()
5012   "Push the current set of process marked articles on the stack."
5013   (interactive)
5014   (push (copy-sequence gnus-newsgroup-processable)
5015         gnus-newsgroup-process-stack))
5016
5017 (defun gnus-summary-kill-process-mark ()
5018   "Push the current set of process marked articles on the stack and unmark."
5019   (interactive)
5020   (gnus-summary-save-process-mark)
5021   (gnus-summary-unmark-all-processable))
5022
5023 (defun gnus-summary-yank-process-mark ()
5024   "Pop the last process mark state off the stack and restore it."
5025   (interactive)
5026   (unless gnus-newsgroup-process-stack
5027     (error "Empty mark stack"))
5028   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5029
5030 (defun gnus-summary-process-mark-set (set)
5031   "Make SET into the current process marked articles."
5032   (gnus-summary-unmark-all-processable)
5033   (while set
5034     (gnus-summary-set-process-mark (pop set))))
5035
5036 ;;; Searching and stuff
5037
5038 (defun gnus-summary-search-group (&optional backward use-level)
5039   "Search for next unread newsgroup.
5040 If optional argument BACKWARD is non-nil, search backward instead."
5041   (save-excursion
5042     (set-buffer gnus-group-buffer)
5043     (when (gnus-group-search-forward
5044            backward nil (if use-level (gnus-group-group-level) nil))
5045       (gnus-group-group-name))))
5046
5047 (defun gnus-summary-best-group (&optional exclude-group)
5048   "Find the name of the best unread group.
5049 If EXCLUDE-GROUP, do not go to this group."
5050   (save-excursion
5051     (set-buffer gnus-group-buffer)
5052     (save-excursion
5053       (gnus-group-best-unread-group exclude-group))))
5054
5055 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5056   (if backward (gnus-summary-find-prev)
5057     (let* ((dummy (gnus-summary-article-intangible-p))
5058            (article (or article (gnus-summary-article-number)))
5059            (arts (gnus-data-find-list article))
5060            result)
5061       (when (and (not dummy)
5062                  (or (not gnus-summary-check-current)
5063                      (not unread)
5064                      (not (gnus-data-unread-p (car arts)))))
5065         (setq arts (cdr arts)))
5066       (when (setq result
5067                   (if unread
5068                       (progn
5069                         (while arts
5070                           (when (or (and undownloaded
5071                                          (eq gnus-undownloaded-mark
5072                                              (gnus-data-mark (car arts))))
5073                                     (gnus-data-unread-p (car arts)))
5074                             (setq result (car arts)
5075                                   arts nil))
5076                           (setq arts (cdr arts)))
5077                         result)
5078                     (car arts)))
5079         (goto-char (gnus-data-pos result))
5080         (gnus-data-number result)))))
5081
5082 (defun gnus-summary-find-prev (&optional unread article)
5083   (let* ((eobp (eobp))
5084          (article (or article (gnus-summary-article-number)))
5085          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5086          result)
5087     (when (and (not eobp)
5088                (or (not gnus-summary-check-current)
5089                    (not unread)
5090                    (not (gnus-data-unread-p (car arts)))))
5091       (setq arts (cdr arts)))
5092     (when (setq result
5093                 (if unread
5094                     (progn
5095                       (while arts
5096                         (when (gnus-data-unread-p (car arts))
5097                           (setq result (car arts)
5098                                 arts nil))
5099                         (setq arts (cdr arts)))
5100                       result)
5101                   (car arts)))
5102       (goto-char (gnus-data-pos result))
5103       (gnus-data-number result))))
5104
5105 (defun gnus-summary-find-subject (subject &optional unread backward article)
5106   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5107          (article (or article (gnus-summary-article-number)))
5108          (articles (gnus-data-list backward))
5109          (arts (gnus-data-find-list article articles))
5110          result)
5111     (when (or (not gnus-summary-check-current)
5112               (not unread)
5113               (not (gnus-data-unread-p (car arts))))
5114       (setq arts (cdr arts)))
5115     (while arts
5116       (and (or (not unread)
5117                (gnus-data-unread-p (car arts)))
5118            (vectorp (gnus-data-header (car arts)))
5119            (gnus-subject-equal
5120             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5121            (setq result (car arts)
5122                  arts nil))
5123       (setq arts (cdr arts)))
5124     (and result
5125          (goto-char (gnus-data-pos result))
5126          (gnus-data-number result))))
5127
5128 (defun gnus-summary-search-forward (&optional unread subject backward)
5129   "Search forward for an article.
5130 If UNREAD, look for unread articles.  If SUBJECT, look for
5131 articles with that subject.  If BACKWARD, search backward instead."
5132   (cond (subject (gnus-summary-find-subject subject unread backward))
5133         (backward (gnus-summary-find-prev unread))
5134         (t (gnus-summary-find-next unread))))
5135
5136 (defun gnus-recenter (&optional n)
5137   "Center point in window and redisplay frame.
5138 Also do horizontal recentering."
5139   (interactive "P")
5140   (when (and gnus-auto-center-summary
5141              (not (eq gnus-auto-center-summary 'vertical)))
5142     (gnus-horizontal-recenter))
5143   (recenter n))
5144
5145 (defun gnus-summary-recenter ()
5146   "Center point in the summary window.
5147 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5148 displayed, no centering will be performed."
5149   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5150   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5151   (let* ((top (cond ((< (window-height) 4) 0)
5152                     ((< (window-height) 7) 1)
5153                     (t (if (numberp gnus-auto-center-summary)
5154                            gnus-auto-center-summary
5155                          2))))
5156          (height (1- (window-height)))
5157          (bottom (save-excursion (goto-char (point-max))
5158                                  (forward-line (- height))
5159                                  (point)))
5160          (window (get-buffer-window (current-buffer))))
5161     ;; The user has to want it.
5162     (when gnus-auto-center-summary
5163       (when (get-buffer-window gnus-article-buffer)
5164         ;; Only do recentering when the article buffer is displayed,
5165         ;; Set the window start to either `bottom', which is the biggest
5166         ;; possible valid number, or the second line from the top,
5167         ;; whichever is the least.
5168         (set-window-start
5169          window (min bottom (save-excursion
5170                               (forward-line (- top)) (point)))
5171          t))
5172       ;; Do horizontal recentering while we're at it.
5173       (when (and (get-buffer-window (current-buffer) t)
5174                  (not (eq gnus-auto-center-summary 'vertical)))
5175         (let ((selected (selected-window)))
5176           (select-window (get-buffer-window (current-buffer) t))
5177           (gnus-summary-position-point)
5178           (gnus-horizontal-recenter)
5179           (select-window selected))))))
5180
5181 (defun gnus-summary-jump-to-group (newsgroup)
5182   "Move point to NEWSGROUP in group mode buffer."
5183   ;; Keep update point of group mode buffer if visible.
5184   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5185       (save-window-excursion
5186         ;; Take care of tree window mode.
5187         (when (get-buffer-window gnus-group-buffer)
5188           (pop-to-buffer gnus-group-buffer))
5189         (gnus-group-jump-to-group newsgroup))
5190     (save-excursion
5191       ;; Take care of tree window mode.
5192       (if (get-buffer-window gnus-group-buffer)
5193           (pop-to-buffer gnus-group-buffer)
5194         (set-buffer gnus-group-buffer))
5195       (gnus-group-jump-to-group newsgroup))))
5196
5197 ;; This function returns a list of article numbers based on the
5198 ;; difference between the ranges of read articles in this group and
5199 ;; the range of active articles.
5200 (defun gnus-list-of-unread-articles (group)
5201   (let* ((read (gnus-info-read (gnus-get-info group)))
5202          (active (or (gnus-active group) (gnus-activate-group group)))
5203          (last (cdr active))
5204          first nlast unread)
5205     ;; If none are read, then all are unread.
5206     (if (not read)
5207         (setq first (car active))
5208       ;; If the range of read articles is a single range, then the
5209       ;; first unread article is the article after the last read
5210       ;; article.  Sounds logical, doesn't it?
5211       (if (and (not (listp (cdr read)))
5212                (or (< (car read) (car active))
5213                    (progn (setq read (list read))
5214                           nil)))
5215           (setq first (max (car active) (1+ (cdr read))))
5216         ;; `read' is a list of ranges.
5217         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5218                                   (caar read)))
5219                   1)
5220           (setq first (car active)))
5221         (while read
5222           (when first
5223             (while (< first nlast)
5224               (push first unread)
5225               (setq first (1+ first))))
5226           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5227           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5228           (setq read (cdr read)))))
5229     ;; And add the last unread articles.
5230     (while (<= first last)
5231       (push first unread)
5232       (setq first (1+ first)))
5233     ;; Return the list of unread articles.
5234     (delq 0 (nreverse unread))))
5235
5236 (defun gnus-list-of-read-articles (group)
5237   "Return a list of unread, unticked and non-dormant articles."
5238   (let* ((info (gnus-get-info group))
5239          (marked (gnus-info-marks info))
5240          (active (gnus-active group)))
5241     (and info active
5242          (gnus-set-difference
5243           (gnus-sorted-complement
5244            (gnus-uncompress-range active)
5245            (gnus-list-of-unread-articles group))
5246           (append
5247            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5248            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5249
5250 ;; Various summary commands
5251
5252 (defun gnus-summary-select-article-buffer ()
5253   "Reconfigure windows to show article buffer."
5254   (interactive)
5255   (if (not (gnus-buffer-live-p gnus-article-buffer))
5256       (error "There is no article buffer for this summary buffer")
5257     (gnus-configure-windows 'article)
5258     (select-window (get-buffer-window gnus-article-buffer))))
5259
5260 (defun gnus-summary-universal-argument (arg)
5261   "Perform any operation on all articles that are process/prefixed."
5262   (interactive "P")
5263   (let ((articles (gnus-summary-work-articles arg))
5264         func article)
5265     (if (eq
5266          (setq
5267           func
5268           (key-binding
5269            (read-key-sequence
5270             (substitute-command-keys
5271              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5272          'undefined)
5273         (gnus-error 1 "Undefined key")
5274       (save-excursion
5275         (while articles
5276           (gnus-summary-goto-subject (setq article (pop articles)))
5277           (let (gnus-newsgroup-processable)
5278             (command-execute func))
5279           (gnus-summary-remove-process-mark article)))))
5280   (gnus-summary-position-point))
5281
5282 (defun gnus-summary-toggle-truncation (&optional arg)
5283   "Toggle truncation of summary lines.
5284 With arg, turn line truncation on iff arg is positive."
5285   (interactive "P")
5286   (setq truncate-lines
5287         (if (null arg) (not truncate-lines)
5288           (> (prefix-numeric-value arg) 0)))
5289   (redraw-display))
5290
5291 (defun gnus-summary-reselect-current-group (&optional all rescan)
5292   "Exit and then reselect the current newsgroup.
5293 The prefix argument ALL means to select all articles."
5294   (interactive "P")
5295   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5296     (error "Ephemeral groups can't be reselected"))
5297   (let ((current-subject (gnus-summary-article-number))
5298         (group gnus-newsgroup-name))
5299     (setq gnus-newsgroup-begin nil)
5300     (gnus-summary-exit)
5301     ;; We have to adjust the point of group mode buffer because
5302     ;; point was moved to the next unread newsgroup by exiting.
5303     (gnus-summary-jump-to-group group)
5304     (when rescan
5305       (save-excursion
5306         (save-window-excursion
5307           ;; Don't show group contents.
5308           (set-window-start (selected-window) (point-max))
5309           (gnus-group-get-new-news-this-group 1))))
5310     (gnus-group-read-group all t)
5311     (gnus-summary-goto-subject current-subject nil t)))
5312
5313 (defun gnus-summary-rescan-group (&optional all)
5314   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5315   (interactive "P")
5316   (gnus-summary-reselect-current-group all t))
5317
5318 (defun gnus-summary-update-info (&optional non-destructive)
5319   (save-excursion
5320     (let ((group gnus-newsgroup-name))
5321       (when group
5322         (when gnus-newsgroup-kill-headers
5323           (setq gnus-newsgroup-killed
5324                 (gnus-compress-sequence
5325                  (nconc
5326                   (gnus-set-sorted-intersection
5327                    (gnus-uncompress-range gnus-newsgroup-killed)
5328                    (setq gnus-newsgroup-unselected
5329                          (sort gnus-newsgroup-unselected '<)))
5330                   (setq gnus-newsgroup-unreads
5331                         (sort gnus-newsgroup-unreads '<)))
5332                  t)))
5333         (unless (listp (cdr gnus-newsgroup-killed))
5334           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5335         (let ((headers gnus-newsgroup-headers))
5336           ;; Set the new ranges of read articles.
5337           (save-excursion
5338             (set-buffer gnus-group-buffer)
5339             (gnus-undo-force-boundary))
5340           (gnus-update-read-articles
5341            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5342           ;; Set the current article marks.
5343           (let ((gnus-newsgroup-scored
5344                  (if (and (not gnus-save-score)
5345                           (not non-destructive))
5346                      nil
5347                    gnus-newsgroup-scored)))
5348             (save-excursion
5349               (gnus-update-marks)))
5350           ;; Do the cross-ref thing.
5351           (when gnus-use-cross-reference
5352             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5353           ;; Do not switch windows but change the buffer to work.
5354           (set-buffer gnus-group-buffer)
5355           (unless (gnus-ephemeral-group-p group)
5356             (gnus-group-update-group group)))))))
5357
5358 (defun gnus-summary-save-newsrc (&optional force)
5359   "Save the current number of read/marked articles in the dribble buffer.
5360 The dribble buffer will then be saved.
5361 If FORCE (the prefix), also save the .newsrc file(s)."
5362   (interactive "P")
5363   (gnus-summary-update-info t)
5364   (if force
5365       (gnus-save-newsrc-file)
5366     (gnus-dribble-save)))
5367
5368 (defun gnus-summary-exit (&optional temporary)
5369   "Exit reading current newsgroup, and then return to group selection mode.
5370 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5371   (interactive)
5372   (gnus-set-global-variables)
5373   (gnus-kill-save-kill-buffer)
5374   (gnus-async-halt-prefetch)
5375   (let* ((group gnus-newsgroup-name)
5376          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5377          (mode major-mode)
5378          (group-point nil)
5379          (buf (current-buffer)))
5380     (unless quit-config
5381       ;; Do adaptive scoring, and possibly save score files.
5382       (when gnus-newsgroup-adaptive
5383         (gnus-score-adaptive))
5384       (when gnus-use-scoring
5385         (gnus-score-save)))
5386     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5387     ;; If we have several article buffers, we kill them at exit.
5388     (unless gnus-single-article-buffer
5389       (gnus-kill-buffer gnus-original-article-buffer)
5390       (setq gnus-article-current nil))
5391     (when gnus-use-cache
5392       (gnus-cache-possibly-remove-articles)
5393       (gnus-cache-save-buffers))
5394     (gnus-async-prefetch-remove-group group)
5395     (when gnus-suppress-duplicates
5396       (gnus-dup-enter-articles))
5397     (when gnus-use-trees
5398       (gnus-tree-close group))
5399     (when gnus-use-cache
5400       (gnus-cache-write-active))
5401     ;; Remove entries for this group.
5402     (nnmail-purge-split-history (gnus-group-real-name group))
5403     ;; Make all changes in this group permanent.
5404     (unless quit-config
5405       (gnus-run-hooks 'gnus-exit-group-hook)
5406       (gnus-summary-update-info))
5407     (gnus-close-group group)
5408     ;; Make sure where we were, and go to next newsgroup.
5409     (set-buffer gnus-group-buffer)
5410     (unless quit-config
5411       (gnus-group-jump-to-group group))
5412     (gnus-run-hooks 'gnus-summary-exit-hook)
5413     (unless (or quit-config
5414                 ;; If this group has disappeared from the summary
5415                 ;; buffer, don't skip forwards.
5416                 (not (string= group (gnus-group-group-name))))
5417       (gnus-group-next-unread-group 1))
5418     (setq group-point (point))
5419     (if temporary
5420         nil                             ;Nothing to do.
5421       ;; If we have several article buffers, we kill them at exit.
5422       (unless gnus-single-article-buffer
5423         (gnus-kill-buffer gnus-article-buffer)
5424         (gnus-kill-buffer gnus-original-article-buffer)
5425         (setq gnus-article-current nil))
5426       (set-buffer buf)
5427       (if (not gnus-kill-summary-on-exit)
5428           (gnus-deaden-summary)
5429         ;; We set all buffer-local variables to nil.  It is unclear why
5430         ;; this is needed, but if we don't, buffer-local variables are
5431         ;; not garbage-collected, it seems.  This would the lead to en
5432         ;; ever-growing Emacs.
5433         (gnus-summary-clear-local-variables)
5434         (when (get-buffer gnus-article-buffer)
5435           (bury-buffer gnus-article-buffer))
5436         ;; We clear the global counterparts of the buffer-local
5437         ;; variables as well, just to be on the safe side.
5438         (set-buffer gnus-group-buffer)
5439         (gnus-summary-clear-local-variables)
5440         ;; Return to group mode buffer.
5441         (when (eq mode 'gnus-summary-mode)
5442           (gnus-kill-buffer buf)))
5443       (setq gnus-current-select-method gnus-select-method)
5444       (pop-to-buffer gnus-group-buffer)
5445       (if (not quit-config)
5446           (progn
5447             (goto-char group-point)
5448             (gnus-configure-windows 'group 'force)
5449             (unless (pos-visible-in-window-p)
5450               (forward-line (/ (static-if (featurep 'xemacs)
5451                                    (window-displayed-height)
5452                                  (1- (window-height)))
5453                                -2))
5454               (set-window-start (selected-window) (point))
5455               (goto-char group-point)))
5456         (gnus-handle-ephemeral-exit quit-config))
5457       ;; Clear the current group name.
5458       (unless quit-config
5459         (setq gnus-newsgroup-name nil)))))
5460
5461 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5462 (defun gnus-summary-exit-no-update (&optional no-questions)
5463   "Quit reading current newsgroup without updating read article info."
5464   (interactive)
5465   (let* ((group gnus-newsgroup-name)
5466          (quit-config (gnus-group-quit-config group)))
5467     (when (or no-questions
5468               gnus-expert-user
5469               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5470       (gnus-async-halt-prefetch)
5471       (mapcar 'funcall
5472               (delq 'gnus-summary-expire-articles
5473                     (copy-sequence gnus-summary-prepare-exit-hook)))
5474       ;; If we have several article buffers, we kill them at exit.
5475       (unless gnus-single-article-buffer
5476         (gnus-kill-buffer gnus-article-buffer)
5477         (gnus-kill-buffer gnus-original-article-buffer)
5478         (setq gnus-article-current nil))
5479       (if (not gnus-kill-summary-on-exit)
5480           (gnus-deaden-summary)
5481         (gnus-close-group group)
5482         (gnus-summary-clear-local-variables)
5483         (set-buffer gnus-group-buffer)
5484         (gnus-summary-clear-local-variables)
5485         (when (get-buffer gnus-summary-buffer)
5486           (kill-buffer gnus-summary-buffer)))
5487       (unless gnus-single-article-buffer
5488         (setq gnus-article-current nil))
5489       (when gnus-use-trees
5490         (gnus-tree-close group))
5491       (gnus-async-prefetch-remove-group group)
5492       (when (get-buffer gnus-article-buffer)
5493         (bury-buffer gnus-article-buffer))
5494       ;; Return to the group buffer.
5495       (gnus-configure-windows 'group 'force)
5496       ;; Clear the current group name.
5497       (setq gnus-newsgroup-name nil)
5498       (when (equal (gnus-group-group-name) group)
5499         (gnus-group-next-unread-group 1))
5500       (when quit-config
5501         (gnus-handle-ephemeral-exit quit-config)))))
5502
5503 (defun gnus-handle-ephemeral-exit (quit-config)
5504   "Handle movement when leaving an ephemeral group.
5505 The state which existed when entering the ephemeral is reset."
5506   (if (not (buffer-name (car quit-config)))
5507       (gnus-configure-windows 'group 'force)
5508     (set-buffer (car quit-config))
5509     (cond ((eq major-mode 'gnus-summary-mode)
5510            (gnus-set-global-variables))
5511           ((eq major-mode 'gnus-article-mode)
5512            (save-excursion
5513              ;; The `gnus-summary-buffer' variable may point
5514              ;; to the old summary buffer when using a single
5515              ;; article buffer.
5516              (unless (gnus-buffer-live-p gnus-summary-buffer)
5517                (set-buffer gnus-group-buffer))
5518              (set-buffer gnus-summary-buffer)
5519              (gnus-set-global-variables))))
5520     (if (or (eq (cdr quit-config) 'article)
5521             (eq (cdr quit-config) 'pick))
5522         (progn
5523           ;; The current article may be from the ephemeral group
5524           ;; thus it is best that we reload this article
5525           (gnus-summary-show-article)
5526           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5527               (gnus-configure-windows 'pick 'force)
5528             (gnus-configure-windows (cdr quit-config) 'force)))
5529       (gnus-configure-windows (cdr quit-config) 'force))
5530     (when (eq major-mode 'gnus-summary-mode)
5531       (gnus-summary-next-subject 1 nil t)
5532       (gnus-summary-recenter)
5533       (gnus-summary-position-point))))
5534
5535 (defun gnus-summary-preview-mime-message ()
5536   "MIME decode and play this message."
5537   (interactive)
5538   (let ((gnus-break-pages nil)
5539         (gnus-show-mime t))
5540     (gnus-summary-select-article gnus-show-all-headers t))
5541   (select-window (get-buffer-window gnus-article-buffer)))
5542
5543 ;;; Dead summaries.
5544
5545 (defvar gnus-dead-summary-mode-map nil)
5546
5547 (unless gnus-dead-summary-mode-map
5548   (setq gnus-dead-summary-mode-map (make-keymap))
5549   (suppress-keymap gnus-dead-summary-mode-map)
5550   (substitute-key-definition
5551    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5552   (let ((keys '("\C-d" "\r" "\177" [delete])))
5553     (while keys
5554       (define-key gnus-dead-summary-mode-map
5555         (pop keys) 'gnus-summary-wake-up-the-dead))))
5556
5557 (defvar gnus-dead-summary-mode nil
5558   "Minor mode for Gnus summary buffers.")
5559
5560 (defun gnus-dead-summary-mode (&optional arg)
5561   "Minor mode for Gnus summary buffers."
5562   (interactive "P")
5563   (when (eq major-mode 'gnus-summary-mode)
5564     (make-local-variable 'gnus-dead-summary-mode)
5565     (setq gnus-dead-summary-mode
5566           (if (null arg) (not gnus-dead-summary-mode)
5567             (> (prefix-numeric-value arg) 0)))
5568     (when gnus-dead-summary-mode
5569       (gnus-add-minor-mode
5570        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5571
5572 (defun gnus-deaden-summary ()
5573   "Make the current summary buffer into a dead summary buffer."
5574   ;; Kill any previous dead summary buffer.
5575   (when (and gnus-dead-summary
5576              (buffer-name gnus-dead-summary))
5577     (save-excursion
5578       (set-buffer gnus-dead-summary)
5579       (when gnus-dead-summary-mode
5580         (kill-buffer (current-buffer)))))
5581   ;; Make this the current dead summary.
5582   (setq gnus-dead-summary (current-buffer))
5583   (gnus-dead-summary-mode 1)
5584   (let ((name (buffer-name)))
5585     (when (string-match "Summary" name)
5586       (rename-buffer
5587        (concat (substring name 0 (match-beginning 0)) "Dead "
5588                (substring name (match-beginning 0)))
5589        t))))
5590
5591 (defun gnus-kill-or-deaden-summary (buffer)
5592   "Kill or deaden the summary BUFFER."
5593   (save-excursion
5594     (when (and (buffer-name buffer)
5595                (not gnus-single-article-buffer))
5596       (save-excursion
5597         (set-buffer buffer)
5598         (gnus-kill-buffer gnus-article-buffer)
5599         (gnus-kill-buffer gnus-original-article-buffer)))
5600     (cond (gnus-kill-summary-on-exit
5601            (when (and gnus-use-trees
5602                       (gnus-buffer-exists-p buffer))
5603              (save-excursion
5604                (set-buffer buffer)
5605                (gnus-tree-close gnus-newsgroup-name)))
5606            (gnus-kill-buffer buffer))
5607           ((gnus-buffer-exists-p buffer)
5608            (save-excursion
5609              (set-buffer buffer)
5610              (gnus-deaden-summary))))))
5611
5612 (defun gnus-summary-wake-up-the-dead (&rest args)
5613   "Wake up the dead summary buffer."
5614   (interactive)
5615   (gnus-dead-summary-mode -1)
5616   (let ((name (buffer-name)))
5617     (when (string-match "Dead " name)
5618       (rename-buffer
5619        (concat (substring name 0 (match-beginning 0))
5620                (substring name (match-end 0)))
5621        t)))
5622   (gnus-message 3 "This dead summary is now alive again"))
5623
5624 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5625 (defun gnus-summary-fetch-faq (&optional faq-dir)
5626   "Fetch the FAQ for the current group.
5627 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5628 in."
5629   (interactive
5630    (list
5631     (when current-prefix-arg
5632       (completing-read
5633        "Faq dir: " (and (listp gnus-group-faq-directory)
5634                         (mapcar (lambda (file) (list file))
5635                                 gnus-group-faq-directory))))))
5636   (let (gnus-faq-buffer)
5637     (when (setq gnus-faq-buffer
5638                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5639       (gnus-configure-windows 'summary-faq))))
5640
5641 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5642 (defun gnus-summary-describe-group (&optional force)
5643   "Describe the current newsgroup."
5644   (interactive "P")
5645   (gnus-group-describe-group force gnus-newsgroup-name))
5646
5647 (defun gnus-summary-describe-briefly ()
5648   "Describe summary mode commands briefly."
5649   (interactive)
5650   (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")))
5651
5652 ;; Walking around group mode buffer from summary mode.
5653
5654 (defun gnus-summary-next-group (&optional no-article target-group backward)
5655   "Exit current newsgroup and then select next unread newsgroup.
5656 If prefix argument NO-ARTICLE is non-nil, no article is selected
5657 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5658 previous group instead."
5659   (interactive "P")
5660   ;; Stop pre-fetching.
5661   (gnus-async-halt-prefetch)
5662   (let ((current-group gnus-newsgroup-name)
5663         (current-buffer (current-buffer))
5664         entered)
5665     ;; First we semi-exit this group to update Xrefs and all variables.
5666     ;; We can't do a real exit, because the window conf must remain
5667     ;; the same in case the user is prompted for info, and we don't
5668     ;; want the window conf to change before that...
5669     (gnus-summary-exit t)
5670     (while (not entered)
5671       ;; Then we find what group we are supposed to enter.
5672       (set-buffer gnus-group-buffer)
5673       (gnus-group-jump-to-group current-group)
5674       (setq target-group
5675             (or target-group
5676                 (if (eq gnus-keep-same-level 'best)
5677                     (gnus-summary-best-group gnus-newsgroup-name)
5678                   (gnus-summary-search-group backward gnus-keep-same-level))))
5679       (if (not target-group)
5680           ;; There are no further groups, so we return to the group
5681           ;; buffer.
5682           (progn
5683             (gnus-message 5 "Returning to the group buffer")
5684             (setq entered t)
5685             (when (gnus-buffer-live-p current-buffer)
5686               (set-buffer current-buffer)
5687               (gnus-summary-exit))
5688             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5689         ;; We try to enter the target group.
5690         (gnus-group-jump-to-group target-group)
5691         (let ((unreads (gnus-group-group-unread)))
5692           (if (and (or (eq t unreads)
5693                        (and unreads (not (zerop unreads))))
5694                    (gnus-summary-read-group
5695                     target-group nil no-article
5696                     (and (buffer-name current-buffer) current-buffer)
5697                     nil backward))
5698               (setq entered t)
5699             (setq current-group target-group
5700                   target-group nil)))))))
5701
5702 (defun gnus-summary-prev-group (&optional no-article)
5703   "Exit current newsgroup and then select previous unread newsgroup.
5704 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5705   (interactive "P")
5706   (gnus-summary-next-group no-article nil t))
5707
5708 ;; Walking around summary lines.
5709
5710 (defun gnus-summary-first-subject (&optional unread undownloaded)
5711   "Go to the first unread subject.
5712 If UNREAD is non-nil, go to the first unread article.
5713 Returns the article selected or nil if there are no unread articles."
5714   (interactive "P")
5715   (prog1
5716       (cond
5717        ;; Empty summary.
5718        ((null gnus-newsgroup-data)
5719         (gnus-message 3 "No articles in the group")
5720         nil)
5721        ;; Pick the first article.
5722        ((not unread)
5723         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5724         (gnus-data-number (car gnus-newsgroup-data)))
5725        ;; No unread articles.
5726        ((null gnus-newsgroup-unreads)
5727         (gnus-message 3 "No more unread articles")
5728         nil)
5729        ;; Find the first unread article.
5730        (t
5731         (let ((data gnus-newsgroup-data))
5732           (while (and data
5733                       (and (not (and undownloaded
5734                                      (eq gnus-undownloaded-mark
5735                                          (gnus-data-mark (car data)))))
5736                            (not (gnus-data-unread-p (car data)))))
5737             (setq data (cdr data)))
5738           (when data
5739             (goto-char (gnus-data-pos (car data)))
5740             (gnus-data-number (car data))))))
5741     (gnus-summary-position-point)))
5742
5743 (defun gnus-summary-next-subject (n &optional unread dont-display)
5744   "Go to next N'th summary line.
5745 If N is negative, go to the previous N'th subject line.
5746 If UNREAD is non-nil, only unread articles are selected.
5747 The difference between N and the actual number of steps taken is
5748 returned."
5749   (interactive "p")
5750   (let ((backward (< n 0))
5751         (n (abs n)))
5752     (while (and (> n 0)
5753                 (if backward
5754                     (gnus-summary-find-prev unread)
5755                   (gnus-summary-find-next unread)))
5756       (unless (zerop (setq n (1- n)))
5757         (gnus-summary-show-thread)))
5758     (when (/= 0 n)
5759       (gnus-message 7 "No more%s articles"
5760                     (if unread " unread" "")))
5761     (unless dont-display
5762       (gnus-summary-recenter)
5763       (gnus-summary-position-point))
5764     n))
5765
5766 (defun gnus-summary-next-unread-subject (n)
5767   "Go to next N'th unread summary line."
5768   (interactive "p")
5769   (gnus-summary-next-subject n t))
5770
5771 (defun gnus-summary-prev-subject (n &optional unread)
5772   "Go to previous N'th summary line.
5773 If optional argument UNREAD is non-nil, only unread article is selected."
5774   (interactive "p")
5775   (gnus-summary-next-subject (- n) unread))
5776
5777 (defun gnus-summary-prev-unread-subject (n)
5778   "Go to previous N'th unread summary line."
5779   (interactive "p")
5780   (gnus-summary-next-subject (- n) t))
5781
5782 (defun gnus-summary-goto-subject (article &optional force silent)
5783   "Go the subject line of ARTICLE.
5784 If FORCE, also allow jumping to articles not currently shown."
5785   (interactive "nArticle number: ")
5786   (let ((b (point))
5787         (data (gnus-data-find article)))
5788     ;; We read in the article if we have to.
5789     (and (not data)
5790          force
5791          (gnus-summary-insert-subject
5792           article
5793           (if (or (numberp force) (vectorp force)) force)
5794           t)
5795          (setq data (gnus-data-find article)))
5796     (goto-char b)
5797     (if (not data)
5798         (progn
5799           (unless silent
5800             (gnus-message 3 "Can't find article %d" article))
5801           nil)
5802       (goto-char (gnus-data-pos data))
5803       (gnus-summary-position-point)
5804       article)))
5805
5806 ;; Walking around summary lines with displaying articles.
5807
5808 (defun gnus-summary-expand-window (&optional arg)
5809   "Make the summary buffer take up the entire Emacs frame.
5810 Given a prefix, will force an `article' buffer configuration."
5811   (interactive "P")
5812   (if arg
5813       (gnus-configure-windows 'article 'force)
5814     (gnus-configure-windows 'summary 'force)))
5815
5816 (defun gnus-summary-display-article (article &optional all-header)
5817   "Display ARTICLE in article buffer."
5818   (gnus-set-global-variables)
5819   (if (null article)
5820       nil
5821     (prog1
5822         (if gnus-summary-display-article-function
5823             (funcall gnus-summary-display-article-function article all-header)
5824           (gnus-article-prepare article all-header))
5825       (with-current-buffer gnus-article-buffer
5826         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5827              nil))
5828       (gnus-run-hooks 'gnus-select-article-hook)
5829       (when (and gnus-current-article
5830                  (not (zerop gnus-current-article)))
5831         (gnus-summary-goto-subject gnus-current-article))
5832       (gnus-summary-recenter)
5833       (when (and gnus-use-trees gnus-show-threads)
5834         (gnus-possibly-generate-tree article)
5835         (gnus-highlight-selected-tree article))
5836       ;; Successfully display article.
5837       (gnus-article-set-window-start
5838        (cdr (assq article gnus-newsgroup-bookmarks))))))
5839
5840 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5841   "Select the current article.
5842 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5843 non-nil, the article will be re-fetched even if it already present in
5844 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5845 be displayed."
5846   ;; Make sure we are in the summary buffer to work around bbdb bug.
5847   (unless (eq major-mode 'gnus-summary-mode)
5848     (set-buffer gnus-summary-buffer))
5849   (let ((article (or article (gnus-summary-article-number)))
5850         (all-headers (not (not all-headers))) ;Must be T or NIL.
5851         gnus-summary-display-article-function
5852         did)
5853     (and (not pseudo)
5854          (gnus-summary-article-pseudo-p article)
5855          (error "This is a pseudo-article"))
5856     (prog1
5857         (save-excursion
5858           (set-buffer gnus-summary-buffer)
5859           (if (or (and gnus-single-article-buffer
5860                        (or (null gnus-current-article)
5861                            (null gnus-article-current)
5862                            (null (get-buffer gnus-article-buffer))
5863                            (not (eq article (cdr gnus-article-current)))
5864                            (not (equal (car gnus-article-current)
5865                                        gnus-newsgroup-name))))
5866                   (and (not gnus-single-article-buffer)
5867                        (or (null gnus-current-article)
5868                            (not (eq gnus-current-article article))))
5869                   force)
5870               ;; The requested article is different from the current article.
5871               (prog1
5872                   (gnus-summary-display-article article all-headers)
5873                 (setq did article)
5874                 (when (or all-headers gnus-show-all-headers)
5875                   (if (eq 'gnus-summary-toggle-mime this-command)
5876                       (gnus-article-show-all)
5877                     (gnus-article-show-all-headers))))
5878             (when (or all-headers gnus-show-all-headers)
5879               (gnus-article-show-all-headers))
5880             'old))
5881       (when did
5882         (gnus-article-set-window-start
5883          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5884
5885 (defun gnus-summary-set-current-mark (&optional current-mark)
5886   "Obsolete function."
5887   nil)
5888
5889 (defun gnus-summary-next-article (&optional unread subject backward push)
5890   "Select the next article.
5891 If UNREAD, only unread articles are selected.
5892 If SUBJECT, only articles with SUBJECT are selected.
5893 If BACKWARD, the previous article is selected instead of the next."
5894   (interactive "P")
5895   (cond
5896    ;; Is there such an article?
5897    ((and (gnus-summary-search-forward unread subject backward)
5898          (or (gnus-summary-display-article (gnus-summary-article-number))
5899              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5900     (gnus-summary-position-point))
5901    ;; If not, we try the first unread, if that is wanted.
5902    ((and subject
5903          gnus-auto-select-same
5904          (gnus-summary-first-unread-article))
5905     (gnus-summary-position-point)
5906     (gnus-message 6 "Wrapped"))
5907    ;; Try to get next/previous article not displayed in this group.
5908    ((and gnus-auto-extend-newsgroup
5909          (not unread) (not subject))
5910     (gnus-summary-goto-article
5911      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5912      nil (count-lines (point-min) (point))))
5913    ;; Go to next/previous group.
5914    (t
5915     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5916       (gnus-summary-jump-to-group gnus-newsgroup-name))
5917     (let ((cmd last-command-char)
5918           (point
5919            (save-excursion
5920              (set-buffer gnus-group-buffer)
5921              (point)))
5922           (group
5923            (if (eq gnus-keep-same-level 'best)
5924                (gnus-summary-best-group gnus-newsgroup-name)
5925              (gnus-summary-search-group backward gnus-keep-same-level))))
5926       ;; For some reason, the group window gets selected.  We change
5927       ;; it back.
5928       (select-window (get-buffer-window (current-buffer)))
5929       ;; Select next unread newsgroup automagically.
5930       (cond
5931        ((or (not gnus-auto-select-next)
5932             (not cmd))
5933         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5934        ((or (eq gnus-auto-select-next 'quietly)
5935             (and (eq gnus-auto-select-next 'slightly-quietly)
5936                  push)
5937             (and (eq gnus-auto-select-next 'almost-quietly)
5938                  (gnus-summary-last-article-p)))
5939         ;; Select quietly.
5940         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5941             (gnus-summary-exit)
5942           (gnus-message 7 "No more%s articles (%s)..."
5943                         (if unread " unread" "")
5944                         (if group (concat "selecting " group)
5945                           "exiting"))
5946           (gnus-summary-next-group nil group backward)))
5947        (t
5948         (when (gnus-key-press-event-p last-input-event)
5949           (gnus-summary-walk-group-buffer
5950            gnus-newsgroup-name cmd unread backward point))))))))
5951
5952 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5953   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5954                       (?\C-p (gnus-group-prev-unread-group 1))))
5955         (cursor-in-echo-area t)
5956         keve key group ended)
5957     (save-excursion
5958       (set-buffer gnus-group-buffer)
5959       (goto-char start)
5960       (setq group
5961             (if (eq gnus-keep-same-level 'best)
5962                 (gnus-summary-best-group gnus-newsgroup-name)
5963               (gnus-summary-search-group backward gnus-keep-same-level))))
5964     (while (not ended)
5965       (gnus-message
5966        5 "No more%s articles%s" (if unread " unread" "")
5967        (if (and group
5968                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5969            (format " (Type %s for %s [%s])"
5970                    (single-key-description cmd) group
5971                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5972          (format " (Type %s to exit %s)"
5973                  (single-key-description cmd)
5974                  gnus-newsgroup-name)))
5975       ;; Confirm auto selection.
5976       (setq key (car (setq keve (gnus-read-event-char))))
5977       (setq ended t)
5978       (cond
5979        ((assq key keystrokes)
5980         (let ((obuf (current-buffer)))
5981           (switch-to-buffer gnus-group-buffer)
5982           (when group
5983             (gnus-group-jump-to-group group))
5984           (eval (cadr (assq key keystrokes)))
5985           (setq group (gnus-group-group-name))
5986           (switch-to-buffer obuf))
5987         (setq ended nil))
5988        ((equal key cmd)
5989         (if (or (not group)
5990                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5991             (gnus-summary-exit)
5992           (gnus-summary-next-group nil group backward)))
5993        (t
5994         (push (cdr keve) unread-command-events))))))
5995
5996 (defun gnus-summary-next-unread-article ()
5997   "Select unread article after current one."
5998   (interactive)
5999   (gnus-summary-next-article
6000    (or (not (eq gnus-summary-goto-unread 'never))
6001        (gnus-summary-last-article-p (gnus-summary-article-number)))
6002    (and gnus-auto-select-same
6003         (gnus-summary-article-subject))))
6004
6005 (defun gnus-summary-prev-article (&optional unread subject)
6006   "Select the article after the current one.
6007 If UNREAD is non-nil, only unread articles are selected."
6008   (interactive "P")
6009   (gnus-summary-next-article unread subject t))
6010
6011 (defun gnus-summary-prev-unread-article ()
6012   "Select unread article before current one."
6013   (interactive)
6014   (gnus-summary-prev-article
6015    (or (not (eq gnus-summary-goto-unread 'never))
6016        (gnus-summary-first-article-p (gnus-summary-article-number)))
6017    (and gnus-auto-select-same
6018         (gnus-summary-article-subject))))
6019
6020 (defun gnus-summary-next-page (&optional lines circular)
6021   "Show next page of the selected article.
6022 If at the end of the current article, select the next article.
6023 LINES says how many lines should be scrolled up.
6024
6025 If CIRCULAR is non-nil, go to the start of the article instead of
6026 selecting the next article when reaching the end of the current
6027 article."
6028   (interactive "P")
6029   (setq gnus-summary-buffer (current-buffer))
6030   (gnus-set-global-variables)
6031   (let ((article (gnus-summary-article-number))
6032         (article-window (get-buffer-window gnus-article-buffer t))
6033         endp)
6034     ;; If the buffer is empty, we have no article.
6035     (unless article
6036       (error "No article to select"))
6037     (gnus-configure-windows 'article)
6038     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6039         (if (and (eq gnus-summary-goto-unread 'never)
6040                  (not (gnus-summary-last-article-p article)))
6041             (gnus-summary-next-article)
6042           (gnus-summary-next-unread-article))
6043       (if (or (null gnus-current-article)
6044               (null gnus-article-current)
6045               (/= article (cdr gnus-article-current))
6046               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6047           ;; Selected subject is different from current article's.
6048           (gnus-summary-display-article article)
6049         (when article-window
6050           (gnus-eval-in-buffer-window gnus-article-buffer
6051             (setq endp (gnus-article-next-page lines)))
6052           (when endp
6053             (cond (circular
6054                    (gnus-summary-beginning-of-article))
6055                   (lines
6056                    (gnus-message 3 "End of message"))
6057                   ((null lines)
6058                    (if (and (eq gnus-summary-goto-unread 'never)
6059                             (not (gnus-summary-last-article-p article)))
6060                        (gnus-summary-next-article)
6061                      (gnus-summary-next-unread-article))))))))
6062     (gnus-summary-recenter)
6063     (gnus-summary-position-point)))
6064
6065 (defun gnus-summary-prev-page (&optional lines move)
6066   "Show previous page of selected article.
6067 Argument LINES specifies lines to be scrolled down.
6068 If MOVE, move to the previous unread article if point is at
6069 the beginning of the buffer."
6070   (interactive "P")
6071   (let ((article (gnus-summary-article-number))
6072         (article-window (get-buffer-window gnus-article-buffer t))
6073         endp)
6074     (gnus-configure-windows 'article)
6075     (if (or (null gnus-current-article)
6076             (null gnus-article-current)
6077             (/= article (cdr gnus-article-current))
6078             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6079         ;; Selected subject is different from current article's.
6080         (gnus-summary-display-article article)
6081       (gnus-summary-recenter)
6082       (when article-window
6083         (gnus-eval-in-buffer-window gnus-article-buffer
6084           (setq endp (gnus-article-prev-page lines)))
6085         (when (and move endp)
6086           (cond (lines
6087                  (gnus-message 3 "Beginning of message"))
6088                 ((null lines)
6089                  (if (and (eq gnus-summary-goto-unread 'never)
6090                           (not (gnus-summary-first-article-p article)))
6091                      (gnus-summary-prev-article)
6092                    (gnus-summary-prev-unread-article))))))))
6093   (gnus-summary-position-point))
6094
6095 (defun gnus-summary-prev-page-or-article (&optional lines)
6096   "Show previous page of selected article.
6097 Argument LINES specifies lines to be scrolled down.
6098 If at the beginning of the article, go to the next article."
6099   (interactive "P")
6100   (gnus-summary-prev-page lines t))
6101
6102 (defun gnus-summary-scroll-up (lines)
6103   "Scroll up (or down) one line current article.
6104 Argument LINES specifies lines to be scrolled up (or down if negative)."
6105   (interactive "p")
6106   (gnus-configure-windows 'article)
6107   (gnus-summary-show-thread)
6108   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6109     (gnus-eval-in-buffer-window gnus-article-buffer
6110       (cond ((> lines 0)
6111              (when (gnus-article-next-page lines)
6112                (gnus-message 3 "End of message")))
6113             ((< lines 0)
6114              (gnus-article-prev-page (- lines))))))
6115   (gnus-summary-recenter)
6116   (gnus-summary-position-point))
6117
6118 (defun gnus-summary-scroll-down (lines)
6119   "Scroll down (or up) one line current article.
6120 Argument LINES specifies lines to be scrolled down (or up if negative)."
6121   (interactive "p")
6122   (gnus-summary-scroll-up (- lines)))
6123
6124 (defun gnus-summary-next-same-subject ()
6125   "Select next article which has the same subject as current one."
6126   (interactive)
6127   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6128
6129 (defun gnus-summary-prev-same-subject ()
6130   "Select previous article which has the same subject as current one."
6131   (interactive)
6132   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6133
6134 (defun gnus-summary-next-unread-same-subject ()
6135   "Select next unread article which has the same subject as current one."
6136   (interactive)
6137   (gnus-summary-next-article t (gnus-summary-article-subject)))
6138
6139 (defun gnus-summary-prev-unread-same-subject ()
6140   "Select previous unread article which has the same subject as current one."
6141   (interactive)
6142   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6143
6144 (defun gnus-summary-first-unread-article ()
6145   "Select the first unread article.
6146 Return nil if there are no unread articles."
6147   (interactive)
6148   (prog1
6149       (when (gnus-summary-first-subject t)
6150         (gnus-summary-show-thread)
6151         (gnus-summary-first-subject t)
6152         (gnus-summary-display-article (gnus-summary-article-number)))
6153     (gnus-summary-position-point)))
6154
6155 (defun gnus-summary-first-unread-subject ()
6156   "Place the point on the subject line of the first unread article.
6157 Return nil if there are no unread articles."
6158   (interactive)
6159   (prog1
6160       (when (gnus-summary-first-subject t)
6161         (gnus-summary-show-thread)
6162         (gnus-summary-first-subject t))
6163     (gnus-summary-position-point)))
6164
6165 (defun gnus-summary-first-article ()
6166   "Select the first article.
6167 Return nil if there are no articles."
6168   (interactive)
6169   (prog1
6170       (when (gnus-summary-first-subject)
6171         (gnus-summary-show-thread)
6172         (gnus-summary-first-subject)
6173         (gnus-summary-display-article (gnus-summary-article-number)))
6174     (gnus-summary-position-point)))
6175
6176 (defun gnus-summary-best-unread-article ()
6177   "Select the unread article with the highest score."
6178   (interactive)
6179   (let ((best -1000000)
6180         (data gnus-newsgroup-data)
6181         article score)
6182     (while data
6183       (and (gnus-data-unread-p (car data))
6184            (> (setq score
6185                     (gnus-summary-article-score (gnus-data-number (car data))))
6186               best)
6187            (setq best score
6188                  article (gnus-data-number (car data))))
6189       (setq data (cdr data)))
6190     (prog1
6191         (if article
6192             (gnus-summary-goto-article article)
6193           (error "No unread articles"))
6194       (gnus-summary-position-point))))
6195
6196 (defun gnus-summary-last-subject ()
6197   "Go to the last displayed subject line in the group."
6198   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6199     (when article
6200       (gnus-summary-goto-subject article))))
6201
6202 (defun gnus-summary-goto-article (article &optional all-headers force)
6203   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6204 If ALL-HEADERS is non-nil, no header lines are hidden.
6205 If FORCE, go to the article even if it isn't displayed.  If FORCE
6206 is a number, it is the line the article is to be displayed on."
6207   (interactive
6208    (list
6209     (completing-read
6210      "Article number or Message-ID: "
6211      (mapcar (lambda (number) (list (int-to-string number)))
6212              gnus-newsgroup-limit))
6213     current-prefix-arg
6214     t))
6215   (prog1
6216       (if (and (stringp article)
6217                (string-match "@" article))
6218           (gnus-summary-refer-article article)
6219         (when (stringp article)
6220           (setq article (string-to-number article)))
6221         (if (gnus-summary-goto-subject article force)
6222             (gnus-summary-display-article article all-headers)
6223           (gnus-message 4 "Couldn't go to article %s" article) nil))
6224     (gnus-summary-position-point)))
6225
6226 (defun gnus-summary-goto-last-article ()
6227   "Go to the previously read article."
6228   (interactive)
6229   (prog1
6230       (when gnus-last-article
6231         (gnus-summary-goto-article gnus-last-article nil t))
6232     (gnus-summary-position-point)))
6233
6234 (defun gnus-summary-pop-article (number)
6235   "Pop one article off the history and go to the previous.
6236 NUMBER articles will be popped off."
6237   (interactive "p")
6238   (let (to)
6239     (setq gnus-newsgroup-history
6240           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6241     (if to
6242         (gnus-summary-goto-article (car to) nil t)
6243       (error "Article history empty")))
6244   (gnus-summary-position-point))
6245
6246 ;; Summary commands and functions for limiting the summary buffer.
6247
6248 (defun gnus-summary-limit-to-articles (n)
6249   "Limit the summary buffer to the next N articles.
6250 If not given a prefix, use the process marked articles instead."
6251   (interactive "P")
6252   (prog1
6253       (let ((articles (gnus-summary-work-articles n)))
6254         (setq gnus-newsgroup-processable nil)
6255         (gnus-summary-limit articles))
6256     (gnus-summary-position-point)))
6257
6258 (defun gnus-summary-pop-limit (&optional total)
6259   "Restore the previous limit.
6260 If given a prefix, remove all limits."
6261   (interactive "P")
6262   (when total
6263     (setq gnus-newsgroup-limits
6264           (list (mapcar (lambda (h) (mail-header-number h))
6265                         gnus-newsgroup-headers))))
6266   (unless gnus-newsgroup-limits
6267     (error "No limit to pop"))
6268   (prog1
6269       (gnus-summary-limit nil 'pop)
6270     (gnus-summary-position-point)))
6271
6272 (defun gnus-summary-limit-to-subject (subject &optional header)
6273   "Limit the summary buffer to articles that have subjects that match a regexp."
6274   (interactive "sLimit to subject (regexp): ")
6275   (unless header
6276     (setq header "subject"))
6277   (when (not (equal "" subject))
6278     (prog1
6279         (let ((articles (gnus-summary-find-matching
6280                          (or header "subject") subject 'all)))
6281           (unless articles
6282             (error "Found no matches for \"%s\"" subject))
6283           (gnus-summary-limit articles))
6284       (gnus-summary-position-point))))
6285
6286 (defun gnus-summary-limit-to-author (from)
6287   "Limit the summary buffer to articles that have authors that match a regexp."
6288   (interactive "sLimit to author (regexp): ")
6289   (gnus-summary-limit-to-subject from "from"))
6290
6291 (defun gnus-summary-limit-to-age (age &optional younger-p)
6292   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6293 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6294 articles that are younger than AGE days."
6295   (interactive
6296    (let ((younger current-prefix-arg)
6297          (days-got nil)
6298          days)
6299      (while (not days-got)
6300        (setq days (if younger
6301                       (read-string "Limit to articles within (in days): ")
6302                     (read-string "Limit to articles old than (in days): ")))
6303        (when (> (length days) 0)
6304          (setq days (read days)))
6305        (if (numberp days)
6306            (setq days-got t)
6307          (message "Please enter a number.")
6308          (sleep-for 1)))
6309      (list days younger)))
6310   (prog1
6311       (let ((data gnus-newsgroup-data)
6312             (cutoff (days-to-time age))
6313             articles d date is-younger)
6314         (while (setq d (pop data))
6315           (when (and (vectorp (gnus-data-header d))
6316                      (setq date (mail-header-date (gnus-data-header d))))
6317             (setq is-younger (time-less-p
6318                               (time-since (condition-case ()
6319                                               (date-to-time date)
6320                                             (error '(0 0))))
6321                               cutoff))
6322             (when (if younger-p
6323                       is-younger
6324                     (not is-younger))
6325               (push (gnus-data-number d) articles))))
6326         (gnus-summary-limit (nreverse articles)))
6327     (gnus-summary-position-point)))
6328
6329 (defun gnus-summary-limit-to-extra (header regexp)
6330   "Limit the summary buffer to articles that match an 'extra' header."
6331   (interactive
6332    (let ((header
6333           (intern
6334            (gnus-completing-read
6335             (symbol-name (car gnus-extra-headers))      
6336             "Limit extra header:"       
6337             (mapcar (lambda (x) 
6338                       (cons (symbol-name x) x))
6339                     gnus-extra-headers)
6340             nil                 
6341             t))))
6342      (list header
6343            (read-string (format "Limit to header %s (regexp): " header)))))
6344   (when (not (equal "" regexp))
6345     (prog1
6346         (let ((articles (gnus-summary-find-matching
6347                          (cons 'extra header) regexp 'all)))
6348           (unless articles
6349             (error "Found no matches for \"%s\"" regexp))
6350           (gnus-summary-limit articles))
6351       (gnus-summary-position-point))))
6352
6353 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6354 (make-obsolete
6355  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6356
6357 (defun gnus-summary-limit-to-unread (&optional all)
6358   "Limit the summary buffer to articles that are not marked as read.
6359 If ALL is non-nil, limit strictly to unread articles."
6360   (interactive "P")
6361   (if all
6362       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6363     (gnus-summary-limit-to-marks
6364      ;; Concat all the marks that say that an article is read and have
6365      ;; those removed.
6366      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6367            gnus-killed-mark gnus-kill-file-mark
6368            gnus-low-score-mark gnus-expirable-mark
6369            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6370            gnus-duplicate-mark gnus-souped-mark)
6371      'reverse)))
6372
6373 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6374 (make-obsolete 'gnus-summary-delete-marked-with
6375                'gnus-summary-limit-exlude-marks)
6376
6377 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6378   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6379 If REVERSE, limit the summary buffer to articles that are marked
6380 with MARKS.  MARKS can either be a string of marks or a list of marks.
6381 Returns how many articles were removed."
6382   (interactive "sMarks: ")
6383   (gnus-summary-limit-to-marks marks t))
6384
6385 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6386   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6387 If REVERSE (the prefix), limit the summary buffer to articles that are
6388 not marked with MARKS.  MARKS can either be a string of marks or a
6389 list of marks.
6390 Returns how many articles were removed."
6391   (interactive "sMarks: \nP")
6392   (prog1
6393       (let ((data gnus-newsgroup-data)
6394             (marks (if (listp marks) marks
6395                      (append marks nil))) ; Transform to list.
6396             articles)
6397         (while data
6398           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6399                   (memq (gnus-data-mark (car data)) marks))
6400             (push (gnus-data-number (car data)) articles))
6401           (setq data (cdr data)))
6402         (gnus-summary-limit articles))
6403     (gnus-summary-position-point)))
6404
6405 (defun gnus-summary-limit-to-score (&optional score)
6406   "Limit to articles with score at or above SCORE."
6407   (interactive "P")
6408   (setq score (if score
6409                   (prefix-numeric-value score)
6410                 (or gnus-summary-default-score 0)))
6411   (let ((data gnus-newsgroup-data)
6412         articles)
6413     (while data
6414       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6415                 score)
6416         (push (gnus-data-number (car data)) articles))
6417       (setq data (cdr data)))
6418     (prog1
6419         (gnus-summary-limit articles)
6420       (gnus-summary-position-point))))
6421
6422 (defun gnus-summary-limit-include-thread (id)
6423   "Display all the hidden articles that in the current thread."
6424   (interactive (list (mail-header-id (gnus-summary-article-header))))
6425   (let ((articles (gnus-articles-in-thread
6426                    (gnus-id-to-thread (gnus-root-id id)))))
6427     (prog1
6428         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6429       (gnus-summary-position-point))))
6430
6431 (defun gnus-summary-limit-include-dormant ()
6432   "Display all the hidden articles that are marked as dormant.
6433 Note that this command only works on a subset of the articles currently
6434 fetched for this group."
6435   (interactive)
6436   (unless gnus-newsgroup-dormant
6437     (error "There are no dormant articles in this group"))
6438   (prog1
6439       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6440     (gnus-summary-position-point)))
6441
6442 (defun gnus-summary-limit-exclude-dormant ()
6443   "Hide all dormant articles."
6444   (interactive)
6445   (prog1
6446       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6447     (gnus-summary-position-point)))
6448
6449 (defun gnus-summary-limit-exclude-childless-dormant ()
6450   "Hide all dormant articles that have no children."
6451   (interactive)
6452   (let ((data (gnus-data-list t))
6453         articles d children)
6454     ;; Find all articles that are either not dormant or have
6455     ;; children.
6456     (while (setq d (pop data))
6457       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6458                 (and (setq children
6459                            (gnus-article-children (gnus-data-number d)))
6460                      (let (found)
6461                        (while children
6462                          (when (memq (car children) articles)
6463                            (setq children nil
6464                                  found t))
6465                          (pop children))
6466                        found)))
6467         (push (gnus-data-number d) articles)))
6468     ;; Do the limiting.
6469     (prog1
6470         (gnus-summary-limit articles)
6471       (gnus-summary-position-point))))
6472
6473 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6474   "Mark all unread excluded articles as read.
6475 If ALL, mark even excluded ticked and dormants as read."
6476   (interactive "P")
6477   (let ((articles (gnus-sorted-complement
6478                    (sort
6479                     (mapcar (lambda (h) (mail-header-number h))
6480                             gnus-newsgroup-headers)
6481                     '<)
6482                    (sort gnus-newsgroup-limit '<)))
6483         article)
6484     (setq gnus-newsgroup-unreads
6485           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6486     (if all
6487         (setq gnus-newsgroup-dormant nil
6488               gnus-newsgroup-marked nil
6489               gnus-newsgroup-reads
6490               (nconc
6491                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6492                gnus-newsgroup-reads))
6493       (while (setq article (pop articles))
6494         (unless (or (memq article gnus-newsgroup-dormant)
6495                     (memq article gnus-newsgroup-marked))
6496           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6497
6498 (defun gnus-summary-limit (articles &optional pop)
6499   (if pop
6500       ;; We pop the previous limit off the stack and use that.
6501       (setq articles (car gnus-newsgroup-limits)
6502             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6503     ;; We use the new limit, so we push the old limit on the stack.
6504     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6505   ;; Set the limit.
6506   (setq gnus-newsgroup-limit articles)
6507   (let ((total (length gnus-newsgroup-data))
6508         (data (gnus-data-find-list (gnus-summary-article-number)))
6509         (gnus-summary-mark-below nil)   ; Inhibit this.
6510         found)
6511     ;; This will do all the work of generating the new summary buffer
6512     ;; according to the new limit.
6513     (gnus-summary-prepare)
6514     ;; Hide any threads, possibly.
6515     (and gnus-show-threads
6516          gnus-thread-hide-subtree
6517          (gnus-summary-hide-all-threads))
6518     ;; Try to return to the article you were at, or one in the
6519     ;; neighborhood.
6520     (when data
6521       ;; We try to find some article after the current one.
6522       (while data
6523         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6524           (setq data nil
6525                 found t))
6526         (setq data (cdr data))))
6527     (unless found
6528       ;; If there is no data, that means that we were after the last
6529       ;; article.  The same goes when we can't find any articles
6530       ;; after the current one.
6531       (goto-char (point-max))
6532       (gnus-summary-find-prev))
6533     (gnus-set-mode-line 'summary)
6534     ;; We return how many articles were removed from the summary
6535     ;; buffer as a result of the new limit.
6536     (- total (length gnus-newsgroup-data))))
6537
6538 (defsubst gnus-invisible-cut-children (threads)
6539   (let ((num 0))
6540     (while threads
6541       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6542         (incf num))
6543       (pop threads))
6544     (< num 2)))
6545
6546 (defsubst gnus-cut-thread (thread)
6547   "Go forwards in the thread until we find an article that we want to display."
6548   (when (or (eq gnus-fetch-old-headers 'some)
6549             (eq gnus-fetch-old-headers 'invisible)
6550             (numberp gnus-fetch-old-headers)
6551             (eq gnus-build-sparse-threads 'some)
6552             (eq gnus-build-sparse-threads 'more))
6553     ;; Deal with old-fetched headers and sparse threads.
6554     (while (and
6555             thread
6556             (or
6557              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6558              (gnus-summary-article-ancient-p
6559               (mail-header-number (car thread))))
6560             (if (or (<= (length (cdr thread)) 1)
6561                     (eq gnus-fetch-old-headers 'invisible))
6562                 (setq gnus-newsgroup-limit
6563                       (delq (mail-header-number (car thread))
6564                             gnus-newsgroup-limit)
6565                       thread (cadr thread))
6566               (when (gnus-invisible-cut-children (cdr thread))
6567                 (let ((th (cdr thread)))
6568                   (while th
6569                     (if (memq (mail-header-number (caar th))
6570                               gnus-newsgroup-limit)
6571                         (setq thread (car th)
6572                               th nil)
6573                       (setq th (cdr th))))))))))
6574   thread)
6575
6576 (defun gnus-cut-threads (threads)
6577   "Cut off all uninteresting articles from the beginning of threads."
6578   (when (or (eq gnus-fetch-old-headers 'some)
6579             (eq gnus-fetch-old-headers 'invisible)
6580             (numberp gnus-fetch-old-headers)
6581             (eq gnus-build-sparse-threads 'some)
6582             (eq gnus-build-sparse-threads 'more))
6583     (let ((th threads))
6584       (while th
6585         (setcar th (gnus-cut-thread (car th)))
6586         (setq th (cdr th)))))
6587   ;; Remove nixed out threads.
6588   (delq nil threads))
6589
6590 (defun gnus-summary-initial-limit (&optional show-if-empty)
6591   "Figure out what the initial limit is supposed to be on group entry.
6592 This entails weeding out unwanted dormants, low-scored articles,
6593 fetch-old-headers verbiage, and so on."
6594   ;; Most groups have nothing to remove.
6595   (if (or gnus-inhibit-limiting
6596           (and (null gnus-newsgroup-dormant)
6597                (not (eq gnus-fetch-old-headers 'some))
6598                (not (numberp gnus-fetch-old-headers))
6599                (not (eq gnus-fetch-old-headers 'invisible))
6600                (null gnus-summary-expunge-below)
6601                (not (eq gnus-build-sparse-threads 'some))
6602                (not (eq gnus-build-sparse-threads 'more))
6603                (null gnus-thread-expunge-below)
6604                (not gnus-use-nocem)))
6605       ()                                ; Do nothing.
6606     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6607     (setq gnus-newsgroup-limit nil)
6608     (mapatoms
6609      (lambda (node)
6610        (unless (car (symbol-value node))
6611          ;; These threads have no parents -- they are roots.
6612          (let ((nodes (cdr (symbol-value node)))
6613                thread)
6614            (while nodes
6615              (if (and gnus-thread-expunge-below
6616                       (< (gnus-thread-total-score (car nodes))
6617                          gnus-thread-expunge-below))
6618                  (gnus-expunge-thread (pop nodes))
6619                (setq thread (pop nodes))
6620                (gnus-summary-limit-children thread))))))
6621      gnus-newsgroup-dependencies)
6622     ;; If this limitation resulted in an empty group, we might
6623     ;; pop the previous limit and use it instead.
6624     (when (and (not gnus-newsgroup-limit)
6625                show-if-empty)
6626       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6627     gnus-newsgroup-limit))
6628
6629 (defun gnus-summary-limit-children (thread)
6630   "Return 1 if this subthread is visible and 0 if it is not."
6631   ;; First we get the number of visible children to this thread.  This
6632   ;; is done by recursing down the thread using this function, so this
6633   ;; will really go down to a leaf article first, before slowly
6634   ;; working its way up towards the root.
6635   (when thread
6636     (let ((children
6637            (if (cdr thread)
6638                (apply '+ (mapcar 'gnus-summary-limit-children
6639                                  (cdr thread)))
6640              0))
6641           (number (mail-header-number (car thread)))
6642           score)
6643       (if (and
6644            (not (memq number gnus-newsgroup-marked))
6645            (or
6646             ;; If this article is dormant and has absolutely no visible
6647             ;; children, then this article isn't visible.
6648             (and (memq number gnus-newsgroup-dormant)
6649                  (zerop children))
6650             ;; If this is "fetch-old-headered" and there is no
6651             ;; visible children, then we don't want this article.
6652             (and (or (eq gnus-fetch-old-headers 'some)
6653                      (numberp gnus-fetch-old-headers))
6654                  (gnus-summary-article-ancient-p number)
6655                  (zerop children))
6656             ;; If this is "fetch-old-headered" and `invisible', then
6657             ;; we don't want this article.
6658             (and (eq gnus-fetch-old-headers 'invisible)
6659                  (gnus-summary-article-ancient-p number))
6660             ;; If this is a sparsely inserted article with no children,
6661             ;; we don't want it.
6662             (and (eq gnus-build-sparse-threads 'some)
6663                  (gnus-summary-article-sparse-p number)
6664                  (zerop children))
6665             ;; If we use expunging, and this article is really
6666             ;; low-scored, then we don't want this article.
6667             (when (and gnus-summary-expunge-below
6668                        (< (setq score
6669                                 (or (cdr (assq number gnus-newsgroup-scored))
6670                                     gnus-summary-default-score))
6671                           gnus-summary-expunge-below))
6672               ;; We increase the expunge-tally here, but that has
6673               ;; nothing to do with the limits, really.
6674               (incf gnus-newsgroup-expunged-tally)
6675               ;; We also mark as read here, if that's wanted.
6676               (when (and gnus-summary-mark-below
6677                          (< score gnus-summary-mark-below))
6678                 (setq gnus-newsgroup-unreads
6679                       (delq number gnus-newsgroup-unreads))
6680                 (if gnus-newsgroup-auto-expire
6681                     (push number gnus-newsgroup-expirable)
6682                   (push (cons number gnus-low-score-mark)
6683                         gnus-newsgroup-reads)))
6684               t)
6685             ;; Check NoCeM things.
6686             (if (and gnus-use-nocem
6687                      (gnus-nocem-unwanted-article-p
6688                       (mail-header-id (car thread))))
6689                 (progn
6690                   (setq gnus-newsgroup-unreads
6691                         (delq number gnus-newsgroup-unreads))
6692                   t))))
6693           ;; Nope, invisible article.
6694           0
6695         ;; Ok, this article is to be visible, so we add it to the limit
6696         ;; and return 1.
6697         (push number gnus-newsgroup-limit)
6698         1))))
6699
6700 (defun gnus-expunge-thread (thread)
6701   "Mark all articles in THREAD as read."
6702   (let* ((number (mail-header-number (car thread))))
6703     (incf gnus-newsgroup-expunged-tally)
6704     ;; We also mark as read here, if that's wanted.
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   ;; Go recursively through all subthreads.
6712   (mapcar 'gnus-expunge-thread (cdr thread)))
6713
6714 ;; Summary article oriented commands
6715
6716 (defun gnus-summary-refer-parent-article (n)
6717   "Refer parent article N times.
6718 If N is negative, go to ancestor -N instead.
6719 The difference between N and the number of articles fetched is returned."
6720   (interactive "p")
6721   (let ((skip 1)
6722         error header ref)
6723     (when (not (natnump n))
6724       (setq skip (abs n)
6725             n 1))
6726     (while (and (> n 0)
6727                 (not error))
6728       (setq header (gnus-summary-article-header))
6729       (if (and (eq (mail-header-number header)
6730                    (cdr gnus-article-current))
6731                (equal gnus-newsgroup-name
6732                       (car gnus-article-current)))
6733           ;; If we try to find the parent of the currently
6734           ;; displayed article, then we take a look at the actual
6735           ;; References header, since this is slightly more
6736           ;; reliable than the References field we got from the
6737           ;; server.
6738           (save-excursion
6739             (set-buffer gnus-original-article-buffer)
6740             (nnheader-narrow-to-headers)
6741             (unless (setq ref (message-fetch-field "references"))
6742               (setq ref (message-fetch-field "in-reply-to")))
6743             (widen))
6744         (setq ref
6745               ;; It's not the current article, so we take a bet on
6746               ;; the value we got from the server.
6747               (mail-header-references header)))
6748       (if (and ref
6749                (not (equal ref "")))
6750           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6751             (gnus-message 1 "Couldn't find parent"))
6752         (gnus-message 1 "No references in article %d"
6753                       (gnus-summary-article-number))
6754         (setq error t))
6755       (decf n))
6756     (gnus-summary-position-point)
6757     n))
6758
6759 (defun gnus-summary-refer-references ()
6760   "Fetch all articles mentioned in the References header.
6761 Return the number of articles fetched."
6762   (interactive)
6763   (let ((ref (mail-header-references (gnus-summary-article-header)))
6764         (current (gnus-summary-article-number))
6765         (n 0))
6766     (if (or (not ref)
6767             (equal ref ""))
6768         (error "No References in the current article")
6769       ;; For each Message-ID in the References header...
6770       (while (string-match "<[^>]*>" ref)
6771         (incf n)
6772         ;; ... fetch that article.
6773         (gnus-summary-refer-article
6774          (prog1 (match-string 0 ref)
6775            (setq ref (substring ref (match-end 0))))))
6776       (gnus-summary-goto-subject current)
6777       (gnus-summary-position-point)
6778       n)))
6779
6780 (defun gnus-summary-refer-thread (&optional limit)
6781   "Fetch all articles in the current thread.
6782 If LIMIT (the numerical prefix), fetch that many old headers instead
6783 of what's specified by the `gnus-refer-thread-limit' variable."
6784   (interactive "P")
6785   (let ((id (mail-header-id (gnus-summary-article-header)))
6786         (limit (if limit (prefix-numeric-value limit)
6787                  gnus-refer-thread-limit)))
6788     ;; We want to fetch LIMIT *old* headers, but we also have to
6789     ;; re-fetch all the headers in the current buffer, because many of
6790     ;; them may be undisplayed.  So we adjust LIMIT.
6791     (when (numberp limit)
6792       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6793     (unless (eq gnus-fetch-old-headers 'invisible)
6794       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6795       ;; Retrieve the headers and read them in.
6796       (if (eq (gnus-retrieve-headers
6797                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6798               'nov)
6799           (gnus-build-all-threads)
6800         (error "Can't fetch thread from backends that don't support NOV"))
6801       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6802     (gnus-summary-limit-include-thread id)))
6803
6804 (defun gnus-summary-refer-article (message-id)
6805   "Fetch an article specified by MESSAGE-ID."
6806   (interactive "sMessage-ID: ")
6807   (when (and (stringp message-id)
6808              (not (zerop (length message-id))))
6809     ;; Construct the correct Message-ID if necessary.
6810     ;; Suggested by tale@pawl.rpi.edu.
6811     (unless (string-match "^<" message-id)
6812       (setq message-id (concat "<" message-id)))
6813     (unless (string-match ">$" message-id)
6814       (setq message-id (concat message-id ">")))
6815     (let* ((header (gnus-id-to-header message-id))
6816            (sparse (and header
6817                         (gnus-summary-article-sparse-p
6818                          (mail-header-number header))
6819                         (memq (mail-header-number header)
6820                               gnus-newsgroup-limit)))
6821            number)
6822       (cond
6823        ;; If the article is present in the buffer we just go to it.
6824        ((and header
6825              (or (not (gnus-summary-article-sparse-p
6826                        (mail-header-number header)))
6827                  sparse))
6828         (prog1
6829             (gnus-summary-goto-article
6830              (mail-header-number header) nil t)
6831           (when sparse
6832             (gnus-summary-update-article (mail-header-number header)))))
6833        (t
6834         ;; We fetch the article.
6835         (catch 'found
6836           (dolist (gnus-override-method (gnus-refer-article-methods))
6837             (gnus-check-server gnus-override-method)
6838             ;; Fetch the header, and display the article.
6839             (when (setq number (gnus-summary-insert-subject message-id))
6840               (gnus-summary-select-article nil nil nil number)
6841               (throw 'found t)))
6842           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6843
6844 (defun gnus-refer-article-methods ()
6845   "Return a list of referrable methods."
6846   (cond
6847    ;; No method, so we default to current and native.
6848    ((null gnus-refer-article-method)
6849     (list gnus-current-select-method gnus-select-method))
6850    ;; Current.
6851    ((eq 'current gnus-refer-article-method)
6852     (list gnus-current-select-method))
6853    ;; List of select methods.
6854    ((not (stringp (cadr gnus-refer-article-method)))
6855     (let (out)
6856       (dolist (method gnus-refer-article-method)
6857         (push (if (eq 'current method)
6858                   gnus-current-select-method
6859                 method)
6860               out))
6861       (nreverse out)))
6862    ;; One single select method.
6863    (t
6864     (list gnus-refer-article-method))))
6865
6866 (defun gnus-summary-edit-parameters ()
6867   "Edit the group parameters of the current group."
6868   (interactive)
6869   (gnus-group-edit-group gnus-newsgroup-name 'params))
6870
6871 (defun gnus-summary-customize-parameters ()
6872   "Customize the group parameters of the current group."
6873   (interactive)
6874   (gnus-group-customize gnus-newsgroup-name))
6875
6876 (defun gnus-summary-enter-digest-group (&optional force)
6877   "Enter an nndoc group based on the current article.
6878 If FORCE, force a digest interpretation.  If not, try
6879 to guess what the document format is."
6880   (interactive "P")
6881   (let ((conf gnus-current-window-configuration))
6882     (save-excursion
6883       (gnus-summary-select-article))
6884     (setq gnus-current-window-configuration conf)
6885     (let* ((name (format "%s-%d"
6886                          (gnus-group-prefixed-name
6887                           gnus-newsgroup-name (list 'nndoc ""))
6888                          (save-excursion
6889                            (set-buffer gnus-summary-buffer)
6890                            gnus-current-article)))
6891            (ogroup gnus-newsgroup-name)
6892            (params (append (gnus-info-params (gnus-get-info ogroup))
6893                            (list (cons 'to-group ogroup))
6894                            (list (cons 'save-article-group ogroup))))
6895            (case-fold-search t)
6896            (buf (current-buffer))
6897            dig to-address)
6898       (save-excursion
6899         (set-buffer gnus-original-article-buffer)
6900         ;; Have the digest group inherit the main mail address of
6901         ;; the parent article.
6902         (when (setq to-address (or (message-fetch-field "reply-to")
6903                                    (message-fetch-field "from")))
6904           (setq params (append (list (cons 'to-address to-address)))))
6905         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6906         (insert-buffer-substring gnus-original-article-buffer)
6907         ;; Remove lines that may lead nndoc to misinterpret the
6908         ;; document type.
6909         (narrow-to-region
6910          (goto-char (point-min))
6911          (or (search-forward "\n\n" nil t) (point)))
6912         (goto-char (point-min))
6913         (delete-matching-lines "^Path:\\|^From ")
6914         (widen))
6915       (unwind-protect
6916           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6917                     (gnus-newsgroup-ephemeral-ignored-charsets
6918                      gnus-newsgroup-ignored-charsets))
6919                 (gnus-group-read-ephemeral-group
6920                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6921                               (nndoc-article-type
6922                                ,(if force 'mbox 'guess))) t))
6923               ;; Make all postings to this group go to the parent group.
6924               (nconc (gnus-info-params (gnus-get-info name))
6925                      params)
6926             ;; Couldn't select this doc group.
6927             (switch-to-buffer buf)
6928             (gnus-set-global-variables)
6929             (gnus-configure-windows 'summary)
6930             (gnus-message 3 "Article couldn't be entered?"))
6931         (kill-buffer dig)))))
6932
6933 (defun gnus-summary-read-document (n)
6934   "Open a new group based on the current article(s).
6935 This will allow you to read digests and other similar
6936 documents as newsgroups.
6937 Obeys the standard process/prefix convention."
6938   (interactive "P")
6939   (let* ((articles (gnus-summary-work-articles n))
6940          (ogroup gnus-newsgroup-name)
6941          (params (append (gnus-info-params (gnus-get-info ogroup))
6942                          (list (cons 'to-group ogroup))))
6943          article group egroup groups vgroup)
6944     (while (setq article (pop articles))
6945       (setq group (format "%s-%d" gnus-newsgroup-name article))
6946       (gnus-summary-remove-process-mark article)
6947       (when (gnus-summary-display-article article)
6948         (save-excursion
6949           (with-temp-buffer
6950             (insert-buffer-substring gnus-original-article-buffer)
6951             ;; Remove some headers that may lead nndoc to make
6952             ;; the wrong guess.
6953             (message-narrow-to-head)
6954             (goto-char (point-min))
6955             (delete-matching-lines "^\\(Path\\):\\|^From ")
6956             (widen)
6957             (if (setq egroup
6958                       (gnus-group-read-ephemeral-group
6959                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6960                                      (nndoc-article-type guess))
6961                        t nil t))
6962                 (progn
6963                   ;; Make all postings to this group go to the parent group.
6964                   (nconc (gnus-info-params (gnus-get-info egroup))
6965                          params)
6966                   (push egroup groups))
6967               ;; Couldn't select this doc group.
6968               (gnus-error 3 "Article couldn't be entered"))))))
6969     ;; Now we have selected all the documents.
6970     (cond
6971      ((not groups)
6972       (error "None of the articles could be interpreted as documents"))
6973      ((gnus-group-read-ephemeral-group
6974        (setq vgroup (format
6975                      "nnvirtual:%s-%s" gnus-newsgroup-name
6976                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6977        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6978        t
6979        (cons (current-buffer) 'summary)))
6980      (t
6981       (error "Couldn't select virtual nndoc group")))))
6982
6983 (defun gnus-summary-isearch-article (&optional regexp-p)
6984   "Do incremental search forward on the current article.
6985 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6986   (interactive "P")
6987   (let* ((gnus-inhibit-treatment t)
6988          (old (gnus-summary-select-article)))
6989     (gnus-configure-windows 'article)
6990     (gnus-eval-in-buffer-window gnus-article-buffer
6991       (save-restriction
6992         (widen)
6993         (when (eq 'old old)
6994           (gnus-article-show-all-headers))
6995         (goto-char (point-min))
6996         (isearch-forward regexp-p)))))
6997
6998 (defun gnus-summary-search-article-forward (regexp &optional backward)
6999   "Search for an article containing REGEXP forward.
7000 If BACKWARD, search backward instead."
7001   (interactive
7002    (list (read-string
7003           (format "Search article %s (regexp%s): "
7004                   (if current-prefix-arg "backward" "forward")
7005                   (if gnus-last-search-regexp
7006                       (concat ", default " gnus-last-search-regexp)
7007                     "")))
7008          current-prefix-arg))
7009   (if (string-equal regexp "")
7010       (setq regexp (or gnus-last-search-regexp ""))
7011     (setq gnus-last-search-regexp regexp))
7012   (if (gnus-summary-search-article regexp backward)
7013       (gnus-summary-show-thread)
7014     (error "Search failed: \"%s\"" regexp)))
7015
7016 (defun gnus-summary-search-article-backward (regexp)
7017   "Search for an article containing REGEXP backward."
7018   (interactive
7019    (list (read-string
7020           (format "Search article backward (regexp%s): "
7021                   (if gnus-last-search-regexp
7022                       (concat ", default " gnus-last-search-regexp)
7023                     "")))))
7024   (gnus-summary-search-article-forward regexp 'backward))
7025
7026 (eval-when-compile
7027   (defmacro gnus-summary-search-article-position-point (regexp backward)
7028     "Dehighlight the last matched text and goto the beginning position."
7029     (` (if (and gnus-summary-search-article-matched-data
7030                 (let ((text (caddr gnus-summary-search-article-matched-data))
7031                       (inhibit-read-only t)
7032                       buffer-read-only)
7033                   (delete-region
7034                    (goto-char (car gnus-summary-search-article-matched-data))
7035                    (cadr gnus-summary-search-article-matched-data))
7036                   (insert text)
7037                   (string-match (, regexp) text)))
7038            (if (, backward) (beginning-of-line) (end-of-line))
7039          (goto-char (if (, backward) (point-max) (point-min))))))
7040
7041   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7042     "Place point where X-Face image is displayed."
7043     (if (featurep 'xemacs)
7044         (` (let ((end (if (search-forward "\n\n" nil t)
7045                           (goto-char (1- (point)))
7046                         (point-min)))
7047                  extent)
7048              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7049              (unless (and (re-search-forward "^From:" end t)
7050                           (setq extent (extent-at (point)))
7051                           (extent-begin-glyph extent))
7052                (goto-char (, opoint)))))
7053       (` (let ((end (if (search-forward "\n\n" nil t)
7054                         (goto-char (1- (point)))
7055                       (point-min))))
7056            (goto-char
7057             (or (text-property-any (or (search-backward "\n\n" nil t)
7058                                        (point-min))
7059                                    end 'x-face-mule-bitmap-image t)
7060                 (, opoint)))))))
7061
7062   (defmacro gnus-summary-search-article-highlight-matched-text
7063     (backward treated x-face)
7064     "Highlight matched text in the function `gnus-summary-search-article'."
7065     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7066              (end (set-marker (make-marker) (match-end 0)))
7067              (inhibit-read-only t)
7068              buffer-read-only)
7069          (unless treated
7070            (let ((,@
7071                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7072                     (mapcar
7073                      (lambda (item) (setq items (delq item items)))
7074                      '(gnus-treat-buttonize
7075                        gnus-treat-fill-article
7076                        gnus-treat-fill-long-lines
7077                        gnus-treat-emphasize
7078                        gnus-treat-highlight-headers
7079                        gnus-treat-highlight-citation
7080                        gnus-treat-highlight-signature
7081                        gnus-treat-overstrike
7082                        gnus-treat-display-xface
7083                        gnus-treat-buttonize-head
7084                        gnus-treat-decode-article-as-default-mime-charset))
7085                     (static-if (featurep 'xemacs)
7086                         items
7087                       (cons '(x-face-mule-delete-x-face-field
7088                               (quote never))
7089                             items))))
7090                  (gnus-treat-display-xface
7091                   (when (, x-face) gnus-treat-display-xface)))
7092              (gnus-article-prepare-mime-display)))
7093          (goto-char (if (, backward) start end))
7094          (when (, x-face)
7095            (gnus-summary-search-article-highlight-goto-x-face (point)))
7096          (setq gnus-summary-search-article-matched-data
7097                (list start end (buffer-substring start end)))
7098          (unless (eq start end);; matched text has been deleted. :-<
7099            (put-text-property start end 'face
7100                               (or (find-face 'isearch)
7101                                   'secondary-selection))))))
7102   )
7103
7104 (defun gnus-summary-search-article (regexp &optional backward)
7105   "Search for an article containing REGEXP.
7106 Optional argument BACKWARD means do search for backward.
7107 `gnus-select-article-hook' is not called during the search."
7108   ;; We have to require this here to make sure that the following
7109   ;; dynamic binding isn't shadowed by autoloading.
7110   (require 'gnus-async)
7111   (require 'gnus-art)
7112   (let ((gnus-select-article-hook nil)  ;Disable hook.
7113         (gnus-article-display-hook nil)
7114         (gnus-article-prepare-hook nil)
7115         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7116         (gnus-use-article-prefetch nil)
7117         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7118         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7119         (sum (current-buffer))
7120         (found nil)
7121         point treated)
7122     (gnus-save-hidden-threads
7123       (static-if (featurep 'xemacs)
7124           (let ((gnus-inhibit-treatment t))
7125             (setq treated (eq 'old (gnus-summary-select-article)))
7126             (when (and treated
7127                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7128                                  (window-live-p (get-buffer-window
7129                                                  gnus-article-buffer t)))))
7130               (gnus-summary-select-article nil t)
7131               (setq treated nil)))
7132         (let ((gnus-inhibit-treatment t)
7133               (x-face-mule-delete-x-face-field 'never))
7134           (setq treated (eq 'old (gnus-summary-select-article)))
7135           (when (and treated
7136                      (not
7137                       (and (gnus-buffer-live-p gnus-article-buffer)
7138                            (window-live-p (get-buffer-window
7139                                            gnus-article-buffer t))
7140                            (or (not (string-match "^\\^X-Face:" regexp))
7141                                (with-current-buffer gnus-article-buffer
7142                                  gnus-summary-search-article-matched-data)))))
7143             (gnus-summary-select-article nil t)
7144             (setq treated nil))))
7145       (set-buffer gnus-article-buffer)
7146       (widen)
7147       (if treated
7148           (progn
7149             (gnus-article-show-all-headers)
7150             (gnus-summary-search-article-position-point regexp backward))
7151         (goto-char (if backward (point-max) (point-min))))
7152       (while (not found)
7153         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7154         (if (if backward
7155                 (re-search-backward regexp nil t)
7156               (re-search-forward regexp nil t))
7157             ;; We found the regexp.
7158             (progn
7159               (gnus-summary-search-article-highlight-matched-text
7160                backward treated (string-match "^\\^X-Face:" regexp))
7161               (setq found 'found)
7162               (forward-line
7163                (/ (- 2 (window-height
7164                         (get-buffer-window gnus-article-buffer t)))
7165                   2))
7166               (set-window-start
7167                (get-buffer-window (current-buffer))
7168                (point))
7169               (set-buffer sum)
7170               (setq point (point)))
7171           ;; We didn't find it, so we go to the next article.
7172           (set-buffer sum)
7173           (setq found 'not)
7174           (while (eq found 'not)
7175             (if (not (if backward (gnus-summary-find-prev)
7176                        (gnus-summary-find-next)))
7177                 ;; No more articles.
7178                 (setq found t)
7179               ;; Select the next article and adjust point.
7180               (unless (gnus-summary-article-sparse-p
7181                        (gnus-summary-article-number))
7182                 (setq found nil)
7183                 (let ((gnus-inhibit-treatment t))
7184                   (gnus-summary-select-article))
7185                 (setq treated nil)
7186                 (set-buffer gnus-article-buffer)
7187                 (widen)
7188                 (goto-char (if backward (point-max) (point-min))))))))
7189       (gnus-message 7 ""))
7190     ;; Return whether we found the regexp.
7191     (when (eq found 'found)
7192       (goto-char point)
7193       (gnus-summary-show-thread)
7194       (gnus-summary-goto-subject gnus-current-article)
7195       (gnus-summary-position-point)
7196       t)))
7197
7198 (defun gnus-summary-find-matching (header regexp &optional backward unread
7199                                           not-case-fold)
7200   "Return a list of all articles that match REGEXP on HEADER.
7201 The search stars on the current article and goes forwards unless
7202 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7203 If UNREAD is non-nil, only unread articles will
7204 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7205 in the comparisons."
7206   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7207                 (gnus-data-find-list
7208                  (gnus-summary-article-number) (gnus-data-list backward))))
7209         (case-fold-search (not not-case-fold))
7210         articles d func)
7211     (if (consp header)
7212         (if (eq (car header) 'extra)
7213             (setq func
7214                   `(lambda (h)
7215                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7216                          "")))
7217           (error "%s is an invalid header" header))
7218       (unless (fboundp (intern (concat "mail-header-" header)))
7219         (error "%s is not a valid header" header))
7220       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7221     (while data
7222       (setq d (car data))
7223       (and (or (not unread)             ; We want all articles...
7224                (gnus-data-unread-p d))  ; Or just unreads.
7225            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7226            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7227            (push (gnus-data-number d) articles)) ; Success!
7228       (setq data (cdr data)))
7229     (nreverse articles)))
7230
7231 (defun gnus-summary-execute-command (header regexp command &optional backward)
7232   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7233 If HEADER is an empty string (or nil), the match is done on the entire
7234 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7235   (interactive
7236    (list (let ((completion-ignore-case t))
7237            (completing-read
7238             "Header name: "
7239             (mapcar (lambda (string) (list string))
7240                     '("Number" "Subject" "From" "Lines" "Date"
7241                       "Message-ID" "Xref" "References" "Body"))
7242             nil 'require-match))
7243          (read-string "Regexp: ")
7244          (read-key-sequence "Command: ")
7245          current-prefix-arg))
7246   (when (equal header "Body")
7247     (setq header ""))
7248   ;; Hidden thread subtrees must be searched as well.
7249   (gnus-summary-show-all-threads)
7250   ;; We don't want to change current point nor window configuration.
7251   (save-excursion
7252     (save-window-excursion
7253       (gnus-message 6 "Executing %s..." (key-description command))
7254       ;; We'd like to execute COMMAND interactively so as to give arguments.
7255       (gnus-execute header regexp
7256                     `(call-interactively ',(key-binding command))
7257                     backward)
7258       (gnus-message 6 "Executing %s...done" (key-description command)))))
7259
7260 (defun gnus-summary-beginning-of-article ()
7261   "Scroll the article back to the beginning."
7262   (interactive)
7263   (gnus-summary-select-article)
7264   (gnus-configure-windows 'article)
7265   (gnus-eval-in-buffer-window gnus-article-buffer
7266     (widen)
7267     (goto-char (point-min))
7268     (when gnus-page-broken
7269       (gnus-narrow-to-page))))
7270
7271 (defun gnus-summary-end-of-article ()
7272   "Scroll to the end of the article."
7273   (interactive)
7274   (gnus-summary-select-article)
7275   (gnus-configure-windows 'article)
7276   (gnus-eval-in-buffer-window gnus-article-buffer
7277     (widen)
7278     (goto-char (point-max))
7279     (recenter -3)
7280     (when gnus-page-broken
7281       (gnus-narrow-to-page))))
7282
7283 (defun gnus-summary-print-article (&optional filename n)
7284   "Generate and print a PostScript image of the N next (mail) articles.
7285
7286 If N is negative, print the N previous articles.  If N is nil and articles
7287 have been marked with the process mark, print these instead.
7288
7289 If the optional first argument FILENAME is nil, send the image to the
7290 printer.  If FILENAME is a string, save the PostScript image in a file with
7291 that name.  If FILENAME is a number, prompt the user for the name of the file
7292 to save in."
7293   (interactive (list (ps-print-preprint current-prefix-arg)
7294                      current-prefix-arg))
7295   (dolist (article (gnus-summary-work-articles n))
7296     (gnus-summary-select-article nil nil 'pseudo article)
7297     (gnus-eval-in-buffer-window gnus-article-buffer
7298       (let ((buffer (generate-new-buffer " *print*")))
7299         (unwind-protect
7300             (progn
7301               (copy-to-buffer buffer (point-min) (point-max))
7302               (set-buffer buffer)
7303               (gnus-article-delete-invisible-text)
7304               (let ((ps-left-header
7305                      (list
7306                       (concat "("
7307                               (mail-header-subject gnus-current-headers) ")")
7308                       (concat "("
7309                               (mail-header-from gnus-current-headers) ")")))
7310                     (ps-right-header
7311                      (list
7312                       "/pagenumberstring load"
7313                       (concat "("
7314                               (mail-header-date gnus-current-headers) ")"))))
7315                 (gnus-run-hooks 'gnus-ps-print-hook)
7316                 (save-excursion
7317                   (ps-print-buffer-with-faces filename))))
7318           (kill-buffer buffer))))))
7319
7320 (defun gnus-summary-show-article (&optional arg)
7321   "Force re-fetching of the current article.
7322 If ARG (the prefix) is a number, show the article with the charset 
7323 defined in `gnus-summary-show-article-charset-alist', or the charset
7324 inputed.
7325 If ARG (the prefix) is non-nil and not a number, show the raw article 
7326 without any article massaging functions being run."
7327   (interactive "P")
7328   (cond 
7329    ((numberp arg)
7330     (let ((gnus-newsgroup-charset 
7331            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7332                (read-coding-system "Charset: ")))
7333           (gnus-newsgroup-ignored-charsets 'gnus-all))
7334       (gnus-summary-select-article nil 'force)))
7335    ((not arg)
7336     ;; Select the article the normal way.
7337     (gnus-summary-select-article nil 'force))
7338    (t
7339     ;; We have to require this here to make sure that the following
7340     ;; dynamic binding isn't shadowed by autoloading.
7341     (require 'gnus-async)
7342     (require 'gnus-art)
7343     ;; Bind the article treatment functions to nil.
7344     (let ((gnus-have-all-headers t)
7345           gnus-article-display-hook
7346           gnus-article-prepare-hook
7347           gnus-article-decode-hook
7348           gnus-break-pages
7349           gnus-show-mime)
7350       (gnus-summary-select-article nil 'force))))
7351   (gnus-summary-goto-subject gnus-current-article)
7352   (gnus-summary-position-point))
7353
7354 (defun gnus-summary-verbose-headers (&optional arg)
7355   "Toggle permanent full header display.
7356 If ARG is a positive number, turn header display on.
7357 If ARG is a negative number, turn header display off."
7358   (interactive "P")
7359   (setq gnus-show-all-headers
7360         (cond ((or (not (numberp arg))
7361                    (zerop arg))
7362                (not gnus-show-all-headers))
7363               ((natnump arg)
7364                t)))
7365   (gnus-summary-show-article))
7366
7367 (defun gnus-summary-toggle-header (&optional arg)
7368   "Show the headers if they are hidden, or hide them if they are shown.
7369 If ARG is a positive number, show the entire header.
7370 If ARG is a negative number, hide the unwanted header lines."
7371   (interactive "P")
7372   (save-excursion
7373     (set-buffer gnus-article-buffer)
7374     (save-restriction
7375       (let* ((buffer-read-only nil)
7376              (inhibit-point-motion-hooks t)
7377              hidden e)
7378         (setq hidden
7379               (if (numberp arg)
7380                   (>= arg 0)
7381                 (save-restriction 
7382                   (article-narrow-to-head)
7383                   (gnus-article-hidden-text-p 'headers))))
7384         (goto-char (point-min))
7385         (when (search-forward "\n\n" nil t)
7386           (delete-region (point-min) (1- (point))))
7387         (goto-char (point-min))
7388         (save-excursion
7389           (set-buffer gnus-original-article-buffer)
7390           (goto-char (point-min))
7391           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7392         (insert-buffer-substring gnus-original-article-buffer 1 e)
7393         (save-restriction
7394           (narrow-to-region (point-min) (point))
7395           (article-decode-encoded-words)
7396           (if  hidden
7397               (let ((gnus-treat-hide-headers nil)
7398                     (gnus-treat-hide-boring-headers nil))
7399                 (gnus-treat-article 'head))
7400             (gnus-treat-article 'head)))))))
7401
7402 (defun gnus-summary-show-all-headers ()
7403   "Make all header lines visible."
7404   (interactive)
7405   (gnus-article-show-all-headers))
7406
7407 (defun gnus-summary-toggle-mime (&optional arg)
7408   "Toggle MIME processing.
7409 If ARG is a positive number, turn MIME processing on."
7410   (interactive "P")
7411   (setq gnus-show-mime
7412         (if (null arg)
7413             (not gnus-show-mime)
7414           (> (prefix-numeric-value arg) 0)))
7415   (gnus-summary-select-article t 'force))
7416
7417 (defun gnus-summary-caesar-message (&optional arg)
7418   "Caesar rotate the current article by 13.
7419 The numerical prefix specifies how many places to rotate each letter
7420 forward."
7421   (interactive "P")
7422   (gnus-summary-select-article)
7423   (let ((mail-header-separator ""))
7424     (gnus-eval-in-buffer-window gnus-article-buffer
7425       (save-restriction
7426         (widen)
7427         (let ((start (window-start))
7428               buffer-read-only)
7429           (message-caesar-buffer-body arg)
7430           (set-window-start (get-buffer-window (current-buffer)) start))))))
7431
7432 (defun gnus-summary-stop-page-breaking ()
7433   "Stop page breaking in the current article."
7434   (interactive)
7435   (gnus-summary-select-article)
7436   (gnus-eval-in-buffer-window gnus-article-buffer
7437     (widen)
7438     (when (gnus-visual-p 'page-marker)
7439       (let ((buffer-read-only nil))
7440         (gnus-remove-text-with-property 'gnus-prev)
7441         (gnus-remove-text-with-property 'gnus-next))
7442       (setq gnus-page-broken nil))))
7443
7444 (defun gnus-summary-move-article (&optional n to-newsgroup
7445                                             select-method action)
7446   "Move the current article to a different newsgroup.
7447 If N is a positive number, move the N next articles.
7448 If N is a negative number, move the N previous articles.
7449 If N is nil and any articles have been marked with the process mark,
7450 move those articles instead.
7451 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7452 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7453 re-spool using this method.
7454
7455 For this function to work, both the current newsgroup and the
7456 newsgroup that you want to move to have to support the `request-move'
7457 and `request-accept' functions.
7458
7459 ACTION can be either `move' (the default), `crosspost' or `copy'."
7460   (interactive "P")
7461   (unless action
7462     (setq action 'move))
7463   ;; Disable marking as read.
7464   (let (gnus-mark-article-hook)
7465     (save-window-excursion
7466       (gnus-summary-select-article)))
7467   ;; Check whether the source group supports the required functions.
7468   (cond ((and (eq action 'move)
7469               (not (gnus-check-backend-function
7470                     'request-move-article gnus-newsgroup-name)))
7471          (error "The current group does not support article moving"))
7472         ((and (eq action 'crosspost)
7473               (not (gnus-check-backend-function
7474                     'request-replace-article gnus-newsgroup-name)))
7475          (error "The current group does not support article editing")))
7476   (let ((articles (gnus-summary-work-articles n))
7477         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7478         (names '((move "Move" "Moving")
7479                  (copy "Copy" "Copying")
7480                  (crosspost "Crosspost" "Crossposting")))
7481         (copy-buf (save-excursion
7482                     (nnheader-set-temp-buffer " *copy article*")))
7483         (default-marks gnus-article-mark-lists)
7484         (no-expire-marks (delete '(expirable . expire)
7485                                  (copy-sequence gnus-article-mark-lists)))
7486         art-group to-method new-xref article to-groups)
7487     (unless (assq action names)
7488       (error "Unknown action %s" action))
7489     ;; Read the newsgroup name.
7490     (when (and (not to-newsgroup)
7491                (not select-method))
7492       (setq to-newsgroup
7493             (gnus-read-move-group-name
7494              (cadr (assq action names))
7495              (symbol-value (intern (format "gnus-current-%s-group" action)))
7496              articles prefix))
7497       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7498     (setq to-method (or select-method
7499                         (gnus-group-name-to-method to-newsgroup)))
7500     ;; Check the method we are to move this article to...
7501     (unless (gnus-check-backend-function
7502              'request-accept-article (car to-method))
7503       (error "%s does not support article copying" (car to-method)))
7504     (unless (gnus-check-server to-method)
7505       (error "Can't open server %s" (car to-method)))
7506     (gnus-message 6 "%s to %s: %s..."
7507                   (caddr (assq action names))
7508                   (or (car select-method) to-newsgroup) articles)
7509     (while articles
7510       (setq article (pop articles))
7511       (setq
7512        art-group
7513        (cond
7514         ;; Move the article.
7515         ((eq action 'move)
7516          ;; Remove this article from future suppression.
7517          (gnus-dup-unsuppress-article article)
7518          (gnus-request-move-article
7519           article                       ; Article to move
7520           gnus-newsgroup-name           ; From newsgroup
7521           (nth 1 (gnus-find-method-for-group
7522                   gnus-newsgroup-name)) ; Server
7523           (list 'gnus-request-accept-article
7524                 to-newsgroup (list 'quote select-method)
7525                 (not articles) t)       ; Accept form
7526           (not articles)))              ; Only save nov last time
7527         ;; Copy the article.
7528         ((eq action 'copy)
7529          (save-excursion
7530            (set-buffer copy-buf)
7531            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7532              (gnus-request-accept-article
7533               to-newsgroup select-method (not articles) t))))
7534         ;; Crosspost the article.
7535         ((eq action 'crosspost)
7536          (let ((xref (message-tokenize-header
7537                       (mail-header-xref (gnus-summary-article-header article))
7538                       " ")))
7539            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7540                                   ":" article))
7541            (unless xref
7542              (setq xref (list (system-name))))
7543            (setq new-xref
7544                  (concat
7545                   (mapconcat 'identity
7546                              (delete "Xref:" (delete new-xref xref))
7547                              " ")
7548                   " " new-xref))
7549            (save-excursion
7550              (set-buffer copy-buf)
7551              ;; First put the article in the destination group.
7552              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7553              (when (consp (setq art-group
7554                                 (gnus-request-accept-article
7555                                  to-newsgroup select-method (not articles))))
7556                (setq new-xref (concat new-xref " " (car art-group)
7557                                       ":" (cdr art-group)))
7558                ;; Now we have the new Xrefs header, so we insert
7559                ;; it and replace the new article.
7560                (nnheader-replace-header "Xref" new-xref)
7561                (gnus-request-replace-article
7562                 (cdr art-group) to-newsgroup (current-buffer))
7563                art-group))))))
7564       (cond
7565        ((not art-group)
7566         (gnus-message 1 "Couldn't %s article %s: %s"
7567                       (cadr (assq action names)) article
7568                       (nnheader-get-report (car to-method))))
7569        ((and (eq art-group 'junk)
7570              (eq action 'move))
7571         (gnus-summary-mark-article article gnus-canceled-mark)
7572         (gnus-message 4 "Deleted article %s" article))
7573        (t
7574         (let* ((pto-group (gnus-group-prefixed-name
7575                            (car art-group) to-method))
7576                (entry
7577                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7578                (info (nth 2 entry))
7579                (to-group (gnus-info-group info))
7580                to-marks)
7581           ;; Update the group that has been moved to.
7582           (when (and info
7583                      (memq action '(move copy)))
7584             (unless (member to-group to-groups)
7585               (push to-group to-groups))
7586
7587             (unless (memq article gnus-newsgroup-unreads)
7588               (push 'read to-marks)
7589               (gnus-info-set-read
7590                info (gnus-add-to-range (gnus-info-read info)
7591                                        (list (cdr art-group)))))
7592
7593             ;; See whether the article is to be put in the cache.
7594             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7595                              default-marks
7596                            no-expire-marks))
7597                   (to-article (cdr art-group)))
7598
7599               ;; Enter the article into the cache in the new group,
7600               ;; if that is required.
7601               (when gnus-use-cache
7602                 (gnus-cache-possibly-enter-article
7603                  to-group to-article
7604                  (let ((header (copy-sequence
7605                                 (gnus-summary-article-header article))))
7606                    (mail-header-set-number header to-article)
7607                    header)
7608                  (memq article gnus-newsgroup-marked)
7609                  (memq article gnus-newsgroup-dormant)
7610                  (memq article gnus-newsgroup-unreads)))
7611
7612               (when gnus-preserve-marks 
7613                 ;; Copy any marks over to the new group.
7614                 (when (and (equal to-group gnus-newsgroup-name)
7615                            (not (memq article gnus-newsgroup-unreads)))
7616                   ;; Mark this article as read in this group.
7617                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7618                   (setcdr (gnus-active to-group) to-article)
7619                   (setcdr gnus-newsgroup-active to-article))
7620
7621                 (while marks
7622                   (when (memq article (symbol-value
7623                                        (intern (format "gnus-newsgroup-%s"
7624                                                        (caar marks)))))
7625                     (push (cdar marks) to-marks)
7626                     ;; If the other group is the same as this group,
7627                     ;; then we have to add the mark to the list.
7628                     (when (equal to-group gnus-newsgroup-name)
7629                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7630                            (cons to-article
7631                                  (symbol-value
7632                                   (intern (format "gnus-newsgroup-%s"
7633                                                   (caar marks)))))))
7634                     ;; Copy the marks to other group.
7635                     (gnus-add-marked-articles
7636                      to-group (cdar marks) (list to-article) info))
7637                   (setq marks (cdr marks)))
7638
7639                 (gnus-request-set-mark to-group (list (list (list to-article)
7640                                                             'set
7641                                                             to-marks))))
7642
7643               (gnus-dribble-enter
7644                (concat "(gnus-group-set-info '"
7645                        (gnus-prin1-to-string (gnus-get-info to-group))
7646                        ")"))))
7647
7648           ;; Update the Xref header in this article to point to
7649           ;; the new crossposted article we have just created.
7650           (when (eq action 'crosspost)
7651             (save-excursion
7652               (set-buffer copy-buf)
7653               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7654               (nnheader-replace-header "Xref" new-xref)
7655               (gnus-request-replace-article
7656                article gnus-newsgroup-name (current-buffer)))))
7657
7658         ;;;!!!Why is this necessary?
7659         (set-buffer gnus-summary-buffer)
7660
7661         (gnus-summary-goto-subject article)
7662         (when (eq action 'move)
7663           (gnus-summary-mark-article article gnus-canceled-mark))))
7664       (gnus-summary-remove-process-mark article))
7665     ;; Re-activate all groups that have been moved to.
7666     (while to-groups
7667       (save-excursion
7668         (set-buffer gnus-group-buffer)
7669         (when (gnus-group-goto-group (car to-groups) t)
7670           (gnus-group-get-new-news-this-group 1 t))
7671         (pop to-groups)))
7672
7673     (gnus-kill-buffer copy-buf)
7674     (gnus-summary-position-point)
7675     (gnus-set-mode-line 'summary)))
7676
7677 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7678   "Move the current article to a different newsgroup.
7679 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7680 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7681 re-spool using this method."
7682   (interactive "P")
7683   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7684
7685 (defun gnus-summary-crosspost-article (&optional n)
7686   "Crosspost the current article to some other group."
7687   (interactive "P")
7688   (gnus-summary-move-article n nil nil 'crosspost))
7689
7690 (defcustom gnus-summary-respool-default-method nil
7691   "Default method for respooling an article.
7692 If nil, use to the current newsgroup method."
7693   :type '(choice (gnus-select-method :value (nnml ""))
7694                  (const nil))
7695   :group 'gnus-summary-mail)
7696
7697 (defun gnus-summary-respool-article (&optional n method)
7698   "Respool the current article.
7699 The article will be squeezed through the mail spooling process again,
7700 which means that it will be put in some mail newsgroup or other
7701 depending on `nnmail-split-methods'.
7702 If N is a positive number, respool the N next articles.
7703 If N is a negative number, respool the N previous articles.
7704 If N is nil and any articles have been marked with the process mark,
7705 respool those articles instead.
7706
7707 Respooling can be done both from mail groups and \"real\" newsgroups.
7708 In the former case, the articles in question will be moved from the
7709 current group into whatever groups they are destined to.  In the
7710 latter case, they will be copied into the relevant groups."
7711   (interactive
7712    (list current-prefix-arg
7713          (let* ((methods (gnus-methods-using 'respool))
7714                 (methname
7715                  (symbol-name (or gnus-summary-respool-default-method
7716                                   (car (gnus-find-method-for-group
7717                                         gnus-newsgroup-name)))))
7718                 (method
7719                  (gnus-completing-read
7720                   methname "What backend do you want to use when respooling?"
7721                   methods nil t nil 'gnus-mail-method-history))
7722                 ms)
7723            (cond
7724             ((zerop (length (setq ms (gnus-servers-using-backend
7725                                       (intern method)))))
7726              (list (intern method) ""))
7727             ((= 1 (length ms))
7728              (car ms))
7729             (t
7730              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7731                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7732                            ms-alist))))))))
7733   (unless method
7734     (error "No method given for respooling"))
7735   (if (assoc (symbol-name
7736               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7737              (gnus-methods-using 'respool))
7738       (gnus-summary-move-article n nil method)
7739     (gnus-summary-copy-article n nil method)))
7740
7741 (defun gnus-summary-import-article (file)
7742   "Import an arbitrary file into a mail newsgroup."
7743   (interactive "fImport file: ")
7744   (let ((group gnus-newsgroup-name)
7745         (now (current-time))
7746         atts lines)
7747     (unless (gnus-check-backend-function 'request-accept-article group)
7748       (error "%s does not support article importing" group))
7749     (or (file-readable-p file)
7750         (not (file-regular-p file))
7751         (error "Can't read %s" file))
7752     (save-excursion
7753       (set-buffer (gnus-get-buffer-create " *import file*"))
7754       (erase-buffer)
7755       (nnheader-insert-file-contents file)
7756       (goto-char (point-min))
7757       (unless (nnheader-article-p)
7758         ;; This doesn't look like an article, so we fudge some headers.
7759         (setq atts (file-attributes file)
7760               lines (count-lines (point-min) (point-max)))
7761         (insert "From: " (read-string "From: ") "\n"
7762                 "Subject: " (read-string "Subject: ") "\n"
7763                 "Date: " (message-make-date (nth 5 atts))
7764                 "\n"
7765                 "Message-ID: " (message-make-message-id) "\n"
7766                 "Lines: " (int-to-string lines) "\n"
7767                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7768       (gnus-request-accept-article group nil t)
7769       (kill-buffer (current-buffer)))))
7770
7771 (defun gnus-summary-article-posted-p ()
7772   "Say whether the current (mail) article is available from news as well.
7773 This will be the case if the article has both been mailed and posted."
7774   (interactive)
7775   (let ((id (mail-header-references (gnus-summary-article-header)))
7776         (gnus-override-method (car (gnus-refer-article-methods))))
7777     (if (gnus-request-head id "")
7778         (gnus-message 2 "The current message was found on %s"
7779                       gnus-override-method)
7780       (gnus-message 2 "The current message couldn't be found on %s"
7781                     gnus-override-method)
7782       nil)))
7783
7784 (defun gnus-summary-expire-articles (&optional now)
7785   "Expire all articles that are marked as expirable in the current group."
7786   (interactive)
7787   (when (gnus-check-backend-function
7788          'request-expire-articles gnus-newsgroup-name)
7789     ;; This backend supports expiry.
7790     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7791            (expirable (if total
7792                           (progn
7793                             ;; We need to update the info for
7794                             ;; this group for `gnus-list-of-read-articles'
7795                             ;; to give us the right answer.
7796                             (gnus-run-hooks 'gnus-exit-group-hook)
7797                             (gnus-summary-update-info)
7798                             (gnus-list-of-read-articles gnus-newsgroup-name))
7799                         (setq gnus-newsgroup-expirable
7800                               (sort gnus-newsgroup-expirable '<))))
7801            (expiry-wait (if now 'immediate
7802                           (gnus-group-find-parameter
7803                            gnus-newsgroup-name 'expiry-wait)))
7804            es)
7805       (when expirable
7806         ;; There are expirable articles in this group, so we run them
7807         ;; through the expiry process.
7808         (gnus-message 6 "Expiring articles...")
7809         (unless (gnus-check-group gnus-newsgroup-name)
7810           (error "Can't open server for %s" gnus-newsgroup-name))
7811         ;; The list of articles that weren't expired is returned.
7812         (save-excursion
7813           (if expiry-wait
7814               (let ((nnmail-expiry-wait-function nil)
7815                     (nnmail-expiry-wait expiry-wait))
7816                 (setq es (gnus-request-expire-articles
7817                           expirable gnus-newsgroup-name)))
7818             (setq es (gnus-request-expire-articles
7819                       expirable gnus-newsgroup-name))))
7820         (unless total
7821           (setq gnus-newsgroup-expirable es))
7822         ;; We go through the old list of expirable, and mark all
7823         ;; really expired articles as nonexistent.
7824         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7825           (let ((gnus-use-cache nil))
7826             (while expirable
7827               (unless (memq (car expirable) es)
7828                 (when (gnus-data-find (car expirable))
7829                   (gnus-summary-mark-article
7830                    (car expirable) gnus-canceled-mark)))
7831               (setq expirable (cdr expirable)))))
7832         (gnus-message 6 "Expiring articles...done")))))
7833
7834 (defun gnus-summary-expire-articles-now ()
7835   "Expunge all expirable articles in the current group.
7836 This means that *all* articles that are marked as expirable will be
7837 deleted forever, right now."
7838   (interactive)
7839   (or gnus-expert-user
7840       (gnus-yes-or-no-p
7841        "Are you really, really, really sure you want to delete all these messages? ")
7842       (error "Phew!"))
7843   (gnus-summary-expire-articles t))
7844
7845 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7846 (defun gnus-summary-delete-article (&optional n)
7847   "Delete the N next (mail) articles.
7848 This command actually deletes articles.  This is not a marking
7849 command.  The article will disappear forever from your life, never to
7850 return.
7851 If N is negative, delete backwards.
7852 If N is nil and articles have been marked with the process mark,
7853 delete these instead."
7854   (interactive "P")
7855   (unless (gnus-check-backend-function 'request-expire-articles
7856                                        gnus-newsgroup-name)
7857     (error "The current newsgroup does not support article deletion"))
7858   ;; Compute the list of articles to delete.
7859   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7860         not-deleted)
7861     (if (and gnus-novice-user
7862              (not (gnus-yes-or-no-p
7863                    (format "Do you really want to delete %s forever? "
7864                            (if (> (length articles) 1)
7865                                (format "these %s articles" (length articles))
7866                              "this article")))))
7867         ()
7868       ;; Delete the articles.
7869       (setq not-deleted (gnus-request-expire-articles
7870                          articles gnus-newsgroup-name 'force))
7871       (while articles
7872         (gnus-summary-remove-process-mark (car articles))
7873         ;; The backend might not have been able to delete the article
7874         ;; after all.
7875         (unless (memq (car articles) not-deleted)
7876           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7877         (setq articles (cdr articles)))
7878       (when not-deleted
7879         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7880     (gnus-summary-position-point)
7881     (gnus-set-mode-line 'summary)
7882     not-deleted))
7883
7884 (defun gnus-summary-edit-article (&optional force)
7885   "Edit the current article.
7886 This will have permanent effect only in mail groups.
7887 If FORCE is non-nil, allow editing of articles even in read-only
7888 groups."
7889   (interactive "P")
7890   (save-excursion
7891     (set-buffer gnus-summary-buffer)
7892     (let ((mail-parse-charset gnus-newsgroup-charset)
7893           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7894       (gnus-set-global-variables)
7895       (when (and (not force)
7896                  (gnus-group-read-only-p))
7897         (error "The current newsgroup does not support article editing"))
7898       (gnus-summary-show-article t)
7899       (gnus-article-edit-article
7900        'ignore
7901        `(lambda (no-highlight)
7902           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7903                 (mail-parse-ignored-charsets
7904                  ',gnus-newsgroup-ignored-charsets))
7905             (gnus-summary-edit-article-done
7906              ,(or (mail-header-references gnus-current-headers) "")
7907              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7908
7909 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7910
7911 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7912                                                  no-highlight)
7913   "Make edits to the current article permanent."
7914   (interactive)
7915   ;; Replace the article.
7916   (let ((buf (current-buffer)))
7917     (with-temp-buffer
7918       (insert-buffer-substring buf)
7919       (if (and (not read-only)
7920                (not (gnus-request-replace-article
7921                      (cdr gnus-article-current) (car gnus-article-current)
7922                      (current-buffer) t)))
7923           (error "Couldn't replace article")
7924         ;; Update the summary buffer.
7925         (if (and references
7926                  (equal (message-tokenize-header references " ")
7927                         (message-tokenize-header
7928                          (or (message-fetch-field "references") "") " ")))
7929             ;; We only have to update this line.
7930             (save-excursion
7931               (save-restriction
7932                 (message-narrow-to-head)
7933                 (let ((head (buffer-string))
7934                       header)
7935                   (with-temp-buffer
7936                     (insert (format "211 %d Article retrieved.\n"
7937                                     (cdr gnus-article-current)))
7938                     (insert head)
7939                     (insert ".\n")
7940                     (let ((nntp-server-buffer (current-buffer)))
7941                       (setq header (car (gnus-get-newsgroup-headers
7942                                          (save-excursion
7943                                            (set-buffer gnus-summary-buffer)
7944                                            gnus-newsgroup-dependencies)
7945                                          t))))
7946                     (save-excursion
7947                       (set-buffer gnus-summary-buffer)
7948                       (gnus-data-set-header
7949                        (gnus-data-find (cdr gnus-article-current))
7950                        header)
7951                       (gnus-summary-update-article-line
7952                        (cdr gnus-article-current) header))))))
7953           ;; Update threads.
7954           (set-buffer (or buffer gnus-summary-buffer))
7955           (gnus-summary-update-article (cdr gnus-article-current)))
7956         ;; Prettify the article buffer again.
7957         (unless no-highlight
7958           (save-excursion
7959             (set-buffer gnus-article-buffer)
7960             ;;;!!! Fix this -- article should be rehighlighted.
7961             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7962             (set-buffer gnus-original-article-buffer)
7963             (gnus-request-article
7964              (cdr gnus-article-current)
7965              (car gnus-article-current) (current-buffer))))
7966         ;; Prettify the summary buffer line.
7967         (when (gnus-visual-p 'summary-highlight 'highlight)
7968           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7969
7970 (defun gnus-summary-edit-wash (key)
7971   "Perform editing command KEY in the article buffer."
7972   (interactive
7973    (list
7974     (progn
7975       (message "%s" (concat (this-command-keys) "- "))
7976       (read-char))))
7977   (message "")
7978   (gnus-summary-edit-article)
7979   (execute-kbd-macro (concat (this-command-keys) key))
7980   (gnus-article-edit-done))
7981
7982 ;;; Respooling
7983
7984 (defun gnus-summary-respool-query (&optional silent trace)
7985   "Query where the respool algorithm would put this article."
7986   (interactive)
7987   (let (gnus-mark-article-hook)
7988     (gnus-summary-select-article)
7989     (save-excursion
7990       (set-buffer gnus-original-article-buffer)
7991       (save-restriction
7992         (message-narrow-to-head)
7993         (let ((groups (nnmail-article-group 'identity trace)))
7994           (unless silent
7995             (if groups
7996                 (message "This message would go to %s"
7997                          (mapconcat 'car groups ", "))
7998               (message "This message would go to no groups"))
7999             groups))))))
8000
8001 (defun gnus-summary-respool-trace ()
8002   "Trace where the respool algorithm would put this article.
8003 Display a buffer showing all fancy splitting patterns which matched."
8004   (interactive)
8005   (gnus-summary-respool-query nil t))
8006
8007 ;; Summary marking commands.
8008
8009 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8010   "Mark articles which has the same subject as read, and then select the next.
8011 If UNMARK is positive, remove any kind of mark.
8012 If UNMARK is negative, tick articles."
8013   (interactive "P")
8014   (when unmark
8015     (setq unmark (prefix-numeric-value unmark)))
8016   (let ((count
8017          (gnus-summary-mark-same-subject
8018           (gnus-summary-article-subject) unmark)))
8019     ;; Select next unread article.  If auto-select-same mode, should
8020     ;; select the first unread article.
8021     (gnus-summary-next-article t (and gnus-auto-select-same
8022                                       (gnus-summary-article-subject)))
8023     (gnus-message 7 "%d article%s marked as %s"
8024                   count (if (= count 1) " is" "s are")
8025                   (if unmark "unread" "read"))))
8026
8027 (defun gnus-summary-kill-same-subject (&optional unmark)
8028   "Mark articles which has the same subject as read.
8029 If UNMARK is positive, remove any kind of mark.
8030 If UNMARK is negative, tick articles."
8031   (interactive "P")
8032   (when unmark
8033     (setq unmark (prefix-numeric-value unmark)))
8034   (let ((count
8035          (gnus-summary-mark-same-subject
8036           (gnus-summary-article-subject) unmark)))
8037     ;; If marked as read, go to next unread subject.
8038     (when (null unmark)
8039       ;; Go to next unread subject.
8040       (gnus-summary-next-subject 1 t))
8041     (gnus-message 7 "%d articles are marked as %s"
8042                   count (if unmark "unread" "read"))))
8043
8044 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8045   "Mark articles with same SUBJECT as read, and return marked number.
8046 If optional argument UNMARK is positive, remove any kinds of marks.
8047 If optional argument UNMARK is negative, mark articles as unread instead."
8048   (let ((count 1))
8049     (save-excursion
8050       (cond
8051        ((null unmark)                   ; Mark as read.
8052         (while (and
8053                 (progn
8054                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8055                   (gnus-summary-show-thread) t)
8056                 (gnus-summary-find-subject subject))
8057           (setq count (1+ count))))
8058        ((> unmark 0)                    ; Tick.
8059         (while (and
8060                 (progn
8061                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8062                   (gnus-summary-show-thread) t)
8063                 (gnus-summary-find-subject subject))
8064           (setq count (1+ count))))
8065        (t                               ; Mark as unread.
8066         (while (and
8067                 (progn
8068                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8069                   (gnus-summary-show-thread) t)
8070                 (gnus-summary-find-subject subject))
8071           (setq count (1+ count)))))
8072       (gnus-set-mode-line 'summary)
8073       ;; Return the number of marked articles.
8074       count)))
8075
8076 (defun gnus-summary-mark-as-processable (n &optional unmark)
8077   "Set the process mark on the next N articles.
8078 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8079 the process mark instead.  The difference between N and the actual
8080 number of articles marked is returned."
8081   (interactive "p")
8082   (let ((backward (< n 0))
8083         (n (abs n)))
8084     (while (and
8085             (> n 0)
8086             (if unmark
8087                 (gnus-summary-remove-process-mark
8088                  (gnus-summary-article-number))
8089               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8090             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8091       (setq n (1- n)))
8092     (when (/= 0 n)
8093       (gnus-message 7 "No more articles"))
8094     (gnus-summary-recenter)
8095     (gnus-summary-position-point)
8096     n))
8097
8098 (defun gnus-summary-unmark-as-processable (n)
8099   "Remove the process mark from the next N articles.
8100 If N is negative, unmark backward instead.  The difference between N and
8101 the actual number of articles unmarked is returned."
8102   (interactive "p")
8103   (gnus-summary-mark-as-processable n t))
8104
8105 (defun gnus-summary-unmark-all-processable ()
8106   "Remove the process mark from all articles."
8107   (interactive)
8108   (save-excursion
8109     (while gnus-newsgroup-processable
8110       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8111   (gnus-summary-position-point))
8112
8113 (defun gnus-summary-mark-as-expirable (n)
8114   "Mark N articles forward as expirable.
8115 If N is negative, mark backward instead.  The difference between N and
8116 the actual number of articles marked is returned."
8117   (interactive "p")
8118   (gnus-summary-mark-forward n gnus-expirable-mark))
8119
8120 (defun gnus-summary-mark-article-as-replied (article)
8121   "Mark ARTICLE replied and update the summary line."
8122   (push article gnus-newsgroup-replied)
8123   (let ((buffer-read-only nil))
8124     (when (gnus-summary-goto-subject article nil t)
8125       (gnus-summary-update-secondary-mark article))))
8126
8127 (defun gnus-summary-set-bookmark (article)
8128   "Set a bookmark in current article."
8129   (interactive (list (gnus-summary-article-number)))
8130   (when (or (not (get-buffer gnus-article-buffer))
8131             (not gnus-current-article)
8132             (not gnus-article-current)
8133             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8134     (error "No current article selected"))
8135   ;; Remove old bookmark, if one exists.
8136   (let ((old (assq article gnus-newsgroup-bookmarks)))
8137     (when old
8138       (setq gnus-newsgroup-bookmarks
8139             (delq old gnus-newsgroup-bookmarks))))
8140   ;; Set the new bookmark, which is on the form
8141   ;; (article-number . line-number-in-body).
8142   (push
8143    (cons article
8144          (save-excursion
8145            (set-buffer gnus-article-buffer)
8146            (count-lines
8147             (min (point)
8148                  (save-excursion
8149                    (goto-char (point-min))
8150                    (search-forward "\n\n" nil t)
8151                    (point)))
8152             (point))))
8153    gnus-newsgroup-bookmarks)
8154   (gnus-message 6 "A bookmark has been added to the current article."))
8155
8156 (defun gnus-summary-remove-bookmark (article)
8157   "Remove the bookmark from the current article."
8158   (interactive (list (gnus-summary-article-number)))
8159   ;; Remove old bookmark, if one exists.
8160   (let ((old (assq article gnus-newsgroup-bookmarks)))
8161     (if old
8162         (progn
8163           (setq gnus-newsgroup-bookmarks
8164                 (delq old gnus-newsgroup-bookmarks))
8165           (gnus-message 6 "Removed bookmark."))
8166       (gnus-message 6 "No bookmark in current article."))))
8167
8168 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8169 (defun gnus-summary-mark-as-dormant (n)
8170   "Mark N articles forward as dormant.
8171 If N is negative, mark backward instead.  The difference between N and
8172 the actual number of articles marked is returned."
8173   (interactive "p")
8174   (gnus-summary-mark-forward n gnus-dormant-mark))
8175
8176 (defun gnus-summary-set-process-mark (article)
8177   "Set the process mark on ARTICLE and update the summary line."
8178   (setq gnus-newsgroup-processable
8179         (cons article
8180               (delq article gnus-newsgroup-processable)))
8181   (when (gnus-summary-goto-subject article)
8182     (gnus-summary-show-thread)
8183     (gnus-summary-goto-subject article)
8184     (gnus-summary-update-secondary-mark article)))
8185
8186 (defun gnus-summary-remove-process-mark (article)
8187   "Remove the process mark from ARTICLE and update the summary line."
8188   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8189   (when (gnus-summary-goto-subject article)
8190     (gnus-summary-show-thread)
8191     (gnus-summary-goto-subject article)
8192     (gnus-summary-update-secondary-mark article)))
8193
8194 (defun gnus-summary-set-saved-mark (article)
8195   "Set the process mark on ARTICLE and update the summary line."
8196   (push article gnus-newsgroup-saved)
8197   (when (gnus-summary-goto-subject article)
8198     (gnus-summary-update-secondary-mark article)))
8199
8200 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8201   "Mark N articles as read forwards.
8202 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8203 The difference between N and the actual number of articles marked is
8204 returned.
8205 Iff NO-EXPIRE, auto-expiry will be inhibited."
8206   (interactive "p")
8207   (gnus-summary-show-thread)
8208   (let ((backward (< n 0))
8209         (gnus-summary-goto-unread
8210          (and gnus-summary-goto-unread
8211               (not (eq gnus-summary-goto-unread 'never))
8212               (not (memq mark (list gnus-unread-mark
8213                                     gnus-ticked-mark gnus-dormant-mark)))))
8214         (n (abs n))
8215         (mark (or mark gnus-del-mark)))
8216     (while (and (> n 0)
8217                 (gnus-summary-mark-article nil mark no-expire)
8218                 (zerop (gnus-summary-next-subject
8219                         (if backward -1 1)
8220                         (and gnus-summary-goto-unread
8221                              (not (eq gnus-summary-goto-unread 'never)))
8222                         t)))
8223       (setq n (1- n)))
8224     (when (/= 0 n)
8225       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8226     (gnus-summary-recenter)
8227     (gnus-summary-position-point)
8228     (gnus-set-mode-line 'summary)
8229     n))
8230
8231 (defun gnus-summary-mark-article-as-read (mark)
8232   "Mark the current article quickly as read with MARK."
8233   (let ((article (gnus-summary-article-number)))
8234     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8235     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8236     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8237     (push (cons article mark) gnus-newsgroup-reads)
8238     ;; Possibly remove from cache, if that is used.
8239     (when gnus-use-cache
8240       (gnus-cache-enter-remove-article article))
8241     ;; Allow the backend to change the mark.
8242     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8243     ;; Check for auto-expiry.
8244     (when (and gnus-newsgroup-auto-expire
8245                (memq mark gnus-auto-expirable-marks))
8246       (setq mark gnus-expirable-mark)
8247       ;; Let the backend know about the mark change.
8248       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8249       (push article gnus-newsgroup-expirable))
8250     ;; Set the mark in the buffer.
8251     (gnus-summary-update-mark mark 'unread)
8252     t))
8253
8254 (defun gnus-summary-mark-article-as-unread (mark)
8255   "Mark the current article quickly as unread with MARK."
8256   (let* ((article (gnus-summary-article-number))
8257          (old-mark (gnus-summary-article-mark article)))
8258     ;; Allow the backend to change the mark.
8259     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8260     (if (eq mark old-mark)
8261         t
8262       (if (<= article 0)
8263           (progn
8264             (gnus-error 1 "Can't mark negative article numbers")
8265             nil)
8266         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8267         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8268         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8269         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8270         (cond ((= mark gnus-ticked-mark)
8271                (push article gnus-newsgroup-marked))
8272               ((= mark gnus-dormant-mark)
8273                (push article gnus-newsgroup-dormant))
8274               (t
8275                (push article gnus-newsgroup-unreads)))
8276         (gnus-pull article gnus-newsgroup-reads)
8277
8278         ;; See whether the article is to be put in the cache.
8279         (and gnus-use-cache
8280              (vectorp (gnus-summary-article-header article))
8281              (save-excursion
8282                (gnus-cache-possibly-enter-article
8283                 gnus-newsgroup-name article
8284                 (gnus-summary-article-header article)
8285                 (= mark gnus-ticked-mark)
8286                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8287
8288         ;; Fix the mark.
8289         (gnus-summary-update-mark mark 'unread)
8290         t))))
8291
8292 (defun gnus-summary-mark-article (&optional article mark no-expire)
8293   "Mark ARTICLE with MARK.  MARK can be any character.
8294 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8295 `??' (dormant) and `?E' (expirable).
8296 If MARK is nil, then the default character `?r' is used.
8297 If ARTICLE is nil, then the article on the current line will be
8298 marked.
8299 Iff NO-EXPIRE, auto-expiry will be inhibited."
8300   ;; The mark might be a string.
8301   (when (stringp mark)
8302     (setq mark (aref mark 0)))
8303   ;; If no mark is given, then we check auto-expiring.
8304   (when (null mark)
8305     (setq mark gnus-del-mark))
8306   (when (and (not no-expire)
8307              gnus-newsgroup-auto-expire
8308              (memq mark gnus-auto-expirable-marks))
8309     (setq mark gnus-expirable-mark))
8310   (let ((article (or article (gnus-summary-article-number)))
8311         (old-mark (gnus-summary-article-mark article)))
8312     ;; Allow the backend to change the mark.
8313     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8314     (if (eq mark old-mark)
8315         t
8316       (unless article
8317         (error "No article on current line"))
8318       (if (not (if (or (= mark gnus-unread-mark)
8319                        (= mark gnus-ticked-mark)
8320                        (= mark gnus-dormant-mark))
8321                    (gnus-mark-article-as-unread article mark)
8322                  (gnus-mark-article-as-read article mark)))
8323           t
8324         ;; See whether the article is to be put in the cache.
8325         (and gnus-use-cache
8326              (not (= mark gnus-canceled-mark))
8327              (vectorp (gnus-summary-article-header article))
8328              (save-excursion
8329                (gnus-cache-possibly-enter-article
8330                 gnus-newsgroup-name article
8331                 (gnus-summary-article-header article)
8332                 (= mark gnus-ticked-mark)
8333                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8334
8335         (when (gnus-summary-goto-subject article nil t)
8336           (let ((buffer-read-only nil))
8337             (gnus-summary-show-thread)
8338             ;; Fix the mark.
8339             (gnus-summary-update-mark mark 'unread)
8340             t))))))
8341
8342 (defun gnus-summary-update-secondary-mark (article)
8343   "Update the secondary (read, process, cache) mark."
8344   (gnus-summary-update-mark
8345    (cond ((memq article gnus-newsgroup-processable)
8346           gnus-process-mark)
8347          ((memq article gnus-newsgroup-cached)
8348           gnus-cached-mark)
8349          ((memq article gnus-newsgroup-replied)
8350           gnus-replied-mark)
8351          ((memq article gnus-newsgroup-saved)
8352           gnus-saved-mark)
8353          (t gnus-unread-mark))
8354    'replied)
8355   (when (gnus-visual-p 'summary-highlight 'highlight)
8356     (gnus-run-hooks 'gnus-summary-update-hook))
8357   t)
8358
8359 (defun gnus-summary-update-mark (mark type)
8360   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8361         (buffer-read-only nil))
8362     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8363     (when forward
8364       (when (looking-at "\r")
8365         (incf forward))
8366       (when (<= (+ forward (point)) (point-max))
8367         ;; Go to the right position on the line.
8368         (goto-char (+ forward (point)))
8369         ;; Replace the old mark with the new mark.
8370         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8371         ;; Optionally update the marks by some user rule.
8372         (when (eq type 'unread)
8373           (gnus-data-set-mark
8374            (gnus-data-find (gnus-summary-article-number)) mark)
8375           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8376
8377 (defun gnus-mark-article-as-read (article &optional mark)
8378   "Enter ARTICLE in the pertinent lists and remove it from others."
8379   ;; Make the article expirable.
8380   (let ((mark (or mark gnus-del-mark)))
8381     (if (= mark gnus-expirable-mark)
8382         (push article gnus-newsgroup-expirable)
8383       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8384     ;; Remove from unread and marked lists.
8385     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8386     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8387     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8388     (push (cons article mark) gnus-newsgroup-reads)
8389     ;; Possibly remove from cache, if that is used.
8390     (when gnus-use-cache
8391       (gnus-cache-enter-remove-article article))
8392     t))
8393
8394 (defun gnus-mark-article-as-unread (article &optional mark)
8395   "Enter ARTICLE in the pertinent lists and remove it from others."
8396   (let ((mark (or mark gnus-ticked-mark)))
8397     (if (<= article 0)
8398         (progn
8399           (gnus-error 1 "Can't mark negative article numbers")
8400           nil)
8401       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8402             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8403             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8404             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8405
8406       ;; Unsuppress duplicates?
8407       (when gnus-suppress-duplicates
8408         (gnus-dup-unsuppress-article article))
8409
8410       (cond ((= mark gnus-ticked-mark)
8411              (push article gnus-newsgroup-marked))
8412             ((= mark gnus-dormant-mark)
8413              (push article gnus-newsgroup-dormant))
8414             (t
8415              (push article gnus-newsgroup-unreads)))
8416       (gnus-pull article gnus-newsgroup-reads)
8417       t)))
8418
8419 (defalias 'gnus-summary-mark-as-unread-forward
8420   'gnus-summary-tick-article-forward)
8421 (make-obsolete 'gnus-summary-mark-as-unread-forward
8422                'gnus-summary-tick-article-forward)
8423 (defun gnus-summary-tick-article-forward (n)
8424   "Tick N articles forwards.
8425 If N is negative, tick backwards instead.
8426 The difference between N and the number of articles ticked is returned."
8427   (interactive "p")
8428   (gnus-summary-mark-forward n gnus-ticked-mark))
8429
8430 (defalias 'gnus-summary-mark-as-unread-backward
8431   'gnus-summary-tick-article-backward)
8432 (make-obsolete 'gnus-summary-mark-as-unread-backward
8433                'gnus-summary-tick-article-backward)
8434 (defun gnus-summary-tick-article-backward (n)
8435   "Tick N articles backwards.
8436 The difference between N and the number of articles ticked is returned."
8437   (interactive "p")
8438   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8439
8440 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8441 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8442 (defun gnus-summary-tick-article (&optional article clear-mark)
8443   "Mark current article as unread.
8444 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8445 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8446   (interactive)
8447   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8448                                        gnus-ticked-mark)))
8449
8450 (defun gnus-summary-mark-as-read-forward (n)
8451   "Mark N articles as read forwards.
8452 If N is negative, mark backwards instead.
8453 The difference between N and the actual number of articles marked is
8454 returned."
8455   (interactive "p")
8456   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8457
8458 (defun gnus-summary-mark-as-read-backward (n)
8459   "Mark the N articles as read backwards.
8460 The difference between N and the actual number of articles marked is
8461 returned."
8462   (interactive "p")
8463   (gnus-summary-mark-forward
8464    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8465
8466 (defun gnus-summary-mark-as-read (&optional article mark)
8467   "Mark current article as read.
8468 ARTICLE specifies the article to be marked as read.
8469 MARK specifies a string to be inserted at the beginning of the line."
8470   (gnus-summary-mark-article article mark))
8471
8472 (defun gnus-summary-clear-mark-forward (n)
8473   "Clear marks from N articles forward.
8474 If N is negative, clear backward instead.
8475 The difference between N and the number of marks cleared is returned."
8476   (interactive "p")
8477   (gnus-summary-mark-forward n gnus-unread-mark))
8478
8479 (defun gnus-summary-clear-mark-backward (n)
8480   "Clear marks from N articles backward.
8481 The difference between N and the number of marks cleared is returned."
8482   (interactive "p")
8483   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8484
8485 (defun gnus-summary-mark-unread-as-read ()
8486   "Intended to be used by `gnus-summary-mark-article-hook'."
8487   (when (memq gnus-current-article gnus-newsgroup-unreads)
8488     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8489
8490 (defun gnus-summary-mark-read-and-unread-as-read ()
8491   "Intended to be used by `gnus-summary-mark-article-hook'."
8492   (let ((mark (gnus-summary-article-mark)))
8493     (when (or (gnus-unread-mark-p mark)
8494               (gnus-read-mark-p mark))
8495       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8496
8497 (defun gnus-summary-mark-region-as-read (point mark all)
8498   "Mark all unread articles between point and mark as read.
8499 If given a prefix, mark all articles between point and mark as read,
8500 even ticked and dormant ones."
8501   (interactive "r\nP")
8502   (save-excursion
8503     (let (article)
8504       (goto-char point)
8505       (beginning-of-line)
8506       (while (and
8507               (< (point) mark)
8508               (progn
8509                 (when (or all
8510                           (memq (setq article (gnus-summary-article-number))
8511                                 gnus-newsgroup-unreads))
8512                   (gnus-summary-mark-article article gnus-del-mark))
8513                 t)
8514               (gnus-summary-find-next))))))
8515
8516 (defun gnus-summary-mark-below (score mark)
8517   "Mark articles with score less than SCORE with MARK."
8518   (interactive "P\ncMark: ")
8519   (setq score (if score
8520                   (prefix-numeric-value score)
8521                 (or gnus-summary-default-score 0)))
8522   (save-excursion
8523     (set-buffer gnus-summary-buffer)
8524     (goto-char (point-min))
8525     (while
8526         (progn
8527           (and (< (gnus-summary-article-score) score)
8528                (gnus-summary-mark-article nil mark))
8529           (gnus-summary-find-next)))))
8530
8531 (defun gnus-summary-kill-below (&optional score)
8532   "Mark articles with score below SCORE as read."
8533   (interactive "P")
8534   (gnus-summary-mark-below score gnus-killed-mark))
8535
8536 (defun gnus-summary-clear-above (&optional score)
8537   "Clear all marks from articles with score above SCORE."
8538   (interactive "P")
8539   (gnus-summary-mark-above score gnus-unread-mark))
8540
8541 (defun gnus-summary-tick-above (&optional score)
8542   "Tick all articles with score above SCORE."
8543   (interactive "P")
8544   (gnus-summary-mark-above score gnus-ticked-mark))
8545
8546 (defun gnus-summary-mark-above (score mark)
8547   "Mark articles with score over SCORE with MARK."
8548   (interactive "P\ncMark: ")
8549   (setq score (if score
8550                   (prefix-numeric-value score)
8551                 (or gnus-summary-default-score 0)))
8552   (save-excursion
8553     (set-buffer gnus-summary-buffer)
8554     (goto-char (point-min))
8555     (while (and (progn
8556                   (when (> (gnus-summary-article-score) score)
8557                     (gnus-summary-mark-article nil mark))
8558                   t)
8559                 (gnus-summary-find-next)))))
8560
8561 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8562 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8563 (defun gnus-summary-limit-include-expunged (&optional no-error)
8564   "Display all the hidden articles that were expunged for low scores."
8565   (interactive)
8566   (let ((buffer-read-only nil))
8567     (let ((scored gnus-newsgroup-scored)
8568           headers h)
8569       (while scored
8570         (unless (gnus-summary-goto-subject (caar scored))
8571           (and (setq h (gnus-summary-article-header (caar scored)))
8572                (< (cdar scored) gnus-summary-expunge-below)
8573                (push h headers)))
8574         (setq scored (cdr scored)))
8575       (if (not headers)
8576           (when (not no-error)
8577             (error "No expunged articles hidden"))
8578         (goto-char (point-min))
8579         (gnus-summary-prepare-unthreaded (nreverse headers))
8580         (goto-char (point-min))
8581         (gnus-summary-position-point)
8582         t))))
8583
8584 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8585   "Mark all unread articles in this newsgroup as read.
8586 If prefix argument ALL is non-nil, ticked and dormant articles will
8587 also be marked as read.
8588 If QUIETLY is non-nil, no questions will be asked.
8589 If TO-HERE is non-nil, it should be a point in the buffer.  All
8590 articles before this point will be marked as read.
8591 Note that this function will only catch up the unread article
8592 in the current summary buffer limitation.
8593 The number of articles marked as read is returned."
8594   (interactive "P")
8595   (prog1
8596       (save-excursion
8597         (when (or quietly
8598                   (not gnus-interactive-catchup) ;Without confirmation?
8599                   gnus-expert-user
8600                   (gnus-y-or-n-p
8601                    (if all
8602                        "Mark absolutely all articles as read? "
8603                      "Mark all unread articles as read? ")))
8604           (if (and not-mark
8605                    (not gnus-newsgroup-adaptive)
8606                    (not gnus-newsgroup-auto-expire)
8607                    (not gnus-suppress-duplicates)
8608                    (or (not gnus-use-cache)
8609                        (eq gnus-use-cache 'passive)))
8610               (progn
8611                 (when all
8612                   (setq gnus-newsgroup-marked nil
8613                         gnus-newsgroup-dormant nil))
8614                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8615             ;; We actually mark all articles as canceled, which we
8616             ;; have to do when using auto-expiry or adaptive scoring.
8617             (gnus-summary-show-all-threads)
8618             (when (gnus-summary-first-subject (not all) t)
8619               (while (and
8620                       (if to-here (< (point) to-here) t)
8621                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8622                       (gnus-summary-find-next (not all) nil nil t))))
8623             (gnus-set-mode-line 'summary))
8624           t))
8625     (gnus-summary-position-point)))
8626
8627 (defun gnus-summary-catchup-to-here (&optional all)
8628   "Mark all unticked articles before the current one as read.
8629 If ALL is non-nil, also mark ticked and dormant articles as read."
8630   (interactive "P")
8631   (save-excursion
8632     (gnus-save-hidden-threads
8633       (let ((beg (point)))
8634         ;; We check that there are unread articles.
8635         (when (or all (gnus-summary-find-prev))
8636           (gnus-summary-catchup all t beg)))))
8637   (gnus-summary-position-point))
8638
8639 (defun gnus-summary-catchup-all (&optional quietly)
8640   "Mark all articles in this newsgroup as read."
8641   (interactive "P")
8642   (gnus-summary-catchup t quietly))
8643
8644 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8645   "Mark all unread articles in this group as read, then exit.
8646 If prefix argument ALL is non-nil, all articles are marked as read."
8647   (interactive "P")
8648   (when (gnus-summary-catchup all quietly nil 'fast)
8649     ;; Select next newsgroup or exit.
8650     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8651              (eq gnus-auto-select-next 'quietly))
8652         (gnus-summary-next-group nil)
8653       (gnus-summary-exit))))
8654
8655 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8656   "Mark all articles in this newsgroup as read, and then exit."
8657   (interactive "P")
8658   (gnus-summary-catchup-and-exit t quietly))
8659
8660 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8661   "Mark all articles in this group as read and select the next group.
8662 If given a prefix, mark all articles, unread as well as ticked, as
8663 read."
8664   (interactive "P")
8665   (save-excursion
8666     (gnus-summary-catchup all))
8667   (gnus-summary-next-group))
8668
8669 ;; Thread-based commands.
8670
8671 (defun gnus-summary-articles-in-thread (&optional article)
8672   "Return a list of all articles in the current thread.
8673 If ARTICLE is non-nil, return all articles in the thread that starts
8674 with that article."
8675   (let* ((article (or article (gnus-summary-article-number)))
8676          (data (gnus-data-find-list article))
8677          (top-level (gnus-data-level (car data)))
8678          (top-subject
8679           (cond ((null gnus-thread-operation-ignore-subject)
8680                  (gnus-simplify-subject-re
8681                   (mail-header-subject (gnus-data-header (car data)))))
8682                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8683                  (gnus-simplify-subject-fuzzy
8684                   (mail-header-subject (gnus-data-header (car data)))))
8685                 (t nil)))
8686          (end-point (save-excursion
8687                       (if (gnus-summary-go-to-next-thread)
8688                           (point) (point-max))))
8689          articles)
8690     (while (and data
8691                 (< (gnus-data-pos (car data)) end-point))
8692       (when (or (not top-subject)
8693                 (string= top-subject
8694                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8695                              (gnus-simplify-subject-fuzzy
8696                               (mail-header-subject
8697                                (gnus-data-header (car data))))
8698                            (gnus-simplify-subject-re
8699                             (mail-header-subject
8700                              (gnus-data-header (car data)))))))
8701         (push (gnus-data-number (car data)) articles))
8702       (unless (and (setq data (cdr data))
8703                    (> (gnus-data-level (car data)) top-level))
8704         (setq data nil)))
8705     ;; Return the list of articles.
8706     (nreverse articles)))
8707
8708 (defun gnus-summary-rethread-current ()
8709   "Rethread the thread the current article is part of."
8710   (interactive)
8711   (let* ((gnus-show-threads t)
8712          (article (gnus-summary-article-number))
8713          (id (mail-header-id (gnus-summary-article-header)))
8714          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8715     (unless id
8716       (error "No article on the current line"))
8717     (gnus-rebuild-thread id)
8718     (gnus-summary-goto-subject article)))
8719
8720 (defun gnus-summary-reparent-thread ()
8721   "Make the current article child of the marked (or previous) article.
8722
8723 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8724 is non-nil or the Subject: of both articles are the same."
8725   (interactive)
8726   (unless (not (gnus-group-read-only-p))
8727     (error "The current newsgroup does not support article editing"))
8728   (unless (<= (length gnus-newsgroup-processable) 1)
8729     (error "No more than one article may be marked"))
8730   (save-window-excursion
8731     (let ((gnus-article-buffer " *reparent*")
8732           (current-article (gnus-summary-article-number))
8733           ;; First grab the marked article, otherwise one line up.
8734           (parent-article (if (not (null gnus-newsgroup-processable))
8735                               (car gnus-newsgroup-processable)
8736                             (save-excursion
8737                               (if (eq (forward-line -1) 0)
8738                                   (gnus-summary-article-number)
8739                                 (error "Beginning of summary buffer"))))))
8740       (unless (not (eq current-article parent-article))
8741         (error "An article may not be self-referential"))
8742       (let ((message-id (mail-header-id
8743                          (gnus-summary-article-header parent-article))))
8744         (unless (and message-id (not (equal message-id "")))
8745           (error "No message-id in desired parent"))
8746         (gnus-with-article current-article
8747           (goto-char (point-min))
8748           (if (re-search-forward "^References: " nil t)
8749               (progn
8750                 (re-search-forward "^[^ \t]" nil t)
8751                 (forward-line -1)
8752                 (end-of-line)
8753                 (insert " " message-id))
8754             (insert "References: " message-id "\n")))
8755         (set-buffer gnus-summary-buffer)
8756         (gnus-summary-unmark-all-processable)
8757         (gnus-summary-update-article current-article)
8758         (gnus-summary-rethread-current)
8759         (gnus-message 3 "Article %d is now the child of article %d"
8760                       current-article parent-article)))))
8761
8762 (defun gnus-summary-toggle-threads (&optional arg)
8763   "Toggle showing conversation threads.
8764 If ARG is positive number, turn showing conversation threads on."
8765   (interactive "P")
8766   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8767     (setq gnus-show-threads
8768           (if (null arg) (not gnus-show-threads)
8769             (> (prefix-numeric-value arg) 0)))
8770     (gnus-summary-prepare)
8771     (gnus-summary-goto-subject current)
8772     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8773     (gnus-summary-position-point)))
8774
8775 (defun gnus-summary-show-all-threads ()
8776   "Show all threads."
8777   (interactive)
8778   (save-excursion
8779     (let ((buffer-read-only nil))
8780       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8781   (gnus-summary-position-point))
8782
8783 (defun gnus-summary-show-thread ()
8784   "Show thread subtrees.
8785 Returns nil if no thread was there to be shown."
8786   (interactive)
8787   (let ((buffer-read-only nil)
8788         (orig (point))
8789         ;; first goto end then to beg, to have point at beg after let
8790         (end (progn (end-of-line) (point)))
8791         (beg (progn (beginning-of-line) (point))))
8792     (prog1
8793         ;; Any hidden lines here?
8794         (search-forward "\r" end t)
8795       (subst-char-in-region beg end ?\^M ?\n t)
8796       (goto-char orig)
8797       (gnus-summary-position-point))))
8798
8799 (defun gnus-summary-hide-all-threads ()
8800   "Hide all thread subtrees."
8801   (interactive)
8802   (save-excursion
8803     (goto-char (point-min))
8804     (gnus-summary-hide-thread)
8805     (while (zerop (gnus-summary-next-thread 1 t))
8806       (gnus-summary-hide-thread)))
8807   (gnus-summary-position-point))
8808
8809 (defun gnus-summary-hide-thread ()
8810   "Hide thread subtrees.
8811 Returns nil if no threads were there to be hidden."
8812   (interactive)
8813   (let ((buffer-read-only nil)
8814         (start (point))
8815         (article (gnus-summary-article-number)))
8816     (goto-char start)
8817     ;; Go forward until either the buffer ends or the subthread
8818     ;; ends.
8819     (when (and (not (eobp))
8820                (or (zerop (gnus-summary-next-thread 1 t))
8821                    (goto-char (point-max))))
8822       (prog1
8823           (if (and (> (point) start)
8824                    (search-backward "\n" start t))
8825               (progn
8826                 (subst-char-in-region start (point) ?\n ?\^M)
8827                 (gnus-summary-goto-subject article))
8828             (goto-char start)
8829             nil)))))
8830
8831 (defun gnus-summary-go-to-next-thread (&optional previous)
8832   "Go to the same level (or less) next thread.
8833 If PREVIOUS is non-nil, go to previous thread instead.
8834 Return the article number moved to, or nil if moving was impossible."
8835   (let ((level (gnus-summary-thread-level))
8836         (way (if previous -1 1))
8837         (beg (point)))
8838     (forward-line way)
8839     (while (and (not (eobp))
8840                 (< level (gnus-summary-thread-level)))
8841       (forward-line way))
8842     (if (eobp)
8843         (progn
8844           (goto-char beg)
8845           nil)
8846       (setq beg (point))
8847       (prog1
8848           (gnus-summary-article-number)
8849         (goto-char beg)))))
8850
8851 (defun gnus-summary-next-thread (n &optional silent)
8852   "Go to the same level next N'th thread.
8853 If N is negative, search backward instead.
8854 Returns the difference between N and the number of skips actually
8855 done.
8856
8857 If SILENT, don't output messages."
8858   (interactive "p")
8859   (let ((backward (< n 0))
8860         (n (abs n)))
8861     (while (and (> n 0)
8862                 (gnus-summary-go-to-next-thread backward))
8863       (decf n))
8864     (unless silent
8865       (gnus-summary-position-point))
8866     (when (and (not silent) (/= 0 n))
8867       (gnus-message 7 "No more threads"))
8868     n))
8869
8870 (defun gnus-summary-prev-thread (n)
8871   "Go to the same level previous N'th thread.
8872 Returns the difference between N and the number of skips actually
8873 done."
8874   (interactive "p")
8875   (gnus-summary-next-thread (- n)))
8876
8877 (defun gnus-summary-go-down-thread ()
8878   "Go down one level in the current thread."
8879   (let ((children (gnus-summary-article-children)))
8880     (when children
8881       (gnus-summary-goto-subject (car children)))))
8882
8883 (defun gnus-summary-go-up-thread ()
8884   "Go up one level in the current thread."
8885   (let ((parent (gnus-summary-article-parent)))
8886     (when parent
8887       (gnus-summary-goto-subject parent))))
8888
8889 (defun gnus-summary-down-thread (n)
8890   "Go down thread N steps.
8891 If N is negative, go up instead.
8892 Returns the difference between N and how many steps down that were
8893 taken."
8894   (interactive "p")
8895   (let ((up (< n 0))
8896         (n (abs n)))
8897     (while (and (> n 0)
8898                 (if up (gnus-summary-go-up-thread)
8899                   (gnus-summary-go-down-thread)))
8900       (setq n (1- n)))
8901     (gnus-summary-position-point)
8902     (when (/= 0 n)
8903       (gnus-message 7 "Can't go further"))
8904     n))
8905
8906 (defun gnus-summary-up-thread (n)
8907   "Go up thread N steps.
8908 If N is negative, go up instead.
8909 Returns the difference between N and how many steps down that were
8910 taken."
8911   (interactive "p")
8912   (gnus-summary-down-thread (- n)))
8913
8914 (defun gnus-summary-top-thread ()
8915   "Go to the top of the thread."
8916   (interactive)
8917   (while (gnus-summary-go-up-thread))
8918   (gnus-summary-article-number))
8919
8920 (defun gnus-summary-kill-thread (&optional unmark)
8921   "Mark articles under current thread as read.
8922 If the prefix argument is positive, remove any kinds of marks.
8923 If the prefix argument is negative, tick articles instead."
8924   (interactive "P")
8925   (when unmark
8926     (setq unmark (prefix-numeric-value unmark)))
8927   (let ((articles (gnus-summary-articles-in-thread)))
8928     (save-excursion
8929       ;; Expand the thread.
8930       (gnus-summary-show-thread)
8931       ;; Mark all the articles.
8932       (while articles
8933         (gnus-summary-goto-subject (car articles))
8934         (cond ((null unmark)
8935                (gnus-summary-mark-article-as-read gnus-killed-mark))
8936               ((> unmark 0)
8937                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8938               (t
8939                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8940         (setq articles (cdr articles))))
8941     ;; Hide killed subtrees.
8942     (and (null unmark)
8943          gnus-thread-hide-killed
8944          (gnus-summary-hide-thread))
8945     ;; If marked as read, go to next unread subject.
8946     (when (null unmark)
8947       ;; Go to next unread subject.
8948       (gnus-summary-next-subject 1 t)))
8949   (gnus-set-mode-line 'summary))
8950
8951 ;; Summary sorting commands
8952
8953 (defun gnus-summary-sort-by-number (&optional reverse)
8954   "Sort the summary buffer by article number.
8955 Argument REVERSE means reverse order."
8956   (interactive "P")
8957   (gnus-summary-sort 'number reverse))
8958
8959 (defun gnus-summary-sort-by-author (&optional reverse)
8960   "Sort the summary buffer by author name alphabetically.
8961 If case-fold-search is non-nil, case of letters is ignored.
8962 Argument REVERSE means reverse order."
8963   (interactive "P")
8964   (gnus-summary-sort 'author reverse))
8965
8966 (defun gnus-summary-sort-by-subject (&optional reverse)
8967   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8968 If case-fold-search is non-nil, case of letters is ignored.
8969 Argument REVERSE means reverse order."
8970   (interactive "P")
8971   (gnus-summary-sort 'subject reverse))
8972
8973 (defun gnus-summary-sort-by-date (&optional reverse)
8974   "Sort the summary buffer by date.
8975 Argument REVERSE means reverse order."
8976   (interactive "P")
8977   (gnus-summary-sort 'date reverse))
8978
8979 (defun gnus-summary-sort-by-score (&optional reverse)
8980   "Sort the summary buffer by score.
8981 Argument REVERSE means reverse order."
8982   (interactive "P")
8983   (gnus-summary-sort 'score reverse))
8984
8985 (defun gnus-summary-sort-by-lines (&optional reverse)
8986   "Sort the summary buffer by the number of lines.
8987 Argument REVERSE means reverse order."
8988   (interactive "P")
8989   (gnus-summary-sort 'lines reverse))
8990
8991 (defun gnus-summary-sort-by-chars (&optional reverse)
8992   "Sort the summary buffer by article length.
8993 Argument REVERSE means reverse order."
8994   (interactive "P")
8995   (gnus-summary-sort 'chars reverse))   
8996
8997 (defun gnus-summary-sort (predicate reverse)
8998   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8999   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9000          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9001          (gnus-thread-sort-functions
9002           (if (not reverse)
9003               thread
9004             `(lambda (t1 t2)
9005                (,thread t2 t1))))
9006          (gnus-article-sort-functions
9007           (if (not reverse)
9008               article
9009             `(lambda (t1 t2)
9010                (,article t2 t1))))
9011          (buffer-read-only)
9012          (gnus-summary-prepare-hook nil))
9013     ;; We do the sorting by regenerating the threads.
9014     (gnus-summary-prepare)
9015     ;; Hide subthreads if needed.
9016     (when (and gnus-show-threads gnus-thread-hide-subtree)
9017       (gnus-summary-hide-all-threads))))
9018
9019 ;; Summary saving commands.
9020
9021 (defun gnus-summary-save-article (&optional n not-saved)
9022   "Save the current article using the default saver function.
9023 If N is a positive number, save the N next articles.
9024 If N is a negative number, save the N previous articles.
9025 If N is nil and any articles have been marked with the process mark,
9026 save those articles instead.
9027 The variable `gnus-default-article-saver' specifies the saver function."
9028   (interactive "P")
9029   (let* ((articles (gnus-summary-work-articles n))
9030          (save-buffer (save-excursion
9031                         (nnheader-set-temp-buffer " *Gnus Save*")))
9032          (num (length articles))
9033          header file)
9034     (dolist (article articles)
9035       (setq header (gnus-summary-article-header article))
9036       (if (not (vectorp header))
9037           ;; This is a pseudo-article.
9038           (if (assq 'name header)
9039               (gnus-copy-file (cdr (assq 'name header)))
9040             (gnus-message 1 "Article %d is unsaveable" article))
9041         ;; This is a real article.
9042         (save-window-excursion
9043           (gnus-summary-select-article t nil nil article))
9044         (save-excursion
9045           (set-buffer save-buffer)
9046           (erase-buffer)
9047           (insert-buffer-substring gnus-original-article-buffer))
9048         (setq file (gnus-article-save save-buffer file num))
9049         (gnus-summary-remove-process-mark article)
9050         (unless not-saved
9051           (gnus-summary-set-saved-mark article))))
9052     (gnus-kill-buffer save-buffer)
9053     (gnus-summary-position-point)
9054     (gnus-set-mode-line 'summary)
9055     n))
9056
9057 (defun gnus-summary-pipe-output (&optional arg)
9058   "Pipe the current article to a subprocess.
9059 If N is a positive number, pipe the N next articles.
9060 If N is a negative number, pipe the N previous articles.
9061 If N is nil and any articles have been marked with the process mark,
9062 pipe those articles instead."
9063   (interactive "P")
9064   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9065     (gnus-summary-save-article arg t))
9066   (gnus-configure-windows 'pipe))
9067
9068 (defun gnus-summary-save-article-mail (&optional arg)
9069   "Append the current article to an mail file.
9070 If N is a positive number, save the N next articles.
9071 If N is a negative number, save the N previous articles.
9072 If N is nil and any articles have been marked with the process mark,
9073 save those articles instead."
9074   (interactive "P")
9075   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9076     (gnus-summary-save-article arg)))
9077
9078 (defun gnus-summary-save-article-rmail (&optional arg)
9079   "Append the current article to an rmail file.
9080 If N is a positive number, save the N next articles.
9081 If N is a negative number, save the N previous articles.
9082 If N is nil and any articles have been marked with the process mark,
9083 save those articles instead."
9084   (interactive "P")
9085   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9086     (gnus-summary-save-article arg)))
9087
9088 (defun gnus-summary-save-article-file (&optional arg)
9089   "Append the current article to a file.
9090 If N is a positive number, save the N next articles.
9091 If N is a negative number, save the N previous articles.
9092 If N is nil and any articles have been marked with the process mark,
9093 save those articles instead."
9094   (interactive "P")
9095   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9096     (gnus-summary-save-article arg)))
9097
9098 (defun gnus-summary-write-article-file (&optional arg)
9099   "Write the current article to a file, deleting the previous file.
9100 If N is a positive number, save the N next articles.
9101 If N is a negative number, save the N previous articles.
9102 If N is nil and any articles have been marked with the process mark,
9103 save those articles instead."
9104   (interactive "P")
9105   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9106     (gnus-summary-save-article arg)))
9107
9108 (defun gnus-summary-save-article-body-file (&optional arg)
9109   "Append the current article body to a file.
9110 If N is a positive number, save the N next articles.
9111 If N is a negative number, save the N previous articles.
9112 If N is nil and any articles have been marked with the process mark,
9113 save those articles instead."
9114   (interactive "P")
9115   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9116     (gnus-summary-save-article arg)))
9117
9118 (defun gnus-summary-pipe-message (program)
9119   "Pipe the current article through PROGRAM."
9120   (interactive "sProgram: ")
9121   (gnus-summary-select-article)
9122   (let ((mail-header-separator ""))
9123     (gnus-eval-in-buffer-window gnus-article-buffer
9124       (save-restriction
9125         (widen)
9126         (let ((start (window-start))
9127               buffer-read-only)
9128           (message-pipe-buffer-body program)
9129           (set-window-start (get-buffer-window (current-buffer)) start))))))
9130
9131 (defun gnus-get-split-value (methods)
9132   "Return a value based on the split METHODS."
9133   (let (split-name method result match)
9134     (when methods
9135       (save-excursion
9136         (set-buffer gnus-original-article-buffer)
9137         (save-restriction
9138           (nnheader-narrow-to-headers)
9139           (while methods
9140             (goto-char (point-min))
9141             (setq method (pop methods))
9142             (setq match (car method))
9143             (when (cond
9144                    ((stringp match)
9145                     ;; Regular expression.
9146                     (ignore-errors
9147                       (re-search-forward match nil t)))
9148                    ((gnus-functionp match)
9149                     ;; Function.
9150                     (save-restriction
9151                       (widen)
9152                       (setq result (funcall match gnus-newsgroup-name))))
9153                    ((consp match)
9154                     ;; Form.
9155                     (save-restriction
9156                       (widen)
9157                       (setq result (eval match)))))
9158               (setq split-name (append (cdr method) split-name))
9159               (cond ((stringp result)
9160                      (push (expand-file-name
9161                             result gnus-article-save-directory)
9162                            split-name))
9163                     ((consp result)
9164                      (setq split-name (append result split-name)))))))))
9165     (nreverse split-name)))
9166
9167 (defun gnus-valid-move-group-p (group)
9168   (and (boundp group)
9169        (symbol-name group)
9170        (symbol-value group)
9171        (gnus-get-function (gnus-find-method-for-group
9172                            (symbol-name group)) 'request-accept-article t)))
9173
9174 (defun gnus-read-move-group-name (prompt default articles prefix)
9175   "Read a group name."
9176   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9177          (minibuffer-confirm-incomplete nil) ; XEmacs
9178          (prom
9179           (format "%s %s to:"
9180                   prompt
9181                   (if (> (length articles) 1)
9182                       (format "these %d articles" (length articles))
9183                     "this article")))
9184          (to-newsgroup
9185           (cond
9186            ((null split-name)
9187             (gnus-completing-read default prom
9188                                   gnus-active-hashtb
9189                                   'gnus-valid-move-group-p
9190                                   nil prefix
9191                                   'gnus-group-history))
9192            ((= 1 (length split-name))
9193             (gnus-completing-read (car split-name) prom
9194                                   gnus-active-hashtb
9195                                   'gnus-valid-move-group-p
9196                                   nil nil
9197                                   'gnus-group-history))
9198            (t
9199             (gnus-completing-read nil prom
9200                                   (mapcar (lambda (el) (list el))
9201                                           (nreverse split-name))
9202                                   nil nil nil
9203                                   'gnus-group-history)))))
9204     (when to-newsgroup
9205       (if (or (string= to-newsgroup "")
9206               (string= to-newsgroup prefix))
9207           (setq to-newsgroup default))
9208       (unless to-newsgroup
9209         (error "No group name entered"))
9210       (or (gnus-active to-newsgroup)
9211           (gnus-activate-group to-newsgroup)
9212           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9213                                      to-newsgroup))
9214               (or (and (gnus-request-create-group
9215                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9216                        (gnus-activate-group
9217                         to-newsgroup nil nil
9218                         (gnus-group-name-to-method to-newsgroup))
9219                        (gnus-subscribe-group to-newsgroup))
9220                   (error "Couldn't create group %s" to-newsgroup)))
9221           (error "No such group: %s" to-newsgroup)))
9222     to-newsgroup))
9223
9224 (defun gnus-summary-save-parts (type dir n reverse)
9225   "Save parts matching TYPE to DIR.
9226 If REVERSE, save parts that do not match TYPE."
9227   (interactive
9228    (list (read-string "Save parts of type: " "image/.*")
9229          (read-file-name "Save to directory: " t nil t)
9230          current-prefix-arg))
9231   (gnus-summary-iterate n
9232     (let ((gnus-display-mime-function nil)
9233           (gnus-inhibit-treatment t))
9234       (gnus-summary-select-article))
9235     (save-excursion
9236       (set-buffer gnus-article-buffer)
9237       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9238         (when handles
9239           (gnus-summary-save-parts-1 type dir handles reverse)
9240           (mm-destroy-parts handles))))))
9241
9242 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9243   (if (stringp (car handle))
9244       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9245               (cdr handle))
9246     (when (if reverse
9247               (not (string-match type (mm-handle-media-type handle)))
9248             (string-match type (mm-handle-media-type handle)))
9249       (let ((file (expand-file-name
9250                    (file-name-nondirectory
9251                     (or
9252                      (mail-content-type-get
9253                       (mm-handle-disposition handle) 'filename)
9254                      (concat gnus-newsgroup-name "." gnus-current-article)))
9255                    dir)))
9256         (unless (file-exists-p file)
9257           (mm-save-part-to-file handle file))))))
9258
9259 ;; Summary extract commands
9260
9261 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9262   (let ((buffer-read-only nil)
9263         (article (gnus-summary-article-number))
9264         after-article b e)
9265     (unless (gnus-summary-goto-subject article)
9266       (error "No such article: %d" article))
9267     (gnus-summary-position-point)
9268     ;; If all commands are to be bunched up on one line, we collect
9269     ;; them here.
9270     (unless gnus-view-pseudos-separately
9271       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9272             files action)
9273         (while ps
9274           (setq action (cdr (assq 'action (car ps))))
9275           (setq files (list (cdr (assq 'name (car ps)))))
9276           (while (and ps (cdr ps)
9277                       (string= (or action "1")
9278                                (or (cdr (assq 'action (cadr ps))) "2")))
9279             (push (cdr (assq 'name (cadr ps))) files)
9280             (setcdr ps (cddr ps)))
9281           (when files
9282             (when (not (string-match "%s" action))
9283               (push " " files))
9284             (push " " files)
9285             (when (assq 'execute (car ps))
9286               (setcdr (assq 'execute (car ps))
9287                       (funcall (if (string-match "%s" action)
9288                                    'format 'concat)
9289                                action
9290                                (mapconcat
9291                                 (lambda (f)
9292                                   (if (equal f " ")
9293                                       f
9294                                     (gnus-quote-arg-for-sh-or-csh f)))
9295                                 files " ")))))
9296           (setq ps (cdr ps)))))
9297     (if (and gnus-view-pseudos (not not-view))
9298         (while pslist
9299           (when (assq 'execute (car pslist))
9300             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9301                                   (eq gnus-view-pseudos 'not-confirm)))
9302           (setq pslist (cdr pslist)))
9303       (save-excursion
9304         (while pslist
9305           (setq after-article (or (cdr (assq 'article (car pslist)))
9306                                   (gnus-summary-article-number)))
9307           (gnus-summary-goto-subject after-article)
9308           (forward-line 1)
9309           (setq b (point))
9310           (insert "    " (file-name-nondirectory
9311                           (cdr (assq 'name (car pslist))))
9312                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9313           (setq e (point))
9314           (forward-line -1)             ; back to `b'
9315           (gnus-add-text-properties
9316            b (1- e) (list 'gnus-number gnus-reffed-article-number
9317                           gnus-mouse-face-prop gnus-mouse-face))
9318           (gnus-data-enter
9319            after-article gnus-reffed-article-number
9320            gnus-unread-mark b (car pslist) 0 (- e b))
9321           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9322           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9323           (setq pslist (cdr pslist)))))))
9324
9325 (defun gnus-pseudos< (p1 p2)
9326   (let ((c1 (cdr (assq 'action p1)))
9327         (c2 (cdr (assq 'action p2))))
9328     (and c1 c2 (string< c1 c2))))
9329
9330 (defun gnus-request-pseudo-article (props)
9331   (cond ((assq 'execute props)
9332          (gnus-execute-command (cdr (assq 'execute props)))))
9333   (let ((gnus-current-article (gnus-summary-article-number)))
9334     (gnus-run-hooks 'gnus-mark-article-hook)))
9335
9336 (defun gnus-execute-command (command &optional automatic)
9337   (save-excursion
9338     (gnus-article-setup-buffer)
9339     (set-buffer gnus-article-buffer)
9340     (setq buffer-read-only nil)
9341     (let ((command (if automatic command
9342                      (read-string "Command: " (cons command 0)))))
9343       (erase-buffer)
9344       (insert "$ " command "\n\n")
9345       (if gnus-view-pseudo-asynchronously
9346           (start-process "gnus-execute" (current-buffer) shell-file-name
9347                          shell-command-switch command)
9348         (call-process shell-file-name nil t nil
9349                       shell-command-switch command)))))
9350
9351 ;; Summary kill commands.
9352
9353 (defun gnus-summary-edit-global-kill (article)
9354   "Edit the \"global\" kill file."
9355   (interactive (list (gnus-summary-article-number)))
9356   (gnus-group-edit-global-kill article))
9357
9358 (defun gnus-summary-edit-local-kill ()
9359   "Edit a local kill file applied to the current newsgroup."
9360   (interactive)
9361   (setq gnus-current-headers (gnus-summary-article-header))
9362   (gnus-group-edit-local-kill
9363    (gnus-summary-article-number) gnus-newsgroup-name))
9364
9365 ;;; Header reading.
9366
9367 (defun gnus-read-header (id &optional header)
9368   "Read the headers of article ID and enter them into the Gnus system."
9369   (let ((group gnus-newsgroup-name)
9370         (gnus-override-method
9371          (or
9372           gnus-override-method
9373           (and (gnus-news-group-p gnus-newsgroup-name)
9374                (car (gnus-refer-article-methods)))))
9375         where)
9376     ;; First we check to see whether the header in question is already
9377     ;; fetched.
9378     (if (stringp id)
9379         ;; This is a Message-ID.
9380         (setq header (or header (gnus-id-to-header id)))
9381       ;; This is an article number.
9382       (setq header (or header (gnus-summary-article-header id))))
9383     (if (and header
9384              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9385         ;; We have found the header.
9386         header
9387       ;; If this is a sparse article, we have to nix out its
9388       ;; previous entry in the thread hashtb.
9389       (when (and header
9390                  (gnus-summary-article-sparse-p (mail-header-number header)))
9391         (let* ((parent (gnus-parent-id (mail-header-references header)))
9392                (thread (and parent (gnus-id-to-thread parent))))
9393           (when thread
9394             (delq (assq header thread) thread))))
9395       ;; We have to really fetch the header to this article.
9396       (save-excursion
9397         (set-buffer nntp-server-buffer)
9398         (when (setq where (gnus-request-head id group))
9399           (nnheader-fold-continuation-lines)
9400           (goto-char (point-max))
9401           (insert ".\n")
9402           (goto-char (point-min))
9403           (insert "211 ")
9404           (princ (cond
9405                   ((numberp id) id)
9406                   ((cdr where) (cdr where))
9407                   (header (mail-header-number header))
9408                   (t gnus-reffed-article-number))
9409                  (current-buffer))
9410           (insert " Article retrieved.\n"))
9411         (if (or (not where)
9412                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9413             ()                          ; Malformed head.
9414           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9415             (when (and (stringp id)
9416                        (not (string= (gnus-group-real-name group)
9417                                      (car where))))
9418               ;; If we fetched by Message-ID and the article came
9419               ;; from a different group, we fudge some bogus article
9420               ;; numbers for this article.
9421               (mail-header-set-number header gnus-reffed-article-number))
9422             (save-excursion
9423               (set-buffer gnus-summary-buffer)
9424               (decf gnus-reffed-article-number)
9425               (gnus-remove-header (mail-header-number header))
9426               (push header gnus-newsgroup-headers)
9427               (setq gnus-current-headers header)
9428               (push (mail-header-number header) gnus-newsgroup-limit)))
9429           header)))))
9430
9431 (defun gnus-remove-header (number)
9432   "Remove header NUMBER from `gnus-newsgroup-headers'."
9433   (if (and gnus-newsgroup-headers
9434            (= number (mail-header-number (car gnus-newsgroup-headers))))
9435       (pop gnus-newsgroup-headers)
9436     (let ((headers gnus-newsgroup-headers))
9437       (while (and (cdr headers)
9438                   (not (= number (mail-header-number (cadr headers)))))
9439         (pop headers))
9440       (when (cdr headers)
9441         (setcdr headers (cddr headers))))))
9442
9443 ;;;
9444 ;;; summary highlights
9445 ;;;
9446
9447 (defun gnus-highlight-selected-summary ()
9448   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9449   ;; Highlight selected article in summary buffer
9450   (when gnus-summary-selected-face
9451     (save-excursion
9452       (let* ((beg (progn (beginning-of-line) (point)))
9453              (end (progn (end-of-line) (point)))
9454              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9455              (from (if (get-text-property beg gnus-mouse-face-prop)
9456                        beg
9457                      (or (next-single-property-change
9458                           beg gnus-mouse-face-prop nil end)
9459                          beg)))
9460              (to
9461               (if (= from end)
9462                   (- from 2)
9463                 (or (next-single-property-change
9464                      from gnus-mouse-face-prop nil end)
9465                     end))))
9466         ;; If no mouse-face prop on line we will have to = from = end,
9467         ;; so we highlight the entire line instead.
9468         (when (= (+ to 2) from)
9469           (setq from beg)
9470           (setq to end))
9471         (if gnus-newsgroup-selected-overlay
9472             ;; Move old overlay.
9473             (gnus-move-overlay
9474              gnus-newsgroup-selected-overlay from to (current-buffer))
9475           ;; Create new overlay.
9476           (gnus-overlay-put
9477            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9478            'face gnus-summary-selected-face))))))
9479
9480 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9481 (defun gnus-summary-highlight-line ()
9482   "Highlight current line according to `gnus-summary-highlight'."
9483   (let* ((list gnus-summary-highlight)
9484          (p (point))
9485          (end (progn (end-of-line) (point)))
9486          ;; now find out where the line starts and leave point there.
9487          (beg (progn (beginning-of-line) (point)))
9488          (article (gnus-summary-article-number))
9489          (score (or (cdr (assq (or article gnus-current-article)
9490                                gnus-newsgroup-scored))
9491                     gnus-summary-default-score 0))
9492          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9493          (inhibit-read-only t))
9494     ;; Eval the cars of the lists until we find a match.
9495     (let ((default gnus-summary-default-score))
9496       (while (and list
9497                   (not (eval (caar list))))
9498         (setq list (cdr list))))
9499     (let ((face (cdar list)))
9500       (unless (eq face (get-text-property beg 'face))
9501         (gnus-put-text-property-excluding-characters-with-faces
9502          beg end 'face
9503          (setq face (if (boundp face) (symbol-value face) face)))
9504         (when gnus-summary-highlight-line-function
9505           (funcall gnus-summary-highlight-line-function article face))))
9506     (goto-char p)))
9507
9508 (defun gnus-update-read-articles (group unread &optional compute)
9509   "Update the list of read articles in GROUP."
9510   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9511          (entry (gnus-gethash group gnus-newsrc-hashtb))
9512          (info (nth 2 entry))
9513          (prev 1)
9514          (unread (sort (copy-sequence unread) '<))
9515          read)
9516     (if (or (not info) (not active))
9517         ;; There is no info on this group if it was, in fact,
9518         ;; killed.  Gnus stores no information on killed groups, so
9519         ;; there's nothing to be done.
9520         ;; One could store the information somewhere temporarily,
9521         ;; perhaps...  Hmmm...
9522         ()
9523       ;; Remove any negative articles numbers.
9524       (while (and unread (< (car unread) 0))
9525         (setq unread (cdr unread)))
9526       ;; Remove any expired article numbers
9527       (while (and unread (< (car unread) (car active)))
9528         (setq unread (cdr unread)))
9529       ;; Compute the ranges of read articles by looking at the list of
9530       ;; unread articles.
9531       (while unread
9532         (when (/= (car unread) prev)
9533           (push (if (= prev (1- (car unread))) prev
9534                   (cons prev (1- (car unread))))
9535                 read))
9536         (setq prev (1+ (car unread)))
9537         (setq unread (cdr unread)))
9538       (when (<= prev (cdr active))
9539         (push (cons prev (cdr active)) read))
9540       (setq read (if (> (length read) 1) (nreverse read) read))
9541       (if compute
9542           read
9543         (save-excursion
9544           (set-buffer gnus-group-buffer)
9545           (gnus-undo-register
9546             `(progn
9547                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9548                (gnus-info-set-read ',info ',(gnus-info-read info))
9549                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9550                (gnus-group-update-group ,group t))))
9551         ;; Propagate the read marks to the backend.
9552         (if (gnus-check-backend-function 'request-set-mark group)
9553             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9554                   (add (gnus-remove-from-range read (gnus-info-read info))))
9555               (when (or add del)
9556                 (unless (gnus-check-group group)
9557                   (error "Can't open server for %s" group))
9558                 (gnus-request-set-mark
9559                  group (delq nil (list (if add (list add 'add '(read)))
9560                                        (if del (list del 'del '(read)))))))))
9561         ;; Enter this list into the group info.
9562         (gnus-info-set-read info read)
9563         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9564         (gnus-get-unread-articles-in-group info (gnus-active group))
9565         t))))
9566
9567 (defun gnus-offer-save-summaries ()
9568   "Offer to save all active summary buffers."
9569   (save-excursion
9570     (let ((buflist (buffer-list))
9571           buffers bufname)
9572       ;; Go through all buffers and find all summaries.
9573       (while buflist
9574         (and (setq bufname (buffer-name (car buflist)))
9575              (string-match "Summary" bufname)
9576              (save-excursion
9577                (set-buffer bufname)
9578                ;; We check that this is, indeed, a summary buffer.
9579                (and (eq major-mode 'gnus-summary-mode)
9580                     ;; Also make sure this isn't bogus.
9581                     gnus-newsgroup-prepared
9582                     ;; Also make sure that this isn't a dead summary buffer.
9583                     (not gnus-dead-summary-mode)))
9584              (push bufname buffers))
9585         (setq buflist (cdr buflist)))
9586       ;; Go through all these summary buffers and offer to save them.
9587       (when buffers
9588         (map-y-or-n-p
9589          "Update summary buffer %s? "
9590          (lambda (buf)
9591            (switch-to-buffer buf)
9592            (gnus-summary-exit))
9593          buffers)))))
9594
9595
9596 ;;; @ for mime-partial
9597 ;;;
9598
9599 (defun gnus-request-partial-message ()
9600   (save-excursion
9601     (let ((number (gnus-summary-article-number))
9602           (group gnus-newsgroup-name)
9603           (mother gnus-article-buffer))
9604       (set-buffer (get-buffer-create " *Partial Article*"))
9605       (erase-buffer)
9606       (setq mime-preview-buffer mother)
9607       (gnus-request-article-this-buffer number group)
9608       (mime-parse-buffer)
9609       )))
9610
9611 (autoload 'mime-combine-message/partial-pieces-automatically
9612   "mime-partial"
9613   "Internal method to combine message/partial messages automatically.")
9614
9615 (mime-add-condition
9616  'action '((type . message)(subtype . partial)
9617            (major-mode . gnus-original-article-mode)
9618            (method . mime-combine-message/partial-pieces-automatically)
9619            (summary-buffer-exp . gnus-summary-buffer)
9620            (request-partial-message-method . gnus-request-partial-message)
9621            ))
9622
9623
9624 ;;; @ for message/rfc822
9625 ;;;
9626
9627 (defun gnus-mime-extract-message/rfc822 (entity situation)
9628   (let (group article num cwin swin cur)
9629     (with-current-buffer (mime-entity-buffer entity)
9630       (save-restriction
9631         (narrow-to-region (mime-entity-body-start entity)
9632                           (mime-entity-body-end entity))
9633         (setq group (or (cdr (assq 'group situation))
9634                         (completing-read "Group: "
9635                                          gnus-active-hashtb
9636                                          nil
9637                                          (gnus-read-active-file-p)
9638                                          gnus-newsgroup-name))
9639               article (gnus-request-accept-article group)
9640               )
9641         ))
9642     (when (and (consp article)
9643                (numberp (setq article (cdr article))))
9644       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9645             cwin (get-buffer-window (current-buffer) t)
9646             )
9647       (save-window-excursion
9648         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9649             (select-window swin)
9650           (set-buffer gnus-summary-buffer)
9651           )
9652         (setq cur gnus-current-article)
9653         (forward-line num)
9654         (let (gnus-show-threads)
9655           (gnus-summary-goto-subject article t)
9656           )
9657         (gnus-summary-clear-mark-forward 1)
9658         (gnus-summary-goto-subject cur)
9659         )
9660       (when (and cwin (window-frame cwin))
9661         (select-frame (window-frame cwin))
9662         )
9663       (when (boundp 'mime-acting-situation-to-override)
9664         (set-alist 'mime-acting-situation-to-override
9665                    'group
9666                    group)
9667         (set-alist 'mime-acting-situation-to-override
9668                    'after-method
9669                    `(progn
9670                       (save-current-buffer
9671                         (set-buffer gnus-group-buffer)
9672                         (gnus-activate-group ,group)
9673                         )
9674                       (gnus-summary-goto-article ,cur
9675                                                  gnus-show-all-headers)
9676                       ))
9677         (set-alist 'mime-acting-situation-to-override
9678                    'number num)
9679         )
9680       )))
9681
9682 (mime-add-condition
9683  'action '((type . message)(subtype . rfc822)
9684            (major-mode . gnus-original-article-mode)
9685            (method . gnus-mime-extract-message/rfc822)
9686            (mode . "extract")
9687            ))
9688
9689 (mime-add-condition
9690  'action '((type . message)(subtype . news)
9691            (major-mode . gnus-original-article-mode)
9692            (method . gnus-mime-extract-message/rfc822)
9693            (mode . "extract")
9694            ))
9695
9696 (defun gnus-mime-extract-multipart (entity situation)
9697   (let ((children (mime-entity-children entity))
9698         mime-acting-situation-to-override
9699         f)
9700     (while children
9701       (mime-play-entity (car children)
9702                         (cons (assq 'mode situation)
9703                               mime-acting-situation-to-override))
9704       (setq children (cdr children)))
9705     (if (setq f (cdr (assq 'after-method
9706                            mime-acting-situation-to-override)))
9707         (eval f)
9708       )))  
9709
9710 (mime-add-condition
9711  'action '((type . multipart)
9712            (method . gnus-mime-extract-multipart)
9713            (mode . "extract")
9714            )
9715  'with-default)
9716
9717
9718 ;;; @ end
9719 ;;;
9720
9721 (defun gnus-summary-setup-default-charset ()
9722   "Setup newsgroup default charset."
9723   (if (equal gnus-newsgroup-name "nndraft:drafts")
9724       (setq gnus-newsgroup-charset nil)
9725     (let* ((name (and gnus-newsgroup-name
9726                       (gnus-group-real-name gnus-newsgroup-name)))
9727            (ignored-charsets 
9728             (or gnus-newsgroup-ephemeral-ignored-charsets
9729                 (append
9730                  (and gnus-newsgroup-name
9731                       (or (gnus-group-find-parameter gnus-newsgroup-name
9732                                                      'ignored-charsets t)
9733                           (let ((alist gnus-group-ignored-charsets-alist)
9734                                 elem (charsets nil))
9735                             (while (setq elem (pop alist))
9736                               (when (and name
9737                                          (string-match (car elem) name))
9738                                 (setq alist nil
9739                                       charsets (cdr elem))))
9740                             charsets)))
9741                  gnus-newsgroup-ignored-charsets))))
9742       (setq gnus-newsgroup-charset
9743             (or gnus-newsgroup-ephemeral-charset
9744                 (and gnus-newsgroup-name
9745                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9746                          (let ((alist gnus-group-charset-alist)
9747                                elem charset)
9748                            (while (setq elem (pop alist))
9749                              (when (and name
9750                                         (string-match (car elem) name))
9751                                (setq alist nil
9752                                      charset (cadr elem))))
9753                            charset)))
9754                 gnus-default-charset))
9755       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9756            ignored-charsets))))
9757
9758 ;;;
9759 ;;; Mime Commands
9760 ;;;
9761
9762 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9763   "Display the current article buffer fully MIME-buttonized.
9764 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9765 treated as multipart/mixed."
9766   (interactive "P")
9767   (require 'gnus-art)
9768   (let ((gnus-unbuttonized-mime-types nil)
9769         (gnus-mime-display-multipart-as-mixed show-all-parts))
9770     (gnus-summary-show-article)))
9771
9772 (defun gnus-summary-repair-multipart (article)
9773   "Add a Content-Type header to a multipart article without one."
9774   (interactive (list (gnus-summary-article-number)))
9775   (gnus-with-article article
9776     (message-narrow-to-head)
9777     (goto-char (point-max))
9778     (widen)
9779     (when (search-forward "\n--" nil t)
9780       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9781         (message-narrow-to-head)
9782         (message-remove-header "Mime-Version")
9783         (message-remove-header "Content-Type")
9784         (goto-char (point-max))
9785         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9786                         separator))
9787         (insert "Mime-Version: 1.0\n")
9788         (widen))))
9789   (let (gnus-mark-article-hook)
9790     (gnus-summary-select-article t t nil article)))
9791
9792 (defun gnus-summary-toggle-display-buttonized ()
9793   "Toggle the buttonizing of the article buffer."
9794   (interactive)
9795   (require 'gnus-art)
9796   (if (setq gnus-inhibit-mime-unbuttonizing
9797             (not gnus-inhibit-mime-unbuttonizing))
9798       (let ((gnus-unbuttonized-mime-types nil))
9799         (gnus-summary-show-article))
9800     (gnus-summary-show-article)))
9801
9802 ;;;
9803 ;;; Intelli-mouse commmands
9804 ;;;
9805
9806 (defun gnus-wheel-summary-scroll (event)
9807   (interactive "e")
9808   (let ((amount (if (memq 'shift (event-modifiers event))
9809                     (car gnus-wheel-scroll-amount)
9810                   (cdr gnus-wheel-scroll-amount)))
9811         (direction (- (* (static-if (featurep 'xemacs)
9812                              (event-button event)
9813                            (cond ((eq 'mouse-4 (event-basic-type event))
9814                                   4)
9815                                  ((eq 'mouse-5 (event-basic-type event))
9816                                   5)))
9817                          2) 9))
9818         edge)
9819     (gnus-summary-scroll-up (* amount direction))
9820     (when (gnus-eval-in-buffer-window gnus-article-buffer
9821             (save-restriction
9822               (widen)
9823               (and (if (< 0 direction)
9824                        (gnus-article-next-page 0)
9825                      (gnus-article-prev-page 0)
9826                      (bobp))
9827                    (if (setq edge (get-text-property
9828                                    (point-min) 'gnus-wheel-edge))
9829                        (setq edge (* edge direction))
9830                      (setq edge -1))
9831                    (or (plusp edge)
9832                        (let ((buffer-read-only nil)
9833                              (inhibit-read-only t))
9834                          (put-text-property (point-min) (point-max)
9835                                             'gnus-wheel-edge direction)
9836                          nil))
9837                    (or (> edge gnus-wheel-edge-resistance)
9838                        (let ((buffer-read-only nil)
9839                              (inhibit-read-only t))
9840                          (put-text-property (point-min) (point-max)
9841                                             'gnus-wheel-edge
9842                                             (* (1+ edge) direction))
9843                          nil))
9844                    (eq last-command 'gnus-wheel-summary-scroll))))
9845       (gnus-summary-next-article nil nil (minusp direction)))))
9846
9847 (defun gnus-wheel-install ()
9848   "Enable mouse wheel support on summary window."
9849   (when gnus-use-wheel
9850     (let ((keys 
9851            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9852       (dolist (key keys)
9853         (define-key gnus-summary-mode-map key
9854           'gnus-wheel-summary-scroll)))))
9855
9856 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9857
9858 ;;;
9859 ;;; Traditional PGP commmands
9860 ;;;
9861
9862 (defun gnus-summary-decrypt-article (&optional force)
9863   "Decrypt the current article in traditional PGP way.
9864 This will have permanent effect only in mail groups.
9865 If FORCE is non-nil, allow editing of articles even in read-only
9866 groups."
9867   (interactive "P")
9868   (gnus-summary-select-article t)
9869   (gnus-eval-in-buffer-window gnus-article-buffer
9870     (save-excursion
9871       (save-restriction
9872         (widen)
9873         (goto-char (point-min))
9874         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9875           (error "Not a traditional PGP message!"))
9876         (let ((armor-start (match-beginning 0)))
9877           (if (and (pgg-decrypt-region armor-start (point-max))
9878                    (or force (not (gnus-group-read-only-p))))
9879               (let ((inhibit-read-only t) 
9880                     buffer-read-only)
9881                 (delete-region armor-start
9882                                (progn 
9883                                  (re-search-forward "^-+END PGP" nil t)
9884                                  (beginning-of-line 2)
9885                                  (point)))
9886                 (insert-buffer-substring pgg-output-buffer))))))))
9887
9888 (defun gnus-summary-verify-article ()
9889   "Verify the current article in traditional PGP way."
9890   (interactive)
9891   (save-excursion
9892     (set-buffer gnus-original-article-buffer)
9893     (goto-char (point-min))
9894     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9895       (error "Not a traditional PGP message!"))
9896     (re-search-forward "^-+END PGP" nil t)
9897     (beginning-of-line 2)
9898     (call-interactively (function pgg-verify-region))))
9899
9900 ;;;
9901 ;;; with article
9902 ;;;
9903
9904 (defmacro gnus-with-article (article &rest forms)
9905   "Select ARTICLE and perform FORMS in the original article buffer.
9906 Then replace the article with the result."
9907   `(progn
9908      ;; We don't want the article to be marked as read.
9909      (let (gnus-mark-article-hook)
9910        (gnus-summary-select-article t t nil ,article))
9911      (set-buffer gnus-original-article-buffer)
9912      ,@forms
9913      (if (not (gnus-check-backend-function
9914                'request-replace-article (car gnus-article-current)))
9915          (gnus-message 5 "Read-only group; not replacing")
9916        (unless (gnus-request-replace-article
9917                 ,article (car gnus-article-current)
9918                 (current-buffer) t)
9919          (error "Couldn't replace article")))
9920      ;; The cache and backlog have to be flushed somewhat.
9921      (when gnus-keep-backlog
9922        (gnus-backlog-remove-article
9923         (car gnus-article-current) (cdr gnus-article-current)))
9924      (when gnus-use-cache
9925        (gnus-cache-update-article
9926         (car gnus-article-current) (cdr gnus-article-current)))))
9927
9928 (put 'gnus-with-article 'lisp-indent-function 1)
9929 (put 'gnus-with-article 'edebug-form-spec '(form body))
9930
9931 ;;;
9932 ;;; Generic summary marking commands
9933 ;;;
9934
9935 (defvar gnus-summary-marking-alist
9936   '((read gnus-del-mark "d")
9937     (unread gnus-unread-mark "u")
9938     (ticked gnus-ticked-mark "!")
9939     (dormant gnus-dormant-mark "?")
9940     (expirable gnus-expirable-mark "e"))
9941   "An alist of names/marks/keystrokes.")
9942
9943 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9944 (defvar gnus-summary-mark-map)
9945
9946 (defun gnus-summary-make-all-marking-commands ()
9947   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9948   (dolist (elem gnus-summary-marking-alist)
9949     (apply 'gnus-summary-make-marking-command elem)))
9950
9951 (defun gnus-summary-make-marking-command (name mark keystroke)
9952   (let ((map (make-sparse-keymap)))
9953     (define-key gnus-summary-generic-mark-map keystroke map)
9954     (dolist (lway `((next "next" next nil "n")
9955                     (next-unread "next unread" next t "N")
9956                     (prev "previous" prev nil "p")
9957                     (prev-unread "previous unread" prev t "P")
9958                     (nomove "" nil nil ,keystroke)))
9959       (let ((func (gnus-summary-make-marking-command-1
9960                    mark (car lway) lway name)))
9961         (setq func (eval func))
9962         (define-key map (nth 4 lway) func)))))
9963       
9964 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9965   `(defun ,(intern
9966             (format "gnus-summary-put-mark-as-%s%s"
9967                     name (if (eq way 'nomove)
9968                              ""
9969                            (concat "-" (symbol-name way)))))
9970      (n)
9971      ,(format
9972        "Mark the current article as %s%s.
9973 If N, the prefix, then repeat N times.
9974 If N is negative, move in reverse order.
9975 The difference between N and the actual number of articles marked is
9976 returned."
9977        name (car (cdr lway)))
9978      (interactive "p")
9979      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9980     
9981 (defun gnus-summary-generic-mark (n mark move unread)
9982   "Mark N articles with MARK."
9983   (unless (eq major-mode 'gnus-summary-mode)
9984     (error "This command can only be used in the summary buffer"))
9985   (gnus-summary-show-thread)
9986   (let ((nummove
9987          (cond
9988           ((eq move 'next) 1)
9989           ((eq move 'prev) -1)
9990           (t 0))))
9991     (if (zerop nummove)
9992         (setq n 1)
9993       (when (< n 0)
9994         (setq n (abs n)
9995               nummove (* -1 nummove))))
9996     (while (and (> n 0)
9997                 (gnus-summary-mark-article nil mark)
9998                 (zerop (gnus-summary-next-subject nummove unread t)))
9999       (setq n (1- n)))
10000     (when (/= 0 n)
10001       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10002     (gnus-summary-recenter)
10003     (gnus-summary-position-point)
10004     (gnus-set-mode-line 'summary)
10005     n))
10006
10007 (gnus-summary-make-all-marking-commands)
10008
10009 (gnus-ems-redefine)
10010
10011 (provide 'gnus-sum)
10012
10013 (run-hooks 'gnus-sum-load-hook)
10014
10015 ;;; gnus-sum.el ends here