Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 ;; Recursive :-(.
41 ;; (require 'gnus-art)
42 (require 'nnoo)
43 (require 'mime-view)
44
45 (eval-when-compile
46   (require 'mime-play)
47   (require 'static))
48
49 (eval-and-compile
50   (autoload 'gnus-cache-articles-in-group "gnus-cache")
51   (autoload 'pgg-decrypt-region "pgg" nil t)
52   (autoload 'pgg-verify-region "pgg" nil t))
53
54 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
55 (autoload 'gnus-cache-write-active "gnus-cache")
56 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
57 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
58 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
59 (autoload 'mm-uu-dissect "mm-uu")
60
61 (defcustom gnus-kill-summary-on-exit t
62   "*If non-nil, kill the summary buffer when you exit from it.
63 If nil, the summary will become a \"*Dead Summary*\" buffer, and
64 it will be killed sometime later."
65   :group 'gnus-summary-exit
66   :type 'boolean)
67
68 (defcustom gnus-fetch-old-headers nil
69   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
70 If an unread article in the group refers to an older, already read (or
71 just marked as read) article, the old article will not normally be
72 displayed in the Summary buffer.  If this variable is non-nil, Gnus
73 will attempt to grab the headers to the old articles, and thereby
74 build complete threads.  If it has the value `some', only enough
75 headers to connect otherwise loose threads will be displayed.  This
76 variable can also be a number.  In that case, no more than that number
77 of old headers will be fetched.  If it has the value `invisible', all
78 old headers will be fetched, but none will be displayed.
79
80 The server has to support NOV for any of this to work."
81   :group 'gnus-thread
82   :type '(choice (const :tag "off" nil)
83                  (const some)
84                  number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-refer-thread-limit 200
88   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
89 If t, fetch all the available old headers."
90   :group 'gnus-thread
91   :type '(choice number
92                  (sexp :menu-tag "other" t)))
93
94 (defcustom gnus-summary-make-false-root 'adopt
95   "*nil means that Gnus won't gather loose threads.
96 If the root of a thread has expired or been read in a previous
97 session, the information necessary to build a complete thread has been
98 lost.  Instead of having many small sub-threads from this original thread
99 scattered all over the summary buffer, Gnus can gather them.
100
101 If non-nil, Gnus will try to gather all loose sub-threads from an
102 original thread into one large thread.
103
104 If this variable is non-nil, it should be one of `none', `adopt',
105 `dummy' or `empty'.
106
107 If this variable is `none', Gnus will not make a false root, but just
108 present the sub-threads after another.
109 If this variable is `dummy', Gnus will create a dummy root that will
110 have all the sub-threads as children.
111 If this variable is `adopt', Gnus will make one of the \"children\"
112 the parent and mark all the step-children as such.
113 If this variable is `empty', the \"children\" are printed with empty
114 subject fields.  (Or rather, they will be printed with a string
115 given by the `gnus-summary-same-subject' variable.)"
116   :group 'gnus-thread
117   :type '(choice (const :tag "off" nil)
118                  (const none)
119                  (const dummy)
120                  (const adopt)
121                  (const empty)))
122
123 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
124   "*A regexp to match subjects to be excluded from loose thread gathering.
125 As loose thread gathering is done on subjects only, that means that
126 there can be many false gatherings performed.  By rooting out certain
127 common subjects, gathering might become saner."
128   :group 'gnus-thread
129   :type 'regexp)
130
131 (defcustom gnus-summary-gather-subject-limit nil
132   "*Maximum length of subject comparisons when gathering loose threads.
133 Use nil to compare full subjects.  Setting this variable to a low
134 number will help gather threads that have been corrupted by
135 newsreaders chopping off subject lines, but it might also mean that
136 unrelated articles that have subject that happen to begin with the
137 same few characters will be incorrectly gathered.
138
139 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
140 comparing subjects."
141   :group 'gnus-thread
142   :type '(choice (const :tag "off" nil)
143                  (const fuzzy)
144                  (sexp :menu-tag "on" t)))
145
146 (defcustom gnus-simplify-subject-functions nil
147   "List of functions taking a string argument that simplify subjects.
148 The functions are applied recursively.
149
150 Useful functions to put in this list include: `gnus-simplify-subject-re',
151 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 (they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 If threads are hidden, you have to run the command
238 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
239 to expose hidden threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-hide-killed t
244   "*If non-nil, hide killed threads automatically."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-ignore-subject t
249   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
250 If nil, articles that have different subjects from their parents will
251 start separate threads."
252   :group 'gnus-thread
253   :type 'boolean)
254
255 (defcustom gnus-thread-operation-ignore-subject t
256   "*If non-nil, subjects will be ignored when doing thread commands.
257 This affects commands like `gnus-summary-kill-thread' and
258 `gnus-summary-lower-thread'.
259
260 If this variable is nil, articles in the same thread with different
261 subjects will not be included in the operation in question.  If this
262 variable is `fuzzy', only articles that have subjects that are fuzzily
263 equal will be included."
264   :group 'gnus-thread
265   :type '(choice (const :tag "off" nil)
266                  (const fuzzy)
267                  (sexp :tag "on" t)))
268
269 (defcustom gnus-thread-indent-level 4
270   "*Number that says how much each sub-thread should be indented."
271   :group 'gnus-thread
272   :type 'integer)
273
274 (defcustom gnus-auto-extend-newsgroup t
275   "*If non-nil, extend newsgroup forward and backward when requested."
276   :group 'gnus-summary-choose
277   :type 'boolean)
278
279 (defcustom gnus-auto-select-first t
280   "*If nil, don't select the first unread article when entering a group.
281 If this variable is `best', select the highest-scored unread article
282 in the group.  If t, select the first unread article.
283
284 This variable can also be a function to place point on a likely
285 subject line.  Useful values include `gnus-summary-first-unread-subject',
286 `gnus-summary-first-unread-article' and
287 `gnus-summary-best-unread-article'.
288
289 If you want to prevent automatic selection of the first unread article
290 in some newsgroups, set the variable to nil in
291 `gnus-select-group-hook'."
292   :group 'gnus-group-select
293   :type '(choice (const :tag "none" nil)
294                  (const best)
295                  (sexp :menu-tag "first" t)
296                  (function-item gnus-summary-first-unread-subject)
297                  (function-item gnus-summary-first-unread-article)
298                  (function-item gnus-summary-best-unread-article)))
299
300 (defcustom gnus-dont-select-after-jump-to-other-group nil
301   "If non-nil, don't select the first unread article after entering the
302 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
303 it is depend on the value of `gnus-auto-select-first' whether to select
304 or not."
305   :group 'gnus-group-select
306   :type 'boolean)
307
308 (defcustom gnus-auto-select-next t
309   "*If non-nil, offer to go to the next group from the end of the previous.
310 If the value is t and the next newsgroup is empty, Gnus will exit
311 summary mode and go back to group mode.  If the value is neither nil
312 nor t, Gnus will select the following unread newsgroup.  In
313 particular, if the value is the symbol `quietly', the next unread
314 newsgroup will be selected without any confirmation, and if it is
315 `almost-quietly', the next group will be selected without any
316 confirmation if you are located on the last article in the group.
317 Finally, if this variable is `slightly-quietly', the `Z n' command
318 will go to the next group without confirmation."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "off" nil)
321                  (const quietly)
322                  (const almost-quietly)
323                  (const slightly-quietly)
324                  (sexp :menu-tag "on" t)))
325
326 (defcustom gnus-auto-select-same nil
327   "*If non-nil, select the next article with the same subject.
328 If there are no more articles with the same subject, go to
329 the first unread article."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-summary-check-current nil
334   "*If non-nil, consider the current article when moving.
335 The \"unread\" movement commands will stay on the same line if the
336 current article is unread."
337   :group 'gnus-summary-maneuvering
338   :type 'boolean)
339
340 (defcustom gnus-auto-center-summary t
341   "*If non-nil, always center the current summary buffer.
342 In particular, if `vertical' do only vertical recentering.  If non-nil
343 and non-`vertical', do both horizontal and vertical recentering."
344   :group 'gnus-summary-maneuvering
345   :type '(choice (const :tag "none" nil)
346                  (const vertical)
347                  (integer :tag "height")
348                  (sexp :menu-tag "both" t)))
349
350 (defcustom gnus-show-all-headers nil
351   "*If non-nil, don't hide any headers."
352   :group 'gnus-article-hiding
353   :group 'gnus-article-headers
354   :type 'boolean)
355
356 (defcustom gnus-summary-ignore-duplicates nil
357   "*If non-nil, ignore articles with identical Message-ID headers."
358   :group 'gnus-summary
359   :type 'boolean)
360
361 (defcustom gnus-single-article-buffer t
362   "*If non-nil, display all articles in the same buffer.
363 If nil, each group will get its own article buffer."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-break-pages t
368   "*If non-nil, do page breaking on articles.
369 The page delimiter is specified by the `gnus-page-delimiter'
370 variable."
371   :group 'gnus-article-various
372   :type 'boolean)
373
374 (defcustom gnus-show-mime t
375   "*If non-nil, do mime processing of articles.
376 The articles will simply be fed to the function given by
377 `gnus-article-display-method-for-mime'."
378   :group 'gnus-article-mime
379   :type 'boolean)
380
381 (defcustom gnus-move-split-methods nil
382   "*Variable used to suggest where articles are to be moved to.
383 It uses the same syntax as the `gnus-split-methods' variable.
384 However, whereas `gnus-split-methods' specifies file names as targets,
385 this variable specifies group names."
386   :group 'gnus-summary-mail
387   :type '(repeat (choice (list :value (fun) function)
388                          (cons :value ("" "") regexp (repeat string))
389                          (sexp :value nil))))
390
391 (defcustom gnus-unread-mark ?  ;Whitespace
392   "*Mark used for unread articles."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-ticked-mark ?!
397   "*Mark used for ticked articles."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-dormant-mark ??
402   "*Mark used for dormant articles."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-del-mark ?r
407   "*Mark used for del'd articles."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-read-mark ?R
412   "*Mark used for read articles."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-expirable-mark ?E
417   "*Mark used for expirable articles."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-killed-mark ?K
422   "*Mark used for killed articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-souped-mark ?F
427   "*Mark used for killed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-kill-file-mark ?X
432   "*Mark used for articles killed by kill files."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-low-score-mark ?Y
437   "*Mark used for articles with a low score."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-catchup-mark ?C
442   "*Mark used for articles that are caught up."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-replied-mark ?A
447   "*Mark used for articles that have been replied to."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-cached-mark ?*
452   "*Mark used for articles that are in the cache."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-saved-mark ?S
457   "*Mark used for articles that have been saved to."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-no-mark ?  ;Whitespace
462   "*Mark used for articles that have no other secondary mark."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-ancient-mark ?O
467   "*Mark used for ancient articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-sparse-mark ?Q
472   "*Mark used for sparsely reffed articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-canceled-mark ?G
477   "*Mark used for canceled articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-duplicate-mark ?M
482   "*Mark used for duplicate articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-undownloaded-mark ?@
487   "*Mark used for articles that weren't downloaded."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-downloadable-mark ?%
492   "*Mark used for articles that are to be downloaded."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-unsendable-mark ?=
497   "*Mark used for articles that won't be sent."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-score-over-mark ?+
502   "*Score mark used for articles with high scores."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-score-below-mark ?-
507   "*Score mark used for articles with low scores."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-empty-thread-mark ?  ;Whitespace
512   "*There is no thread under the article."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-not-empty-thread-mark ?=
517   "*There is a thread under the article."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-view-pseudo-asynchronously nil
522   "*If non-nil, Gnus will view pseudo-articles asynchronously."
523   :group 'gnus-extract-view
524   :type 'boolean)
525
526 (defcustom gnus-auto-expirable-marks
527   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
528         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
529         gnus-souped-mark gnus-duplicate-mark)
530   "*The list of marks converted into expiration if a group is auto-expirable."
531   :version "21.1"
532   :group 'gnus-summary
533   :type '(repeat character))
534
535 (defcustom gnus-inhibit-user-auto-expire t
536   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
537   :version "21.1"
538   :group 'gnus-summary
539   :type 'boolean)
540
541 (defcustom gnus-view-pseudos nil
542   "*If `automatic', pseudo-articles will be viewed automatically.
543 If `not-confirm', pseudos will be viewed automatically, and the user
544 will not be asked to confirm the command."
545   :group 'gnus-extract-view
546   :type '(choice (const :tag "off" nil)
547                  (const automatic)
548                  (const not-confirm)))
549
550 (defcustom gnus-view-pseudos-separately t
551   "*If non-nil, one pseudo-article will be created for each file to be viewed.
552 If nil, all files that use the same viewing command will be given as a
553 list of parameters to that command."
554   :group 'gnus-extract-view
555   :type 'boolean)
556
557 (defcustom gnus-insert-pseudo-articles t
558   "*If non-nil, insert pseudo-articles when decoding articles."
559   :group 'gnus-extract-view
560   :type 'boolean)
561
562 (defcustom gnus-summary-dummy-line-format
563   "  %(:                          :%) %S\n"
564   "*The format specification for the dummy roots in the summary buffer.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions.
567
568 %S  The subject"
569   :group 'gnus-threading
570   :type 'string)
571
572 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
573   "*The format specification for the summary mode line.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions:
576
577 %G  Group name
578 %p  Unprefixed group name
579 %A  Current article number
580 %z  Current article score
581 %V  Gnus version
582 %U  Number of unread articles in the group
583 %e  Number of unselected articles in the group
584 %Z  A string with unread/unselected article counts
585 %g  Shortish group name
586 %S  Subject of the current article
587 %u  User-defined spec
588 %s  Current score file name
589 %d  Number of dormant articles
590 %r  Number of articles that have been marked as read in this session
591 %E  Number of articles expunged by the score files"
592   :group 'gnus-summary-format
593   :type 'string)
594
595 (defcustom gnus-list-identifiers nil
596   "Regexp that matches list identifiers to be removed from subject.
597 This can also be a list of regexps."
598   :version "21.1"
599   :group 'gnus-summary-format
600   :group 'gnus-article-hiding
601   :type '(choice (const :tag "none" nil)
602                  (regexp :value ".*")
603                  (repeat :value (".*") regexp)))
604
605 (defcustom gnus-summary-mark-below 0
606   "*Mark all articles with a score below this variable as read.
607 This variable is local to each summary buffer and usually set by the
608 score file."
609   :group 'gnus-score-default
610   :type 'integer)
611
612 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
613   "*List of functions used for sorting articles in the summary buffer.
614 This variable is only used when not using a threaded display."
615   :group 'gnus-summary-sort
616   :type '(repeat (choice (function-item gnus-article-sort-by-number)
617                          (function-item gnus-article-sort-by-author)
618                          (function-item gnus-article-sort-by-subject)
619                          (function-item gnus-article-sort-by-date)
620                          (function-item gnus-article-sort-by-score)
621                          (function :tag "other"))))
622
623 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
624   "*List of functions used for sorting threads in the summary buffer.
625 By default, threads are sorted by article number.
626
627 Each function takes two threads and return non-nil if the first thread
628 should be sorted before the other.  If you use more than one function,
629 the primary sort function should be the last.  You should probably
630 always include `gnus-thread-sort-by-number' in the list of sorting
631 functions -- preferably first.
632
633 Ready-made functions include `gnus-thread-sort-by-number',
634 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
635 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
636 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
637   :group 'gnus-summary-sort
638   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
639                          (function-item gnus-thread-sort-by-author)
640                          (function-item gnus-thread-sort-by-subject)
641                          (function-item gnus-thread-sort-by-date)
642                          (function-item gnus-thread-sort-by-score)
643                          (function-item gnus-thread-sort-by-total-score)
644                          (function :tag "other"))))
645
646 (defcustom gnus-thread-score-function '+
647   "*Function used for calculating the total score of a thread.
648
649 The function is called with the scores of the article and each
650 subthread and should then return the score of the thread.
651
652 Some functions you can use are `+', `max', or `min'."
653   :group 'gnus-summary-sort
654   :type 'function)
655
656 (defcustom gnus-summary-expunge-below nil
657   "All articles that have a score less than this variable will be expunged.
658 This variable is local to the summary buffers."
659   :group 'gnus-score-default
660   :type '(choice (const :tag "off" nil)
661                  integer))
662
663 (defcustom gnus-thread-expunge-below nil
664   "All threads that have a total score less than this variable will be expunged.
665 See `gnus-thread-score-function' for en explanation of what a
666 \"thread score\" is.
667
668 This variable is local to the summary buffers."
669   :group 'gnus-threading
670   :group 'gnus-score-default
671   :type '(choice (const :tag "off" nil)
672                  integer))
673
674 (defcustom gnus-summary-mode-hook nil
675   "*A hook for Gnus summary mode.
676 This hook is run before any variables are set in the summary buffer."
677   :options '(turn-on-gnus-mailing-list-mode)
678   :group 'gnus-summary-various
679   :type 'hook)
680
681 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
682 (when (featurep 'xemacs)
683   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
684   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
685   (add-hook 'gnus-summary-mode-hook
686             'gnus-xmas-switch-horizontal-scrollbar-off))
687
688 (defcustom gnus-summary-menu-hook nil
689   "*Hook run after the creation of the summary mode menu."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-summary-exit-hook nil
694   "*A hook called on exit from the summary buffer.
695 It will be called with point in the group buffer."
696   :group 'gnus-summary-exit
697   :type 'hook)
698
699 (defcustom gnus-summary-prepare-hook nil
700   "*A hook called after the summary buffer has been generated.
701 If you want to modify the summary buffer, you can use this hook."
702   :group 'gnus-summary-various
703   :type 'hook)
704
705 (defcustom gnus-summary-prepared-hook nil
706   "*A hook called as the last thing after the summary buffer has been generated."
707   :group 'gnus-summary-various
708   :type 'hook)
709
710 (defcustom gnus-summary-generate-hook nil
711   "*A hook run just before generating the summary buffer.
712 This hook is commonly used to customize threading variables and the
713 like."
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 (defcustom gnus-select-group-hook nil
718   "*A hook called when a newsgroup is selected.
719
720 If you'd like to simplify subjects like the
721 `gnus-summary-next-same-subject' command does, you can use the
722 following hook:
723
724  (setq gnus-select-group-hook
725       (list
726         (lambda ()
727           (mapcar (lambda (header)
728                      (mail-header-set-subject
729                       header
730                       (gnus-simplify-subject
731                        (mail-header-subject header) 're-only)))
732                   gnus-newsgroup-headers))))"
733   :group 'gnus-group-select
734   :type 'hook)
735
736 (defcustom gnus-select-article-hook nil
737   "*A hook called when an article is selected."
738   :group 'gnus-summary-choose
739   :type 'hook)
740
741 (defcustom gnus-visual-mark-article-hook
742   (list 'gnus-highlight-selected-summary)
743   "*Hook run after selecting an article in the summary buffer.
744 It is meant to be used for highlighting the article in some way.  It
745 is not run if `gnus-visual' is nil."
746   :group 'gnus-summary-visual
747   :type 'hook)
748
749 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
750   "*A hook called before parsing the headers."
751   :group 'gnus-various
752   :type 'hook)
753
754 (defcustom gnus-exit-group-hook nil
755   "*A hook called when exiting summary mode.
756 This hook is not called from the non-updating exit commands like `Q'."
757   :group 'gnus-various
758   :type 'hook)
759
760 (defcustom gnus-summary-update-hook
761   (list 'gnus-summary-highlight-line)
762   "*A hook called when a summary line is changed.
763 The hook will not be called if `gnus-visual' is nil.
764
765 The default function `gnus-summary-highlight-line' will
766 highlight the line according to the `gnus-summary-highlight'
767 variable."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
772   "*A hook called when an article is selected for the first time.
773 The hook is intended to mark an article as read (or unread)
774 automatically when it is selected."
775   :group 'gnus-summary-choose
776   :type 'hook)
777
778 (defcustom gnus-group-no-more-groups-hook nil
779   "*A hook run when returning to group mode having no more (unread) groups."
780   :group 'gnus-group-select
781   :type 'hook)
782
783 (defcustom gnus-ps-print-hook nil
784   "*A hook run before ps-printing something from Gnus."
785   :group 'gnus-summary
786   :type 'hook)
787
788 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
789   "Face used for highlighting the current article in the summary buffer."
790   :group 'gnus-summary-visual
791   :type 'face)
792
793 (defcustom gnus-summary-highlight
794   '(((= mark gnus-canceled-mark)
795      . gnus-summary-cancelled-face)
796     ((and (> score default)
797           (or (= mark gnus-dormant-mark)
798               (= mark gnus-ticked-mark)))
799      . gnus-summary-high-ticked-face)
800     ((and (< score default)
801           (or (= mark gnus-dormant-mark)
802               (= mark gnus-ticked-mark)))
803      . gnus-summary-low-ticked-face)
804     ((or (= mark gnus-dormant-mark)
805          (= mark gnus-ticked-mark))
806      . gnus-summary-normal-ticked-face)
807     ((and (> score default) (= mark gnus-ancient-mark))
808      . gnus-summary-high-ancient-face)
809     ((and (< score default) (= mark gnus-ancient-mark))
810      . gnus-summary-low-ancient-face)
811     ((= mark gnus-ancient-mark)
812      . gnus-summary-normal-ancient-face)
813     ((and (> score default) (= mark gnus-unread-mark))
814      . gnus-summary-high-unread-face)
815     ((and (< score default) (= mark gnus-unread-mark))
816      . gnus-summary-low-unread-face)
817     ((and (memq article gnus-newsgroup-incorporated)
818           (= mark gnus-unread-mark))
819      . gnus-summary-incorporated-face)
820     ((= mark gnus-unread-mark)
821      . gnus-summary-normal-unread-face)
822     ((and (> score default) (memq mark (list gnus-downloadable-mark
823                                              gnus-undownloaded-mark)))
824      . gnus-summary-high-unread-face)
825     ((and (< score default) (memq mark (list gnus-downloadable-mark
826                                              gnus-undownloaded-mark)))
827      . gnus-summary-low-unread-face)
828     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
829      . gnus-summary-normal-unread-face)
830     ((> score default)
831      . gnus-summary-high-read-face)
832     ((< score default)
833      . gnus-summary-low-read-face)
834     (t
835      . gnus-summary-normal-read-face))
836   "*Controls the highlighting of summary buffer lines.
837
838 A list of (FORM . FACE) pairs.  When deciding how a a particular
839 summary line should be displayed, each form is evaluated.  The content
840 of the face field after the first true form is used.  You can change
841 how those summary lines are displayed, by editing the face field.
842
843 You can use the following variables in the FORM field.
844
845 score:   The articles score
846 default: The default article score.
847 below:   The score below which articles are automatically marked as read.
848 mark:    The articles mark."
849   :group 'gnus-summary-visual
850   :type '(repeat (cons (sexp :tag "Form" nil)
851                        face)))
852
853 (defcustom gnus-alter-header-function nil
854   "Function called to allow alteration of article header structures.
855 The function is called with one parameter, the article header vector,
856 which it may alter in any way.")
857
858 (defvar gnus-decode-encoded-word-function
859   (mime-find-field-decoder 'From 'nov)
860   "Variable that says which function should be used to decode a string with encoded words.")
861
862 (defcustom gnus-extra-headers nil
863   "*Extra headers to parse."
864   :version "21.1"
865   :group 'gnus-summary
866   :type '(repeat symbol))
867
868 (defcustom gnus-ignored-from-addresses
869   (and user-mail-address (regexp-quote user-mail-address))
870   "*Regexp of From headers that may be suppressed in favor of To headers."
871   :version "21.1"
872   :group 'gnus-summary
873   :type 'regexp)
874
875 (defcustom gnus-group-charset-alist
876   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
877     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
878     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
879     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
880     ("^relcom\\>" koi8-r)
881     ("^fido7\\>" koi8-r)
882     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
883     ("^israel\\>" iso-8859-1)
884     ("^han\\>" euc-kr)
885     ("^alt.chinese.text.big5\\>" chinese-big5)
886     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
887     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
888     (".*" iso-8859-1))
889   "Alist of regexps (to match group names) and default charsets to be used when reading."
890   :type '(repeat (list (regexp :tag "Group")
891                        (symbol :tag "Charset")))
892   :group 'gnus-charset)
893
894 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
895   "List of charsets that should be ignored.
896 When these charsets are used in the \"charset\" parameter, the
897 default charset will be used instead."
898   :version "21.1"
899   :type '(repeat symbol)
900   :group 'gnus-charset)
901
902 (defcustom gnus-group-ignored-charsets-alist
903   '(("alt\\.chinese\\.text" iso-8859-1))
904   "Alist of regexps (to match group names) and charsets that should be ignored.
905 When these charsets are used in the \"charset\" parameter, the
906 default charset will be used instead."
907   :type '(repeat (cons (regexp :tag "Group")
908                        (repeat symbol)))
909   :group 'gnus-charset)
910
911 (defcustom gnus-group-highlight-words-alist nil
912   "Alist of group regexps and highlight regexps.
913 This variable uses the same syntax as `gnus-emphasis-alist'."
914   :version "21.1"
915   :type '(repeat (cons (regexp :tag "Group")
916                        (repeat (list (regexp :tag "Highlight regexp")
917                                      (number :tag "Group for entire word" 0)
918                                      (number :tag "Group for displayed part" 0)
919                                      (symbol :tag "Face"
920                                              gnus-emphasis-highlight-words)))))
921   :group 'gnus-summary-visual)
922
923 (defcustom gnus-use-wheel nil
924   "Use Intelli-mouse on summary movement"
925   :type 'boolean
926   :group 'gnus-summary-maneuvering)
927
928 (defcustom gnus-wheel-scroll-amount '(5 . 1)
929   "Amount to scroll messages by spinning the mouse wheel.
930 This is actually a cons cell, where the first item is the amount to scroll
931 on a normal wheel event, and the second is the amount to scroll when the
932 wheel is moved with the shift key depressed."
933   :type '(cons (integer :tag "Shift") integer)
934   :group 'gnus-summary-maneuvering)
935
936 (defcustom gnus-wheel-edge-resistance 2
937   "How hard it should be to change the current article
938 by moving the mouse over the edge of the article window."
939   :type 'integer
940   :group 'gnus-summary-maneuvering)
941
942 (defcustom gnus-summary-show-article-charset-alist
943   nil
944   "Alist of number and charset.
945 The article will be shown with the charset corresponding to the
946 numbered argument.
947 For example: ((1 . cn-gb-2312) (2 . big5))."
948   :version "21.1"
949   :type '(repeat (cons (number :tag "Argument" 1)
950                        (symbol :tag "Charset")))
951   :group 'gnus-charset)
952
953 (defcustom gnus-preserve-marks t
954   "Whether marks are preserved when moving, copying and respooling messages."
955   :version "21.1"
956   :type 'boolean
957   :group 'gnus-summary-marks)
958
959 (defcustom gnus-alter-articles-to-read-function nil
960   "Function to be called to alter the list of articles to be selected."
961   :type 'function
962   :group 'gnus-summary)
963
964 (defcustom gnus-orphan-score nil
965   "*All orphans get this score added.  Set in the score file."
966   :group 'gnus-score-default
967   :type '(choice (const nil)
968                  integer))
969
970 (defcustom gnus-summary-save-parts-default-mime "image/.*"
971   "*A regexp to match MIME parts when saving multiple parts of a message
972 with gnus-summary-save-parts (X m). This regexp will be used by default
973 when prompting the user for which type of files to save."
974   :group 'gnus-summary
975   :type 'regexp)
976
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 ;;; Internal variables
987
988 (defvar gnus-article-mime-handles nil)
989 (defvar gnus-article-decoded-p nil)
990 (defvar gnus-article-charset nil)
991 (defvar gnus-article-ignored-charsets nil)
992 (defvar gnus-scores-exclude-files nil)
993 (defvar gnus-page-broken nil)
994 (defvar gnus-inhibit-mime-unbuttonizing nil)
995
996 (defvar gnus-original-article nil)
997 (defvar gnus-article-internal-prepare-hook nil)
998 (defvar gnus-newsgroup-process-stack nil)
999
1000 (defvar gnus-thread-indent-array nil)
1001 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1002 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1003   "Function called to sort the articles within a thread after it has been gathered together.")
1004
1005 (defvar gnus-summary-save-parts-type-history nil)
1006 (defvar gnus-summary-save-parts-last-directory nil)
1007
1008 (defvar gnus-summary-save-parts-type-history nil)
1009 (defvar gnus-summary-save-parts-last-directory nil)
1010
1011 ;; Avoid highlighting in kill files.
1012 (defvar gnus-summary-inhibit-highlight nil)
1013 (defvar gnus-newsgroup-selected-overlay nil)
1014 (defvar gnus-inhibit-limiting nil)
1015 (defvar gnus-newsgroup-adaptive-score-file nil)
1016 (defvar gnus-current-score-file nil)
1017 (defvar gnus-current-move-group nil)
1018 (defvar gnus-current-copy-group nil)
1019 (defvar gnus-current-crosspost-group nil)
1020
1021 (defvar gnus-newsgroup-dependencies nil)
1022 (defvar gnus-newsgroup-adaptive nil)
1023 (defvar gnus-summary-display-article-function nil)
1024 (defvar gnus-summary-highlight-line-function nil
1025   "Function called after highlighting a summary line.")
1026
1027 (defvar gnus-summary-line-format-alist
1028   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1029     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1030     (?s gnus-tmp-subject-or-nil ?s)
1031     (?n gnus-tmp-name ?s)
1032     (?A (std11-address-string
1033          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1034     (?a (or (std11-full-name-string
1035              (car (mime-entity-read-field gnus-tmp-header 'From)))
1036             gnus-tmp-from) ?s)
1037     (?F gnus-tmp-from ?s)
1038     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1039     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1040     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1041     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1042     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1043     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1044     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1045     (?L gnus-tmp-lines ?d)
1046     (?I gnus-tmp-indentation ?s)
1047     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1048     (?R gnus-tmp-replied ?c)
1049     (?\[ gnus-tmp-opening-bracket ?c)
1050     (?\] gnus-tmp-closing-bracket ?c)
1051     (?\> (make-string gnus-tmp-level ? ) ?s)
1052     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1053     (?i gnus-tmp-score ?d)
1054     (?z gnus-tmp-score-char ?c)
1055     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1056     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1057     (?U gnus-tmp-unread ?c)
1058     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1059     (?t (gnus-summary-number-of-articles-in-thread
1060          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1061         ?d)
1062     (?e (gnus-summary-number-of-articles-in-thread
1063          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1064         ?c)
1065     (?u gnus-tmp-user-defined ?s)
1066     (?P (gnus-pick-line-number) ?d))
1067   "An alist of format specifications that can appear in summary lines.
1068 These are paired with what variables they correspond with, along with
1069 the type of the variable (string, integer, character, etc).")
1070
1071 (defvar gnus-summary-dummy-line-format-alist
1072   `((?S gnus-tmp-subject ?s)
1073     (?N gnus-tmp-number ?d)
1074     (?u gnus-tmp-user-defined ?s)))
1075
1076 (defvar gnus-summary-mode-line-format-alist
1077   `((?G gnus-tmp-group-name ?s)
1078     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1079     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1080     (?A gnus-tmp-article-number ?d)
1081     (?Z gnus-tmp-unread-and-unselected ?s)
1082     (?V gnus-version ?s)
1083     (?U gnus-tmp-unread-and-unticked ?d)
1084     (?S gnus-tmp-subject ?s)
1085     (?e gnus-tmp-unselected ?d)
1086     (?u gnus-tmp-user-defined ?s)
1087     (?d (length gnus-newsgroup-dormant) ?d)
1088     (?t (length gnus-newsgroup-marked) ?d)
1089     (?r (length gnus-newsgroup-reads) ?d)
1090     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1091     (?E gnus-newsgroup-expunged-tally ?d)
1092     (?s (gnus-current-score-file-nondirectory) ?s)))
1093
1094 (defvar gnus-last-search-regexp nil
1095   "Default regexp for article search command.")
1096
1097 (defvar gnus-summary-search-article-matched-data nil
1098   "Last matched data of article search command.  It is the local variable
1099 in `gnus-article-buffer' which consists of the list of start position,
1100 end position and text.")
1101
1102 (defvar gnus-last-shell-command nil
1103   "Default shell command on article.")
1104
1105 (defvar gnus-newsgroup-begin nil)
1106 (defvar gnus-newsgroup-end nil)
1107 (defvar gnus-newsgroup-last-rmail nil)
1108 (defvar gnus-newsgroup-last-mail nil)
1109 (defvar gnus-newsgroup-last-folder nil)
1110 (defvar gnus-newsgroup-last-file nil)
1111 (defvar gnus-newsgroup-auto-expire nil)
1112 (defvar gnus-newsgroup-active nil)
1113
1114 (defvar gnus-newsgroup-data nil)
1115 (defvar gnus-newsgroup-data-reverse nil)
1116 (defvar gnus-newsgroup-limit nil)
1117 (defvar gnus-newsgroup-limits nil)
1118
1119 (defvar gnus-newsgroup-unreads nil
1120   "List of unread articles in the current newsgroup.")
1121
1122 (defvar gnus-newsgroup-unselected nil
1123   "List of unselected unread articles in the current newsgroup.")
1124
1125 (defvar gnus-newsgroup-reads nil
1126   "Alist of read articles and article marks in the current newsgroup.")
1127
1128 (defvar gnus-newsgroup-expunged-tally nil)
1129
1130 (defvar gnus-newsgroup-marked nil
1131   "List of ticked articles in the current newsgroup (a subset of unread art).")
1132
1133 (defvar gnus-newsgroup-killed nil
1134   "List of ranges of articles that have been through the scoring process.")
1135
1136 (defvar gnus-newsgroup-cached nil
1137   "List of articles that come from the article cache.")
1138
1139 (defvar gnus-newsgroup-saved nil
1140   "List of articles that have been saved.")
1141
1142 (defvar gnus-newsgroup-kill-headers nil)
1143
1144 (defvar gnus-newsgroup-replied nil
1145   "List of articles that have been replied to in the current newsgroup.")
1146
1147 (defvar gnus-newsgroup-expirable nil
1148   "List of articles in the current newsgroup that can be expired.")
1149
1150 (defvar gnus-newsgroup-processable nil
1151   "List of articles in the current newsgroup that can be processed.")
1152
1153 (defvar gnus-newsgroup-downloadable nil
1154   "List of articles in the current newsgroup that can be processed.")
1155
1156 (defvar gnus-newsgroup-undownloaded nil
1157   "List of articles in the current newsgroup that haven't been downloaded..")
1158
1159 (defvar gnus-newsgroup-unsendable nil
1160   "List of articles in the current newsgroup that won't be sent.")
1161
1162 (defvar gnus-newsgroup-bookmarks nil
1163   "List of articles in the current newsgroup that have bookmarks.")
1164
1165 (defvar gnus-newsgroup-dormant nil
1166   "List of dormant articles in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-scored nil
1169   "List of scored articles in the current newsgroup.")
1170
1171 (defvar gnus-newsgroup-incorporated nil
1172   "List of incorporated articles in the current newsgroup.")
1173
1174 (defvar gnus-newsgroup-headers nil
1175   "List of article headers in the current newsgroup.")
1176
1177 (defvar gnus-newsgroup-threads nil)
1178
1179 (defvar gnus-newsgroup-prepared nil
1180   "Whether the current group has been prepared properly.")
1181
1182 (defvar gnus-newsgroup-ancient nil
1183   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-sparse nil)
1186
1187 (defvar gnus-current-article nil)
1188 (defvar gnus-article-current nil)
1189 (defvar gnus-current-headers nil)
1190 (defvar gnus-have-all-headers nil)
1191 (defvar gnus-last-article nil)
1192 (defvar gnus-newsgroup-history nil)
1193 (defvar gnus-newsgroup-charset nil)
1194 (defvar gnus-newsgroup-ephemeral-charset nil)
1195 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1196
1197 (defvar gnus-article-before-search nil)
1198
1199 (defconst gnus-summary-local-variables
1200   '(gnus-newsgroup-name
1201     gnus-newsgroup-begin gnus-newsgroup-end
1202     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1203     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1204     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1205     gnus-newsgroup-unselected gnus-newsgroup-marked
1206     gnus-newsgroup-reads gnus-newsgroup-saved
1207     gnus-newsgroup-replied gnus-newsgroup-expirable
1208     gnus-newsgroup-processable gnus-newsgroup-killed
1209     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1210     gnus-newsgroup-unsendable
1211     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1212     gnus-newsgroup-headers gnus-newsgroup-threads
1213     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1214     gnus-current-article gnus-current-headers gnus-have-all-headers
1215     gnus-last-article gnus-article-internal-prepare-hook
1216     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1217     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1218     gnus-thread-expunge-below
1219     gnus-score-alist gnus-current-score-file
1220     (gnus-summary-expunge-below . global)
1221     (gnus-summary-mark-below . global)
1222     (gnus-orphan-score . global)
1223     gnus-newsgroup-active gnus-scores-exclude-files
1224     gnus-newsgroup-history gnus-newsgroup-ancient
1225     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1226     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1227     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1228     (gnus-newsgroup-expunged-tally . 0)
1229     gnus-cache-removable-articles gnus-newsgroup-cached
1230     gnus-newsgroup-data gnus-newsgroup-data-reverse
1231     gnus-newsgroup-limit gnus-newsgroup-limits
1232     gnus-newsgroup-charset
1233     gnus-newsgroup-incorporated)
1234   "Variables that are buffer-local to the summary buffers.")
1235
1236 (defvar gnus-newsgroup-variables nil
1237   "Variables that have separate values in the newsgroups.")
1238
1239 ;; Byte-compiler warning.
1240 (eval-when-compile (defvar gnus-article-mode-map))
1241
1242 ;; Subject simplification.
1243
1244 (defun gnus-simplify-whitespace (str)
1245   "Remove excessive whitespace from STR."
1246   (let ((mystr str))
1247     ;; Multiple spaces.
1248     (while (string-match "[ \t][ \t]+" mystr)
1249       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1250                           " "
1251                           (substring mystr (match-end 0)))))
1252     ;; Leading spaces.
1253     (when (string-match "^[ \t]+" mystr)
1254       (setq mystr (substring mystr (match-end 0))))
1255     ;; Trailing spaces.
1256     (when (string-match "[ \t]+$" mystr)
1257       (setq mystr (substring mystr 0 (match-beginning 0))))
1258     mystr))
1259
1260 (defsubst gnus-simplify-subject-re (subject)
1261   "Remove \"Re:\" from subject lines."
1262   (if (string-match "^[Rr][Ee]: *" subject)
1263       (substring subject (match-end 0))
1264     subject))
1265
1266 (defun gnus-simplify-subject (subject &optional re-only)
1267   "Remove `Re:' and words in parentheses.
1268 If RE-ONLY is non-nil, strip leading `Re:'s only."
1269   (let ((case-fold-search t))           ;Ignore case.
1270     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1271     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1272       (setq subject (substring subject (match-end 0))))
1273     ;; Remove uninteresting prefixes.
1274     (when (and (not re-only)
1275                gnus-simplify-ignored-prefixes
1276                (string-match gnus-simplify-ignored-prefixes subject))
1277       (setq subject (substring subject (match-end 0))))
1278     ;; Remove words in parentheses from end.
1279     (unless re-only
1280       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1281         (setq subject (substring subject 0 (match-beginning 0)))))
1282     ;; Return subject string.
1283     subject))
1284
1285 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1286 ;; all whitespace.
1287 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1288   (goto-char (point-min))
1289   (while (re-search-forward regexp nil t)
1290     (replace-match (or newtext ""))))
1291
1292 (defun gnus-simplify-buffer-fuzzy ()
1293   "Simplify string in the buffer fuzzily.
1294 The string in the accessible portion of the current buffer is simplified.
1295 It is assumed to be a single-line subject.
1296 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1297 matter is removed.  Additional things can be deleted by setting
1298 `gnus-simplify-subject-fuzzy-regexp'."
1299   (let ((case-fold-search t)
1300         (modified-tick))
1301     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1302
1303     (while (not (eq modified-tick (buffer-modified-tick)))
1304       (setq modified-tick (buffer-modified-tick))
1305       (cond
1306        ((listp gnus-simplify-subject-fuzzy-regexp)
1307         (mapcar 'gnus-simplify-buffer-fuzzy-step
1308                 gnus-simplify-subject-fuzzy-regexp))
1309        (gnus-simplify-subject-fuzzy-regexp
1310         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1311       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1312       (gnus-simplify-buffer-fuzzy-step
1313        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1314       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1315
1316     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1317     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1318     (gnus-simplify-buffer-fuzzy-step " $")
1319     (gnus-simplify-buffer-fuzzy-step "^ +")))
1320
1321 (defun gnus-simplify-subject-fuzzy (subject)
1322   "Simplify a subject string fuzzily.
1323 See `gnus-simplify-buffer-fuzzy' for details."
1324   (save-excursion
1325     (gnus-set-work-buffer)
1326     (let ((case-fold-search t))
1327       ;; Remove uninteresting prefixes.
1328       (when (and gnus-simplify-ignored-prefixes
1329                  (string-match gnus-simplify-ignored-prefixes subject))
1330         (setq subject (substring subject (match-end 0))))
1331       (insert subject)
1332       (inline (gnus-simplify-buffer-fuzzy))
1333       (buffer-string))))
1334
1335 (defsubst gnus-simplify-subject-fully (subject)
1336   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1337   (cond
1338    (gnus-simplify-subject-functions
1339     (gnus-map-function gnus-simplify-subject-functions subject))
1340    ((null gnus-summary-gather-subject-limit)
1341     (gnus-simplify-subject-re subject))
1342    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1343     (gnus-simplify-subject-fuzzy subject))
1344    ((numberp gnus-summary-gather-subject-limit)
1345     (gnus-limit-string (gnus-simplify-subject-re subject)
1346                        gnus-summary-gather-subject-limit))
1347    (t
1348     subject)))
1349
1350 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1351   "Check whether two subjects are equal.
1352 If optional argument simple-first is t, first argument is already
1353 simplified."
1354   (cond
1355    ((null simple-first)
1356     (equal (gnus-simplify-subject-fully s1)
1357            (gnus-simplify-subject-fully s2)))
1358    (t
1359     (equal s1
1360            (gnus-simplify-subject-fully s2)))))
1361
1362 (defun gnus-summary-bubble-group ()
1363   "Increase the score of the current group.
1364 This is a handy function to add to `gnus-summary-exit-hook' to
1365 increase the score of each group you read."
1366   (gnus-group-add-score gnus-newsgroup-name))
1367
1368 \f
1369 ;;;
1370 ;;; Gnus summary mode
1371 ;;;
1372
1373 (put 'gnus-summary-mode 'mode-class 'special)
1374
1375 (defvar gnus-article-commands-menu)
1376
1377 (when t
1378   ;; Non-orthogonal keys
1379
1380   (gnus-define-keys gnus-summary-mode-map
1381     " " gnus-summary-next-page
1382     "\177" gnus-summary-prev-page
1383     [delete] gnus-summary-prev-page
1384     [backspace] gnus-summary-prev-page
1385     "\r" gnus-summary-scroll-up
1386     "\M-\r" gnus-summary-scroll-down
1387     "n" gnus-summary-next-unread-article
1388     "p" gnus-summary-prev-unread-article
1389     "N" gnus-summary-next-article
1390     "P" gnus-summary-prev-article
1391     "\M-\C-n" gnus-summary-next-same-subject
1392     "\M-\C-p" gnus-summary-prev-same-subject
1393     "\M-n" gnus-summary-next-unread-subject
1394     "\M-p" gnus-summary-prev-unread-subject
1395     "." gnus-summary-first-unread-article
1396     "," gnus-summary-best-unread-article
1397     "\M-s" gnus-summary-search-article-forward
1398     "\M-r" gnus-summary-search-article-backward
1399     "<" gnus-summary-beginning-of-article
1400     ">" gnus-summary-end-of-article
1401     "j" gnus-summary-goto-article
1402     "^" gnus-summary-refer-parent-article
1403     "\M-^" gnus-summary-refer-article
1404     "u" gnus-summary-tick-article-forward
1405     "!" gnus-summary-tick-article-forward
1406     "U" gnus-summary-tick-article-backward
1407     "d" gnus-summary-mark-as-read-forward
1408     "D" gnus-summary-mark-as-read-backward
1409     "E" gnus-summary-mark-as-expirable
1410     "\M-u" gnus-summary-clear-mark-forward
1411     "\M-U" gnus-summary-clear-mark-backward
1412     "k" gnus-summary-kill-same-subject-and-select
1413     "\C-k" gnus-summary-kill-same-subject
1414     "\M-\C-k" gnus-summary-kill-thread
1415     "\M-\C-l" gnus-summary-lower-thread
1416     "e" gnus-summary-edit-article
1417     "#" gnus-summary-mark-as-processable
1418     "\M-#" gnus-summary-unmark-as-processable
1419     "\M-\C-t" gnus-summary-toggle-threads
1420     "\M-\C-s" gnus-summary-show-thread
1421     "\M-\C-h" gnus-summary-hide-thread
1422     "\M-\C-f" gnus-summary-next-thread
1423     "\M-\C-b" gnus-summary-prev-thread
1424     [(meta down)] gnus-summary-next-thread
1425     [(meta up)] gnus-summary-prev-thread
1426     "\M-\C-u" gnus-summary-up-thread
1427     "\M-\C-d" gnus-summary-down-thread
1428     "&" gnus-summary-execute-command
1429     "c" gnus-summary-catchup-and-exit
1430     "\C-w" gnus-summary-mark-region-as-read
1431     "\C-t" gnus-summary-toggle-truncation
1432     "?" gnus-summary-mark-as-dormant
1433     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1434     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1435     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1436     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1437     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1438     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1439     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1440     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1441     "=" gnus-summary-expand-window
1442     "\C-x\C-s" gnus-summary-reselect-current-group
1443     "\M-g" gnus-summary-rescan-group
1444     "w" gnus-summary-stop-page-breaking
1445     "\C-c\C-r" gnus-summary-caesar-message
1446     "\M-t" gnus-summary-toggle-mime
1447     "f" gnus-summary-followup
1448     "F" gnus-summary-followup-with-original
1449     "C" gnus-summary-cancel-article
1450     "r" gnus-summary-reply
1451     "R" gnus-summary-reply-with-original
1452     "\C-c\C-f" gnus-summary-mail-forward
1453     "o" gnus-summary-save-article
1454     "\C-o" gnus-summary-save-article-mail
1455     "|" gnus-summary-pipe-output
1456     "\M-k" gnus-summary-edit-local-kill
1457     "\M-K" gnus-summary-edit-global-kill
1458     ;; "V" gnus-version
1459     "\C-c\C-d" gnus-summary-describe-group
1460     "q" gnus-summary-exit
1461     "Q" gnus-summary-exit-no-update
1462     "\C-c\C-i" gnus-info-find-node
1463     gnus-mouse-2 gnus-mouse-pick-article
1464     "m" gnus-summary-mail-other-window
1465     "a" gnus-summary-post-news
1466     "x" gnus-summary-limit-to-unread
1467     "s" gnus-summary-isearch-article
1468     "t" gnus-article-toggle-headers
1469     "g" gnus-summary-show-article
1470     "l" gnus-summary-goto-last-article
1471     "v" gnus-summary-preview-mime-message
1472     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1473     "\C-d" gnus-summary-enter-digest-group
1474     "\M-\C-d" gnus-summary-read-document
1475     "\M-\C-e" gnus-summary-edit-parameters
1476     "\M-\C-a" gnus-summary-customize-parameters
1477     "\C-c\C-b" gnus-bug
1478     "*" gnus-cache-enter-article
1479     "\M-*" gnus-cache-remove-article
1480     "\M-&" gnus-summary-universal-argument
1481     "\C-l" gnus-recenter
1482     "I" gnus-summary-increase-score
1483     "L" gnus-summary-lower-score
1484     "\M-i" gnus-symbolic-argument
1485     "h" gnus-summary-select-article-buffer
1486
1487     "V" gnus-summary-score-map
1488     "X" gnus-uu-extract-map
1489     "S" gnus-summary-send-map)
1490
1491   ;; Sort of orthogonal keymap
1492   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1493     "t" gnus-summary-tick-article-forward
1494     "!" gnus-summary-tick-article-forward
1495     "d" gnus-summary-mark-as-read-forward
1496     "r" gnus-summary-mark-as-read-forward
1497     "c" gnus-summary-clear-mark-forward
1498     " " gnus-summary-clear-mark-forward
1499     "e" gnus-summary-mark-as-expirable
1500     "x" gnus-summary-mark-as-expirable
1501     "?" gnus-summary-mark-as-dormant
1502     "b" gnus-summary-set-bookmark
1503     "B" gnus-summary-remove-bookmark
1504     "#" gnus-summary-mark-as-processable
1505     "\M-#" gnus-summary-unmark-as-processable
1506     "S" gnus-summary-limit-include-expunged
1507     "C" gnus-summary-catchup
1508     "H" gnus-summary-catchup-to-here
1509     "\C-c" gnus-summary-catchup-all
1510     "k" gnus-summary-kill-same-subject-and-select
1511     "K" gnus-summary-kill-same-subject
1512     "P" gnus-uu-mark-map)
1513
1514   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1515     "c" gnus-summary-clear-above
1516     "u" gnus-summary-tick-above
1517     "m" gnus-summary-mark-above
1518     "k" gnus-summary-kill-below)
1519
1520   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1521     "/" gnus-summary-limit-to-subject
1522     "n" gnus-summary-limit-to-articles
1523     "w" gnus-summary-pop-limit
1524     "s" gnus-summary-limit-to-subject
1525     "a" gnus-summary-limit-to-author
1526     "u" gnus-summary-limit-to-unread
1527     "m" gnus-summary-limit-to-marks
1528     "M" gnus-summary-limit-exclude-marks
1529     "v" gnus-summary-limit-to-score
1530     "*" gnus-summary-limit-include-cached
1531     "D" gnus-summary-limit-include-dormant
1532     "T" gnus-summary-limit-include-thread
1533     "d" gnus-summary-limit-exclude-dormant
1534     "t" gnus-summary-limit-to-age
1535     "x" gnus-summary-limit-to-extra
1536     "E" gnus-summary-limit-include-expunged
1537     "c" gnus-summary-limit-exclude-childless-dormant
1538     "C" gnus-summary-limit-mark-excluded-as-read)
1539
1540   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1541     "n" gnus-summary-next-unread-article
1542     "p" gnus-summary-prev-unread-article
1543     "N" gnus-summary-next-article
1544     "P" gnus-summary-prev-article
1545     "\C-n" gnus-summary-next-same-subject
1546     "\C-p" gnus-summary-prev-same-subject
1547     "\M-n" gnus-summary-next-unread-subject
1548     "\M-p" gnus-summary-prev-unread-subject
1549     "f" gnus-summary-first-unread-article
1550     "b" gnus-summary-best-unread-article
1551     "j" gnus-summary-goto-article
1552     "g" gnus-summary-goto-subject
1553     "l" gnus-summary-goto-last-article
1554     "o" gnus-summary-pop-article)
1555
1556   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1557     "k" gnus-summary-kill-thread
1558     "l" gnus-summary-lower-thread
1559     "i" gnus-summary-raise-thread
1560     "T" gnus-summary-toggle-threads
1561     "t" gnus-summary-rethread-current
1562     "^" gnus-summary-reparent-thread
1563     "s" gnus-summary-show-thread
1564     "S" gnus-summary-show-all-threads
1565     "h" gnus-summary-hide-thread
1566     "H" gnus-summary-hide-all-threads
1567     "n" gnus-summary-next-thread
1568     "p" gnus-summary-prev-thread
1569     "u" gnus-summary-up-thread
1570     "o" gnus-summary-top-thread
1571     "d" gnus-summary-down-thread
1572     "#" gnus-uu-mark-thread
1573     "\M-#" gnus-uu-unmark-thread)
1574
1575   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1576     "g" gnus-summary-prepare
1577     "c" gnus-summary-insert-cached-articles)
1578
1579   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1580     "c" gnus-summary-catchup-and-exit
1581     "C" gnus-summary-catchup-all-and-exit
1582     "E" gnus-summary-exit-no-update
1583     "J" gnus-summary-jump-to-other-group
1584     "Q" gnus-summary-exit
1585     "Z" gnus-summary-exit
1586     "n" gnus-summary-catchup-and-goto-next-group
1587     "R" gnus-summary-reselect-current-group
1588     "G" gnus-summary-rescan-group
1589     "N" gnus-summary-next-group
1590     "s" gnus-summary-save-newsrc
1591     "P" gnus-summary-prev-group)
1592
1593   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1594     " " gnus-summary-next-page
1595     "n" gnus-summary-next-page
1596     "\177" gnus-summary-prev-page
1597     [delete] gnus-summary-prev-page
1598     "p" gnus-summary-prev-page
1599     "\r" gnus-summary-scroll-up
1600     "\M-\r" gnus-summary-scroll-down
1601     "<" gnus-summary-beginning-of-article
1602     ">" gnus-summary-end-of-article
1603     "b" gnus-summary-beginning-of-article
1604     "e" gnus-summary-end-of-article
1605     "^" gnus-summary-refer-parent-article
1606     "r" gnus-summary-refer-parent-article
1607     "D" gnus-summary-enter-digest-group
1608     "R" gnus-summary-refer-references
1609     "T" gnus-summary-refer-thread
1610     "g" gnus-summary-show-article
1611     "s" gnus-summary-isearch-article
1612     "P" gnus-summary-print-article
1613     "M" gnus-mailing-list-insinuate
1614     "t" gnus-article-babel)
1615
1616   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1617     "b" gnus-article-add-buttons
1618     "B" gnus-article-add-buttons-to-head
1619     "o" gnus-article-treat-overstrike
1620     "e" gnus-article-emphasize
1621     "w" gnus-article-fill-cited-article
1622     "Q" gnus-article-fill-long-lines
1623     "C" gnus-article-capitalize-sentences
1624     "c" gnus-article-remove-cr
1625     "Z" gnus-article-decode-HZ
1626     "f" gnus-article-display-x-face
1627     "l" gnus-summary-stop-page-breaking
1628     "r" gnus-summary-caesar-message
1629     "t" gnus-article-toggle-headers
1630     "v" gnus-summary-verbose-headers
1631     "m" gnus-summary-toggle-mime
1632     "H" gnus-article-strip-headers-in-body
1633     "p" gnus-article-verify-x-pgp-sig
1634     "d" gnus-article-treat-dumbquotes
1635     "s" gnus-smiley-display)
1636
1637   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1638     "a" gnus-article-hide
1639     "h" gnus-article-toggle-headers
1640     "b" gnus-article-hide-boring-headers
1641     "s" gnus-article-hide-signature
1642     "c" gnus-article-hide-citation
1643     "C" gnus-article-hide-citation-in-followups
1644     "l" gnus-article-hide-list-identifiers
1645     "p" gnus-article-hide-pgp
1646     "B" gnus-article-strip-banner
1647     "P" gnus-article-hide-pem
1648     "\C-c" gnus-article-hide-citation-maybe)
1649
1650   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1651     "a" gnus-article-highlight
1652     "h" gnus-article-highlight-headers
1653     "c" gnus-article-highlight-citation
1654     "s" gnus-article-highlight-signature)
1655
1656   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1657     "z" gnus-article-date-ut
1658     "u" gnus-article-date-ut
1659     "l" gnus-article-date-local
1660     "p" gnus-article-date-english
1661     "e" gnus-article-date-lapsed
1662     "o" gnus-article-date-original
1663     "i" gnus-article-date-iso8601
1664     "s" gnus-article-date-user)
1665
1666   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1667     "t" gnus-article-remove-trailing-blank-lines
1668     "l" gnus-article-strip-leading-blank-lines
1669     "m" gnus-article-strip-multiple-blank-lines
1670     "a" gnus-article-strip-blank-lines
1671     "A" gnus-article-strip-all-blank-lines
1672     "s" gnus-article-strip-leading-space
1673     "e" gnus-article-strip-trailing-space)
1674
1675   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1676     "v" gnus-version
1677     "f" gnus-summary-fetch-faq
1678     "d" gnus-summary-describe-group
1679     "h" gnus-summary-describe-briefly
1680     "i" gnus-info-find-node)
1681
1682   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1683     "e" gnus-summary-expire-articles
1684     "\M-\C-e" gnus-summary-expire-articles-now
1685     "\177" gnus-summary-delete-article
1686     [delete] gnus-summary-delete-article
1687     [backspace] gnus-summary-delete-article
1688     "m" gnus-summary-move-article
1689     "r" gnus-summary-respool-article
1690     "w" gnus-summary-edit-article
1691     "c" gnus-summary-copy-article
1692     "B" gnus-summary-crosspost-article
1693     "q" gnus-summary-respool-query
1694     "t" gnus-summary-respool-trace
1695     "i" gnus-summary-import-article
1696     "p" gnus-summary-article-posted-p)
1697
1698   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1699     "o" gnus-summary-save-article
1700     "m" gnus-summary-save-article-mail
1701     "F" gnus-summary-write-article-file
1702     "r" gnus-summary-save-article-rmail
1703     "f" gnus-summary-save-article-file
1704     "b" gnus-summary-save-article-body-file
1705     "h" gnus-summary-save-article-folder
1706     "v" gnus-summary-save-article-vm
1707     "p" gnus-summary-pipe-output
1708     "s" gnus-soup-add-article)
1709
1710   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1711     "b" gnus-summary-display-buttonized
1712     "m" gnus-summary-repair-multipart
1713     "v" gnus-article-view-part
1714     "o" gnus-article-save-part
1715     "c" gnus-article-copy-part
1716     "C" gnus-article-view-part-as-charset
1717     "e" gnus-article-externalize-part
1718     "E" gnus-article-encrypt-body
1719     "i" gnus-article-inline-part
1720     "|" gnus-article-pipe-part))
1721
1722 (defun gnus-summary-make-menu-bar ()
1723   (gnus-turn-off-edit-menu 'summary)
1724
1725   (unless (boundp 'gnus-summary-misc-menu)
1726
1727     (easy-menu-define
1728      gnus-summary-kill-menu gnus-summary-mode-map ""
1729      (cons
1730       "Score"
1731       (nconc
1732        (list
1733         ["Customize" gnus-score-customize t])
1734        (gnus-make-score-map 'increase)
1735        (gnus-make-score-map 'lower)
1736        '(("Mark"
1737           ["Kill below" gnus-summary-kill-below t]
1738           ["Mark above" gnus-summary-mark-above t]
1739           ["Tick above" gnus-summary-tick-above t]
1740           ["Clear above" gnus-summary-clear-above t])
1741          ["Current score" gnus-summary-current-score t]
1742          ["Set score" gnus-summary-set-score t]
1743          ["Switch current score file..." gnus-score-change-score-file t]
1744          ["Set mark below..." gnus-score-set-mark-below t]
1745          ["Set expunge below..." gnus-score-set-expunge-below t]
1746          ["Edit current score file" gnus-score-edit-current-scores t]
1747          ["Edit score file" gnus-score-edit-file t]
1748          ["Trace score" gnus-score-find-trace t]
1749          ["Find words" gnus-score-find-favourite-words t]
1750          ["Rescore buffer" gnus-summary-rescore t]
1751          ["Increase score..." gnus-summary-increase-score t]
1752          ["Lower score..." gnus-summary-lower-score t]))))
1753
1754     ;; Define both the Article menu in the summary buffer and the equivalent
1755     ;; Commands menu in the article buffer here for consistency.
1756     (let ((innards
1757            `(("Hide"
1758               ["All" gnus-article-hide t]
1759               ["Headers" gnus-article-toggle-headers t]
1760               ["Signature" gnus-article-hide-signature t]
1761               ["Citation" gnus-article-hide-citation t]
1762               ["List identifiers" gnus-article-hide-list-identifiers t]
1763               ["PGP" gnus-article-hide-pgp t]
1764               ["Banner" gnus-article-strip-banner t]
1765               ["Boring headers" gnus-article-hide-boring-headers t])
1766              ("Highlight"
1767               ["All" gnus-article-highlight t]
1768               ["Headers" gnus-article-highlight-headers t]
1769               ["Signature" gnus-article-highlight-signature t]
1770               ["Citation" gnus-article-highlight-citation t])
1771              ("Date"
1772               ["Local" gnus-article-date-local t]
1773               ["ISO8601" gnus-article-date-iso8601 t]
1774               ["UT" gnus-article-date-ut t]
1775               ["Original" gnus-article-date-original t]
1776               ["Lapsed" gnus-article-date-lapsed t]
1777               ["User-defined" gnus-article-date-user t])
1778              ("Washing"
1779               ("Remove Blanks"
1780                ["Leading" gnus-article-strip-leading-blank-lines t]
1781                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1782                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1783                ["All of the above" gnus-article-strip-blank-lines t]
1784                ["All" gnus-article-strip-all-blank-lines t]
1785                ["Leading space" gnus-article-strip-leading-space t]
1786                ["Trailing space" gnus-article-strip-trailing-space t])
1787               ["Overstrike" gnus-article-treat-overstrike t]
1788               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1789               ["Emphasis" gnus-article-emphasize t]
1790               ["Word wrap" gnus-article-fill-cited-article t]
1791               ["Fill long lines" gnus-article-fill-long-lines t]
1792               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1793               ["CR" gnus-article-remove-cr t]
1794               ["Show X-Face" gnus-article-display-x-face t]
1795               ["Rot 13" gnus-summary-caesar-message
1796                ,@(if (featurep 'xemacs) '(t)
1797                    '(:help "\"Caesar rotate\" article by 13"))]
1798               ["Unix pipe" gnus-summary-pipe-message t]
1799               ["Add buttons" gnus-article-add-buttons t]
1800               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1801               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1802               ["Toggle MIME" gnus-summary-toggle-mime t]
1803               ["Verbose header" gnus-summary-verbose-headers t]
1804               ["Toggle header" gnus-summary-toggle-header t]
1805               ["Toggle smileys" gnus-smiley-display t]
1806               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1807               ["HZ" gnus-article-decode-HZ t])
1808              ("Output"
1809               ["Save in default format" gnus-summary-save-article
1810                ,@(if (featurep 'xemacs) '(t)
1811                    '(:help "Save article using default method"))]
1812               ["Save in file" gnus-summary-save-article-file
1813                ,@(if (featurep 'xemacs) '(t)
1814                    '(:help "Save article in file"))]
1815               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1816               ["Save in MH folder" gnus-summary-save-article-folder t]
1817               ["Save in VM folder" gnus-summary-save-article-vm t]
1818               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1819               ["Save body in file" gnus-summary-save-article-body-file t]
1820               ["Pipe through a filter" gnus-summary-pipe-output t]
1821               ["Add to SOUP packet" gnus-soup-add-article t]
1822               ["Print" gnus-summary-print-article t])
1823              ("Backend"
1824               ["Respool article..." gnus-summary-respool-article t]
1825               ["Move article..." gnus-summary-move-article
1826                (gnus-check-backend-function
1827                 'request-move-article gnus-newsgroup-name)]
1828               ["Copy article..." gnus-summary-copy-article t]
1829               ["Crosspost article..." gnus-summary-crosspost-article
1830                (gnus-check-backend-function
1831                 'request-replace-article gnus-newsgroup-name)]
1832               ["Import file..." gnus-summary-import-article t]
1833               ["Check if posted" gnus-summary-article-posted-p t]
1834               ["Edit article" gnus-summary-edit-article
1835                (not (gnus-group-read-only-p))]
1836               ["Delete article" gnus-summary-delete-article
1837                (gnus-check-backend-function
1838                 'request-expire-articles gnus-newsgroup-name)]
1839               ["Query respool" gnus-summary-respool-query t]
1840               ["Trace respool" gnus-summary-respool-trace t]
1841               ["Delete expirable articles" gnus-summary-expire-articles-now
1842                (gnus-check-backend-function
1843                 'request-expire-articles gnus-newsgroup-name)])
1844              ("Extract"
1845               ["Uudecode" gnus-uu-decode-uu
1846                ,@(if (featurep 'xemacs) '(t)
1847                    '(:help "Decode uuencoded article(s)"))]
1848               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1849               ["Unshar" gnus-uu-decode-unshar t]
1850               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1851               ["Save" gnus-uu-decode-save t]
1852               ["Binhex" gnus-uu-decode-binhex t]
1853               ["Postscript" gnus-uu-decode-postscript t])
1854              ("Cache"
1855               ["Enter article" gnus-cache-enter-article t]
1856               ["Remove article" gnus-cache-remove-article t])
1857              ["Translate" gnus-article-babel t]
1858              ["Select article buffer" gnus-summary-select-article-buffer t]
1859              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1860              ["Isearch article..." gnus-summary-isearch-article t]
1861              ["Beginning of the article" gnus-summary-beginning-of-article t]
1862              ["End of the article" gnus-summary-end-of-article t]
1863              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1864              ["Fetch referenced articles" gnus-summary-refer-references t]
1865              ["Fetch current thread" gnus-summary-refer-thread t]
1866              ["Fetch article with id..." gnus-summary-refer-article t]
1867              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1868              ["Redisplay" gnus-summary-show-article t])))
1869       (easy-menu-define
1870        gnus-summary-article-menu gnus-summary-mode-map ""
1871        (cons "Article" innards))
1872
1873       (if (not (keymapp gnus-summary-article-menu))
1874           (easy-menu-define
1875             gnus-article-commands-menu gnus-article-mode-map ""
1876             (cons "Commands" innards))
1877         ;; in Emacs, don't share menu.
1878         (setq gnus-article-commands-menu 
1879               (copy-keymap gnus-summary-article-menu))
1880         (define-key gnus-article-mode-map [menu-bar commands]
1881           (cons "Commands" gnus-article-commands-menu))))
1882
1883     (easy-menu-define
1884      gnus-summary-thread-menu gnus-summary-mode-map ""
1885      '("Threads"
1886        ["Toggle threading" gnus-summary-toggle-threads t]
1887        ["Hide threads" gnus-summary-hide-all-threads t]
1888        ["Show threads" gnus-summary-show-all-threads t]
1889        ["Hide thread" gnus-summary-hide-thread t]
1890        ["Show thread" gnus-summary-show-thread t]
1891        ["Go to next thread" gnus-summary-next-thread t]
1892        ["Go to previous thread" gnus-summary-prev-thread t]
1893        ["Go down thread" gnus-summary-down-thread t]
1894        ["Go up thread" gnus-summary-up-thread t]
1895        ["Top of thread" gnus-summary-top-thread t]
1896        ["Mark thread as read" gnus-summary-kill-thread t]
1897        ["Lower thread score" gnus-summary-lower-thread t]
1898        ["Raise thread score" gnus-summary-raise-thread t]
1899        ["Rethread current" gnus-summary-rethread-current t]))
1900
1901     (easy-menu-define
1902      gnus-summary-post-menu gnus-summary-mode-map ""
1903      `("Post"
1904        ["Post an article" gnus-summary-post-news
1905         ,@(if (featurep 'xemacs) '(t)
1906             '(:help "Post an article"))]
1907        ["Followup" gnus-summary-followup
1908         ,@(if (featurep 'xemacs) '(t)
1909             '(:help "Post followup to this article"))]
1910        ["Followup and yank" gnus-summary-followup-with-original
1911         ,@(if (featurep 'xemacs) '(t)
1912             '(:help "Post followup to this article, quoting its contents"))]
1913        ["Supersede article" gnus-summary-supersede-article t]
1914        ["Cancel article" gnus-summary-cancel-article
1915         ,@(if (featurep 'xemacs) '(t)
1916             '(:help "Cancel an article you posted"))]
1917        ["Reply" gnus-summary-reply t]
1918        ["Reply and yank" gnus-summary-reply-with-original t]
1919        ["Wide reply" gnus-summary-wide-reply t]
1920        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1921         ,@(if (featurep 'xemacs) '(t)
1922             '(:help "Mail a reply, quoting this article"))]
1923        ["Mail forward" gnus-summary-mail-forward t]
1924        ["Post forward" gnus-summary-post-forward t]
1925        ["Digest and mail" gnus-uu-digest-mail-forward t]
1926        ["Digest and post" gnus-uu-digest-post-forward t]
1927        ["Resend message" gnus-summary-resend-message t]
1928        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1929        ["Send a mail" gnus-summary-mail-other-window t]
1930        ["Uuencode and post" gnus-uu-post-news
1931         ,@(if (featurep 'xemacs) '(t)
1932             '(:help "Post a uuencoded article"))]
1933        ["Followup via news" gnus-summary-followup-to-mail t]
1934        ["Followup via news and yank"
1935         gnus-summary-followup-to-mail-with-original t]
1936        ;;("Draft"
1937        ;;["Send" gnus-summary-send-draft t]
1938        ;;["Send bounced" gnus-resend-bounced-mail t])
1939        ))
1940
1941     (easy-menu-define
1942      gnus-summary-misc-menu gnus-summary-mode-map ""
1943      `("Misc"
1944        ("Mark Read"
1945         ["Mark as read" gnus-summary-mark-as-read-forward t]
1946         ["Mark same subject and select"
1947          gnus-summary-kill-same-subject-and-select t]
1948         ["Mark same subject" gnus-summary-kill-same-subject t]
1949         ["Catchup" gnus-summary-catchup
1950          ,@(if (featurep 'xemacs) '(t)
1951              '(:help "Mark unread articles in this group as read"))]
1952         ["Catchup all" gnus-summary-catchup-all t]
1953         ["Catchup to here" gnus-summary-catchup-to-here t]
1954         ["Catchup region" gnus-summary-mark-region-as-read t]
1955         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1956        ("Mark Various"
1957         ["Tick" gnus-summary-tick-article-forward t]
1958         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1959         ["Remove marks" gnus-summary-clear-mark-forward t]
1960         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1961         ["Set bookmark" gnus-summary-set-bookmark t]
1962         ["Remove bookmark" gnus-summary-remove-bookmark t])
1963        ("Mark Limit"
1964         ["Marks..." gnus-summary-limit-to-marks t]
1965         ["Subject..." gnus-summary-limit-to-subject t]
1966         ["Author..." gnus-summary-limit-to-author t]
1967         ["Age..." gnus-summary-limit-to-age t]
1968         ["Extra..." gnus-summary-limit-to-extra t]
1969         ["Score" gnus-summary-limit-to-score t]
1970         ["Unread" gnus-summary-limit-to-unread t]
1971         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1972         ["Articles" gnus-summary-limit-to-articles t]
1973         ["Pop limit" gnus-summary-pop-limit t]
1974         ["Show dormant" gnus-summary-limit-include-dormant t]
1975         ["Hide childless dormant"
1976          gnus-summary-limit-exclude-childless-dormant t]
1977         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1978         ["Hide marked" gnus-summary-limit-exclude-marks t]
1979         ["Show expunged" gnus-summary-show-all-expunged t])
1980        ("Process Mark"
1981         ["Set mark" gnus-summary-mark-as-processable t]
1982         ["Remove mark" gnus-summary-unmark-as-processable t]
1983         ["Remove all marks" gnus-summary-unmark-all-processable t]
1984         ["Mark above" gnus-uu-mark-over t]
1985         ["Mark series" gnus-uu-mark-series t]
1986         ["Mark region" gnus-uu-mark-region t]
1987         ["Unmark region" gnus-uu-unmark-region t]
1988         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1989         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1990         ["Mark all" gnus-uu-mark-all t]
1991         ["Mark buffer" gnus-uu-mark-buffer t]
1992         ["Mark sparse" gnus-uu-mark-sparse t]
1993         ["Mark thread" gnus-uu-mark-thread t]
1994         ["Unmark thread" gnus-uu-unmark-thread t]
1995         ("Process Mark Sets"
1996          ["Kill" gnus-summary-kill-process-mark t]
1997          ["Yank" gnus-summary-yank-process-mark
1998           gnus-newsgroup-process-stack]
1999          ["Save" gnus-summary-save-process-mark t]))
2000        ("Scroll article"
2001         ["Page forward" gnus-summary-next-page
2002          ,@(if (featurep 'xemacs) '(t)
2003              '(:help "Show next page of article"))]
2004         ["Page backward" gnus-summary-prev-page
2005          ,@(if (featurep 'xemacs) '(t)
2006              '(:help "Show previous page of article"))]
2007         ["Line forward" gnus-summary-scroll-up t])
2008        ("Move"
2009         ["Next unread article" gnus-summary-next-unread-article t]
2010         ["Previous unread article" gnus-summary-prev-unread-article t]
2011         ["Next article" gnus-summary-next-article t]
2012         ["Previous article" gnus-summary-prev-article t]
2013         ["Next unread subject" gnus-summary-next-unread-subject t]
2014         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2015         ["Next article same subject" gnus-summary-next-same-subject t]
2016         ["Previous article same subject" gnus-summary-prev-same-subject t]
2017         ["First unread article" gnus-summary-first-unread-article t]
2018         ["Best unread article" gnus-summary-best-unread-article t]
2019         ["Go to subject number..." gnus-summary-goto-subject t]
2020         ["Go to article number..." gnus-summary-goto-article t]
2021         ["Go to the last article" gnus-summary-goto-last-article t]
2022         ["Pop article off history" gnus-summary-pop-article t])
2023        ("Sort"
2024         ["Sort by number" gnus-summary-sort-by-number t]
2025         ["Sort by author" gnus-summary-sort-by-author t]
2026         ["Sort by subject" gnus-summary-sort-by-subject t]
2027         ["Sort by date" gnus-summary-sort-by-date t]
2028         ["Sort by score" gnus-summary-sort-by-score t]
2029         ["Sort by lines" gnus-summary-sort-by-lines t]
2030         ["Sort by characters" gnus-summary-sort-by-chars t])
2031        ("Help"
2032         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2033         ["Describe group" gnus-summary-describe-group t]
2034         ["Read manual" gnus-info-find-node t])
2035        ("Modes"
2036         ["Pick and read" gnus-pick-mode t]
2037         ["Binary" gnus-binary-mode t])
2038        ("Regeneration"
2039         ["Regenerate" gnus-summary-prepare t]
2040         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2041         ["Toggle threading" gnus-summary-toggle-threads t])
2042        ["Filter articles..." gnus-summary-execute-command t]
2043        ["Run command on subjects..." gnus-summary-universal-argument t]
2044        ["Search articles forward..." gnus-summary-search-article-forward t]
2045        ["Search articles backward..." gnus-summary-search-article-backward t]
2046        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2047        ["Expand window" gnus-summary-expand-window t]
2048        ["Expire expirable articles" gnus-summary-expire-articles
2049         (gnus-check-backend-function
2050          'request-expire-articles gnus-newsgroup-name)]
2051        ["Edit local kill file" gnus-summary-edit-local-kill t]
2052        ["Edit main kill file" gnus-summary-edit-global-kill t]
2053        ["Edit group parameters" gnus-summary-edit-parameters t]
2054        ["Customize group parameters" gnus-summary-customize-parameters t]
2055        ["Send a bug report" gnus-bug t]
2056        ("Exit"
2057         ["Catchup and exit" gnus-summary-catchup-and-exit
2058          ,@(if (featurep 'xemacs) '(t)
2059              '(:help "Mark unread articles in this group as read, then exit"))]
2060         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2061         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2062         ["Exit group" gnus-summary-exit
2063          ,@(if (featurep 'xemacs) '(t)
2064              '(:help "Exit current group, return to group selection mode"))]
2065         ["Exit group without updating" gnus-summary-exit-no-update t]
2066         ["Exit and goto next group" gnus-summary-next-group t]
2067         ["Exit and goto prev group" gnus-summary-prev-group t]
2068         ["Reselect group" gnus-summary-reselect-current-group t]
2069         ["Rescan group" gnus-summary-rescan-group t]
2070         ["Update dribble" gnus-summary-save-newsrc t])))
2071
2072     (gnus-run-hooks 'gnus-summary-menu-hook)))
2073
2074 (defvar gnus-summary-tool-bar-map nil)
2075
2076 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2077 (defun gnus-summary-make-tool-bar ()
2078   (if (and (fboundp 'tool-bar-add-item-from-menu)
2079            (default-value 'tool-bar-mode)
2080            (not gnus-summary-tool-bar-map))
2081       (setq gnus-summary-tool-bar-map
2082             (let ((tool-bar-map (make-sparse-keymap))
2083                   (load-path (mm-image-load-path)))
2084               (tool-bar-add-item-from-menu
2085                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2086               (tool-bar-add-item-from-menu
2087                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2088               (tool-bar-add-item-from-menu
2089                'gnus-summary-post-news "post" gnus-summary-mode-map)
2090               (tool-bar-add-item-from-menu
2091                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2092               (tool-bar-add-item-from-menu
2093                'gnus-summary-followup "followup" gnus-summary-mode-map)
2094               (tool-bar-add-item-from-menu
2095                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2096               (tool-bar-add-item-from-menu
2097                'gnus-summary-reply "reply" gnus-summary-mode-map)
2098               (tool-bar-add-item-from-menu
2099                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2100               (tool-bar-add-item-from-menu
2101                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2102               (tool-bar-add-item-from-menu
2103                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2104               (tool-bar-add-item-from-menu
2105                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2106               (tool-bar-add-item-from-menu
2107                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2108               (tool-bar-add-item-from-menu
2109                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2110               (tool-bar-add-item-from-menu
2111                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2112               (tool-bar-add-item-from-menu
2113                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2114               tool-bar-map)))
2115   (if gnus-summary-tool-bar-map
2116       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2117
2118 (defun gnus-score-set-default (var value)
2119   "A version of set that updates the GNU Emacs menu-bar."
2120   (set var value)
2121   ;; It is the message that forces the active status to be updated.
2122   (message ""))
2123
2124 (defun gnus-make-score-map (type)
2125   "Make a summary score map of type TYPE."
2126   (if t
2127       nil
2128     (let ((headers '(("author" "from" string)
2129                      ("subject" "subject" string)
2130                      ("article body" "body" string)
2131                      ("article head" "head" string)
2132                      ("xref" "xref" string)
2133                      ("extra header" "extra" string)
2134                      ("lines" "lines" number)
2135                      ("followups to author" "followup" string)))
2136           (types '((number ("less than" <)
2137                            ("greater than" >)
2138                            ("equal" =))
2139                    (string ("substring" s)
2140                            ("exact string" e)
2141                            ("fuzzy string" f)
2142                            ("regexp" r))))
2143           (perms '(("temporary" (current-time-string))
2144                    ("permanent" nil)
2145                    ("immediate" now)))
2146           header)
2147       (list
2148        (apply
2149         'nconc
2150         (list
2151          (if (eq type 'lower)
2152              "Lower score"
2153            "Increase score"))
2154         (let (outh)
2155           (while headers
2156             (setq header (car headers))
2157             (setq outh
2158                   (cons
2159                    (apply
2160                     'nconc
2161                     (list (car header))
2162                     (let ((ts (cdr (assoc (nth 2 header) types)))
2163                           outt)
2164                       (while ts
2165                         (setq outt
2166                               (cons
2167                                (apply
2168                                 'nconc
2169                                 (list (caar ts))
2170                                 (let ((ps perms)
2171                                       outp)
2172                                   (while ps
2173                                     (setq outp
2174                                           (cons
2175                                            (vector
2176                                             (caar ps)
2177                                             (list
2178                                              'gnus-summary-score-entry
2179                                              (nth 1 header)
2180                                              (if (or (string= (nth 1 header)
2181                                                               "head")
2182                                                      (string= (nth 1 header)
2183                                                               "body"))
2184                                                  ""
2185                                                (list 'gnus-summary-header
2186                                                      (nth 1 header)))
2187                                              (list 'quote (nth 1 (car ts)))
2188                                              (list 'gnus-score-delta-default
2189                                                    nil)
2190                                              (nth 1 (car ps))
2191                                              t)
2192                                             t)
2193                                            outp))
2194                                     (setq ps (cdr ps)))
2195                                   (list (nreverse outp))))
2196                                outt))
2197                         (setq ts (cdr ts)))
2198                       (list (nreverse outt))))
2199                    outh))
2200             (setq headers (cdr headers)))
2201           (list (nreverse outh))))))))
2202
2203 \f
2204
2205 (defun gnus-summary-mode (&optional group)
2206   "Major mode for reading articles.
2207
2208 All normal editing commands are switched off.
2209 \\<gnus-summary-mode-map>
2210 Each line in this buffer represents one article.  To read an
2211 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2212 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2213 respectively.
2214
2215 You can also post articles and send mail from this buffer.  To
2216 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2217 of an article, type `\\[gnus-summary-reply]'.
2218
2219 There are approx. one gazillion commands you can execute in this
2220 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2221
2222 The following commands are available:
2223
2224 \\{gnus-summary-mode-map}"
2225   (interactive)
2226   (kill-all-local-variables)
2227   (when (gnus-visual-p 'summary-menu 'menu)
2228     (gnus-summary-make-menu-bar)
2229     (gnus-summary-make-tool-bar))
2230   (gnus-summary-make-local-variables)
2231   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2232     (gnus-summary-make-local-variables))
2233   (gnus-make-thread-indent-array)
2234   (gnus-simplify-mode-line)
2235   (setq major-mode 'gnus-summary-mode)
2236   (setq mode-name "Summary")
2237   (make-local-variable 'minor-mode-alist)
2238   (use-local-map gnus-summary-mode-map)
2239   (buffer-disable-undo)
2240   (setq buffer-read-only t)             ;Disable modification
2241   (setq truncate-lines t)
2242   (setq selective-display t)
2243   (setq selective-display-ellipses t)   ;Display `...'
2244   (gnus-summary-set-display-table)
2245   (gnus-set-default-directory)
2246   (setq gnus-newsgroup-name group)
2247   (unless (gnus-news-group-p group)
2248     (setq gnus-newsgroup-incorporated
2249           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2250   (make-local-variable 'gnus-summary-line-format)
2251   (make-local-variable 'gnus-summary-line-format-spec)
2252   (make-local-variable 'gnus-summary-dummy-line-format)
2253   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2254   (make-local-variable 'gnus-summary-mark-positions)
2255   (make-local-hook 'pre-command-hook)
2256   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2257   (gnus-run-hooks 'gnus-summary-mode-hook)
2258   (turn-on-gnus-mailing-list-mode)
2259   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2260   (gnus-update-summary-mark-positions))
2261
2262 (defun gnus-summary-make-local-variables ()
2263   "Make all the local summary buffer variables."
2264   (let (global)
2265     (dolist (local gnus-summary-local-variables)
2266       (if (consp local)
2267           (progn
2268             (if (eq (cdr local) 'global)
2269                 ;; Copy the global value of the variable.
2270                 (setq global (symbol-value (car local)))
2271               ;; Use the value from the list.
2272               (setq global (eval (cdr local))))
2273             (set (make-local-variable (car local)) global))
2274         ;; Simple nil-valued local variable.
2275         (set (make-local-variable local) nil)))))
2276
2277 (defun gnus-summary-clear-local-variables ()
2278   (let ((locals gnus-summary-local-variables))
2279     (while locals
2280       (if (consp (car locals))
2281           (and (vectorp (caar locals))
2282                (set (caar locals) nil))
2283         (and (vectorp (car locals))
2284              (set (car locals) nil)))
2285       (setq locals (cdr locals)))))
2286
2287 ;; Summary data functions.
2288
2289 (defmacro gnus-data-number (data)
2290   `(car ,data))
2291
2292 (defmacro gnus-data-set-number (data number)
2293   `(setcar ,data ,number))
2294
2295 (defmacro gnus-data-mark (data)
2296   `(nth 1 ,data))
2297
2298 (defmacro gnus-data-set-mark (data mark)
2299   `(setcar (nthcdr 1 ,data) ,mark))
2300
2301 (defmacro gnus-data-pos (data)
2302   `(nth 2 ,data))
2303
2304 (defmacro gnus-data-set-pos (data pos)
2305   `(setcar (nthcdr 2 ,data) ,pos))
2306
2307 (defmacro gnus-data-header (data)
2308   `(nth 3 ,data))
2309
2310 (defmacro gnus-data-set-header (data header)
2311   `(setcar (nthcdr 3 ,data) ,header))
2312
2313 (defmacro gnus-data-level (data)
2314   `(nth 4 ,data))
2315
2316 (defmacro gnus-data-unread-p (data)
2317   `(= (nth 1 ,data) gnus-unread-mark))
2318
2319 (defmacro gnus-data-read-p (data)
2320   `(/= (nth 1 ,data) gnus-unread-mark))
2321
2322 (defmacro gnus-data-pseudo-p (data)
2323   `(consp (nth 3 ,data)))
2324
2325 (defmacro gnus-data-find (number)
2326   `(assq ,number gnus-newsgroup-data))
2327
2328 (defmacro gnus-data-find-list (number &optional data)
2329   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2330      (memq (assq ,number bdata)
2331            bdata)))
2332
2333 (defmacro gnus-data-make (number mark pos header level)
2334   `(list ,number ,mark ,pos ,header ,level))
2335
2336 (defun gnus-data-enter (after-article number mark pos header level offset)
2337   (let ((data (gnus-data-find-list after-article)))
2338     (unless data
2339       (error "No such article: %d" after-article))
2340     (setcdr data (cons (gnus-data-make number mark pos header level)
2341                        (cdr data)))
2342     (setq gnus-newsgroup-data-reverse nil)
2343     (gnus-data-update-list (cddr data) offset)))
2344
2345 (defun gnus-data-enter-list (after-article list &optional offset)
2346   (when list
2347     (let ((data (and after-article (gnus-data-find-list after-article)))
2348           (ilist list))
2349       (if (not (or data
2350                    after-article))
2351           (let ((odata gnus-newsgroup-data))
2352             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2353             (when offset
2354               (gnus-data-update-list odata offset)))
2355         ;; Find the last element in the list to be spliced into the main
2356         ;; list.
2357         (while (cdr list)
2358           (setq list (cdr list)))
2359         (if (not data)
2360             (progn
2361               (setcdr list gnus-newsgroup-data)
2362               (setq gnus-newsgroup-data ilist)
2363               (when offset
2364                 (gnus-data-update-list (cdr list) offset)))
2365           (setcdr list (cdr data))
2366           (setcdr data ilist)
2367           (when offset
2368             (gnus-data-update-list (cdr list) offset))))
2369       (setq gnus-newsgroup-data-reverse nil))))
2370
2371 (defun gnus-data-remove (article &optional offset)
2372   (let ((data gnus-newsgroup-data))
2373     (if (= (gnus-data-number (car data)) article)
2374         (progn
2375           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2376                 gnus-newsgroup-data-reverse nil)
2377           (when offset
2378             (gnus-data-update-list gnus-newsgroup-data offset)))
2379       (while (cdr data)
2380         (when (= (gnus-data-number (cadr data)) article)
2381           (setcdr data (cddr data))
2382           (when offset
2383             (gnus-data-update-list (cdr data) offset))
2384           (setq data nil
2385                 gnus-newsgroup-data-reverse nil))
2386         (setq data (cdr data))))))
2387
2388 (defmacro gnus-data-list (backward)
2389   `(if ,backward
2390        (or gnus-newsgroup-data-reverse
2391            (setq gnus-newsgroup-data-reverse
2392                  (reverse gnus-newsgroup-data)))
2393      gnus-newsgroup-data))
2394
2395 (defun gnus-data-update-list (data offset)
2396   "Add OFFSET to the POS of all data entries in DATA."
2397   (setq gnus-newsgroup-data-reverse nil)
2398   (while data
2399     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2400     (setq data (cdr data))))
2401
2402 (defun gnus-summary-article-pseudo-p (article)
2403   "Say whether this article is a pseudo article or not."
2404   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2405
2406 (defmacro gnus-summary-article-sparse-p (article)
2407   "Say whether this article is a sparse article or not."
2408   `(memq ,article gnus-newsgroup-sparse))
2409
2410 (defmacro gnus-summary-article-ancient-p (article)
2411   "Say whether this article is a sparse article or not."
2412   `(memq ,article gnus-newsgroup-ancient))
2413
2414 (defun gnus-article-parent-p (number)
2415   "Say whether this article is a parent or not."
2416   (let ((data (gnus-data-find-list number)))
2417     (and (cdr data)                     ; There has to be an article after...
2418          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2419             (gnus-data-level (nth 1 data))))))
2420
2421 (defun gnus-article-children (number)
2422   "Return a list of all children to NUMBER."
2423   (let* ((data (gnus-data-find-list number))
2424          (level (gnus-data-level (car data)))
2425          children)
2426     (setq data (cdr data))
2427     (while (and data
2428                 (= (gnus-data-level (car data)) (1+ level)))
2429       (push (gnus-data-number (car data)) children)
2430       (setq data (cdr data)))
2431     children))
2432
2433 (defmacro gnus-summary-skip-intangible ()
2434   "If the current article is intangible, then jump to a different article."
2435   '(let ((to (get-text-property (point) 'gnus-intangible)))
2436      (and to (gnus-summary-goto-subject to))))
2437
2438 (defmacro gnus-summary-article-intangible-p ()
2439   "Say whether this article is intangible or not."
2440   '(get-text-property (point) 'gnus-intangible))
2441
2442 (defun gnus-article-read-p (article)
2443   "Say whether ARTICLE is read or not."
2444   (not (or (memq article gnus-newsgroup-marked)
2445            (memq article gnus-newsgroup-unreads)
2446            (memq article gnus-newsgroup-unselected)
2447            (memq article gnus-newsgroup-dormant))))
2448
2449 ;; Some summary mode macros.
2450
2451 (defmacro gnus-summary-article-number ()
2452   "The article number of the article on the current line.
2453 If there isn's an article number here, then we return the current
2454 article number."
2455   '(progn
2456      (gnus-summary-skip-intangible)
2457      (or (get-text-property (point) 'gnus-number)
2458          (gnus-summary-last-subject))))
2459
2460 (defmacro gnus-summary-article-header (&optional number)
2461   "Return the header of article NUMBER."
2462   `(gnus-data-header (gnus-data-find
2463                       ,(or number '(gnus-summary-article-number)))))
2464
2465 (defmacro gnus-summary-thread-level (&optional number)
2466   "Return the level of thread that starts with article NUMBER."
2467   `(if (and (eq gnus-summary-make-false-root 'dummy)
2468             (get-text-property (point) 'gnus-intangible))
2469        0
2470      (gnus-data-level (gnus-data-find
2471                        ,(or number '(gnus-summary-article-number))))))
2472
2473 (defmacro gnus-summary-article-mark (&optional number)
2474   "Return the mark of article NUMBER."
2475   `(gnus-data-mark (gnus-data-find
2476                     ,(or number '(gnus-summary-article-number)))))
2477
2478 (defmacro gnus-summary-article-pos (&optional number)
2479   "Return the position of the line of article NUMBER."
2480   `(gnus-data-pos (gnus-data-find
2481                    ,(or number '(gnus-summary-article-number)))))
2482
2483 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2484 (defmacro gnus-summary-article-subject (&optional number)
2485   "Return current subject string or nil if nothing."
2486   `(let ((headers
2487           ,(if number
2488                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2489              '(gnus-data-header (assq (gnus-summary-article-number)
2490                                       gnus-newsgroup-data)))))
2491      (and headers
2492           (vectorp headers)
2493           (mail-header-subject headers))))
2494
2495 (defmacro gnus-summary-article-score (&optional number)
2496   "Return current article score."
2497   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2498                   gnus-newsgroup-scored))
2499        gnus-summary-default-score 0))
2500
2501 (defun gnus-summary-article-children (&optional number)
2502   "Return a list of article numbers that are children of article NUMBER."
2503   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2504          (level (gnus-data-level (car data)))
2505          l children)
2506     (while (and (setq data (cdr data))
2507                 (> (setq l (gnus-data-level (car data))) level))
2508       (and (= (1+ level) l)
2509            (push (gnus-data-number (car data))
2510                  children)))
2511     (nreverse children)))
2512
2513 (defun gnus-summary-article-parent (&optional number)
2514   "Return the article number of the parent of article NUMBER."
2515   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2516                                     (gnus-data-list t)))
2517          (level (gnus-data-level (car data))))
2518     (if (zerop level)
2519         ()                              ; This is a root.
2520       ;; We search until we find an article with a level less than
2521       ;; this one.  That function has to be the parent.
2522       (while (and (setq data (cdr data))
2523                   (not (< (gnus-data-level (car data)) level))))
2524       (and data (gnus-data-number (car data))))))
2525
2526 (defun gnus-unread-mark-p (mark)
2527   "Say whether MARK is the unread mark."
2528   (= mark gnus-unread-mark))
2529
2530 (defun gnus-read-mark-p (mark)
2531   "Say whether MARK is one of the marks that mark as read.
2532 This is all marks except unread, ticked, dormant, and expirable."
2533   (not (or (= mark gnus-unread-mark)
2534            (= mark gnus-ticked-mark)
2535            (= mark gnus-dormant-mark)
2536            (= mark gnus-expirable-mark))))
2537
2538 (defmacro gnus-article-mark (number)
2539   "Return the MARK of article NUMBER.
2540 This macro should only be used when computing the mark the \"first\"
2541 time; i.e., when generating the summary lines.  After that,
2542 `gnus-summary-article-mark' should be used to examine the
2543 marks of articles."
2544   `(cond
2545     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2546     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2547     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2548     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2549     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2550     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2551     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2552     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2553            gnus-ancient-mark))))
2554
2555 ;; Saving hidden threads.
2556
2557 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2558 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2559
2560 (defmacro gnus-save-hidden-threads (&rest forms)
2561   "Save hidden threads, eval FORMS, and restore the hidden threads."
2562   (let ((config (make-symbol "config")))
2563     `(let ((,config (gnus-hidden-threads-configuration)))
2564        (unwind-protect
2565            (save-excursion
2566              ,@forms)
2567          (gnus-restore-hidden-threads-configuration ,config)))))
2568
2569 (defun gnus-data-compute-positions ()
2570   "Compute the positions of all articles."
2571   (setq gnus-newsgroup-data-reverse nil)
2572   (let ((data gnus-newsgroup-data))
2573     (save-excursion
2574       (gnus-save-hidden-threads
2575         (gnus-summary-show-all-threads)
2576         (goto-char (point-min))
2577         (while data
2578           (while (get-text-property (point) 'gnus-intangible)
2579             (forward-line 1))
2580           (gnus-data-set-pos (car data) (+ (point) 3))
2581           (setq data (cdr data))
2582           (forward-line 1))))))
2583
2584 (defun gnus-hidden-threads-configuration ()
2585   "Return the current hidden threads configuration."
2586   (save-excursion
2587     (let (config)
2588       (goto-char (point-min))
2589       (while (search-forward "\r" nil t)
2590         (push (1- (point)) config))
2591       config)))
2592
2593 (defun gnus-restore-hidden-threads-configuration (config)
2594   "Restore hidden threads configuration from CONFIG."
2595   (save-excursion
2596     (let (point buffer-read-only)
2597       (while (setq point (pop config))
2598         (when (and (< point (point-max))
2599                    (goto-char point)
2600                    (eq (char-after) ?\n))
2601           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2602
2603 ;; Various summary mode internalish functions.
2604
2605 (defun gnus-mouse-pick-article (e)
2606   (interactive "e")
2607   (mouse-set-point e)
2608   (gnus-summary-next-page nil t))
2609
2610 (defun gnus-summary-set-display-table ()
2611   "Change the display table.
2612 Odd characters have a tendency to mess
2613 up nicely formatted displays - we make all possible glyphs
2614 display only a single character."
2615
2616   ;; We start from the standard display table, if any.
2617   (let ((table (or (copy-sequence standard-display-table)
2618                    (make-display-table)))
2619         (i 32))
2620     ;; Nix out all the control chars...
2621     (while (>= (setq i (1- i)) 0)
2622       (aset table i [??]))
2623     ;; ... but not newline and cr, of course.  (cr is necessary for the
2624     ;; selective display).
2625     (aset table ?\n nil)
2626     (aset table ?\r nil)
2627     ;; We keep TAB as well.
2628     (aset table ?\t nil)
2629     ;; We nix out any glyphs over 126 that are not set already.
2630     (let ((i 256))
2631       (while (>= (setq i (1- i)) 127)
2632         ;; Only modify if the entry is nil.
2633         (unless (aref table i)
2634           (aset table i [??]))))
2635     (setq buffer-display-table table)))
2636
2637 (defun gnus-summary-buffer-name (group)
2638   "Return the summary buffer name of GROUP."
2639   (concat "*Summary " group "*"))
2640
2641 (defun gnus-summary-setup-buffer (group)
2642   "Initialize summary buffer."
2643   (let ((buffer (gnus-summary-buffer-name group)))
2644     (if (get-buffer buffer)
2645         (progn
2646           (set-buffer buffer)
2647           (setq gnus-summary-buffer (current-buffer))
2648           (not gnus-newsgroup-prepared))
2649       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2650       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2651       (gnus-summary-mode group)
2652       (when gnus-carpal
2653         (gnus-carpal-setup-buffer 'summary))
2654       (unless gnus-single-article-buffer
2655         (make-local-variable 'gnus-article-buffer)
2656         (make-local-variable 'gnus-article-current)
2657         (make-local-variable 'gnus-original-article-buffer))
2658       (setq gnus-newsgroup-name group)
2659       t)))
2660
2661 (defun gnus-set-global-variables ()
2662   "Set the global equivalents of the buffer-local variables.
2663 They are set to the latest values they had.  These reflect the summary
2664 buffer that was in action when the last article was fetched."
2665   (when (eq major-mode 'gnus-summary-mode)
2666     (setq gnus-summary-buffer (current-buffer))
2667     (let ((name gnus-newsgroup-name)
2668           (marked gnus-newsgroup-marked)
2669           (unread gnus-newsgroup-unreads)
2670           (headers gnus-current-headers)
2671           (data gnus-newsgroup-data)
2672           (summary gnus-summary-buffer)
2673           (article-buffer gnus-article-buffer)
2674           (original gnus-original-article-buffer)
2675           (gac gnus-article-current)
2676           (reffed gnus-reffed-article-number)
2677           (score-file gnus-current-score-file)
2678           (default-charset gnus-newsgroup-charset)
2679           vlist)
2680       (let ((locals gnus-newsgroup-variables))
2681         (while locals
2682           (if (consp (car locals))
2683               (push (eval (caar locals)) vlist)
2684             (push (eval (car locals)) vlist))
2685           (setq locals (cdr locals)))
2686         (setq vlist (nreverse vlist)))
2687       (save-excursion
2688         (set-buffer gnus-group-buffer)
2689         (setq gnus-newsgroup-name name
2690               gnus-newsgroup-marked marked
2691               gnus-newsgroup-unreads unread
2692               gnus-current-headers headers
2693               gnus-newsgroup-data data
2694               gnus-article-current gac
2695               gnus-summary-buffer summary
2696               gnus-article-buffer article-buffer
2697               gnus-original-article-buffer original
2698               gnus-reffed-article-number reffed
2699               gnus-current-score-file score-file
2700               gnus-newsgroup-charset default-charset)
2701         (let ((locals gnus-newsgroup-variables))
2702           (while locals
2703             (if (consp (car locals))
2704                 (set (caar locals) (pop vlist))
2705               (set (car locals) (pop vlist)))
2706             (setq locals (cdr locals))))
2707         ;; The article buffer also has local variables.
2708         (when (gnus-buffer-live-p gnus-article-buffer)
2709           (set-buffer gnus-article-buffer)
2710           (setq gnus-summary-buffer summary))))))
2711
2712 (defun gnus-summary-article-unread-p (article)
2713   "Say whether ARTICLE is unread or not."
2714   (memq article gnus-newsgroup-unreads))
2715
2716 (defun gnus-summary-first-article-p (&optional article)
2717   "Return whether ARTICLE is the first article in the buffer."
2718   (if (not (setq article (or article (gnus-summary-article-number))))
2719       nil
2720     (eq article (caar gnus-newsgroup-data))))
2721
2722 (defun gnus-summary-last-article-p (&optional article)
2723   "Return whether ARTICLE is the last article in the buffer."
2724   (if (not (setq article (or article (gnus-summary-article-number))))
2725       ;; All non-existent numbers are the last article.  :-)
2726       t
2727     (not (cdr (gnus-data-find-list article)))))
2728
2729 (defun gnus-make-thread-indent-array ()
2730   (let ((n 200))
2731     (unless (and gnus-thread-indent-array
2732                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2733       (setq gnus-thread-indent-array (make-vector 201 "")
2734             gnus-thread-indent-array-level gnus-thread-indent-level)
2735       (while (>= n 0)
2736         (aset gnus-thread-indent-array n
2737               (make-string (* n gnus-thread-indent-level) ? ))
2738         (setq n (1- n))))))
2739
2740 (defun gnus-update-summary-mark-positions ()
2741   "Compute where the summary marks are to go."
2742   (save-excursion
2743     (when (gnus-buffer-exists-p gnus-summary-buffer)
2744       (set-buffer gnus-summary-buffer))
2745     (let ((gnus-replied-mark 129)
2746           (gnus-score-below-mark 130)
2747           (gnus-score-over-mark 130)
2748           (gnus-download-mark 131)
2749           (spec gnus-summary-line-format-spec)
2750           gnus-visual pos)
2751       (save-excursion
2752         (gnus-set-work-buffer)
2753         (let ((gnus-summary-line-format-spec spec)
2754               (gnus-newsgroup-downloadable '((0 . t))))
2755           (gnus-summary-insert-line
2756            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2757            0 nil 128 t nil "" nil 1)
2758           (goto-char (point-min))
2759           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2760                                              (- (point) 2)))))
2761           (goto-char (point-min))
2762           (push (cons 'replied (and (search-forward "\201" nil t)
2763                                     (- (point) 2)))
2764                 pos)
2765           (goto-char (point-min))
2766           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2767                 pos)
2768           (goto-char (point-min))
2769           (push (cons 'download
2770                       (and (search-forward "\203" nil t) (- (point) 2)))
2771                 pos)))
2772       (setq gnus-summary-mark-positions pos))))
2773
2774 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2775   "Insert a dummy root in the summary buffer."
2776   (beginning-of-line)
2777   (gnus-add-text-properties
2778    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2779    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2780
2781 (defun gnus-summary-from-or-to-or-newsgroups (header)
2782   (let ((to (cdr (assq 'To (mail-header-extra header))))
2783         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2784         (default-mime-charset (with-current-buffer gnus-summary-buffer
2785                                 default-mime-charset)))
2786     (cond
2787      ((and to
2788            gnus-ignored-from-addresses
2789            (string-match gnus-ignored-from-addresses
2790                          (mail-header-from header)))
2791       (concat "-> "
2792               (or (car (funcall gnus-extract-address-components
2793                                 (funcall
2794                                  gnus-decode-encoded-word-function to)))
2795                   (funcall gnus-decode-encoded-word-function to))))
2796      ((and newsgroups
2797            gnus-ignored-from-addresses
2798            (string-match gnus-ignored-from-addresses
2799                          (mail-header-from header)))
2800       (concat "=> " newsgroups))
2801      (t
2802       (or (car (funcall gnus-extract-address-components
2803                         (mail-header-from header)))
2804           (mail-header-from header))))))
2805
2806 (defun gnus-summary-insert-line (gnus-tmp-header
2807                                  gnus-tmp-level gnus-tmp-current
2808                                  gnus-tmp-unread gnus-tmp-replied
2809                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2810                                  &optional gnus-tmp-dummy gnus-tmp-score
2811                                  gnus-tmp-process)
2812   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2813          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2814          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2815          (gnus-tmp-score-char
2816           (if (or (null gnus-summary-default-score)
2817                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2818                       gnus-summary-zcore-fuzz))
2819               ?  ;Whitespace
2820             (if (< gnus-tmp-score gnus-summary-default-score)
2821                 gnus-score-below-mark gnus-score-over-mark)))
2822          (gnus-tmp-replied
2823           (cond (gnus-tmp-process gnus-process-mark)
2824                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2825                  gnus-cached-mark)
2826                 (gnus-tmp-replied gnus-replied-mark)
2827                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2828                  gnus-saved-mark)
2829                 (t gnus-no-mark)))
2830          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2831          (gnus-tmp-name
2832           (cond
2833            ((string-match "<[^>]+> *$" gnus-tmp-from)
2834             (let ((beg (match-beginning 0)))
2835               (or (and (string-match "^\".+\"" gnus-tmp-from)
2836                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2837                   (substring gnus-tmp-from 0 beg))))
2838            ((string-match "(.+)" gnus-tmp-from)
2839             (substring gnus-tmp-from
2840                        (1+ (match-beginning 0)) (1- (match-end 0))))
2841            (t gnus-tmp-from)))
2842          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2843          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2844          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2845          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2846          (buffer-read-only nil))
2847     (when (string= gnus-tmp-name "")
2848       (setq gnus-tmp-name gnus-tmp-from))
2849     (unless (numberp gnus-tmp-lines)
2850       (setq gnus-tmp-lines 0))
2851     (gnus-put-text-property-excluding-characters-with-faces
2852      (point)
2853      (progn (eval gnus-summary-line-format-spec) (point))
2854      'gnus-number gnus-tmp-number)
2855     (when (gnus-visual-p 'summary-highlight 'highlight)
2856       (forward-line -1)
2857       (gnus-run-hooks 'gnus-summary-update-hook)
2858       (forward-line 1))))
2859
2860 (defun gnus-summary-update-line (&optional dont-update)
2861   "Update summary line after change."
2862   (when (and gnus-summary-default-score
2863              (not gnus-summary-inhibit-highlight))
2864     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2865            (article (gnus-summary-article-number))
2866            (score (gnus-summary-article-score article)))
2867       (unless dont-update
2868         (if (and gnus-summary-mark-below
2869                  (< (gnus-summary-article-score)
2870                     gnus-summary-mark-below))
2871             ;; This article has a low score, so we mark it as read.
2872             (when (memq article gnus-newsgroup-unreads)
2873               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2874           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2875             ;; This article was previously marked as read on account
2876             ;; of a low score, but now it has risen, so we mark it as
2877             ;; unread.
2878             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2879         (gnus-summary-update-mark
2880          (if (or (null gnus-summary-default-score)
2881                  (<= (abs (- score gnus-summary-default-score))
2882                      gnus-summary-zcore-fuzz))
2883              ?  ;Whitespace
2884            (if (< score gnus-summary-default-score)
2885                gnus-score-below-mark gnus-score-over-mark))
2886          'score))
2887       ;; Do visual highlighting.
2888       (when (gnus-visual-p 'summary-highlight 'highlight)
2889         (gnus-run-hooks 'gnus-summary-update-hook)))))
2890
2891 (defvar gnus-tmp-new-adopts nil)
2892
2893 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2894   "Return the number of articles in THREAD.
2895 This may be 0 in some cases -- if none of the articles in
2896 the thread are to be displayed."
2897   (let* ((number
2898           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2899           (cond
2900            ((not (listp thread))
2901             1)
2902            ((and (consp thread) (cdr thread))
2903             (apply
2904              '+ 1 (mapcar
2905                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2906            ((null thread)
2907             1)
2908            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2909             1)
2910            (t 0))))
2911     (when (and level (zerop level) gnus-tmp-new-adopts)
2912       (incf number
2913             (apply '+ (mapcar
2914                        'gnus-summary-number-of-articles-in-thread
2915                        gnus-tmp-new-adopts))))
2916     (if char
2917         (if (> number 1) gnus-not-empty-thread-mark
2918           gnus-empty-thread-mark)
2919       number)))
2920
2921 (defun gnus-summary-set-local-parameters (group)
2922   "Go through the local params of GROUP and set all variable specs in that list."
2923   (let ((params (gnus-group-find-parameter group))
2924         elem)
2925     (while params
2926       (setq elem (car params)
2927             params (cdr params))
2928       (and (consp elem)                 ; Has to be a cons.
2929            (consp (cdr elem))           ; The cdr has to be a list.
2930            (symbolp (car elem))         ; Has to be a symbol in there.
2931            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2932            (ignore-errors               ; So we set it.
2933              (make-local-variable (car elem))
2934              (set (car elem) (eval (nth 1 elem))))))))
2935
2936 (defun gnus-summary-read-group (group &optional show-all no-article
2937                                       kill-buffer no-display backward
2938                                       select-articles)
2939   "Start reading news in newsgroup GROUP.
2940 If SHOW-ALL is non-nil, already read articles are also listed.
2941 If NO-ARTICLE is non-nil, no article is selected initially.
2942 If NO-DISPLAY, don't generate a summary buffer."
2943   (let (result)
2944     (while (and group
2945                 (null (setq result
2946                             (let ((gnus-auto-select-next nil))
2947                               (or (gnus-summary-read-group-1
2948                                    group show-all no-article
2949                                    kill-buffer no-display
2950                                    select-articles)
2951                                   (setq show-all nil
2952                                         select-articles nil)))))
2953                 (eq gnus-auto-select-next 'quietly))
2954       (set-buffer gnus-group-buffer)
2955       ;; The entry function called above goes to the next
2956       ;; group automatically, so we go two groups back
2957       ;; if we are searching for the previous group.
2958       (when backward
2959         (gnus-group-prev-unread-group 2))
2960       (if (not (equal group (gnus-group-group-name)))
2961           (setq group (gnus-group-group-name))
2962         (setq group nil)))
2963     result))
2964
2965 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2966   "Directly jump to the other GROUP from summary buffer.
2967 If SHOW-ALL is non-nil, already read articles are also listed."
2968   (interactive
2969    (if (eq gnus-summary-buffer (current-buffer))
2970        (list (completing-read
2971               "Group: " gnus-active-hashtb nil t
2972               (when (and gnus-newsgroup-name
2973                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2974                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2975               'gnus-group-history)
2976              current-prefix-arg)
2977      (error "%s must be invoked from a gnus summary buffer." this-command)))
2978   (unless (or (zerop (length group))
2979               (and gnus-newsgroup-name
2980                    (string-equal gnus-newsgroup-name group)))
2981     (gnus-summary-exit)
2982     (gnus-summary-read-group group show-all
2983                              gnus-dont-select-after-jump-to-other-group)))
2984
2985 (defun gnus-summary-read-group-1 (group show-all no-article
2986                                         kill-buffer no-display
2987                                         &optional select-articles)
2988   ;; Killed foreign groups can't be entered.
2989   ;;  (when (and (not (gnus-group-native-p group))
2990   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
2991   ;;    (error "Dead non-native groups can't be entered"))
2992   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2993   (let* ((new-group (gnus-summary-setup-buffer group))
2994          (quit-config (gnus-group-quit-config group))
2995          (did-select (and new-group (gnus-select-newsgroup
2996                                      group show-all select-articles))))
2997     (cond
2998      ;; This summary buffer exists already, so we just select it.
2999      ((not new-group)
3000       (gnus-set-global-variables)
3001       (when kill-buffer
3002         (gnus-kill-or-deaden-summary kill-buffer))
3003       (gnus-configure-windows 'summary 'force)
3004       (gnus-set-mode-line 'summary)
3005       (gnus-summary-position-point)
3006       (message "")
3007       t)
3008      ;; We couldn't select this group.
3009      ((null did-select)
3010       (when (and (eq major-mode 'gnus-summary-mode)
3011                  (not (equal (current-buffer) kill-buffer)))
3012         (kill-buffer (current-buffer))
3013         (if (not quit-config)
3014             (progn
3015               ;; Update the info -- marks might need to be removed,
3016               ;; for instance.
3017               (gnus-summary-update-info)
3018               (set-buffer gnus-group-buffer)
3019               (gnus-group-jump-to-group group)
3020               (gnus-group-next-unread-group 1))
3021           (gnus-handle-ephemeral-exit quit-config)))
3022       (let ((grpinfo (gnus-get-info group)))
3023         (if (null (gnus-info-read grpinfo))
3024             (gnus-message 3 "Group %s contains no messages" group)
3025           (gnus-message 3 "Can't select group")))
3026       nil)
3027      ;; The user did a `C-g' while prompting for number of articles,
3028      ;; so we exit this group.
3029      ((eq did-select 'quit)
3030       (and (eq major-mode 'gnus-summary-mode)
3031            (not (equal (current-buffer) kill-buffer))
3032            (kill-buffer (current-buffer)))
3033       (when kill-buffer
3034         (gnus-kill-or-deaden-summary kill-buffer))
3035       (if (not quit-config)
3036           (progn
3037             (set-buffer gnus-group-buffer)
3038             (gnus-group-jump-to-group group)
3039             (gnus-group-next-unread-group 1)
3040             (gnus-configure-windows 'group 'force))
3041         (gnus-handle-ephemeral-exit quit-config))
3042       ;; Finally signal the quit.
3043       (signal 'quit nil))
3044      ;; The group was successfully selected.
3045      (t
3046       (gnus-set-global-variables)
3047       ;; Save the active value in effect when the group was entered.
3048       (setq gnus-newsgroup-active
3049             (gnus-copy-sequence
3050              (gnus-active gnus-newsgroup-name)))
3051       ;; You can change the summary buffer in some way with this hook.
3052       (gnus-run-hooks 'gnus-select-group-hook)
3053       ;; Set any local variables in the group parameters.
3054       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3055       (gnus-update-format-specifications
3056        nil 'summary 'summary-mode 'summary-dummy)
3057       (gnus-update-summary-mark-positions)
3058       ;; Do score processing.
3059       (when gnus-use-scoring
3060         (gnus-possibly-score-headers))
3061       ;; Check whether to fill in the gaps in the threads.
3062       (when gnus-build-sparse-threads
3063         (gnus-build-sparse-threads))
3064       ;; Find the initial limit.
3065       (if gnus-show-threads
3066           (if show-all
3067               (let ((gnus-newsgroup-dormant nil))
3068                 (gnus-summary-initial-limit show-all))
3069             (gnus-summary-initial-limit show-all))
3070         ;; When untreaded, all articles are always shown.
3071         (setq gnus-newsgroup-limit
3072               (mapcar
3073                (lambda (header) (mail-header-number header))
3074                gnus-newsgroup-headers)))
3075       ;; Generate the summary buffer.
3076       (unless no-display
3077         (gnus-summary-prepare))
3078       (when gnus-use-trees
3079         (gnus-tree-open group)
3080         (setq gnus-summary-highlight-line-function
3081               'gnus-tree-highlight-article))
3082       ;; If the summary buffer is empty, but there are some low-scored
3083       ;; articles or some excluded dormants, we include these in the
3084       ;; buffer.
3085       (when (and (zerop (buffer-size))
3086                  (not no-display))
3087         (cond (gnus-newsgroup-dormant
3088                (gnus-summary-limit-include-dormant))
3089               ((and gnus-newsgroup-scored show-all)
3090                (gnus-summary-limit-include-expunged t))))
3091       ;; Function `gnus-apply-kill-file' must be called in this hook.
3092       (gnus-run-hooks 'gnus-apply-kill-hook)
3093       (if (and (zerop (buffer-size))
3094                (not no-display))
3095           (progn
3096             ;; This newsgroup is empty.
3097             (gnus-summary-catchup-and-exit nil t)
3098             (gnus-message 6 "No unread news")
3099             (when kill-buffer
3100               (gnus-kill-or-deaden-summary kill-buffer))
3101             ;; Return nil from this function.
3102             nil)
3103         ;; Hide conversation thread subtrees.  We cannot do this in
3104         ;; gnus-summary-prepare-hook since kill processing may not
3105         ;; work with hidden articles.
3106         (and gnus-show-threads
3107              gnus-thread-hide-subtree
3108              (gnus-summary-hide-all-threads))
3109         (when kill-buffer
3110           (gnus-kill-or-deaden-summary kill-buffer))
3111         ;; Show first unread article if requested.
3112         (if (and (not no-article)
3113                  (not no-display)
3114                  gnus-newsgroup-unreads
3115                  gnus-auto-select-first)
3116             (progn
3117               (gnus-configure-windows 'summary)
3118               (cond
3119                ((eq gnus-auto-select-first 'best)
3120                 (gnus-summary-best-unread-article))
3121                ((eq gnus-auto-select-first t)
3122                 (gnus-summary-first-unread-article))
3123                ((gnus-functionp gnus-auto-select-first)
3124                 (funcall gnus-auto-select-first))))
3125           ;; Don't select any articles, just move point to the first
3126           ;; article in the group.
3127           (goto-char (point-min))
3128           (gnus-summary-position-point)
3129           (gnus-configure-windows 'summary 'force)
3130           (gnus-set-mode-line 'summary))
3131         (when (get-buffer-window gnus-group-buffer t)
3132           ;; Gotta use windows, because recenter does weird stuff if
3133           ;; the current buffer ain't the displayed window.
3134           (let ((owin (selected-window)))
3135             (select-window (get-buffer-window gnus-group-buffer t))
3136             (when (gnus-group-goto-group group)
3137               (recenter))
3138             (select-window owin)))
3139         ;; Mark this buffer as "prepared".
3140         (setq gnus-newsgroup-prepared t)
3141         (gnus-run-hooks 'gnus-summary-prepared-hook)
3142         t)))))
3143
3144 (defun gnus-summary-prepare ()
3145   "Generate the summary buffer."
3146   (interactive)
3147   (let ((buffer-read-only nil))
3148     (erase-buffer)
3149     (setq gnus-newsgroup-data nil
3150           gnus-newsgroup-data-reverse nil)
3151     (gnus-run-hooks 'gnus-summary-generate-hook)
3152     ;; Generate the buffer, either with threads or without.
3153     (when gnus-newsgroup-headers
3154       (gnus-summary-prepare-threads
3155        (if gnus-show-threads
3156            (gnus-sort-gathered-threads
3157             (funcall gnus-summary-thread-gathering-function
3158                      (gnus-sort-threads
3159                       (gnus-cut-threads (gnus-make-threads)))))
3160          ;; Unthreaded display.
3161          (gnus-sort-articles gnus-newsgroup-headers))))
3162     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3163     ;; Call hooks for modifying summary buffer.
3164     (goto-char (point-min))
3165     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3166
3167 (defsubst gnus-general-simplify-subject (subject)
3168   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3169   (setq subject
3170         (cond
3171          ;; Truncate the subject.
3172          (gnus-simplify-subject-functions
3173           (gnus-map-function gnus-simplify-subject-functions subject))
3174          ((numberp gnus-summary-gather-subject-limit)
3175           (setq subject (gnus-simplify-subject-re subject))
3176           (if (> (length subject) gnus-summary-gather-subject-limit)
3177               (substring subject 0 gnus-summary-gather-subject-limit)
3178             subject))
3179          ;; Fuzzily simplify it.
3180          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3181           (gnus-simplify-subject-fuzzy subject))
3182          ;; Just remove the leading "Re:".
3183          (t
3184           (gnus-simplify-subject-re subject))))
3185
3186   (if (and gnus-summary-gather-exclude-subject
3187            (string-match gnus-summary-gather-exclude-subject subject))
3188       nil                               ; This article shouldn't be gathered
3189     subject))
3190
3191 (defun gnus-summary-simplify-subject-query ()
3192   "Query where the respool algorithm would put this article."
3193   (interactive)
3194   (gnus-summary-select-article)
3195   (message "%s"
3196            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3197
3198 (defun gnus-gather-threads-by-subject (threads)
3199   "Gather threads by looking at Subject headers."
3200   (if (not gnus-summary-make-false-root)
3201       threads
3202     (let ((hashtb (gnus-make-hashtable 1024))
3203           (prev threads)
3204           (result threads)
3205           subject hthread whole-subject)
3206       (while threads
3207         (setq subject (gnus-general-simplify-subject
3208                        (setq whole-subject (mail-header-subject
3209                                             (caar threads)))))
3210         (when subject
3211           (if (setq hthread (gnus-gethash subject hashtb))
3212               (progn
3213                 ;; We enter a dummy root into the thread, if we
3214                 ;; haven't done that already.
3215                 (unless (stringp (caar hthread))
3216                   (setcar hthread (list whole-subject (car hthread))))
3217                 ;; We add this new gathered thread to this gathered
3218                 ;; thread.
3219                 (setcdr (car hthread)
3220                         (nconc (cdar hthread) (list (car threads))))
3221                 ;; Remove it from the list of threads.
3222                 (setcdr prev (cdr threads))
3223                 (setq threads prev))
3224             ;; Enter this thread into the hash table.
3225             (gnus-sethash subject threads hashtb)))
3226         (setq prev threads)
3227         (setq threads (cdr threads)))
3228       result)))
3229
3230 (defun gnus-gather-threads-by-references (threads)
3231   "Gather threads by looking at References headers."
3232   (let ((idhashtb (gnus-make-hashtable 1024))
3233         (thhashtb (gnus-make-hashtable 1024))
3234         (prev threads)
3235         (result threads)
3236         ids references id gthread gid entered ref)
3237     (while threads
3238       (when (setq references (mail-header-references (caar threads)))
3239         (setq id (mail-header-id (caar threads))
3240               ids (gnus-split-references references)
3241               entered nil)
3242         (while (setq ref (pop ids))
3243           (setq ids (delete ref ids))
3244           (if (not (setq gid (gnus-gethash ref idhashtb)))
3245               (progn
3246                 (gnus-sethash ref id idhashtb)
3247                 (gnus-sethash id threads thhashtb))
3248             (setq gthread (gnus-gethash gid thhashtb))
3249             (unless entered
3250               ;; We enter a dummy root into the thread, if we
3251               ;; haven't done that already.
3252               (unless (stringp (caar gthread))
3253                 (setcar gthread (list (mail-header-subject (caar gthread))
3254                                       (car gthread))))
3255               ;; We add this new gathered thread to this gathered
3256               ;; thread.
3257               (setcdr (car gthread)
3258                       (nconc (cdar gthread) (list (car threads)))))
3259             ;; Add it into the thread hash table.
3260             (gnus-sethash id gthread thhashtb)
3261             (setq entered t)
3262             ;; Remove it from the list of threads.
3263             (setcdr prev (cdr threads))
3264             (setq threads prev))))
3265       (setq prev threads)
3266       (setq threads (cdr threads)))
3267     result))
3268
3269 (defun gnus-sort-gathered-threads (threads)
3270   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3271   (let ((result threads))
3272     (while threads
3273       (when (stringp (caar threads))
3274         (setcdr (car threads)
3275                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3276       (setq threads (cdr threads)))
3277     result))
3278
3279 (defun gnus-thread-loop-p (root thread)
3280   "Say whether ROOT is in THREAD."
3281   (let ((stack (list thread))
3282         (infloop 0)
3283         th)
3284     (while (setq thread (pop stack))
3285       (setq th (cdr thread))
3286       (while (and th
3287                   (not (eq (caar th) root)))
3288         (pop th))
3289       (if th
3290           ;; We have found a loop.
3291           (let (ref-dep)
3292             (setcdr thread (delq (car th) (cdr thread)))
3293             (if (boundp (setq ref-dep (intern "none"
3294                                               gnus-newsgroup-dependencies)))
3295                 (setcdr (symbol-value ref-dep)
3296                         (nconc (cdr (symbol-value ref-dep))
3297                                (list (car th))))
3298               (set ref-dep (list nil (car th))))
3299             (setq infloop 1
3300                   stack nil))
3301         ;; Push all the subthreads onto the stack.
3302         (push (cdr thread) stack)))
3303     infloop))
3304
3305 (defun gnus-make-threads ()
3306   "Go through the dependency hashtb and find the roots.  Return all threads."
3307   (let (threads)
3308     (while (catch 'infloop
3309              (mapatoms
3310               (lambda (refs)
3311                 ;; Deal with self-referencing References loops.
3312                 (when (and (car (symbol-value refs))
3313                            (not (zerop
3314                                  (apply
3315                                   '+
3316                                   (mapcar
3317                                    (lambda (thread)
3318                                      (gnus-thread-loop-p
3319                                       (car (symbol-value refs)) thread))
3320                                    (cdr (symbol-value refs)))))))
3321                   (setq threads nil)
3322                   (throw 'infloop t))
3323                 (unless (car (symbol-value refs))
3324                   ;; These threads do not refer back to any other articles,
3325                   ;; so they're roots.
3326                   (setq threads (append (cdr (symbol-value refs)) threads))))
3327               gnus-newsgroup-dependencies)))
3328     threads))
3329
3330 ;; Build the thread tree.
3331 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3332   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3333
3334 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3335 if it was already present.
3336
3337 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3338 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3339 Message-IDs will be renamed be renamed to a unique Message-ID before
3340 being entered.
3341
3342 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3343   (let* ((id (mail-header-id header))
3344          (id-dep (and id (intern id dependencies)))
3345          ref ref-dep ref-header)
3346     ;; Enter this `header' in the `dependencies' table.
3347     (cond
3348      ((not id-dep)
3349       (setq header nil))
3350      ;; The first two cases do the normal part: enter a new `header'
3351      ;; in the `dependencies' table.
3352      ((not (boundp id-dep))
3353       (set id-dep (list header)))
3354      ((null (car (symbol-value id-dep)))
3355       (setcar (symbol-value id-dep) header))
3356
3357      ;; From here the `header' was already present in the
3358      ;; `dependencies' table.
3359      (force-new
3360       ;; Overrides an existing entry;
3361       ;; just set the header part of the entry.
3362       (setcar (symbol-value id-dep) header))
3363
3364      ;; Renames the existing `header' to a unique Message-ID.
3365      ((not gnus-summary-ignore-duplicates)
3366       ;; An article with this Message-ID has already been seen.
3367       ;; We rename the Message-ID.
3368       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3369            (list header))
3370       (mail-header-set-id header id))
3371
3372      ;; The last case ignores an existing entry, except it adds any
3373      ;; additional Xrefs (in case the two articles came from different
3374      ;; servers.
3375      ;; Also sets `header' to `nil' meaning that the `dependencies'
3376      ;; table was *not* modified.
3377      (t
3378       (mail-header-set-xref
3379        (car (symbol-value id-dep))
3380        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3381                    "")
3382                (or (mail-header-xref header) "")))
3383       (setq header nil)))
3384
3385     (when header
3386       ;; First check if that we are not creating a References loop.
3387       (setq ref (gnus-parent-id (mail-header-references header)))
3388       (while (and ref
3389                   (setq ref-dep (intern-soft ref dependencies))
3390                   (boundp ref-dep)
3391                   (setq ref-header (car (symbol-value ref-dep))))
3392         (if (string= id ref)
3393             ;; Yuk!  This is a reference loop.  Make the article be a
3394             ;; root article.
3395             (progn
3396               (mail-header-set-references (car (symbol-value id-dep)) "none")
3397               (setq ref nil))
3398           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3399       (setq ref (gnus-parent-id (mail-header-references header)))
3400       (setq ref-dep (intern (or ref "none") dependencies))
3401       (if (boundp ref-dep)
3402           (setcdr (symbol-value ref-dep)
3403                   (nconc (cdr (symbol-value ref-dep))
3404                          (list (symbol-value id-dep))))
3405         (set ref-dep (list nil (symbol-value id-dep)))))
3406     header))
3407
3408 (defun gnus-build-sparse-threads ()
3409   (let ((headers gnus-newsgroup-headers)
3410         (mail-parse-charset gnus-newsgroup-charset)
3411         (gnus-summary-ignore-duplicates t)
3412         header references generation relations
3413         subject child end new-child date)
3414     ;; First we create an alist of generations/relations, where
3415     ;; generations is how much we trust the relation, and the relation
3416     ;; is parent/child.
3417     (gnus-message 7 "Making sparse threads...")
3418     (save-excursion
3419       (nnheader-set-temp-buffer " *gnus sparse threads*")
3420       (while (setq header (pop headers))
3421         (when (and (setq references (mail-header-references header))
3422                    (not (string= references "")))
3423           (insert references)
3424           (setq child (mail-header-id header)
3425                 subject (mail-header-subject header)
3426                 date (mail-header-date header)
3427                 generation 0)
3428           (while (search-backward ">" nil t)
3429             (setq end (1+ (point)))
3430             (when (search-backward "<" nil t)
3431               (setq new-child (buffer-substring (point) end))
3432               (push (list (incf generation)
3433                           child (setq child new-child)
3434                           subject date)
3435                     relations)))
3436           (when child
3437             (push (list (1+ generation) child nil subject) relations))
3438           (erase-buffer)))
3439       (kill-buffer (current-buffer)))
3440     ;; Sort over trustworthiness.
3441     (mapcar
3442      (lambda (relation)
3443        (when (gnus-dependencies-add-header
3444               (make-full-mail-header-from-decoded-header
3445                gnus-reffed-article-number
3446                (nth 3 relation) "" (or (nth 4 relation) "")
3447                (nth 1 relation)
3448                (or (nth 2 relation) "") 0 0 "")
3449               gnus-newsgroup-dependencies nil)
3450          (push gnus-reffed-article-number gnus-newsgroup-limit)
3451          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3452          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3453                gnus-newsgroup-reads)
3454          (decf gnus-reffed-article-number)))
3455      (sort relations 'car-less-than-car))
3456     (gnus-message 7 "Making sparse threads...done")))
3457
3458 (defun gnus-build-old-threads ()
3459   ;; Look at all the articles that refer back to old articles, and
3460   ;; fetch the headers for the articles that aren't there.  This will
3461   ;; build complete threads - if the roots haven't been expired by the
3462   ;; server, that is.
3463   (let ((mail-parse-charset gnus-newsgroup-charset)
3464         id heads)
3465     (mapatoms
3466      (lambda (refs)
3467        (when (not (car (symbol-value refs)))
3468          (setq heads (cdr (symbol-value refs)))
3469          (while heads
3470            (if (memq (mail-header-number (caar heads))
3471                      gnus-newsgroup-dormant)
3472                (setq heads (cdr heads))
3473              (setq id (symbol-name refs))
3474              (while (and (setq id (gnus-build-get-header id))
3475                          (not (car (gnus-id-to-thread id)))))
3476              (setq heads nil)))))
3477      gnus-newsgroup-dependencies)))
3478
3479 ;; This function has to be called with point after the article number
3480 ;; on the beginning of the line.
3481 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3482   (let ((eol (gnus-point-at-eol))
3483         (buffer (current-buffer))
3484         header)
3485
3486     ;; overview: [num subject from date id refs chars lines misc]
3487     (unwind-protect
3488         (progn
3489           (narrow-to-region (point) eol)
3490           (unless (eobp)
3491             (forward-char))
3492
3493           (setq header
3494                 (make-full-mail-header
3495                  number                         ; number
3496                  (nnheader-nov-field)           ; subject
3497                  (nnheader-nov-field)           ; from
3498                  (nnheader-nov-field)           ; date
3499                  (nnheader-nov-read-message-id) ; id
3500                  (nnheader-nov-field)           ; refs
3501                  (nnheader-nov-read-integer)    ; chars
3502                  (nnheader-nov-read-integer)    ; lines
3503                  (unless (eobp)
3504                    (if (looking-at "Xref: ")
3505                        (goto-char (match-end 0)))
3506                    (nnheader-nov-field))        ; Xref
3507                  (nnheader-nov-parse-extra))))  ; extra
3508
3509       (widen))
3510
3511     (when gnus-alter-header-function
3512       (funcall gnus-alter-header-function header))
3513     (gnus-dependencies-add-header header dependencies force-new)))
3514
3515 (defun gnus-build-get-header (id)
3516   "Look through the buffer of NOV lines and find the header to ID.
3517 Enter this line into the dependencies hash table, and return
3518 the id of the parent article (if any)."
3519   (let ((deps gnus-newsgroup-dependencies)
3520         found header)
3521     (prog1
3522         (save-excursion
3523           (set-buffer nntp-server-buffer)
3524           (let ((case-fold-search nil))
3525             (goto-char (point-min))
3526             (while (and (not found)
3527                         (search-forward id nil t))
3528               (beginning-of-line)
3529               (setq found (looking-at
3530                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3531                                    (regexp-quote id))))
3532               (or found (beginning-of-line 2)))
3533             (when found
3534               (beginning-of-line)
3535               (and
3536                (setq header (gnus-nov-parse-line
3537                              (read (current-buffer)) deps))
3538                (gnus-parent-id (mail-header-references header))))))
3539       (when header
3540         (let ((number (mail-header-number header)))
3541           (push number gnus-newsgroup-limit)
3542           (push header gnus-newsgroup-headers)
3543           (if (memq number gnus-newsgroup-unselected)
3544               (progn
3545                 (push number gnus-newsgroup-unreads)
3546                 (setq gnus-newsgroup-unselected
3547                       (delq number gnus-newsgroup-unselected)))
3548             (push number gnus-newsgroup-ancient)))))))
3549
3550 (defun gnus-build-all-threads ()
3551   "Read all the headers."
3552   (let ((gnus-summary-ignore-duplicates t)
3553         (mail-parse-charset gnus-newsgroup-charset)
3554         (dependencies gnus-newsgroup-dependencies)
3555         header article)
3556     (save-excursion
3557       (set-buffer nntp-server-buffer)
3558       (let ((case-fold-search nil))
3559         (goto-char (point-min))
3560         (while (not (eobp))
3561           (ignore-errors
3562             (setq article (read (current-buffer))
3563                   header (gnus-nov-parse-line article dependencies)))
3564           (when header
3565             (save-excursion
3566               (set-buffer gnus-summary-buffer)
3567               (push header gnus-newsgroup-headers)
3568               (if (memq (setq article (mail-header-number header))
3569                         gnus-newsgroup-unselected)
3570                   (progn
3571                     (push article gnus-newsgroup-unreads)
3572                     (setq gnus-newsgroup-unselected
3573                           (delq article gnus-newsgroup-unselected)))
3574                 (push article gnus-newsgroup-ancient)))
3575             (forward-line 1)))))))
3576
3577 (defun gnus-summary-update-article-line (article header)
3578   "Update the line for ARTICLE using HEADERS."
3579   (let* ((id (mail-header-id header))
3580          (thread (gnus-id-to-thread id)))
3581     (unless thread
3582       (error "Article in no thread"))
3583     ;; Update the thread.
3584     (setcar thread header)
3585     (gnus-summary-goto-subject article)
3586     (let* ((datal (gnus-data-find-list article))
3587            (data (car datal))
3588            (length (when (cdr datal)
3589                      (- (gnus-data-pos data)
3590                         (gnus-data-pos (cadr datal)))))
3591            (buffer-read-only nil)
3592            (level (gnus-summary-thread-level)))
3593       (gnus-delete-line)
3594       (gnus-summary-insert-line
3595        header level nil (gnus-article-mark article)
3596        (memq article gnus-newsgroup-replied)
3597        (memq article gnus-newsgroup-expirable)
3598        ;; Only insert the Subject string when it's different
3599        ;; from the previous Subject string.
3600        (if (and
3601             gnus-show-threads
3602             (gnus-subject-equal
3603              (condition-case ()
3604                  (mail-header-subject
3605                   (gnus-data-header
3606                    (cadr
3607                     (gnus-data-find-list
3608                      article
3609                      (gnus-data-list t)))))
3610                ;; Error on the side of excessive subjects.
3611                (error ""))
3612              (mail-header-subject header)))
3613            ""
3614          (mail-header-subject header))
3615        nil (cdr (assq article gnus-newsgroup-scored))
3616        (memq article gnus-newsgroup-processable))
3617       (when length
3618         (gnus-data-update-list
3619          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3620
3621 (defun gnus-summary-update-article (article &optional iheader)
3622   "Update ARTICLE in the summary buffer."
3623   (set-buffer gnus-summary-buffer)
3624   (let* ((header (gnus-summary-article-header article))
3625          (id (mail-header-id header))
3626          (data (gnus-data-find article))
3627          (thread (gnus-id-to-thread id))
3628          (references (mail-header-references header))
3629          (parent
3630           (gnus-id-to-thread
3631            (or (gnus-parent-id
3632                 (when (and references
3633                            (not (equal "" references)))
3634                   references))
3635                "none")))
3636          (buffer-read-only nil)
3637          (old (car thread)))
3638     (when thread
3639       (unless iheader
3640         (setcar thread nil)
3641         (when parent
3642           (delq thread parent)))
3643       (if (gnus-summary-insert-subject id header)
3644           ;; Set the (possibly) new article number in the data structure.
3645           (gnus-data-set-number data (gnus-id-to-article id))
3646         (setcar thread old)
3647         nil))))
3648
3649 (defun gnus-rebuild-thread (id &optional line)
3650   "Rebuild the thread containing ID.
3651 If LINE, insert the rebuilt thread starting on line LINE."
3652   (let ((buffer-read-only nil)
3653         old-pos current thread data)
3654     (if (not gnus-show-threads)
3655         (setq thread (list (car (gnus-id-to-thread id))))
3656       ;; Get the thread this article is part of.
3657       (setq thread (gnus-remove-thread id)))
3658     (setq old-pos (gnus-point-at-bol))
3659     (setq current (save-excursion
3660                     (and (zerop (forward-line -1))
3661                          (gnus-summary-article-number))))
3662     ;; If this is a gathered thread, we have to go some re-gathering.
3663     (when (stringp (car thread))
3664       (let ((subject (car thread))
3665             roots thr)
3666         (setq thread (cdr thread))
3667         (while thread
3668           (unless (memq (setq thr (gnus-id-to-thread
3669                                    (gnus-root-id
3670                                     (mail-header-id (caar thread)))))
3671                         roots)
3672             (push thr roots))
3673           (setq thread (cdr thread)))
3674         ;; We now have all (unique) roots.
3675         (if (= (length roots) 1)
3676             ;; All the loose roots are now one solid root.
3677             (setq thread (car roots))
3678           (setq thread (cons subject (gnus-sort-threads roots))))))
3679     (let (threads)
3680       ;; We then insert this thread into the summary buffer.
3681       (when line
3682         (goto-char (point-min))
3683         (forward-line (1- line)))
3684       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3685         (if gnus-show-threads
3686             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3687           (gnus-summary-prepare-unthreaded thread))
3688         (setq data (nreverse gnus-newsgroup-data))
3689         (setq threads gnus-newsgroup-threads))
3690       ;; We splice the new data into the data structure.
3691       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3692       ;;!!! then we want to insert at the beginning of the buffer.
3693       ;;!!! That happens to be true with Gnus now, but that may
3694       ;;!!! change in the future.  Perhaps.
3695       (gnus-data-enter-list
3696        (if line nil current) data (- (point) old-pos))
3697       (setq gnus-newsgroup-threads
3698             (nconc threads gnus-newsgroup-threads))
3699       (gnus-data-compute-positions))))
3700
3701 (defun gnus-number-to-header (number)
3702   "Return the header for article NUMBER."
3703   (let ((headers gnus-newsgroup-headers))
3704     (while (and headers
3705                 (not (= number (mail-header-number (car headers)))))
3706       (pop headers))
3707     (when headers
3708       (car headers))))
3709
3710 (defun gnus-parent-headers (in-headers &optional generation)
3711   "Return the headers of the GENERATIONeth parent of HEADERS."
3712   (unless generation
3713     (setq generation 1))
3714   (let ((parent t)
3715         (headers in-headers)
3716         references)
3717     (while (and parent
3718                 (not (zerop generation))
3719                 (setq references (mail-header-references headers)))
3720       (setq headers (if (and references
3721                              (setq parent (gnus-parent-id references)))
3722                         (car (gnus-id-to-thread parent))
3723                       nil))
3724       (decf generation))
3725     (and (not (eq headers in-headers))
3726          headers)))
3727
3728 (defun gnus-id-to-thread (id)
3729   "Return the (sub-)thread where ID appears."
3730   (gnus-gethash id gnus-newsgroup-dependencies))
3731
3732 (defun gnus-id-to-article (id)
3733   "Return the article number of ID."
3734   (let ((thread (gnus-id-to-thread id)))
3735     (when (and thread
3736                (car thread))
3737       (mail-header-number (car thread)))))
3738
3739 (defun gnus-id-to-header (id)
3740   "Return the article headers of ID."
3741   (car (gnus-id-to-thread id)))
3742
3743 (defun gnus-article-displayed-root-p (article)
3744   "Say whether ARTICLE is a root(ish) article."
3745   (let ((level (gnus-summary-thread-level article))
3746         (refs (mail-header-references  (gnus-summary-article-header article)))
3747         particle)
3748     (cond
3749      ((null level) nil)
3750      ((zerop level) t)
3751      ((null refs) t)
3752      ((null (gnus-parent-id refs)) t)
3753      ((and (= 1 level)
3754            (null (setq particle (gnus-id-to-article
3755                                  (gnus-parent-id refs))))
3756            (null (gnus-summary-thread-level particle)))))))
3757
3758 (defun gnus-root-id (id)
3759   "Return the id of the root of the thread where ID appears."
3760   (let (last-id prev)
3761     (while (and id (setq prev (car (gnus-id-to-thread id))))
3762       (setq last-id id
3763             id (gnus-parent-id (mail-header-references prev))))
3764     last-id))
3765
3766 (defun gnus-articles-in-thread (thread)
3767   "Return the list of articles in THREAD."
3768   (cons (mail-header-number (car thread))
3769         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3770
3771 (defun gnus-remove-thread (id &optional dont-remove)
3772   "Remove the thread that has ID in it."
3773   (let (headers thread last-id)
3774     ;; First go up in this thread until we find the root.
3775     (setq last-id (gnus-root-id id)
3776           headers (message-flatten-list (gnus-id-to-thread last-id)))
3777     ;; We have now found the real root of this thread.  It might have
3778     ;; been gathered into some loose thread, so we have to search
3779     ;; through the threads to find the thread we wanted.
3780     (let ((threads gnus-newsgroup-threads)
3781           sub)
3782       (while threads
3783         (setq sub (car threads))
3784         (if (stringp (car sub))
3785             ;; This is a gathered thread, so we look at the roots
3786             ;; below it to find whether this article is in this
3787             ;; gathered root.
3788             (progn
3789               (setq sub (cdr sub))
3790               (while sub
3791                 (when (member (caar sub) headers)
3792                   (setq thread (car threads)
3793                         threads nil
3794                         sub nil))
3795                 (setq sub (cdr sub))))
3796           ;; It's an ordinary thread, so we check it.
3797           (when (eq (car sub) (car headers))
3798             (setq thread sub
3799                   threads nil)))
3800         (setq threads (cdr threads)))
3801       ;; If this article is in no thread, then it's a root.
3802       (if thread
3803           (unless dont-remove
3804             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3805         (setq thread (gnus-id-to-thread last-id)))
3806       (when thread
3807         (prog1
3808             thread                      ; We return this thread.
3809           (unless dont-remove
3810             (if (stringp (car thread))
3811                 (progn
3812                   ;; If we use dummy roots, then we have to remove the
3813                   ;; dummy root as well.
3814                   (when (eq gnus-summary-make-false-root 'dummy)
3815                     ;; We go to the dummy root by going to
3816                     ;; the first sub-"thread", and then one line up.
3817                     (gnus-summary-goto-article
3818                      (mail-header-number (caadr thread)))
3819                     (forward-line -1)
3820                     (gnus-delete-line)
3821                     (gnus-data-compute-positions))
3822                   (setq thread (cdr thread))
3823                   (while thread
3824                     (gnus-remove-thread-1 (car thread))
3825                     (setq thread (cdr thread))))
3826               (gnus-remove-thread-1 thread))))))))
3827
3828 (defun gnus-remove-thread-1 (thread)
3829   "Remove the thread THREAD recursively."
3830   (let ((number (mail-header-number (pop thread)))
3831         d)
3832     (setq thread (reverse thread))
3833     (while thread
3834       (gnus-remove-thread-1 (pop thread)))
3835     (when (setq d (gnus-data-find number))
3836       (goto-char (gnus-data-pos d))
3837       (gnus-summary-show-thread)
3838       (gnus-data-remove
3839        number
3840        (- (gnus-point-at-bol)
3841           (prog1
3842               (1+ (gnus-point-at-eol))
3843             (gnus-delete-line)))))))
3844
3845 (defun gnus-sort-threads-1 (threads func)
3846   (sort (mapcar (lambda (thread)
3847                   (cons (car thread)
3848                         (and (cdr thread)
3849                              (gnus-sort-threads-1 (cdr thread) func))))
3850                 threads) func))
3851
3852 (defun gnus-sort-threads (threads)
3853   "Sort THREADS."
3854   (if (not gnus-thread-sort-functions)
3855       threads
3856     (gnus-message 8 "Sorting threads...")
3857     (prog1
3858         (gnus-sort-threads-1
3859          threads
3860          (gnus-make-sort-function gnus-thread-sort-functions))
3861       (gnus-message 8 "Sorting threads...done"))))
3862
3863 (defun gnus-sort-articles (articles)
3864   "Sort ARTICLES."
3865   (when gnus-article-sort-functions
3866     (gnus-message 7 "Sorting articles...")
3867     (prog1
3868         (setq gnus-newsgroup-headers
3869               (sort articles (gnus-make-sort-function
3870                               gnus-article-sort-functions)))
3871       (gnus-message 7 "Sorting articles...done"))))
3872
3873 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3874 (defmacro gnus-thread-header (thread)
3875   "Return header of first article in THREAD.
3876 Note that THREAD must never, ever be anything else than a variable -
3877 using some other form will lead to serious barfage."
3878   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3879   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3880   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3881         (vector thread) 2))
3882
3883 (defsubst gnus-article-sort-by-number (h1 h2)
3884   "Sort articles by article number."
3885   (< (mail-header-number h1)
3886      (mail-header-number h2)))
3887
3888 (defun gnus-thread-sort-by-number (h1 h2)
3889   "Sort threads by root article number."
3890   (gnus-article-sort-by-number
3891    (gnus-thread-header h1) (gnus-thread-header h2)))
3892
3893 (defsubst gnus-article-sort-by-lines (h1 h2)
3894   "Sort articles by article Lines header."
3895   (< (mail-header-lines h1)
3896      (mail-header-lines h2)))
3897
3898 (defun gnus-thread-sort-by-lines (h1 h2)
3899   "Sort threads by root article Lines header."
3900   (gnus-article-sort-by-lines
3901    (gnus-thread-header h1) (gnus-thread-header h2)))
3902
3903 (defsubst gnus-article-sort-by-chars (h1 h2)
3904   "Sort articles by octet length."
3905   (< (mail-header-chars h1)
3906      (mail-header-chars h2)))
3907
3908 (defun gnus-thread-sort-by-chars (h1 h2)
3909   "Sort threads by root article octet length."
3910   (gnus-article-sort-by-chars
3911    (gnus-thread-header h1) (gnus-thread-header h2)))
3912
3913 (defsubst gnus-article-sort-by-author (h1 h2)
3914   "Sort articles by root author."
3915   (string-lessp
3916    (let ((addr (car (mime-entity-read-field h1 'From))))
3917      (or (std11-full-name-string addr)
3918          (std11-address-string addr)
3919          ""))
3920    (let ((addr (car (mime-entity-read-field h2 'From))))
3921      (or (std11-full-name-string addr)
3922          (std11-address-string addr)
3923          ""))
3924    ))
3925
3926 (defun gnus-thread-sort-by-author (h1 h2)
3927   "Sort threads by root author."
3928   (gnus-article-sort-by-author
3929    (gnus-thread-header h1)  (gnus-thread-header h2)))
3930
3931 (defsubst gnus-article-sort-by-subject (h1 h2)
3932   "Sort articles by root subject."
3933   (string-lessp
3934    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3935    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3936
3937 (defun gnus-thread-sort-by-subject (h1 h2)
3938   "Sort threads by root subject."
3939   (gnus-article-sort-by-subject
3940    (gnus-thread-header h1) (gnus-thread-header h2)))
3941
3942 (defsubst gnus-article-sort-by-date (h1 h2)
3943   "Sort articles by root article date."
3944   (time-less-p
3945    (gnus-date-get-time (mail-header-date h1))
3946    (gnus-date-get-time (mail-header-date h2))))
3947
3948 (defun gnus-thread-sort-by-date (h1 h2)
3949   "Sort threads by root article date."
3950   (gnus-article-sort-by-date
3951    (gnus-thread-header h1) (gnus-thread-header h2)))
3952
3953 (defsubst gnus-article-sort-by-score (h1 h2)
3954   "Sort articles by root article score.
3955 Unscored articles will be counted as having a score of zero."
3956   (> (or (cdr (assq (mail-header-number h1)
3957                     gnus-newsgroup-scored))
3958          gnus-summary-default-score 0)
3959      (or (cdr (assq (mail-header-number h2)
3960                     gnus-newsgroup-scored))
3961          gnus-summary-default-score 0)))
3962
3963 (defun gnus-thread-sort-by-score (h1 h2)
3964   "Sort threads by root article score."
3965   (gnus-article-sort-by-score
3966    (gnus-thread-header h1) (gnus-thread-header h2)))
3967
3968 (defun gnus-thread-sort-by-total-score (h1 h2)
3969   "Sort threads by the sum of all scores in the thread.
3970 Unscored articles will be counted as having a score of zero."
3971   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3972
3973 (defun gnus-thread-total-score (thread)
3974   ;; This function find the total score of THREAD.
3975   (cond ((null thread)
3976          0)
3977         ((consp thread)
3978          (if (stringp (car thread))
3979              (apply gnus-thread-score-function 0
3980                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3981            (gnus-thread-total-score-1 thread)))
3982         (t
3983          (gnus-thread-total-score-1 (list thread)))))
3984
3985 (defun gnus-thread-total-score-1 (root)
3986   ;; This function find the total score of the thread below ROOT.
3987   (setq root (car root))
3988   (apply gnus-thread-score-function
3989          (or (append
3990               (mapcar 'gnus-thread-total-score
3991                       (cdr (gnus-id-to-thread (mail-header-id root))))
3992               (when (> (mail-header-number root) 0)
3993                 (list (or (cdr (assq (mail-header-number root)
3994                                      gnus-newsgroup-scored))
3995                           gnus-summary-default-score 0))))
3996              (list gnus-summary-default-score)
3997              '(0))))
3998
3999 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4000 (defvar gnus-tmp-prev-subject nil)
4001 (defvar gnus-tmp-false-parent nil)
4002 (defvar gnus-tmp-root-expunged nil)
4003 (defvar gnus-tmp-dummy-line nil)
4004
4005 (eval-when-compile (defvar gnus-tmp-header))
4006 (defun gnus-extra-header (type &optional header)
4007   "Return the extra header of TYPE."
4008   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4009       ""))
4010
4011 (defun gnus-summary-prepare-threads (threads)
4012   "Prepare summary buffer from THREADS and indentation LEVEL.
4013 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4014 or a straight list of headers."
4015   (gnus-message 7 "Generating summary...")
4016
4017   (setq gnus-newsgroup-threads threads)
4018   (beginning-of-line)
4019
4020   (let ((gnus-tmp-level 0)
4021         (default-score (or gnus-summary-default-score 0))
4022         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4023         thread number subject stack state gnus-tmp-gathered beg-match
4024         new-roots gnus-tmp-new-adopts thread-end
4025         gnus-tmp-header gnus-tmp-unread
4026         gnus-tmp-replied gnus-tmp-subject-or-nil
4027         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4028         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4029         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4030
4031     (setq gnus-tmp-prev-subject nil)
4032
4033     (if (vectorp (car threads))
4034         ;; If this is a straight (sic) list of headers, then a
4035         ;; threaded summary display isn't required, so we just create
4036         ;; an unthreaded one.
4037         (gnus-summary-prepare-unthreaded threads)
4038
4039       ;; Do the threaded display.
4040
4041       (while (or threads stack gnus-tmp-new-adopts new-roots)
4042
4043         (if (and (= gnus-tmp-level 0)
4044                  (or (not stack)
4045                      (= (caar stack) 0))
4046                  (not gnus-tmp-false-parent)
4047                  (or gnus-tmp-new-adopts new-roots))
4048             (if gnus-tmp-new-adopts
4049                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4050                       thread (list (car gnus-tmp-new-adopts))
4051                       gnus-tmp-header (caar thread)
4052                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4053               (when new-roots
4054                 (setq thread (list (car new-roots))
4055                       gnus-tmp-header (caar thread)
4056                       new-roots (cdr new-roots))))
4057
4058           (if threads
4059               ;; If there are some threads, we do them before the
4060               ;; threads on the stack.
4061               (setq thread threads
4062                     gnus-tmp-header (caar thread))
4063             ;; There were no current threads, so we pop something off
4064             ;; the stack.
4065             (setq state (car stack)
4066                   gnus-tmp-level (car state)
4067                   thread (cdr state)
4068                   stack (cdr stack)
4069                   gnus-tmp-header (caar thread))))
4070
4071         (setq gnus-tmp-false-parent nil)
4072         (setq gnus-tmp-root-expunged nil)
4073         (setq thread-end nil)
4074
4075         (if (stringp gnus-tmp-header)
4076             ;; The header is a dummy root.
4077             (cond
4078              ((eq gnus-summary-make-false-root 'adopt)
4079               ;; We let the first article adopt the rest.
4080               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4081                                                (cddar thread)))
4082               (setq gnus-tmp-gathered
4083                     (nconc (mapcar
4084                             (lambda (h) (mail-header-number (car h)))
4085                             (cddar thread))
4086                            gnus-tmp-gathered))
4087               (setq thread (cons (list (caar thread)
4088                                        (cadar thread))
4089                                  (cdr thread)))
4090               (setq gnus-tmp-level -1
4091                     gnus-tmp-false-parent t))
4092              ((eq gnus-summary-make-false-root 'empty)
4093               ;; We print adopted articles with empty subject fields.
4094               (setq gnus-tmp-gathered
4095                     (nconc (mapcar
4096                             (lambda (h) (mail-header-number (car h)))
4097                             (cddar thread))
4098                            gnus-tmp-gathered))
4099               (setq gnus-tmp-level -1))
4100              ((eq gnus-summary-make-false-root 'dummy)
4101               ;; We remember that we probably want to output a dummy
4102               ;; root.
4103               (setq gnus-tmp-dummy-line gnus-tmp-header)
4104               (setq gnus-tmp-prev-subject gnus-tmp-header))
4105              (t
4106               ;; We do not make a root for the gathered
4107               ;; sub-threads at all.
4108               (setq gnus-tmp-level -1)))
4109
4110           (setq number (mail-header-number gnus-tmp-header)
4111                 subject (mail-header-subject gnus-tmp-header))
4112
4113           (cond
4114            ;; If the thread has changed subject, we might want to make
4115            ;; this subthread into a root.
4116            ((and (null gnus-thread-ignore-subject)
4117                  (not (zerop gnus-tmp-level))
4118                  gnus-tmp-prev-subject
4119                  (not (inline
4120                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4121             (setq new-roots (nconc new-roots (list (car thread)))
4122                   thread-end t
4123                   gnus-tmp-header nil))
4124            ;; If the article lies outside the current limit,
4125            ;; then we do not display it.
4126            ((not (memq number gnus-newsgroup-limit))
4127             (setq gnus-tmp-gathered
4128                   (nconc (mapcar
4129                           (lambda (h) (mail-header-number (car h)))
4130                           (cdar thread))
4131                          gnus-tmp-gathered))
4132             (setq gnus-tmp-new-adopts (if (cdar thread)
4133                                           (append gnus-tmp-new-adopts
4134                                                   (cdar thread))
4135                                         gnus-tmp-new-adopts)
4136                   thread-end t
4137                   gnus-tmp-header nil)
4138             (when (zerop gnus-tmp-level)
4139               (setq gnus-tmp-root-expunged t)))
4140            ;; Perhaps this article is to be marked as read?
4141            ((and gnus-summary-mark-below
4142                  (< (or (cdr (assq number gnus-newsgroup-scored))
4143                         default-score)
4144                     gnus-summary-mark-below)
4145                  ;; Don't touch sparse articles.
4146                  (not (gnus-summary-article-sparse-p number))
4147                  (not (gnus-summary-article-ancient-p number)))
4148             (setq gnus-newsgroup-unreads
4149                   (delq number gnus-newsgroup-unreads))
4150             (if gnus-newsgroup-auto-expire
4151                 (push number gnus-newsgroup-expirable)
4152               (push (cons number gnus-low-score-mark)
4153                     gnus-newsgroup-reads))))
4154
4155           (when gnus-tmp-header
4156             ;; We may have an old dummy line to output before this
4157             ;; article.
4158             (when (and gnus-tmp-dummy-line
4159                        (gnus-subject-equal
4160                         gnus-tmp-dummy-line
4161                         (mail-header-subject gnus-tmp-header)))
4162               (gnus-summary-insert-dummy-line
4163                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4164               (setq gnus-tmp-dummy-line nil))
4165
4166             ;; Compute the mark.
4167             (setq gnus-tmp-unread (gnus-article-mark number))
4168
4169             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4170                                   gnus-tmp-header gnus-tmp-level)
4171                   gnus-newsgroup-data)
4172
4173             ;; Actually insert the line.
4174             (setq
4175              gnus-tmp-subject-or-nil
4176              (cond
4177               ((and gnus-thread-ignore-subject
4178                     gnus-tmp-prev-subject
4179                     (not (inline (gnus-subject-equal
4180                                   gnus-tmp-prev-subject subject))))
4181                subject)
4182               ((zerop gnus-tmp-level)
4183                (if (and (eq gnus-summary-make-false-root 'empty)
4184                         (memq number gnus-tmp-gathered)
4185                         gnus-tmp-prev-subject
4186                         (inline (gnus-subject-equal
4187                                  gnus-tmp-prev-subject subject)))
4188                    gnus-summary-same-subject
4189                  subject))
4190               (t gnus-summary-same-subject)))
4191             (if (and (eq gnus-summary-make-false-root 'adopt)
4192                      (= gnus-tmp-level 1)
4193                      (memq number gnus-tmp-gathered))
4194                 (setq gnus-tmp-opening-bracket ?\<
4195                       gnus-tmp-closing-bracket ?\>)
4196               (setq gnus-tmp-opening-bracket ?\[
4197                     gnus-tmp-closing-bracket ?\]))
4198             (setq
4199              gnus-tmp-indentation
4200              (aref gnus-thread-indent-array gnus-tmp-level)
4201              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4202              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4203                                 gnus-summary-default-score 0)
4204              gnus-tmp-score-char
4205              (if (or (null gnus-summary-default-score)
4206                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4207                          gnus-summary-zcore-fuzz))
4208                  ?  ;Whitespace
4209                (if (< gnus-tmp-score gnus-summary-default-score)
4210                    gnus-score-below-mark gnus-score-over-mark))
4211              gnus-tmp-replied
4212              (cond ((memq number gnus-newsgroup-processable)
4213                     gnus-process-mark)
4214                    ((memq number gnus-newsgroup-cached)
4215                     gnus-cached-mark)
4216                    ((memq number gnus-newsgroup-replied)
4217                     gnus-replied-mark)
4218                    ((memq number gnus-newsgroup-saved)
4219                     gnus-saved-mark)
4220                    (t gnus-no-mark))
4221              gnus-tmp-from (mail-header-from gnus-tmp-header)
4222              gnus-tmp-name
4223              (cond
4224               ((string-match "<[^>]+> *$" gnus-tmp-from)
4225                (setq beg-match (match-beginning 0))
4226                (or (and (string-match "^\".+\"" gnus-tmp-from)
4227                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4228                    (substring gnus-tmp-from 0 beg-match)))
4229               ((string-match "(.+)" gnus-tmp-from)
4230                (substring gnus-tmp-from
4231                           (1+ (match-beginning 0)) (1- (match-end 0))))
4232               (t gnus-tmp-from)))
4233             (when (string= gnus-tmp-name "")
4234               (setq gnus-tmp-name gnus-tmp-from))
4235             (unless (numberp gnus-tmp-lines)
4236               (setq gnus-tmp-lines 0))
4237             (gnus-put-text-property
4238              (point)
4239              (progn (eval gnus-summary-line-format-spec) (point))
4240              'gnus-number number)
4241             (when gnus-visual-p
4242               (forward-line -1)
4243               (gnus-run-hooks 'gnus-summary-update-hook)
4244               (forward-line 1))
4245
4246             (setq gnus-tmp-prev-subject subject)))
4247
4248         (when (nth 1 thread)
4249           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4250         (incf gnus-tmp-level)
4251         (setq threads (if thread-end nil (cdar thread)))
4252         (unless threads
4253           (setq gnus-tmp-level 0)))))
4254   (gnus-message 7 "Generating summary...done"))
4255
4256 (defun gnus-summary-prepare-unthreaded (headers)
4257   "Generate an unthreaded summary buffer based on HEADERS."
4258   (let (header number mark)
4259
4260     (beginning-of-line)
4261
4262     (while headers
4263       ;; We may have to root out some bad articles...
4264       (when (memq (setq number (mail-header-number
4265                                 (setq header (pop headers))))
4266                   gnus-newsgroup-limit)
4267         ;; Mark article as read when it has a low score.
4268         (when (and gnus-summary-mark-below
4269                    (< (or (cdr (assq number gnus-newsgroup-scored))
4270                           gnus-summary-default-score 0)
4271                       gnus-summary-mark-below)
4272                    (not (gnus-summary-article-ancient-p number)))
4273           (setq gnus-newsgroup-unreads
4274                 (delq number gnus-newsgroup-unreads))
4275           (if gnus-newsgroup-auto-expire
4276               (push number gnus-newsgroup-expirable)
4277             (push (cons number gnus-low-score-mark)
4278                   gnus-newsgroup-reads)))
4279
4280         (setq mark (gnus-article-mark number))
4281         (push (gnus-data-make number mark (1+ (point)) header 0)
4282               gnus-newsgroup-data)
4283         (gnus-summary-insert-line
4284          header 0 number
4285          mark (memq number gnus-newsgroup-replied)
4286          (memq number gnus-newsgroup-expirable)
4287          (mail-header-subject header) nil
4288          (cdr (assq number gnus-newsgroup-scored))
4289          (memq number gnus-newsgroup-processable))))))
4290
4291 (defun gnus-summary-remove-list-identifiers ()
4292   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4293   (let ((regexp (if (consp gnus-list-identifiers)
4294                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4295                   gnus-list-identifiers))
4296         changed subject)
4297     (when regexp
4298       (dolist (header gnus-newsgroup-headers)
4299         (setq subject (mail-header-subject header)
4300               changed nil)
4301         (while (string-match
4302                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4303                 subject)
4304           (setq subject
4305                 (concat (substring subject 0 (match-beginning 2))
4306                         (substring subject (match-end 0)))
4307                 changed t))
4308         (when (and changed
4309                    (string-match
4310                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4311           (setq subject
4312                 (concat (substring subject 0 (match-beginning 1))
4313                         (substring subject (match-end 1)))))
4314         (when changed
4315           (mail-header-set-subject header subject))))))
4316
4317 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4318   "Select newsgroup GROUP.
4319 If READ-ALL is non-nil, all articles in the group are selected.
4320 If SELECT-ARTICLES, only select those articles from GROUP."
4321   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4322          ;;!!! Dirty hack; should be removed.
4323          (gnus-summary-ignore-duplicates
4324           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4325               t
4326             gnus-summary-ignore-duplicates))
4327          (info (nth 2 entry))
4328          articles fetched-articles cached)
4329
4330     (unless (gnus-check-server
4331              (setq gnus-current-select-method
4332                    (gnus-find-method-for-group group)))
4333       (error "Couldn't open server"))
4334
4335     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4336         (gnus-activate-group group)     ; Or we can activate it...
4337         (progn                          ; Or we bug out.
4338           (when (equal major-mode 'gnus-summary-mode)
4339             (kill-buffer (current-buffer)))
4340           (error "Couldn't activate group %s: %s"
4341                  group (gnus-status-message group))))
4342
4343     (unless (gnus-request-group group t)
4344       (when (equal major-mode 'gnus-summary-mode)
4345         (kill-buffer (current-buffer)))
4346       (error "Couldn't request group %s: %s"
4347              group (gnus-status-message group)))
4348
4349     (setq gnus-newsgroup-name group)
4350     (setq gnus-newsgroup-unselected nil)
4351     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4352     (gnus-summary-setup-default-charset)
4353
4354     ;; Adjust and set lists of article marks.
4355     (when info
4356       (gnus-adjust-marked-articles info))
4357
4358     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4359     (setq cached
4360           (if (gnus-virtual-group-p group)
4361               gnus-newsgroup-cached
4362             (gnus-cache-articles-in-group group)))
4363
4364     (setq gnus-newsgroup-unreads
4365           (gnus-set-difference
4366            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4367            gnus-newsgroup-dormant))
4368
4369     (setq gnus-newsgroup-processable nil)
4370
4371     (gnus-update-read-articles group gnus-newsgroup-unreads)
4372
4373     (if (setq articles select-articles)
4374         (setq gnus-newsgroup-unselected
4375               (gnus-sorted-intersection
4376                gnus-newsgroup-unreads
4377                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4378       (setq articles (gnus-articles-to-read group read-all)))
4379
4380     (cond
4381      ((null articles)
4382       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4383       'quit)
4384      ((eq articles 0) nil)
4385      (t
4386       ;; Init the dependencies hash table.
4387       (setq gnus-newsgroup-dependencies
4388             (gnus-make-hashtable (length articles)))
4389       (gnus-set-global-variables)
4390       ;; Retrieve the headers and read them in.
4391       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4392       (setq gnus-newsgroup-headers
4393             (gnus-retrieve-parsed-headers
4394              articles gnus-newsgroup-name
4395              ;; We might want to fetch old headers, but
4396              ;; not if there is only 1 article.
4397              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4398                            (not (numberp gnus-fetch-old-headers)))
4399                       (> (length articles) 1))
4400                   gnus-fetch-old-headers)))
4401       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4402
4403       ;; Suppress duplicates?
4404       (when gnus-suppress-duplicates
4405         (gnus-dup-suppress-articles))
4406
4407       ;; Set the initial limit.
4408       (setq gnus-newsgroup-limit (copy-sequence articles))
4409       ;; Remove canceled articles from the list of unread articles.
4410       (setq gnus-newsgroup-unreads
4411             (gnus-set-sorted-intersection
4412              gnus-newsgroup-unreads
4413              (setq fetched-articles
4414                    (mapcar (lambda (headers) (mail-header-number headers))
4415                            gnus-newsgroup-headers))))
4416       ;; Removed marked articles that do not exist.
4417       (gnus-update-missing-marks
4418        (gnus-sorted-complement fetched-articles articles))
4419
4420       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4421       (when cached
4422         (setq gnus-newsgroup-cached cached))
4423
4424       ;; We might want to build some more threads first.
4425       (when (and gnus-fetch-old-headers
4426                  (eq gnus-headers-retrieved-by 'nov))
4427         (if (eq gnus-fetch-old-headers 'invisible)
4428             (gnus-build-all-threads)
4429           (gnus-build-old-threads)))
4430       ;; Let the Gnus agent mark articles as read.
4431       (when gnus-agent
4432         (gnus-agent-get-undownloaded-list))
4433       ;; Remove list identifiers from subject
4434       (when gnus-list-identifiers
4435         (gnus-summary-remove-list-identifiers))
4436       ;; Check whether auto-expire is to be done in this group.
4437       (setq gnus-newsgroup-auto-expire
4438             (gnus-group-auto-expirable-p group))
4439       ;; Set up the article buffer now, if necessary.
4440       (unless gnus-single-article-buffer
4441         (gnus-article-setup-buffer))
4442       ;; First and last article in this newsgroup.
4443       (when gnus-newsgroup-headers
4444         (setq gnus-newsgroup-begin
4445               (mail-header-number (car gnus-newsgroup-headers))
4446               gnus-newsgroup-end
4447               (mail-header-number
4448                (gnus-last-element gnus-newsgroup-headers))))
4449       ;; GROUP is successfully selected.
4450       (or gnus-newsgroup-headers t)))))
4451
4452 (defun gnus-articles-to-read (group &optional read-all)
4453   "Find out what articles the user wants to read."
4454   (let* ((articles
4455           ;; Select all articles if `read-all' is non-nil, or if there
4456           ;; are no unread articles.
4457           (if (or read-all
4458                   (and (zerop (length gnus-newsgroup-marked))
4459                        (zerop (length gnus-newsgroup-unreads)))
4460                   (eq (gnus-group-find-parameter group 'display)
4461                       'all))
4462               (or
4463                (gnus-uncompress-range (gnus-active group))
4464                (gnus-cache-articles-in-group group))
4465             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4466                           (copy-sequence gnus-newsgroup-unreads))
4467                   '<)))
4468          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4469          (scored (length scored-list))
4470          (number (length articles))
4471          (marked (+ (length gnus-newsgroup-marked)
4472                     (length gnus-newsgroup-dormant)))
4473          (select
4474           (cond
4475            ((numberp read-all)
4476             read-all)
4477            (t
4478             (condition-case ()
4479                 (cond
4480                  ((and (or (<= scored marked) (= scored number))
4481                        (natnump gnus-large-newsgroup)
4482                        (> number gnus-large-newsgroup))
4483                   (let* ((cursor-in-echo-area nil)
4484                          (input (read-from-minibuffer
4485                                  (format
4486                                   "How many articles from %s (max %d): "
4487                                   (gnus-limit-string gnus-newsgroup-name 35)
4488                                   number)
4489                                  (cons (number-to-string gnus-large-newsgroup)
4490                                        0))))
4491                     (if (string-match "^[ \t]*$" input)
4492                         number
4493                       input)))
4494                  ((and (> scored marked) (< scored number)
4495                        (> (- scored number) 20))
4496                   (let ((input
4497                          (read-string
4498                           (format "%s %s (%d scored, %d total): "
4499                                   "How many articles from"
4500                                   group scored number))))
4501                     (if (string-match "^[ \t]*$" input)
4502                         number input)))
4503                  (t number))
4504               (quit
4505                (message "Quit getting the articles to read")
4506                nil))))))
4507     (setq select (if (stringp select) (string-to-number select) select))
4508     (if (or (null select) (zerop select))
4509         select
4510       (if (and (not (zerop scored)) (<= (abs select) scored))
4511           (progn
4512             (setq articles (sort scored-list '<))
4513             (setq number (length articles)))
4514         (setq articles (copy-sequence articles)))
4515
4516       (when (< (abs select) number)
4517         (if (< select 0)
4518             ;; Select the N oldest articles.
4519             (setcdr (nthcdr (1- (abs select)) articles) nil)
4520           ;; Select the N most recent articles.
4521           (setq articles (nthcdr (- number select) articles))))
4522       (setq gnus-newsgroup-unselected
4523             (gnus-sorted-intersection
4524              gnus-newsgroup-unreads
4525              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4526       (when gnus-alter-articles-to-read-function
4527         (setq gnus-newsgroup-unreads
4528               (sort
4529                (funcall gnus-alter-articles-to-read-function
4530                         gnus-newsgroup-name gnus-newsgroup-unreads)
4531                '<)))
4532       articles)))
4533
4534 (defun gnus-killed-articles (killed articles)
4535   (let (out)
4536     (while articles
4537       (when (inline (gnus-member-of-range (car articles) killed))
4538         (push (car articles) out))
4539       (setq articles (cdr articles)))
4540     out))
4541
4542 (defun gnus-uncompress-marks (marks)
4543   "Uncompress the mark ranges in MARKS."
4544   (let ((uncompressed '(score bookmark))
4545         out)
4546     (while marks
4547       (if (memq (caar marks) uncompressed)
4548           (push (car marks) out)
4549         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4550       (setq marks (cdr marks)))
4551     out))
4552
4553 (defun gnus-adjust-marked-articles (info)
4554   "Set all article lists and remove all marks that are no longer valid."
4555   (let* ((marked-lists (gnus-info-marks info))
4556          (active (gnus-active (gnus-info-group info)))
4557          (min (car active))
4558          (max (cdr active))
4559          (types gnus-article-mark-lists)
4560          (uncompressed '(score bookmark killed))
4561          marks var articles article mark)
4562
4563     (while marked-lists
4564       (setq marks (pop marked-lists))
4565       (set (setq var (intern (format "gnus-newsgroup-%s"
4566                                      (car (rassq (setq mark (car marks))
4567                                                  types)))))
4568            (if (memq (car marks) uncompressed) (cdr marks)
4569              (gnus-uncompress-range (cdr marks))))
4570
4571       (setq articles (symbol-value var))
4572
4573       ;; All articles have to be subsets of the active articles.
4574       (cond
4575        ;; Adjust "simple" lists.
4576        ((memq mark '(tick dormant expire reply save))
4577         (while articles
4578           (when (or (< (setq article (pop articles)) min) (> article max))
4579             (set var (delq article (symbol-value var))))))
4580        ;; Adjust assocs.
4581        ((memq mark uncompressed)
4582         (when (not (listp (cdr (symbol-value var))))
4583           (set var (list (symbol-value var))))
4584         (when (not (listp (cdr articles)))
4585           (setq articles (list articles)))
4586         (while articles
4587           (when (or (not (consp (setq article (pop articles))))
4588                     (< (car article) min)
4589                     (> (car article) max))
4590             (set var (delq article (symbol-value var))))))))))
4591
4592 (defun gnus-update-missing-marks (missing)
4593   "Go through the list of MISSING articles and remove them from the mark lists."
4594   (when missing
4595     (let ((types gnus-article-mark-lists)
4596           var m)
4597       ;; Go through all types.
4598       (while types
4599         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4600         (when (symbol-value var)
4601           ;; This list has articles.  So we delete all missing articles
4602           ;; from it.
4603           (setq m missing)
4604           (while m
4605             (set var (delq (pop m) (symbol-value var)))))))))
4606
4607 (defun gnus-update-marks ()
4608   "Enter the various lists of marked articles into the newsgroup info list."
4609   (let ((types gnus-article-mark-lists)
4610         (info (gnus-get-info gnus-newsgroup-name))
4611         (uncompressed '(score bookmark killed))
4612         type list newmarked symbol delta-marks)
4613     (when info
4614       ;; Add all marks lists to the list of marks lists.
4615       (while (setq type (pop types))
4616         (setq list (symbol-value
4617                     (setq symbol
4618                           (intern (format "gnus-newsgroup-%s"
4619                                           (car type))))))
4620
4621         (when list
4622           ;; Get rid of the entries of the articles that have the
4623           ;; default score.
4624           (when (and (eq (cdr type) 'score)
4625                      gnus-save-score
4626                      list)
4627             (let* ((arts list)
4628                    (prev (cons nil list))
4629                    (all prev))
4630               (while arts
4631                 (if (or (not (consp (car arts)))
4632                         (= (cdar arts) gnus-summary-default-score))
4633                     (setcdr prev (cdr arts))
4634                   (setq prev arts))
4635                 (setq arts (cdr arts)))
4636               (setq list (cdr all)))))
4637
4638         (unless (memq (cdr type) uncompressed)
4639           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4640
4641         (when (gnus-check-backend-function
4642                'request-set-mark gnus-newsgroup-name)
4643           ;; propagate flags to server, with the following exceptions:
4644           ;; uncompressed:s are not proper flags (they are cons cells)
4645           ;; cache is a internal gnus flag
4646           ;; download are local to one gnus installation (well)
4647           ;; unsend are for nndraft groups only
4648           ;; xxx: generality of this?  this suits nnimap anyway
4649           (unless (memq (cdr type) (append '(cache download unsend)
4650                                            uncompressed))
4651             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4652                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4653                    (add (gnus-remove-from-range
4654                          (gnus-copy-sequence list) old)))
4655               (when add
4656                 (push (list add 'add (list (cdr type))) delta-marks))
4657               (when del
4658                 (push (list del 'del (list (cdr type))) delta-marks)))))
4659
4660         (when list
4661           (push (cons (cdr type) list) newmarked)))
4662
4663       (when delta-marks
4664         (unless (gnus-check-group gnus-newsgroup-name)
4665           (error "Can't open server for %s" gnus-newsgroup-name))
4666         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4667
4668       ;; Enter these new marks into the info of the group.
4669       (if (nthcdr 3 info)
4670           (setcar (nthcdr 3 info) newmarked)
4671         ;; Add the marks lists to the end of the info.
4672         (when newmarked
4673           (setcdr (nthcdr 2 info) (list newmarked))))
4674
4675       ;; Cut off the end of the info if there's nothing else there.
4676       (let ((i 5))
4677         (while (and (> i 2)
4678                     (not (nth i info)))
4679           (when (nthcdr (decf i) info)
4680             (setcdr (nthcdr i info) nil)))))))
4681
4682 (defun gnus-set-mode-line (where)
4683   "Set the mode line of the article or summary buffers.
4684 If WHERE is `summary', the summary mode line format will be used."
4685   ;; Is this mode line one we keep updated?
4686   (when (and (memq where gnus-updated-mode-lines)
4687              (symbol-value
4688               (intern (format "gnus-%s-mode-line-format-spec" where))))
4689     (let (mode-string)
4690       (save-excursion
4691         ;; We evaluate this in the summary buffer since these
4692         ;; variables are buffer-local to that buffer.
4693         (set-buffer gnus-summary-buffer)
4694         ;; We bind all these variables that are used in the `eval' form
4695         ;; below.
4696         (let* ((mformat (symbol-value
4697                          (intern
4698                           (format "gnus-%s-mode-line-format-spec" where))))
4699                (gnus-tmp-group-name (gnus-group-name-decode
4700                                      gnus-newsgroup-name
4701                                      (gnus-group-name-charset
4702                                       nil
4703                                       gnus-newsgroup-name)))
4704                (gnus-tmp-article-number (or gnus-current-article 0))
4705                (gnus-tmp-unread gnus-newsgroup-unreads)
4706                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4707                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4708                (gnus-tmp-unread-and-unselected
4709                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4710                             (zerop gnus-tmp-unselected))
4711                        "")
4712                       ((zerop gnus-tmp-unselected)
4713                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4714                       (t (format "{%d(+%d) more}"
4715                                  gnus-tmp-unread-and-unticked
4716                                  gnus-tmp-unselected))))
4717                (gnus-tmp-subject
4718                 (if (and gnus-current-headers
4719                          (vectorp gnus-current-headers))
4720                     (gnus-mode-string-quote
4721                      (mail-header-subject gnus-current-headers))
4722                   ""))
4723                bufname-length max-len
4724                gnus-tmp-header);; passed as argument to any user-format-funcs
4725           (setq mode-string (eval mformat))
4726           (setq bufname-length (if (string-match "%b" mode-string)
4727                                    (- (length
4728                                        (buffer-name
4729                                         (if (eq where 'summary)
4730                                             nil
4731                                           (get-buffer gnus-article-buffer))))
4732                                       2)
4733                                  0))
4734           (setq max-len (max 4 (if gnus-mode-non-string-length
4735                                    (- (window-width)
4736                                       gnus-mode-non-string-length
4737                                       bufname-length)
4738                                  (length mode-string))))
4739           ;; We might have to chop a bit of the string off...
4740           (when (> (length mode-string) max-len)
4741             (setq mode-string
4742                   (concat (gnus-truncate-string mode-string (- max-len 3))
4743                           "...")))
4744           ;; Pad the mode string a bit.
4745           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4746       ;; Update the mode line.
4747       (setq mode-line-buffer-identification
4748             (gnus-mode-line-buffer-identification (list mode-string)))
4749       (set-buffer-modified-p t))))
4750
4751 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4752   "Go through the HEADERS list and add all Xrefs to a hash table.
4753 The resulting hash table is returned, or nil if no Xrefs were found."
4754   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4755          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4756          (xref-hashtb (gnus-make-hashtable))
4757          start group entry number xrefs header)
4758     (while headers
4759       (setq header (pop headers))
4760       (when (and (setq xrefs (mail-header-xref header))
4761                  (not (memq (setq number (mail-header-number header))
4762                             unreads)))
4763         (setq start 0)
4764         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4765           (setq start (match-end 0))
4766           (setq group (if prefix
4767                           (concat prefix (substring xrefs (match-beginning 1)
4768                                                     (match-end 1)))
4769                         (substring xrefs (match-beginning 1) (match-end 1))))
4770           (setq number
4771                 (string-to-int (substring xrefs (match-beginning 2)
4772                                           (match-end 2))))
4773           (if (setq entry (gnus-gethash group xref-hashtb))
4774               (setcdr entry (cons number (cdr entry)))
4775             (gnus-sethash group (cons number nil) xref-hashtb)))))
4776     (and start xref-hashtb)))
4777
4778 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4779   "Look through all the headers and mark the Xrefs as read."
4780   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4781         name entry info xref-hashtb idlist method nth4)
4782     (save-excursion
4783       (set-buffer gnus-group-buffer)
4784       (when (setq xref-hashtb
4785                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4786         (mapatoms
4787          (lambda (group)
4788            (unless (string= from-newsgroup (setq name (symbol-name group)))
4789              (setq idlist (symbol-value group))
4790              ;; Dead groups are not updated.
4791              (and (prog1
4792                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4793                             info (nth 2 entry))
4794                     (when (stringp (setq nth4 (gnus-info-method info)))
4795                       (setq nth4 (gnus-server-to-method nth4))))
4796                   ;; Only do the xrefs if the group has the same
4797                   ;; select method as the group we have just read.
4798                   (or (gnus-methods-equal-p
4799                        nth4 (gnus-find-method-for-group from-newsgroup))
4800                       virtual
4801                       (equal nth4 (setq method (gnus-find-method-for-group
4802                                                 from-newsgroup)))
4803                       (and (equal (car nth4) (car method))
4804                            (equal (nth 1 nth4) (nth 1 method))))
4805                   gnus-use-cross-reference
4806                   (or (not (eq gnus-use-cross-reference t))
4807                       virtual
4808                       ;; Only do cross-references on subscribed
4809                       ;; groups, if that is what is wanted.
4810                       (<= (gnus-info-level info) gnus-level-subscribed))
4811                   (gnus-group-make-articles-read name idlist))))
4812          xref-hashtb)))))
4813
4814 (defun gnus-compute-read-articles (group articles)
4815   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4816          (info (nth 2 entry))
4817          (active (gnus-active group))
4818          ninfo)
4819     (when entry
4820       ;; First peel off all invalid article numbers.
4821       (when active
4822         (let ((ids articles)
4823               id first)
4824           (while (setq id (pop ids))
4825             (when (and first (> id (cdr active)))
4826               ;; We'll end up in this situation in one particular
4827               ;; obscure situation.  If you re-scan a group and get
4828               ;; a new article that is cross-posted to a different
4829               ;; group that has not been re-scanned, you might get
4830               ;; crossposted article that has a higher number than
4831               ;; Gnus believes possible.  So we re-activate this
4832               ;; group as well.  This might mean doing the
4833               ;; crossposting thingy will *increase* the number
4834               ;; of articles in some groups.  Tsk, tsk.
4835               (setq active (or (gnus-activate-group group) active)))
4836             (when (or (> id (cdr active))
4837                       (< id (car active)))
4838               (setq articles (delq id articles))))))
4839       ;; If the read list is nil, we init it.
4840       (if (and active
4841                (null (gnus-info-read info))
4842                (> (car active) 1))
4843           (setq ninfo (cons 1 (1- (car active))))
4844         (setq ninfo (gnus-info-read info)))
4845       ;; Then we add the read articles to the range.
4846       (gnus-add-to-range
4847        ninfo (setq articles (sort articles '<))))))
4848
4849 (defun gnus-group-make-articles-read (group articles)
4850   "Update the info of GROUP to say that ARTICLES are read."
4851   (let* ((num 0)
4852          (entry (gnus-gethash group gnus-newsrc-hashtb))
4853          (info (nth 2 entry))
4854          (active (gnus-active group))
4855          range)
4856     (when entry
4857       (setq range (gnus-compute-read-articles group articles))
4858       (save-excursion
4859         (set-buffer gnus-group-buffer)
4860         (gnus-undo-register
4861           `(progn
4862              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4863              (gnus-info-set-read ',info ',(gnus-info-read info))
4864              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4865              (gnus-group-update-group ,group t))))
4866       ;; Add the read articles to the range.
4867       (gnus-info-set-read info range)
4868       ;; Then we have to re-compute how many unread
4869       ;; articles there are in this group.
4870       (when active
4871         (cond
4872          ((not range)
4873           (setq num (- (1+ (cdr active)) (car active))))
4874          ((not (listp (cdr range)))
4875           (setq num (- (cdr active) (- (1+ (cdr range))
4876                                        (car range)))))
4877          (t
4878           (while range
4879             (if (numberp (car range))
4880                 (setq num (1+ num))
4881               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4882             (setq range (cdr range)))
4883           (setq num (- (cdr active) num))))
4884         ;; Update the number of unread articles.
4885         (setcar entry num)
4886         ;; Update the group buffer.
4887         (gnus-group-update-group group t)))))
4888
4889 (defvar gnus-newsgroup-none-id 0)
4890
4891 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4892   (let ((cur nntp-server-buffer)
4893         (dependencies
4894          (or dependencies
4895              (save-excursion (set-buffer gnus-summary-buffer)
4896                              gnus-newsgroup-dependencies)))
4897         headers id end ref
4898         (mail-parse-charset gnus-newsgroup-charset)
4899         (mail-parse-ignored-charsets
4900          (save-excursion (condition-case nil
4901                              (set-buffer gnus-summary-buffer)
4902                            (error))
4903                          gnus-newsgroup-ignored-charsets)))
4904     (save-excursion
4905       (set-buffer nntp-server-buffer)
4906       ;; Translate all TAB characters into SPACE characters.
4907       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4908       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4909       (gnus-run-hooks 'gnus-parse-headers-hook)
4910       (let ((case-fold-search t)
4911             in-reply-to header p lines chars ctype)
4912         (goto-char (point-min))
4913         ;; Search to the beginning of the next header.  Error messages
4914         ;; do not begin with 2 or 3.
4915         (while (re-search-forward "^[23][0-9]+ " nil t)
4916           (setq id nil
4917                 ref nil)
4918           ;; This implementation of this function, with nine
4919           ;; search-forwards instead of the one re-search-forward and
4920           ;; a case (which basically was the old function) is actually
4921           ;; about twice as fast, even though it looks messier.  You
4922           ;; can't have everything, I guess.  Speed and elegance
4923           ;; doesn't always go hand in hand.
4924           (setq
4925            header
4926            (make-full-mail-header
4927             ;; Number.
4928             (prog1
4929                 (read cur)
4930               (end-of-line)
4931               (setq p (point))
4932               (narrow-to-region (point)
4933                                 (or (and (search-forward "\n.\n" nil t)
4934                                          (- (point) 2))
4935                                     (point))))
4936             ;; Subject.
4937             (progn
4938               (goto-char p)
4939               (if (search-forward "\nsubject: " nil t)
4940                   (nnheader-header-value)
4941                 "(none)"))
4942             ;; From.
4943             (progn
4944               (goto-char p)
4945               (if (or (search-forward "\nfrom: " nil t)
4946                       (search-forward "\nfrom:" nil t))
4947                   (nnheader-header-value)
4948                 "(nobody)"))
4949             ;; Date.
4950             (progn
4951               (goto-char p)
4952               (if (search-forward "\ndate: " nil t)
4953                   (nnheader-header-value)
4954                 ""))
4955             ;; Message-ID.
4956             (progn
4957               (goto-char p)
4958               (setq id (if (re-search-forward
4959                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4960                            ;; We do it this way to make sure the Message-ID
4961                            ;; is (somewhat) syntactically valid.
4962                            (buffer-substring (match-beginning 1)
4963                                              (match-end 1))
4964                          ;; If there was no message-id, we just fake one
4965                          ;; to make subsequent routines simpler.
4966                          (nnheader-generate-fake-message-id))))
4967             ;; References.
4968             (progn
4969               (goto-char p)
4970               (if (search-forward "\nreferences: " nil t)
4971                   (progn
4972                     (setq end (point))
4973                     (prog1
4974                         (nnheader-header-value)
4975                       (setq ref
4976                             (buffer-substring
4977                              (progn
4978                                ;; (end-of-line)
4979                                (search-backward ">" end t)
4980                                (1+ (point)))
4981                              (progn
4982                                (search-backward "<" end t)
4983                                (point))))))
4984                 ;; Get the references from the in-reply-to header if there
4985                 ;; were no references and the in-reply-to header looks
4986                 ;; promising.
4987                 (if (and (search-forward "\nin-reply-to: " nil t)
4988                          (setq in-reply-to (nnheader-header-value))
4989                          (string-match "<[^>]+>" in-reply-to))
4990                     (let (ref2)
4991                       (setq ref (substring in-reply-to (match-beginning 0)
4992                                            (match-end 0)))
4993                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4994                         (setq ref2 (substring in-reply-to (match-beginning 0)
4995                                               (match-end 0)))
4996                         (when (> (length ref2) (length ref))
4997                           (setq ref ref2)))
4998                       ref)
4999                   (setq ref nil))))
5000             ;; Chars.
5001             (progn
5002               (goto-char p)
5003               (if (search-forward "\nchars: " nil t)
5004                   (if (numberp (setq chars (ignore-errors (read cur))))
5005                       chars 0)
5006                 0))
5007             ;; Lines.
5008             (progn
5009               (goto-char p)
5010               (if (search-forward "\nlines: " nil t)
5011                   (if (numberp (setq lines (ignore-errors (read cur))))
5012                       lines 0)
5013                 0))
5014             ;; Xref.
5015             (progn
5016               (goto-char p)
5017               (and (search-forward "\nxref: " nil t)
5018                    (nnheader-header-value)))
5019             ;; Extra.
5020             (when gnus-extra-headers
5021               (let ((extra gnus-extra-headers)
5022                     out)
5023                 (while extra
5024                   (goto-char p)
5025                   (when (search-forward
5026                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
5027                     (push (cons (car extra) (nnheader-header-value)) out))
5028                   (pop extra))
5029                 out))))
5030           (goto-char p)
5031           (if (and (search-forward "\ncontent-type: " nil t)
5032                    (setq ctype (nnheader-header-value)))
5033               (mime-entity-set-content-type-internal
5034                header (mime-parse-Content-Type ctype)))
5035           (when (equal id ref)
5036             (setq ref nil))
5037
5038           (when gnus-alter-header-function
5039             (funcall gnus-alter-header-function header)
5040             (setq id (mail-header-id header)
5041                   ref (gnus-parent-id (mail-header-references header))))
5042
5043           (when (setq header
5044                       (gnus-dependencies-add-header
5045                        header dependencies force-new))
5046             (push header headers))
5047           (goto-char (point-max))
5048           (widen))
5049         (nreverse headers)))))
5050
5051 ;; Goes through the xover lines and returns a list of vectors
5052 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5053                                                   force-new dependencies
5054                                                   group also-fetch-heads)
5055   "Parse the news overview data in the server buffer.
5056 Return a list of headers that match SEQUENCE (see
5057 `nntp-retrieve-headers')."
5058   ;; Get the Xref when the users reads the articles since most/some
5059   ;; NNTP servers do not include Xrefs when using XOVER.
5060   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5061   (let ((mail-parse-charset gnus-newsgroup-charset)
5062         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5063         (cur nntp-server-buffer)
5064         (dependencies (or dependencies gnus-newsgroup-dependencies))
5065         number headers header)
5066     (save-excursion
5067       (set-buffer nntp-server-buffer)
5068       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5069       ;; Allow the user to mangle the headers before parsing them.
5070       (gnus-run-hooks 'gnus-parse-headers-hook)
5071       (goto-char (point-min))
5072       (while (not (eobp))
5073         (condition-case ()
5074             (while (and sequence (not (eobp)))
5075               (setq number (read cur))
5076               (while (and sequence
5077                           (< (car sequence) number))
5078                 (setq sequence (cdr sequence)))
5079               (and sequence
5080                    (eq number (car sequence))
5081                    (progn
5082                      (setq sequence (cdr sequence))
5083                      (setq header (inline
5084                                     (gnus-nov-parse-line
5085                                      number dependencies force-new))))
5086                    (push header headers))
5087               (forward-line 1))
5088           (error
5089            (gnus-error 4 "Strange nov line (%d)"
5090                        (count-lines (point-min) (point)))))
5091         (forward-line 1))
5092       ;; A common bug in inn is that if you have posted an article and
5093       ;; then retrieves the active file, it will answer correctly --
5094       ;; the new article is included.  However, a NOV entry for the
5095       ;; article may not have been generated yet, so this may fail.
5096       ;; We work around this problem by retrieving the last few
5097       ;; headers using HEAD.
5098       (if (or (not also-fetch-heads)
5099               (not sequence))
5100           ;; We (probably) got all the headers.
5101           (nreverse headers)
5102         (let ((gnus-nov-is-evil t))
5103           (nconc
5104            (nreverse headers)
5105            (gnus-retrieve-parsed-headers sequence group)
5106            ))))))
5107
5108 (defun gnus-article-get-xrefs ()
5109   "Fill in the Xref value in `gnus-current-headers', if necessary.
5110 This is meant to be called in `gnus-article-internal-prepare-hook'."
5111   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5112                                  gnus-current-headers)))
5113     (or (not gnus-use-cross-reference)
5114         (not headers)
5115         (and (mail-header-xref headers)
5116              (not (string= (mail-header-xref headers) "")))
5117         (let ((case-fold-search t)
5118               xref)
5119           (save-restriction
5120             (nnheader-narrow-to-headers)
5121             (goto-char (point-min))
5122             (when (or (and (not (eobp))
5123                            (eq (downcase (char-after)) ?x)
5124                            (looking-at "Xref:"))
5125                       (search-forward "\nXref:" nil t))
5126               (goto-char (1+ (match-end 0)))
5127               (setq xref (buffer-substring (point)
5128                                            (progn (end-of-line) (point))))
5129               (mail-header-set-xref headers xref)))))))
5130
5131 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5132   "Find article ID and insert the summary line for that article.
5133 OLD-HEADER can either be a header or a line number to insert
5134 the subject line on."
5135   (let* ((line (and (numberp old-header) old-header))
5136          (old-header (and (vectorp old-header) old-header))
5137          (header (cond ((and old-header use-old-header)
5138                         old-header)
5139                        ((and (numberp id)
5140                              (gnus-number-to-header id))
5141                         (gnus-number-to-header id))
5142                        (t
5143                         (gnus-read-header id))))
5144          (number (and (numberp id) id))
5145          d)
5146     (when header
5147       ;; Rebuild the thread that this article is part of and go to the
5148       ;; article we have fetched.
5149       (when (and (not gnus-show-threads)
5150                  old-header)
5151         (when (and number
5152                    (setq d (gnus-data-find (mail-header-number old-header))))
5153           (goto-char (gnus-data-pos d))
5154           (gnus-data-remove
5155            number
5156            (- (gnus-point-at-bol)
5157               (prog1
5158                   (1+ (gnus-point-at-eol))
5159                 (gnus-delete-line))))))
5160       (when old-header
5161         (mail-header-set-number header (mail-header-number old-header)))
5162       (setq gnus-newsgroup-sparse
5163             (delq (setq number (mail-header-number header))
5164                   gnus-newsgroup-sparse))
5165       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5166       (push number gnus-newsgroup-limit)
5167       (gnus-rebuild-thread (mail-header-id header) line)
5168       (gnus-summary-goto-subject number nil t))
5169     (when (and (numberp number)
5170                (> number 0))
5171       ;; We have to update the boundaries even if we can't fetch the
5172       ;; article if ID is a number -- so that the next `P' or `N'
5173       ;; command will fetch the previous (or next) article even
5174       ;; if the one we tried to fetch this time has been canceled.
5175       (when (> number gnus-newsgroup-end)
5176         (setq gnus-newsgroup-end number))
5177       (when (< number gnus-newsgroup-begin)
5178         (setq gnus-newsgroup-begin number))
5179       (setq gnus-newsgroup-unselected
5180             (delq number gnus-newsgroup-unselected)))
5181     ;; Report back a success?
5182     (and header (mail-header-number header))))
5183
5184 ;;; Process/prefix in the summary buffer
5185
5186 (defun gnus-summary-work-articles (n)
5187   "Return a list of articles to be worked upon.
5188 The prefix argument, the list of process marked articles, and the
5189 current article will be taken into consideration."
5190   (save-excursion
5191     (set-buffer gnus-summary-buffer)
5192     (cond
5193      (n
5194       ;; A numerical prefix has been given.
5195       (setq n (prefix-numeric-value n))
5196       (let ((backward (< n 0))
5197             (n (abs (prefix-numeric-value n)))
5198             articles article)
5199         (save-excursion
5200           (while
5201               (and (> n 0)
5202                    (push (setq article (gnus-summary-article-number))
5203                          articles)
5204                    (if backward
5205                        (gnus-summary-find-prev nil article)
5206                      (gnus-summary-find-next nil article)))
5207             (decf n)))
5208         (nreverse articles)))
5209      ((and (gnus-region-active-p) (mark))
5210       (message "region active")
5211       ;; Work on the region between point and mark.
5212       (let ((max (max (point) (mark)))
5213             articles article)
5214         (save-excursion
5215           (goto-char (min (point) (mark)))
5216           (while
5217               (and
5218                (push (setq article (gnus-summary-article-number)) articles)
5219                (gnus-summary-find-next nil article)
5220                (< (point) max)))
5221           (nreverse articles))))
5222      (gnus-newsgroup-processable
5223       ;; There are process-marked articles present.
5224       ;; Save current state.
5225       (gnus-summary-save-process-mark)
5226       ;; Return the list.
5227       (reverse gnus-newsgroup-processable))
5228      (t
5229       ;; Just return the current article.
5230       (list (gnus-summary-article-number))))))
5231
5232 (defmacro gnus-summary-iterate (arg &rest forms)
5233   "Iterate over the process/prefixed articles and do FORMS.
5234 ARG is the interactive prefix given to the command.  FORMS will be
5235 executed with point over the summary line of the articles."
5236   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5237     `(let ((,articles (gnus-summary-work-articles ,arg)))
5238        (while ,articles
5239          (gnus-summary-goto-subject (car ,articles))
5240          ,@forms
5241          (pop ,articles)))))
5242
5243 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5244 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5245
5246 (defun gnus-summary-save-process-mark ()
5247   "Push the current set of process marked articles on the stack."
5248   (interactive)
5249   (push (copy-sequence gnus-newsgroup-processable)
5250         gnus-newsgroup-process-stack))
5251
5252 (defun gnus-summary-kill-process-mark ()
5253   "Push the current set of process marked articles on the stack and unmark."
5254   (interactive)
5255   (gnus-summary-save-process-mark)
5256   (gnus-summary-unmark-all-processable))
5257
5258 (defun gnus-summary-yank-process-mark ()
5259   "Pop the last process mark state off the stack and restore it."
5260   (interactive)
5261   (unless gnus-newsgroup-process-stack
5262     (error "Empty mark stack"))
5263   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5264
5265 (defun gnus-summary-process-mark-set (set)
5266   "Make SET into the current process marked articles."
5267   (gnus-summary-unmark-all-processable)
5268   (while set
5269     (gnus-summary-set-process-mark (pop set))))
5270
5271 ;;; Searching and stuff
5272
5273 (defun gnus-summary-search-group (&optional backward use-level)
5274   "Search for next unread newsgroup.
5275 If optional argument BACKWARD is non-nil, search backward instead."
5276   (save-excursion
5277     (set-buffer gnus-group-buffer)
5278     (when (gnus-group-search-forward
5279            backward nil (if use-level (gnus-group-group-level) nil))
5280       (gnus-group-group-name))))
5281
5282 (defun gnus-summary-best-group (&optional exclude-group)
5283   "Find the name of the best unread group.
5284 If EXCLUDE-GROUP, do not go to this group."
5285   (save-excursion
5286     (set-buffer gnus-group-buffer)
5287     (save-excursion
5288       (gnus-group-best-unread-group exclude-group))))
5289
5290 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5291   (if backward (gnus-summary-find-prev)
5292     (let* ((dummy (gnus-summary-article-intangible-p))
5293            (article (or article (gnus-summary-article-number)))
5294            (arts (gnus-data-find-list article))
5295            result)
5296       (when (and (not dummy)
5297                  (or (not gnus-summary-check-current)
5298                      (not unread)
5299                      (not (gnus-data-unread-p (car arts)))))
5300         (setq arts (cdr arts)))
5301       (when (setq result
5302                   (if unread
5303                       (progn
5304                         (while arts
5305                           (when (or (and undownloaded
5306                                          (eq gnus-undownloaded-mark
5307                                              (gnus-data-mark (car arts))))
5308                                     (gnus-data-unread-p (car arts)))
5309                             (setq result (car arts)
5310                                   arts nil))
5311                           (setq arts (cdr arts)))
5312                         result)
5313                     (car arts)))
5314         (goto-char (gnus-data-pos result))
5315         (gnus-data-number result)))))
5316
5317 (defun gnus-summary-find-prev (&optional unread article)
5318   (let* ((eobp (eobp))
5319          (article (or article (gnus-summary-article-number)))
5320          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5321          result)
5322     (when (and (not eobp)
5323                (or (not gnus-summary-check-current)
5324                    (not unread)
5325                    (not (gnus-data-unread-p (car arts)))))
5326       (setq arts (cdr arts)))
5327     (when (setq result
5328                 (if unread
5329                     (progn
5330                       (while arts
5331                         (when (gnus-data-unread-p (car arts))
5332                           (setq result (car arts)
5333                                 arts nil))
5334                         (setq arts (cdr arts)))
5335                       result)
5336                   (car arts)))
5337       (goto-char (gnus-data-pos result))
5338       (gnus-data-number result))))
5339
5340 (defun gnus-summary-find-subject (subject &optional unread backward article)
5341   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5342          (article (or article (gnus-summary-article-number)))
5343          (articles (gnus-data-list backward))
5344          (arts (gnus-data-find-list article articles))
5345          result)
5346     (when (or (not gnus-summary-check-current)
5347               (not unread)
5348               (not (gnus-data-unread-p (car arts))))
5349       (setq arts (cdr arts)))
5350     (while arts
5351       (and (or (not unread)
5352                (gnus-data-unread-p (car arts)))
5353            (vectorp (gnus-data-header (car arts)))
5354            (gnus-subject-equal
5355             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5356            (setq result (car arts)
5357                  arts nil))
5358       (setq arts (cdr arts)))
5359     (and result
5360          (goto-char (gnus-data-pos result))
5361          (gnus-data-number result))))
5362
5363 (defun gnus-summary-search-forward (&optional unread subject backward)
5364   "Search forward for an article.
5365 If UNREAD, look for unread articles.  If SUBJECT, look for
5366 articles with that subject.  If BACKWARD, search backward instead."
5367   (cond (subject (gnus-summary-find-subject subject unread backward))
5368         (backward (gnus-summary-find-prev unread))
5369         (t (gnus-summary-find-next unread))))
5370
5371 (defun gnus-recenter (&optional n)
5372   "Center point in window and redisplay frame.
5373 Also do horizontal recentering."
5374   (interactive "P")
5375   (when (and gnus-auto-center-summary
5376              (not (eq gnus-auto-center-summary 'vertical)))
5377     (gnus-horizontal-recenter))
5378   (recenter n))
5379
5380 (defun gnus-summary-recenter ()
5381   "Center point in the summary window.
5382 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5383 displayed, no centering will be performed."
5384   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5385   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5386   (interactive)
5387   (let* ((top (cond ((< (window-height) 4) 0)
5388                     ((< (window-height) 7) 1)
5389                     (t (if (numberp gnus-auto-center-summary)
5390                            gnus-auto-center-summary
5391                          2))))
5392          (height (1- (window-height)))
5393          (bottom (save-excursion (goto-char (point-max))
5394                                  (forward-line (- height))
5395                                  (point)))
5396          (window (get-buffer-window (current-buffer))))
5397     ;; The user has to want it.
5398     (when gnus-auto-center-summary
5399       (when (get-buffer-window gnus-article-buffer)
5400         ;; Only do recentering when the article buffer is displayed,
5401         ;; Set the window start to either `bottom', which is the biggest
5402         ;; possible valid number, or the second line from the top,
5403         ;; whichever is the least.
5404         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5405           (if (> bottom top-pos)
5406               ;; Keep the second line from the top visible
5407               (set-window-start window top-pos t)
5408             ;; Try to keep the bottom line visible; if it's partially
5409             ;; obscured, either scroll one more line to make it fully
5410             ;; visible, or revert to using TOP-POS.
5411             (save-excursion
5412               (goto-char (point-max))
5413               (forward-line -1)
5414               (let ((last-line-start (point)))
5415                 (goto-char bottom)
5416                 (set-window-start window (point) t)
5417                 (when (not (pos-visible-in-window-p last-line-start window))
5418                   (forward-line 1)
5419                   (set-window-start window (min (point) top-pos) t)))))))
5420       ;; Do horizontal recentering while we're at it.
5421       (when (and (get-buffer-window (current-buffer) t)
5422                  (not (eq gnus-auto-center-summary 'vertical)))
5423         (let ((selected (selected-window)))
5424           (select-window (get-buffer-window (current-buffer) t))
5425           (gnus-summary-position-point)
5426           (gnus-horizontal-recenter)
5427           (select-window selected))))))
5428
5429 (defun gnus-summary-jump-to-group (newsgroup)
5430   "Move point to NEWSGROUP in group mode buffer."
5431   ;; Keep update point of group mode buffer if visible.
5432   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5433       (save-window-excursion
5434         ;; Take care of tree window mode.
5435         (when (get-buffer-window gnus-group-buffer)
5436           (pop-to-buffer gnus-group-buffer))
5437         (gnus-group-jump-to-group newsgroup))
5438     (save-excursion
5439       ;; Take care of tree window mode.
5440       (if (get-buffer-window gnus-group-buffer)
5441           (pop-to-buffer gnus-group-buffer)
5442         (set-buffer gnus-group-buffer))
5443       (gnus-group-jump-to-group newsgroup))))
5444
5445 ;; This function returns a list of article numbers based on the
5446 ;; difference between the ranges of read articles in this group and
5447 ;; the range of active articles.
5448 (defun gnus-list-of-unread-articles (group)
5449   (let* ((read (gnus-info-read (gnus-get-info group)))
5450          (active (or (gnus-active group) (gnus-activate-group group)))
5451          (last (cdr active))
5452          first nlast unread)
5453     ;; If none are read, then all are unread.
5454     (if (not read)
5455         (setq first (car active))
5456       ;; If the range of read articles is a single range, then the
5457       ;; first unread article is the article after the last read
5458       ;; article.  Sounds logical, doesn't it?
5459       (if (and (not (listp (cdr read)))
5460                (or (< (car read) (car active))
5461                    (progn (setq read (list read))
5462                           nil)))
5463           (setq first (max (car active) (1+ (cdr read))))
5464         ;; `read' is a list of ranges.
5465         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5466                                   (caar read)))
5467                   1)
5468           (setq first (car active)))
5469         (while read
5470           (when first
5471             (while (< first nlast)
5472               (push first unread)
5473               (setq first (1+ first))))
5474           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5475           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5476           (setq read (cdr read)))))
5477     ;; And add the last unread articles.
5478     (while (<= first last)
5479       (push first unread)
5480       (setq first (1+ first)))
5481     ;; Return the list of unread articles.
5482     (delq 0 (nreverse unread))))
5483
5484 (defun gnus-list-of-read-articles (group)
5485   "Return a list of unread, unticked and non-dormant articles."
5486   (let* ((info (gnus-get-info group))
5487          (marked (gnus-info-marks info))
5488          (active (gnus-active group)))
5489     (and info active
5490          (gnus-set-difference
5491           (gnus-sorted-complement
5492            (gnus-uncompress-range active)
5493            (gnus-list-of-unread-articles group))
5494           (append
5495            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5496            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5497
5498 ;; Various summary commands
5499
5500 (defun gnus-summary-select-article-buffer ()
5501   "Reconfigure windows to show article buffer."
5502   (interactive)
5503   (if (not (gnus-buffer-live-p gnus-article-buffer))
5504       (error "There is no article buffer for this summary buffer")
5505     (gnus-configure-windows 'article)
5506     (select-window (get-buffer-window gnus-article-buffer))))
5507
5508 (defun gnus-summary-universal-argument (arg)
5509   "Perform any operation on all articles that are process/prefixed."
5510   (interactive "P")
5511   (let ((articles (gnus-summary-work-articles arg))
5512         func article)
5513     (if (eq
5514          (setq
5515           func
5516           (key-binding
5517            (read-key-sequence
5518             (substitute-command-keys
5519              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5520          'undefined)
5521         (gnus-error 1 "Undefined key")
5522       (save-excursion
5523         (while articles
5524           (gnus-summary-goto-subject (setq article (pop articles)))
5525           (let (gnus-newsgroup-processable)
5526             (command-execute func))
5527           (gnus-summary-remove-process-mark article)))))
5528   (gnus-summary-position-point))
5529
5530 (defun gnus-summary-toggle-truncation (&optional arg)
5531   "Toggle truncation of summary lines.
5532 With arg, turn line truncation on iff arg is positive."
5533   (interactive "P")
5534   (setq truncate-lines
5535         (if (null arg) (not truncate-lines)
5536           (> (prefix-numeric-value arg) 0)))
5537   (redraw-display))
5538
5539 (defun gnus-summary-reselect-current-group (&optional all rescan)
5540   "Exit and then reselect the current newsgroup.
5541 The prefix argument ALL means to select all articles."
5542   (interactive "P")
5543   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5544     (error "Ephemeral groups can't be reselected"))
5545   (let ((current-subject (gnus-summary-article-number))
5546         (group gnus-newsgroup-name))
5547     (setq gnus-newsgroup-begin nil)
5548     (gnus-summary-exit)
5549     ;; We have to adjust the point of group mode buffer because
5550     ;; point was moved to the next unread newsgroup by exiting.
5551     (gnus-summary-jump-to-group group)
5552     (when rescan
5553       (save-excursion
5554         (save-window-excursion
5555           ;; Don't show group contents.
5556           (set-window-start (selected-window) (point-max))
5557           (gnus-group-get-new-news-this-group 1))))
5558     (gnus-group-read-group all t)
5559     (gnus-summary-goto-subject current-subject nil t)))
5560
5561 (defun gnus-summary-rescan-group (&optional all)
5562   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5563   (interactive "P")
5564   (gnus-summary-reselect-current-group all t))
5565
5566 (defun gnus-summary-update-info (&optional non-destructive)
5567   (save-excursion
5568     (let ((group gnus-newsgroup-name))
5569       (when group
5570         (when gnus-newsgroup-kill-headers
5571           (setq gnus-newsgroup-killed
5572                 (gnus-compress-sequence
5573                  (nconc
5574                   (gnus-set-sorted-intersection
5575                    (gnus-uncompress-range gnus-newsgroup-killed)
5576                    (setq gnus-newsgroup-unselected
5577                          (sort gnus-newsgroup-unselected '<)))
5578                   (setq gnus-newsgroup-unreads
5579                         (sort gnus-newsgroup-unreads '<)))
5580                  t)))
5581         (unless (listp (cdr gnus-newsgroup-killed))
5582           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5583         (let ((headers gnus-newsgroup-headers))
5584           ;; Set the new ranges of read articles.
5585           (save-excursion
5586             (set-buffer gnus-group-buffer)
5587             (gnus-undo-force-boundary))
5588           (gnus-update-read-articles
5589            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5590           ;; Set the current article marks.
5591           (let ((gnus-newsgroup-scored
5592                  (if (and (not gnus-save-score)
5593                           (not non-destructive))
5594                      nil
5595                    gnus-newsgroup-scored)))
5596             (save-excursion
5597               (gnus-update-marks)))
5598           ;; Do the cross-ref thing.
5599           (when gnus-use-cross-reference
5600             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5601           ;; Do not switch windows but change the buffer to work.
5602           (set-buffer gnus-group-buffer)
5603           (unless (gnus-ephemeral-group-p group)
5604             (gnus-group-update-group group)))))))
5605
5606 (defun gnus-summary-save-newsrc (&optional force)
5607   "Save the current number of read/marked articles in the dribble buffer.
5608 The dribble buffer will then be saved.
5609 If FORCE (the prefix), also save the .newsrc file(s)."
5610   (interactive "P")
5611   (gnus-summary-update-info t)
5612   (if force
5613       (gnus-save-newsrc-file)
5614     (gnus-dribble-save)))
5615
5616 (defun gnus-summary-exit (&optional temporary)
5617   "Exit reading current newsgroup, and then return to group selection mode.
5618 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5619   (interactive)
5620   (gnus-set-global-variables)
5621   (gnus-kill-save-kill-buffer)
5622   (gnus-async-halt-prefetch)
5623   (let* ((group gnus-newsgroup-name)
5624          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5625          (mode major-mode)
5626          (group-point nil)
5627          (buf (current-buffer)))
5628     (unless quit-config
5629       ;; Do adaptive scoring, and possibly save score files.
5630       (when gnus-newsgroup-adaptive
5631         (gnus-score-adaptive))
5632       (when gnus-use-scoring
5633         (gnus-score-save)))
5634     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5635     ;; If we have several article buffers, we kill them at exit.
5636     (unless gnus-single-article-buffer
5637       (gnus-kill-buffer gnus-original-article-buffer)
5638       (setq gnus-article-current nil))
5639     (when gnus-use-cache
5640       (gnus-cache-possibly-remove-articles)
5641       (gnus-cache-save-buffers))
5642     (gnus-async-prefetch-remove-group group)
5643     (when gnus-suppress-duplicates
5644       (gnus-dup-enter-articles))
5645     (when gnus-use-trees
5646       (gnus-tree-close group))
5647     (when gnus-use-cache
5648       (gnus-cache-write-active))
5649     ;; Remove entries for this group.
5650     (nnmail-purge-split-history (gnus-group-real-name group))
5651     ;; Make all changes in this group permanent.
5652     (unless quit-config
5653       (gnus-run-hooks 'gnus-exit-group-hook)
5654       (gnus-summary-update-info))
5655     (gnus-close-group group)
5656     ;; Make sure where we were, and go to next newsgroup.
5657     (set-buffer gnus-group-buffer)
5658     (unless quit-config
5659       (gnus-group-jump-to-group group))
5660     (gnus-run-hooks 'gnus-summary-exit-hook)
5661     (unless (or quit-config
5662                 ;; If this group has disappeared from the summary
5663                 ;; buffer, don't skip forwards.
5664                 (not (string= group (gnus-group-group-name))))
5665       (gnus-group-next-unread-group 1))
5666     (setq group-point (point))
5667     (if temporary
5668         nil                             ;Nothing to do.
5669       ;; If we have several article buffers, we kill them at exit.
5670       (unless gnus-single-article-buffer
5671         (gnus-kill-buffer gnus-article-buffer)
5672         (gnus-kill-buffer gnus-original-article-buffer)
5673         (setq gnus-article-current nil))
5674       (set-buffer buf)
5675       (if (not gnus-kill-summary-on-exit)
5676           (gnus-deaden-summary)
5677         ;; We set all buffer-local variables to nil.  It is unclear why
5678         ;; this is needed, but if we don't, buffer-local variables are
5679         ;; not garbage-collected, it seems.  This would the lead to en
5680         ;; ever-growing Emacs.
5681         (gnus-summary-clear-local-variables)
5682         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5683           (gnus-summary-clear-local-variables))
5684         (when (get-buffer gnus-article-buffer)
5685           (bury-buffer gnus-article-buffer))
5686         ;; We clear the global counterparts of the buffer-local
5687         ;; variables as well, just to be on the safe side.
5688         (set-buffer gnus-group-buffer)
5689         (gnus-summary-clear-local-variables)
5690         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5691           (gnus-summary-clear-local-variables))
5692         ;; Return to group mode buffer.
5693         (when (eq mode 'gnus-summary-mode)
5694           (gnus-kill-buffer buf)))
5695       (setq gnus-current-select-method gnus-select-method)
5696       (pop-to-buffer gnus-group-buffer)
5697       (if (not quit-config)
5698           (progn
5699             (goto-char group-point)
5700             (gnus-configure-windows 'group 'force)
5701             (unless (pos-visible-in-window-p)
5702               (forward-line (/ (static-if (featurep 'xemacs)
5703                                    (window-displayed-height)
5704                                  (1- (window-height)))
5705                                -2))
5706               (set-window-start (selected-window) (point))
5707               (goto-char group-point)))
5708         (gnus-handle-ephemeral-exit quit-config))
5709       ;; Clear the current group name.
5710       (unless quit-config
5711         (setq gnus-newsgroup-name nil)))))
5712
5713 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5714 (defun gnus-summary-exit-no-update (&optional no-questions)
5715   "Quit reading current newsgroup without updating read article info."
5716   (interactive)
5717   (let* ((group gnus-newsgroup-name)
5718          (quit-config (gnus-group-quit-config group)))
5719     (when (or no-questions
5720               gnus-expert-user
5721               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5722       (gnus-async-halt-prefetch)
5723       (mapcar 'funcall
5724               (delq 'gnus-summary-expire-articles
5725                     (copy-sequence gnus-summary-prepare-exit-hook)))
5726       ;; If we have several article buffers, we kill them at exit.
5727       (unless gnus-single-article-buffer
5728         (gnus-kill-buffer gnus-article-buffer)
5729         (gnus-kill-buffer gnus-original-article-buffer)
5730         (setq gnus-article-current nil))
5731       (if (not gnus-kill-summary-on-exit)
5732           (gnus-deaden-summary)
5733         (gnus-close-group group)
5734         (gnus-summary-clear-local-variables)
5735         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5736           (gnus-summary-clear-local-variables))
5737         (set-buffer gnus-group-buffer)
5738         (gnus-summary-clear-local-variables)
5739         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5740           (gnus-summary-clear-local-variables))
5741         (when (get-buffer gnus-summary-buffer)
5742           (kill-buffer gnus-summary-buffer)))
5743       (unless gnus-single-article-buffer
5744         (setq gnus-article-current nil))
5745       (when gnus-use-trees
5746         (gnus-tree-close group))
5747       (gnus-async-prefetch-remove-group group)
5748       (when (get-buffer gnus-article-buffer)
5749         (bury-buffer gnus-article-buffer))
5750       ;; Return to the group buffer.
5751       (gnus-configure-windows 'group 'force)
5752       ;; Clear the current group name.
5753       (setq gnus-newsgroup-name nil)
5754       (when (equal (gnus-group-group-name) group)
5755         (gnus-group-next-unread-group 1))
5756       (when quit-config
5757         (gnus-handle-ephemeral-exit quit-config)))))
5758
5759 (defun gnus-handle-ephemeral-exit (quit-config)
5760   "Handle movement when leaving an ephemeral group.
5761 The state which existed when entering the ephemeral is reset."
5762   (if (not (buffer-name (car quit-config)))
5763       (gnus-configure-windows 'group 'force)
5764     (set-buffer (car quit-config))
5765     (cond ((eq major-mode 'gnus-summary-mode)
5766            (gnus-set-global-variables))
5767           ((eq major-mode 'gnus-article-mode)
5768            (save-excursion
5769              ;; The `gnus-summary-buffer' variable may point
5770              ;; to the old summary buffer when using a single
5771              ;; article buffer.
5772              (unless (gnus-buffer-live-p gnus-summary-buffer)
5773                (set-buffer gnus-group-buffer))
5774              (set-buffer gnus-summary-buffer)
5775              (gnus-set-global-variables))))
5776     (if (or (eq (cdr quit-config) 'article)
5777             (eq (cdr quit-config) 'pick))
5778         (progn
5779           ;; The current article may be from the ephemeral group
5780           ;; thus it is best that we reload this article
5781           (gnus-summary-show-article)
5782           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5783               (gnus-configure-windows 'pick 'force)
5784             (gnus-configure-windows (cdr quit-config) 'force)))
5785       (gnus-configure-windows (cdr quit-config) 'force))
5786     (when (eq major-mode 'gnus-summary-mode)
5787       (gnus-summary-next-subject 1 nil t)
5788       (gnus-summary-recenter)
5789       (gnus-summary-position-point))))
5790
5791 (defun gnus-summary-preview-mime-message ()
5792   "MIME decode and play this message."
5793   (interactive)
5794   (let ((gnus-break-pages nil)
5795         (gnus-show-mime t))
5796     (gnus-summary-select-article gnus-show-all-headers t))
5797   (select-window (get-buffer-window gnus-article-buffer)))
5798
5799 ;;; Dead summaries.
5800
5801 (defvar gnus-dead-summary-mode-map nil)
5802
5803 (unless gnus-dead-summary-mode-map
5804   (setq gnus-dead-summary-mode-map (make-keymap))
5805   (suppress-keymap gnus-dead-summary-mode-map)
5806   (substitute-key-definition
5807    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5808   (let ((keys '("\C-d" "\r" "\177" [delete])))
5809     (while keys
5810       (define-key gnus-dead-summary-mode-map
5811         (pop keys) 'gnus-summary-wake-up-the-dead))))
5812
5813 (defvar gnus-dead-summary-mode nil
5814   "Minor mode for Gnus summary buffers.")
5815
5816 (defun gnus-dead-summary-mode (&optional arg)
5817   "Minor mode for Gnus summary buffers."
5818   (interactive "P")
5819   (when (eq major-mode 'gnus-summary-mode)
5820     (make-local-variable 'gnus-dead-summary-mode)
5821     (setq gnus-dead-summary-mode
5822           (if (null arg) (not gnus-dead-summary-mode)
5823             (> (prefix-numeric-value arg) 0)))
5824     (when gnus-dead-summary-mode
5825       (gnus-add-minor-mode
5826        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5827
5828 (defun gnus-deaden-summary ()
5829   "Make the current summary buffer into a dead summary buffer."
5830   ;; Kill any previous dead summary buffer.
5831   (when (and gnus-dead-summary
5832              (buffer-name gnus-dead-summary))
5833     (save-excursion
5834       (set-buffer gnus-dead-summary)
5835       (when gnus-dead-summary-mode
5836         (kill-buffer (current-buffer)))))
5837   ;; Make this the current dead summary.
5838   (setq gnus-dead-summary (current-buffer))
5839   (gnus-dead-summary-mode 1)
5840   (let ((name (buffer-name)))
5841     (when (string-match "Summary" name)
5842       (rename-buffer
5843        (concat (substring name 0 (match-beginning 0)) "Dead "
5844                (substring name (match-beginning 0)))
5845        t)
5846       (bury-buffer))))
5847
5848 (defun gnus-kill-or-deaden-summary (buffer)
5849   "Kill or deaden the summary BUFFER."
5850   (save-excursion
5851     (when (and (buffer-name buffer)
5852                (not gnus-single-article-buffer))
5853       (save-excursion
5854         (set-buffer buffer)
5855         (gnus-kill-buffer gnus-article-buffer)
5856         (gnus-kill-buffer gnus-original-article-buffer)))
5857     (cond (gnus-kill-summary-on-exit
5858            (when (and gnus-use-trees
5859                       (gnus-buffer-exists-p buffer))
5860              (save-excursion
5861                (set-buffer buffer)
5862                (gnus-tree-close gnus-newsgroup-name)))
5863            (gnus-kill-buffer buffer))
5864           ((gnus-buffer-exists-p buffer)
5865            (save-excursion
5866              (set-buffer buffer)
5867              (gnus-deaden-summary))))))
5868
5869 (defun gnus-summary-wake-up-the-dead (&rest args)
5870   "Wake up the dead summary buffer."
5871   (interactive)
5872   (gnus-dead-summary-mode -1)
5873   (let ((name (buffer-name)))
5874     (when (string-match "Dead " name)
5875       (rename-buffer
5876        (concat (substring name 0 (match-beginning 0))
5877                (substring name (match-end 0)))
5878        t)))
5879   (gnus-message 3 "This dead summary is now alive again"))
5880
5881 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5882 (defun gnus-summary-fetch-faq (&optional faq-dir)
5883   "Fetch the FAQ for the current group.
5884 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5885 in."
5886   (interactive
5887    (list
5888     (when current-prefix-arg
5889       (completing-read
5890        "Faq dir: " (and (listp gnus-group-faq-directory)
5891                         (mapcar (lambda (file) (list file))
5892                                 gnus-group-faq-directory))))))
5893   (let (gnus-faq-buffer)
5894     (when (setq gnus-faq-buffer
5895                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5896       (gnus-configure-windows 'summary-faq))))
5897
5898 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5899 (defun gnus-summary-describe-group (&optional force)
5900   "Describe the current newsgroup."
5901   (interactive "P")
5902   (gnus-group-describe-group force gnus-newsgroup-name))
5903
5904 (defun gnus-summary-describe-briefly ()
5905   "Describe summary mode commands briefly."
5906   (interactive)
5907   (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")))
5908
5909 ;; Walking around group mode buffer from summary mode.
5910
5911 (defun gnus-summary-next-group (&optional no-article target-group backward)
5912   "Exit current newsgroup and then select next unread newsgroup.
5913 If prefix argument NO-ARTICLE is non-nil, no article is selected
5914 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5915 previous group instead."
5916   (interactive "P")
5917   ;; Stop pre-fetching.
5918   (gnus-async-halt-prefetch)
5919   (let ((current-group gnus-newsgroup-name)
5920         (current-buffer (current-buffer))
5921         entered)
5922     ;; First we semi-exit this group to update Xrefs and all variables.
5923     ;; We can't do a real exit, because the window conf must remain
5924     ;; the same in case the user is prompted for info, and we don't
5925     ;; want the window conf to change before that...
5926     (gnus-summary-exit t)
5927     (while (not entered)
5928       ;; Then we find what group we are supposed to enter.
5929       (set-buffer gnus-group-buffer)
5930       (gnus-group-jump-to-group current-group)
5931       (setq target-group
5932             (or target-group
5933                 (if (eq gnus-keep-same-level 'best)
5934                     (gnus-summary-best-group gnus-newsgroup-name)
5935                   (gnus-summary-search-group backward gnus-keep-same-level))))
5936       (if (not target-group)
5937           ;; There are no further groups, so we return to the group
5938           ;; buffer.
5939           (progn
5940             (gnus-message 5 "Returning to the group buffer")
5941             (setq entered t)
5942             (when (gnus-buffer-live-p current-buffer)
5943               (set-buffer current-buffer)
5944               (gnus-summary-exit))
5945             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5946         ;; We try to enter the target group.
5947         (gnus-group-jump-to-group target-group)
5948         (let ((unreads (gnus-group-group-unread)))
5949           (if (and (or (eq t unreads)
5950                        (and unreads (not (zerop unreads))))
5951                    (gnus-summary-read-group
5952                     target-group nil no-article
5953                     (and (buffer-name current-buffer) current-buffer)
5954                     nil backward))
5955               (setq entered t)
5956             (setq current-group target-group
5957                   target-group nil)))))))
5958
5959 (defun gnus-summary-prev-group (&optional no-article)
5960   "Exit current newsgroup and then select previous unread newsgroup.
5961 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5962   (interactive "P")
5963   (gnus-summary-next-group no-article nil t))
5964
5965 ;; Walking around summary lines.
5966
5967 (defun gnus-summary-first-subject (&optional unread undownloaded)
5968   "Go to the first unread subject.
5969 If UNREAD is non-nil, go to the first unread article.
5970 Returns the article selected or nil if there are no unread articles."
5971   (interactive "P")
5972   (prog1
5973       (cond
5974        ;; Empty summary.
5975        ((null gnus-newsgroup-data)
5976         (gnus-message 3 "No articles in the group")
5977         nil)
5978        ;; Pick the first article.
5979        ((not unread)
5980         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5981         (gnus-data-number (car gnus-newsgroup-data)))
5982        ;; No unread articles.
5983        ((null gnus-newsgroup-unreads)
5984         (gnus-message 3 "No more unread articles")
5985         nil)
5986        ;; Find the first unread article.
5987        (t
5988         (let ((data gnus-newsgroup-data))
5989           (while (and data
5990                       (and (not (and undownloaded
5991                                      (eq gnus-undownloaded-mark
5992                                          (gnus-data-mark (car data)))))
5993                            (not (gnus-data-unread-p (car data)))))
5994             (setq data (cdr data)))
5995           (when data
5996             (goto-char (gnus-data-pos (car data)))
5997             (gnus-data-number (car data))))))
5998     (gnus-summary-position-point)))
5999
6000 (defun gnus-summary-next-subject (n &optional unread dont-display)
6001   "Go to next N'th summary line.
6002 If N is negative, go to the previous N'th subject line.
6003 If UNREAD is non-nil, only unread articles are selected.
6004 The difference between N and the actual number of steps taken is
6005 returned."
6006   (interactive "p")
6007   (let ((backward (< n 0))
6008         (n (abs n)))
6009     (while (and (> n 0)
6010                 (if backward
6011                     (gnus-summary-find-prev unread)
6012                   (gnus-summary-find-next unread)))
6013       (unless (zerop (setq n (1- n)))
6014         (gnus-summary-show-thread)))
6015     (when (/= 0 n)
6016       (gnus-message 7 "No more%s articles"
6017                     (if unread " unread" "")))
6018     (unless dont-display
6019       (gnus-summary-recenter)
6020       (gnus-summary-position-point))
6021     n))
6022
6023 (defun gnus-summary-next-unread-subject (n)
6024   "Go to next N'th unread summary line."
6025   (interactive "p")
6026   (gnus-summary-next-subject n t))
6027
6028 (defun gnus-summary-prev-subject (n &optional unread)
6029   "Go to previous N'th summary line.
6030 If optional argument UNREAD is non-nil, only unread article is selected."
6031   (interactive "p")
6032   (gnus-summary-next-subject (- n) unread))
6033
6034 (defun gnus-summary-prev-unread-subject (n)
6035   "Go to previous N'th unread summary line."
6036   (interactive "p")
6037   (gnus-summary-next-subject (- n) t))
6038
6039 (defun gnus-summary-goto-subject (article &optional force silent)
6040   "Go the subject line of ARTICLE.
6041 If FORCE, also allow jumping to articles not currently shown."
6042   (interactive "nArticle number: ")
6043   (let ((b (point))
6044         (data (gnus-data-find article)))
6045     ;; We read in the article if we have to.
6046     (and (not data)
6047          force
6048          (gnus-summary-insert-subject
6049           article
6050           (if (or (numberp force) (vectorp force)) force)
6051           t)
6052          (setq data (gnus-data-find article)))
6053     (goto-char b)
6054     (if (not data)
6055         (progn
6056           (unless silent
6057             (gnus-message 3 "Can't find article %d" article))
6058           nil)
6059       (goto-char (gnus-data-pos data))
6060       (gnus-summary-position-point)
6061       article)))
6062
6063 ;; Walking around summary lines with displaying articles.
6064
6065 (defun gnus-summary-expand-window (&optional arg)
6066   "Make the summary buffer take up the entire Emacs frame.
6067 Given a prefix, will force an `article' buffer configuration."
6068   (interactive "P")
6069   (if arg
6070       (gnus-configure-windows 'article 'force)
6071     (gnus-configure-windows 'summary 'force)))
6072
6073 (defun gnus-summary-display-article (article &optional all-header)
6074   "Display ARTICLE in article buffer."
6075   (when (gnus-buffer-live-p gnus-article-buffer)
6076     (with-current-buffer gnus-article-buffer
6077       (set-buffer-multibyte t)))
6078   (gnus-set-global-variables)
6079   (when (gnus-buffer-live-p gnus-article-buffer)
6080     (with-current-buffer gnus-article-buffer
6081       (setq gnus-article-charset gnus-newsgroup-charset)
6082       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)))
6083   (if (null article)
6084       nil
6085     (prog1
6086         (if gnus-summary-display-article-function
6087             (funcall gnus-summary-display-article-function article all-header)
6088           (gnus-article-prepare article all-header))
6089       (with-current-buffer gnus-article-buffer
6090         (set (make-local-variable 'gnus-summary-search-article-matched-data)
6091              nil))
6092       (gnus-run-hooks 'gnus-select-article-hook)
6093       (when (and gnus-current-article
6094                  (not (zerop gnus-current-article)))
6095         (gnus-summary-goto-subject gnus-current-article))
6096       (gnus-summary-recenter)
6097       (when (and gnus-use-trees gnus-show-threads)
6098         (gnus-possibly-generate-tree article)
6099         (gnus-highlight-selected-tree article))
6100       ;; Successfully display article.
6101       (gnus-article-set-window-start
6102        (cdr (assq article gnus-newsgroup-bookmarks))))))
6103
6104 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6105   "Select the current article.
6106 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6107 non-nil, the article will be re-fetched even if it already present in
6108 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6109 be displayed."
6110   ;; Make sure we are in the summary buffer to work around bbdb bug.
6111   (unless (eq major-mode 'gnus-summary-mode)
6112     (set-buffer gnus-summary-buffer))
6113   (let ((article (or article (gnus-summary-article-number)))
6114         (all-headers (not (not all-headers))) ;Must be T or NIL.
6115         gnus-summary-display-article-function)
6116     (and (not pseudo)
6117          (gnus-summary-article-pseudo-p article)
6118          (error "This is a pseudo-article"))
6119     (save-excursion
6120       (set-buffer gnus-summary-buffer)
6121       (if (or (and gnus-single-article-buffer
6122                    (or (null gnus-current-article)
6123                        (null gnus-article-current)
6124                        (null (get-buffer gnus-article-buffer))
6125                        (not (eq article (cdr gnus-article-current)))
6126                        (not (equal (car gnus-article-current)
6127                                    gnus-newsgroup-name))))
6128               (and (not gnus-single-article-buffer)
6129                    (or (null gnus-current-article)
6130                        (not (eq gnus-current-article article))))
6131               force)
6132           ;; The requested article is different from the current article.
6133           (progn
6134             (gnus-summary-display-article article all-headers)
6135             (when (or all-headers gnus-show-all-headers)
6136               (gnus-article-show-all-headers))
6137             (gnus-article-set-window-start
6138              (cdr (assq article gnus-newsgroup-bookmarks)))
6139             article)
6140         (when (or all-headers gnus-show-all-headers)
6141           (gnus-article-show-all-headers))
6142         'old))))
6143
6144 (defun gnus-summary-force-verify-and-decrypt ()
6145   (interactive)
6146   (let ((mm-verify-option 'known)
6147         (mm-decrypt-option 'known))
6148     (gnus-summary-select-article nil 'force)))
6149
6150 (defun gnus-summary-set-current-mark (&optional current-mark)
6151   "Obsolete function."
6152   nil)
6153
6154 (defun gnus-summary-next-article (&optional unread subject backward push)
6155   "Select the next article.
6156 If UNREAD, only unread articles are selected.
6157 If SUBJECT, only articles with SUBJECT are selected.
6158 If BACKWARD, the previous article is selected instead of the next."
6159   (interactive "P")
6160   (cond
6161    ;; Is there such an article?
6162    ((and (gnus-summary-search-forward unread subject backward)
6163          (or (gnus-summary-display-article (gnus-summary-article-number))
6164              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6165     (gnus-summary-position-point))
6166    ;; If not, we try the first unread, if that is wanted.
6167    ((and subject
6168          gnus-auto-select-same
6169          (gnus-summary-first-unread-article))
6170     (gnus-summary-position-point)
6171     (gnus-message 6 "Wrapped"))
6172    ;; Try to get next/previous article not displayed in this group.
6173    ((and gnus-auto-extend-newsgroup
6174          (not unread) (not subject))
6175     (gnus-summary-goto-article
6176      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6177      nil (count-lines (point-min) (point))))
6178    ;; Go to next/previous group.
6179    (t
6180     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6181       (gnus-summary-jump-to-group gnus-newsgroup-name))
6182     (let ((cmd last-command-char)
6183           (point
6184            (save-excursion
6185              (set-buffer gnus-group-buffer)
6186              (point)))
6187           (group
6188            (if (eq gnus-keep-same-level 'best)
6189                (gnus-summary-best-group gnus-newsgroup-name)
6190              (gnus-summary-search-group backward gnus-keep-same-level))))
6191       ;; For some reason, the group window gets selected.  We change
6192       ;; it back.
6193       (select-window (get-buffer-window (current-buffer)))
6194       ;; Select next unread newsgroup automagically.
6195       (cond
6196        ((or (not gnus-auto-select-next)
6197             (not cmd))
6198         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6199        ((or (eq gnus-auto-select-next 'quietly)
6200             (and (eq gnus-auto-select-next 'slightly-quietly)
6201                  push)
6202             (and (eq gnus-auto-select-next 'almost-quietly)
6203                  (gnus-summary-last-article-p)))
6204         ;; Select quietly.
6205         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6206             (gnus-summary-exit)
6207           (gnus-message 7 "No more%s articles (%s)..."
6208                         (if unread " unread" "")
6209                         (if group (concat "selecting " group)
6210                           "exiting"))
6211           (gnus-summary-next-group nil group backward)))
6212        (t
6213         (when (gnus-key-press-event-p last-input-event)
6214           (gnus-summary-walk-group-buffer
6215            gnus-newsgroup-name cmd unread backward point))))))))
6216
6217 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6218   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6219                       (?\C-p (gnus-group-prev-unread-group 1))))
6220         (cursor-in-echo-area t)
6221         keve key group ended)
6222     (save-excursion
6223       (set-buffer gnus-group-buffer)
6224       (goto-char start)
6225       (setq group
6226             (if (eq gnus-keep-same-level 'best)
6227                 (gnus-summary-best-group gnus-newsgroup-name)
6228               (gnus-summary-search-group backward gnus-keep-same-level))))
6229     (while (not ended)
6230       (gnus-message
6231        5 "No more%s articles%s" (if unread " unread" "")
6232        (if (and group
6233                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6234            (format " (Type %s for %s [%s])"
6235                    (single-key-description cmd) group
6236                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6237          (format " (Type %s to exit %s)"
6238                  (single-key-description cmd)
6239                  gnus-newsgroup-name)))
6240       ;; Confirm auto selection.
6241       (setq key (car (setq keve (gnus-read-event-char))))
6242       (setq ended t)
6243       (cond
6244        ((assq key keystrokes)
6245         (let ((obuf (current-buffer)))
6246           (switch-to-buffer gnus-group-buffer)
6247           (when group
6248             (gnus-group-jump-to-group group))
6249           (eval (cadr (assq key keystrokes)))
6250           (setq group (gnus-group-group-name))
6251           (switch-to-buffer obuf))
6252         (setq ended nil))
6253        ((equal key cmd)
6254         (if (or (not group)
6255                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6256             (gnus-summary-exit)
6257           (gnus-summary-next-group nil group backward)))
6258        (t
6259         (push (cdr keve) unread-command-events))))))
6260
6261 (defun gnus-summary-next-unread-article ()
6262   "Select unread article after current one."
6263   (interactive)
6264   (gnus-summary-next-article
6265    (or (not (eq gnus-summary-goto-unread 'never))
6266        (gnus-summary-last-article-p (gnus-summary-article-number)))
6267    (and gnus-auto-select-same
6268         (gnus-summary-article-subject))))
6269
6270 (defun gnus-summary-prev-article (&optional unread subject)
6271   "Select the article after the current one.
6272 If UNREAD is non-nil, only unread articles are selected."
6273   (interactive "P")
6274   (gnus-summary-next-article unread subject t))
6275
6276 (defun gnus-summary-prev-unread-article ()
6277   "Select unread article before current one."
6278   (interactive)
6279   (gnus-summary-prev-article
6280    (or (not (eq gnus-summary-goto-unread 'never))
6281        (gnus-summary-first-article-p (gnus-summary-article-number)))
6282    (and gnus-auto-select-same
6283         (gnus-summary-article-subject))))
6284
6285 (defun gnus-summary-next-page (&optional lines circular)
6286   "Show next page of the selected article.
6287 If at the end of the current article, select the next article.
6288 LINES says how many lines should be scrolled up.
6289
6290 If CIRCULAR is non-nil, go to the start of the article instead of
6291 selecting the next article when reaching the end of the current
6292 article."
6293   (interactive "P")
6294   (setq gnus-summary-buffer (current-buffer))
6295   (gnus-set-global-variables)
6296   (let ((article (gnus-summary-article-number))
6297         (article-window (get-buffer-window gnus-article-buffer t))
6298         endp)
6299     ;; If the buffer is empty, we have no article.
6300     (unless article
6301       (error "No article to select"))
6302     (gnus-configure-windows 'article)
6303     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6304         (if (and (eq gnus-summary-goto-unread 'never)
6305                  (not (gnus-summary-last-article-p article)))
6306             (gnus-summary-next-article)
6307           (gnus-summary-next-unread-article))
6308       (if (or (null gnus-current-article)
6309               (null gnus-article-current)
6310               (/= article (cdr gnus-article-current))
6311               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6312           ;; Selected subject is different from current article's.
6313           (gnus-summary-display-article article)
6314         (when article-window
6315           (gnus-eval-in-buffer-window gnus-article-buffer
6316             (setq endp (gnus-article-next-page lines)))
6317           (when endp
6318             (cond (circular
6319                    (gnus-summary-beginning-of-article))
6320                   (lines
6321                    (gnus-message 3 "End of message"))
6322                   ((null lines)
6323                    (if (and (eq gnus-summary-goto-unread 'never)
6324                             (not (gnus-summary-last-article-p article)))
6325                        (gnus-summary-next-article)
6326                      (gnus-summary-next-unread-article))))))))
6327     (gnus-summary-recenter)
6328     (gnus-summary-position-point)))
6329
6330 (defun gnus-summary-prev-page (&optional lines move)
6331   "Show previous page of selected article.
6332 Argument LINES specifies lines to be scrolled down.
6333 If MOVE, move to the previous unread article if point is at
6334 the beginning of the buffer."
6335   (interactive "P")
6336   (let ((article (gnus-summary-article-number))
6337         (article-window (get-buffer-window gnus-article-buffer t))
6338         endp)
6339     (gnus-configure-windows 'article)
6340     (if (or (null gnus-current-article)
6341             (null gnus-article-current)
6342             (/= article (cdr gnus-article-current))
6343             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6344         ;; Selected subject is different from current article's.
6345         (gnus-summary-display-article article)
6346       (gnus-summary-recenter)
6347       (when article-window
6348         (gnus-eval-in-buffer-window gnus-article-buffer
6349           (setq endp (gnus-article-prev-page lines)))
6350         (when (and move endp)
6351           (cond (lines
6352                  (gnus-message 3 "Beginning of message"))
6353                 ((null lines)
6354                  (if (and (eq gnus-summary-goto-unread 'never)
6355                           (not (gnus-summary-first-article-p article)))
6356                      (gnus-summary-prev-article)
6357                    (gnus-summary-prev-unread-article))))))))
6358   (gnus-summary-position-point))
6359
6360 (defun gnus-summary-prev-page-or-article (&optional lines)
6361   "Show previous page of selected article.
6362 Argument LINES specifies lines to be scrolled down.
6363 If at the beginning of the article, go to the next article."
6364   (interactive "P")
6365   (gnus-summary-prev-page lines t))
6366
6367 (defun gnus-summary-scroll-up (lines)
6368   "Scroll up (or down) one line current article.
6369 Argument LINES specifies lines to be scrolled up (or down if negative)."
6370   (interactive "p")
6371   (gnus-configure-windows 'article)
6372   (gnus-summary-show-thread)
6373   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6374     (gnus-eval-in-buffer-window gnus-article-buffer
6375       (cond ((> lines 0)
6376              (when (gnus-article-next-page lines)
6377                (gnus-message 3 "End of message")))
6378             ((< lines 0)
6379              (gnus-article-prev-page (- lines))))))
6380   (gnus-summary-recenter)
6381   (gnus-summary-position-point))
6382
6383 (defun gnus-summary-scroll-down (lines)
6384   "Scroll down (or up) one line current article.
6385 Argument LINES specifies lines to be scrolled down (or up if negative)."
6386   (interactive "p")
6387   (gnus-summary-scroll-up (- lines)))
6388
6389 (defun gnus-summary-next-same-subject ()
6390   "Select next article which has the same subject as current one."
6391   (interactive)
6392   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6393
6394 (defun gnus-summary-prev-same-subject ()
6395   "Select previous article which has the same subject as current one."
6396   (interactive)
6397   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6398
6399 (defun gnus-summary-next-unread-same-subject ()
6400   "Select next unread article which has the same subject as current one."
6401   (interactive)
6402   (gnus-summary-next-article t (gnus-summary-article-subject)))
6403
6404 (defun gnus-summary-prev-unread-same-subject ()
6405   "Select previous unread article which has the same subject as current one."
6406   (interactive)
6407   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6408
6409 (defun gnus-summary-first-unread-article ()
6410   "Select the first unread article.
6411 Return nil if there are no unread articles."
6412   (interactive)
6413   (prog1
6414       (when (gnus-summary-first-subject t)
6415         (gnus-summary-show-thread)
6416         (gnus-summary-first-subject t)
6417         (gnus-summary-display-article (gnus-summary-article-number)))
6418     (gnus-summary-position-point)))
6419
6420 (defun gnus-summary-first-unread-subject ()
6421   "Place the point on the subject line of the first unread article.
6422 Return nil if there are no unread articles."
6423   (interactive)
6424   (prog1
6425       (when (gnus-summary-first-subject t)
6426         (gnus-summary-show-thread)
6427         (gnus-summary-first-subject t))
6428     (gnus-summary-position-point)))
6429
6430 (defun gnus-summary-first-article ()
6431   "Select the first article.
6432 Return nil if there are no articles."
6433   (interactive)
6434   (prog1
6435       (when (gnus-summary-first-subject)
6436         (gnus-summary-show-thread)
6437         (gnus-summary-first-subject)
6438         (gnus-summary-display-article (gnus-summary-article-number)))
6439     (gnus-summary-position-point)))
6440
6441 (defun gnus-summary-best-unread-article ()
6442   "Select the unread article with the highest score."
6443   (interactive)
6444   (let ((best -1000000)
6445         (data gnus-newsgroup-data)
6446         article score)
6447     (while data
6448       (and (gnus-data-unread-p (car data))
6449            (> (setq score
6450                     (gnus-summary-article-score (gnus-data-number (car data))))
6451               best)
6452            (setq best score
6453                  article (gnus-data-number (car data))))
6454       (setq data (cdr data)))
6455     (prog1
6456         (if article
6457             (gnus-summary-goto-article article)
6458           (error "No unread articles"))
6459       (gnus-summary-position-point))))
6460
6461 (defun gnus-summary-last-subject ()
6462   "Go to the last displayed subject line in the group."
6463   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6464     (when article
6465       (gnus-summary-goto-subject article))))
6466
6467 (defun gnus-summary-goto-article (article &optional all-headers force)
6468   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6469 If ALL-HEADERS is non-nil, no header lines are hidden.
6470 If FORCE, go to the article even if it isn't displayed.  If FORCE
6471 is a number, it is the line the article is to be displayed on."
6472   (interactive
6473    (list
6474     (completing-read
6475      "Article number or Message-ID: "
6476      (mapcar (lambda (number) (list (int-to-string number)))
6477              gnus-newsgroup-limit))
6478     current-prefix-arg
6479     t))
6480   (prog1
6481       (if (and (stringp article)
6482                (string-match "@" article))
6483           (gnus-summary-refer-article article)
6484         (when (stringp article)
6485           (setq article (string-to-number article)))
6486         (if (gnus-summary-goto-subject article force)
6487             (gnus-summary-display-article article all-headers)
6488           (gnus-message 4 "Couldn't go to article %s" article) nil))
6489     (gnus-summary-position-point)))
6490
6491 (defun gnus-summary-goto-last-article ()
6492   "Go to the previously read article."
6493   (interactive)
6494   (prog1
6495       (when gnus-last-article
6496         (gnus-summary-goto-article gnus-last-article nil t))
6497     (gnus-summary-position-point)))
6498
6499 (defun gnus-summary-pop-article (number)
6500   "Pop one article off the history and go to the previous.
6501 NUMBER articles will be popped off."
6502   (interactive "p")
6503   (let (to)
6504     (setq gnus-newsgroup-history
6505           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6506     (if to
6507         (gnus-summary-goto-article (car to) nil t)
6508       (error "Article history empty")))
6509   (gnus-summary-position-point))
6510
6511 ;; Summary commands and functions for limiting the summary buffer.
6512
6513 (defun gnus-summary-limit-to-articles (n)
6514   "Limit the summary buffer to the next N articles.
6515 If not given a prefix, use the process marked articles instead."
6516   (interactive "P")
6517   (prog1
6518       (let ((articles (gnus-summary-work-articles n)))
6519         (setq gnus-newsgroup-processable nil)
6520         (gnus-summary-limit articles))
6521     (gnus-summary-position-point)))
6522
6523 (defun gnus-summary-pop-limit (&optional total)
6524   "Restore the previous limit.
6525 If given a prefix, remove all limits."
6526   (interactive "P")
6527   (when total
6528     (setq gnus-newsgroup-limits
6529           (list (mapcar (lambda (h) (mail-header-number h))
6530                         gnus-newsgroup-headers))))
6531   (unless gnus-newsgroup-limits
6532     (error "No limit to pop"))
6533   (prog1
6534       (gnus-summary-limit nil 'pop)
6535     (gnus-summary-position-point)))
6536
6537 (defun gnus-summary-limit-to-subject (subject &optional header)
6538   "Limit the summary buffer to articles that have subjects that match a regexp."
6539   (interactive "sLimit to subject (regexp): ")
6540   (unless header
6541     (setq header "subject"))
6542   (when (not (equal "" subject))
6543     (prog1
6544         (let ((articles (gnus-summary-find-matching
6545                          (or header "subject") subject 'all)))
6546           (unless articles
6547             (error "Found no matches for \"%s\"" subject))
6548           (gnus-summary-limit articles))
6549       (gnus-summary-position-point))))
6550
6551 (defun gnus-summary-limit-to-author (from)
6552   "Limit the summary buffer to articles that have authors that match a regexp."
6553   (interactive "sLimit to author (regexp): ")
6554   (gnus-summary-limit-to-subject from "from"))
6555
6556 (defun gnus-summary-limit-to-age (age &optional younger-p)
6557   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6558 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6559 articles that are younger than AGE days."
6560   (interactive
6561    (let ((younger current-prefix-arg)
6562          (days-got nil)
6563          days)
6564      (while (not days-got)
6565        (setq days (if younger
6566                       (read-string "Limit to articles within (in days): ")
6567                     (read-string "Limit to articles older than (in days): ")))
6568        (when (> (length days) 0)
6569          (setq days (read days)))
6570        (if (numberp days)
6571            (setq days-got t)
6572          (message "Please enter a number.")
6573          (sleep-for 1)))
6574      (list days younger)))
6575   (prog1
6576       (let ((data gnus-newsgroup-data)
6577             (cutoff (days-to-time age))
6578             articles d date is-younger)
6579         (while (setq d (pop data))
6580           (when (and (vectorp (gnus-data-header d))
6581                      (setq date (mail-header-date (gnus-data-header d))))
6582             (setq is-younger (time-less-p
6583                               (time-since (condition-case ()
6584                                               (date-to-time date)
6585                                             (error '(0 0))))
6586                               cutoff))
6587             (when (if younger-p
6588                       is-younger
6589                     (not is-younger))
6590               (push (gnus-data-number d) articles))))
6591         (gnus-summary-limit (nreverse articles)))
6592     (gnus-summary-position-point)))
6593
6594 (defun gnus-summary-limit-to-extra (header regexp)
6595   "Limit the summary buffer to articles that match an 'extra' header."
6596   (interactive
6597    (let ((header
6598           (intern
6599            (gnus-completing-read
6600             (symbol-name (car gnus-extra-headers))
6601             "Limit extra header:"
6602             (mapcar (lambda (x)
6603                       (cons (symbol-name x) x))
6604                     gnus-extra-headers)
6605             nil
6606             t))))
6607      (list header
6608            (read-string (format "Limit to header %s (regexp): " header)))))
6609   (when (not (equal "" regexp))
6610     (prog1
6611         (let ((articles (gnus-summary-find-matching
6612                          (cons 'extra header) regexp 'all)))
6613           (unless articles
6614             (error "Found no matches for \"%s\"" regexp))
6615           (gnus-summary-limit articles))
6616       (gnus-summary-position-point))))
6617
6618 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6619 (make-obsolete
6620  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6621
6622 (defun gnus-summary-limit-to-unread (&optional all)
6623   "Limit the summary buffer to articles that are not marked as read.
6624 If ALL is non-nil, limit strictly to unread articles."
6625   (interactive "P")
6626   (if all
6627       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6628     (gnus-summary-limit-to-marks
6629      ;; Concat all the marks that say that an article is read and have
6630      ;; those removed.
6631      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6632            gnus-killed-mark gnus-kill-file-mark
6633            gnus-low-score-mark gnus-expirable-mark
6634            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6635            gnus-duplicate-mark gnus-souped-mark)
6636      'reverse)))
6637
6638 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6639 (make-obsolete 'gnus-summary-delete-marked-with
6640                'gnus-summary-limit-exlude-marks)
6641
6642 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6643   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6644 If REVERSE, limit the summary buffer to articles that are marked
6645 with MARKS.  MARKS can either be a string of marks or a list of marks.
6646 Returns how many articles were removed."
6647   (interactive "sMarks: ")
6648   (gnus-summary-limit-to-marks marks t))
6649
6650 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6651   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6652 If REVERSE (the prefix), limit the summary buffer to articles that are
6653 not marked with MARKS.  MARKS can either be a string of marks or a
6654 list of marks.
6655 Returns how many articles were removed."
6656   (interactive "sMarks: \nP")
6657   (prog1
6658       (let ((data gnus-newsgroup-data)
6659             (marks (if (listp marks) marks
6660                      (append marks nil))) ; Transform to list.
6661             articles)
6662         (while data
6663           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6664                   (memq (gnus-data-mark (car data)) marks))
6665             (push (gnus-data-number (car data)) articles))
6666           (setq data (cdr data)))
6667         (gnus-summary-limit articles))
6668     (gnus-summary-position-point)))
6669
6670 (defun gnus-summary-limit-to-score (&optional score)
6671   "Limit to articles with score at or above SCORE."
6672   (interactive "P")
6673   (setq score (if score
6674                   (prefix-numeric-value score)
6675                 (or gnus-summary-default-score 0)))
6676   (let ((data gnus-newsgroup-data)
6677         articles)
6678     (while data
6679       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6680                 score)
6681         (push (gnus-data-number (car data)) articles))
6682       (setq data (cdr data)))
6683     (prog1
6684         (gnus-summary-limit articles)
6685       (gnus-summary-position-point))))
6686
6687 (defun gnus-summary-limit-include-thread (id)
6688   "Display all the hidden articles that in the current thread."
6689   (interactive (list (mail-header-id (gnus-summary-article-header))))
6690   (let ((articles (gnus-articles-in-thread
6691                    (gnus-id-to-thread (gnus-root-id id)))))
6692     (prog1
6693         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6694       (gnus-summary-position-point))))
6695
6696 (defun gnus-summary-limit-include-dormant ()
6697   "Display all the hidden articles that are marked as dormant.
6698 Note that this command only works on a subset of the articles currently
6699 fetched for this group."
6700   (interactive)
6701   (unless gnus-newsgroup-dormant
6702     (error "There are no dormant articles in this group"))
6703   (prog1
6704       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6705     (gnus-summary-position-point)))
6706
6707 (defun gnus-summary-limit-exclude-dormant ()
6708   "Hide all dormant articles."
6709   (interactive)
6710   (prog1
6711       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6712     (gnus-summary-position-point)))
6713
6714 (defun gnus-summary-limit-exclude-childless-dormant ()
6715   "Hide all dormant articles that have no children."
6716   (interactive)
6717   (let ((data (gnus-data-list t))
6718         articles d children)
6719     ;; Find all articles that are either not dormant or have
6720     ;; children.
6721     (while (setq d (pop data))
6722       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6723                 (and (setq children
6724                            (gnus-article-children (gnus-data-number d)))
6725                      (let (found)
6726                        (while children
6727                          (when (memq (car children) articles)
6728                            (setq children nil
6729                                  found t))
6730                          (pop children))
6731                        found)))
6732         (push (gnus-data-number d) articles)))
6733     ;; Do the limiting.
6734     (prog1
6735         (gnus-summary-limit articles)
6736       (gnus-summary-position-point))))
6737
6738 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6739   "Mark all unread excluded articles as read.
6740 If ALL, mark even excluded ticked and dormants as read."
6741   (interactive "P")
6742   (let ((articles (gnus-sorted-complement
6743                    (sort
6744                     (mapcar (lambda (h) (mail-header-number h))
6745                             gnus-newsgroup-headers)
6746                     '<)
6747                    (sort gnus-newsgroup-limit '<)))
6748         article)
6749     (setq gnus-newsgroup-unreads
6750           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6751     (if all
6752         (setq gnus-newsgroup-dormant nil
6753               gnus-newsgroup-marked nil
6754               gnus-newsgroup-reads
6755               (nconc
6756                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6757                gnus-newsgroup-reads))
6758       (while (setq article (pop articles))
6759         (unless (or (memq article gnus-newsgroup-dormant)
6760                     (memq article gnus-newsgroup-marked))
6761           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6762
6763 (defun gnus-summary-limit (articles &optional pop)
6764   (if pop
6765       ;; We pop the previous limit off the stack and use that.
6766       (setq articles (car gnus-newsgroup-limits)
6767             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6768     ;; We use the new limit, so we push the old limit on the stack.
6769     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6770   ;; Set the limit.
6771   (setq gnus-newsgroup-limit articles)
6772   (let ((total (length gnus-newsgroup-data))
6773         (data (gnus-data-find-list (gnus-summary-article-number)))
6774         (gnus-summary-mark-below nil)   ; Inhibit this.
6775         found)
6776     ;; This will do all the work of generating the new summary buffer
6777     ;; according to the new limit.
6778     (gnus-summary-prepare)
6779     ;; Hide any threads, possibly.
6780     (and gnus-show-threads
6781          gnus-thread-hide-subtree
6782          (gnus-summary-hide-all-threads))
6783     ;; Try to return to the article you were at, or one in the
6784     ;; neighborhood.
6785     (when data
6786       ;; We try to find some article after the current one.
6787       (while data
6788         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6789           (setq data nil
6790                 found t))
6791         (setq data (cdr data))))
6792     (unless found
6793       ;; If there is no data, that means that we were after the last
6794       ;; article.  The same goes when we can't find any articles
6795       ;; after the current one.
6796       (goto-char (point-max))
6797       (gnus-summary-find-prev))
6798     (gnus-set-mode-line 'summary)
6799     ;; We return how many articles were removed from the summary
6800     ;; buffer as a result of the new limit.
6801     (- total (length gnus-newsgroup-data))))
6802
6803 (defsubst gnus-invisible-cut-children (threads)
6804   (let ((num 0))
6805     (while threads
6806       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6807         (incf num))
6808       (pop threads))
6809     (< num 2)))
6810
6811 (defsubst gnus-cut-thread (thread)
6812   "Go forwards in the thread until we find an article that we want to display."
6813   (when (or (eq gnus-fetch-old-headers 'some)
6814             (eq gnus-fetch-old-headers 'invisible)
6815             (numberp gnus-fetch-old-headers)
6816             (eq gnus-build-sparse-threads 'some)
6817             (eq gnus-build-sparse-threads 'more))
6818     ;; Deal with old-fetched headers and sparse threads.
6819     (while (and
6820             thread
6821             (or
6822              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6823              (gnus-summary-article-ancient-p
6824               (mail-header-number (car thread))))
6825             (if (or (<= (length (cdr thread)) 1)
6826                     (eq gnus-fetch-old-headers 'invisible))
6827                 (setq gnus-newsgroup-limit
6828                       (delq (mail-header-number (car thread))
6829                             gnus-newsgroup-limit)
6830                       thread (cadr thread))
6831               (when (gnus-invisible-cut-children (cdr thread))
6832                 (let ((th (cdr thread)))
6833                   (while th
6834                     (if (memq (mail-header-number (caar th))
6835                               gnus-newsgroup-limit)
6836                         (setq thread (car th)
6837                               th nil)
6838                       (setq th (cdr th))))))))))
6839   thread)
6840
6841 (defun gnus-cut-threads (threads)
6842   "Cut off all uninteresting articles from the beginning of threads."
6843   (when (or (eq gnus-fetch-old-headers 'some)
6844             (eq gnus-fetch-old-headers 'invisible)
6845             (numberp gnus-fetch-old-headers)
6846             (eq gnus-build-sparse-threads 'some)
6847             (eq gnus-build-sparse-threads 'more))
6848     (let ((th threads))
6849       (while th
6850         (setcar th (gnus-cut-thread (car th)))
6851         (setq th (cdr th)))))
6852   ;; Remove nixed out threads.
6853   (delq nil threads))
6854
6855 (defun gnus-summary-initial-limit (&optional show-if-empty)
6856   "Figure out what the initial limit is supposed to be on group entry.
6857 This entails weeding out unwanted dormants, low-scored articles,
6858 fetch-old-headers verbiage, and so on."
6859   ;; Most groups have nothing to remove.
6860   (if (or gnus-inhibit-limiting
6861           (and (null gnus-newsgroup-dormant)
6862                (not (eq gnus-fetch-old-headers 'some))
6863                (not (numberp gnus-fetch-old-headers))
6864                (not (eq gnus-fetch-old-headers 'invisible))
6865                (null gnus-summary-expunge-below)
6866                (not (eq gnus-build-sparse-threads 'some))
6867                (not (eq gnus-build-sparse-threads 'more))
6868                (null gnus-thread-expunge-below)
6869                (not gnus-use-nocem)))
6870       ()                                ; Do nothing.
6871     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6872     (setq gnus-newsgroup-limit nil)
6873     (mapatoms
6874      (lambda (node)
6875        (unless (car (symbol-value node))
6876          ;; These threads have no parents -- they are roots.
6877          (let ((nodes (cdr (symbol-value node)))
6878                thread)
6879            (while nodes
6880              (if (and gnus-thread-expunge-below
6881                       (< (gnus-thread-total-score (car nodes))
6882                          gnus-thread-expunge-below))
6883                  (gnus-expunge-thread (pop nodes))
6884                (setq thread (pop nodes))
6885                (gnus-summary-limit-children thread))))))
6886      gnus-newsgroup-dependencies)
6887     ;; If this limitation resulted in an empty group, we might
6888     ;; pop the previous limit and use it instead.
6889     (when (and (not gnus-newsgroup-limit)
6890                show-if-empty)
6891       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6892     gnus-newsgroup-limit))
6893
6894 (defun gnus-summary-limit-children (thread)
6895   "Return 1 if this subthread is visible and 0 if it is not."
6896   ;; First we get the number of visible children to this thread.  This
6897   ;; is done by recursing down the thread using this function, so this
6898   ;; will really go down to a leaf article first, before slowly
6899   ;; working its way up towards the root.
6900   (when thread
6901     (let ((children
6902            (if (cdr thread)
6903                (apply '+ (mapcar 'gnus-summary-limit-children
6904                                  (cdr thread)))
6905              0))
6906           (number (mail-header-number (car thread)))
6907           score)
6908       (if (and
6909            (not (memq number gnus-newsgroup-marked))
6910            (or
6911             ;; If this article is dormant and has absolutely no visible
6912             ;; children, then this article isn't visible.
6913             (and (memq number gnus-newsgroup-dormant)
6914                  (zerop children))
6915             ;; If this is "fetch-old-headered" and there is no
6916             ;; visible children, then we don't want this article.
6917             (and (or (eq gnus-fetch-old-headers 'some)
6918                      (numberp gnus-fetch-old-headers))
6919                  (gnus-summary-article-ancient-p number)
6920                  (zerop children))
6921             ;; If this is "fetch-old-headered" and `invisible', then
6922             ;; we don't want this article.
6923             (and (eq gnus-fetch-old-headers 'invisible)
6924                  (gnus-summary-article-ancient-p number))
6925             ;; If this is a sparsely inserted article with no children,
6926             ;; we don't want it.
6927             (and (eq gnus-build-sparse-threads 'some)
6928                  (gnus-summary-article-sparse-p number)
6929                  (zerop children))
6930             ;; If we use expunging, and this article is really
6931             ;; low-scored, then we don't want this article.
6932             (when (and gnus-summary-expunge-below
6933                        (< (setq score
6934                                 (or (cdr (assq number gnus-newsgroup-scored))
6935                                     gnus-summary-default-score))
6936                           gnus-summary-expunge-below))
6937               ;; We increase the expunge-tally here, but that has
6938               ;; nothing to do with the limits, really.
6939               (incf gnus-newsgroup-expunged-tally)
6940               ;; We also mark as read here, if that's wanted.
6941               (when (and gnus-summary-mark-below
6942                          (< score gnus-summary-mark-below))
6943                 (setq gnus-newsgroup-unreads
6944                       (delq number gnus-newsgroup-unreads))
6945                 (if gnus-newsgroup-auto-expire
6946                     (push number gnus-newsgroup-expirable)
6947                   (push (cons number gnus-low-score-mark)
6948                         gnus-newsgroup-reads)))
6949               t)
6950             ;; Check NoCeM things.
6951             (if (and gnus-use-nocem
6952                      (gnus-nocem-unwanted-article-p
6953                       (mail-header-id (car thread))))
6954                 (progn
6955                   (setq gnus-newsgroup-unreads
6956                         (delq number gnus-newsgroup-unreads))
6957                   t))))
6958           ;; Nope, invisible article.
6959           0
6960         ;; Ok, this article is to be visible, so we add it to the limit
6961         ;; and return 1.
6962         (push number gnus-newsgroup-limit)
6963         1))))
6964
6965 (defun gnus-expunge-thread (thread)
6966   "Mark all articles in THREAD as read."
6967   (let* ((number (mail-header-number (car thread))))
6968     (incf gnus-newsgroup-expunged-tally)
6969     ;; We also mark as read here, if that's wanted.
6970     (setq gnus-newsgroup-unreads
6971           (delq number gnus-newsgroup-unreads))
6972     (if gnus-newsgroup-auto-expire
6973         (push number gnus-newsgroup-expirable)
6974       (push (cons number gnus-low-score-mark)
6975             gnus-newsgroup-reads)))
6976   ;; Go recursively through all subthreads.
6977   (mapcar 'gnus-expunge-thread (cdr thread)))
6978
6979 ;; Summary article oriented commands
6980
6981 (defun gnus-summary-refer-parent-article (n)
6982   "Refer parent article N times.
6983 If N is negative, go to ancestor -N instead.
6984 The difference between N and the number of articles fetched is returned."
6985   (interactive "p")
6986   (let ((skip 1)
6987         error header ref)
6988     (when (not (natnump n))
6989       (setq skip (abs n)
6990             n 1))
6991     (while (and (> n 0)
6992                 (not error))
6993       (setq header (gnus-summary-article-header))
6994       (if (and (eq (mail-header-number header)
6995                    (cdr gnus-article-current))
6996                (equal gnus-newsgroup-name
6997                       (car gnus-article-current)))
6998           ;; If we try to find the parent of the currently
6999           ;; displayed article, then we take a look at the actual
7000           ;; References header, since this is slightly more
7001           ;; reliable than the References field we got from the
7002           ;; server.
7003           (save-excursion
7004             (set-buffer gnus-original-article-buffer)
7005             (nnheader-narrow-to-headers)
7006             (unless (setq ref (message-fetch-field "references"))
7007               (setq ref (message-fetch-field "in-reply-to")))
7008             (widen))
7009         (setq ref
7010               ;; It's not the current article, so we take a bet on
7011               ;; the value we got from the server.
7012               (mail-header-references header)))
7013       (if (and ref
7014                (not (equal ref "")))
7015           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7016             (gnus-message 1 "Couldn't find parent"))
7017         (gnus-message 1 "No references in article %d"
7018                       (gnus-summary-article-number))
7019         (setq error t))
7020       (decf n))
7021     (gnus-summary-position-point)
7022     n))
7023
7024 (defun gnus-summary-refer-references ()
7025   "Fetch all articles mentioned in the References header.
7026 Return the number of articles fetched."
7027   (interactive)
7028   (let ((ref (mail-header-references (gnus-summary-article-header)))
7029         (current (gnus-summary-article-number))
7030         (n 0))
7031     (if (or (not ref)
7032             (equal ref ""))
7033         (error "No References in the current article")
7034       ;; For each Message-ID in the References header...
7035       (while (string-match "<[^>]*>" ref)
7036         (incf n)
7037         ;; ... fetch that article.
7038         (gnus-summary-refer-article
7039          (prog1 (match-string 0 ref)
7040            (setq ref (substring ref (match-end 0))))))
7041       (gnus-summary-goto-subject current)
7042       (gnus-summary-position-point)
7043       n)))
7044
7045 (defun gnus-summary-refer-thread (&optional limit)
7046   "Fetch all articles in the current thread.
7047 If LIMIT (the numerical prefix), fetch that many old headers instead
7048 of what's specified by the `gnus-refer-thread-limit' variable."
7049   (interactive "P")
7050   (let ((id (mail-header-id (gnus-summary-article-header)))
7051         (limit (if limit (prefix-numeric-value limit)
7052                  gnus-refer-thread-limit)))
7053     ;; We want to fetch LIMIT *old* headers, but we also have to
7054     ;; re-fetch all the headers in the current buffer, because many of
7055     ;; them may be undisplayed.  So we adjust LIMIT.
7056     (when (numberp limit)
7057       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7058     (unless (eq gnus-fetch-old-headers 'invisible)
7059       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7060       ;; Retrieve the headers and read them in.
7061       (if (eq (gnus-retrieve-headers
7062                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7063               'nov)
7064           (gnus-build-all-threads)
7065         (error "Can't fetch thread from backends that don't support NOV"))
7066       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7067     (gnus-summary-limit-include-thread id)))
7068
7069 (defun gnus-summary-refer-article (message-id)
7070   "Fetch an article specified by MESSAGE-ID."
7071   (interactive "sMessage-ID: ")
7072   (when (and (stringp message-id)
7073              (not (zerop (length message-id))))
7074     ;; Construct the correct Message-ID if necessary.
7075     ;; Suggested by tale@pawl.rpi.edu.
7076     (unless (string-match "^<" message-id)
7077       (setq message-id (concat "<" message-id)))
7078     (unless (string-match ">$" message-id)
7079       (setq message-id (concat message-id ">")))
7080     (let* ((header (gnus-id-to-header message-id))
7081            (sparse (and header
7082                         (gnus-summary-article-sparse-p
7083                          (mail-header-number header))
7084                         (memq (mail-header-number header)
7085                               gnus-newsgroup-limit)))
7086            number)
7087       (cond
7088        ;; If the article is present in the buffer we just go to it.
7089        ((and header
7090              (or (not (gnus-summary-article-sparse-p
7091                        (mail-header-number header)))
7092                  sparse))
7093         (prog1
7094             (gnus-summary-goto-article
7095              (mail-header-number header) nil t)
7096           (when sparse
7097             (gnus-summary-update-article (mail-header-number header)))))
7098        (t
7099         ;; We fetch the article.
7100         (catch 'found
7101           (dolist (gnus-override-method (gnus-refer-article-methods))
7102             (gnus-check-server gnus-override-method)
7103             ;; Fetch the header, and display the article.
7104             (when (setq number (gnus-summary-insert-subject message-id))
7105               (gnus-summary-select-article nil nil nil number)
7106               (throw 'found t)))
7107           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7108
7109 (defun gnus-refer-article-methods ()
7110   "Return a list of referrable methods."
7111   (cond
7112    ;; No method, so we default to current and native.
7113    ((null gnus-refer-article-method)
7114     (list gnus-current-select-method gnus-select-method))
7115    ;; Current.
7116    ((eq 'current gnus-refer-article-method)
7117     (list gnus-current-select-method))
7118    ;; List of select methods.
7119    ((not (and (symbolp (car gnus-refer-article-method))
7120               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7121     (let (out)
7122       (dolist (method gnus-refer-article-method)
7123         (push (if (eq 'current method)
7124                   gnus-current-select-method
7125                 method)
7126               out))
7127       (nreverse out)))
7128    ;; One single select method.
7129    (t
7130     (list gnus-refer-article-method))))
7131
7132 (defun gnus-summary-edit-parameters ()
7133   "Edit the group parameters of the current group."
7134   (interactive)
7135   (gnus-group-edit-group gnus-newsgroup-name 'params))
7136
7137 (defun gnus-summary-customize-parameters ()
7138   "Customize the group parameters of the current group."
7139   (interactive)
7140   (gnus-group-customize gnus-newsgroup-name))
7141
7142 (defun gnus-summary-enter-digest-group (&optional force)
7143   "Enter an nndoc group based on the current article.
7144 If FORCE, force a digest interpretation.  If not, try
7145 to guess what the document format is."
7146   (interactive "P")
7147   (let ((conf gnus-current-window-configuration))
7148     (save-excursion
7149       (gnus-summary-select-article))
7150     (setq gnus-current-window-configuration conf)
7151     (let* ((name (format "%s-%d"
7152                          (gnus-group-prefixed-name
7153                           gnus-newsgroup-name (list 'nndoc ""))
7154                          (save-excursion
7155                            (set-buffer gnus-summary-buffer)
7156                            gnus-current-article)))
7157            (ogroup gnus-newsgroup-name)
7158            (params (append (gnus-info-params (gnus-get-info ogroup))
7159                            (list (cons 'to-group ogroup))
7160                            (list (cons 'save-article-group ogroup))))
7161            (case-fold-search t)
7162            (buf (current-buffer))
7163            dig to-address)
7164       (save-excursion
7165         (set-buffer gnus-original-article-buffer)
7166         ;; Have the digest group inherit the main mail address of
7167         ;; the parent article.
7168         (when (setq to-address (or (message-fetch-field "reply-to")
7169                                    (message-fetch-field "from")))
7170           (setq params (append
7171                         (list (cons 'to-address
7172                                     (funcall gnus-decode-encoded-word-function
7173                                              to-address))))))
7174         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7175         (insert-buffer-substring gnus-original-article-buffer)
7176         ;; Remove lines that may lead nndoc to misinterpret the
7177         ;; document type.
7178         (narrow-to-region
7179          (goto-char (point-min))
7180          (or (search-forward "\n\n" nil t) (point)))
7181         (goto-char (point-min))
7182         (delete-matching-lines "^Path:\\|^From ")
7183         (widen))
7184       (unwind-protect
7185           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7186                     (gnus-newsgroup-ephemeral-ignored-charsets
7187                      gnus-newsgroup-ignored-charsets))
7188                 (gnus-group-read-ephemeral-group
7189                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7190                               (nndoc-article-type
7191                                ,(if force 'mbox 'guess))) t))
7192               ;; Make all postings to this group go to the parent group.
7193               (nconc (gnus-info-params (gnus-get-info name))
7194                      params)
7195             ;; Couldn't select this doc group.
7196             (switch-to-buffer buf)
7197             (gnus-set-global-variables)
7198             (gnus-configure-windows 'summary)
7199             (gnus-message 3 "Article couldn't be entered?"))
7200         (kill-buffer dig)))))
7201
7202 (defun gnus-summary-read-document (n)
7203   "Open a new group based on the current article(s).
7204 This will allow you to read digests and other similar
7205 documents as newsgroups.
7206 Obeys the standard process/prefix convention."
7207   (interactive "P")
7208   (let* ((articles (gnus-summary-work-articles n))
7209          (ogroup gnus-newsgroup-name)
7210          (params (append (gnus-info-params (gnus-get-info ogroup))
7211                          (list (cons 'to-group ogroup))))
7212          article group egroup groups vgroup)
7213     (while (setq article (pop articles))
7214       (setq group (format "%s-%d" gnus-newsgroup-name article))
7215       (gnus-summary-remove-process-mark article)
7216       (when (gnus-summary-display-article article)
7217         (save-excursion
7218           (with-temp-buffer
7219             (insert-buffer-substring gnus-original-article-buffer)
7220             ;; Remove some headers that may lead nndoc to make
7221             ;; the wrong guess.
7222             (message-narrow-to-head)
7223             (goto-char (point-min))
7224             (delete-matching-lines "^\\(Path\\):\\|^From ")
7225             (widen)
7226             (if (setq egroup
7227                       (gnus-group-read-ephemeral-group
7228                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7229                                      (nndoc-article-type guess))
7230                        t nil t))
7231                 (progn
7232                   ;; Make all postings to this group go to the parent group.
7233                   (nconc (gnus-info-params (gnus-get-info egroup))
7234                          params)
7235                   (push egroup groups))
7236               ;; Couldn't select this doc group.
7237               (gnus-error 3 "Article couldn't be entered"))))))
7238     ;; Now we have selected all the documents.
7239     (cond
7240      ((not groups)
7241       (error "None of the articles could be interpreted as documents"))
7242      ((gnus-group-read-ephemeral-group
7243        (setq vgroup (format
7244                      "nnvirtual:%s-%s" gnus-newsgroup-name
7245                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7246        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7247        t
7248        (cons (current-buffer) 'summary)))
7249      (t
7250       (error "Couldn't select virtual nndoc group")))))
7251
7252 (defun gnus-summary-isearch-article (&optional regexp-p)
7253   "Do incremental search forward on the current article.
7254 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7255   (interactive "P")
7256   (let* ((gnus-inhibit-treatment t)
7257          (old (gnus-summary-select-article)))
7258     (gnus-configure-windows 'article)
7259     (gnus-eval-in-buffer-window gnus-article-buffer
7260       (save-restriction
7261         (widen)
7262         (when (eq 'old old)
7263           (gnus-article-show-all-headers))
7264         (goto-char (point-min))
7265         (isearch-forward regexp-p)))))
7266
7267 (defun gnus-summary-search-article-forward (regexp &optional backward)
7268   "Search for an article containing REGEXP forward.
7269 If BACKWARD, search backward instead."
7270   (interactive
7271    (list (read-string
7272           (format "Search article %s (regexp%s): "
7273                   (if current-prefix-arg "backward" "forward")
7274                   (if gnus-last-search-regexp
7275                       (concat ", default " gnus-last-search-regexp)
7276                     "")))
7277          current-prefix-arg))
7278   (if (string-equal regexp "")
7279       (setq regexp (or gnus-last-search-regexp ""))
7280     (setq gnus-last-search-regexp regexp)
7281     (setq gnus-article-before-search gnus-current-article))
7282   ;; Intentionally set gnus-last-article.
7283   (setq gnus-last-article gnus-article-before-search)
7284   (let ((gnus-last-article gnus-last-article))
7285     (if (gnus-summary-search-article regexp backward)
7286         (gnus-summary-show-thread)
7287       (error "Search failed: \"%s\"" regexp))))
7288
7289 (defun gnus-summary-search-article-backward (regexp)
7290   "Search for an article containing REGEXP backward."
7291   (interactive
7292    (list (read-string
7293           (format "Search article backward (regexp%s): "
7294                   (if gnus-last-search-regexp
7295                       (concat ", default " gnus-last-search-regexp)
7296                     "")))))
7297   (gnus-summary-search-article-forward regexp 'backward))
7298
7299 (eval-when-compile
7300   (defmacro gnus-summary-search-article-position-point (regexp backward)
7301     "Dehighlight the last matched text and goto the beginning position."
7302     (` (if (and gnus-summary-search-article-matched-data
7303                 (let ((text (caddr gnus-summary-search-article-matched-data))
7304                       (inhibit-read-only t)
7305                       buffer-read-only)
7306                   (delete-region
7307                    (goto-char (car gnus-summary-search-article-matched-data))
7308                    (cadr gnus-summary-search-article-matched-data))
7309                   (insert text)
7310                   (string-match (, regexp) text)))
7311            (if (, backward) (beginning-of-line) (end-of-line))
7312          (goto-char (if (, backward) (point-max) (point-min))))))
7313
7314   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7315     "Place point where X-Face image is displayed."
7316     (if (featurep 'xemacs)
7317         (` (let ((end (if (search-forward "\n\n" nil t)
7318                           (goto-char (1- (point)))
7319                         (point-min)))
7320                  extent)
7321              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7322              (unless (and (re-search-forward "^From:" end t)
7323                           (setq extent (extent-at (point)))
7324                           (extent-begin-glyph extent))
7325                (goto-char (, opoint)))))
7326       (` (let ((end (if (search-forward "\n\n" nil t)
7327                         (goto-char (1- (point)))
7328                       (point-min)))
7329                (start (or (search-backward "\n\n" nil t) (point-min))))
7330            (goto-char
7331             (or (text-property-any start end 'x-face-image t);; x-face-e21
7332                 (text-property-any start end 'x-face-mule-bitmap-image t)
7333                 (, opoint)))))))
7334
7335   (defmacro gnus-summary-search-article-highlight-matched-text
7336     (backward treated x-face)
7337     "Highlight matched text in the function `gnus-summary-search-article'."
7338     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7339              (end (set-marker (make-marker) (match-end 0)))
7340              (inhibit-read-only t)
7341              buffer-read-only)
7342          (unless treated
7343            (let ((,@
7344                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7345                     (mapcar
7346                      (lambda (item) (setq items (delq item items)))
7347                      '(gnus-treat-buttonize
7348                        gnus-treat-fill-article
7349                        gnus-treat-fill-long-lines
7350                        gnus-treat-emphasize
7351                        gnus-treat-highlight-headers
7352                        gnus-treat-highlight-citation
7353                        gnus-treat-highlight-signature
7354                        gnus-treat-overstrike
7355                        gnus-treat-display-xface
7356                        gnus-treat-buttonize-head
7357                        gnus-treat-decode-article-as-default-mime-charset))
7358                     (static-if (featurep 'xemacs)
7359                         items
7360                       (cons '(x-face-mule-delete-x-face-field
7361                               (quote never))
7362                             items))))
7363                  (gnus-treat-display-xface
7364                   (when (, x-face) gnus-treat-display-xface)))
7365              (gnus-article-prepare-mime-display)))
7366          (goto-char (if (, backward) start end))
7367          (when (, x-face)
7368            (gnus-summary-search-article-highlight-goto-x-face (point)))
7369          (setq gnus-summary-search-article-matched-data
7370                (list start end (buffer-substring start end)))
7371          (unless (eq start end);; matched text has been deleted. :-<
7372            (put-text-property start end 'face
7373                               (or (find-face 'isearch)
7374                                   'secondary-selection))))))
7375   )
7376
7377 (defun gnus-summary-search-article (regexp &optional backward)
7378   "Search for an article containing REGEXP.
7379 Optional argument BACKWARD means do search for backward.
7380 `gnus-select-article-hook' is not called during the search."
7381   ;; We have to require this here to make sure that the following
7382   ;; dynamic binding isn't shadowed by autoloading.
7383   (require 'gnus-async)
7384   (require 'gnus-art)
7385   (let ((gnus-select-article-hook nil)  ;Disable hook.
7386         (gnus-article-display-hook nil)
7387         (gnus-article-prepare-hook nil)
7388         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7389         (gnus-use-article-prefetch nil)
7390         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7391         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7392         (sum (current-buffer))
7393         (found nil)
7394         point treated)
7395     (gnus-save-hidden-threads
7396       (static-if (featurep 'xemacs)
7397           (let ((gnus-inhibit-treatment t))
7398             (setq treated (eq 'old (gnus-summary-select-article)))
7399             (when (and treated
7400                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7401                                  (window-live-p (get-buffer-window
7402                                                  gnus-article-buffer t)))))
7403               (gnus-summary-select-article nil t)
7404               (setq treated nil)))
7405         (let ((gnus-inhibit-treatment t)
7406               (x-face-mule-delete-x-face-field 'never))
7407           (setq treated (eq 'old (gnus-summary-select-article)))
7408           (when (and treated
7409                      (not
7410                       (and (gnus-buffer-live-p gnus-article-buffer)
7411                            (window-live-p (get-buffer-window
7412                                            gnus-article-buffer t))
7413                            (or (not (string-match "^\\^X-Face:" regexp))
7414                                (with-current-buffer gnus-article-buffer
7415                                  gnus-summary-search-article-matched-data)))))
7416             (gnus-summary-select-article nil t)
7417             (setq treated nil))))
7418       (set-buffer gnus-article-buffer)
7419       (widen)
7420       (if treated
7421           (progn
7422             (gnus-article-show-all-headers)
7423             (gnus-summary-search-article-position-point regexp backward))
7424         (goto-char (if backward (point-max) (point-min))))
7425       (while (not found)
7426         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7427         (if (if backward
7428                 (re-search-backward regexp nil t)
7429               (re-search-forward regexp nil t))
7430             ;; We found the regexp.
7431             (progn
7432               (gnus-summary-search-article-highlight-matched-text
7433                backward treated (string-match "^\\^X-Face:" regexp))
7434               (setq found 'found)
7435               (forward-line
7436                (/ (- 2 (window-height
7437                         (get-buffer-window gnus-article-buffer t)))
7438                   2))
7439               (set-window-start
7440                (get-buffer-window (current-buffer))
7441                (point))
7442               (set-buffer sum)
7443               (setq point (point)))
7444           ;; We didn't find it, so we go to the next article.
7445           (set-buffer sum)
7446           (setq found 'not)
7447           (while (eq found 'not)
7448             (if (not (if backward (gnus-summary-find-prev)
7449                        (gnus-summary-find-next)))
7450                 ;; No more articles.
7451                 (setq found t)
7452               ;; Select the next article and adjust point.
7453               (unless (gnus-summary-article-sparse-p
7454                        (gnus-summary-article-number))
7455                 (setq found nil)
7456                 (let ((gnus-inhibit-treatment t))
7457                   (gnus-summary-select-article))
7458                 (setq treated nil)
7459                 (set-buffer gnus-article-buffer)
7460                 (widen)
7461                 (goto-char (if backward (point-max) (point-min))))))))
7462       (gnus-message 7 ""))
7463     ;; Return whether we found the regexp.
7464     (when (eq found 'found)
7465       (goto-char point)
7466       (gnus-summary-show-thread)
7467       (gnus-summary-goto-subject gnus-current-article)
7468       (gnus-summary-position-point)
7469       t)))
7470
7471 (defun gnus-summary-find-matching (header regexp &optional backward unread
7472                                           not-case-fold)
7473   "Return a list of all articles that match REGEXP on HEADER.
7474 The search stars on the current article and goes forwards unless
7475 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7476 If UNREAD is non-nil, only unread articles will
7477 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7478 in the comparisons."
7479   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7480                 (gnus-data-find-list
7481                  (gnus-summary-article-number) (gnus-data-list backward))))
7482         (case-fold-search (not not-case-fold))
7483         articles d func)
7484     (if (consp header)
7485         (if (eq (car header) 'extra)
7486             (setq func
7487                   `(lambda (h)
7488                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7489                          "")))
7490           (error "%s is an invalid header" header))
7491       (unless (fboundp (intern (concat "mail-header-" header)))
7492         (error "%s is not a valid header" header))
7493       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7494     (while data
7495       (setq d (car data))
7496       (and (or (not unread)             ; We want all articles...
7497                (gnus-data-unread-p d))  ; Or just unreads.
7498            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7499            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7500            (push (gnus-data-number d) articles)) ; Success!
7501       (setq data (cdr data)))
7502     (nreverse articles)))
7503
7504 (defun gnus-summary-execute-command (header regexp command &optional backward)
7505   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7506 If HEADER is an empty string (or nil), the match is done on the entire
7507 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7508   (interactive
7509    (list (let ((completion-ignore-case t))
7510            (completing-read
7511             "Header name: "
7512             (mapcar (lambda (string) (list string))
7513                     '("Number" "Subject" "From" "Lines" "Date"
7514                       "Message-ID" "Xref" "References" "Body"))
7515             nil 'require-match))
7516          (read-string "Regexp: ")
7517          (read-key-sequence "Command: ")
7518          current-prefix-arg))
7519   (when (equal header "Body")
7520     (setq header ""))
7521   ;; Hidden thread subtrees must be searched as well.
7522   (gnus-summary-show-all-threads)
7523   ;; We don't want to change current point nor window configuration.
7524   (save-excursion
7525     (save-window-excursion
7526       (gnus-message 6 "Executing %s..." (key-description command))
7527       ;; We'd like to execute COMMAND interactively so as to give arguments.
7528       (gnus-execute header regexp
7529                     `(call-interactively ',(key-binding command))
7530                     backward)
7531       (gnus-message 6 "Executing %s...done" (key-description command)))))
7532
7533 (defun gnus-summary-beginning-of-article ()
7534   "Scroll the article back to the beginning."
7535   (interactive)
7536   (gnus-summary-select-article)
7537   (gnus-configure-windows 'article)
7538   (gnus-eval-in-buffer-window gnus-article-buffer
7539     (widen)
7540     (goto-char (point-min))
7541     (when gnus-page-broken
7542       (gnus-narrow-to-page))))
7543
7544 (defun gnus-summary-end-of-article ()
7545   "Scroll to the end of the article."
7546   (interactive)
7547   (gnus-summary-select-article)
7548   (gnus-configure-windows 'article)
7549   (gnus-eval-in-buffer-window gnus-article-buffer
7550     (widen)
7551     (goto-char (point-max))
7552     (recenter -3)
7553     (when gnus-page-broken
7554       (gnus-narrow-to-page))))
7555
7556 (defun gnus-summary-print-article (&optional filename n)
7557   "Generate and print a PostScript image of the N next (mail) articles.
7558
7559 If N is negative, print the N previous articles.  If N is nil and articles
7560 have been marked with the process mark, print these instead.
7561
7562 If the optional first argument FILENAME is nil, send the image to the
7563 printer.  If FILENAME is a string, save the PostScript image in a file with
7564 that name.  If FILENAME is a number, prompt the user for the name of the file
7565 to save in."
7566   (interactive (list (ps-print-preprint current-prefix-arg)))
7567   (dolist (article (gnus-summary-work-articles n))
7568     (gnus-summary-select-article nil nil 'pseudo article)
7569     (gnus-eval-in-buffer-window gnus-article-buffer
7570       (let ((buffer (generate-new-buffer " *print*")))
7571         (unwind-protect
7572             (progn
7573               (copy-to-buffer buffer (point-min) (point-max))
7574               (set-buffer buffer)
7575               (gnus-article-delete-invisible-text)
7576               (let ((ps-left-header
7577                      (list
7578                       (concat "("
7579                               (mail-header-subject gnus-current-headers) ")")
7580                       (concat "("
7581                               (mail-header-from gnus-current-headers) ")")))
7582                     (ps-right-header
7583                      (list
7584                       "/pagenumberstring load"
7585                       (concat "("
7586                               (mail-header-date gnus-current-headers) ")"))))
7587                 (gnus-run-hooks 'gnus-ps-print-hook)
7588                 (save-excursion
7589                   (ps-spool-buffer-with-faces))))
7590           (kill-buffer buffer))))
7591     (gnus-summary-remove-process-mark article))
7592   (ps-despool filename))
7593
7594 (defun gnus-summary-show-article (&optional arg)
7595   "Force re-fetching of the current article.
7596 If ARG (the prefix) is a number, show the article with the charset
7597 defined in `gnus-summary-show-article-charset-alist', or the charset
7598 inputed.
7599 If ARG (the prefix) is non-nil and not a number, show the raw article
7600 without any article massaging functions being run."
7601   (interactive "P")
7602   (cond
7603    ((numberp arg)
7604     (let ((gnus-newsgroup-charset
7605            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7606                (read-coding-system "Charset: ")))
7607           (gnus-newsgroup-ignored-charsets 'gnus-all))
7608       (gnus-summary-select-article nil 'force)
7609       (let ((deps gnus-newsgroup-dependencies)
7610             head header)
7611         (save-excursion
7612           (set-buffer gnus-original-article-buffer)
7613           (save-restriction
7614             (message-narrow-to-head)
7615             (setq head (buffer-string)))
7616           (with-temp-buffer
7617             (insert (format "211 %d Article retrieved.\n"
7618                             (cdr gnus-article-current)))
7619             (insert head)
7620             (insert ".\n")
7621             (let ((nntp-server-buffer (current-buffer)))
7622               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7623         (gnus-data-set-header
7624          (gnus-data-find (cdr gnus-article-current))
7625          header)
7626         (gnus-summary-update-article-line
7627          (cdr gnus-article-current) header))))
7628    ((not arg)
7629     ;; Select the article the normal way.
7630     (gnus-summary-select-article nil 'force))
7631    (t
7632     ;; We have to require this here to make sure that the following
7633     ;; dynamic binding isn't shadowed by autoloading.
7634     (require 'gnus-async)
7635     (require 'gnus-art)
7636     ;; Bind the article treatment functions to nil.
7637     (let ((gnus-have-all-headers t)
7638           gnus-article-display-hook
7639           gnus-article-prepare-hook
7640           gnus-article-decode-hook
7641           gnus-break-pages
7642           gnus-show-mime)
7643       (gnus-summary-select-article nil 'force))))
7644   (gnus-summary-goto-subject gnus-current-article)
7645   (gnus-summary-position-point))
7646
7647 (defun gnus-summary-verbose-headers (&optional arg)
7648   "Toggle permanent full header display.
7649 If ARG is a positive number, turn header display on.
7650 If ARG is a negative number, turn header display off."
7651   (interactive "P")
7652   (setq gnus-show-all-headers
7653         (cond ((or (not (numberp arg))
7654                    (zerop arg))
7655                (not gnus-show-all-headers))
7656               ((natnump arg)
7657                t)))
7658   (gnus-summary-show-article))
7659
7660 (defun gnus-summary-toggle-header (&optional arg)
7661   "Show the headers if they are hidden, or hide them if they are shown.
7662 If ARG is a positive number, show the entire header.
7663 If ARG is a negative number, hide the unwanted header lines."
7664   (interactive "P")
7665   (save-excursion
7666     (set-buffer gnus-article-buffer)
7667     (save-restriction
7668       (let* ((buffer-read-only nil)
7669              (inhibit-point-motion-hooks t)
7670              hidden e)
7671         (setq hidden
7672               (if (numberp arg)
7673                   (>= arg 0)
7674                 (save-restriction
7675                   (article-narrow-to-head)
7676                   (gnus-article-hidden-text-p 'headers))))
7677         (goto-char (point-min))
7678         (when (search-forward "\n\n" nil t)
7679           (delete-region (point-min) (1- (point))))
7680         (goto-char (point-min))
7681         (save-excursion
7682           (set-buffer gnus-original-article-buffer)
7683           (goto-char (point-min))
7684           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7685         (insert-buffer-substring gnus-original-article-buffer 1 e)
7686         (save-restriction
7687           (narrow-to-region (point-min) (point))
7688           (article-decode-encoded-words)
7689           (if  hidden
7690               (let ((gnus-treat-hide-headers nil)
7691                     (gnus-treat-hide-boring-headers nil))
7692                 (setq gnus-article-wash-types
7693                       (delq 'headers gnus-article-wash-types))
7694                 (gnus-treat-article 'head))
7695             (gnus-treat-article 'head)))
7696         (gnus-set-mode-line 'article)))))
7697
7698 (defun gnus-summary-show-all-headers ()
7699   "Make all header lines visible."
7700   (interactive)
7701   (gnus-article-show-all-headers))
7702
7703 (defun gnus-summary-toggle-mime (&optional arg)
7704   "Toggle MIME processing.
7705 If ARG is a positive number, turn MIME processing on."
7706   (interactive "P")
7707   (setq gnus-show-mime
7708         (if (null arg)
7709             (not gnus-show-mime)
7710           (> (prefix-numeric-value arg) 0)))
7711   (gnus-summary-select-article t 'force))
7712
7713 (defun gnus-summary-caesar-message (&optional arg)
7714   "Caesar rotate the current article by 13.
7715 The numerical prefix specifies how many places to rotate each letter
7716 forward."
7717   (interactive "P")
7718   (gnus-summary-select-article)
7719   (let ((mail-header-separator ""))
7720     (gnus-eval-in-buffer-window gnus-article-buffer
7721       (save-restriction
7722         (widen)
7723         (let ((start (window-start))
7724               buffer-read-only)
7725           (message-caesar-buffer-body arg)
7726           (set-window-start (get-buffer-window (current-buffer)) start))))))
7727
7728 (defun gnus-summary-stop-page-breaking ()
7729   "Stop page breaking in the current article."
7730   (interactive)
7731   (gnus-summary-select-article)
7732   (gnus-eval-in-buffer-window gnus-article-buffer
7733     (widen)
7734     (when (gnus-visual-p 'page-marker)
7735       (let ((buffer-read-only nil))
7736         (gnus-remove-text-with-property 'gnus-prev)
7737         (gnus-remove-text-with-property 'gnus-next))
7738       (setq gnus-page-broken nil))))
7739
7740 (defun gnus-summary-move-article (&optional n to-newsgroup
7741                                             select-method action)
7742   "Move the current article to a different newsgroup.
7743 If N is a positive number, move the N next articles.
7744 If N is a negative number, move the N previous articles.
7745 If N is nil and any articles have been marked with the process mark,
7746 move those articles instead.
7747 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7748 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7749 re-spool using this method.
7750
7751 For this function to work, both the current newsgroup and the
7752 newsgroup that you want to move to have to support the `request-move'
7753 and `request-accept' functions.
7754
7755 ACTION can be either `move' (the default), `crosspost' or `copy'."
7756   (interactive "P")
7757   (unless action
7758     (setq action 'move))
7759   ;; Disable marking as read.
7760   (let (gnus-mark-article-hook)
7761     (save-window-excursion
7762       (gnus-summary-select-article)))
7763   ;; Check whether the source group supports the required functions.
7764   (cond ((and (eq action 'move)
7765               (not (gnus-check-backend-function
7766                     'request-move-article gnus-newsgroup-name)))
7767          (error "The current group does not support article moving"))
7768         ((and (eq action 'crosspost)
7769               (not (gnus-check-backend-function
7770                     'request-replace-article gnus-newsgroup-name)))
7771          (error "The current group does not support article editing")))
7772   (let ((articles (gnus-summary-work-articles n))
7773         (prefix (if (gnus-check-backend-function
7774                     'request-move-article gnus-newsgroup-name)
7775                     (gnus-group-real-prefix gnus-newsgroup-name)
7776                   ""))
7777         (names '((move "Move" "Moving")
7778                  (copy "Copy" "Copying")
7779                  (crosspost "Crosspost" "Crossposting")))
7780         (copy-buf (save-excursion
7781                     (nnheader-set-temp-buffer " *copy article*")))
7782         (default-marks gnus-article-mark-lists)
7783         (no-expire-marks (delete '(expirable . expire)
7784                                  (copy-sequence gnus-article-mark-lists)))
7785         art-group to-method new-xref article to-groups)
7786     (unless (assq action names)
7787       (error "Unknown action %s" action))
7788     ;; Read the newsgroup name.
7789     (when (and (not to-newsgroup)
7790                (not select-method))
7791       (setq to-newsgroup
7792             (gnus-read-move-group-name
7793              (cadr (assq action names))
7794              (symbol-value (intern (format "gnus-current-%s-group" action)))
7795              articles prefix))
7796       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7797     (setq to-method (or select-method
7798                         (gnus-server-to-method
7799                          (gnus-group-method to-newsgroup))))
7800     ;; Check the method we are to move this article to...
7801     (unless (gnus-check-backend-function
7802              'request-accept-article (car to-method))
7803       (error "%s does not support article copying" (car to-method)))
7804     (unless (gnus-check-server to-method)
7805       (error "Can't open server %s" (car to-method)))
7806     (gnus-message 6 "%s to %s: %s..."
7807                   (caddr (assq action names))
7808                   (or (car select-method) to-newsgroup) articles)
7809     (while articles
7810       (setq article (pop articles))
7811       (setq
7812        art-group
7813        (cond
7814         ;; Move the article.
7815         ((eq action 'move)
7816          ;; Remove this article from future suppression.
7817          (gnus-dup-unsuppress-article article)
7818          (gnus-request-move-article
7819           article                       ; Article to move
7820           gnus-newsgroup-name           ; From newsgroup
7821           (nth 1 (gnus-find-method-for-group
7822                   gnus-newsgroup-name)) ; Server
7823           (list 'gnus-request-accept-article
7824                 to-newsgroup (list 'quote select-method)
7825                 (not articles) t)       ; Accept form
7826           (not articles)))              ; Only save nov last time
7827         ;; Copy the article.
7828         ((eq action 'copy)
7829          (save-excursion
7830            (set-buffer copy-buf)
7831            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7832              (gnus-request-accept-article
7833               to-newsgroup select-method (not articles) t))))
7834         ;; Crosspost the article.
7835         ((eq action 'crosspost)
7836          (let ((xref (message-tokenize-header
7837                       (mail-header-xref (gnus-summary-article-header article))
7838                       " ")))
7839            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7840                                   ":" article))
7841            (unless xref
7842              (setq xref (list (system-name))))
7843            (setq new-xref
7844                  (concat
7845                   (mapconcat 'identity
7846                              (delete "Xref:" (delete new-xref xref))
7847                              " ")
7848                   " " new-xref))
7849            (save-excursion
7850              (set-buffer copy-buf)
7851              ;; First put the article in the destination group.
7852              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7853              (when (consp (setq art-group
7854                                 (gnus-request-accept-article
7855                                  to-newsgroup select-method (not articles))))
7856                (setq new-xref (concat new-xref " " (car art-group)
7857                                       ":" (cdr art-group)))
7858                ;; Now we have the new Xrefs header, so we insert
7859                ;; it and replace the new article.
7860                (nnheader-replace-header "Xref" new-xref)
7861                (gnus-request-replace-article
7862                 (cdr art-group) to-newsgroup (current-buffer))
7863                art-group))))))
7864       (cond
7865        ((not art-group)
7866         (gnus-message 1 "Couldn't %s article %s: %s"
7867                       (cadr (assq action names)) article
7868                       (nnheader-get-report (car to-method))))
7869        ((eq art-group 'junk)
7870         (when (eq action 'move)
7871           (gnus-summary-mark-article article gnus-canceled-mark)
7872           (gnus-message 4 "Deleted article %s" article)))
7873        (t
7874         (let* ((pto-group (gnus-group-prefixed-name
7875                            (car art-group) to-method))
7876                (entry
7877                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7878                (info (nth 2 entry))
7879                (to-group (gnus-info-group info))
7880                to-marks)
7881           ;; Update the group that has been moved to.
7882           (when (and info
7883                      (memq action '(move copy)))
7884             (unless (member to-group to-groups)
7885               (push to-group to-groups))
7886
7887             (unless (memq article gnus-newsgroup-unreads)
7888               (push 'read to-marks)
7889               (gnus-info-set-read
7890                info (gnus-add-to-range (gnus-info-read info)
7891                                        (list (cdr art-group)))))
7892
7893             ;; See whether the article is to be put in the cache.
7894             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7895                              default-marks
7896                            no-expire-marks))
7897                   (to-article (cdr art-group)))
7898
7899               ;; Enter the article into the cache in the new group,
7900               ;; if that is required.
7901               (when gnus-use-cache
7902                 (gnus-cache-possibly-enter-article
7903                  to-group to-article
7904                  (let ((header (copy-sequence
7905                                 (gnus-summary-article-header article))))
7906                    (mail-header-set-number header to-article)
7907                    header)
7908                  (memq article gnus-newsgroup-marked)
7909                  (memq article gnus-newsgroup-dormant)
7910                  (memq article gnus-newsgroup-unreads)))
7911
7912               (when gnus-preserve-marks
7913                 ;; Copy any marks over to the new group.
7914                 (when (and (equal to-group gnus-newsgroup-name)
7915                            (not (memq article gnus-newsgroup-unreads)))
7916                   ;; Mark this article as read in this group.
7917                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7918                   (setcdr (gnus-active to-group) to-article)
7919                   (setcdr gnus-newsgroup-active to-article))
7920
7921                 (while marks
7922                   (when (memq article (symbol-value
7923                                        (intern (format "gnus-newsgroup-%s"
7924                                                        (caar marks)))))
7925                     (push (cdar marks) to-marks)
7926                     ;; If the other group is the same as this group,
7927                     ;; then we have to add the mark to the list.
7928                     (when (equal to-group gnus-newsgroup-name)
7929                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7930                            (cons to-article
7931                                  (symbol-value
7932                                   (intern (format "gnus-newsgroup-%s"
7933                                                   (caar marks)))))))
7934                     ;; Copy the marks to other group.
7935                     (gnus-add-marked-articles
7936                      to-group (cdar marks) (list to-article) info))
7937                   (setq marks (cdr marks)))
7938
7939                 (gnus-request-set-mark to-group (list (list (list to-article)
7940                                                             'set
7941                                                             to-marks))))
7942
7943               (gnus-dribble-enter
7944                (concat "(gnus-group-set-info '"
7945                        (gnus-prin1-to-string (gnus-get-info to-group))
7946                        ")"))))
7947
7948           ;; Update the Xref header in this article to point to
7949           ;; the new crossposted article we have just created.
7950           (when (eq action 'crosspost)
7951             (save-excursion
7952               (set-buffer copy-buf)
7953               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7954               (nnheader-replace-header "Xref" new-xref)
7955               (gnus-request-replace-article
7956                article gnus-newsgroup-name (current-buffer)))))
7957
7958         ;;;!!!Why is this necessary?
7959         (set-buffer gnus-summary-buffer)
7960
7961         (gnus-summary-goto-subject article)
7962         (when (eq action 'move)
7963           (gnus-summary-mark-article article gnus-canceled-mark))))
7964       (gnus-summary-remove-process-mark article))
7965     ;; Re-activate all groups that have been moved to.
7966     (while to-groups
7967       (save-excursion
7968         (set-buffer gnus-group-buffer)
7969         (when (gnus-group-goto-group (car to-groups) t)
7970           (gnus-group-get-new-news-this-group 1 t))
7971         (pop to-groups)))
7972
7973     (gnus-kill-buffer copy-buf)
7974     (gnus-summary-position-point)
7975     (gnus-set-mode-line 'summary)))
7976
7977 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7978   "Move the current article to a different newsgroup.
7979 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7980 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7981 re-spool using this method."
7982   (interactive "P")
7983   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7984
7985 (defun gnus-summary-crosspost-article (&optional n)
7986   "Crosspost the current article to some other group."
7987   (interactive "P")
7988   (gnus-summary-move-article n nil nil 'crosspost))
7989
7990 (defcustom gnus-summary-respool-default-method nil
7991   "Default method for respooling an article.
7992 If nil, use to the current newsgroup method."
7993   :type '(choice (gnus-select-method :value (nnml ""))
7994                  (const nil))
7995   :group 'gnus-summary-mail)
7996
7997 (defun gnus-summary-respool-article (&optional n method)
7998   "Respool the current article.
7999 The article will be squeezed through the mail spooling process again,
8000 which means that it will be put in some mail newsgroup or other
8001 depending on `nnmail-split-methods'.
8002 If N is a positive number, respool the N next articles.
8003 If N is a negative number, respool the N previous articles.
8004 If N is nil and any articles have been marked with the process mark,
8005 respool those articles instead.
8006
8007 Respooling can be done both from mail groups and \"real\" newsgroups.
8008 In the former case, the articles in question will be moved from the
8009 current group into whatever groups they are destined to.  In the
8010 latter case, they will be copied into the relevant groups."
8011   (interactive
8012    (list current-prefix-arg
8013          (let* ((methods (gnus-methods-using 'respool))
8014                 (methname
8015                  (symbol-name (or gnus-summary-respool-default-method
8016                                   (car (gnus-find-method-for-group
8017                                         gnus-newsgroup-name)))))
8018                 (method
8019                  (gnus-completing-read
8020                   methname "What backend do you want to use when respooling?"
8021                   methods nil t nil 'gnus-mail-method-history))
8022                 ms)
8023            (cond
8024             ((zerop (length (setq ms (gnus-servers-using-backend
8025                                       (intern method)))))
8026              (list (intern method) ""))
8027             ((= 1 (length ms))
8028              (car ms))
8029             (t
8030              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8031                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8032                            ms-alist))))))))
8033   (unless method
8034     (error "No method given for respooling"))
8035   (if (assoc (symbol-name
8036               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8037              (gnus-methods-using 'respool))
8038       (gnus-summary-move-article n nil method)
8039     (gnus-summary-copy-article n nil method)))
8040
8041 (defun gnus-summary-import-article (file)
8042   "Import an arbitrary file into a mail newsgroup."
8043   (interactive "fImport file: ")
8044   (let ((group gnus-newsgroup-name)
8045         (now (current-time))
8046         atts lines)
8047     (unless (gnus-check-backend-function 'request-accept-article group)
8048       (error "%s does not support article importing" group))
8049     (or (file-readable-p file)
8050         (not (file-regular-p file))
8051         (error "Can't read %s" file))
8052     (save-excursion
8053       (set-buffer (gnus-get-buffer-create " *import file*"))
8054       (erase-buffer)
8055       (nnheader-insert-file-contents file)
8056       (goto-char (point-min))
8057       (unless (nnheader-article-p)
8058         ;; This doesn't look like an article, so we fudge some headers.
8059         (setq atts (file-attributes file)
8060               lines (count-lines (point-min) (point-max)))
8061         (insert "From: " (read-string "From: ") "\n"
8062                 "Subject: " (read-string "Subject: ") "\n"
8063                 "Date: " (message-make-date (nth 5 atts))
8064                 "\n"
8065                 "Message-ID: " (message-make-message-id) "\n"
8066                 "Lines: " (int-to-string lines) "\n"
8067                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8068       (gnus-request-accept-article group nil t)
8069       (kill-buffer (current-buffer)))))
8070
8071 (defun gnus-summary-article-posted-p ()
8072   "Say whether the current (mail) article is available from news as well.
8073 This will be the case if the article has both been mailed and posted."
8074   (interactive)
8075   (let ((id (mail-header-references (gnus-summary-article-header)))
8076         (gnus-override-method (car (gnus-refer-article-methods))))
8077     (if (gnus-request-head id "")
8078         (gnus-message 2 "The current message was found on %s"
8079                       gnus-override-method)
8080       (gnus-message 2 "The current message couldn't be found on %s"
8081                     gnus-override-method)
8082       nil)))
8083
8084 (defun gnus-summary-expire-articles (&optional now)
8085   "Expire all articles that are marked as expirable in the current group."
8086   (interactive)
8087   (when (gnus-check-backend-function
8088          'request-expire-articles gnus-newsgroup-name)
8089     ;; This backend supports expiry.
8090     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8091            (expirable (if total
8092                           (progn
8093                             ;; We need to update the info for
8094                             ;; this group for `gnus-list-of-read-articles'
8095                             ;; to give us the right answer.
8096                             (gnus-run-hooks 'gnus-exit-group-hook)
8097                             (gnus-summary-update-info)
8098                             (gnus-list-of-read-articles gnus-newsgroup-name))
8099                         (setq gnus-newsgroup-expirable
8100                               (sort gnus-newsgroup-expirable '<))))
8101            (expiry-wait (if now 'immediate
8102                           (gnus-group-find-parameter
8103                            gnus-newsgroup-name 'expiry-wait)))
8104            (nnmail-expiry-target
8105             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8106                 nnmail-expiry-target))
8107            es)
8108       (when expirable
8109         ;; There are expirable articles in this group, so we run them
8110         ;; through the expiry process.
8111         (gnus-message 6 "Expiring articles...")
8112         (unless (gnus-check-group gnus-newsgroup-name)
8113           (error "Can't open server for %s" gnus-newsgroup-name))
8114         ;; The list of articles that weren't expired is returned.
8115         (save-excursion
8116           (if expiry-wait
8117               (let ((nnmail-expiry-wait-function nil)
8118                     (nnmail-expiry-wait expiry-wait))
8119                 (setq es (gnus-request-expire-articles
8120                           expirable gnus-newsgroup-name)))
8121             (setq es (gnus-request-expire-articles
8122                       expirable gnus-newsgroup-name)))
8123           (unless total
8124             (setq gnus-newsgroup-expirable es))
8125           ;; We go through the old list of expirable, and mark all
8126           ;; really expired articles as nonexistent.
8127           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8128             (let ((gnus-use-cache nil))
8129               (while expirable
8130                 (unless (memq (car expirable) es)
8131                   (when (gnus-data-find (car expirable))
8132                     (gnus-summary-mark-article
8133                      (car expirable) gnus-canceled-mark)))
8134                 (setq expirable (cdr expirable))))))
8135         (gnus-message 6 "Expiring articles...done")))))
8136
8137 (defun gnus-summary-expire-articles-now ()
8138   "Expunge all expirable articles in the current group.
8139 This means that *all* articles that are marked as expirable will be
8140 deleted forever, right now."
8141   (interactive)
8142   (or gnus-expert-user
8143       (gnus-yes-or-no-p
8144        "Are you really, really, really sure you want to delete all these messages? ")
8145       (error "Phew!"))
8146   (gnus-summary-expire-articles t))
8147
8148 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8149 (defun gnus-summary-delete-article (&optional n)
8150   "Delete the N next (mail) articles.
8151 This command actually deletes articles.  This is not a marking
8152 command.  The article will disappear forever from your life, never to
8153 return.
8154 If N is negative, delete backwards.
8155 If N is nil and articles have been marked with the process mark,
8156 delete these instead."
8157   (interactive "P")
8158   (unless (gnus-check-backend-function 'request-expire-articles
8159                                        gnus-newsgroup-name)
8160     (error "The current newsgroup does not support article deletion"))
8161   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8162     (error "Couldn't open server"))
8163   ;; Compute the list of articles to delete.
8164   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8165         not-deleted)
8166     (if (and gnus-novice-user
8167              (not (gnus-yes-or-no-p
8168                    (format "Do you really want to delete %s forever? "
8169                            (if (> (length articles) 1)
8170                                (format "these %s articles" (length articles))
8171                              "this article")))))
8172         ()
8173       ;; Delete the articles.
8174       (setq not-deleted (gnus-request-expire-articles
8175                          articles gnus-newsgroup-name 'force))
8176       (while articles
8177         (gnus-summary-remove-process-mark (car articles))
8178         ;; The backend might not have been able to delete the article
8179         ;; after all.
8180         (unless (memq (car articles) not-deleted)
8181           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8182         (setq articles (cdr articles)))
8183       (when not-deleted
8184         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8185     (gnus-summary-position-point)
8186     (gnus-set-mode-line 'summary)
8187     not-deleted))
8188
8189 (defun gnus-summary-edit-article (&optional force)
8190   "Edit the current article.
8191 This will have permanent effect only in mail groups.
8192 If FORCE is non-nil, allow editing of articles even in read-only
8193 groups."
8194   (interactive "P")
8195   (save-excursion
8196     (set-buffer gnus-summary-buffer)
8197     (let ((mail-parse-charset gnus-newsgroup-charset)
8198           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8199       (gnus-set-global-variables)
8200       (when (and (not force)
8201                  (gnus-group-read-only-p))
8202         (error "The current newsgroup does not support article editing"))
8203       (gnus-summary-show-article t)
8204       (gnus-article-edit-article
8205        'ignore
8206        `(lambda (no-highlight)
8207           (let ((mail-parse-charset ',gnus-newsgroup-charset)
8208                 (message-options message-options)
8209                 (message-options-set-recipient)
8210                 (mail-parse-ignored-charsets
8211                  ',gnus-newsgroup-ignored-charsets))
8212             (gnus-summary-edit-article-done
8213              ,(or (mail-header-references gnus-current-headers) "")
8214              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
8215
8216 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8217
8218 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8219                                                  no-highlight)
8220   "Make edits to the current article permanent."
8221   (interactive)
8222   (save-excursion
8223     ;; The buffer restriction contains the entire article if it exists.
8224     (when (article-goto-body)
8225       (let ((lines (count-lines (point) (point-max)))
8226             (length (- (point-max) (point)))
8227             (case-fold-search t)
8228             (body (copy-marker (point))))
8229         (goto-char (point-min))
8230         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8231           (delete-region (match-beginning 1) (match-end 1))
8232           (insert (number-to-string length)))
8233         (goto-char (point-min))
8234         (when (re-search-forward
8235                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8236           (delete-region (match-beginning 1) (match-end 1))
8237           (insert (number-to-string length)))
8238         (goto-char (point-min))
8239         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8240           (delete-region (match-beginning 1) (match-end 1))
8241           (insert (number-to-string lines))))))
8242   ;; Replace the article.
8243   (let ((buf (current-buffer)))
8244     (with-temp-buffer
8245       (insert-buffer-substring buf)
8246
8247       (if (and (not read-only)
8248                (not (gnus-request-replace-article
8249                      (cdr gnus-article-current) (car gnus-article-current)
8250                      (current-buffer) t)))
8251           (error "Couldn't replace article")
8252         ;; Update the summary buffer.
8253         (if (and references
8254                  (equal (message-tokenize-header references " ")
8255                         (message-tokenize-header
8256                          (or (message-fetch-field "references") "") " ")))
8257             ;; We only have to update this line.
8258             (save-excursion
8259               (save-restriction
8260                 (message-narrow-to-head)
8261                 (let ((head (buffer-string))
8262                       header)
8263                   (with-temp-buffer
8264                     (insert (format "211 %d Article retrieved.\n"
8265                                     (cdr gnus-article-current)))
8266                     (insert head)
8267                     (insert ".\n")
8268                     (let ((nntp-server-buffer (current-buffer)))
8269                       (setq header (car (gnus-get-newsgroup-headers
8270                                          (save-excursion
8271                                            (set-buffer gnus-summary-buffer)
8272                                            gnus-newsgroup-dependencies)
8273                                          t))))
8274                     (save-excursion
8275                       (set-buffer gnus-summary-buffer)
8276                       (gnus-data-set-header
8277                        (gnus-data-find (cdr gnus-article-current))
8278                        header)
8279                       (gnus-summary-update-article-line
8280                        (cdr gnus-article-current) header))))))
8281           ;; Update threads.
8282           (set-buffer (or buffer gnus-summary-buffer))
8283           (gnus-summary-update-article (cdr gnus-article-current)))
8284         ;; Prettify the article buffer again.
8285         (unless no-highlight
8286           (save-excursion
8287             (set-buffer gnus-article-buffer)
8288             ;;;!!! Fix this -- article should be rehighlighted.
8289             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8290             (set-buffer gnus-original-article-buffer)
8291             (gnus-request-article
8292              (cdr gnus-article-current)
8293              (car gnus-article-current) (current-buffer))))
8294         ;; Prettify the summary buffer line.
8295         (when (gnus-visual-p 'summary-highlight 'highlight)
8296           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8297
8298 (defun gnus-summary-edit-wash (key)
8299   "Perform editing command KEY in the article buffer."
8300   (interactive
8301    (list
8302     (progn
8303       (message "%s" (concat (this-command-keys) "- "))
8304       (read-char))))
8305   (message "")
8306   (gnus-summary-edit-article)
8307   (execute-kbd-macro (concat (this-command-keys) key))
8308   (gnus-article-edit-done))
8309
8310 ;;; Respooling
8311
8312 (defun gnus-summary-respool-query (&optional silent trace)
8313   "Query where the respool algorithm would put this article."
8314   (interactive)
8315   (let (gnus-mark-article-hook)
8316     (gnus-summary-select-article)
8317     (save-excursion
8318       (set-buffer gnus-original-article-buffer)
8319       (save-restriction
8320         (message-narrow-to-head)
8321         (let ((groups (nnmail-article-group 'identity trace)))
8322           (unless silent
8323             (if groups
8324                 (message "This message would go to %s"
8325                          (mapconcat 'car groups ", "))
8326               (message "This message would go to no groups"))
8327             groups))))))
8328
8329 (defun gnus-summary-respool-trace ()
8330   "Trace where the respool algorithm would put this article.
8331 Display a buffer showing all fancy splitting patterns which matched."
8332   (interactive)
8333   (gnus-summary-respool-query nil t))
8334
8335 ;; Summary marking commands.
8336
8337 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8338   "Mark articles which has the same subject as read, and then select the next.
8339 If UNMARK is positive, remove any kind of mark.
8340 If UNMARK is negative, tick articles."
8341   (interactive "P")
8342   (when unmark
8343     (setq unmark (prefix-numeric-value unmark)))
8344   (let ((count
8345          (gnus-summary-mark-same-subject
8346           (gnus-summary-article-subject) unmark)))
8347     ;; Select next unread article.  If auto-select-same mode, should
8348     ;; select the first unread article.
8349     (gnus-summary-next-article t (and gnus-auto-select-same
8350                                       (gnus-summary-article-subject)))
8351     (gnus-message 7 "%d article%s marked as %s"
8352                   count (if (= count 1) " is" "s are")
8353                   (if unmark "unread" "read"))))
8354
8355 (defun gnus-summary-kill-same-subject (&optional unmark)
8356   "Mark articles which has the same subject as read.
8357 If UNMARK is positive, remove any kind of mark.
8358 If UNMARK is negative, tick articles."
8359   (interactive "P")
8360   (when unmark
8361     (setq unmark (prefix-numeric-value unmark)))
8362   (let ((count
8363          (gnus-summary-mark-same-subject
8364           (gnus-summary-article-subject) unmark)))
8365     ;; If marked as read, go to next unread subject.
8366     (when (null unmark)
8367       ;; Go to next unread subject.
8368       (gnus-summary-next-subject 1 t))
8369     (gnus-message 7 "%d articles are marked as %s"
8370                   count (if unmark "unread" "read"))))
8371
8372 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8373   "Mark articles with same SUBJECT as read, and return marked number.
8374 If optional argument UNMARK is positive, remove any kinds of marks.
8375 If optional argument UNMARK is negative, mark articles as unread instead."
8376   (let ((count 1))
8377     (save-excursion
8378       (cond
8379        ((null unmark)                   ; Mark as read.
8380         (while (and
8381                 (progn
8382                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8383                   (gnus-summary-show-thread) t)
8384                 (gnus-summary-find-subject subject))
8385           (setq count (1+ count))))
8386        ((> unmark 0)                    ; Tick.
8387         (while (and
8388                 (progn
8389                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8390                   (gnus-summary-show-thread) t)
8391                 (gnus-summary-find-subject subject))
8392           (setq count (1+ count))))
8393        (t                               ; Mark as unread.
8394         (while (and
8395                 (progn
8396                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8397                   (gnus-summary-show-thread) t)
8398                 (gnus-summary-find-subject subject))
8399           (setq count (1+ count)))))
8400       (gnus-set-mode-line 'summary)
8401       ;; Return the number of marked articles.
8402       count)))
8403
8404 (defun gnus-summary-mark-as-processable (n &optional unmark)
8405   "Set the process mark on the next N articles.
8406 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8407 the process mark instead.  The difference between N and the actual
8408 number of articles marked is returned."
8409   (interactive "P")
8410   (if (and (null n) (gnus-region-active-p))
8411       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8412     (setq n (prefix-numeric-value n))
8413     (let ((backward (< n 0))
8414           (n (abs n)))
8415       (while (and
8416               (> n 0)
8417               (if unmark
8418                 (gnus-summary-remove-process-mark
8419                  (gnus-summary-article-number))
8420                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8421               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8422         (setq n (1- n)))
8423       (when (/= 0 n)
8424         (gnus-message 7 "No more articles"))
8425       (gnus-summary-recenter)
8426       (gnus-summary-position-point)
8427       n)))
8428
8429 (defun gnus-summary-unmark-as-processable (n)
8430   "Remove the process mark from the next N articles.
8431 If N is negative, unmark backward instead.  The difference between N and
8432 the actual number of articles unmarked is returned."
8433   (interactive "P")
8434   (gnus-summary-mark-as-processable n t))
8435
8436 (defun gnus-summary-unmark-all-processable ()
8437   "Remove the process mark from all articles."
8438   (interactive)
8439   (save-excursion
8440     (while gnus-newsgroup-processable
8441       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8442   (gnus-summary-position-point))
8443
8444 (defun gnus-summary-add-mark (article type)
8445   "Mark ARTICLE with a mark of TYPE."
8446   (let ((vtype (car (assq type gnus-article-mark-lists)))
8447         var)
8448     (if (not vtype)
8449         (error "No such mark type: %s" type)
8450       (setq var (intern (format "gnus-newsgroup-%s" type)))
8451       (set var (cons article (symbol-value var)))
8452       (if (memq type '(processable cached replied saved))
8453           (gnus-summary-update-secondary-mark article)
8454         ;;; !!! This is bobus.  We should find out what primary
8455         ;;; !!! mark we want to set.
8456         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8457
8458 (defun gnus-summary-mark-as-expirable (n)
8459   "Mark N articles forward as expirable.
8460 If N is negative, mark backward instead.  The difference between N and
8461 the actual number of articles marked is returned."
8462   (interactive "p")
8463   (gnus-summary-mark-forward n gnus-expirable-mark))
8464
8465 (defun gnus-summary-mark-article-as-replied (article)
8466   "Mark ARTICLE replied and update the summary line."
8467   (push article gnus-newsgroup-replied)
8468   (let ((buffer-read-only nil))
8469     (when (gnus-summary-goto-subject article nil t)
8470       (gnus-summary-update-secondary-mark article))))
8471
8472 (defun gnus-summary-set-bookmark (article)
8473   "Set a bookmark in current article."
8474   (interactive (list (gnus-summary-article-number)))
8475   (when (or (not (get-buffer gnus-article-buffer))
8476             (not gnus-current-article)
8477             (not gnus-article-current)
8478             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8479     (error "No current article selected"))
8480   ;; Remove old bookmark, if one exists.
8481   (let ((old (assq article gnus-newsgroup-bookmarks)))
8482     (when old
8483       (setq gnus-newsgroup-bookmarks
8484             (delq old gnus-newsgroup-bookmarks))))
8485   ;; Set the new bookmark, which is on the form
8486   ;; (article-number . line-number-in-body).
8487   (push
8488    (cons article
8489          (save-excursion
8490            (set-buffer gnus-article-buffer)
8491            (count-lines
8492             (min (point)
8493                  (save-excursion
8494                    (goto-char (point-min))
8495                    (search-forward "\n\n" nil t)
8496                    (point)))
8497             (point))))
8498    gnus-newsgroup-bookmarks)
8499   (gnus-message 6 "A bookmark has been added to the current article."))
8500
8501 (defun gnus-summary-remove-bookmark (article)
8502   "Remove the bookmark from the current article."
8503   (interactive (list (gnus-summary-article-number)))
8504   ;; Remove old bookmark, if one exists.
8505   (let ((old (assq article gnus-newsgroup-bookmarks)))
8506     (if old
8507         (progn
8508           (setq gnus-newsgroup-bookmarks
8509                 (delq old gnus-newsgroup-bookmarks))
8510           (gnus-message 6 "Removed bookmark."))
8511       (gnus-message 6 "No bookmark in current article."))))
8512
8513 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8514 (defun gnus-summary-mark-as-dormant (n)
8515   "Mark N articles forward as dormant.
8516 If N is negative, mark backward instead.  The difference between N and
8517 the actual number of articles marked is returned."
8518   (interactive "p")
8519   (gnus-summary-mark-forward n gnus-dormant-mark))
8520
8521 (defun gnus-summary-set-process-mark (article)
8522   "Set the process mark on ARTICLE and update the summary line."
8523   (setq gnus-newsgroup-processable
8524         (cons article
8525               (delq article gnus-newsgroup-processable)))
8526   (when (gnus-summary-goto-subject article)
8527     (gnus-summary-show-thread)
8528     (gnus-summary-goto-subject article)
8529     (gnus-summary-update-secondary-mark article)))
8530
8531 (defun gnus-summary-remove-process-mark (article)
8532   "Remove the process mark from ARTICLE and update the summary line."
8533   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8534   (when (gnus-summary-goto-subject article)
8535     (gnus-summary-show-thread)
8536     (gnus-summary-goto-subject article)
8537     (gnus-summary-update-secondary-mark article)))
8538
8539 (defun gnus-summary-set-saved-mark (article)
8540   "Set the process mark on ARTICLE and update the summary line."
8541   (push article gnus-newsgroup-saved)
8542   (when (gnus-summary-goto-subject article)
8543     (gnus-summary-update-secondary-mark article)))
8544
8545 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8546   "Mark N articles as read forwards.
8547 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8548 The difference between N and the actual number of articles marked is
8549 returned.
8550 Iff NO-EXPIRE, auto-expiry will be inhibited."
8551   (interactive "p")
8552   (gnus-summary-show-thread)
8553   (let ((backward (< n 0))
8554         (gnus-summary-goto-unread
8555          (and gnus-summary-goto-unread
8556               (not (eq gnus-summary-goto-unread 'never))
8557               (not (memq mark (list gnus-unread-mark
8558                                     gnus-ticked-mark gnus-dormant-mark)))))
8559         (n (abs n))
8560         (mark (or mark gnus-del-mark)))
8561     (while (and (> n 0)
8562                 (gnus-summary-mark-article nil mark no-expire)
8563                 (zerop (gnus-summary-next-subject
8564                         (if backward -1 1)
8565                         (and gnus-summary-goto-unread
8566                              (not (eq gnus-summary-goto-unread 'never)))
8567                         t)))
8568       (setq n (1- n)))
8569     (when (/= 0 n)
8570       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8571     (gnus-summary-recenter)
8572     (gnus-summary-position-point)
8573     (gnus-set-mode-line 'summary)
8574     n))
8575
8576 (defun gnus-summary-mark-article-as-read (mark)
8577   "Mark the current article quickly as read with MARK."
8578   (let ((article (gnus-summary-article-number)))
8579     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8580     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8581     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8582     (push (cons article mark) gnus-newsgroup-reads)
8583     ;; Possibly remove from cache, if that is used.
8584     (when gnus-use-cache
8585       (gnus-cache-enter-remove-article article))
8586     ;; Allow the backend to change the mark.
8587     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8588     ;; Check for auto-expiry.
8589     (when (and gnus-newsgroup-auto-expire
8590                (memq mark gnus-auto-expirable-marks))
8591       (setq mark gnus-expirable-mark)
8592       ;; Let the backend know about the mark change.
8593       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8594       (push article gnus-newsgroup-expirable))
8595     ;; Set the mark in the buffer.
8596     (gnus-summary-update-mark mark 'unread)
8597     t))
8598
8599 (defun gnus-summary-mark-article-as-unread (mark)
8600   "Mark the current article quickly as unread with MARK."
8601   (let* ((article (gnus-summary-article-number))
8602          (old-mark (gnus-summary-article-mark article)))
8603     ;; Allow the backend to change the mark.
8604     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8605     (if (eq mark old-mark)
8606         t
8607       (if (<= article 0)
8608           (progn
8609             (gnus-error 1 "Can't mark negative article numbers")
8610             nil)
8611         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8612         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8613         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8614         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8615         (cond ((= mark gnus-ticked-mark)
8616                (push article gnus-newsgroup-marked))
8617               ((= mark gnus-dormant-mark)
8618                (push article gnus-newsgroup-dormant))
8619               (t
8620                (push article gnus-newsgroup-unreads)))
8621         (gnus-pull article gnus-newsgroup-reads)
8622
8623         ;; See whether the article is to be put in the cache.
8624         (and gnus-use-cache
8625              (vectorp (gnus-summary-article-header article))
8626              (save-excursion
8627                (gnus-cache-possibly-enter-article
8628                 gnus-newsgroup-name article
8629                 (gnus-summary-article-header article)
8630                 (= mark gnus-ticked-mark)
8631                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8632
8633         ;; Fix the mark.
8634         (gnus-summary-update-mark mark 'unread)
8635         t))))
8636
8637 (defun gnus-summary-mark-article (&optional article mark no-expire)
8638   "Mark ARTICLE with MARK.  MARK can be any character.
8639 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8640 `??' (dormant) and `?E' (expirable).
8641 If MARK is nil, then the default character `?r' is used.
8642 If ARTICLE is nil, then the article on the current line will be
8643 marked.
8644 Iff NO-EXPIRE, auto-expiry will be inhibited."
8645   ;; The mark might be a string.
8646   (when (stringp mark)
8647     (setq mark (aref mark 0)))
8648   ;; If no mark is given, then we check auto-expiring.
8649   (when (null mark)
8650     (setq mark gnus-del-mark))
8651   (when (and (not no-expire)
8652              gnus-newsgroup-auto-expire
8653              (memq mark gnus-auto-expirable-marks))
8654     (setq mark gnus-expirable-mark))
8655   (let ((article (or article (gnus-summary-article-number)))
8656         (old-mark (gnus-summary-article-mark article)))
8657     ;; Allow the backend to change the mark.
8658     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8659     (if (eq mark old-mark)
8660         t
8661       (unless article
8662         (error "No article on current line"))
8663       (if (not (if (or (= mark gnus-unread-mark)
8664                        (= mark gnus-ticked-mark)
8665                        (= mark gnus-dormant-mark))
8666                    (gnus-mark-article-as-unread article mark)
8667                  (gnus-mark-article-as-read article mark)))
8668           t
8669         ;; See whether the article is to be put in the cache.
8670         (and gnus-use-cache
8671              (not (= mark gnus-canceled-mark))
8672              (vectorp (gnus-summary-article-header article))
8673              (save-excursion
8674                (gnus-cache-possibly-enter-article
8675                 gnus-newsgroup-name article
8676                 (gnus-summary-article-header article)
8677                 (= mark gnus-ticked-mark)
8678                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8679
8680         (when (gnus-summary-goto-subject article nil t)
8681           (let ((buffer-read-only nil))
8682             (gnus-summary-show-thread)
8683             ;; Fix the mark.
8684             (gnus-summary-update-mark mark 'unread)
8685             t))))))
8686
8687 (defun gnus-summary-update-secondary-mark (article)
8688   "Update the secondary (read, process, cache) mark."
8689   (gnus-summary-update-mark
8690    (cond ((memq article gnus-newsgroup-processable)
8691           gnus-process-mark)
8692          ((memq article gnus-newsgroup-cached)
8693           gnus-cached-mark)
8694          ((memq article gnus-newsgroup-replied)
8695           gnus-replied-mark)
8696          ((memq article gnus-newsgroup-saved)
8697           gnus-saved-mark)
8698          (t gnus-no-mark))
8699    'replied)
8700   (when (gnus-visual-p 'summary-highlight 'highlight)
8701     (gnus-run-hooks 'gnus-summary-update-hook))
8702   t)
8703
8704 (defun gnus-summary-update-mark (mark type)
8705   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8706         (buffer-read-only nil))
8707     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8708     (when forward
8709       (when (looking-at "\r")
8710         (incf forward))
8711       (when (<= (+ forward (point)) (point-max))
8712         ;; Go to the right position on the line.
8713         (goto-char (+ forward (point)))
8714         ;; Replace the old mark with the new mark.
8715         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8716         ;; Optionally update the marks by some user rule.
8717         (when (eq type 'unread)
8718           (gnus-data-set-mark
8719            (gnus-data-find (gnus-summary-article-number)) mark)
8720           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8721
8722 (defun gnus-mark-article-as-read (article &optional mark)
8723   "Enter ARTICLE in the pertinent lists and remove it from others."
8724   ;; Make the article expirable.
8725   (let ((mark (or mark gnus-del-mark)))
8726     (if (= mark gnus-expirable-mark)
8727         (push article gnus-newsgroup-expirable)
8728       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8729     ;; Remove from unread and marked lists.
8730     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8731     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8732     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8733     (push (cons article mark) gnus-newsgroup-reads)
8734     ;; Possibly remove from cache, if that is used.
8735     (when gnus-use-cache
8736       (gnus-cache-enter-remove-article article))
8737     t))
8738
8739 (defun gnus-mark-article-as-unread (article &optional mark)
8740   "Enter ARTICLE in the pertinent lists and remove it from others."
8741   (let ((mark (or mark gnus-ticked-mark)))
8742     (if (<= article 0)
8743         (progn
8744           (gnus-error 1 "Can't mark negative article numbers")
8745           nil)
8746       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8747             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8748             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8749             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8750
8751       ;; Unsuppress duplicates?
8752       (when gnus-suppress-duplicates
8753         (gnus-dup-unsuppress-article article))
8754
8755       (cond ((= mark gnus-ticked-mark)
8756              (push article gnus-newsgroup-marked))
8757             ((= mark gnus-dormant-mark)
8758              (push article gnus-newsgroup-dormant))
8759             (t
8760              (push article gnus-newsgroup-unreads)))
8761       (gnus-pull article gnus-newsgroup-reads)
8762       t)))
8763
8764 (defalias 'gnus-summary-mark-as-unread-forward
8765   'gnus-summary-tick-article-forward)
8766 (make-obsolete 'gnus-summary-mark-as-unread-forward
8767                'gnus-summary-tick-article-forward)
8768 (defun gnus-summary-tick-article-forward (n)
8769   "Tick N articles forwards.
8770 If N is negative, tick backwards instead.
8771 The difference between N and the number of articles ticked is returned."
8772   (interactive "p")
8773   (gnus-summary-mark-forward n gnus-ticked-mark))
8774
8775 (defalias 'gnus-summary-mark-as-unread-backward
8776   'gnus-summary-tick-article-backward)
8777 (make-obsolete 'gnus-summary-mark-as-unread-backward
8778                'gnus-summary-tick-article-backward)
8779 (defun gnus-summary-tick-article-backward (n)
8780   "Tick N articles backwards.
8781 The difference between N and the number of articles ticked is returned."
8782   (interactive "p")
8783   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8784
8785 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8786 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8787 (defun gnus-summary-tick-article (&optional article clear-mark)
8788   "Mark current article as unread.
8789 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8790 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8791   (interactive)
8792   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8793                                        gnus-ticked-mark)))
8794
8795 (defun gnus-summary-mark-as-read-forward (n)
8796   "Mark N articles as read forwards.
8797 If N is negative, mark backwards instead.
8798 The difference between N and the actual number of articles marked is
8799 returned."
8800   (interactive "p")
8801   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8802
8803 (defun gnus-summary-mark-as-read-backward (n)
8804   "Mark the N articles as read backwards.
8805 The difference between N and the actual number of articles marked is
8806 returned."
8807   (interactive "p")
8808   (gnus-summary-mark-forward
8809    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8810
8811 (defun gnus-summary-mark-as-read (&optional article mark)
8812   "Mark current article as read.
8813 ARTICLE specifies the article to be marked as read.
8814 MARK specifies a string to be inserted at the beginning of the line."
8815   (gnus-summary-mark-article article mark))
8816
8817 (defun gnus-summary-clear-mark-forward (n)
8818   "Clear marks from N articles forward.
8819 If N is negative, clear backward instead.
8820 The difference between N and the number of marks cleared is returned."
8821   (interactive "p")
8822   (gnus-summary-mark-forward n gnus-unread-mark))
8823
8824 (defun gnus-summary-clear-mark-backward (n)
8825   "Clear marks from N articles backward.
8826 The difference between N and the number of marks cleared is returned."
8827   (interactive "p")
8828   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8829
8830 (defun gnus-summary-mark-unread-as-read ()
8831   "Intended to be used by `gnus-summary-mark-article-hook'."
8832   (when (memq gnus-current-article gnus-newsgroup-unreads)
8833     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8834
8835 (defun gnus-summary-mark-read-and-unread-as-read ()
8836   "Intended to be used by `gnus-summary-mark-article-hook'."
8837   (let ((mark (gnus-summary-article-mark)))
8838     (when (or (gnus-unread-mark-p mark)
8839               (gnus-read-mark-p mark))
8840       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8841
8842 (defun gnus-summary-mark-unread-as-ticked ()
8843    "Intended to be used by `gnus-summary-mark-article-hook'."
8844   (when (memq gnus-current-article gnus-newsgroup-unreads)
8845     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8846
8847 (defun gnus-summary-mark-region-as-read (point mark all)
8848   "Mark all unread articles between point and mark as read.
8849 If given a prefix, mark all articles between point and mark as read,
8850 even ticked and dormant ones."
8851   (interactive "r\nP")
8852   (save-excursion
8853     (let (article)
8854       (goto-char point)
8855       (beginning-of-line)
8856       (while (and
8857               (< (point) mark)
8858               (progn
8859                 (when (or all
8860                           (memq (setq article (gnus-summary-article-number))
8861                                 gnus-newsgroup-unreads))
8862                   (gnus-summary-mark-article article gnus-del-mark))
8863                 t)
8864               (gnus-summary-find-next))))))
8865
8866 (defun gnus-summary-mark-below (score mark)
8867   "Mark articles with score less than SCORE with MARK."
8868   (interactive "P\ncMark: ")
8869   (setq score (if score
8870                   (prefix-numeric-value score)
8871                 (or gnus-summary-default-score 0)))
8872   (save-excursion
8873     (set-buffer gnus-summary-buffer)
8874     (goto-char (point-min))
8875     (while
8876         (progn
8877           (and (< (gnus-summary-article-score) score)
8878                (gnus-summary-mark-article nil mark))
8879           (gnus-summary-find-next)))))
8880
8881 (defun gnus-summary-kill-below (&optional score)
8882   "Mark articles with score below SCORE as read."
8883   (interactive "P")
8884   (gnus-summary-mark-below score gnus-killed-mark))
8885
8886 (defun gnus-summary-clear-above (&optional score)
8887   "Clear all marks from articles with score above SCORE."
8888   (interactive "P")
8889   (gnus-summary-mark-above score gnus-unread-mark))
8890
8891 (defun gnus-summary-tick-above (&optional score)
8892   "Tick all articles with score above SCORE."
8893   (interactive "P")
8894   (gnus-summary-mark-above score gnus-ticked-mark))
8895
8896 (defun gnus-summary-mark-above (score mark)
8897   "Mark articles with score over SCORE with MARK."
8898   (interactive "P\ncMark: ")
8899   (setq score (if score
8900                   (prefix-numeric-value score)
8901                 (or gnus-summary-default-score 0)))
8902   (save-excursion
8903     (set-buffer gnus-summary-buffer)
8904     (goto-char (point-min))
8905     (while (and (progn
8906                   (when (> (gnus-summary-article-score) score)
8907                     (gnus-summary-mark-article nil mark))
8908                   t)
8909                 (gnus-summary-find-next)))))
8910
8911 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8912 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8913 (defun gnus-summary-limit-include-expunged (&optional no-error)
8914   "Display all the hidden articles that were expunged for low scores."
8915   (interactive)
8916   (let ((buffer-read-only nil))
8917     (let ((scored gnus-newsgroup-scored)
8918           headers h)
8919       (while scored
8920         (unless (gnus-number-to-header (caar scored))
8921           (and (setq h (gnus-summary-article-header (caar scored)))
8922                (< (cdar scored) gnus-summary-expunge-below)
8923                (push h headers)))
8924         (setq scored (cdr scored)))
8925       (if (not headers)
8926           (when (not no-error)
8927             (error "No expunged articles hidden"))
8928         (goto-char (point-min))
8929         (gnus-summary-prepare-unthreaded (nreverse headers))
8930         (goto-char (point-min))
8931         (gnus-summary-position-point)
8932         t))))
8933
8934 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8935   "Mark all unread articles in this newsgroup as read.
8936 If prefix argument ALL is non-nil, ticked and dormant articles will
8937 also be marked as read.
8938 If QUIETLY is non-nil, no questions will be asked.
8939 If TO-HERE is non-nil, it should be a point in the buffer.  All
8940 articles before this point will be marked as read.
8941 Note that this function will only catch up the unread article
8942 in the current summary buffer limitation.
8943 The number of articles marked as read is returned."
8944   (interactive "P")
8945   (prog1
8946       (save-excursion
8947         (when (or quietly
8948                   (not gnus-interactive-catchup) ;Without confirmation?
8949                   gnus-expert-user
8950                   (gnus-y-or-n-p
8951                    (if all
8952                        "Mark absolutely all articles as read? "
8953                      "Mark all unread articles as read? ")))
8954           (if (and not-mark
8955                    (not gnus-newsgroup-adaptive)
8956                    (not gnus-newsgroup-auto-expire)
8957                    (not gnus-suppress-duplicates)
8958                    (or (not gnus-use-cache)
8959                        (eq gnus-use-cache 'passive)))
8960               (progn
8961                 (when all
8962                   (setq gnus-newsgroup-marked nil
8963                         gnus-newsgroup-dormant nil))
8964                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8965             ;; We actually mark all articles as canceled, which we
8966             ;; have to do when using auto-expiry or adaptive scoring.
8967             (gnus-summary-show-all-threads)
8968             (when (gnus-summary-first-subject (not all) t)
8969               (while (and
8970                       (if to-here (< (point) to-here) t)
8971                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8972                       (gnus-summary-find-next (not all) nil nil t))))
8973             (gnus-set-mode-line 'summary))
8974           t))
8975     (gnus-summary-position-point)))
8976
8977 (defun gnus-summary-catchup-to-here (&optional all)
8978   "Mark all unticked articles before the current one as read.
8979 If ALL is non-nil, also mark ticked and dormant articles as read."
8980   (interactive "P")
8981   (save-excursion
8982     (gnus-save-hidden-threads
8983       (let ((beg (point)))
8984         ;; We check that there are unread articles.
8985         (when (or all (gnus-summary-find-prev))
8986           (gnus-summary-catchup all t beg)))))
8987   (gnus-summary-position-point))
8988
8989 (defun gnus-summary-catchup-all (&optional quietly)
8990   "Mark all articles in this newsgroup as read."
8991   (interactive "P")
8992   (gnus-summary-catchup t quietly))
8993
8994 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8995   "Mark all unread articles in this group as read, then exit.
8996 If prefix argument ALL is non-nil, all articles are marked as read."
8997   (interactive "P")
8998   (when (gnus-summary-catchup all quietly nil 'fast)
8999     ;; Select next newsgroup or exit.
9000     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9001              (eq gnus-auto-select-next 'quietly))
9002         (gnus-summary-next-group nil)
9003       (gnus-summary-exit))))
9004
9005 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9006   "Mark all articles in this newsgroup as read, and then exit."
9007   (interactive "P")
9008   (gnus-summary-catchup-and-exit t quietly))
9009
9010 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9011   "Mark all articles in this group as read and select the next group.
9012 If given a prefix, mark all articles, unread as well as ticked, as
9013 read."
9014   (interactive "P")
9015   (save-excursion
9016     (gnus-summary-catchup all))
9017   (gnus-summary-next-group))
9018
9019 ;;;
9020 ;;; with article
9021 ;;;
9022
9023 (defmacro gnus-with-article (article &rest forms)
9024   "Select ARTICLE and perform FORMS in the original article buffer.
9025 Then replace the article with the result."
9026   `(progn
9027      ;; We don't want the article to be marked as read.
9028      (let (gnus-mark-article-hook)
9029        (gnus-summary-select-article t t nil ,article))
9030      (set-buffer gnus-original-article-buffer)
9031      ,@forms
9032      (if (not (gnus-check-backend-function
9033                'request-replace-article (car gnus-article-current)))
9034          (gnus-message 5 "Read-only group; not replacing")
9035        (unless (gnus-request-replace-article
9036                 ,article (car gnus-article-current)
9037                 (current-buffer) t)
9038          (error "Couldn't replace article")))
9039      ;; The cache and backlog have to be flushed somewhat.
9040      (when gnus-keep-backlog
9041        (gnus-backlog-remove-article
9042         (car gnus-article-current) (cdr gnus-article-current)))
9043      (when gnus-use-cache
9044        (gnus-cache-update-article
9045         (car gnus-article-current) (cdr gnus-article-current)))))
9046
9047 (put 'gnus-with-article 'lisp-indent-function 1)
9048 (put 'gnus-with-article 'edebug-form-spec '(form body))
9049
9050 ;; Thread-based commands.
9051
9052 (defun gnus-summary-articles-in-thread (&optional article)
9053   "Return a list of all articles in the current thread.
9054 If ARTICLE is non-nil, return all articles in the thread that starts
9055 with that article."
9056   (let* ((article (or article (gnus-summary-article-number)))
9057          (data (gnus-data-find-list article))
9058          (top-level (gnus-data-level (car data)))
9059          (top-subject
9060           (cond ((null gnus-thread-operation-ignore-subject)
9061                  (gnus-simplify-subject-re
9062                   (mail-header-subject (gnus-data-header (car data)))))
9063                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9064                  (gnus-simplify-subject-fuzzy
9065                   (mail-header-subject (gnus-data-header (car data)))))
9066                 (t nil)))
9067          (end-point (save-excursion
9068                       (if (gnus-summary-go-to-next-thread)
9069                           (point) (point-max))))
9070          articles)
9071     (while (and data
9072                 (< (gnus-data-pos (car data)) end-point))
9073       (when (or (not top-subject)
9074                 (string= top-subject
9075                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9076                              (gnus-simplify-subject-fuzzy
9077                               (mail-header-subject
9078                                (gnus-data-header (car data))))
9079                            (gnus-simplify-subject-re
9080                             (mail-header-subject
9081                              (gnus-data-header (car data)))))))
9082         (push (gnus-data-number (car data)) articles))
9083       (unless (and (setq data (cdr data))
9084                    (> (gnus-data-level (car data)) top-level))
9085         (setq data nil)))
9086     ;; Return the list of articles.
9087     (nreverse articles)))
9088
9089 (defun gnus-summary-rethread-current ()
9090   "Rethread the thread the current article is part of."
9091   (interactive)
9092   (let* ((gnus-show-threads t)
9093          (article (gnus-summary-article-number))
9094          (id (mail-header-id (gnus-summary-article-header)))
9095          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9096     (unless id
9097       (error "No article on the current line"))
9098     (gnus-rebuild-thread id)
9099     (gnus-summary-goto-subject article)))
9100
9101 (defun gnus-summary-reparent-thread ()
9102   "Make the current article child of the marked (or previous) article.
9103
9104 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9105 is non-nil or the Subject: of both articles are the same."
9106   (interactive)
9107   (unless (not (gnus-group-read-only-p))
9108     (error "The current newsgroup does not support article editing"))
9109   (unless (<= (length gnus-newsgroup-processable) 1)
9110     (error "No more than one article may be marked"))
9111   (save-window-excursion
9112     (let ((gnus-article-buffer " *reparent*")
9113           (current-article (gnus-summary-article-number))
9114           ;; First grab the marked article, otherwise one line up.
9115           (parent-article (if (not (null gnus-newsgroup-processable))
9116                               (car gnus-newsgroup-processable)
9117                             (save-excursion
9118                               (if (eq (forward-line -1) 0)
9119                                   (gnus-summary-article-number)
9120                                 (error "Beginning of summary buffer"))))))
9121       (unless (not (eq current-article parent-article))
9122         (error "An article may not be self-referential"))
9123       (let ((message-id (mail-header-id
9124                          (gnus-summary-article-header parent-article))))
9125         (unless (and message-id (not (equal message-id "")))
9126           (error "No message-id in desired parent"))
9127         (gnus-with-article current-article
9128           (save-restriction
9129             (goto-char (point-min))
9130             (message-narrow-to-head)
9131             (if (re-search-forward "^References: " nil t)
9132                 (progn
9133                   (re-search-forward "^[^ \t]" nil t)
9134                   (forward-line -1)
9135                   (end-of-line)
9136                   (insert " " message-id))
9137               (insert "References: " message-id "\n"))))
9138         (set-buffer gnus-summary-buffer)
9139         (gnus-summary-unmark-all-processable)
9140         (gnus-summary-update-article current-article)
9141         (gnus-summary-rethread-current)
9142         (gnus-message 3 "Article %d is now the child of article %d"
9143                       current-article parent-article)))))
9144
9145 (defun gnus-summary-toggle-threads (&optional arg)
9146   "Toggle showing conversation threads.
9147 If ARG is positive number, turn showing conversation threads on."
9148   (interactive "P")
9149   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9150     (setq gnus-show-threads
9151           (if (null arg) (not gnus-show-threads)
9152             (> (prefix-numeric-value arg) 0)))
9153     (gnus-summary-prepare)
9154     (gnus-summary-goto-subject current)
9155     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9156     (gnus-summary-position-point)))
9157
9158 (defun gnus-summary-show-all-threads ()
9159   "Show all threads."
9160   (interactive)
9161   (save-excursion
9162     (let ((buffer-read-only nil))
9163       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9164   (gnus-summary-position-point))
9165
9166 (defun gnus-summary-show-thread ()
9167   "Show thread subtrees.
9168 Returns nil if no thread was there to be shown."
9169   (interactive)
9170   (let ((buffer-read-only nil)
9171         (orig (point))
9172         ;; first goto end then to beg, to have point at beg after let
9173         (end (progn (end-of-line) (point)))
9174         (beg (progn (beginning-of-line) (point))))
9175     (prog1
9176         ;; Any hidden lines here?
9177         (search-forward "\r" end t)
9178       (subst-char-in-region beg end ?\^M ?\n t)
9179       (goto-char orig)
9180       (gnus-summary-position-point))))
9181
9182 (defun gnus-summary-hide-all-threads ()
9183   "Hide all thread subtrees."
9184   (interactive)
9185   (save-excursion
9186     (goto-char (point-min))
9187     (gnus-summary-hide-thread)
9188     (while (zerop (gnus-summary-next-thread 1 t))
9189       (gnus-summary-hide-thread)))
9190   (gnus-summary-position-point))
9191
9192 (defun gnus-summary-hide-thread ()
9193   "Hide thread subtrees.
9194 Returns nil if no threads were there to be hidden."
9195   (interactive)
9196   (let ((buffer-read-only nil)
9197         (start (point))
9198         (article (gnus-summary-article-number)))
9199     (goto-char start)
9200     ;; Go forward until either the buffer ends or the subthread
9201     ;; ends.
9202     (when (and (not (eobp))
9203                (or (zerop (gnus-summary-next-thread 1 t))
9204                    (goto-char (point-max))))
9205       (prog1
9206           (if (and (> (point) start)
9207                    (search-backward "\n" start t))
9208               (progn
9209                 (subst-char-in-region start (point) ?\n ?\^M)
9210                 (gnus-summary-goto-subject article))
9211             (goto-char start)
9212             nil)))))
9213
9214 (defun gnus-summary-go-to-next-thread (&optional previous)
9215   "Go to the same level (or less) next thread.
9216 If PREVIOUS is non-nil, go to previous thread instead.
9217 Return the article number moved to, or nil if moving was impossible."
9218   (let ((level (gnus-summary-thread-level))
9219         (way (if previous -1 1))
9220         (beg (point)))
9221     (forward-line way)
9222     (while (and (not (eobp))
9223                 (< level (gnus-summary-thread-level)))
9224       (forward-line way))
9225     (if (eobp)
9226         (progn
9227           (goto-char beg)
9228           nil)
9229       (setq beg (point))
9230       (prog1
9231           (gnus-summary-article-number)
9232         (goto-char beg)))))
9233
9234 (defun gnus-summary-next-thread (n &optional silent)
9235   "Go to the same level next N'th thread.
9236 If N is negative, search backward instead.
9237 Returns the difference between N and the number of skips actually
9238 done.
9239
9240 If SILENT, don't output messages."
9241   (interactive "p")
9242   (let ((backward (< n 0))
9243         (n (abs n)))
9244     (while (and (> n 0)
9245                 (gnus-summary-go-to-next-thread backward))
9246       (decf n))
9247     (unless silent
9248       (gnus-summary-position-point))
9249     (when (and (not silent) (/= 0 n))
9250       (gnus-message 7 "No more threads"))
9251     n))
9252
9253 (defun gnus-summary-prev-thread (n)
9254   "Go to the same level previous N'th thread.
9255 Returns the difference between N and the number of skips actually
9256 done."
9257   (interactive "p")
9258   (gnus-summary-next-thread (- n)))
9259
9260 (defun gnus-summary-go-down-thread ()
9261   "Go down one level in the current thread."
9262   (let ((children (gnus-summary-article-children)))
9263     (when children
9264       (gnus-summary-goto-subject (car children)))))
9265
9266 (defun gnus-summary-go-up-thread ()
9267   "Go up one level in the current thread."
9268   (let ((parent (gnus-summary-article-parent)))
9269     (when parent
9270       (gnus-summary-goto-subject parent))))
9271
9272 (defun gnus-summary-down-thread (n)
9273   "Go down thread N steps.
9274 If N is negative, go up instead.
9275 Returns the difference between N and how many steps down that were
9276 taken."
9277   (interactive "p")
9278   (let ((up (< n 0))
9279         (n (abs n)))
9280     (while (and (> n 0)
9281                 (if up (gnus-summary-go-up-thread)
9282                   (gnus-summary-go-down-thread)))
9283       (setq n (1- n)))
9284     (gnus-summary-position-point)
9285     (when (/= 0 n)
9286       (gnus-message 7 "Can't go further"))
9287     n))
9288
9289 (defun gnus-summary-up-thread (n)
9290   "Go up thread N steps.
9291 If N is negative, go up instead.
9292 Returns the difference between N and how many steps down that were
9293 taken."
9294   (interactive "p")
9295   (gnus-summary-down-thread (- n)))
9296
9297 (defun gnus-summary-top-thread ()
9298   "Go to the top of the thread."
9299   (interactive)
9300   (while (gnus-summary-go-up-thread))
9301   (gnus-summary-article-number))
9302
9303 (defun gnus-summary-kill-thread (&optional unmark)
9304   "Mark articles under current thread as read.
9305 If the prefix argument is positive, remove any kinds of marks.
9306 If the prefix argument is negative, tick articles instead."
9307   (interactive "P")
9308   (when unmark
9309     (setq unmark (prefix-numeric-value unmark)))
9310   (let ((articles (gnus-summary-articles-in-thread)))
9311     (save-excursion
9312       ;; Expand the thread.
9313       (gnus-summary-show-thread)
9314       ;; Mark all the articles.
9315       (while articles
9316         (gnus-summary-goto-subject (car articles))
9317         (cond ((null unmark)
9318                (gnus-summary-mark-article-as-read gnus-killed-mark))
9319               ((> unmark 0)
9320                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9321               (t
9322                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9323         (setq articles (cdr articles))))
9324     ;; Hide killed subtrees.
9325     (and (null unmark)
9326          gnus-thread-hide-killed
9327          (gnus-summary-hide-thread))
9328     ;; If marked as read, go to next unread subject.
9329     (when (null unmark)
9330       ;; Go to next unread subject.
9331       (gnus-summary-next-subject 1 t)))
9332   (gnus-set-mode-line 'summary))
9333
9334 ;; Summary sorting commands
9335
9336 (defun gnus-summary-sort-by-number (&optional reverse)
9337   "Sort the summary buffer by article number.
9338 Argument REVERSE means reverse order."
9339   (interactive "P")
9340   (gnus-summary-sort 'number reverse))
9341
9342 (defun gnus-summary-sort-by-author (&optional reverse)
9343   "Sort the summary buffer by author name alphabetically.
9344 If `case-fold-search' is non-nil, case of letters is ignored.
9345 Argument REVERSE means reverse order."
9346   (interactive "P")
9347   (gnus-summary-sort 'author reverse))
9348
9349 (defun gnus-summary-sort-by-subject (&optional reverse)
9350   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9351 If `case-fold-search' is non-nil, case of letters is ignored.
9352 Argument REVERSE means reverse order."
9353   (interactive "P")
9354   (gnus-summary-sort 'subject reverse))
9355
9356 (defun gnus-summary-sort-by-date (&optional reverse)
9357   "Sort the summary buffer by date.
9358 Argument REVERSE means reverse order."
9359   (interactive "P")
9360   (gnus-summary-sort 'date reverse))
9361
9362 (defun gnus-summary-sort-by-score (&optional reverse)
9363   "Sort the summary buffer by score.
9364 Argument REVERSE means reverse order."
9365   (interactive "P")
9366   (gnus-summary-sort 'score reverse))
9367
9368 (defun gnus-summary-sort-by-lines (&optional reverse)
9369   "Sort the summary buffer by the number of lines.
9370 Argument REVERSE means reverse order."
9371   (interactive "P")
9372   (gnus-summary-sort 'lines reverse))
9373
9374 (defun gnus-summary-sort-by-chars (&optional reverse)
9375   "Sort the summary buffer by article length.
9376 Argument REVERSE means reverse order."
9377   (interactive "P")
9378   (gnus-summary-sort 'chars reverse))
9379
9380 (defun gnus-summary-sort (predicate reverse)
9381   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9382   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9383          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9384          (gnus-thread-sort-functions
9385           (if (not reverse)
9386               thread
9387             `(lambda (t1 t2)
9388                (,thread t2 t1))))
9389          (gnus-sort-gathered-threads-function
9390           gnus-thread-sort-functions)
9391          (gnus-article-sort-functions
9392           (if (not reverse)
9393               article
9394             `(lambda (t1 t2)
9395                (,article t2 t1))))
9396          (buffer-read-only)
9397          (gnus-summary-prepare-hook nil))
9398     ;; We do the sorting by regenerating the threads.
9399     (gnus-summary-prepare)
9400     ;; Hide subthreads if needed.
9401     (when (and gnus-show-threads gnus-thread-hide-subtree)
9402       (gnus-summary-hide-all-threads))))
9403
9404 ;; Summary saving commands.
9405
9406 (defun gnus-summary-save-article (&optional n not-saved)
9407   "Save the current article using the default saver function.
9408 If N is a positive number, save the N next articles.
9409 If N is a negative number, save the N previous articles.
9410 If N is nil and any articles have been marked with the process mark,
9411 save those articles instead.
9412 The variable `gnus-default-article-saver' specifies the saver function."
9413   (interactive "P")
9414   (let* ((articles (gnus-summary-work-articles n))
9415          (save-buffer (save-excursion
9416                         (nnheader-set-temp-buffer " *Gnus Save*")))
9417          (num (length articles))
9418          header file)
9419     (dolist (article articles)
9420       (setq header (gnus-summary-article-header article))
9421       (if (not (vectorp header))
9422           ;; This is a pseudo-article.
9423           (if (assq 'name header)
9424               (gnus-copy-file (cdr (assq 'name header)))
9425             (gnus-message 1 "Article %d is unsaveable" article))
9426         ;; This is a real article.
9427         (save-window-excursion
9428           (gnus-summary-select-article t nil nil article))
9429         (save-excursion
9430           (set-buffer save-buffer)
9431           (erase-buffer)
9432           (insert-buffer-substring gnus-original-article-buffer))
9433         (setq file (gnus-article-save save-buffer file num))
9434         (gnus-summary-remove-process-mark article)
9435         (unless not-saved
9436           (gnus-summary-set-saved-mark article))))
9437     (gnus-kill-buffer save-buffer)
9438     (gnus-summary-position-point)
9439     (gnus-set-mode-line 'summary)
9440     n))
9441
9442 (defun gnus-summary-pipe-output (&optional arg)
9443   "Pipe the current article to a subprocess.
9444 If N is a positive number, pipe the N next articles.
9445 If N is a negative number, pipe the N previous articles.
9446 If N is nil and any articles have been marked with the process mark,
9447 pipe those articles instead."
9448   (interactive "P")
9449   (require 'gnus-art)
9450   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9451     (gnus-summary-save-article arg t))
9452   (gnus-configure-windows 'pipe))
9453
9454 (defun gnus-summary-save-article-mail (&optional arg)
9455   "Append the current article to an mail file.
9456 If N is a positive number, save the N next articles.
9457 If N is a negative number, save the N previous articles.
9458 If N is nil and any articles have been marked with the process mark,
9459 save those articles instead."
9460   (interactive "P")
9461   (require 'gnus-art)
9462   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9463     (gnus-summary-save-article arg)))
9464
9465 (defun gnus-summary-save-article-rmail (&optional arg)
9466   "Append the current article to an rmail file.
9467 If N is a positive number, save the N next articles.
9468 If N is a negative number, save the N previous articles.
9469 If N is nil and any articles have been marked with the process mark,
9470 save those articles instead."
9471   (interactive "P")
9472   (require 'gnus-art)
9473   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9474     (gnus-summary-save-article arg)))
9475
9476 (defun gnus-summary-save-article-file (&optional arg)
9477   "Append the current article to a file.
9478 If N is a positive number, save the N next articles.
9479 If N is a negative number, save the N previous articles.
9480 If N is nil and any articles have been marked with the process mark,
9481 save those articles instead."
9482   (interactive "P")
9483   (require 'gnus-art)
9484   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9485     (gnus-summary-save-article arg)))
9486
9487 (defun gnus-summary-write-article-file (&optional arg)
9488   "Write the current article to a file, deleting the previous file.
9489 If N is a positive number, save the N next articles.
9490 If N is a negative number, save the N previous articles.
9491 If N is nil and any articles have been marked with the process mark,
9492 save those articles instead."
9493   (interactive "P")
9494   (require 'gnus-art)
9495   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9496     (gnus-summary-save-article arg)))
9497
9498 (defun gnus-summary-save-article-body-file (&optional arg)
9499   "Append the current article body to a file.
9500 If N is a positive number, save the N next articles.
9501 If N is a negative number, save the N previous articles.
9502 If N is nil and any articles have been marked with the process mark,
9503 save those articles instead."
9504   (interactive "P")
9505   (require 'gnus-art)
9506   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9507     (gnus-summary-save-article arg)))
9508
9509 (defun gnus-summary-pipe-message (program)
9510   "Pipe the current article through PROGRAM."
9511   (interactive "sProgram: ")
9512   (gnus-summary-select-article)
9513   (let ((mail-header-separator ""))
9514     (gnus-eval-in-buffer-window gnus-article-buffer
9515       (save-restriction
9516         (widen)
9517         (let ((start (window-start))
9518               buffer-read-only)
9519           (message-pipe-buffer-body program)
9520           (set-window-start (get-buffer-window (current-buffer)) start))))))
9521
9522 (defun gnus-get-split-value (methods)
9523   "Return a value based on the split METHODS."
9524   (let (split-name method result match)
9525     (when methods
9526       (save-excursion
9527         (set-buffer gnus-original-article-buffer)
9528         (save-restriction
9529           (nnheader-narrow-to-headers)
9530           (while (and methods (not split-name))
9531             (goto-char (point-min))
9532             (setq method (pop methods))
9533             (setq match (car method))
9534             (when (cond
9535                    ((stringp match)
9536                     ;; Regular expression.
9537                     (ignore-errors
9538                       (re-search-forward match nil t)))
9539                    ((gnus-functionp match)
9540                     ;; Function.
9541                     (save-restriction
9542                       (widen)
9543                       (setq result (funcall match gnus-newsgroup-name))))
9544                    ((consp match)
9545                     ;; Form.
9546                     (save-restriction
9547                       (widen)
9548                       (setq result (eval match)))))
9549               (setq split-name (cdr method))
9550               (cond ((stringp result)
9551                      (push (expand-file-name
9552                             result gnus-article-save-directory)
9553                            split-name))
9554                     ((consp result)
9555                      (setq split-name (append result split-name)))))))))
9556     (nreverse split-name)))
9557
9558 (defun gnus-valid-move-group-p (group)
9559   (and (boundp group)
9560        (symbol-name group)
9561        (symbol-value group)
9562        (gnus-get-function (gnus-find-method-for-group
9563                            (symbol-name group)) 'request-accept-article t)))
9564
9565 (defun gnus-read-move-group-name (prompt default articles prefix)
9566   "Read a group name."
9567   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9568          (minibuffer-confirm-incomplete nil) ; XEmacs
9569          (prom
9570           (format "%s %s to:"
9571                   prompt
9572                   (if (> (length articles) 1)
9573                       (format "these %d articles" (length articles))
9574                     "this article")))
9575          (to-newsgroup
9576           (cond
9577            ((null split-name)
9578             (gnus-completing-read default prom
9579                                   gnus-active-hashtb
9580                                   'gnus-valid-move-group-p
9581                                   nil prefix
9582                                   'gnus-group-history))
9583            ((= 1 (length split-name))
9584             (gnus-completing-read (car split-name) prom
9585                                   gnus-active-hashtb
9586                                   'gnus-valid-move-group-p
9587                                   nil nil
9588                                   'gnus-group-history))
9589            (t
9590             (gnus-completing-read nil prom
9591                                   (mapcar (lambda (el) (list el))
9592                                           (nreverse split-name))
9593                                   nil nil nil
9594                                   'gnus-group-history))))
9595          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9596     (when to-newsgroup
9597       (if (or (string= to-newsgroup "")
9598               (string= to-newsgroup prefix))
9599           (setq to-newsgroup default))
9600       (unless to-newsgroup
9601         (error "No group name entered"))
9602       (or (gnus-active to-newsgroup)
9603           (gnus-activate-group to-newsgroup nil nil to-method)
9604           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9605                                      to-newsgroup))
9606               (or (and (gnus-request-create-group to-newsgroup to-method)
9607                        (gnus-activate-group
9608                         to-newsgroup nil nil to-method)
9609                        (gnus-subscribe-group to-newsgroup))
9610                   (error "Couldn't create group %s" to-newsgroup)))
9611           (error "No such group: %s" to-newsgroup)))
9612     to-newsgroup))
9613
9614 (defun gnus-summary-save-parts (type dir n &optional reverse)
9615   "Save parts matching TYPE to DIR.
9616 If REVERSE, save parts that do not match TYPE."
9617   (interactive
9618    (list (read-string "Save parts of type: "
9619                       (or (car gnus-summary-save-parts-type-history)
9620                           gnus-summary-save-parts-default-mime)
9621                       'gnus-summary-save-parts-type-history)
9622          (setq gnus-summary-save-parts-last-directory
9623                (read-file-name "Save to directory: "
9624                                gnus-summary-save-parts-last-directory
9625                                nil t))
9626          current-prefix-arg))
9627   (gnus-summary-iterate n
9628     (let ((gnus-display-mime-function nil)
9629           (gnus-inhibit-treatment t))
9630       (gnus-summary-select-article))
9631     (save-excursion
9632       (set-buffer gnus-article-buffer)
9633       (let ((handles (or gnus-article-mime-handles
9634                          (mm-dissect-buffer) (mm-uu-dissect))))
9635         (when handles
9636           (gnus-summary-save-parts-1 type dir handles reverse)
9637           (unless gnus-article-mime-handles ;; Don't destroy this case.
9638             (mm-destroy-parts handles)))))))
9639
9640 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9641   (if (stringp (car handle))
9642       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9643               (cdr handle))
9644     (when (if reverse
9645               (not (string-match type (mm-handle-media-type handle)))
9646             (string-match type (mm-handle-media-type handle)))
9647       (let ((file (expand-file-name
9648                    (file-name-nondirectory
9649                     (or
9650                      (mail-content-type-get
9651                       (mm-handle-disposition handle) 'filename)
9652                      (concat gnus-newsgroup-name
9653                              "." (number-to-string
9654                                   (cdr gnus-article-current)))))
9655                    dir)))
9656         (unless (file-exists-p file)
9657           (mm-save-part-to-file handle file))))))
9658
9659 ;; Summary extract commands
9660
9661 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9662   (let ((buffer-read-only nil)
9663         (article (gnus-summary-article-number))
9664         after-article b e)
9665     (unless (gnus-summary-goto-subject article)
9666       (error "No such article: %d" article))
9667     (gnus-summary-position-point)
9668     ;; If all commands are to be bunched up on one line, we collect
9669     ;; them here.
9670     (unless gnus-view-pseudos-separately
9671       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9672             files action)
9673         (while ps
9674           (setq action (cdr (assq 'action (car ps))))
9675           (setq files (list (cdr (assq 'name (car ps)))))
9676           (while (and ps (cdr ps)
9677                       (string= (or action "1")
9678                                (or (cdr (assq 'action (cadr ps))) "2")))
9679             (push (cdr (assq 'name (cadr ps))) files)
9680             (setcdr ps (cddr ps)))
9681           (when files
9682             (when (not (string-match "%s" action))
9683               (push " " files))
9684             (push " " files)
9685             (when (assq 'execute (car ps))
9686               (setcdr (assq 'execute (car ps))
9687                       (funcall (if (string-match "%s" action)
9688                                    'format 'concat)
9689                                action
9690                                (mapconcat
9691                                 (lambda (f)
9692                                   (if (equal f " ")
9693                                       f
9694                                     (gnus-quote-arg-for-sh-or-csh f)))
9695                                 files " ")))))
9696           (setq ps (cdr ps)))))
9697     (if (and gnus-view-pseudos (not not-view))
9698         (while pslist
9699           (when (assq 'execute (car pslist))
9700             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9701                                   (eq gnus-view-pseudos 'not-confirm)))
9702           (setq pslist (cdr pslist)))
9703       (save-excursion
9704         (while pslist
9705           (setq after-article (or (cdr (assq 'article (car pslist)))
9706                                   (gnus-summary-article-number)))
9707           (gnus-summary-goto-subject after-article)
9708           (forward-line 1)
9709           (setq b (point))
9710           (insert "    " (file-name-nondirectory
9711                           (cdr (assq 'name (car pslist))))
9712                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9713           (setq e (point))
9714           (forward-line -1)             ; back to `b'
9715           (gnus-add-text-properties
9716            b (1- e) (list 'gnus-number gnus-reffed-article-number
9717                           gnus-mouse-face-prop gnus-mouse-face))
9718           (gnus-data-enter
9719            after-article gnus-reffed-article-number
9720            gnus-unread-mark b (car pslist) 0 (- e b))
9721           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9722           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9723           (setq pslist (cdr pslist)))))))
9724
9725 (defun gnus-pseudos< (p1 p2)
9726   (let ((c1 (cdr (assq 'action p1)))
9727         (c2 (cdr (assq 'action p2))))
9728     (and c1 c2 (string< c1 c2))))
9729
9730 (defun gnus-request-pseudo-article (props)
9731   (cond ((assq 'execute props)
9732          (gnus-execute-command (cdr (assq 'execute props)))))
9733   (let ((gnus-current-article (gnus-summary-article-number)))
9734     (gnus-run-hooks 'gnus-mark-article-hook)))
9735
9736 (defun gnus-execute-command (command &optional automatic)
9737   (save-excursion
9738     (gnus-article-setup-buffer)
9739     (set-buffer gnus-article-buffer)
9740     (setq buffer-read-only nil)
9741     (let ((command (if automatic command
9742                      (read-string "Command: " (cons command 0)))))
9743       (erase-buffer)
9744       (insert "$ " command "\n\n")
9745       (if gnus-view-pseudo-asynchronously
9746           (start-process "gnus-execute" (current-buffer) shell-file-name
9747                          shell-command-switch command)
9748         (call-process shell-file-name nil t nil
9749                       shell-command-switch command)))))
9750
9751 ;; Summary kill commands.
9752
9753 (defun gnus-summary-edit-global-kill (article)
9754   "Edit the \"global\" kill file."
9755   (interactive (list (gnus-summary-article-number)))
9756   (gnus-group-edit-global-kill article))
9757
9758 (defun gnus-summary-edit-local-kill ()
9759   "Edit a local kill file applied to the current newsgroup."
9760   (interactive)
9761   (setq gnus-current-headers (gnus-summary-article-header))
9762   (gnus-group-edit-local-kill
9763    (gnus-summary-article-number) gnus-newsgroup-name))
9764
9765 ;;; Header reading.
9766
9767 (defun gnus-read-header (id &optional header)
9768   "Read the headers of article ID and enter them into the Gnus system."
9769   (let ((group gnus-newsgroup-name)
9770         (gnus-override-method
9771          (or
9772           gnus-override-method
9773           (and (gnus-news-group-p gnus-newsgroup-name)
9774                (car (gnus-refer-article-methods)))))
9775         where)
9776     ;; First we check to see whether the header in question is already
9777     ;; fetched.
9778     (if (stringp id)
9779         ;; This is a Message-ID.
9780         (setq header (or header (gnus-id-to-header id)))
9781       ;; This is an article number.
9782       (setq header (or header (gnus-summary-article-header id))))
9783     (if (and header
9784              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9785         ;; We have found the header.
9786         header
9787       ;; If this is a sparse article, we have to nix out its
9788       ;; previous entry in the thread hashtb.
9789       (when (and header
9790                  (gnus-summary-article-sparse-p (mail-header-number header)))
9791         (let* ((parent (gnus-parent-id (mail-header-references header)))
9792                (thread (and parent (gnus-id-to-thread parent))))
9793           (when thread
9794             (delq (assq header thread) thread))))
9795       ;; We have to really fetch the header to this article.
9796       (save-excursion
9797         (set-buffer nntp-server-buffer)
9798         (when (setq where (gnus-request-head id group))
9799           (nnheader-fold-continuation-lines)
9800           (goto-char (point-max))
9801           (insert ".\n")
9802           (goto-char (point-min))
9803           (insert "211 ")
9804           (princ (cond
9805                   ((numberp id) id)
9806                   ((cdr where) (cdr where))
9807                   (header (mail-header-number header))
9808                   (t gnus-reffed-article-number))
9809                  (current-buffer))
9810           (insert " Article retrieved.\n"))
9811         (if (or (not where)
9812                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9813             ()                          ; Malformed head.
9814           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9815             (when (and (stringp id)
9816                        (not (string= (gnus-group-real-name group)
9817                                      (car where))))
9818               ;; If we fetched by Message-ID and the article came
9819               ;; from a different group, we fudge some bogus article
9820               ;; numbers for this article.
9821               (mail-header-set-number header gnus-reffed-article-number))
9822             (save-excursion
9823               (set-buffer gnus-summary-buffer)
9824               (decf gnus-reffed-article-number)
9825               (gnus-remove-header (mail-header-number header))
9826               (push header gnus-newsgroup-headers)
9827               (setq gnus-current-headers header)
9828               (push (mail-header-number header) gnus-newsgroup-limit)))
9829           header)))))
9830
9831 (defun gnus-remove-header (number)
9832   "Remove header NUMBER from `gnus-newsgroup-headers'."
9833   (if (and gnus-newsgroup-headers
9834            (= number (mail-header-number (car gnus-newsgroup-headers))))
9835       (pop gnus-newsgroup-headers)
9836     (let ((headers gnus-newsgroup-headers))
9837       (while (and (cdr headers)
9838                   (not (= number (mail-header-number (cadr headers)))))
9839         (pop headers))
9840       (when (cdr headers)
9841         (setcdr headers (cddr headers))))))
9842
9843 ;;;
9844 ;;; summary highlights
9845 ;;;
9846
9847 (defun gnus-highlight-selected-summary ()
9848   "Highlight selected article in summary buffer."
9849   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9850   (when gnus-summary-selected-face
9851     (save-excursion
9852       (let* ((beg (progn (beginning-of-line) (point)))
9853              (end (progn (end-of-line) (point)))
9854              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9855              (from (if (get-text-property beg gnus-mouse-face-prop)
9856                        beg
9857                      (or (next-single-property-change
9858                           beg gnus-mouse-face-prop nil end)
9859                          beg)))
9860              (to
9861               (if (= from end)
9862                   (- from 2)
9863                 (or (next-single-property-change
9864                      from gnus-mouse-face-prop nil end)
9865                     end))))
9866         ;; If no mouse-face prop on line we will have to = from = end,
9867         ;; so we highlight the entire line instead.
9868         (when (= (+ to 2) from)
9869           (setq from beg)
9870           (setq to end))
9871         (if gnus-newsgroup-selected-overlay
9872             ;; Move old overlay.
9873             (gnus-move-overlay
9874              gnus-newsgroup-selected-overlay from to (current-buffer))
9875           ;; Create new overlay.
9876           (gnus-overlay-put
9877            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9878            'face gnus-summary-selected-face))))))
9879
9880 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9881 (defun gnus-summary-highlight-line ()
9882   "Highlight current line according to `gnus-summary-highlight'."
9883   (let* ((list gnus-summary-highlight)
9884          (p (point))
9885          (end (progn (end-of-line) (point)))
9886          ;; now find out where the line starts and leave point there.
9887          (beg (progn (beginning-of-line) (point)))
9888          (article (gnus-summary-article-number))
9889          (score (or (cdr (assq (or article gnus-current-article)
9890                                gnus-newsgroup-scored))
9891                     gnus-summary-default-score 0))
9892          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9893          (inhibit-read-only t))
9894     ;; Eval the cars of the lists until we find a match.
9895     (let ((default gnus-summary-default-score))
9896       (while (and list
9897                   (not (eval (caar list))))
9898         (setq list (cdr list))))
9899     (let ((face (cdar list)))
9900       (unless (eq face (get-text-property beg 'face))
9901         (gnus-put-text-property-excluding-characters-with-faces
9902          beg end 'face
9903          (setq face (if (boundp face) (symbol-value face) face)))
9904         (when gnus-summary-highlight-line-function
9905           (funcall gnus-summary-highlight-line-function article face))))
9906     (goto-char p)))
9907
9908 (defun gnus-update-read-articles (group unread &optional compute)
9909   "Update the list of read articles in GROUP."
9910   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9911          (entry (gnus-gethash group gnus-newsrc-hashtb))
9912          (info (nth 2 entry))
9913          (prev 1)
9914          (unread (sort (copy-sequence unread) '<))
9915          read)
9916     (if (or (not info) (not active))
9917         ;; There is no info on this group if it was, in fact,
9918         ;; killed.  Gnus stores no information on killed groups, so
9919         ;; there's nothing to be done.
9920         ;; One could store the information somewhere temporarily,
9921         ;; perhaps...  Hmmm...
9922         ()
9923       ;; Remove any negative articles numbers.
9924       (while (and unread (< (car unread) 0))
9925         (setq unread (cdr unread)))
9926       ;; Remove any expired article numbers
9927       (while (and unread (< (car unread) (car active)))
9928         (setq unread (cdr unread)))
9929       ;; Compute the ranges of read articles by looking at the list of
9930       ;; unread articles.
9931       (while unread
9932         (when (/= (car unread) prev)
9933           (push (if (= prev (1- (car unread))) prev
9934                   (cons prev (1- (car unread))))
9935                 read))
9936         (setq prev (1+ (car unread)))
9937         (setq unread (cdr unread)))
9938       (when (<= prev (cdr active))
9939         (push (cons prev (cdr active)) read))
9940       (setq read (if (> (length read) 1) (nreverse read) read))
9941       (if compute
9942           read
9943         (save-excursion
9944           (let (setmarkundo)
9945             ;; Propagate the read marks to the backend.
9946             (when (gnus-check-backend-function 'request-set-mark group)
9947               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9948                     (add (gnus-remove-from-range read (gnus-info-read info))))
9949                 (when (or add del)
9950                   (unless (gnus-check-group group)
9951                     (error "Can't open server for %s" group))
9952                   (gnus-request-set-mark
9953                    group (delq nil (list (if add (list add 'add '(read)))
9954                                          (if del (list del 'del '(read))))))
9955                   (setq setmarkundo
9956                         `(gnus-request-set-mark
9957                           ,group
9958                           ',(delq nil (list
9959                                        (if del (list del 'add '(read)))
9960                                        (if add (list add 'del '(read))))))))))
9961             (set-buffer gnus-group-buffer)
9962             (gnus-undo-register
9963               `(progn
9964                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9965                  (gnus-info-set-read ',info ',(gnus-info-read info))
9966                  (gnus-get-unread-articles-in-group ',info
9967                                                     (gnus-active ,group))
9968                  (gnus-group-update-group ,group t)
9969                  ,setmarkundo))))
9970         ;; Enter this list into the group info.
9971         (gnus-info-set-read info read)
9972         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9973         (gnus-get-unread-articles-in-group info (gnus-active group))
9974         t))))
9975
9976 (defun gnus-offer-save-summaries ()
9977   "Offer to save all active summary buffers."
9978   (save-excursion
9979     (let ((buflist (buffer-list))
9980           buffers bufname)
9981       ;; Go through all buffers and find all summaries.
9982       (while buflist
9983         (and (setq bufname (buffer-name (car buflist)))
9984              (string-match "Summary" bufname)
9985              (save-excursion
9986                (set-buffer bufname)
9987                ;; We check that this is, indeed, a summary buffer.
9988                (and (eq major-mode 'gnus-summary-mode)
9989                     ;; Also make sure this isn't bogus.
9990                     gnus-newsgroup-prepared
9991                     ;; Also make sure that this isn't a dead summary buffer.
9992                     (not gnus-dead-summary-mode)))
9993              (push bufname buffers))
9994         (setq buflist (cdr buflist)))
9995       ;; Go through all these summary buffers and offer to save them.
9996       (when buffers
9997         (map-y-or-n-p
9998          "Update summary buffer %s? "
9999          (lambda (buf)
10000            (switch-to-buffer buf)
10001            (gnus-summary-exit))
10002          buffers)))))
10003
10004
10005 ;;; @ for mime-partial
10006 ;;;
10007
10008 (defun gnus-request-partial-message ()
10009   (save-excursion
10010     (let ((number (gnus-summary-article-number))
10011           (group gnus-newsgroup-name)
10012           (mother gnus-article-buffer))
10013       (set-buffer (get-buffer-create " *Partial Article*"))
10014       (erase-buffer)
10015       (setq mime-preview-buffer mother)
10016       (gnus-request-article-this-buffer number group)
10017       (mime-parse-buffer)
10018       )))
10019
10020 (autoload 'mime-combine-message/partial-pieces-automatically
10021   "mime-partial"
10022   "Internal method to combine message/partial messages automatically.")
10023
10024 (mime-add-condition
10025  'action '((type . message)(subtype . partial)
10026            (major-mode . gnus-original-article-mode)
10027            (method . mime-combine-message/partial-pieces-automatically)
10028            (summary-buffer-exp . gnus-summary-buffer)
10029            (request-partial-message-method . gnus-request-partial-message)
10030            ))
10031
10032
10033 ;;; @ for message/rfc822
10034 ;;;
10035
10036 (defun gnus-mime-extract-message/rfc822 (entity situation)
10037   (let (group article num cwin swin cur)
10038     (with-temp-buffer
10039       (mime-insert-entity-content entity)
10040       (setq group (or (cdr (assq 'group situation))
10041                       (completing-read "Group: "
10042                                        gnus-active-hashtb
10043                                        nil
10044                                        (gnus-read-active-file-p)
10045                                        gnus-newsgroup-name))
10046             article (gnus-request-accept-article group)))
10047     (when (and (consp article)
10048                (numberp (setq article (cdr article))))
10049       (setq num (1+ (or (cdr (assq 'number situation)) 0))
10050             cwin (get-buffer-window (current-buffer) t))
10051       (save-window-excursion
10052         (if (setq swin (get-buffer-window gnus-summary-buffer t))
10053             (select-window swin)
10054           (set-buffer gnus-summary-buffer))
10055         (setq cur gnus-current-article)
10056         (forward-line num)
10057         (let (gnus-show-threads)
10058           (gnus-summary-goto-subject article t))
10059         (gnus-summary-clear-mark-forward 1)
10060         (gnus-summary-goto-subject cur))
10061       (when (and cwin (window-frame cwin))
10062         (select-frame (window-frame cwin)))
10063       (when (boundp 'mime-acting-situation-to-override)
10064         (set-alist 'mime-acting-situation-to-override
10065                    'group
10066                    group)
10067         (set-alist 'mime-acting-situation-to-override
10068                    'after-method
10069                    `(progn
10070                       (save-current-buffer
10071                         (set-buffer gnus-group-buffer)
10072                         (gnus-activate-group ,group))
10073                       (gnus-summary-goto-article ,cur
10074                                                  gnus-show-all-headers)))
10075         (set-alist 'mime-acting-situation-to-override
10076                    'number num)))))
10077
10078 (mime-add-condition
10079  'action '((type . message)(subtype . rfc822)
10080            (major-mode . gnus-original-article-mode)
10081            (method . gnus-mime-extract-message/rfc822)
10082            (mode . "extract")
10083            ))
10084
10085 (mime-add-condition
10086  'action '((type . message)(subtype . news)
10087            (major-mode . gnus-original-article-mode)
10088            (method . gnus-mime-extract-message/rfc822)
10089            (mode . "extract")
10090            ))
10091
10092 (defun gnus-mime-extract-multipart (entity situation)
10093   (let ((children (mime-entity-children entity))
10094         mime-acting-situation-to-override
10095         f)
10096     (while children
10097       (mime-play-entity (car children)
10098                         (cons (assq 'mode situation)
10099                               mime-acting-situation-to-override))
10100       (setq children (cdr children)))
10101     (if (setq f (cdr (assq 'after-method
10102                            mime-acting-situation-to-override)))
10103         (eval f)
10104       )))
10105
10106 (mime-add-condition
10107  'action '((type . multipart)
10108            (method . gnus-mime-extract-multipart)
10109            (mode . "extract")
10110            )
10111  'with-default)
10112
10113
10114 ;;; @ end
10115 ;;;
10116
10117 (defun gnus-summary-setup-default-charset ()
10118   "Setup newsgroup default charset."
10119   (if (equal gnus-newsgroup-name "nndraft:drafts")
10120       (setq gnus-newsgroup-charset nil)
10121     (let* ((name (and gnus-newsgroup-name
10122                       (gnus-group-real-name gnus-newsgroup-name)))
10123            (ignored-charsets
10124             (or gnus-newsgroup-ephemeral-ignored-charsets
10125                 (append
10126                  (and gnus-newsgroup-name
10127                       (or (gnus-group-find-parameter gnus-newsgroup-name
10128                                                      'ignored-charsets t)
10129                           (let ((alist gnus-group-ignored-charsets-alist)
10130                                 elem (charsets nil))
10131                             (while (setq elem (pop alist))
10132                               (when (and name
10133                                          (string-match (car elem) name))
10134                                 (setq alist nil
10135                                       charsets (cdr elem))))
10136                             charsets)))
10137                  gnus-newsgroup-ignored-charsets))))
10138       (setq gnus-newsgroup-charset
10139             (or gnus-newsgroup-ephemeral-charset
10140                 (and gnus-newsgroup-name
10141                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
10142                          (let ((alist gnus-group-charset-alist)
10143                                elem charset)
10144                            (while (setq elem (pop alist))
10145                              (when (and name
10146                                         (string-match (car elem) name))
10147                                (setq alist nil
10148                                      charset (cadr elem))))
10149                            charset)))
10150                 gnus-default-charset))
10151       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10152            ignored-charsets))))
10153
10154 ;;;
10155 ;;; Mime Commands
10156 ;;;
10157
10158 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10159   "Display the current article buffer fully MIME-buttonized.
10160 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10161 treated as multipart/mixed."
10162   (interactive "P")
10163   (require 'gnus-art)
10164   (let ((gnus-unbuttonized-mime-types nil)
10165         (gnus-mime-display-multipart-as-mixed show-all-parts))
10166     (gnus-summary-show-article)))
10167
10168 (defun gnus-summary-repair-multipart (article)
10169   "Add a Content-Type header to a multipart article without one."
10170   (interactive (list (gnus-summary-article-number)))
10171   (gnus-with-article article
10172     (message-narrow-to-head)
10173     (message-remove-header "Mime-Version")
10174     (goto-char (point-max))
10175     (insert "Mime-Version: 1.0\n")
10176     (widen)
10177     (when (search-forward "\n--" nil t)
10178       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10179         (message-narrow-to-head)
10180         (message-remove-header "Content-Type")
10181         (goto-char (point-max))
10182         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10183                         separator))
10184         (widen))))
10185   (let (gnus-mark-article-hook)
10186     (gnus-summary-select-article t t nil article)))
10187
10188 (defun gnus-summary-toggle-display-buttonized ()
10189   "Toggle the buttonizing of the article buffer."
10190   (interactive)
10191   (require 'gnus-art)
10192   (if (setq gnus-inhibit-mime-unbuttonizing
10193             (not gnus-inhibit-mime-unbuttonizing))
10194       (let ((gnus-unbuttonized-mime-types nil))
10195         (gnus-summary-show-article))
10196     (gnus-summary-show-article)))
10197
10198 ;;;
10199 ;;; Intelli-mouse commmands
10200 ;;;
10201
10202 (defun gnus-wheel-summary-scroll (event)
10203   (interactive "e")
10204   (let ((amount (if (memq 'shift (event-modifiers event))
10205                     (car gnus-wheel-scroll-amount)
10206                   (cdr gnus-wheel-scroll-amount)))
10207         (direction (- (* (static-if (featurep 'xemacs)
10208                              (event-button event)
10209                            (cond ((eq 'mouse-4 (event-basic-type event))
10210                                   4)
10211                                  ((eq 'mouse-5 (event-basic-type event))
10212                                   5)))
10213                          2) 9))
10214         edge)
10215     (gnus-summary-scroll-up (* amount direction))
10216     (when (gnus-eval-in-buffer-window gnus-article-buffer
10217             (save-restriction
10218               (widen)
10219               (and (if (< 0 direction)
10220                        (gnus-article-next-page 0)
10221                      (gnus-article-prev-page 0)
10222                      (bobp))
10223                    (if (setq edge (get-text-property
10224                                    (point-min) 'gnus-wheel-edge))
10225                        (setq edge (* edge direction))
10226                      (setq edge -1))
10227                    (or (plusp edge)
10228                        (let ((buffer-read-only nil)
10229                              (inhibit-read-only t))
10230                          (put-text-property (point-min) (point-max)
10231                                             'gnus-wheel-edge direction)
10232                          nil))
10233                    (or (> edge gnus-wheel-edge-resistance)
10234                        (let ((buffer-read-only nil)
10235                              (inhibit-read-only t))
10236                          (put-text-property (point-min) (point-max)
10237                                             'gnus-wheel-edge
10238                                             (* (1+ edge) direction))
10239                          nil))
10240                    (eq last-command 'gnus-wheel-summary-scroll))))
10241       (gnus-summary-next-article nil nil (minusp direction)))))
10242
10243 (defun gnus-wheel-install ()
10244   "Enable mouse wheel support on summary window."
10245   (when gnus-use-wheel
10246     (let ((keys
10247            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
10248       (dolist (key keys)
10249         (define-key gnus-summary-mode-map key
10250           'gnus-wheel-summary-scroll)))))
10251
10252 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
10253
10254 ;;;
10255 ;;; Traditional PGP commmands
10256 ;;;
10257
10258 (defun gnus-summary-decrypt-article (&optional force)
10259   "Decrypt the current article in traditional PGP way.
10260 This will have permanent effect only in mail groups.
10261 If FORCE is non-nil, allow editing of articles even in read-only
10262 groups."
10263   (interactive "P")
10264   (gnus-summary-select-article t)
10265   (gnus-eval-in-buffer-window gnus-article-buffer
10266     (save-excursion
10267       (save-restriction
10268         (widen)
10269         (goto-char (point-min))
10270         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10271           (error "Not a traditional PGP message!"))
10272         (let ((armor-start (match-beginning 0)))
10273           (if (and (pgg-decrypt-region armor-start (point-max))
10274                    (or force (not (gnus-group-read-only-p))))
10275               (let ((inhibit-read-only t)
10276                     buffer-read-only)
10277                 (delete-region armor-start
10278                                (progn
10279                                  (re-search-forward "^-+END PGP" nil t)
10280                                  (beginning-of-line 2)
10281                                  (point)))
10282                 (insert-buffer-substring pgg-output-buffer))))))))
10283
10284 (defun gnus-summary-verify-article ()
10285   "Verify the current article in traditional PGP way."
10286   (interactive)
10287   (save-excursion
10288     (set-buffer gnus-original-article-buffer)
10289     (goto-char (point-min))
10290     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10291       (error "Not a traditional PGP message!"))
10292     (re-search-forward "^-+END PGP" nil t)
10293     (beginning-of-line 2)
10294     (call-interactively (function pgg-verify-region))))
10295
10296 ;;;
10297 ;;; Generic summary marking commands
10298 ;;;
10299
10300 (defvar gnus-summary-marking-alist
10301   '((read gnus-del-mark "d")
10302     (unread gnus-unread-mark "u")
10303     (ticked gnus-ticked-mark "!")
10304     (dormant gnus-dormant-mark "?")
10305     (expirable gnus-expirable-mark "e"))
10306   "An alist of names/marks/keystrokes.")
10307
10308 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10309 (defvar gnus-summary-mark-map)
10310
10311 (defun gnus-summary-make-all-marking-commands ()
10312   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10313   (dolist (elem gnus-summary-marking-alist)
10314     (apply 'gnus-summary-make-marking-command elem)))
10315
10316 (defun gnus-summary-make-marking-command (name mark keystroke)
10317   (let ((map (make-sparse-keymap)))
10318     (define-key gnus-summary-generic-mark-map keystroke map)
10319     (dolist (lway `((next "next" next nil "n")
10320                     (next-unread "next unread" next t "N")
10321                     (prev "previous" prev nil "p")
10322                     (prev-unread "previous unread" prev t "P")
10323                     (nomove "" nil nil ,keystroke)))
10324       (let ((func (gnus-summary-make-marking-command-1
10325                    mark (car lway) lway name)))
10326         (setq func (eval func))
10327         (define-key map (nth 4 lway) func)))))
10328
10329 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10330   `(defun ,(intern
10331             (format "gnus-summary-put-mark-as-%s%s"
10332                     name (if (eq way 'nomove)
10333                              ""
10334                            (concat "-" (symbol-name way)))))
10335      (n)
10336      ,(format
10337        "Mark the current article as %s%s.
10338 If N, the prefix, then repeat N times.
10339 If N is negative, move in reverse order.
10340 The difference between N and the actual number of articles marked is
10341 returned."
10342        name (car (cdr lway)))
10343      (interactive "p")
10344      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10345
10346 (defun gnus-summary-generic-mark (n mark move unread)
10347   "Mark N articles with MARK."
10348   (unless (eq major-mode 'gnus-summary-mode)
10349     (error "This command can only be used in the summary buffer"))
10350   (gnus-summary-show-thread)
10351   (let ((nummove
10352          (cond
10353           ((eq move 'next) 1)
10354           ((eq move 'prev) -1)
10355           (t 0))))
10356     (if (zerop nummove)
10357         (setq n 1)
10358       (when (< n 0)
10359         (setq n (abs n)
10360               nummove (* -1 nummove))))
10361     (while (and (> n 0)
10362                 (gnus-summary-mark-article nil mark)
10363                 (zerop (gnus-summary-next-subject nummove unread t)))
10364       (setq n (1- n)))
10365     (when (/= 0 n)
10366       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10367     (gnus-summary-recenter)
10368     (gnus-summary-position-point)
10369     (gnus-set-mode-line 'summary)
10370     n))
10371
10372 (gnus-summary-make-all-marking-commands)
10373
10374 (gnus-ems-redefine)
10375
10376 (provide 'gnus-sum)
10377
10378 (run-hooks 'gnus-sum-load-hook)
10379
10380 ;;; gnus-sum.el ends here