Sync up with Pterodactyl Gnus v0.96.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   (require 'static))
44
45 (eval-and-compile
46   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
50
51 (defcustom gnus-kill-summary-on-exit t
52   "*If non-nil, kill the summary buffer when you exit from it.
53 If nil, the summary will become a \"*Dead Summary*\" buffer, and
54 it will be killed sometime later."
55   :group 'gnus-summary-exit
56   :type 'boolean)
57
58 (defcustom gnus-fetch-old-headers nil
59   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
60 If an unread article in the group refers to an older, already read (or
61 just marked as read) article, the old article will not normally be
62 displayed in the Summary buffer.  If this variable is non-nil, Gnus
63 will attempt to grab the headers to the old articles, and thereby
64 build complete threads.  If it has the value `some', only enough
65 headers to connect otherwise loose threads will be displayed.  This
66 variable can also be a number.  In that case, no more than that number
67 of old headers will be fetched.  If it has the value `invisible', all
68 old headers will be fetched, but none will be displayed.
69
70 The server has to support NOV for any of this to work."
71   :group 'gnus-thread
72   :type '(choice (const :tag "off" nil)
73                  (const some)
74                  number
75                  (sexp :menu-tag "other" t)))
76
77 (defcustom gnus-refer-thread-limit 200
78   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
79 If t, fetch all the available old headers."
80   :group 'gnus-thread
81   :type '(choice number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-summary-make-false-root 'adopt
85   "*nil means that Gnus won't gather loose threads.
86 If the root of a thread has expired or been read in a previous
87 session, the information necessary to build a complete thread has been
88 lost.  Instead of having many small sub-threads from this original thread
89 scattered all over the summary buffer, Gnus can gather them.
90
91 If non-nil, Gnus will try to gather all loose sub-threads from an
92 original thread into one large thread.
93
94 If this variable is non-nil, it should be one of `none', `adopt',
95 `dummy' or `empty'.
96
97 If this variable is `none', Gnus will not make a false root, but just
98 present the sub-threads after another.
99 If this variable is `dummy', Gnus will create a dummy root that will
100 have all the sub-threads as children.
101 If this variable is `adopt', Gnus will make one of the \"children\"
102 the parent and mark all the step-children as such.
103 If this variable is `empty', the \"children\" are printed with empty
104 subject fields.  (Or rather, they will be printed with a string
105 given by the `gnus-summary-same-subject' variable.)"
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const none)
109                  (const dummy)
110                  (const adopt)
111                  (const empty)))
112
113 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
114   "*A regexp to match subjects to be excluded from loose thread gathering.
115 As loose thread gathering is done on subjects only, that means that
116 there can be many false gatherings performed.  By rooting out certain
117 common subjects, gathering might become saner."
118   :group 'gnus-thread
119   :type 'regexp)
120
121 (defcustom gnus-summary-gather-subject-limit nil
122   "*Maximum length of subject comparisons when gathering loose threads.
123 Use nil to compare full subjects.  Setting this variable to a low
124 number will help gather threads that have been corrupted by
125 newsreaders chopping off subject lines, but it might also mean that
126 unrelated articles that have subject that happen to begin with the
127 same few characters will be incorrectly gathered.
128
129 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
130 comparing subjects."
131   :group 'gnus-thread
132   :type '(choice (const :tag "off" nil)
133                  (const fuzzy)
134                  (sexp :menu-tag "on" t)))
135
136 (defcustom gnus-simplify-subject-functions nil
137   "List of functions taking a string argument that simplify subjects.
138 The functions are applied recursively.
139
140 Useful functions to put in this list include: `gnus-simplify-subject-re',
141 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, marking commands will go to the next unread article.
183 If `never', commands that usually go to the next unread article, will
184 go to the next article, whether it is read or not.
185 If nil, only the marking commands will go to the next (un)read article."
186   :group 'gnus-summary-marks
187   :link '(custom-manual "(gnus)Setting Marks")
188   :type '(choice (const :tag "off" nil)
189                  (const never)
190                  (sexp :menu-tag "on" t)))
191
192 (defcustom gnus-summary-default-score 0
193   "*Default article score level.
194 All scores generated by the score files will be added to this score.
195 If this variable is nil, scoring will be disabled."
196   :group 'gnus-score-default
197   :type '(choice (const :tag "disable")
198                  integer))
199
200 (defcustom gnus-summary-zcore-fuzz 0
201   "*Fuzziness factor for the zcore in the summary buffer.
202 Articles with scores closer than this to `gnus-summary-default-score'
203 will not be marked."
204   :group 'gnus-summary-format
205   :type 'integer)
206
207 (defcustom gnus-simplify-subject-fuzzy-regexp nil
208   "*Strings to be removed when doing fuzzy matches.
209 This can either be a regular expression or list of regular expressions
210 that will be removed from subject strings if fuzzy subject
211 simplification is selected."
212   :group 'gnus-thread
213   :type '(repeat regexp))
214
215 (defcustom gnus-show-threads t
216   "*If non-nil, display threads in summary mode."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-subtree nil
221   "*If non-nil, hide all threads initially.
222 If threads are hidden, you have to run the command
223 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
224 to expose hidden threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-killed t
229   "*If non-nil, hide killed threads automatically."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-ignore-subject t
234   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
235 If nil, articles that have different subjects from their parents will
236 start separate threads."
237   :group 'gnus-thread
238   :type 'boolean)
239
240 (defcustom gnus-thread-operation-ignore-subject t
241   "*If non-nil, subjects will be ignored when doing thread commands.
242 This affects commands like `gnus-summary-kill-thread' and
243 `gnus-summary-lower-thread'.
244
245 If this variable is nil, articles in the same thread with different
246 subjects will not be included in the operation in question.  If this
247 variable is `fuzzy', only articles that have subjects that are fuzzily
248 equal will be included."
249   :group 'gnus-thread
250   :type '(choice (const :tag "off" nil)
251                  (const fuzzy)
252                  (sexp :tag "on" t)))
253
254 (defcustom gnus-thread-indent-level 4
255   "*Number that says how much each sub-thread should be indented."
256   :group 'gnus-thread
257   :type 'integer)
258
259 (defcustom gnus-auto-extend-newsgroup t
260   "*If non-nil, extend newsgroup forward and backward when requested."
261   :group 'gnus-summary-choose
262   :type 'boolean)
263
264 (defcustom gnus-auto-select-first t
265   "*If nil, don't select the first unread article when entering a group.
266 If this variable is `best', select the highest-scored unread article
267 in the group.  If t, select the first unread article.
268
269 This variable can also be a function to place point on a likely
270 subject line.  Useful values include `gnus-summary-first-unread-subject',
271 `gnus-summary-first-unread-article' and
272 `gnus-summary-best-unread-article'.
273
274 If you want to prevent automatic selection of the first unread article
275 in some newsgroups, set the variable to nil in
276 `gnus-select-group-hook'."
277   :group 'gnus-group-select
278   :type '(choice (const :tag "none" nil)
279                  (const best)
280                  (sexp :menu-tag "first" t)
281                  (function-item gnus-summary-first-unread-subject)
282                  (function-item gnus-summary-first-unread-article)
283                  (function-item gnus-summary-best-unread-article)))
284
285 (defcustom gnus-dont-select-after-jump-to-other-group nil
286   "If non-nil, don't select the first unread article after entering the
287 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
288 it is depend on the value of `gnus-auto-select-first' whether to select
289 or not."
290   :group 'gnus-group-select
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-next t
294   "*If non-nil, offer to go to the next group from the end of the previous.
295 If the value is t and the next newsgroup is empty, Gnus will exit
296 summary mode and go back to group mode.  If the value is neither nil
297 nor t, Gnus will select the following unread newsgroup.  In
298 particular, if the value is the symbol `quietly', the next unread
299 newsgroup will be selected without any confirmation, and if it is
300 `almost-quietly', the next group will be selected without any
301 confirmation if you are located on the last article in the group.
302 Finally, if this variable is `slightly-quietly', the `Z n' command
303 will go to the next group without confirmation."
304   :group 'gnus-summary-maneuvering
305   :type '(choice (const :tag "off" nil)
306                  (const quietly)
307                  (const almost-quietly)
308                  (const slightly-quietly)
309                  (sexp :menu-tag "on" t)))
310
311 (defcustom gnus-auto-select-same nil
312   "*If non-nil, select the next article with the same subject.
313 If there are no more articles with the same subject, go to
314 the first unread article."
315   :group 'gnus-summary-maneuvering
316   :type 'boolean)
317
318 (defcustom gnus-summary-check-current nil
319   "*If non-nil, consider the current article when moving.
320 The \"unread\" movement commands will stay on the same line if the
321 current article is unread."
322   :group 'gnus-summary-maneuvering
323   :type 'boolean)
324
325 (defcustom gnus-auto-center-summary t
326   "*If non-nil, always center the current summary buffer.
327 In particular, if `vertical' do only vertical recentering.  If non-nil
328 and non-`vertical', do both horizontal and vertical recentering."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "none" nil)
331                  (const vertical)
332                  (integer :tag "height")
333                  (sexp :menu-tag "both" t)))
334
335 (defcustom gnus-show-all-headers nil
336   "*If non-nil, don't hide any headers."
337   :group 'gnus-article-hiding
338   :group 'gnus-article-headers
339   :type 'boolean)
340
341 (defcustom gnus-summary-ignore-duplicates nil
342   "*If non-nil, ignore articles with identical Message-ID headers."
343   :group 'gnus-summary
344   :type 'boolean)
345
346 (defcustom gnus-single-article-buffer t
347   "*If non-nil, display all articles in the same buffer.
348 If nil, each group will get its own article buffer."
349   :group 'gnus-article-various
350   :type 'boolean)
351
352 (defcustom gnus-break-pages t
353   "*If non-nil, do page breaking on articles.
354 The page delimiter is specified by the `gnus-page-delimiter'
355 variable."
356   :group 'gnus-article-various
357   :type 'boolean)
358
359 (defcustom gnus-show-mime t
360   "*If non-nil, do mime processing of articles.
361 The articles will simply be fed to the function given by
362 `gnus-article-display-method-for-mime'."
363   :group 'gnus-article-mime
364   :type 'boolean)
365
366 (defcustom gnus-move-split-methods nil
367   "*Variable used to suggest where articles are to be moved to.
368 It uses the same syntax as the `gnus-split-methods' variable."
369   :group 'gnus-summary-mail
370   :type '(repeat (choice (list :value (fun) function)
371                          (cons :value ("" "") regexp (repeat string))
372                          (sexp :value nil))))
373
374 (defcustom gnus-unread-mark ? ;Whitespace
375   "*Mark used for unread articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-ticked-mark ?!
380   "*Mark used for ticked articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-dormant-mark ??
385   "*Mark used for dormant articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-del-mark ?r
390   "*Mark used for del'd articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-read-mark ?R
395   "*Mark used for read articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-expirable-mark ?E
400   "*Mark used for expirable articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-killed-mark ?K
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-souped-mark ?F
410   "*Mark used for killed articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-kill-file-mark ?X
415   "*Mark used for articles killed by kill files."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-low-score-mark ?Y
420   "*Mark used for articles with a low score."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-catchup-mark ?C
425   "*Mark used for articles that are caught up."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-replied-mark ?A
430   "*Mark used for articles that have been replied to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-cached-mark ?*
435   "*Mark used for articles that are in the cache."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-saved-mark ?S
440   "*Mark used for articles that have been saved to."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-ancient-mark ?O
445   "*Mark used for ancient articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-sparse-mark ?Q
450   "*Mark used for sparsely reffed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-canceled-mark ?G
455   "*Mark used for canceled articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-duplicate-mark ?M
460   "*Mark used for duplicate articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-undownloaded-mark ?@
465   "*Mark used for articles that weren't downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-downloadable-mark ?%
470   "*Mark used for articles that are to be downloaded."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unsendable-mark ?=
475   "*Mark used for articles that won't be sent."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-over-mark ?+
480   "*Score mark used for articles with high scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-score-below-mark ?-
485   "*Score mark used for articles with low scores."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-empty-thread-mark ? ;Whitespace
490   "*There is no thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-not-empty-thread-mark ?=
495   "*There is a thread under the article."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-view-pseudo-asynchronously nil
500   "*If non-nil, Gnus will view pseudo-articles asynchronously."
501   :group 'gnus-extract-view
502   :type 'boolean)
503
504 (defcustom gnus-auto-expirable-marks
505   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
506         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
507         gnus-souped-mark gnus-duplicate-mark)
508   "*The list of marks converted into expiration if a group is auto-expirable."
509   :group 'gnus-summary
510   :type '(repeat character))
511
512 (defcustom gnus-inhibit-user-auto-expire t
513   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
514   :group 'gnus-summary
515   :type 'boolean)
516
517 (defcustom gnus-view-pseudos nil
518   "*If `automatic', pseudo-articles will be viewed automatically.
519 If `not-confirm', pseudos will be viewed automatically, and the user
520 will not be asked to confirm the command."
521   :group 'gnus-extract-view
522   :type '(choice (const :tag "off" nil)
523                  (const automatic)
524                  (const not-confirm)))
525
526 (defcustom gnus-view-pseudos-separately t
527   "*If non-nil, one pseudo-article will be created for each file to be viewed.
528 If nil, all files that use the same viewing command will be given as a
529 list of parameters to that command."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-insert-pseudo-articles t
534   "*If non-nil, insert pseudo-articles when decoding articles."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-summary-dummy-line-format
539   "  %(:                          :%) %S\n"
540   "*The format specification for the dummy roots in the summary buffer.
541 It works along the same lines as a normal formatting string,
542 with some simple extensions.
543
544 %S  The subject"
545   :group 'gnus-threading
546   :type 'string)
547
548 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
549   "*The format specification for the summary mode line.
550 It works along the same lines as a normal formatting string,
551 with some simple extensions:
552
553 %G  Group name
554 %p  Unprefixed group name
555 %A  Current article number
556 %z  Current article score
557 %V  Gnus version
558 %U  Number of unread articles in the group
559 %e  Number of unselected articles in the group
560 %Z  A string with unread/unselected article counts
561 %g  Shortish group name
562 %S  Subject of the current article
563 %u  User-defined spec
564 %s  Current score file name
565 %d  Number of dormant articles
566 %r  Number of articles that have been marked as read in this session
567 %E  Number of articles expunged by the score files"
568   :group 'gnus-summary-format
569   :type 'string)
570
571 (defcustom gnus-list-identifiers nil
572   "Regexp that matches list identifiers to be removed from subject.
573 This can also be a list of regexps."
574   :group 'gnus-summary-format
575   :group 'gnus-article-hiding
576   :type '(choice (const :tag "none" nil)
577                  (regexp :value ".*")
578                  (repeat :value (".*") regexp)))
579
580 (defcustom gnus-summary-mark-below 0
581   "*Mark all articles with a score below this variable as read.
582 This variable is local to each summary buffer and usually set by the
583 score file."
584   :group 'gnus-score-default
585   :type 'integer)
586
587 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
588   "*List of functions used for sorting articles in the summary buffer.
589 This variable is only used when not using a threaded display."
590   :group 'gnus-summary-sort
591   :type '(repeat (choice (function-item gnus-article-sort-by-number)
592                          (function-item gnus-article-sort-by-author)
593                          (function-item gnus-article-sort-by-subject)
594                          (function-item gnus-article-sort-by-date)
595                          (function-item gnus-article-sort-by-score)
596                          (function :tag "other"))))
597
598 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
599   "*List of functions used for sorting threads in the summary buffer.
600 By default, threads are sorted by article number.
601
602 Each function takes two threads and return non-nil if the first thread
603 should be sorted before the other.  If you use more than one function,
604 the primary sort function should be the last.  You should probably
605 always include `gnus-thread-sort-by-number' in the list of sorting
606 functions -- preferably first.
607
608 Ready-made functions include `gnus-thread-sort-by-number',
609 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
610 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
611 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
612   :group 'gnus-summary-sort
613   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
614                          (function-item gnus-thread-sort-by-author)
615                          (function-item gnus-thread-sort-by-subject)
616                          (function-item gnus-thread-sort-by-date)
617                          (function-item gnus-thread-sort-by-score)
618                          (function-item gnus-thread-sort-by-total-score)
619                          (function :tag "other"))))
620
621 (defcustom gnus-thread-score-function '+
622   "*Function used for calculating the total score of a thread.
623
624 The function is called with the scores of the article and each
625 subthread and should then return the score of the thread.
626
627 Some functions you can use are `+', `max', or `min'."
628   :group 'gnus-summary-sort
629   :type 'function)
630
631 (defcustom gnus-summary-expunge-below nil
632   "All articles that have a score less than this variable will be expunged.
633 This variable is local to the summary buffers."
634   :group 'gnus-score-default
635   :type '(choice (const :tag "off" nil)
636                  integer))
637
638 (defcustom gnus-thread-expunge-below nil
639   "All threads that have a total score less than this variable will be expunged.
640 See `gnus-thread-score-function' for en explanation of what a
641 \"thread score\" is.
642
643 This variable is local to the summary buffers."
644   :group 'gnus-threading
645   :group 'gnus-score-default
646   :type '(choice (const :tag "off" nil)
647                  integer))
648
649 (defcustom gnus-summary-mode-hook nil
650   "*A hook for Gnus summary mode.
651 This hook is run before any variables are set in the summary buffer."
652   :group 'gnus-summary-various
653   :type 'hook)
654
655 (defcustom gnus-summary-menu-hook nil
656   "*Hook run after the creation of the summary mode menu."
657   :group 'gnus-summary-visual
658   :type 'hook)
659
660 (defcustom gnus-summary-exit-hook nil
661   "*A hook called on exit from the summary buffer.
662 It will be called with point in the group buffer."
663   :group 'gnus-summary-exit
664   :type 'hook)
665
666 (defcustom gnus-summary-prepare-hook nil
667   "*A hook called after the summary buffer has been generated.
668 If you want to modify the summary buffer, you can use this hook."
669   :group 'gnus-summary-various
670   :type 'hook)
671
672 (defcustom gnus-summary-prepared-hook nil
673   "*A hook called as the last thing after the summary buffer has been generated."
674   :group 'gnus-summary-various
675   :type 'hook)
676
677 (defcustom gnus-summary-generate-hook nil
678   "*A hook run just before generating the summary buffer.
679 This hook is commonly used to customize threading variables and the
680 like."
681   :group 'gnus-summary-various
682   :type 'hook)
683
684 (defcustom gnus-select-group-hook nil
685   "*A hook called when a newsgroup is selected.
686
687 If you'd like to simplify subjects like the
688 `gnus-summary-next-same-subject' command does, you can use the
689 following hook:
690
691  (setq gnus-select-group-hook
692       (list
693         (lambda ()
694           (mapcar (lambda (header)
695                      (mail-header-set-subject
696                       header
697                       (gnus-simplify-subject
698                        (mail-header-subject header) 're-only)))
699                   gnus-newsgroup-headers))))"
700   :group 'gnus-group-select
701   :type 'hook)
702
703 (defcustom gnus-select-article-hook nil
704   "*A hook called when an article is selected."
705   :group 'gnus-summary-choose
706   :type 'hook)
707
708 (defcustom gnus-visual-mark-article-hook
709   (list 'gnus-highlight-selected-summary)
710   "*Hook run after selecting an article in the summary buffer.
711 It is meant to be used for highlighting the article in some way.  It
712 is not run if `gnus-visual' is nil."
713   :group 'gnus-summary-visual
714   :type 'hook)
715
716 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
717   "*A hook called before parsing the headers."
718   :group 'gnus-various
719   :type 'hook)
720
721 (defcustom gnus-exit-group-hook nil
722   "*A hook called when exiting (not quitting) summary mode."
723   :group 'gnus-various
724   :type 'hook)
725
726 (defcustom gnus-summary-update-hook
727   (list 'gnus-summary-highlight-line)
728   "*A hook called when a summary line is changed.
729 The hook will not be called if `gnus-visual' is nil.
730
731 The default function `gnus-summary-highlight-line' will
732 highlight the line according to the `gnus-summary-highlight'
733 variable."
734   :group 'gnus-summary-visual
735   :type 'hook)
736
737 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
738   "*A hook called when an article is selected for the first time.
739 The hook is intended to mark an article as read (or unread)
740 automatically when it is selected."
741   :group 'gnus-summary-choose
742   :type 'hook)
743
744 (defcustom gnus-group-no-more-groups-hook nil
745   "*A hook run when returning to group mode having no more (unread) groups."
746   :group 'gnus-group-select
747   :type 'hook)
748
749 (defcustom gnus-ps-print-hook nil
750   "*A hook run before ps-printing something from Gnus."
751   :group 'gnus-summary
752   :type 'hook)
753
754 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
755   "Face used for highlighting the current article in the summary buffer."
756   :group 'gnus-summary-visual
757   :type 'face)
758
759 (defcustom gnus-summary-highlight
760   '(((= mark gnus-canceled-mark)
761      . gnus-summary-cancelled-face)
762     ((and (> score default)
763           (or (= mark gnus-dormant-mark)
764               (= mark gnus-ticked-mark)))
765      . gnus-summary-high-ticked-face)
766     ((and (< score default)
767           (or (= mark gnus-dormant-mark)
768               (= mark gnus-ticked-mark)))
769      . gnus-summary-low-ticked-face)
770     ((or (= mark gnus-dormant-mark)
771          (= mark gnus-ticked-mark))
772      . gnus-summary-normal-ticked-face)
773     ((and (> score default) (= mark gnus-ancient-mark))
774      . gnus-summary-high-ancient-face)
775     ((and (< score default) (= mark gnus-ancient-mark))
776      . gnus-summary-low-ancient-face)
777     ((= mark gnus-ancient-mark)
778      . gnus-summary-normal-ancient-face)
779     ((and (> score default) (= mark gnus-unread-mark))
780      . gnus-summary-high-unread-face)
781     ((and (< score default) (= mark gnus-unread-mark))
782      . gnus-summary-low-unread-face)
783     ((and (memq article gnus-newsgroup-incorporated) 
784           (= mark gnus-unread-mark))
785      . gnus-summary-incorporated-face)
786     ((= mark gnus-unread-mark)
787      . gnus-summary-normal-unread-face)
788     ((and (> score default) (memq mark (list gnus-downloadable-mark
789                                              gnus-undownloaded-mark)))
790      . gnus-summary-high-unread-face)
791     ((and (< score default) (memq mark (list gnus-downloadable-mark
792                                              gnus-undownloaded-mark)))
793      . gnus-summary-low-unread-face)
794     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
795      . gnus-summary-normal-unread-face)
796     ((> score default)
797      . gnus-summary-high-read-face)
798     ((< score default)
799      . gnus-summary-low-read-face)
800     (t
801      . gnus-summary-normal-read-face))
802   "*Controls the highlighting of summary buffer lines.
803
804 A list of (FORM . FACE) pairs.  When deciding how a a particular
805 summary line should be displayed, each form is evaluated.  The content
806 of the face field after the first true form is used.  You can change
807 how those summary lines are displayed, by editing the face field.
808
809 You can use the following variables in the FORM field.
810
811 score:   The articles score
812 default: The default article score.
813 below:   The score below which articles are automatically marked as read.
814 mark:    The articles mark."
815   :group 'gnus-summary-visual
816   :type '(repeat (cons (sexp :tag "Form" nil)
817                        face)))
818
819 (defcustom gnus-alter-header-function nil
820   "Function called to allow alteration of article header structures.
821 The function is called with one parameter, the article header vector,
822 which it may alter in any way.")
823
824 (defvar gnus-decode-encoded-word-function
825   (mime-find-field-decoder 'From 'nov)
826   "Variable that says which function should be used to decode a string with encoded words.")
827
828 (defcustom gnus-extra-headers nil
829   "*Extra headers to parse."
830   :group 'gnus-summary
831   :type '(repeat symbol))
832
833 (defcustom gnus-ignored-from-addresses
834   (and user-mail-address (regexp-quote user-mail-address))
835   "*Regexp of From headers that may be suppressed in favor of To headers."
836   :group 'gnus-summary
837   :type 'regexp)
838
839 (defcustom gnus-group-charset-alist
840   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
841     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
842     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
843     ("^relcom\\>" koi8-r)
844     ("^fido7\\>" koi8-r)
845     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
846     ("^israel\\>" iso-8859-1)
847     ("^han\\>" euc-kr)
848     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
849     (".*" iso-8859-1))
850   "Alist of regexps (to match group names) and default charsets to be used when reading."
851   :type '(repeat (list (regexp :tag "Group")
852                        (symbol :tag "Charset")))
853   :group 'gnus-charset)
854
855 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
856   "List of charsets that should be ignored.
857 When these charsets are used in the \"charset\" parameter, the
858 default charset will be used instead."
859   :type '(repeat symbol)
860   :group 'gnus-charset)
861
862 (defcustom gnus-group-ignored-charsets-alist 
863   '(("alt\\.chinese\\.text" iso-8859-1))
864   "Alist of regexps (to match group names) and charsets that should be ignored.
865 When these charsets are used in the \"charset\" parameter, the
866 default charset will be used instead."
867   :type '(repeat (cons (regexp :tag "Group")
868                        (repeat symbol)))
869   :group 'gnus-charset)
870
871 (defcustom gnus-group-highlight-words-alist nil
872   "Alist of group regexps and highlight regexps.
873 This variable uses the same syntax as `gnus-emphasis-alist'."
874   :type '(repeat (cons (regexp :tag "Group")
875                        (repeat (list (regexp :tag "Highlight regexp")
876                                      (number :tag "Group for entire word" 0)
877                                      (number :tag "Group for displayed part" 0)
878                                      (symbol :tag "Face" 
879                                              gnus-emphasis-highlight-words)))))
880   :group 'gnus-summary-visual)
881
882 (defcustom gnus-use-wheel nil
883   "Use Intelli-mouse on summary movement"
884   :type 'boolean
885   :group 'gnus-summary-maneuvering)
886
887 (defcustom gnus-wheel-scroll-amount '(5 . 1)
888   "Amount to scroll messages by spinning the mouse wheel.
889 This is actually a cons cell, where the first item is the amount to scroll
890 on a normal wheel event, and the second is the amount to scroll when the
891 wheel is moved with the shift key depressed."
892   :type '(cons (integer :tag "Shift") integer)
893   :group 'gnus-summary-maneuvering)
894
895 (defcustom gnus-wheel-edge-resistance 2
896   "How hard it should be to change the current article
897 by moving the mouse over the edge of the article window."
898   :type 'integer
899   :group 'gnus-summary-maneuvering)
900
901 ;;; Internal variables
902
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has 
915 been gathered together.")
916
917 ;; Avoid highlighting in kill files.
918 (defvar gnus-summary-inhibit-highlight nil)
919 (defvar gnus-newsgroup-selected-overlay nil)
920 (defvar gnus-inhibit-limiting nil)
921 (defvar gnus-newsgroup-adaptive-score-file nil)
922 (defvar gnus-current-score-file nil)
923 (defvar gnus-current-move-group nil)
924 (defvar gnus-current-copy-group nil)
925 (defvar gnus-current-crosspost-group nil)
926
927 (defvar gnus-newsgroup-dependencies nil)
928 (defvar gnus-newsgroup-adaptive nil)
929 (defvar gnus-summary-display-article-function nil)
930 (defvar gnus-summary-highlight-line-function nil
931   "Function called after highlighting a summary line.")
932
933 (defvar gnus-summary-line-format-alist
934   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
935     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
936     (?s gnus-tmp-subject-or-nil ?s)
937     (?n gnus-tmp-name ?s)
938     (?A (std11-address-string
939          (car (mime-read-field 'From gnus-tmp-header))) ?s)
940     (?a (or (std11-full-name-string
941              (car (mime-read-field 'From gnus-tmp-header)))
942             gnus-tmp-from) ?s)
943     (?F gnus-tmp-from ?s)
944     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
945     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
946     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
947     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
948     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
949     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
950     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
951     (?L gnus-tmp-lines ?d)
952     (?I gnus-tmp-indentation ?s)
953     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
954     (?R gnus-tmp-replied ?c)
955     (?\[ gnus-tmp-opening-bracket ?c)
956     (?\] gnus-tmp-closing-bracket ?c)
957     (?\> (make-string gnus-tmp-level ? ) ?s)
958     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
959     (?i gnus-tmp-score ?d)
960     (?z gnus-tmp-score-char ?c)
961     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
962     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
963     (?U gnus-tmp-unread ?c)
964     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
965     (?t (gnus-summary-number-of-articles-in-thread
966          (and (boundp 'thread) (car thread)) gnus-tmp-level)
967         ?d)
968     (?e (gnus-summary-number-of-articles-in-thread
969          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
970         ?c)
971     (?u gnus-tmp-user-defined ?s)
972     (?P (gnus-pick-line-number) ?d))
973   "An alist of format specifications that can appear in summary lines,
974 and what variables they correspond with, along with the type of the
975 variable (string, integer, character, etc).")
976
977 (defvar gnus-summary-dummy-line-format-alist
978   `((?S gnus-tmp-subject ?s)
979     (?N gnus-tmp-number ?d)
980     (?u gnus-tmp-user-defined ?s)))
981
982 (defvar gnus-summary-mode-line-format-alist
983   `((?G gnus-tmp-group-name ?s)
984     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
985     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
986     (?A gnus-tmp-article-number ?d)
987     (?Z gnus-tmp-unread-and-unselected ?s)
988     (?V gnus-version ?s)
989     (?U gnus-tmp-unread-and-unticked ?d)
990     (?S gnus-tmp-subject ?s)
991     (?e gnus-tmp-unselected ?d)
992     (?u gnus-tmp-user-defined ?s)
993     (?d (length gnus-newsgroup-dormant) ?d)
994     (?t (length gnus-newsgroup-marked) ?d)
995     (?r (length gnus-newsgroup-reads) ?d)
996     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
997     (?E gnus-newsgroup-expunged-tally ?d)
998     (?s (gnus-current-score-file-nondirectory) ?s)))
999
1000 (defvar gnus-last-search-regexp nil
1001   "Default regexp for article search command.")
1002
1003 (defvar gnus-last-shell-command nil
1004   "Default shell command on article.")
1005
1006 (defvar gnus-newsgroup-begin nil)
1007 (defvar gnus-newsgroup-end nil)
1008 (defvar gnus-newsgroup-last-rmail nil)
1009 (defvar gnus-newsgroup-last-mail nil)
1010 (defvar gnus-newsgroup-last-folder nil)
1011 (defvar gnus-newsgroup-last-file nil)
1012 (defvar gnus-newsgroup-auto-expire nil)
1013 (defvar gnus-newsgroup-active nil)
1014
1015 (defvar gnus-newsgroup-data nil)
1016 (defvar gnus-newsgroup-data-reverse nil)
1017 (defvar gnus-newsgroup-limit nil)
1018 (defvar gnus-newsgroup-limits nil)
1019
1020 (defvar gnus-newsgroup-unreads nil
1021   "List of unread articles in the current newsgroup.")
1022
1023 (defvar gnus-newsgroup-unselected nil
1024   "List of unselected unread articles in the current newsgroup.")
1025
1026 (defvar gnus-newsgroup-reads nil
1027   "Alist of read articles and article marks in the current newsgroup.")
1028
1029 (defvar gnus-newsgroup-expunged-tally nil)
1030
1031 (defvar gnus-newsgroup-marked nil
1032   "List of ticked articles in the current newsgroup (a subset of unread art).")
1033
1034 (defvar gnus-newsgroup-killed nil
1035   "List of ranges of articles that have been through the scoring process.")
1036
1037 (defvar gnus-newsgroup-cached nil
1038   "List of articles that come from the article cache.")
1039
1040 (defvar gnus-newsgroup-saved nil
1041   "List of articles that have been saved.")
1042
1043 (defvar gnus-newsgroup-kill-headers nil)
1044
1045 (defvar gnus-newsgroup-replied nil
1046   "List of articles that have been replied to in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-expirable nil
1049   "List of articles in the current newsgroup that can be expired.")
1050
1051 (defvar gnus-newsgroup-processable nil
1052   "List of articles in the current newsgroup that can be processed.")
1053
1054 (defvar gnus-newsgroup-downloadable nil
1055   "List of articles in the current newsgroup that can be processed.")
1056
1057 (defvar gnus-newsgroup-undownloaded nil
1058   "List of articles in the current newsgroup that haven't been downloaded..")
1059
1060 (defvar gnus-newsgroup-unsendable nil
1061   "List of articles in the current newsgroup that won't be sent.")
1062
1063 (defvar gnus-newsgroup-bookmarks nil
1064   "List of articles in the current newsgroup that have bookmarks.")
1065
1066 (defvar gnus-newsgroup-dormant nil
1067   "List of dormant articles in the current newsgroup.")
1068
1069 (defvar gnus-newsgroup-scored nil
1070   "List of scored articles in the current newsgroup.")
1071
1072 (defvar gnus-newsgroup-incorporated nil
1073   "List of incorporated articles in the current newsgroup.")
1074
1075 (defvar gnus-newsgroup-headers nil
1076   "List of article headers in the current newsgroup.")
1077
1078 (defvar gnus-newsgroup-threads nil)
1079
1080 (defvar gnus-newsgroup-prepared nil
1081   "Whether the current group has been prepared properly.")
1082
1083 (defvar gnus-newsgroup-ancient nil
1084   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-sparse nil)
1087
1088 (defvar gnus-current-article nil)
1089 (defvar gnus-article-current nil)
1090 (defvar gnus-current-headers nil)
1091 (defvar gnus-have-all-headers nil)
1092 (defvar gnus-last-article nil)
1093 (defvar gnus-newsgroup-history nil)
1094 (defvar gnus-newsgroup-charset nil)
1095 (defvar gnus-newsgroup-ephemeral-charset nil)
1096 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1097
1098 (defconst gnus-summary-local-variables
1099   '(gnus-newsgroup-name
1100     gnus-newsgroup-begin gnus-newsgroup-end
1101     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1102     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1103     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1104     gnus-newsgroup-unselected gnus-newsgroup-marked
1105     gnus-newsgroup-reads gnus-newsgroup-saved
1106     gnus-newsgroup-replied gnus-newsgroup-expirable
1107     gnus-newsgroup-processable gnus-newsgroup-killed
1108     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1109     gnus-newsgroup-unsendable
1110     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1111     gnus-newsgroup-headers gnus-newsgroup-threads
1112     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1113     gnus-current-article gnus-current-headers gnus-have-all-headers
1114     gnus-last-article gnus-article-internal-prepare-hook
1115     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1116     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1117     gnus-thread-expunge-below
1118     gnus-score-alist gnus-current-score-file
1119     (gnus-summary-expunge-below . global)
1120     (gnus-summary-mark-below . global)
1121     gnus-newsgroup-active gnus-scores-exclude-files
1122     gnus-newsgroup-history gnus-newsgroup-ancient
1123     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1124     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1125     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1126     (gnus-newsgroup-expunged-tally . 0)
1127     gnus-cache-removable-articles gnus-newsgroup-cached
1128     gnus-newsgroup-data gnus-newsgroup-data-reverse
1129     gnus-newsgroup-limit gnus-newsgroup-limits
1130     gnus-newsgroup-charset
1131     gnus-newsgroup-incorporated)
1132   "Variables that are buffer-local to the summary buffers.")
1133
1134 ;; Byte-compiler warning.
1135 (defvar gnus-article-mode-map)
1136
1137 ;; Subject simplification.
1138
1139 (defun gnus-simplify-whitespace (str)
1140   "Remove excessive whitespace."
1141   (let ((mystr str))
1142     ;; Multiple spaces.
1143     (while (string-match "[ \t][ \t]+" mystr)
1144       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1145                           " "
1146                           (substring mystr (match-end 0)))))
1147     ;; Leading spaces.
1148     (when (string-match "^[ \t]+" mystr)
1149       (setq mystr (substring mystr (match-end 0))))
1150     ;; Trailing spaces.
1151     (when (string-match "[ \t]+$" mystr)
1152       (setq mystr (substring mystr 0 (match-beginning 0))))
1153     mystr))
1154
1155 (defsubst gnus-simplify-subject-re (subject)
1156   "Remove \"Re:\" from subject lines."
1157   (if (string-match "^[Rr][Ee]: *" subject)
1158       (substring subject (match-end 0))
1159     subject))
1160
1161 (defun gnus-simplify-subject (subject &optional re-only)
1162   "Remove `Re:' and words in parentheses.
1163 If RE-ONLY is non-nil, strip leading `Re:'s only."
1164   (let ((case-fold-search t))           ;Ignore case.
1165     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1166     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1167       (setq subject (substring subject (match-end 0))))
1168     ;; Remove uninteresting prefixes.
1169     (when (and (not re-only)
1170                gnus-simplify-ignored-prefixes
1171                (string-match gnus-simplify-ignored-prefixes subject))
1172       (setq subject (substring subject (match-end 0))))
1173     ;; Remove words in parentheses from end.
1174     (unless re-only
1175       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1176         (setq subject (substring subject 0 (match-beginning 0)))))
1177     ;; Return subject string.
1178     subject))
1179
1180 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1181 ;; all whitespace.
1182 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1183   (goto-char (point-min))
1184   (while (re-search-forward regexp nil t)
1185       (replace-match (or newtext ""))))
1186
1187 (defun gnus-simplify-buffer-fuzzy ()
1188   "Simplify string in the buffer fuzzily.
1189 The string in the accessible portion of the current buffer is simplified.
1190 It is assumed to be a single-line subject.
1191 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1192 matter is removed.  Additional things can be deleted by setting
1193 gnus-simplify-subject-fuzzy-regexp."
1194   (let ((case-fold-search t)
1195         (modified-tick))
1196     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1197
1198     (while (not (eq modified-tick (buffer-modified-tick)))
1199       (setq modified-tick (buffer-modified-tick))
1200       (cond
1201        ((listp gnus-simplify-subject-fuzzy-regexp)
1202         (mapcar 'gnus-simplify-buffer-fuzzy-step
1203                 gnus-simplify-subject-fuzzy-regexp))
1204        (gnus-simplify-subject-fuzzy-regexp
1205         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1206       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1207       (gnus-simplify-buffer-fuzzy-step
1208        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1209       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1210
1211     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1212     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1213     (gnus-simplify-buffer-fuzzy-step " $")
1214     (gnus-simplify-buffer-fuzzy-step "^ +")))
1215
1216 (defun gnus-simplify-subject-fuzzy (subject)
1217   "Simplify a subject string fuzzily.
1218 See `gnus-simplify-buffer-fuzzy' for details."
1219   (save-excursion
1220     (gnus-set-work-buffer)
1221     (let ((case-fold-search t))
1222       ;; Remove uninteresting prefixes.
1223       (when (and gnus-simplify-ignored-prefixes
1224                  (string-match gnus-simplify-ignored-prefixes subject))
1225         (setq subject (substring subject (match-end 0))))
1226       (insert subject)
1227       (inline (gnus-simplify-buffer-fuzzy))
1228       (buffer-string))))
1229
1230 (defsubst gnus-simplify-subject-fully (subject)
1231   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1232   (cond
1233    (gnus-simplify-subject-functions
1234     (gnus-map-function gnus-simplify-subject-functions subject))
1235    ((null gnus-summary-gather-subject-limit)
1236     (gnus-simplify-subject-re subject))
1237    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1238     (gnus-simplify-subject-fuzzy subject))
1239    ((numberp gnus-summary-gather-subject-limit)
1240     (gnus-limit-string (gnus-simplify-subject-re subject)
1241                        gnus-summary-gather-subject-limit))
1242    (t
1243     subject)))
1244
1245 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1246   "Check whether two subjects are equal.
1247 If optional argument simple-first is t, first argument is already
1248 simplified."
1249   (cond
1250    ((null simple-first)
1251     (equal (gnus-simplify-subject-fully s1)
1252            (gnus-simplify-subject-fully s2)))
1253    (t
1254     (equal s1
1255            (gnus-simplify-subject-fully s2)))))
1256
1257 (defun gnus-summary-bubble-group ()
1258   "Increase the score of the current group.
1259 This is a handy function to add to `gnus-summary-exit-hook' to
1260 increase the score of each group you read."
1261   (gnus-group-add-score gnus-newsgroup-name))
1262
1263 \f
1264 ;;;
1265 ;;; Gnus summary mode
1266 ;;;
1267
1268 (put 'gnus-summary-mode 'mode-class 'special)
1269
1270 (when t
1271   ;; Non-orthogonal keys
1272
1273   (gnus-define-keys gnus-summary-mode-map
1274     " " gnus-summary-next-page
1275     "\177" gnus-summary-prev-page
1276     [delete] gnus-summary-prev-page
1277     [backspace] gnus-summary-prev-page
1278     "\r" gnus-summary-scroll-up
1279     "\M-\r" gnus-summary-scroll-down
1280     "n" gnus-summary-next-unread-article
1281     "p" gnus-summary-prev-unread-article
1282     "N" gnus-summary-next-article
1283     "P" gnus-summary-prev-article
1284     "\M-\C-n" gnus-summary-next-same-subject
1285     "\M-\C-p" gnus-summary-prev-same-subject
1286     "\M-n" gnus-summary-next-unread-subject
1287     "\M-p" gnus-summary-prev-unread-subject
1288     "." gnus-summary-first-unread-article
1289     "," gnus-summary-best-unread-article
1290     "\M-s" gnus-summary-search-article-forward
1291     "\M-r" gnus-summary-search-article-backward
1292     "<" gnus-summary-beginning-of-article
1293     ">" gnus-summary-end-of-article
1294     "j" gnus-summary-goto-article
1295     "^" gnus-summary-refer-parent-article
1296     "\M-^" gnus-summary-refer-article
1297     "u" gnus-summary-tick-article-forward
1298     "!" gnus-summary-tick-article-forward
1299     "U" gnus-summary-tick-article-backward
1300     "d" gnus-summary-mark-as-read-forward
1301     "D" gnus-summary-mark-as-read-backward
1302     "E" gnus-summary-mark-as-expirable
1303     "\M-u" gnus-summary-clear-mark-forward
1304     "\M-U" gnus-summary-clear-mark-backward
1305     "k" gnus-summary-kill-same-subject-and-select
1306     "\C-k" gnus-summary-kill-same-subject
1307     "\M-\C-k" gnus-summary-kill-thread
1308     "\M-\C-l" gnus-summary-lower-thread
1309     "e" gnus-summary-edit-article
1310     "#" gnus-summary-mark-as-processable
1311     "\M-#" gnus-summary-unmark-as-processable
1312     "\M-\C-t" gnus-summary-toggle-threads
1313     "\M-\C-s" gnus-summary-show-thread
1314     "\M-\C-h" gnus-summary-hide-thread
1315     "\M-\C-f" gnus-summary-next-thread
1316     "\M-\C-b" gnus-summary-prev-thread
1317     "\M-\C-u" gnus-summary-up-thread
1318     "\M-\C-d" gnus-summary-down-thread
1319     "&" gnus-summary-execute-command
1320     "c" gnus-summary-catchup-and-exit
1321     "\C-w" gnus-summary-mark-region-as-read
1322     "\C-t" gnus-summary-toggle-truncation
1323     "?" gnus-summary-mark-as-dormant
1324     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1325     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1326     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1327     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1328     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1329     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1330     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1331     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1332     "=" gnus-summary-expand-window
1333     "\C-x\C-s" gnus-summary-reselect-current-group
1334     "\M-g" gnus-summary-rescan-group
1335     "w" gnus-summary-stop-page-breaking
1336     "\C-c\C-r" gnus-summary-caesar-message
1337     "\M-t" gnus-summary-toggle-mime
1338     "f" gnus-summary-followup
1339     "F" gnus-summary-followup-with-original
1340     "C" gnus-summary-cancel-article
1341     "r" gnus-summary-reply
1342     "R" gnus-summary-reply-with-original
1343     "\C-c\C-f" gnus-summary-mail-forward
1344     "o" gnus-summary-save-article
1345     "\C-o" gnus-summary-save-article-mail
1346     "|" gnus-summary-pipe-output
1347     "\M-k" gnus-summary-edit-local-kill
1348     "\M-K" gnus-summary-edit-global-kill
1349     ;; "V" gnus-version
1350     "\C-c\C-d" gnus-summary-describe-group
1351     "q" gnus-summary-exit
1352     "Q" gnus-summary-exit-no-update
1353     "\C-c\C-i" gnus-info-find-node
1354     gnus-mouse-2 gnus-mouse-pick-article
1355     "m" gnus-summary-mail-other-window
1356     "a" gnus-summary-post-news
1357     "x" gnus-summary-limit-to-unread
1358     "s" gnus-summary-isearch-article
1359     "t" gnus-article-toggle-headers
1360     "g" gnus-summary-show-article
1361     "l" gnus-summary-goto-last-article
1362     "v" gnus-summary-preview-mime-message
1363     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1364     "\C-d" gnus-summary-enter-digest-group
1365     "\M-\C-d" gnus-summary-read-document
1366     "\M-\C-e" gnus-summary-edit-parameters
1367     "\M-\C-g" gnus-summary-customize-parameters
1368     "\C-c\C-b" gnus-bug
1369     "*" gnus-cache-enter-article
1370     "\M-*" gnus-cache-remove-article
1371     "\M-&" gnus-summary-universal-argument
1372     "\C-l" gnus-recenter
1373     "I" gnus-summary-increase-score
1374     "L" gnus-summary-lower-score
1375     "\M-i" gnus-symbolic-argument
1376     "h" gnus-summary-select-article-buffer
1377
1378     "V" gnus-summary-score-map
1379     "X" gnus-uu-extract-map
1380     "S" gnus-summary-send-map)
1381
1382   ;; Sort of orthogonal keymap
1383   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1384     "t" gnus-summary-tick-article-forward
1385     "!" gnus-summary-tick-article-forward
1386     "d" gnus-summary-mark-as-read-forward
1387     "r" gnus-summary-mark-as-read-forward
1388     "c" gnus-summary-clear-mark-forward
1389     " " gnus-summary-clear-mark-forward
1390     "e" gnus-summary-mark-as-expirable
1391     "x" gnus-summary-mark-as-expirable
1392     "?" gnus-summary-mark-as-dormant
1393     "b" gnus-summary-set-bookmark
1394     "B" gnus-summary-remove-bookmark
1395     "#" gnus-summary-mark-as-processable
1396     "\M-#" gnus-summary-unmark-as-processable
1397     "S" gnus-summary-limit-include-expunged
1398     "C" gnus-summary-catchup
1399     "H" gnus-summary-catchup-to-here
1400     "\C-c" gnus-summary-catchup-all
1401     "k" gnus-summary-kill-same-subject-and-select
1402     "K" gnus-summary-kill-same-subject
1403     "P" gnus-uu-mark-map)
1404
1405   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1406     "c" gnus-summary-clear-above
1407     "u" gnus-summary-tick-above
1408     "m" gnus-summary-mark-above
1409     "k" gnus-summary-kill-below)
1410
1411   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1412     "/" gnus-summary-limit-to-subject
1413     "n" gnus-summary-limit-to-articles
1414     "w" gnus-summary-pop-limit
1415     "s" gnus-summary-limit-to-subject
1416     "a" gnus-summary-limit-to-author
1417     "u" gnus-summary-limit-to-unread
1418     "m" gnus-summary-limit-to-marks
1419     "M" gnus-summary-limit-exclude-marks
1420     "v" gnus-summary-limit-to-score
1421     "*" gnus-summary-limit-include-cached
1422     "D" gnus-summary-limit-include-dormant
1423     "T" gnus-summary-limit-include-thread
1424     "d" gnus-summary-limit-exclude-dormant
1425     "t" gnus-summary-limit-to-age
1426     "x" gnus-summary-limit-to-extra 
1427     "E" gnus-summary-limit-include-expunged
1428     "c" gnus-summary-limit-exclude-childless-dormant
1429     "C" gnus-summary-limit-mark-excluded-as-read)
1430
1431   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1432     "n" gnus-summary-next-unread-article
1433     "p" gnus-summary-prev-unread-article
1434     "N" gnus-summary-next-article
1435     "P" gnus-summary-prev-article
1436     "\C-n" gnus-summary-next-same-subject
1437     "\C-p" gnus-summary-prev-same-subject
1438     "\M-n" gnus-summary-next-unread-subject
1439     "\M-p" gnus-summary-prev-unread-subject
1440     "f" gnus-summary-first-unread-article
1441     "b" gnus-summary-best-unread-article
1442     "j" gnus-summary-goto-article
1443     "g" gnus-summary-goto-subject
1444     "l" gnus-summary-goto-last-article
1445     "o" gnus-summary-pop-article)
1446
1447   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1448     "k" gnus-summary-kill-thread
1449     "l" gnus-summary-lower-thread
1450     "i" gnus-summary-raise-thread
1451     "T" gnus-summary-toggle-threads
1452     "t" gnus-summary-rethread-current
1453     "^" gnus-summary-reparent-thread
1454     "s" gnus-summary-show-thread
1455     "S" gnus-summary-show-all-threads
1456     "h" gnus-summary-hide-thread
1457     "H" gnus-summary-hide-all-threads
1458     "n" gnus-summary-next-thread
1459     "p" gnus-summary-prev-thread
1460     "u" gnus-summary-up-thread
1461     "o" gnus-summary-top-thread
1462     "d" gnus-summary-down-thread
1463     "#" gnus-uu-mark-thread
1464     "\M-#" gnus-uu-unmark-thread)
1465
1466   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1467     "g" gnus-summary-prepare
1468     "c" gnus-summary-insert-cached-articles)
1469
1470   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1471     "c" gnus-summary-catchup-and-exit
1472     "C" gnus-summary-catchup-all-and-exit
1473     "E" gnus-summary-exit-no-update
1474     "J" gnus-summary-jump-to-other-group
1475     "Q" gnus-summary-exit
1476     "Z" gnus-summary-exit
1477     "n" gnus-summary-catchup-and-goto-next-group
1478     "R" gnus-summary-reselect-current-group
1479     "G" gnus-summary-rescan-group
1480     "N" gnus-summary-next-group
1481     "s" gnus-summary-save-newsrc
1482     "P" gnus-summary-prev-group)
1483
1484   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1485     " " gnus-summary-next-page
1486     "n" gnus-summary-next-page
1487     "\177" gnus-summary-prev-page
1488     [delete] gnus-summary-prev-page
1489     "p" gnus-summary-prev-page
1490     "\r" gnus-summary-scroll-up
1491     "\M-\r" gnus-summary-scroll-down
1492     "<" gnus-summary-beginning-of-article
1493     ">" gnus-summary-end-of-article
1494     "b" gnus-summary-beginning-of-article
1495     "e" gnus-summary-end-of-article
1496     "^" gnus-summary-refer-parent-article
1497     "r" gnus-summary-refer-parent-article
1498     "D" gnus-summary-enter-digest-group
1499     "R" gnus-summary-refer-references
1500     "T" gnus-summary-refer-thread
1501     "g" gnus-summary-show-article
1502     "s" gnus-summary-isearch-article
1503     "P" gnus-summary-print-article
1504     "t" gnus-article-babel)
1505
1506   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1507     "b" gnus-article-add-buttons
1508     "B" gnus-article-add-buttons-to-head
1509     "o" gnus-article-treat-overstrike
1510     "e" gnus-article-emphasize
1511     "w" gnus-article-fill-cited-article
1512     "Q" gnus-article-fill-long-lines
1513     "C" gnus-article-capitalize-sentences
1514     "c" gnus-article-remove-cr
1515     "f" gnus-article-display-x-face
1516     "l" gnus-summary-stop-page-breaking
1517     "r" gnus-summary-caesar-message
1518     "t" gnus-article-toggle-headers
1519     "v" gnus-summary-verbose-headers
1520     "m" gnus-summary-toggle-mime
1521     "H" gnus-article-strip-headers-in-body
1522     "d" gnus-article-treat-dumbquotes)
1523
1524   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1525     "a" gnus-article-hide
1526     "h" gnus-article-toggle-headers
1527     "b" gnus-article-hide-boring-headers
1528     "s" gnus-article-hide-signature
1529     "c" gnus-article-hide-citation
1530     "C" gnus-article-hide-citation-in-followups
1531     "l" gnus-article-hide-list-identifiers
1532     "p" gnus-article-hide-pgp
1533     "B" gnus-article-strip-banner
1534     "P" gnus-article-hide-pem
1535     "\C-c" gnus-article-hide-citation-maybe)
1536
1537   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1538     "a" gnus-article-highlight
1539     "h" gnus-article-highlight-headers
1540     "c" gnus-article-highlight-citation
1541     "s" gnus-article-highlight-signature)
1542
1543   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1544     "z" gnus-article-date-ut
1545     "u" gnus-article-date-ut
1546     "l" gnus-article-date-local
1547     "e" gnus-article-date-lapsed
1548     "o" gnus-article-date-original
1549     "i" gnus-article-date-iso8601
1550     "s" gnus-article-date-user)
1551
1552   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1553     "t" gnus-article-remove-trailing-blank-lines
1554     "l" gnus-article-strip-leading-blank-lines
1555     "m" gnus-article-strip-multiple-blank-lines
1556     "a" gnus-article-strip-blank-lines
1557     "A" gnus-article-strip-all-blank-lines
1558     "s" gnus-article-strip-leading-space
1559     "e" gnus-article-strip-trailing-space)
1560
1561   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1562     "v" gnus-version
1563     "f" gnus-summary-fetch-faq
1564     "d" gnus-summary-describe-group
1565     "h" gnus-summary-describe-briefly
1566     "i" gnus-info-find-node)
1567
1568   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1569     "e" gnus-summary-expire-articles
1570     "\M-\C-e" gnus-summary-expire-articles-now
1571     "\177" gnus-summary-delete-article
1572     [delete] gnus-summary-delete-article
1573     [backspace] gnus-summary-delete-article
1574     "m" gnus-summary-move-article
1575     "r" gnus-summary-respool-article
1576     "w" gnus-summary-edit-article
1577     "c" gnus-summary-copy-article
1578     "B" gnus-summary-crosspost-article
1579     "q" gnus-summary-respool-query
1580     "t" gnus-summary-respool-trace
1581     "i" gnus-summary-import-article
1582     "p" gnus-summary-article-posted-p)
1583
1584   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1585     "o" gnus-summary-save-article
1586     "m" gnus-summary-save-article-mail
1587     "F" gnus-summary-write-article-file
1588     "r" gnus-summary-save-article-rmail
1589     "f" gnus-summary-save-article-file
1590     "b" gnus-summary-save-article-body-file
1591     "h" gnus-summary-save-article-folder
1592     "v" gnus-summary-save-article-vm
1593     "p" gnus-summary-pipe-output
1594     "s" gnus-soup-add-article)
1595
1596   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1597     "b" gnus-summary-display-buttonized
1598     "m" gnus-summary-repair-multipart
1599     "v" gnus-article-view-part
1600     "o" gnus-article-save-part
1601     "c" gnus-article-copy-part
1602     "e" gnus-article-externalize-part
1603     "i" gnus-article-inline-part
1604     "|" gnus-article-pipe-part)
1605   )
1606
1607 (defun gnus-summary-make-menu-bar ()
1608   (gnus-turn-off-edit-menu 'summary)
1609
1610   (unless (boundp 'gnus-summary-misc-menu)
1611
1612     (easy-menu-define
1613      gnus-summary-kill-menu gnus-summary-mode-map ""
1614      (cons
1615       "Score"
1616       (nconc
1617        (list
1618         ["Enter score..." gnus-summary-score-entry t]
1619         ["Customize" gnus-score-customize t])
1620        (gnus-make-score-map 'increase)
1621        (gnus-make-score-map 'lower)
1622        '(("Mark"
1623           ["Kill below" gnus-summary-kill-below t]
1624           ["Mark above" gnus-summary-mark-above t]
1625           ["Tick above" gnus-summary-tick-above t]
1626           ["Clear above" gnus-summary-clear-above t])
1627          ["Current score" gnus-summary-current-score t]
1628          ["Set score" gnus-summary-set-score t]
1629          ["Switch current score file..." gnus-score-change-score-file t]
1630          ["Set mark below..." gnus-score-set-mark-below t]
1631          ["Set expunge below..." gnus-score-set-expunge-below t]
1632          ["Edit current score file" gnus-score-edit-current-scores t]
1633          ["Edit score file" gnus-score-edit-file t]
1634          ["Trace score" gnus-score-find-trace t]
1635          ["Find words" gnus-score-find-favourite-words t]
1636          ["Rescore buffer" gnus-summary-rescore t]
1637          ["Increase score..." gnus-summary-increase-score t]
1638          ["Lower score..." gnus-summary-lower-score t]))))
1639
1640     ;; Define both the Article menu in the summary buffer and the equivalent
1641     ;; Commands menu in the article buffer here for consistency.
1642     (let ((innards
1643            '(("Hide"
1644               ["All" gnus-article-hide t]
1645               ["Headers" gnus-article-toggle-headers t]
1646               ["Signature" gnus-article-hide-signature t]
1647               ["Citation" gnus-article-hide-citation t]
1648               ["List identifiers" gnus-article-hide-list-identifiers t]
1649               ["PGP" gnus-article-hide-pgp t]
1650               ["Banner" gnus-article-strip-banner t]
1651               ["Boring headers" gnus-article-hide-boring-headers t])
1652              ("Highlight"
1653               ["All" gnus-article-highlight t]
1654               ["Headers" gnus-article-highlight-headers t]
1655               ["Signature" gnus-article-highlight-signature t]
1656               ["Citation" gnus-article-highlight-citation t])
1657              ("Date"
1658               ["Local" gnus-article-date-local t]
1659               ["ISO8601" gnus-article-date-iso8601 t]
1660               ["UT" gnus-article-date-ut t]
1661               ["Original" gnus-article-date-original t]
1662               ["Lapsed" gnus-article-date-lapsed t]
1663               ["User-defined" gnus-article-date-user t])
1664              ("Washing"
1665               ("Remove Blanks"
1666                ["Leading" gnus-article-strip-leading-blank-lines t]
1667                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1668                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1669                ["All of the above" gnus-article-strip-blank-lines t]
1670                ["All" gnus-article-strip-all-blank-lines t]
1671                ["Leading space" gnus-article-strip-leading-space t]
1672                ["Trailing space" gnus-article-strip-trailing-space t])
1673               ["Overstrike" gnus-article-treat-overstrike t]
1674               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1675               ["Emphasis" gnus-article-emphasize t]
1676               ["Word wrap" gnus-article-fill-cited-article t]
1677               ["Fill long lines" gnus-article-fill-long-lines t]
1678               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1679               ["CR" gnus-article-remove-cr t]
1680               ["Show X-Face" gnus-article-display-x-face t]
1681               ["Rot 13" gnus-summary-caesar-message t]
1682               ["Unix pipe" gnus-summary-pipe-message t]
1683               ["Add buttons" gnus-article-add-buttons t]
1684               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1685               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1686               ["Toggle MIME" gnus-summary-toggle-mime t]
1687               ["Verbose header" gnus-summary-verbose-headers t]
1688               ["Toggle header" gnus-summary-toggle-header t])
1689              ("Output"
1690               ["Save in default format" gnus-summary-save-article t]
1691               ["Save in file" gnus-summary-save-article-file t]
1692               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1693               ["Save in MH folder" gnus-summary-save-article-folder t]
1694               ["Save in VM folder" gnus-summary-save-article-vm t]
1695               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1696               ["Save body in file" gnus-summary-save-article-body-file t]
1697               ["Pipe through a filter" gnus-summary-pipe-output t]
1698               ["Add to SOUP packet" gnus-soup-add-article t]
1699               ["Print" gnus-summary-print-article t])
1700              ("Backend"
1701               ["Respool article..." gnus-summary-respool-article t]
1702               ["Move article..." gnus-summary-move-article
1703                (gnus-check-backend-function
1704                 'request-move-article gnus-newsgroup-name)]
1705               ["Copy article..." gnus-summary-copy-article t]
1706               ["Crosspost article..." gnus-summary-crosspost-article
1707                (gnus-check-backend-function
1708                 'request-replace-article gnus-newsgroup-name)]
1709               ["Import file..." gnus-summary-import-article t]
1710               ["Check if posted" gnus-summary-article-posted-p t]
1711               ["Edit article" gnus-summary-edit-article
1712                (not (gnus-group-read-only-p))]
1713               ["Delete article" gnus-summary-delete-article
1714                (gnus-check-backend-function
1715                 'request-expire-articles gnus-newsgroup-name)]
1716               ["Query respool" gnus-summary-respool-query t]
1717               ["Trace respool" gnus-summary-respool-trace t]
1718               ["Delete expirable articles" gnus-summary-expire-articles-now
1719                (gnus-check-backend-function
1720                 'request-expire-articles gnus-newsgroup-name)])
1721              ("Extract"
1722               ["Uudecode" gnus-uu-decode-uu t]
1723               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1724               ["Unshar" gnus-uu-decode-unshar t]
1725               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1726               ["Save" gnus-uu-decode-save t]
1727               ["Binhex" gnus-uu-decode-binhex t]
1728               ["Postscript" gnus-uu-decode-postscript t])
1729              ("Cache"
1730               ["Enter article" gnus-cache-enter-article t]
1731               ["Remove article" gnus-cache-remove-article t])
1732              ["Translate" gnus-article-babel t]
1733              ["Select article buffer" gnus-summary-select-article-buffer t]
1734              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1735              ["Isearch article..." gnus-summary-isearch-article t]
1736              ["Beginning of the article" gnus-summary-beginning-of-article t]
1737              ["End of the article" gnus-summary-end-of-article t]
1738              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1739              ["Fetch referenced articles" gnus-summary-refer-references t]
1740              ["Fetch current thread" gnus-summary-refer-thread t]
1741              ["Fetch article with id..." gnus-summary-refer-article t]
1742              ["Redisplay" gnus-summary-show-article t])))
1743       (easy-menu-define
1744        gnus-summary-article-menu gnus-summary-mode-map ""
1745        (cons "Article" innards))
1746
1747       (easy-menu-define
1748        gnus-article-commands-menu gnus-article-mode-map ""
1749        (cons "Commands" innards)))
1750
1751     (easy-menu-define
1752      gnus-summary-thread-menu gnus-summary-mode-map ""
1753      '("Threads"
1754        ["Toggle threading" gnus-summary-toggle-threads t]
1755        ["Hide threads" gnus-summary-hide-all-threads t]
1756        ["Show threads" gnus-summary-show-all-threads t]
1757        ["Hide thread" gnus-summary-hide-thread t]
1758        ["Show thread" gnus-summary-show-thread t]
1759        ["Go to next thread" gnus-summary-next-thread t]
1760        ["Go to previous thread" gnus-summary-prev-thread t]
1761        ["Go down thread" gnus-summary-down-thread t]
1762        ["Go up thread" gnus-summary-up-thread t]
1763        ["Top of thread" gnus-summary-top-thread t]
1764        ["Mark thread as read" gnus-summary-kill-thread t]
1765        ["Lower thread score" gnus-summary-lower-thread t]
1766        ["Raise thread score" gnus-summary-raise-thread t]
1767        ["Rethread current" gnus-summary-rethread-current t]
1768        ))
1769
1770     (easy-menu-define
1771      gnus-summary-post-menu gnus-summary-mode-map ""
1772      '("Post"
1773        ["Post an article" gnus-summary-post-news t]
1774        ["Followup" gnus-summary-followup t]
1775        ["Followup and yank" gnus-summary-followup-with-original t]
1776        ["Supersede article" gnus-summary-supersede-article t]
1777        ["Cancel article" gnus-summary-cancel-article t]
1778        ["Reply" gnus-summary-reply t]
1779        ["Reply and yank" gnus-summary-reply-with-original t]
1780        ["Wide reply" gnus-summary-wide-reply t]
1781        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1782        ["Mail forward" gnus-summary-mail-forward t]
1783        ["Post forward" gnus-summary-post-forward t]
1784        ["Digest and mail" gnus-summary-mail-digest t]
1785        ["Digest and post" gnus-summary-post-digest t]
1786        ["Resend message" gnus-summary-resend-message t]
1787        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1788        ["Send a mail" gnus-summary-mail-other-window t]
1789        ["Uuencode and post" gnus-uu-post-news t]
1790        ["Followup via news" gnus-summary-followup-to-mail t]
1791        ["Followup via news and yank"
1792         gnus-summary-followup-to-mail-with-original t]
1793        ;;("Draft"
1794        ;;["Send" gnus-summary-send-draft t]
1795        ;;["Send bounced" gnus-resend-bounced-mail t])
1796        ))
1797
1798     (easy-menu-define
1799      gnus-summary-misc-menu gnus-summary-mode-map ""
1800      '("Misc"
1801        ("Mark Read"
1802         ["Mark as read" gnus-summary-mark-as-read-forward t]
1803         ["Mark same subject and select"
1804          gnus-summary-kill-same-subject-and-select t]
1805         ["Mark same subject" gnus-summary-kill-same-subject t]
1806         ["Catchup" gnus-summary-catchup t]
1807         ["Catchup all" gnus-summary-catchup-all t]
1808         ["Catchup to here" gnus-summary-catchup-to-here t]
1809         ["Catchup region" gnus-summary-mark-region-as-read t]
1810         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1811        ("Mark Various"
1812         ["Tick" gnus-summary-tick-article-forward t]
1813         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1814         ["Remove marks" gnus-summary-clear-mark-forward t]
1815         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1816         ["Set bookmark" gnus-summary-set-bookmark t]
1817         ["Remove bookmark" gnus-summary-remove-bookmark t])
1818        ("Mark Limit"
1819         ["Marks..." gnus-summary-limit-to-marks t]
1820         ["Subject..." gnus-summary-limit-to-subject t]
1821         ["Author..." gnus-summary-limit-to-author t]
1822         ["Age..." gnus-summary-limit-to-age t]
1823         ["Extra..." gnus-summary-limit-to-extra t]
1824         ["Score" gnus-summary-limit-to-score t]
1825         ["Unread" gnus-summary-limit-to-unread t]
1826         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1827         ["Articles" gnus-summary-limit-to-articles t]
1828         ["Pop limit" gnus-summary-pop-limit t]
1829         ["Show dormant" gnus-summary-limit-include-dormant t]
1830         ["Hide childless dormant"
1831          gnus-summary-limit-exclude-childless-dormant t]
1832         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1833         ["Hide marked" gnus-summary-limit-exclude-marks t]
1834         ["Show expunged" gnus-summary-show-all-expunged t])
1835        ("Process Mark"
1836         ["Set mark" gnus-summary-mark-as-processable t]
1837         ["Remove mark" gnus-summary-unmark-as-processable t]
1838         ["Remove all marks" gnus-summary-unmark-all-processable t]
1839         ["Mark above" gnus-uu-mark-over t]
1840         ["Mark series" gnus-uu-mark-series t]
1841         ["Mark region" gnus-uu-mark-region t]
1842         ["Unmark region" gnus-uu-unmark-region t]
1843         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1844         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1845         ["Mark all" gnus-uu-mark-all t]
1846         ["Mark buffer" gnus-uu-mark-buffer t]
1847         ["Mark sparse" gnus-uu-mark-sparse t]
1848         ["Mark thread" gnus-uu-mark-thread t]
1849         ["Unmark thread" gnus-uu-unmark-thread t]
1850         ("Process Mark Sets"
1851          ["Kill" gnus-summary-kill-process-mark t]
1852          ["Yank" gnus-summary-yank-process-mark
1853           gnus-newsgroup-process-stack]
1854          ["Save" gnus-summary-save-process-mark t]))
1855        ("Scroll article"
1856         ["Page forward" gnus-summary-next-page t]
1857         ["Page backward" gnus-summary-prev-page t]
1858         ["Line forward" gnus-summary-scroll-up t])
1859        ("Move"
1860         ["Next unread article" gnus-summary-next-unread-article t]
1861         ["Previous unread article" gnus-summary-prev-unread-article t]
1862         ["Next article" gnus-summary-next-article t]
1863         ["Previous article" gnus-summary-prev-article t]
1864         ["Next unread subject" gnus-summary-next-unread-subject t]
1865         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1866         ["Next article same subject" gnus-summary-next-same-subject t]
1867         ["Previous article same subject" gnus-summary-prev-same-subject t]
1868         ["First unread article" gnus-summary-first-unread-article t]
1869         ["Best unread article" gnus-summary-best-unread-article t]
1870         ["Go to subject number..." gnus-summary-goto-subject t]
1871         ["Go to article number..." gnus-summary-goto-article t]
1872         ["Go to the last article" gnus-summary-goto-last-article t]
1873         ["Pop article off history" gnus-summary-pop-article t])
1874        ("Sort"
1875         ["Sort by number" gnus-summary-sort-by-number t]
1876         ["Sort by author" gnus-summary-sort-by-author t]
1877         ["Sort by subject" gnus-summary-sort-by-subject t]
1878         ["Sort by date" gnus-summary-sort-by-date t]
1879         ["Sort by score" gnus-summary-sort-by-score t]
1880         ["Sort by lines" gnus-summary-sort-by-lines t]
1881         ["Sort by characters" gnus-summary-sort-by-chars t])
1882        ("Help"
1883         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1884         ["Describe group" gnus-summary-describe-group t]
1885         ["Read manual" gnus-info-find-node t])
1886        ("Modes"
1887         ["Pick and read" gnus-pick-mode t]
1888         ["Binary" gnus-binary-mode t])
1889        ("Regeneration"
1890         ["Regenerate" gnus-summary-prepare t]
1891         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1892         ["Toggle threading" gnus-summary-toggle-threads t])
1893        ["Filter articles..." gnus-summary-execute-command t]
1894        ["Run command on subjects..." gnus-summary-universal-argument t]
1895        ["Search articles forward..." gnus-summary-search-article-forward t]
1896        ["Search articles backward..." gnus-summary-search-article-backward t]
1897        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1898        ["Expand window" gnus-summary-expand-window t]
1899        ["Expire expirable articles" gnus-summary-expire-articles
1900         (gnus-check-backend-function
1901          'request-expire-articles gnus-newsgroup-name)]
1902        ["Edit local kill file" gnus-summary-edit-local-kill t]
1903        ["Edit main kill file" gnus-summary-edit-global-kill t]
1904        ["Edit group parameters" gnus-summary-edit-parameters t]
1905        ["Customize group parameters" gnus-summary-customize-parameters t]
1906        ["Send a bug report" gnus-bug t]
1907        ("Exit"
1908         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1909         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1910         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1911         ["Exit group" gnus-summary-exit t]
1912         ["Exit group without updating" gnus-summary-exit-no-update t]
1913         ["Exit and goto next group" gnus-summary-next-group t]
1914         ["Exit and goto prev group" gnus-summary-prev-group t]
1915         ["Reselect group" gnus-summary-reselect-current-group t]
1916         ["Rescan group" gnus-summary-rescan-group t]
1917         ["Update dribble" gnus-summary-save-newsrc t])))
1918
1919     (gnus-run-hooks 'gnus-summary-menu-hook)))
1920
1921 (defun gnus-score-set-default (var value)
1922   "A version of set that updates the GNU Emacs menu-bar."
1923   (set var value)
1924   ;; It is the message that forces the active status to be updated.
1925   (message ""))
1926
1927 (defun gnus-make-score-map (type)
1928   "Make a summary score map of type TYPE."
1929   (if t
1930       nil
1931     (let ((headers '(("author" "from" string)
1932                      ("subject" "subject" string)
1933                      ("article body" "body" string)
1934                      ("article head" "head" string)
1935                      ("xref" "xref" string)
1936                      ("extra header" "extra" string)
1937                      ("lines" "lines" number)
1938                      ("followups to author" "followup" string)))
1939           (types '((number ("less than" <)
1940                            ("greater than" >)
1941                            ("equal" =))
1942                    (string ("substring" s)
1943                            ("exact string" e)
1944                            ("fuzzy string" f)
1945                            ("regexp" r))))
1946           (perms '(("temporary" (current-time-string))
1947                    ("permanent" nil)
1948                    ("immediate" now)))
1949           header)
1950       (list
1951        (apply
1952         'nconc
1953         (list
1954          (if (eq type 'lower)
1955              "Lower score"
1956            "Increase score"))
1957         (let (outh)
1958           (while headers
1959             (setq header (car headers))
1960             (setq outh
1961                   (cons
1962                    (apply
1963                     'nconc
1964                     (list (car header))
1965                     (let ((ts (cdr (assoc (nth 2 header) types)))
1966                           outt)
1967                       (while ts
1968                         (setq outt
1969                               (cons
1970                                (apply
1971                                 'nconc
1972                                 (list (caar ts))
1973                                 (let ((ps perms)
1974                                       outp)
1975                                   (while ps
1976                                     (setq outp
1977                                           (cons
1978                                            (vector
1979                                             (caar ps)
1980                                             (list
1981                                              'gnus-summary-score-entry
1982                                              (nth 1 header)
1983                                              (if (or (string= (nth 1 header)
1984                                                               "head")
1985                                                      (string= (nth 1 header)
1986                                                               "body"))
1987                                                  ""
1988                                                (list 'gnus-summary-header
1989                                                      (nth 1 header)))
1990                                              (list 'quote (nth 1 (car ts)))
1991                                              (list 'gnus-score-default nil)
1992                                              (nth 1 (car ps))
1993                                              t)
1994                                             t)
1995                                            outp))
1996                                     (setq ps (cdr ps)))
1997                                   (list (nreverse outp))))
1998                                outt))
1999                         (setq ts (cdr ts)))
2000                       (list (nreverse outt))))
2001                    outh))
2002             (setq headers (cdr headers)))
2003           (list (nreverse outh))))))))
2004
2005 \f
2006
2007 (defun gnus-summary-mode (&optional group)
2008   "Major mode for reading articles.
2009
2010 All normal editing commands are switched off.
2011 \\<gnus-summary-mode-map>
2012 Each line in this buffer represents one article.  To read an
2013 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2014 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2015 respectively.
2016
2017 You can also post articles and send mail from this buffer.  To
2018 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2019 of an article, type `\\[gnus-summary-reply]'.
2020
2021 There are approx. one gazillion commands you can execute in this
2022 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2023
2024 The following commands are available:
2025
2026 \\{gnus-summary-mode-map}"
2027   (interactive)
2028   (when (gnus-visual-p 'summary-menu 'menu)
2029     (gnus-summary-make-menu-bar))
2030   (kill-all-local-variables)
2031   (gnus-summary-make-local-variables)
2032   (gnus-make-thread-indent-array)
2033   (gnus-simplify-mode-line)
2034   (setq major-mode 'gnus-summary-mode)
2035   (setq mode-name "Summary")
2036   (make-local-variable 'minor-mode-alist)
2037   (use-local-map gnus-summary-mode-map)
2038   (buffer-disable-undo)
2039   (setq buffer-read-only t)             ;Disable modification
2040   (setq truncate-lines t)
2041   (setq selective-display t)
2042   (setq selective-display-ellipses t)   ;Display `...'
2043   (gnus-summary-set-display-table)
2044   (gnus-set-default-directory)
2045   (setq gnus-newsgroup-name group)
2046   (unless (gnus-news-group-p group)
2047     (setq gnus-newsgroup-incorporated
2048           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2049   (make-local-variable 'gnus-summary-line-format)
2050   (make-local-variable 'gnus-summary-line-format-spec)
2051   (make-local-variable 'gnus-summary-dummy-line-format)
2052   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2053   (make-local-variable 'gnus-summary-mark-positions)
2054   (make-local-hook 'pre-command-hook)
2055   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2056   (gnus-run-hooks 'gnus-summary-mode-hook)
2057   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2058   (gnus-update-summary-mark-positions))
2059
2060 (defun gnus-summary-make-local-variables ()
2061   "Make all the local summary buffer variables."
2062   (let (global)
2063     (dolist (local gnus-summary-local-variables)
2064       (if (consp local)
2065           (progn
2066             (if (eq (cdr local) 'global)
2067                 ;; Copy the global value of the variable.
2068                 (setq global (symbol-value (car local)))
2069               ;; Use the value from the list.
2070               (setq global (eval (cdr local))))
2071             (set (make-local-variable (car local)) global))
2072         ;; Simple nil-valued local variable.
2073         (set (make-local-variable local) nil)))))
2074
2075 (defun gnus-summary-clear-local-variables ()
2076   (let ((locals gnus-summary-local-variables))
2077     (while locals
2078       (if (consp (car locals))
2079           (and (vectorp (caar locals))
2080                (set (caar locals) nil))
2081         (and (vectorp (car locals))
2082              (set (car locals) nil)))
2083       (setq locals (cdr locals)))))
2084
2085 ;; Summary data functions.
2086
2087 (defmacro gnus-data-number (data)
2088   `(car ,data))
2089
2090 (defmacro gnus-data-set-number (data number)
2091   `(setcar ,data ,number))
2092
2093 (defmacro gnus-data-mark (data)
2094   `(nth 1 ,data))
2095
2096 (defmacro gnus-data-set-mark (data mark)
2097   `(setcar (nthcdr 1 ,data) ,mark))
2098
2099 (defmacro gnus-data-pos (data)
2100   `(nth 2 ,data))
2101
2102 (defmacro gnus-data-set-pos (data pos)
2103   `(setcar (nthcdr 2 ,data) ,pos))
2104
2105 (defmacro gnus-data-header (data)
2106   `(nth 3 ,data))
2107
2108 (defmacro gnus-data-set-header (data header)
2109   `(setf (nth 3 ,data) ,header))
2110
2111 (defmacro gnus-data-level (data)
2112   `(nth 4 ,data))
2113
2114 (defmacro gnus-data-unread-p (data)
2115   `(= (nth 1 ,data) gnus-unread-mark))
2116
2117 (defmacro gnus-data-read-p (data)
2118   `(/= (nth 1 ,data) gnus-unread-mark))
2119
2120 (defmacro gnus-data-pseudo-p (data)
2121   `(consp (nth 3 ,data)))
2122
2123 (defmacro gnus-data-find (number)
2124   `(assq ,number gnus-newsgroup-data))
2125
2126 (defmacro gnus-data-find-list (number &optional data)
2127   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2128      (memq (assq ,number bdata)
2129            bdata)))
2130
2131 (defmacro gnus-data-make (number mark pos header level)
2132   `(list ,number ,mark ,pos ,header ,level))
2133
2134 (defun gnus-data-enter (after-article number mark pos header level offset)
2135   (let ((data (gnus-data-find-list after-article)))
2136     (unless data
2137       (error "No such article: %d" after-article))
2138     (setcdr data (cons (gnus-data-make number mark pos header level)
2139                        (cdr data)))
2140     (setq gnus-newsgroup-data-reverse nil)
2141     (gnus-data-update-list (cddr data) offset)))
2142
2143 (defun gnus-data-enter-list (after-article list &optional offset)
2144   (when list
2145     (let ((data (and after-article (gnus-data-find-list after-article)))
2146           (ilist list))
2147       (if (not (or data
2148                    after-article))
2149           (let ((odata gnus-newsgroup-data))
2150             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2151             (when offset
2152               (gnus-data-update-list odata offset)))
2153         ;; Find the last element in the list to be spliced into the main
2154         ;; list.
2155         (while (cdr list)
2156           (setq list (cdr list)))
2157         (if (not data)
2158             (progn
2159               (setcdr list gnus-newsgroup-data)
2160               (setq gnus-newsgroup-data ilist)
2161               (when offset
2162                 (gnus-data-update-list (cdr list) offset)))
2163           (setcdr list (cdr data))
2164           (setcdr data ilist)
2165           (when offset
2166             (gnus-data-update-list (cdr list) offset))))
2167       (setq gnus-newsgroup-data-reverse nil))))
2168
2169 (defun gnus-data-remove (article &optional offset)
2170   (let ((data gnus-newsgroup-data))
2171     (if (= (gnus-data-number (car data)) article)
2172         (progn
2173           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2174                 gnus-newsgroup-data-reverse nil)
2175           (when offset
2176             (gnus-data-update-list gnus-newsgroup-data offset)))
2177       (while (cdr data)
2178         (when (= (gnus-data-number (cadr data)) article)
2179           (setcdr data (cddr data))
2180           (when offset
2181             (gnus-data-update-list (cdr data) offset))
2182           (setq data nil
2183                 gnus-newsgroup-data-reverse nil))
2184         (setq data (cdr data))))))
2185
2186 (defmacro gnus-data-list (backward)
2187   `(if ,backward
2188        (or gnus-newsgroup-data-reverse
2189            (setq gnus-newsgroup-data-reverse
2190                  (reverse gnus-newsgroup-data)))
2191      gnus-newsgroup-data))
2192
2193 (defun gnus-data-update-list (data offset)
2194   "Add OFFSET to the POS of all data entries in DATA."
2195   (setq gnus-newsgroup-data-reverse nil)
2196   (while data
2197     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2198     (setq data (cdr data))))
2199
2200 (defun gnus-summary-article-pseudo-p (article)
2201   "Say whether this article is a pseudo article or not."
2202   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2203
2204 (defmacro gnus-summary-article-sparse-p (article)
2205   "Say whether this article is a sparse article or not."
2206   `(memq ,article gnus-newsgroup-sparse))
2207
2208 (defmacro gnus-summary-article-ancient-p (article)
2209   "Say whether this article is a sparse article or not."
2210   `(memq ,article gnus-newsgroup-ancient))
2211
2212 (defun gnus-article-parent-p (number)
2213   "Say whether this article is a parent or not."
2214   (let ((data (gnus-data-find-list number)))
2215     (and (cdr data)                     ; There has to be an article after...
2216          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2217             (gnus-data-level (nth 1 data))))))
2218
2219 (defun gnus-article-children (number)
2220   "Return a list of all children to NUMBER."
2221   (let* ((data (gnus-data-find-list number))
2222          (level (gnus-data-level (car data)))
2223          children)
2224     (setq data (cdr data))
2225     (while (and data
2226                 (= (gnus-data-level (car data)) (1+ level)))
2227       (push (gnus-data-number (car data)) children)
2228       (setq data (cdr data)))
2229     children))
2230
2231 (defmacro gnus-summary-skip-intangible ()
2232   "If the current article is intangible, then jump to a different article."
2233   '(let ((to (get-text-property (point) 'gnus-intangible)))
2234      (and to (gnus-summary-goto-subject to))))
2235
2236 (defmacro gnus-summary-article-intangible-p ()
2237   "Say whether this article is intangible or not."
2238   '(get-text-property (point) 'gnus-intangible))
2239
2240 (defun gnus-article-read-p (article)
2241   "Say whether ARTICLE is read or not."
2242   (not (or (memq article gnus-newsgroup-marked)
2243            (memq article gnus-newsgroup-unreads)
2244            (memq article gnus-newsgroup-unselected)
2245            (memq article gnus-newsgroup-dormant))))
2246
2247 ;; Some summary mode macros.
2248
2249 (defmacro gnus-summary-article-number ()
2250   "The article number of the article on the current line.
2251 If there isn's an article number here, then we return the current
2252 article number."
2253   '(progn
2254      (gnus-summary-skip-intangible)
2255      (or (get-text-property (point) 'gnus-number)
2256          (gnus-summary-last-subject))))
2257
2258 (defmacro gnus-summary-article-header (&optional number)
2259   "Return the header of article NUMBER."
2260   `(gnus-data-header (gnus-data-find
2261                       ,(or number '(gnus-summary-article-number)))))
2262
2263 (defmacro gnus-summary-thread-level (&optional number)
2264   "Return the level of thread that starts with article NUMBER."
2265   `(if (and (eq gnus-summary-make-false-root 'dummy)
2266             (get-text-property (point) 'gnus-intangible))
2267        0
2268      (gnus-data-level (gnus-data-find
2269                        ,(or number '(gnus-summary-article-number))))))
2270
2271 (defmacro gnus-summary-article-mark (&optional number)
2272   "Return the mark of article NUMBER."
2273   `(gnus-data-mark (gnus-data-find
2274                     ,(or number '(gnus-summary-article-number)))))
2275
2276 (defmacro gnus-summary-article-pos (&optional number)
2277   "Return the position of the line of article NUMBER."
2278   `(gnus-data-pos (gnus-data-find
2279                    ,(or number '(gnus-summary-article-number)))))
2280
2281 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2282 (defmacro gnus-summary-article-subject (&optional number)
2283   "Return current subject string or nil if nothing."
2284   `(let ((headers
2285           ,(if number
2286                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2287              '(gnus-data-header (assq (gnus-summary-article-number)
2288                                       gnus-newsgroup-data)))))
2289      (and headers
2290           (vectorp headers)
2291           (mail-header-subject headers))))
2292
2293 (defmacro gnus-summary-article-score (&optional number)
2294   "Return current article score."
2295   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2296                   gnus-newsgroup-scored))
2297        gnus-summary-default-score 0))
2298
2299 (defun gnus-summary-article-children (&optional number)
2300   "Return a list of article numbers that are children of article NUMBER."
2301   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2302          (level (gnus-data-level (car data)))
2303          l children)
2304     (while (and (setq data (cdr data))
2305                 (> (setq l (gnus-data-level (car data))) level))
2306       (and (= (1+ level) l)
2307            (push (gnus-data-number (car data))
2308                  children)))
2309     (nreverse children)))
2310
2311 (defun gnus-summary-article-parent (&optional number)
2312   "Return the article number of the parent of article NUMBER."
2313   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2314                                     (gnus-data-list t)))
2315          (level (gnus-data-level (car data))))
2316     (if (zerop level)
2317         ()                              ; This is a root.
2318       ;; We search until we find an article with a level less than
2319       ;; this one.  That function has to be the parent.
2320       (while (and (setq data (cdr data))
2321                   (not (< (gnus-data-level (car data)) level))))
2322       (and data (gnus-data-number (car data))))))
2323
2324 (defun gnus-unread-mark-p (mark)
2325   "Say whether MARK is the unread mark."
2326   (= mark gnus-unread-mark))
2327
2328 (defun gnus-read-mark-p (mark)
2329   "Say whether MARK is one of the marks that mark as read.
2330 This is all marks except unread, ticked, dormant, and expirable."
2331   (not (or (= mark gnus-unread-mark)
2332            (= mark gnus-ticked-mark)
2333            (= mark gnus-dormant-mark)
2334            (= mark gnus-expirable-mark))))
2335
2336 (defmacro gnus-article-mark (number)
2337   "Return the MARK of article NUMBER.
2338 This macro should only be used when computing the mark the \"first\"
2339 time; i.e., when generating the summary lines.  After that,
2340 `gnus-summary-article-mark' should be used to examine the
2341 marks of articles."
2342   `(cond
2343     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2344     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2345     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2346     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2347     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2348     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2349     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2350     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2351            gnus-ancient-mark))))
2352
2353 ;; Saving hidden threads.
2354
2355 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2356 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2357
2358 (defmacro gnus-save-hidden-threads (&rest forms)
2359   "Save hidden threads, eval FORMS, and restore the hidden threads."
2360   (let ((config (make-symbol "config")))
2361     `(let ((,config (gnus-hidden-threads-configuration)))
2362        (unwind-protect
2363            (save-excursion
2364              ,@forms)
2365          (gnus-restore-hidden-threads-configuration ,config)))))
2366
2367 (defun gnus-data-compute-positions ()
2368   "Compute the positions of all articles."
2369   (setq gnus-newsgroup-data-reverse nil)
2370   (let ((data gnus-newsgroup-data))
2371     (save-excursion
2372       (gnus-save-hidden-threads
2373         (gnus-summary-show-all-threads)
2374         (goto-char (point-min))
2375         (while data
2376           (while (get-text-property (point) 'gnus-intangible)
2377             (forward-line 1))
2378           (gnus-data-set-pos (car data) (+ (point) 3))
2379           (setq data (cdr data))
2380           (forward-line 1))))))
2381
2382 (defun gnus-hidden-threads-configuration ()
2383   "Return the current hidden threads configuration."
2384   (save-excursion
2385     (let (config)
2386       (goto-char (point-min))
2387       (while (search-forward "\r" nil t)
2388         (push (1- (point)) config))
2389       config)))
2390
2391 (defun gnus-restore-hidden-threads-configuration (config)
2392   "Restore hidden threads configuration from CONFIG."
2393   (let (point buffer-read-only)
2394     (while (setq point (pop config))
2395       (when (and (< point (point-max))
2396                  (goto-char point)
2397                  (eq (char-after) ?\n))
2398         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2399
2400 ;; Various summary mode internalish functions.
2401
2402 (defun gnus-mouse-pick-article (e)
2403   (interactive "e")
2404   (mouse-set-point e)
2405   (gnus-summary-next-page nil t))
2406
2407 (defun gnus-summary-set-display-table ()
2408   ;; Change the display table.  Odd characters have a tendency to mess
2409   ;; up nicely formatted displays - we make all possible glyphs
2410   ;; display only a single character.
2411
2412   ;; We start from the standard display table, if any.
2413   (let ((table (or (copy-sequence standard-display-table)
2414                    (make-display-table)))
2415         (i 32))
2416     ;; Nix out all the control chars...
2417     (while (>= (setq i (1- i)) 0)
2418       (aset table i [??]))
2419     ;; ... but not newline and cr, of course.  (cr is necessary for the
2420     ;; selective display).
2421     (aset table ?\n nil)
2422     (aset table ?\r nil)
2423     ;; We keep TAB as well.
2424     (aset table ?\t nil)
2425     ;; We nix out any glyphs over 126 that are not set already.
2426     (let ((i 256))
2427       (while (>= (setq i (1- i)) 127)
2428         ;; Only modify if the entry is nil.
2429         (unless (aref table i)
2430           (aset table i [??]))))
2431     (setq buffer-display-table table)))
2432
2433 (defun gnus-summary-setup-buffer (group)
2434   "Initialize summary buffer."
2435   (let ((buffer (concat "*Summary " group "*")))
2436     (if (get-buffer buffer)
2437         (progn
2438           (set-buffer buffer)
2439           (setq gnus-summary-buffer (current-buffer))
2440           (not gnus-newsgroup-prepared))
2441       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2442       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2443       (gnus-summary-mode group)
2444       (when gnus-carpal
2445         (gnus-carpal-setup-buffer 'summary))
2446       (unless gnus-single-article-buffer
2447         (make-local-variable 'gnus-article-buffer)
2448         (make-local-variable 'gnus-article-current)
2449         (make-local-variable 'gnus-original-article-buffer))
2450       (setq gnus-newsgroup-name group)
2451       t)))
2452
2453 (defun gnus-set-global-variables ()
2454   ;; Set the global equivalents of the summary buffer-local variables
2455   ;; to the latest values they had.  These reflect the summary buffer
2456   ;; that was in action when the last article was fetched.
2457   (when (eq major-mode 'gnus-summary-mode)
2458     (setq gnus-summary-buffer (current-buffer))
2459     (let ((name gnus-newsgroup-name)
2460           (marked gnus-newsgroup-marked)
2461           (unread gnus-newsgroup-unreads)
2462           (headers gnus-current-headers)
2463           (data gnus-newsgroup-data)
2464           (summary gnus-summary-buffer)
2465           (article-buffer gnus-article-buffer)
2466           (original gnus-original-article-buffer)
2467           (gac gnus-article-current)
2468           (reffed gnus-reffed-article-number)
2469           (score-file gnus-current-score-file)
2470           (default-charset gnus-newsgroup-charset))
2471       (save-excursion
2472         (set-buffer gnus-group-buffer)
2473         (setq gnus-newsgroup-name name
2474               gnus-newsgroup-marked marked
2475               gnus-newsgroup-unreads unread
2476               gnus-current-headers headers
2477               gnus-newsgroup-data data
2478               gnus-article-current gac
2479               gnus-summary-buffer summary
2480               gnus-article-buffer article-buffer
2481               gnus-original-article-buffer original
2482               gnus-reffed-article-number reffed
2483               gnus-current-score-file score-file
2484               gnus-newsgroup-charset default-charset)
2485         ;; The article buffer also has local variables.
2486         (when (gnus-buffer-live-p gnus-article-buffer)
2487           (set-buffer gnus-article-buffer)
2488           (setq gnus-summary-buffer summary))))))
2489
2490 (defun gnus-summary-article-unread-p (article)
2491   "Say whether ARTICLE is unread or not."
2492   (memq article gnus-newsgroup-unreads))
2493
2494 (defun gnus-summary-first-article-p (&optional article)
2495   "Return whether ARTICLE is the first article in the buffer."
2496   (if (not (setq article (or article (gnus-summary-article-number))))
2497       nil
2498     (eq article (caar gnus-newsgroup-data))))
2499
2500 (defun gnus-summary-last-article-p (&optional article)
2501   "Return whether ARTICLE is the last article in the buffer."
2502   (if (not (setq article (or article (gnus-summary-article-number))))
2503       ;; All non-existent numbers are the last article.  :-)
2504       t
2505     (not (cdr (gnus-data-find-list article)))))
2506
2507 (defun gnus-make-thread-indent-array ()
2508   (let ((n 200))
2509     (unless (and gnus-thread-indent-array
2510                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2511       (setq gnus-thread-indent-array (make-vector 201 "")
2512             gnus-thread-indent-array-level gnus-thread-indent-level)
2513       (while (>= n 0)
2514         (aset gnus-thread-indent-array n
2515               (make-string (* n gnus-thread-indent-level) ? ))
2516         (setq n (1- n))))))
2517
2518 (defun gnus-update-summary-mark-positions ()
2519   "Compute where the summary marks are to go."
2520   (save-excursion
2521     (when (gnus-buffer-exists-p gnus-summary-buffer)
2522       (set-buffer gnus-summary-buffer))
2523     (let ((gnus-replied-mark 129)
2524           (gnus-score-below-mark 130)
2525           (gnus-score-over-mark 130)
2526           (gnus-download-mark 131)
2527           (spec gnus-summary-line-format-spec)
2528           gnus-visual pos)
2529       (save-excursion
2530         (gnus-set-work-buffer)
2531         (let ((gnus-summary-line-format-spec spec)
2532               (gnus-newsgroup-downloadable '((0 . t))))
2533           (gnus-summary-insert-line
2534            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2535            0 nil 128 t nil "" nil 1)
2536           (goto-char (point-min))
2537           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2538                                              (- (point) 2)))))
2539           (goto-char (point-min))
2540           (push (cons 'replied (and (search-forward "\201" nil t)
2541                                     (- (point) 2)))
2542                 pos)
2543           (goto-char (point-min))
2544           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2545                 pos)
2546           (goto-char (point-min))
2547           (push (cons 'download
2548                       (and (search-forward "\203" nil t) (- (point) 2)))
2549                 pos)))
2550       (setq gnus-summary-mark-positions pos))))
2551
2552 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2553   "Insert a dummy root in the summary buffer."
2554   (beginning-of-line)
2555   (gnus-add-text-properties
2556    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2557    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2558
2559 (defun gnus-summary-from-or-to-or-newsgroups (header)
2560   (let ((to (cdr (assq 'To (mail-header-extra header))))
2561         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2562         (default-mime-charset (with-current-buffer gnus-summary-buffer
2563                                 default-mime-charset)))
2564     (cond
2565      ((and to
2566            gnus-ignored-from-addresses
2567            (string-match gnus-ignored-from-addresses
2568                          (mail-header-from header)))
2569       (concat "-> "
2570               (or (car (funcall gnus-extract-address-components
2571                                 (funcall
2572                                  gnus-decode-encoded-word-function to)))
2573                   (funcall gnus-decode-encoded-word-function to))))
2574      ((and newsgroups
2575            gnus-ignored-from-addresses
2576            (string-match gnus-ignored-from-addresses
2577                          (mail-header-from header)))
2578       (concat "=> " newsgroups))
2579      (t
2580       (or (car (funcall gnus-extract-address-components
2581                         (mail-header-from header)))
2582           (mail-header-from header))))))
2583
2584 (defun gnus-summary-insert-line (gnus-tmp-header
2585                                  gnus-tmp-level gnus-tmp-current
2586                                  gnus-tmp-unread gnus-tmp-replied
2587                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2588                                  &optional gnus-tmp-dummy gnus-tmp-score
2589                                  gnus-tmp-process)
2590   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2591          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2592          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2593          (gnus-tmp-score-char
2594           (if (or (null gnus-summary-default-score)
2595                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2596                       gnus-summary-zcore-fuzz))
2597               ? ;Whitespace
2598             (if (< gnus-tmp-score gnus-summary-default-score)
2599                 gnus-score-below-mark gnus-score-over-mark)))
2600          (gnus-tmp-replied
2601           (cond (gnus-tmp-process gnus-process-mark)
2602                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2603                  gnus-cached-mark)
2604                 (gnus-tmp-replied gnus-replied-mark)
2605                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2606                  gnus-saved-mark)
2607                 (t gnus-unread-mark)))
2608          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2609          (gnus-tmp-name
2610           (cond
2611            ((string-match "<[^>]+> *$" gnus-tmp-from)
2612             (let ((beg (match-beginning 0)))
2613               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2614                        (substring gnus-tmp-from (1+ (match-beginning 0))
2615                                   (1- (match-end 0))))
2616                   (substring gnus-tmp-from 0 beg))))
2617            ((string-match "(.+)" gnus-tmp-from)
2618             (substring gnus-tmp-from
2619                        (1+ (match-beginning 0)) (1- (match-end 0))))
2620            (t gnus-tmp-from)))
2621          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2622          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2623          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2624          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2625          (buffer-read-only nil))
2626     (when (string= gnus-tmp-name "")
2627       (setq gnus-tmp-name gnus-tmp-from))
2628     (unless (numberp gnus-tmp-lines)
2629       (setq gnus-tmp-lines 0))
2630     (gnus-put-text-property-excluding-characters-with-faces
2631      (point)
2632      (progn (eval gnus-summary-line-format-spec) (point))
2633      'gnus-number gnus-tmp-number)
2634     (when (gnus-visual-p 'summary-highlight 'highlight)
2635       (forward-line -1)
2636       (gnus-run-hooks 'gnus-summary-update-hook)
2637       (forward-line 1))))
2638
2639 (defun gnus-summary-update-line (&optional dont-update)
2640   ;; Update summary line after change.
2641   (when (and gnus-summary-default-score
2642              (not gnus-summary-inhibit-highlight))
2643     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2644            (article (gnus-summary-article-number))
2645            (score (gnus-summary-article-score article)))
2646       (unless dont-update
2647         (if (and gnus-summary-mark-below
2648                  (< (gnus-summary-article-score)
2649                     gnus-summary-mark-below))
2650             ;; This article has a low score, so we mark it as read.
2651             (when (memq article gnus-newsgroup-unreads)
2652               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2653           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2654             ;; This article was previously marked as read on account
2655             ;; of a low score, but now it has risen, so we mark it as
2656             ;; unread.
2657             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2658         (gnus-summary-update-mark
2659          (if (or (null gnus-summary-default-score)
2660                  (<= (abs (- score gnus-summary-default-score))
2661                      gnus-summary-zcore-fuzz))
2662              ? ;Whitespace
2663            (if (< score gnus-summary-default-score)
2664                gnus-score-below-mark gnus-score-over-mark))
2665          'score))
2666       ;; Do visual highlighting.
2667       (when (gnus-visual-p 'summary-highlight 'highlight)
2668         (gnus-run-hooks 'gnus-summary-update-hook)))))
2669
2670 (defvar gnus-tmp-new-adopts nil)
2671
2672 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2673   "Return the number of articles in THREAD.
2674 This may be 0 in some cases -- if none of the articles in
2675 the thread are to be displayed."
2676   (let* ((number
2677           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2678           (cond
2679            ((not (listp thread))
2680             1)
2681            ((and (consp thread) (cdr thread))
2682             (apply
2683              '+ 1 (mapcar
2684                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2685            ((null thread)
2686             1)
2687            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2688             1)
2689            (t 0))))
2690     (when (and level (zerop level) gnus-tmp-new-adopts)
2691       (incf number
2692             (apply '+ (mapcar
2693                        'gnus-summary-number-of-articles-in-thread
2694                        gnus-tmp-new-adopts))))
2695     (if char
2696         (if (> number 1) gnus-not-empty-thread-mark
2697           gnus-empty-thread-mark)
2698       number)))
2699
2700 (defun gnus-summary-set-local-parameters (group)
2701   "Go through the local params of GROUP and set all variable specs in that list."
2702   (let ((params (gnus-group-find-parameter group))
2703         elem)
2704     (while params
2705       (setq elem (car params)
2706             params (cdr params))
2707       (and (consp elem)                 ; Has to be a cons.
2708            (consp (cdr elem))           ; The cdr has to be a list.
2709            (symbolp (car elem))         ; Has to be a symbol in there.
2710            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2711            (ignore-errors               ; So we set it.
2712              (make-local-variable (car elem))
2713              (set (car elem) (eval (nth 1 elem))))))))
2714
2715 (defun gnus-summary-read-group (group &optional show-all no-article
2716                                       kill-buffer no-display backward
2717                                       select-articles)
2718   "Start reading news in newsgroup GROUP.
2719 If SHOW-ALL is non-nil, already read articles are also listed.
2720 If NO-ARTICLE is non-nil, no article is selected initially.
2721 If NO-DISPLAY, don't generate a summary buffer."
2722   (let (result)
2723     (while (and group
2724                 (null (setq result
2725                             (let ((gnus-auto-select-next nil))
2726                               (or (gnus-summary-read-group-1
2727                                    group show-all no-article
2728                                    kill-buffer no-display
2729                                    select-articles)
2730                                   (setq show-all nil
2731                                         select-articles nil)))))
2732                 (eq gnus-auto-select-next 'quietly))
2733       (set-buffer gnus-group-buffer)
2734       ;; The entry function called above goes to the next
2735       ;; group automatically, so we go two groups back
2736       ;; if we are searching for the previous group.
2737       (when backward
2738         (gnus-group-prev-unread-group 2))
2739       (if (not (equal group (gnus-group-group-name)))
2740           (setq group (gnus-group-group-name))
2741         (setq group nil)))
2742     result))
2743
2744 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2745   "Directly jump to the other GROUP from summary buffer.
2746 If SHOW-ALL is non-nil, already read articles are also listed."
2747   (interactive
2748    (if (eq gnus-summary-buffer (current-buffer))
2749        (list (completing-read
2750               "Group: " gnus-active-hashtb nil t
2751               (when (and gnus-newsgroup-name
2752                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2753                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2754               'gnus-group-history)
2755              current-prefix-arg)
2756      (error "%s must be invoked from a gnus summary buffer." this-command)))
2757   (unless (or (zerop (length group))
2758               (and gnus-newsgroup-name
2759                    (string-equal gnus-newsgroup-name group)))
2760     (gnus-summary-exit)
2761     (gnus-summary-read-group group show-all
2762                              gnus-dont-select-after-jump-to-other-group)))
2763
2764 (defun gnus-summary-read-group-1 (group show-all no-article
2765                                         kill-buffer no-display
2766                                         &optional select-articles)
2767   ;; Killed foreign groups can't be entered.
2768   (when (and (not (gnus-group-native-p group))
2769              (not (gnus-gethash group gnus-newsrc-hashtb)))
2770     (error "Dead non-native groups can't be entered"))
2771   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2772   (let* ((new-group (gnus-summary-setup-buffer group))
2773          (quit-config (gnus-group-quit-config group))
2774          (did-select (and new-group (gnus-select-newsgroup
2775                                      group show-all select-articles))))
2776     (cond
2777      ;; This summary buffer exists already, so we just select it.
2778      ((not new-group)
2779       (gnus-set-global-variables)
2780       (when kill-buffer
2781         (gnus-kill-or-deaden-summary kill-buffer))
2782       (gnus-configure-windows 'summary 'force)
2783       (gnus-set-mode-line 'summary)
2784       (gnus-summary-position-point)
2785       (message "")
2786       t)
2787      ;; We couldn't select this group.
2788      ((null did-select)
2789       (when (and (eq major-mode 'gnus-summary-mode)
2790                  (not (equal (current-buffer) kill-buffer)))
2791         (kill-buffer (current-buffer))
2792         (if (not quit-config)
2793             (progn
2794               ;; Update the info -- marks might need to be removed,
2795               ;; for instance.
2796               (gnus-summary-update-info)
2797               (set-buffer gnus-group-buffer)
2798               (gnus-group-jump-to-group group)
2799               (gnus-group-next-unread-group 1))
2800           (gnus-handle-ephemeral-exit quit-config)))
2801       (gnus-message 3 "Can't select group")
2802       nil)
2803      ;; The user did a `C-g' while prompting for number of articles,
2804      ;; so we exit this group.
2805      ((eq did-select 'quit)
2806       (and (eq major-mode 'gnus-summary-mode)
2807            (not (equal (current-buffer) kill-buffer))
2808            (kill-buffer (current-buffer)))
2809       (when kill-buffer
2810         (gnus-kill-or-deaden-summary kill-buffer))
2811       (if (not quit-config)
2812           (progn
2813             (set-buffer gnus-group-buffer)
2814             (gnus-group-jump-to-group group)
2815             (gnus-group-next-unread-group 1)
2816             (gnus-configure-windows 'group 'force))
2817         (gnus-handle-ephemeral-exit quit-config))
2818       ;; Finally signal the quit.
2819       (signal 'quit nil))
2820      ;; The group was successfully selected.
2821      (t
2822       (gnus-set-global-variables)
2823       ;; Save the active value in effect when the group was entered.
2824       (setq gnus-newsgroup-active
2825             (gnus-copy-sequence
2826              (gnus-active gnus-newsgroup-name)))
2827       ;; You can change the summary buffer in some way with this hook.
2828       (gnus-run-hooks 'gnus-select-group-hook)
2829       ;; Set any local variables in the group parameters.
2830       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2831       (gnus-update-format-specifications
2832        nil 'summary 'summary-mode 'summary-dummy)
2833       (gnus-update-summary-mark-positions)
2834       ;; Do score processing.
2835       (when gnus-use-scoring
2836         (gnus-possibly-score-headers))
2837       ;; Check whether to fill in the gaps in the threads.
2838       (when gnus-build-sparse-threads
2839         (gnus-build-sparse-threads))
2840       ;; Find the initial limit.
2841       (if gnus-show-threads
2842           (if show-all
2843               (let ((gnus-newsgroup-dormant nil))
2844                 (gnus-summary-initial-limit show-all))
2845             (gnus-summary-initial-limit show-all))
2846         (setq gnus-newsgroup-limit
2847               (mapcar
2848                (lambda (header) (mail-header-number header))
2849                gnus-newsgroup-headers)))
2850       ;; Generate the summary buffer.
2851       (unless no-display
2852         (gnus-summary-prepare))
2853       (when gnus-use-trees
2854         (gnus-tree-open group)
2855         (setq gnus-summary-highlight-line-function
2856               'gnus-tree-highlight-article))
2857       ;; If the summary buffer is empty, but there are some low-scored
2858       ;; articles or some excluded dormants, we include these in the
2859       ;; buffer.
2860       (when (and (zerop (buffer-size))
2861                  (not no-display))
2862         (cond (gnus-newsgroup-dormant
2863                (gnus-summary-limit-include-dormant))
2864               ((and gnus-newsgroup-scored show-all)
2865                (gnus-summary-limit-include-expunged t))))
2866       ;; Function `gnus-apply-kill-file' must be called in this hook.
2867       (gnus-run-hooks 'gnus-apply-kill-hook)
2868       (if (and (zerop (buffer-size))
2869                (not no-display))
2870           (progn
2871             ;; This newsgroup is empty.
2872             (gnus-summary-catchup-and-exit nil t)
2873             (gnus-message 6 "No unread news")
2874             (when kill-buffer
2875               (gnus-kill-or-deaden-summary kill-buffer))
2876             ;; Return nil from this function.
2877             nil)
2878         ;; Hide conversation thread subtrees.  We cannot do this in
2879         ;; gnus-summary-prepare-hook since kill processing may not
2880         ;; work with hidden articles.
2881         (and gnus-show-threads
2882              gnus-thread-hide-subtree
2883              (gnus-summary-hide-all-threads))
2884         (when kill-buffer
2885           (gnus-kill-or-deaden-summary kill-buffer))
2886         ;; Show first unread article if requested.
2887         (if (and (not no-article)
2888                  (not no-display)
2889                  gnus-newsgroup-unreads
2890                  gnus-auto-select-first)
2891             (progn
2892               (gnus-configure-windows 'summary)
2893               (cond
2894                ((eq gnus-auto-select-first 'best)
2895                 (gnus-summary-best-unread-article))
2896                ((eq gnus-auto-select-first t)
2897                 (gnus-summary-first-unread-article))
2898                ((gnus-functionp gnus-auto-select-first)
2899                 (funcall gnus-auto-select-first))))
2900           ;; Don't select any articles, just move point to the first
2901           ;; article in the group.
2902           (goto-char (point-min))
2903           (gnus-summary-position-point)
2904           (gnus-configure-windows 'summary 'force)
2905           (gnus-set-mode-line 'summary))
2906         (when (get-buffer-window gnus-group-buffer t)
2907           ;; Gotta use windows, because recenter does weird stuff if
2908           ;; the current buffer ain't the displayed window.
2909           (let ((owin (selected-window)))
2910             (select-window (get-buffer-window gnus-group-buffer t))
2911             (when (gnus-group-goto-group group)
2912               (recenter))
2913             (select-window owin)))
2914         ;; Mark this buffer as "prepared".
2915         (setq gnus-newsgroup-prepared t)
2916         (gnus-run-hooks 'gnus-summary-prepared-hook)
2917         t)))))
2918
2919 (defun gnus-summary-prepare ()
2920   "Generate the summary buffer."
2921   (interactive)
2922   (let ((buffer-read-only nil))
2923     (erase-buffer)
2924     (setq gnus-newsgroup-data nil
2925           gnus-newsgroup-data-reverse nil)
2926     (gnus-run-hooks 'gnus-summary-generate-hook)
2927     ;; Generate the buffer, either with threads or without.
2928     (when gnus-newsgroup-headers
2929       (gnus-summary-prepare-threads
2930        (if gnus-show-threads
2931            (gnus-sort-gathered-threads
2932             (funcall gnus-summary-thread-gathering-function
2933                      (gnus-sort-threads
2934                       (gnus-cut-threads (gnus-make-threads)))))
2935          ;; Unthreaded display.
2936          (gnus-sort-articles gnus-newsgroup-headers))))
2937     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2938     ;; Call hooks for modifying summary buffer.
2939     (goto-char (point-min))
2940     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2941
2942 (defsubst gnus-general-simplify-subject (subject)
2943   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2944   (setq subject
2945         (cond
2946          ;; Truncate the subject.
2947          (gnus-simplify-subject-functions
2948           (gnus-map-function gnus-simplify-subject-functions subject))
2949          ((numberp gnus-summary-gather-subject-limit)
2950           (setq subject (gnus-simplify-subject-re subject))
2951           (if (> (length subject) gnus-summary-gather-subject-limit)
2952               (substring subject 0 gnus-summary-gather-subject-limit)
2953             subject))
2954          ;; Fuzzily simplify it.
2955          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2956           (gnus-simplify-subject-fuzzy subject))
2957          ;; Just remove the leading "Re:".
2958          (t
2959           (gnus-simplify-subject-re subject))))
2960
2961   (if (and gnus-summary-gather-exclude-subject
2962            (string-match gnus-summary-gather-exclude-subject subject))
2963       nil                               ; This article shouldn't be gathered
2964     subject))
2965
2966 (defun gnus-summary-simplify-subject-query ()
2967   "Query where the respool algorithm would put this article."
2968   (interactive)
2969   (gnus-summary-select-article)
2970   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2971
2972 (defun gnus-gather-threads-by-subject (threads)
2973   "Gather threads by looking at Subject headers."
2974   (if (not gnus-summary-make-false-root)
2975       threads
2976     (let ((hashtb (gnus-make-hashtable 1024))
2977           (prev threads)
2978           (result threads)
2979           subject hthread whole-subject)
2980       (while threads
2981         (setq subject (gnus-general-simplify-subject
2982                        (setq whole-subject (mail-header-subject
2983                                             (caar threads)))))
2984         (when subject
2985           (if (setq hthread (gnus-gethash subject hashtb))
2986               (progn
2987                 ;; We enter a dummy root into the thread, if we
2988                 ;; haven't done that already.
2989                 (unless (stringp (caar hthread))
2990                   (setcar hthread (list whole-subject (car hthread))))
2991                 ;; We add this new gathered thread to this gathered
2992                 ;; thread.
2993                 (setcdr (car hthread)
2994                         (nconc (cdar hthread) (list (car threads))))
2995                 ;; Remove it from the list of threads.
2996                 (setcdr prev (cdr threads))
2997                 (setq threads prev))
2998             ;; Enter this thread into the hash table.
2999             (gnus-sethash subject threads hashtb)))
3000         (setq prev threads)
3001         (setq threads (cdr threads)))
3002       result)))
3003
3004 (defun gnus-gather-threads-by-references (threads)
3005   "Gather threads by looking at References headers."
3006   (let ((idhashtb (gnus-make-hashtable 1024))
3007         (thhashtb (gnus-make-hashtable 1024))
3008         (prev threads)
3009         (result threads)
3010         ids references id gthread gid entered ref)
3011     (while threads
3012       (when (setq references (mail-header-references (caar threads)))
3013         (setq id (mail-header-id (caar threads))
3014               ids (gnus-split-references references)
3015               entered nil)
3016         (while (setq ref (pop ids))
3017           (setq ids (delete ref ids))
3018           (if (not (setq gid (gnus-gethash ref idhashtb)))
3019               (progn
3020                 (gnus-sethash ref id idhashtb)
3021                 (gnus-sethash id threads thhashtb))
3022             (setq gthread (gnus-gethash gid thhashtb))
3023             (unless entered
3024               ;; We enter a dummy root into the thread, if we
3025               ;; haven't done that already.
3026               (unless (stringp (caar gthread))
3027                 (setcar gthread (list (mail-header-subject (caar gthread))
3028                                       (car gthread))))
3029               ;; We add this new gathered thread to this gathered
3030               ;; thread.
3031               (setcdr (car gthread)
3032                       (nconc (cdar gthread) (list (car threads)))))
3033             ;; Add it into the thread hash table.
3034             (gnus-sethash id gthread thhashtb)
3035             (setq entered t)
3036             ;; Remove it from the list of threads.
3037             (setcdr prev (cdr threads))
3038             (setq threads prev))))
3039       (setq prev threads)
3040       (setq threads (cdr threads)))
3041     result))
3042
3043 (defun gnus-sort-gathered-threads (threads)
3044   "Sort subtreads inside each gathered thread by article number."
3045   (let ((result threads))
3046     (while threads
3047       (when (stringp (caar threads))
3048         (setcdr (car threads)
3049                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3050       (setq threads (cdr threads)))
3051     result))
3052
3053 (defun gnus-thread-loop-p (root thread)
3054   "Say whether ROOT is in THREAD."
3055   (let ((stack (list thread))
3056         (infloop 0)
3057         th)
3058     (while (setq thread (pop stack))
3059       (setq th (cdr thread))
3060       (while (and th
3061                   (not (eq (caar th) root)))
3062         (pop th))
3063       (if th
3064           ;; We have found a loop.
3065           (let (ref-dep)
3066             (setcdr thread (delq (car th) (cdr thread)))
3067             (if (boundp (setq ref-dep (intern "none"
3068                                               gnus-newsgroup-dependencies)))
3069                 (setcdr (symbol-value ref-dep)
3070                         (nconc (cdr (symbol-value ref-dep))
3071                                (list (car th))))
3072               (set ref-dep (list nil (car th))))
3073             (setq infloop 1
3074                   stack nil))
3075         ;; Push all the subthreads onto the stack.
3076         (push (cdr thread) stack)))
3077     infloop))
3078
3079 (defun gnus-make-threads ()
3080   "Go through the dependency hashtb and find the roots.  Return all threads."
3081   (let (threads)
3082     (while (catch 'infloop
3083              (mapatoms
3084               (lambda (refs)
3085                 ;; Deal with self-referencing References loops.
3086                 (when (and (car (symbol-value refs))
3087                            (not (zerop
3088                                  (apply
3089                                   '+
3090                                   (mapcar
3091                                    (lambda (thread)
3092                                      (gnus-thread-loop-p
3093                                       (car (symbol-value refs)) thread))
3094                                    (cdr (symbol-value refs)))))))
3095                   (setq threads nil)
3096                   (throw 'infloop t))
3097                 (unless (car (symbol-value refs))
3098                   ;; These threads do not refer back to any other articles,
3099                   ;; so they're roots.
3100                   (setq threads (append (cdr (symbol-value refs)) threads))))
3101               gnus-newsgroup-dependencies)))
3102     threads))
3103
3104 ;; Build the thread tree.
3105 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3106   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3107
3108 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3109 if it was already present.
3110
3111 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3112 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3113 Message-IDs will be renamed be renamed to a unique Message-ID before
3114 being entered.
3115
3116 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3117   (let* ((id (mail-header-id header))
3118          (id-dep (and id (intern id dependencies)))
3119          ref ref-dep ref-header)
3120     ;; Enter this `header' in the `dependencies' table.
3121     (cond
3122      ((not id-dep)
3123       (setq header nil))
3124      ;; The first two cases do the normal part: enter a new `header'
3125      ;; in the `dependencies' table.
3126      ((not (boundp id-dep))
3127       (set id-dep (list header)))
3128      ((null (car (symbol-value id-dep)))
3129       (setcar (symbol-value id-dep) header))
3130
3131      ;; From here the `header' was already present in the
3132      ;; `dependencies' table.
3133      (force-new
3134       ;; Overrides an existing entry;
3135       ;; just set the header part of the entry.
3136       (setcar (symbol-value id-dep) header))
3137
3138      ;; Renames the existing `header' to a unique Message-ID.
3139      ((not gnus-summary-ignore-duplicates)
3140       ;; An article with this Message-ID has already been seen.
3141       ;; We rename the Message-ID.
3142       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3143            (list header))
3144       (mail-header-set-id header id))
3145
3146      ;; The last case ignores an existing entry, except it adds any
3147      ;; additional Xrefs (in case the two articles came from different
3148      ;; servers.
3149      ;; Also sets `header' to `nil' meaning that the `dependencies'
3150      ;; table was *not* modified.
3151      (t
3152       (mail-header-set-xref
3153        (car (symbol-value id-dep))
3154        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3155                    "")
3156                (or (mail-header-xref header) "")))
3157       (setq header nil)))
3158
3159     (when header
3160       ;; First check if that we are not creating a References loop.
3161       (setq ref (gnus-parent-id (mail-header-references header)))
3162       (while (and ref
3163                   (setq ref-dep (intern-soft ref dependencies))
3164                   (boundp ref-dep)
3165                   (setq ref-header (car (symbol-value ref-dep))))
3166         (if (string= id ref)
3167             ;; Yuk!  This is a reference loop.  Make the article be a
3168             ;; root article.
3169             (progn
3170               (mail-header-set-references (car (symbol-value id-dep)) "none")
3171               (setq ref nil))
3172           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3173       (setq ref (gnus-parent-id (mail-header-references header)))
3174       (setq ref-dep (intern (or ref "none") dependencies))
3175       (if (boundp ref-dep)
3176           (setcdr (symbol-value ref-dep)
3177                   (nconc (cdr (symbol-value ref-dep))
3178                          (list (symbol-value id-dep))))
3179         (set ref-dep (list nil (symbol-value id-dep)))))
3180     header))
3181
3182 (defun gnus-build-sparse-threads ()
3183   (let ((headers gnus-newsgroup-headers)
3184         (gnus-summary-ignore-duplicates t)
3185         header references generation relations
3186         subject child end new-child date)
3187     ;; First we create an alist of generations/relations, where
3188     ;; generations is how much we trust the relation, and the relation
3189     ;; is parent/child.
3190     (gnus-message 7 "Making sparse threads...")
3191     (save-excursion
3192       (nnheader-set-temp-buffer " *gnus sparse threads*")
3193       (while (setq header (pop headers))
3194         (when (and (setq references (mail-header-references header))
3195                    (not (string= references "")))
3196           (insert references)
3197           (setq child (mail-header-id header)
3198                 subject (mail-header-subject header)
3199                 date (mail-header-date header)
3200                 generation 0)
3201           (while (search-backward ">" nil t)
3202             (setq end (1+ (point)))
3203             (when (search-backward "<" nil t)
3204               (setq new-child (buffer-substring (point) end))
3205               (push (list (incf generation)
3206                           child (setq child new-child)
3207                           subject date)
3208                     relations)))
3209           (when child
3210             (push (list (1+ generation) child nil subject) relations))
3211           (erase-buffer)))
3212       (kill-buffer (current-buffer)))
3213     ;; Sort over trustworthiness.
3214     (mapcar
3215      (lambda (relation)
3216        (when (gnus-dependencies-add-header
3217               (make-full-mail-header
3218                gnus-reffed-article-number
3219                (nth 3 relation) "" (or (nth 4 relation) "")
3220                (nth 1 relation)
3221                (or (nth 2 relation) "") 0 0 "")
3222               gnus-newsgroup-dependencies nil)
3223          (push gnus-reffed-article-number gnus-newsgroup-limit)
3224          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3225          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3226                gnus-newsgroup-reads)
3227          (decf gnus-reffed-article-number)))
3228      (sort relations 'car-less-than-car))
3229     (gnus-message 7 "Making sparse threads...done")))
3230
3231 (defun gnus-build-old-threads ()
3232   ;; Look at all the articles that refer back to old articles, and
3233   ;; fetch the headers for the articles that aren't there.  This will
3234   ;; build complete threads - if the roots haven't been expired by the
3235   ;; server, that is.
3236   (let (id heads)
3237     (mapatoms
3238      (lambda (refs)
3239        (when (not (car (symbol-value refs)))
3240          (setq heads (cdr (symbol-value refs)))
3241          (while heads
3242            (if (memq (mail-header-number (caar heads))
3243                      gnus-newsgroup-dormant)
3244                (setq heads (cdr heads))
3245              (setq id (symbol-name refs))
3246              (while (and (setq id (gnus-build-get-header id))
3247                          (not (car (gnus-id-to-thread id)))))
3248              (setq heads nil)))))
3249      gnus-newsgroup-dependencies)))
3250
3251 ;; This function has to be called with point after the article number
3252 ;; on the beginning of the line.
3253 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3254   (let ((eol (gnus-point-at-eol))
3255         (buffer (current-buffer))
3256         header)
3257
3258     ;; overview: [num subject from date id refs chars lines misc]
3259     (unless (eobp)
3260       (forward-char))
3261
3262     (setq header
3263           (make-full-mail-header
3264            number                               ; number
3265            (nnheader-nov-field)                 ; subject
3266            (nnheader-nov-field)                 ; from
3267            (nnheader-nov-field)                 ; date
3268            (nnheader-nov-read-message-id)       ; id
3269            (nnheader-nov-field)                 ; refs
3270            (nnheader-nov-read-integer)          ; chars
3271            (nnheader-nov-read-integer)          ; lines
3272            (unless (eobp)
3273              (nnheader-nov-field))              ; misc
3274            (nnheader-nov-parse-extra)))         ; extra
3275
3276     (when gnus-alter-header-function
3277       (funcall gnus-alter-header-function header))
3278     (gnus-dependencies-add-header header dependencies force-new)))
3279
3280 (defun gnus-build-get-header (id)
3281   ;; Look through the buffer of NOV lines and find the header to
3282   ;; ID.  Enter this line into the dependencies hash table, and return
3283   ;; the id of the parent article (if any).
3284   (let ((deps gnus-newsgroup-dependencies)
3285         found header)
3286     (prog1
3287         (save-excursion
3288           (set-buffer nntp-server-buffer)
3289           (let ((case-fold-search nil))
3290             (goto-char (point-min))
3291             (while (and (not found)
3292                         (search-forward id nil t))
3293               (beginning-of-line)
3294               (setq found (looking-at
3295                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3296                                    (regexp-quote id))))
3297               (or found (beginning-of-line 2)))
3298             (when found
3299               (beginning-of-line)
3300               (and
3301                (setq header (gnus-nov-parse-line
3302                              (read (current-buffer)) deps))
3303                (gnus-parent-id (mail-header-references header))))))
3304       (when header
3305         (let ((number (mail-header-number header)))
3306           (push number gnus-newsgroup-limit)
3307           (push header gnus-newsgroup-headers)
3308           (if (memq number gnus-newsgroup-unselected)
3309               (progn
3310                 (push number gnus-newsgroup-unreads)
3311                 (setq gnus-newsgroup-unselected
3312                       (delq number gnus-newsgroup-unselected)))
3313             (push number gnus-newsgroup-ancient)))))))
3314
3315 (defun gnus-build-all-threads ()
3316   "Read all the headers."
3317   (let ((gnus-summary-ignore-duplicates t)
3318         (dependencies gnus-newsgroup-dependencies)
3319         header article)
3320     (save-excursion
3321       (set-buffer nntp-server-buffer)
3322       (let ((case-fold-search nil))
3323         (goto-char (point-min))
3324         (while (not (eobp))
3325           (ignore-errors
3326             (setq article (read (current-buffer))
3327                   header (gnus-nov-parse-line
3328                           article dependencies)))
3329           (when header
3330             (save-excursion
3331               (set-buffer gnus-summary-buffer)
3332               (push header gnus-newsgroup-headers)
3333               (if (memq (setq article (mail-header-number header))
3334                         gnus-newsgroup-unselected)
3335                   (progn
3336                     (push article gnus-newsgroup-unreads)
3337                     (setq gnus-newsgroup-unselected
3338                           (delq article gnus-newsgroup-unselected)))
3339                 (push article gnus-newsgroup-ancient)))
3340             (forward-line 1)))))))
3341
3342 (defun gnus-summary-update-article-line (article header)
3343   "Update the line for ARTICLE using HEADERS."
3344   (let* ((id (mail-header-id header))
3345          (thread (gnus-id-to-thread id)))
3346     (unless thread
3347       (error "Article in no thread"))
3348     ;; Update the thread.
3349     (setcar thread header)
3350     (gnus-summary-goto-subject article)
3351     (let* ((datal (gnus-data-find-list article))
3352            (data (car datal))
3353            (length (when (cdr datal)
3354                      (- (gnus-data-pos data)
3355                         (gnus-data-pos (cadr datal)))))
3356            (buffer-read-only nil)
3357            (level (gnus-summary-thread-level)))
3358       (gnus-delete-line)
3359       (gnus-summary-insert-line
3360        header level nil (gnus-article-mark article)
3361        (memq article gnus-newsgroup-replied)
3362        (memq article gnus-newsgroup-expirable)
3363        ;; Only insert the Subject string when it's different
3364        ;; from the previous Subject string.
3365        (if (gnus-subject-equal
3366             (condition-case ()
3367                 (mail-header-subject
3368                  (gnus-data-header
3369                   (cadr
3370                    (gnus-data-find-list
3371                     article
3372                     (gnus-data-list t)))))
3373               ;; Error on the side of excessive subjects.
3374               (error ""))
3375             (mail-header-subject header))
3376            ""
3377          (mail-header-subject header))
3378        nil (cdr (assq article gnus-newsgroup-scored))
3379        (memq article gnus-newsgroup-processable))
3380       (when length
3381         (gnus-data-update-list
3382          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3383
3384 (defun gnus-summary-update-article (article &optional iheader)
3385   "Update ARTICLE in the summary buffer."
3386   (set-buffer gnus-summary-buffer)
3387   (let* ((header (gnus-summary-article-header article))
3388          (id (mail-header-id header))
3389          (data (gnus-data-find article))
3390          (thread (gnus-id-to-thread id))
3391          (references (mail-header-references header))
3392          (parent
3393           (gnus-id-to-thread
3394            (or (gnus-parent-id
3395                 (when (and references
3396                            (not (equal "" references)))
3397                   references))
3398                "none")))
3399          (buffer-read-only nil)
3400          (old (car thread)))
3401     (when thread
3402       (unless iheader
3403         (setcar thread nil)
3404         (when parent
3405           (delq thread parent)))
3406       (if (gnus-summary-insert-subject id header)
3407           ;; Set the (possibly) new article number in the data structure.
3408           (gnus-data-set-number data (gnus-id-to-article id))
3409         (setcar thread old)
3410         nil))))
3411
3412 (defun gnus-rebuild-thread (id &optional line)
3413   "Rebuild the thread containing ID.
3414 If LINE, insert the rebuilt thread starting on line LINE."
3415   (let ((buffer-read-only nil)
3416         old-pos current thread data)
3417     (if (not gnus-show-threads)
3418         (setq thread (list (car (gnus-id-to-thread id))))
3419       ;; Get the thread this article is part of.
3420       (setq thread (gnus-remove-thread id)))
3421     (setq old-pos (gnus-point-at-bol))
3422     (setq current (save-excursion
3423                     (and (zerop (forward-line -1))
3424                          (gnus-summary-article-number))))
3425     ;; If this is a gathered thread, we have to go some re-gathering.
3426     (when (stringp (car thread))
3427       (let ((subject (car thread))
3428             roots thr)
3429         (setq thread (cdr thread))
3430         (while thread
3431           (unless (memq (setq thr (gnus-id-to-thread
3432                                    (gnus-root-id
3433                                     (mail-header-id (caar thread)))))
3434                         roots)
3435             (push thr roots))
3436           (setq thread (cdr thread)))
3437         ;; We now have all (unique) roots.
3438         (if (= (length roots) 1)
3439             ;; All the loose roots are now one solid root.
3440             (setq thread (car roots))
3441           (setq thread (cons subject (gnus-sort-threads roots))))))
3442     (let (threads)
3443       ;; We then insert this thread into the summary buffer.
3444       (when line
3445         (goto-char (point-min))
3446         (forward-line (1- line)))
3447       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3448         (if gnus-show-threads
3449             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3450           (gnus-summary-prepare-unthreaded thread))
3451         (setq data (nreverse gnus-newsgroup-data))
3452         (setq threads gnus-newsgroup-threads))
3453       ;; We splice the new data into the data structure.
3454       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3455       ;;!!! then we want to insert at the beginning of the buffer.
3456       ;;!!! That happens to be true with Gnus now, but that may
3457       ;;!!! change in the future.  Perhaps.
3458       (gnus-data-enter-list
3459        (if line nil current) data (- (point) old-pos))
3460       (setq gnus-newsgroup-threads
3461             (nconc threads gnus-newsgroup-threads))
3462       (gnus-data-compute-positions))))
3463
3464 (defun gnus-number-to-header (number)
3465   "Return the header for article NUMBER."
3466   (let ((headers gnus-newsgroup-headers))
3467     (while (and headers
3468                 (not (= number (mail-header-number (car headers)))))
3469       (pop headers))
3470     (when headers
3471       (car headers))))
3472
3473 (defun gnus-parent-headers (in-headers &optional generation)
3474   "Return the headers of the GENERATIONeth parent of HEADERS."
3475   (unless generation
3476     (setq generation 1))
3477   (let ((parent t)
3478         (headers in-headers)
3479         references)
3480     (while (and parent
3481                 (not (zerop generation))
3482                 (setq references (mail-header-references headers)))
3483       (setq headers (if (and references
3484                              (setq parent (gnus-parent-id references)))
3485                         (car (gnus-id-to-thread parent))
3486                       nil))
3487       (decf generation))
3488     (and (not (eq headers in-headers))
3489          headers)))
3490
3491 (defun gnus-id-to-thread (id)
3492   "Return the (sub-)thread where ID appears."
3493   (gnus-gethash id gnus-newsgroup-dependencies))
3494
3495 (defun gnus-id-to-article (id)
3496   "Return the article number of ID."
3497   (let ((thread (gnus-id-to-thread id)))
3498     (when (and thread
3499                (car thread))
3500       (mail-header-number (car thread)))))
3501
3502 (defun gnus-id-to-header (id)
3503   "Return the article headers of ID."
3504   (car (gnus-id-to-thread id)))
3505
3506 (defun gnus-article-displayed-root-p (article)
3507   "Say whether ARTICLE is a root(ish) article."
3508   (let ((level (gnus-summary-thread-level article))
3509         (refs (mail-header-references  (gnus-summary-article-header article)))
3510         particle)
3511     (cond
3512      ((null level) nil)
3513      ((zerop level) t)
3514      ((null refs) t)
3515      ((null (gnus-parent-id refs)) t)
3516      ((and (= 1 level)
3517            (null (setq particle (gnus-id-to-article
3518                                  (gnus-parent-id refs))))
3519            (null (gnus-summary-thread-level particle)))))))
3520
3521 (defun gnus-root-id (id)
3522   "Return the id of the root of the thread where ID appears."
3523   (let (last-id prev)
3524     (while (and id (setq prev (car (gnus-id-to-thread id))))
3525       (setq last-id id
3526             id (gnus-parent-id (mail-header-references prev))))
3527     last-id))
3528
3529 (defun gnus-articles-in-thread (thread)
3530   "Return the list of articles in THREAD."
3531   (cons (mail-header-number (car thread))
3532         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3533
3534 (defun gnus-remove-thread (id &optional dont-remove)
3535   "Remove the thread that has ID in it."
3536   (let (headers thread last-id)
3537     ;; First go up in this thread until we find the root.
3538     (setq last-id (gnus-root-id id)
3539           headers (message-flatten-list (gnus-id-to-thread last-id)))
3540     ;; We have now found the real root of this thread.  It might have
3541     ;; been gathered into some loose thread, so we have to search
3542     ;; through the threads to find the thread we wanted.
3543     (let ((threads gnus-newsgroup-threads)
3544           sub)
3545       (while threads
3546         (setq sub (car threads))
3547         (if (stringp (car sub))
3548             ;; This is a gathered thread, so we look at the roots
3549             ;; below it to find whether this article is in this
3550             ;; gathered root.
3551             (progn
3552               (setq sub (cdr sub))
3553               (while sub
3554                 (when (member (caar sub) headers)
3555                   (setq thread (car threads)
3556                         threads nil
3557                         sub nil))
3558                 (setq sub (cdr sub))))
3559           ;; It's an ordinary thread, so we check it.
3560           (when (eq (car sub) (car headers))
3561             (setq thread sub
3562                   threads nil)))
3563         (setq threads (cdr threads)))
3564       ;; If this article is in no thread, then it's a root.
3565       (if thread
3566           (unless dont-remove
3567             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3568         (setq thread (gnus-id-to-thread last-id)))
3569       (when thread
3570         (prog1
3571             thread                      ; We return this thread.
3572           (unless dont-remove
3573             (if (stringp (car thread))
3574                 (progn
3575                   ;; If we use dummy roots, then we have to remove the
3576                   ;; dummy root as well.
3577                   (when (eq gnus-summary-make-false-root 'dummy)
3578                     ;; We go to the dummy root by going to
3579                     ;; the first sub-"thread", and then one line up.
3580                     (gnus-summary-goto-article
3581                      (mail-header-number (caadr thread)))
3582                     (forward-line -1)
3583                     (gnus-delete-line)
3584                     (gnus-data-compute-positions))
3585                   (setq thread (cdr thread))
3586                   (while thread
3587                     (gnus-remove-thread-1 (car thread))
3588                     (setq thread (cdr thread))))
3589               (gnus-summary-show-all-threads)
3590               (gnus-remove-thread-1 thread))))))))
3591
3592 (defun gnus-remove-thread-1 (thread)
3593   "Remove the thread THREAD recursively."
3594   (let ((number (mail-header-number (pop thread)))
3595         d)
3596     (setq thread (reverse thread))
3597     (while thread
3598       (gnus-remove-thread-1 (pop thread)))
3599     (when (setq d (gnus-data-find number))
3600       (goto-char (gnus-data-pos d))
3601       (gnus-data-remove
3602        number
3603        (- (gnus-point-at-bol)
3604           (prog1
3605               (1+ (gnus-point-at-eol))
3606             (gnus-delete-line)))))))
3607
3608 (defun gnus-sort-threads (threads)
3609   "Sort THREADS."
3610   (if (not gnus-thread-sort-functions)
3611       threads
3612     (gnus-message 8 "Sorting threads...")
3613     (prog1
3614         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3615       (gnus-message 8 "Sorting threads...done"))))
3616
3617 (defun gnus-sort-articles (articles)
3618   "Sort ARTICLES."
3619   (when gnus-article-sort-functions
3620     (gnus-message 7 "Sorting articles...")
3621     (prog1
3622         (setq gnus-newsgroup-headers
3623               (sort articles (gnus-make-sort-function
3624                               gnus-article-sort-functions)))
3625       (gnus-message 7 "Sorting articles...done"))))
3626
3627 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3628 (defmacro gnus-thread-header (thread)
3629   ;; Return header of first article in THREAD.
3630   ;; Note that THREAD must never, ever be anything else than a variable -
3631   ;; using some other form will lead to serious barfage.
3632   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3633   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3634   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3635         (vector thread) 2))
3636
3637 (defsubst gnus-article-sort-by-number (h1 h2)
3638   "Sort articles by article number."
3639   (< (mail-header-number h1)
3640      (mail-header-number h2)))
3641
3642 (defun gnus-thread-sort-by-number (h1 h2)
3643   "Sort threads by root article number."
3644   (gnus-article-sort-by-number
3645    (gnus-thread-header h1) (gnus-thread-header h2)))
3646
3647 (defsubst gnus-article-sort-by-lines (h1 h2)
3648   "Sort articles by article Lines header."
3649   (< (mail-header-lines h1)
3650      (mail-header-lines h2)))
3651
3652 (defun gnus-thread-sort-by-lines (h1 h2)
3653   "Sort threads by root article Lines header."
3654   (gnus-article-sort-by-lines
3655    (gnus-thread-header h1) (gnus-thread-header h2)))
3656
3657 (defsubst gnus-article-sort-by-chars (h1 h2)
3658   "Sort articles by octet length."
3659   (< (mail-header-chars h1)
3660      (mail-header-chars h2)))
3661
3662 (defun gnus-thread-sort-by-chars (h1 h2)
3663   "Sort threads by root article octet length."
3664   (gnus-article-sort-by-chars
3665    (gnus-thread-header h1) (gnus-thread-header h2)))
3666
3667 (defsubst gnus-article-sort-by-author (h1 h2)
3668   "Sort articles by root author."
3669   (string-lessp
3670    (let ((addr (car (mime-read-field 'From h1))))
3671      (or (std11-full-name-string addr)
3672          (std11-address-string addr)
3673          ""))
3674    (let ((addr (car (mime-read-field 'From h2))))
3675      (or (std11-full-name-string addr)
3676          (std11-address-string addr)
3677          ""))
3678    ))
3679
3680 (defun gnus-thread-sort-by-author (h1 h2)
3681   "Sort threads by root author."
3682   (gnus-article-sort-by-author
3683    (gnus-thread-header h1)  (gnus-thread-header h2)))
3684
3685 (defsubst gnus-article-sort-by-subject (h1 h2)
3686   "Sort articles by root subject."
3687   (string-lessp
3688    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3689    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3690
3691 (defun gnus-thread-sort-by-subject (h1 h2)
3692   "Sort threads by root subject."
3693   (gnus-article-sort-by-subject
3694    (gnus-thread-header h1) (gnus-thread-header h2)))
3695
3696 (defsubst gnus-article-sort-by-date (h1 h2)
3697   "Sort articles by root article date."
3698   (time-less-p
3699    (gnus-date-get-time (mail-header-date h1))
3700    (gnus-date-get-time (mail-header-date h2))))
3701
3702 (defun gnus-thread-sort-by-date (h1 h2)
3703   "Sort threads by root article date."
3704   (gnus-article-sort-by-date
3705    (gnus-thread-header h1) (gnus-thread-header h2)))
3706
3707 (defsubst gnus-article-sort-by-score (h1 h2)
3708   "Sort articles by root article score.
3709 Unscored articles will be counted as having a score of zero."
3710   (> (or (cdr (assq (mail-header-number h1)
3711                     gnus-newsgroup-scored))
3712          gnus-summary-default-score 0)
3713      (or (cdr (assq (mail-header-number h2)
3714                     gnus-newsgroup-scored))
3715          gnus-summary-default-score 0)))
3716
3717 (defun gnus-thread-sort-by-score (h1 h2)
3718   "Sort threads by root article score."
3719   (gnus-article-sort-by-score
3720    (gnus-thread-header h1) (gnus-thread-header h2)))
3721
3722 (defun gnus-thread-sort-by-total-score (h1 h2)
3723   "Sort threads by the sum of all scores in the thread.
3724 Unscored articles will be counted as having a score of zero."
3725   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3726
3727 (defun gnus-thread-total-score (thread)
3728   ;; This function find the total score of THREAD.
3729   (cond ((null thread)
3730          0)
3731         ((consp thread)
3732          (if (stringp (car thread))
3733              (apply gnus-thread-score-function 0
3734                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3735            (gnus-thread-total-score-1 thread)))
3736         (t
3737          (gnus-thread-total-score-1 (list thread)))))
3738
3739 (defun gnus-thread-total-score-1 (root)
3740   ;; This function find the total score of the thread below ROOT.
3741   (setq root (car root))
3742   (apply gnus-thread-score-function
3743          (or (append
3744               (mapcar 'gnus-thread-total-score
3745                       (cdr (gnus-id-to-thread (mail-header-id root))))
3746               (when (> (mail-header-number root) 0)
3747                 (list (or (cdr (assq (mail-header-number root)
3748                                      gnus-newsgroup-scored))
3749                           gnus-summary-default-score 0))))
3750              (list gnus-summary-default-score)
3751              '(0))))
3752
3753 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3754 (defvar gnus-tmp-prev-subject nil)
3755 (defvar gnus-tmp-false-parent nil)
3756 (defvar gnus-tmp-root-expunged nil)
3757 (defvar gnus-tmp-dummy-line nil)
3758
3759 (defvar gnus-tmp-header)
3760 (defun gnus-extra-header (type &optional header)
3761   "Return the extra header of TYPE."
3762   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3763       ""))
3764
3765 (defun gnus-summary-prepare-threads (threads)
3766   "Prepare summary buffer from THREADS and indentation LEVEL.
3767 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3768 or a straight list of headers."
3769   (gnus-message 7 "Generating summary...")
3770
3771   (setq gnus-newsgroup-threads threads)
3772   (beginning-of-line)
3773
3774   (let ((gnus-tmp-level 0)
3775         (default-score (or gnus-summary-default-score 0))
3776         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3777         thread number subject stack state gnus-tmp-gathered beg-match
3778         new-roots gnus-tmp-new-adopts thread-end
3779         gnus-tmp-header gnus-tmp-unread
3780         gnus-tmp-replied gnus-tmp-subject-or-nil
3781         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3782         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3783         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3784
3785     (setq gnus-tmp-prev-subject nil)
3786
3787     (if (vectorp (car threads))
3788         ;; If this is a straight (sic) list of headers, then a
3789         ;; threaded summary display isn't required, so we just create
3790         ;; an unthreaded one.
3791         (gnus-summary-prepare-unthreaded threads)
3792
3793       ;; Do the threaded display.
3794
3795       (while (or threads stack gnus-tmp-new-adopts new-roots)
3796
3797         (if (and (= gnus-tmp-level 0)
3798                  (or (not stack)
3799                      (= (caar stack) 0))
3800                  (not gnus-tmp-false-parent)
3801                  (or gnus-tmp-new-adopts new-roots))
3802             (if gnus-tmp-new-adopts
3803                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3804                       thread (list (car gnus-tmp-new-adopts))
3805                       gnus-tmp-header (caar thread)
3806                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3807               (when new-roots
3808                 (setq thread (list (car new-roots))
3809                       gnus-tmp-header (caar thread)
3810                       new-roots (cdr new-roots))))
3811
3812           (if threads
3813               ;; If there are some threads, we do them before the
3814               ;; threads on the stack.
3815               (setq thread threads
3816                     gnus-tmp-header (caar thread))
3817             ;; There were no current threads, so we pop something off
3818             ;; the stack.
3819             (setq state (car stack)
3820                   gnus-tmp-level (car state)
3821                   thread (cdr state)
3822                   stack (cdr stack)
3823                   gnus-tmp-header (caar thread))))
3824
3825         (setq gnus-tmp-false-parent nil)
3826         (setq gnus-tmp-root-expunged nil)
3827         (setq thread-end nil)
3828
3829         (if (stringp gnus-tmp-header)
3830             ;; The header is a dummy root.
3831             (cond
3832              ((eq gnus-summary-make-false-root 'adopt)
3833               ;; We let the first article adopt the rest.
3834               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3835                                                (cddar thread)))
3836               (setq gnus-tmp-gathered
3837                     (nconc (mapcar
3838                             (lambda (h) (mail-header-number (car h)))
3839                             (cddar thread))
3840                            gnus-tmp-gathered))
3841               (setq thread (cons (list (caar thread)
3842                                        (cadar thread))
3843                                  (cdr thread)))
3844               (setq gnus-tmp-level -1
3845                     gnus-tmp-false-parent t))
3846              ((eq gnus-summary-make-false-root 'empty)
3847               ;; We print adopted articles with empty subject fields.
3848               (setq gnus-tmp-gathered
3849                     (nconc (mapcar
3850                             (lambda (h) (mail-header-number (car h)))
3851                             (cddar thread))
3852                            gnus-tmp-gathered))
3853               (setq gnus-tmp-level -1))
3854              ((eq gnus-summary-make-false-root 'dummy)
3855               ;; We remember that we probably want to output a dummy
3856               ;; root.
3857               (setq gnus-tmp-dummy-line gnus-tmp-header)
3858               (setq gnus-tmp-prev-subject gnus-tmp-header))
3859              (t
3860               ;; We do not make a root for the gathered
3861               ;; sub-threads at all.
3862               (setq gnus-tmp-level -1)))
3863
3864           (setq number (mail-header-number gnus-tmp-header)
3865                 subject (mail-header-subject gnus-tmp-header))
3866
3867           (cond
3868            ;; If the thread has changed subject, we might want to make
3869            ;; this subthread into a root.
3870            ((and (null gnus-thread-ignore-subject)
3871                  (not (zerop gnus-tmp-level))
3872                  gnus-tmp-prev-subject
3873                  (not (inline
3874                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3875             (setq new-roots (nconc new-roots (list (car thread)))
3876                   thread-end t
3877                   gnus-tmp-header nil))
3878            ;; If the article lies outside the current limit,
3879            ;; then we do not display it.
3880            ((not (memq number gnus-newsgroup-limit))
3881             (setq gnus-tmp-gathered
3882                   (nconc (mapcar
3883                           (lambda (h) (mail-header-number (car h)))
3884                           (cdar thread))
3885                          gnus-tmp-gathered))
3886             (setq gnus-tmp-new-adopts (if (cdar thread)
3887                                           (append gnus-tmp-new-adopts
3888                                                   (cdar thread))
3889                                         gnus-tmp-new-adopts)
3890                   thread-end t
3891                   gnus-tmp-header nil)
3892             (when (zerop gnus-tmp-level)
3893               (setq gnus-tmp-root-expunged t)))
3894            ;; Perhaps this article is to be marked as read?
3895            ((and gnus-summary-mark-below
3896                  (< (or (cdr (assq number gnus-newsgroup-scored))
3897                         default-score)
3898                     gnus-summary-mark-below)
3899                  ;; Don't touch sparse articles.
3900                  (not (gnus-summary-article-sparse-p number))
3901                  (not (gnus-summary-article-ancient-p number)))
3902             (setq gnus-newsgroup-unreads
3903                   (delq number gnus-newsgroup-unreads))
3904             (if gnus-newsgroup-auto-expire
3905                 (push number gnus-newsgroup-expirable)
3906               (push (cons number gnus-low-score-mark)
3907                     gnus-newsgroup-reads))))
3908
3909           (when gnus-tmp-header
3910             ;; We may have an old dummy line to output before this
3911             ;; article.
3912             (when (and gnus-tmp-dummy-line
3913                        (gnus-subject-equal
3914                         gnus-tmp-dummy-line
3915                         (mail-header-subject gnus-tmp-header)))
3916               (gnus-summary-insert-dummy-line
3917                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3918               (setq gnus-tmp-dummy-line nil))
3919
3920             ;; Compute the mark.
3921             (setq gnus-tmp-unread (gnus-article-mark number))
3922
3923             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3924                                   gnus-tmp-header gnus-tmp-level)
3925                   gnus-newsgroup-data)
3926
3927             ;; Actually insert the line.
3928             (setq
3929              gnus-tmp-subject-or-nil
3930              (cond
3931               ((and gnus-thread-ignore-subject
3932                     gnus-tmp-prev-subject
3933                     (not (inline (gnus-subject-equal
3934                                   gnus-tmp-prev-subject subject))))
3935                subject)
3936               ((zerop gnus-tmp-level)
3937                (if (and (eq gnus-summary-make-false-root 'empty)
3938                         (memq number gnus-tmp-gathered)
3939                         gnus-tmp-prev-subject
3940                         (inline (gnus-subject-equal
3941                                  gnus-tmp-prev-subject subject)))
3942                    gnus-summary-same-subject
3943                  subject))
3944               (t gnus-summary-same-subject)))
3945             (if (and (eq gnus-summary-make-false-root 'adopt)
3946                      (= gnus-tmp-level 1)
3947                      (memq number gnus-tmp-gathered))
3948                 (setq gnus-tmp-opening-bracket ?\<
3949                       gnus-tmp-closing-bracket ?\>)
3950               (setq gnus-tmp-opening-bracket ?\[
3951                     gnus-tmp-closing-bracket ?\]))
3952             (setq
3953              gnus-tmp-indentation
3954              (aref gnus-thread-indent-array gnus-tmp-level)
3955              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3956              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3957                                 gnus-summary-default-score 0)
3958              gnus-tmp-score-char
3959              (if (or (null gnus-summary-default-score)
3960                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3961                          gnus-summary-zcore-fuzz))
3962                  ? ;Whitespace
3963                (if (< gnus-tmp-score gnus-summary-default-score)
3964                    gnus-score-below-mark gnus-score-over-mark))
3965              gnus-tmp-replied
3966              (cond ((memq number gnus-newsgroup-processable)
3967                     gnus-process-mark)
3968                    ((memq number gnus-newsgroup-cached)
3969                     gnus-cached-mark)
3970                    ((memq number gnus-newsgroup-replied)
3971                     gnus-replied-mark)
3972                    ((memq number gnus-newsgroup-saved)
3973                     gnus-saved-mark)
3974                    (t gnus-unread-mark))
3975              gnus-tmp-from (mail-header-from gnus-tmp-header)
3976              gnus-tmp-name
3977              (cond
3978               ((string-match "<[^>]+> *$" gnus-tmp-from)
3979                (setq beg-match (match-beginning 0))
3980                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3981                         (substring gnus-tmp-from (1+ (match-beginning 0))
3982                                    (1- (match-end 0))))
3983                    (substring gnus-tmp-from 0 beg-match)))
3984               ((string-match "(.+)" gnus-tmp-from)
3985                (substring gnus-tmp-from
3986                           (1+ (match-beginning 0)) (1- (match-end 0))))
3987               (t gnus-tmp-from)))
3988             (when (string= gnus-tmp-name "")
3989               (setq gnus-tmp-name gnus-tmp-from))
3990             (unless (numberp gnus-tmp-lines)
3991               (setq gnus-tmp-lines 0))
3992             (gnus-put-text-property
3993              (point)
3994              (progn (eval gnus-summary-line-format-spec) (point))
3995              'gnus-number number)
3996             (when gnus-visual-p
3997               (forward-line -1)
3998               (gnus-run-hooks 'gnus-summary-update-hook)
3999               (forward-line 1))
4000
4001             (setq gnus-tmp-prev-subject subject)))
4002
4003         (when (nth 1 thread)
4004           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4005         (incf gnus-tmp-level)
4006         (setq threads (if thread-end nil (cdar thread)))
4007         (unless threads
4008           (setq gnus-tmp-level 0)))))
4009   (gnus-message 7 "Generating summary...done"))
4010
4011 (defun gnus-summary-prepare-unthreaded (headers)
4012   "Generate an unthreaded summary buffer based on HEADERS."
4013   (let (header number mark)
4014
4015     (beginning-of-line)
4016
4017     (while headers
4018       ;; We may have to root out some bad articles...
4019       (when (memq (setq number (mail-header-number
4020                                 (setq header (pop headers))))
4021                   gnus-newsgroup-limit)
4022         ;; Mark article as read when it has a low score.
4023         (when (and gnus-summary-mark-below
4024                    (< (or (cdr (assq number gnus-newsgroup-scored))
4025                           gnus-summary-default-score 0)
4026                       gnus-summary-mark-below)
4027                    (not (gnus-summary-article-ancient-p number)))
4028           (setq gnus-newsgroup-unreads
4029                 (delq number gnus-newsgroup-unreads))
4030           (if gnus-newsgroup-auto-expire
4031               (push number gnus-newsgroup-expirable)
4032             (push (cons number gnus-low-score-mark)
4033                   gnus-newsgroup-reads)))
4034
4035         (setq mark (gnus-article-mark number))
4036         (push (gnus-data-make number mark (1+ (point)) header 0)
4037               gnus-newsgroup-data)
4038         (gnus-summary-insert-line
4039          header 0 number
4040          mark (memq number gnus-newsgroup-replied)
4041          (memq number gnus-newsgroup-expirable)
4042          (mail-header-subject header) nil
4043          (cdr (assq number gnus-newsgroup-scored))
4044          (memq number gnus-newsgroup-processable))))))
4045
4046 (defun gnus-summary-remove-list-identifiers ()
4047   "Remove list identifiers in `gnus-list-identifiers' from articles in
4048 the current group."
4049   (let ((regexp (if (stringp gnus-list-identifiers)
4050                     gnus-list-identifiers
4051                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4052     (when regexp
4053       (dolist (header gnus-newsgroup-headers)
4054         (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4055                             (mail-header-subject header))
4056           (mail-header-set-subject
4057            header (concat (substring (mail-header-subject header)
4058                                      0 (match-beginning 2))
4059                           (substring (mail-header-subject header)
4060                                      (match-end 2)))))))))
4061
4062 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4063   "Select newsgroup GROUP.
4064 If READ-ALL is non-nil, all articles in the group are selected.
4065 If SELECT-ARTICLES, only select those articles from GROUP."
4066   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4067          ;;!!! Dirty hack; should be removed.
4068          (gnus-summary-ignore-duplicates
4069           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4070               t
4071             gnus-summary-ignore-duplicates))
4072          (info (nth 2 entry))
4073          articles fetched-articles cached)
4074
4075     (unless (gnus-check-server
4076              (setq gnus-current-select-method
4077                    (gnus-find-method-for-group group)))
4078       (error "Couldn't open server"))
4079
4080     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4081         (gnus-activate-group group)     ; Or we can activate it...
4082         (progn                          ; Or we bug out.
4083           (when (equal major-mode 'gnus-summary-mode)
4084             (kill-buffer (current-buffer)))
4085           (error "Couldn't request group %s: %s"
4086                  group (gnus-status-message group))))
4087
4088     (unless (gnus-request-group group t)
4089       (when (equal major-mode 'gnus-summary-mode)
4090         (kill-buffer (current-buffer)))
4091       (error "Couldn't request group %s: %s"
4092              group (gnus-status-message group)))
4093
4094     (setq gnus-newsgroup-name group)
4095     (setq gnus-newsgroup-unselected nil)
4096     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4097     (gnus-summary-setup-default-charset)
4098
4099     ;; Adjust and set lists of article marks.
4100     (when info
4101       (gnus-adjust-marked-articles info))
4102
4103     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4104     (setq cached
4105           (if (gnus-virtual-group-p group)
4106               gnus-newsgroup-cached
4107             (gnus-cache-articles-in-group group)))
4108
4109     (setq gnus-newsgroup-unreads
4110           (gnus-set-difference
4111            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4112            gnus-newsgroup-dormant))
4113
4114     (setq gnus-newsgroup-processable nil)
4115
4116     (gnus-update-read-articles group gnus-newsgroup-unreads)
4117
4118     (if (setq articles select-articles)
4119         (setq gnus-newsgroup-unselected
4120               (gnus-sorted-intersection
4121                gnus-newsgroup-unreads
4122                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4123       (setq articles (gnus-articles-to-read group read-all)))
4124
4125     (cond
4126      ((null articles)
4127       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4128       'quit)
4129      ((eq articles 0) nil)
4130      (t
4131       ;; Init the dependencies hash table.
4132       (setq gnus-newsgroup-dependencies
4133             (gnus-make-hashtable (length articles)))
4134       (gnus-set-global-variables)
4135       ;; Retrieve the headers and read them in.
4136       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4137       (setq gnus-newsgroup-headers
4138             (gnus-retrieve-parsed-headers
4139              articles gnus-newsgroup-name
4140              ;; We might want to fetch old headers, but
4141              ;; not if there is only 1 article.
4142              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4143                            (not (numberp gnus-fetch-old-headers)))
4144                       (> (length articles) 1))
4145                   gnus-fetch-old-headers)))
4146       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4147
4148       ;; Suppress duplicates?
4149       (when gnus-suppress-duplicates
4150         (gnus-dup-suppress-articles))
4151
4152       ;; Set the initial limit.
4153       (setq gnus-newsgroup-limit (copy-sequence articles))
4154       ;; Remove canceled articles from the list of unread articles.
4155       (setq gnus-newsgroup-unreads
4156             (gnus-set-sorted-intersection
4157              gnus-newsgroup-unreads
4158              (setq fetched-articles
4159                    (mapcar (lambda (headers) (mail-header-number headers))
4160                            gnus-newsgroup-headers))))
4161       ;; Removed marked articles that do not exist.
4162       (gnus-update-missing-marks
4163        (gnus-sorted-complement fetched-articles articles))
4164
4165       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4166       (when cached
4167         (setq gnus-newsgroup-cached cached))
4168
4169       ;; We might want to build some more threads first.
4170       (when (and gnus-fetch-old-headers
4171                  (eq gnus-headers-retrieved-by 'nov))
4172         (if (eq gnus-fetch-old-headers 'invisible)
4173             (gnus-build-all-threads)
4174           (gnus-build-old-threads)))
4175       ;; Let the Gnus agent mark articles as read.
4176       (when gnus-agent
4177         (gnus-agent-get-undownloaded-list))
4178       ;; Remove list identifiers from subject
4179       (when gnus-list-identifiers
4180         (gnus-summary-remove-list-identifiers))
4181       ;; Check whether auto-expire is to be done in this group.
4182       (setq gnus-newsgroup-auto-expire
4183             (gnus-group-auto-expirable-p group))
4184       ;; Set up the article buffer now, if necessary.
4185       (unless gnus-single-article-buffer
4186         (gnus-article-setup-buffer))
4187       ;; First and last article in this newsgroup.
4188       (when gnus-newsgroup-headers
4189         (setq gnus-newsgroup-begin
4190               (mail-header-number (car gnus-newsgroup-headers))
4191               gnus-newsgroup-end
4192               (mail-header-number
4193                (gnus-last-element gnus-newsgroup-headers))))
4194       ;; GROUP is successfully selected.
4195       (or gnus-newsgroup-headers t)))))
4196
4197 (defun gnus-articles-to-read (group &optional read-all)
4198   ;; Find out what articles the user wants to read.
4199   (let* ((articles
4200           ;; Select all articles if `read-all' is non-nil, or if there
4201           ;; are no unread articles.
4202           (if (or read-all
4203                   (and (zerop (length gnus-newsgroup-marked))
4204                        (zerop (length gnus-newsgroup-unreads)))
4205                   (eq (gnus-group-find-parameter group 'display)
4206                       'all))
4207               (or
4208                (gnus-uncompress-range (gnus-active group))
4209                (gnus-cache-articles-in-group group))
4210             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4211                           (copy-sequence gnus-newsgroup-unreads))
4212                   '<)))
4213          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4214          (scored (length scored-list))
4215          (number (length articles))
4216          (marked (+ (length gnus-newsgroup-marked)
4217                     (length gnus-newsgroup-dormant)))
4218          (select
4219           (cond
4220            ((numberp read-all)
4221             read-all)
4222            (t
4223             (condition-case ()
4224                 (cond
4225                  ((and (or (<= scored marked) (= scored number))
4226                        (natnump gnus-large-newsgroup)
4227                        (> number gnus-large-newsgroup))
4228                   (let ((input (read-from-minibuffer
4229                                 (format
4230                                  "How many articles from %s (max %d): "
4231                                  (gnus-limit-string gnus-newsgroup-name 35)
4232                                  number)
4233                                 (static-if (< emacs-major-version 20)
4234                                     (number-to-string gnus-large-newsgroup)
4235                                   (cons
4236                                    (number-to-string gnus-large-newsgroup)
4237                                    0)))))
4238                     (if (string-match "^[ \t]*$" input)
4239                         number
4240                       input)))
4241                  ((and (> scored marked) (< scored number)
4242                        (> (- scored number) 20))
4243                   (let ((input
4244                          (read-string
4245                           (format "%s %s (%d scored, %d total): "
4246                                   "How many articles from"
4247                                   group scored number))))
4248                     (if (string-match "^[ \t]*$" input)
4249                         number input)))
4250                  (t number))
4251               (quit nil))))))
4252     (setq select (if (stringp select) (string-to-number select) select))
4253     (if (or (null select) (zerop select))
4254         select
4255       (if (and (not (zerop scored)) (<= (abs select) scored))
4256           (progn
4257             (setq articles (sort scored-list '<))
4258             (setq number (length articles)))
4259         (setq articles (copy-sequence articles)))
4260
4261       (when (< (abs select) number)
4262         (if (< select 0)
4263             ;; Select the N oldest articles.
4264             (setcdr (nthcdr (1- (abs select)) articles) nil)
4265           ;; Select the N most recent articles.
4266           (setq articles (nthcdr (- number select) articles))))
4267       (setq gnus-newsgroup-unselected
4268             (gnus-sorted-intersection
4269              gnus-newsgroup-unreads
4270              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4271       articles)))
4272
4273 (defun gnus-killed-articles (killed articles)
4274   (let (out)
4275     (while articles
4276       (when (inline (gnus-member-of-range (car articles) killed))
4277         (push (car articles) out))
4278       (setq articles (cdr articles)))
4279     out))
4280
4281 (defun gnus-uncompress-marks (marks)
4282   "Uncompress the mark ranges in MARKS."
4283   (let ((uncompressed '(score bookmark))
4284         out)
4285     (while marks
4286       (if (memq (caar marks) uncompressed)
4287           (push (car marks) out)
4288         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4289       (setq marks (cdr marks)))
4290     out))
4291
4292 (defun gnus-adjust-marked-articles (info)
4293   "Set all article lists and remove all marks that are no longer valid."
4294   (let* ((marked-lists (gnus-info-marks info))
4295          (active (gnus-active (gnus-info-group info)))
4296          (min (car active))
4297          (max (cdr active))
4298          (types gnus-article-mark-lists)
4299          (uncompressed '(score bookmark killed))
4300          marks var articles article mark)
4301
4302     (while marked-lists
4303       (setq marks (pop marked-lists))
4304       (set (setq var (intern (format "gnus-newsgroup-%s"
4305                                      (car (rassq (setq mark (car marks))
4306                                                  types)))))
4307            (if (memq (car marks) uncompressed) (cdr marks)
4308              (gnus-uncompress-range (cdr marks))))
4309
4310       (setq articles (symbol-value var))
4311
4312       ;; All articles have to be subsets of the active articles.
4313       (cond
4314        ;; Adjust "simple" lists.
4315        ((memq mark '(tick dormant expire reply save))
4316         (while articles
4317           (when (or (< (setq article (pop articles)) min) (> article max))
4318             (set var (delq article (symbol-value var))))))
4319        ;; Adjust assocs.
4320        ((memq mark uncompressed)
4321         (when (not (listp (cdr (symbol-value var))))
4322           (set var (list (symbol-value var))))
4323         (when (not (listp (cdr articles)))
4324           (setq articles (list articles)))
4325         (while articles
4326           (when (or (not (consp (setq article (pop articles))))
4327                     (< (car article) min)
4328                     (> (car article) max))
4329             (set var (delq article (symbol-value var))))))))))
4330
4331 (defun gnus-update-missing-marks (missing)
4332   "Go through the list of MISSING articles and remove them from the mark lists."
4333   (when missing
4334     (let ((types gnus-article-mark-lists)
4335           var m)
4336       ;; Go through all types.
4337       (while types
4338         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4339         (when (symbol-value var)
4340           ;; This list has articles.  So we delete all missing articles
4341           ;; from it.
4342           (setq m missing)
4343           (while m
4344             (set var (delq (pop m) (symbol-value var)))))))))
4345
4346 (defun gnus-update-marks ()
4347   "Enter the various lists of marked articles into the newsgroup info list."
4348   (let ((types gnus-article-mark-lists)
4349         (info (gnus-get-info gnus-newsgroup-name))
4350         (uncompressed '(score bookmark killed))
4351         type list newmarked symbol delta-marks)
4352     (when info
4353       ;; Add all marks lists to the list of marks lists.
4354       (while (setq type (pop types))
4355         (setq list (symbol-value
4356                           (setq symbol
4357                                 (intern (format "gnus-newsgroup-%s"
4358                                                 (car type))))))
4359
4360         (when list
4361           ;; Get rid of the entries of the articles that have the
4362           ;; default score.
4363           (when (and (eq (cdr type) 'score)
4364                      gnus-save-score
4365                      list)
4366             (let* ((arts list)
4367                    (prev (cons nil list))
4368                    (all prev))
4369               (while arts
4370                 (if (or (not (consp (car arts)))
4371                         (= (cdar arts) gnus-summary-default-score))
4372                     (setcdr prev (cdr arts))
4373                   (setq prev arts))
4374                 (setq arts (cdr arts)))
4375               (setq list (cdr all)))))
4376
4377        (or (memq (cdr type) uncompressed)
4378            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4379        
4380        (when (gnus-check-backend-function 'request-set-mark
4381                                           gnus-newsgroup-name)
4382          ;; uncompressed:s are not proper flags (they are cons cells)
4383          ;; cache is a internal gnus flag
4384          (unless (memq (cdr type) (cons 'cache uncompressed))
4385            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4386                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4387                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4388              (if add
4389                  (push (list add 'add (list (cdr type))) delta-marks))
4390              (if del
4391                  (push (list del 'del (list (cdr type))) delta-marks)))))
4392           
4393         (when list
4394          (push (cons (cdr type) list) newmarked)))
4395
4396       (when delta-marks
4397         (unless (gnus-check-group gnus-newsgroup-name)
4398           (error "Can't open server for %s" gnus-newsgroup-name))
4399         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4400           
4401       ;; Enter these new marks into the info of the group.
4402       (if (nthcdr 3 info)
4403           (setcar (nthcdr 3 info) newmarked)
4404         ;; Add the marks lists to the end of the info.
4405         (when newmarked
4406           (setcdr (nthcdr 2 info) (list newmarked))))
4407
4408       ;; Cut off the end of the info if there's nothing else there.
4409       (let ((i 5))
4410         (while (and (> i 2)
4411                     (not (nth i info)))
4412           (when (nthcdr (decf i) info)
4413             (setcdr (nthcdr i info) nil)))))))
4414
4415 (defun gnus-set-mode-line (where)
4416   "This function sets the mode line of the article or summary buffers.
4417 If WHERE is `summary', the summary mode line format will be used."
4418   ;; Is this mode line one we keep updated?
4419   (when (and (memq where gnus-updated-mode-lines)
4420              (symbol-value
4421               (intern (format "gnus-%s-mode-line-format-spec" where))))
4422     (let (mode-string)
4423       (save-excursion
4424         ;; We evaluate this in the summary buffer since these
4425         ;; variables are buffer-local to that buffer.
4426         (set-buffer gnus-summary-buffer)
4427         ;; We bind all these variables that are used in the `eval' form
4428         ;; below.
4429         (let* ((mformat (symbol-value
4430                          (intern
4431                           (format "gnus-%s-mode-line-format-spec" where))))
4432                (gnus-tmp-group-name gnus-newsgroup-name)
4433                (gnus-tmp-article-number (or gnus-current-article 0))
4434                (gnus-tmp-unread gnus-newsgroup-unreads)
4435                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4436                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4437                (gnus-tmp-unread-and-unselected
4438                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4439                             (zerop gnus-tmp-unselected))
4440                        "")
4441                       ((zerop gnus-tmp-unselected)
4442                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4443                       (t (format "{%d(+%d) more}"
4444                                  gnus-tmp-unread-and-unticked
4445                                  gnus-tmp-unselected))))
4446                (gnus-tmp-subject
4447                 (if (and gnus-current-headers
4448                          (vectorp gnus-current-headers))
4449                     (gnus-mode-string-quote
4450                      (mail-header-subject gnus-current-headers))
4451                   ""))
4452                bufname-length max-len
4453                gnus-tmp-header);; passed as argument to any user-format-funcs
4454           (setq mode-string (eval mformat))
4455           (setq bufname-length (if (string-match "%b" mode-string)
4456                                    (- (length
4457                                        (buffer-name
4458                                         (if (eq where 'summary)
4459                                             nil
4460                                           (get-buffer gnus-article-buffer))))
4461                                       2)
4462                                  0))
4463           (setq max-len (max 4 (if gnus-mode-non-string-length
4464                                    (- (window-width)
4465                                       gnus-mode-non-string-length
4466                                       bufname-length)
4467                                  (length mode-string))))
4468           ;; We might have to chop a bit of the string off...
4469           (when (> (length mode-string) max-len)
4470             (setq mode-string
4471                   (concat (gnus-truncate-string mode-string (- max-len 3))
4472                           "...")))
4473           ;; Pad the mode string a bit.
4474           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4475       ;; Update the mode line.
4476       (setq mode-line-buffer-identification
4477             (gnus-mode-line-buffer-identification (list mode-string)))
4478       (set-buffer-modified-p t))))
4479
4480 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4481   "Go through the HEADERS list and add all Xrefs to a hash table.
4482 The resulting hash table is returned, or nil if no Xrefs were found."
4483   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4484          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4485          (xref-hashtb (gnus-make-hashtable))
4486          start group entry number xrefs header)
4487     (while headers
4488       (setq header (pop headers))
4489       (when (and (setq xrefs (mail-header-xref header))
4490                  (not (memq (setq number (mail-header-number header))
4491                             unreads)))
4492         (setq start 0)
4493         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4494           (setq start (match-end 0))
4495           (setq group (if prefix
4496                           (concat prefix (substring xrefs (match-beginning 1)
4497                                                     (match-end 1)))
4498                         (substring xrefs (match-beginning 1) (match-end 1))))
4499           (setq number
4500                 (string-to-int (substring xrefs (match-beginning 2)
4501                                           (match-end 2))))
4502           (if (setq entry (gnus-gethash group xref-hashtb))
4503               (setcdr entry (cons number (cdr entry)))
4504             (gnus-sethash group (cons number nil) xref-hashtb)))))
4505     (and start xref-hashtb)))
4506
4507 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4508   "Look through all the headers and mark the Xrefs as read."
4509   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4510         name entry info xref-hashtb idlist method nth4)
4511     (save-excursion
4512       (set-buffer gnus-group-buffer)
4513       (when (setq xref-hashtb
4514                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4515         (mapatoms
4516          (lambda (group)
4517            (unless (string= from-newsgroup (setq name (symbol-name group)))
4518              (setq idlist (symbol-value group))
4519              ;; Dead groups are not updated.
4520              (and (prog1
4521                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4522                             info (nth 2 entry))
4523                     (when (stringp (setq nth4 (gnus-info-method info)))
4524                       (setq nth4 (gnus-server-to-method nth4))))
4525                   ;; Only do the xrefs if the group has the same
4526                   ;; select method as the group we have just read.
4527                   (or (gnus-methods-equal-p
4528                        nth4 (gnus-find-method-for-group from-newsgroup))
4529                       virtual
4530                       (equal nth4 (setq method (gnus-find-method-for-group
4531                                                 from-newsgroup)))
4532                       (and (equal (car nth4) (car method))
4533                            (equal (nth 1 nth4) (nth 1 method))))
4534                   gnus-use-cross-reference
4535                   (or (not (eq gnus-use-cross-reference t))
4536                       virtual
4537                       ;; Only do cross-references on subscribed
4538                       ;; groups, if that is what is wanted.
4539                       (<= (gnus-info-level info) gnus-level-subscribed))
4540                   (gnus-group-make-articles-read name idlist))))
4541          xref-hashtb)))))
4542
4543 (defun gnus-compute-read-articles (group articles)
4544   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4545          (info (nth 2 entry))
4546          (active (gnus-active group))
4547          ninfo)
4548     (when entry
4549       ;; First peel off all invalid article numbers.
4550       (when active
4551         (let ((ids articles)
4552               id first)
4553           (while (setq id (pop ids))
4554             (when (and first (> id (cdr active)))
4555               ;; We'll end up in this situation in one particular
4556               ;; obscure situation.  If you re-scan a group and get
4557               ;; a new article that is cross-posted to a different
4558               ;; group that has not been re-scanned, you might get
4559               ;; crossposted article that has a higher number than
4560               ;; Gnus believes possible.  So we re-activate this
4561               ;; group as well.  This might mean doing the
4562               ;; crossposting thingy will *increase* the number
4563               ;; of articles in some groups.  Tsk, tsk.
4564               (setq active (or (gnus-activate-group group) active)))
4565             (when (or (> id (cdr active))
4566                       (< id (car active)))
4567               (setq articles (delq id articles))))))
4568       ;; If the read list is nil, we init it.
4569       (if (and active
4570                (null (gnus-info-read info))
4571                (> (car active) 1))
4572           (setq ninfo (cons 1 (1- (car active))))
4573         (setq ninfo (gnus-info-read info)))
4574       ;; Then we add the read articles to the range.
4575       (gnus-add-to-range
4576        ninfo (setq articles (sort articles '<))))))
4577
4578 (defun gnus-group-make-articles-read (group articles)
4579   "Update the info of GROUP to say that ARTICLES are read."
4580   (let* ((num 0)
4581          (entry (gnus-gethash group gnus-newsrc-hashtb))
4582          (info (nth 2 entry))
4583          (active (gnus-active group))
4584          range)
4585     (when entry
4586       (setq range (gnus-compute-read-articles group articles))
4587       (save-excursion
4588         (set-buffer gnus-group-buffer)
4589         (gnus-undo-register
4590           `(progn
4591              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4592              (gnus-info-set-read ',info ',(gnus-info-read info))
4593              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4594              (gnus-group-update-group ,group t))))
4595       ;; Add the read articles to the range.
4596       (gnus-info-set-read info range)
4597       ;; Then we have to re-compute how many unread
4598       ;; articles there are in this group.
4599       (when active
4600         (cond
4601          ((not range)
4602           (setq num (- (1+ (cdr active)) (car active))))
4603          ((not (listp (cdr range)))
4604           (setq num (- (cdr active) (- (1+ (cdr range))
4605                                        (car range)))))
4606          (t
4607           (while range
4608             (if (numberp (car range))
4609                 (setq num (1+ num))
4610               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4611             (setq range (cdr range)))
4612           (setq num (- (cdr active) num))))
4613         ;; Update the number of unread articles.
4614         (setcar entry num)
4615         ;; Update the group buffer.
4616         (gnus-group-update-group group t)))))
4617
4618 (defvar gnus-newsgroup-none-id 0)
4619
4620 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4621   (let ((cur nntp-server-buffer)
4622         (dependencies
4623          (or dependencies
4624              (save-excursion (set-buffer gnus-summary-buffer)
4625                              gnus-newsgroup-dependencies)))
4626         headers id end ref
4627         (mail-parse-charset gnus-newsgroup-charset)
4628         (mail-parse-ignored-charsets
4629          (save-excursion (condition-case nil
4630                              (set-buffer gnus-summary-buffer)
4631                            (error))
4632                          gnus-newsgroup-ignored-charsets)))
4633     (save-excursion
4634       (set-buffer nntp-server-buffer)
4635       ;; Translate all TAB characters into SPACE characters.
4636       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4637       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4638       (gnus-run-hooks 'gnus-parse-headers-hook)
4639       (let ((case-fold-search t)
4640             in-reply-to header p lines chars ctype)
4641         (goto-char (point-min))
4642         ;; Search to the beginning of the next header.  Error messages
4643         ;; do not begin with 2 or 3.
4644         (while (re-search-forward "^[23][0-9]+ " nil t)
4645           (setq id nil
4646                 ref nil)
4647           ;; This implementation of this function, with nine
4648           ;; search-forwards instead of the one re-search-forward and
4649           ;; a case (which basically was the old function) is actually
4650           ;; about twice as fast, even though it looks messier.  You
4651           ;; can't have everything, I guess.  Speed and elegance
4652           ;; doesn't always go hand in hand.
4653           (setq
4654            header
4655            (make-full-mail-header
4656             ;; Number.
4657             (prog1
4658                 (read cur)
4659               (end-of-line)
4660               (setq p (point))
4661               (narrow-to-region (point)
4662                                 (or (and (search-forward "\n.\n" nil t)
4663                                          (- (point) 2))
4664                                     (point))))
4665             ;; Subject.
4666             (progn
4667               (goto-char p)
4668               (if (search-forward "\nsubject: " nil t)
4669                   (buffer-substring (match-end 0) (std11-field-end))
4670                 "(none)"))
4671             ;; From.
4672             (progn
4673               (goto-char p)
4674               (if (search-forward "\nfrom: " nil t)
4675                   (buffer-substring (match-end 0) (std11-field-end))
4676                 "(nobody)"))
4677             ;; Date.
4678             (progn
4679               (goto-char p)
4680               (if (search-forward "\ndate: " nil t)
4681                   (buffer-substring (match-end 0) (std11-field-end))
4682                 ""))
4683             ;; Message-ID.
4684             (progn
4685               (goto-char p)
4686               (setq id (if (re-search-forward
4687                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4688                            ;; We do it this way to make sure the Message-ID
4689                            ;; is (somewhat) syntactically valid.
4690                            (buffer-substring (match-beginning 1)
4691                                              (match-end 1))
4692                          ;; If there was no message-id, we just fake one
4693                          ;; to make subsequent routines simpler.
4694                          (nnheader-generate-fake-message-id))))
4695             ;; References.
4696             (progn
4697               (goto-char p)
4698               (if (search-forward "\nreferences: " nil t)
4699                   (progn
4700                     (setq end (point))
4701                     (prog1
4702                         (buffer-substring (match-end 0) (std11-field-end))
4703                       (setq ref
4704                             (buffer-substring
4705                              (progn
4706                                ;; (end-of-line)
4707                                (search-backward ">" end t)
4708                                (1+ (point)))
4709                              (progn
4710                                (search-backward "<" end t)
4711                                (point))))))
4712                 ;; Get the references from the in-reply-to header if there
4713                 ;; were no references and the in-reply-to header looks
4714                 ;; promising.
4715                 (if (and (search-forward "\nin-reply-to: " nil t)
4716                          (setq in-reply-to
4717                                (buffer-substring (match-end 0)
4718                                                  (std11-field-end)))
4719                          (string-match "<[^>]+>" in-reply-to))
4720                     (let (ref2)
4721                       (setq ref (substring in-reply-to (match-beginning 0)
4722                                            (match-end 0)))
4723                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4724                         (setq ref2 (substring in-reply-to (match-beginning 0)
4725                                               (match-end 0)))
4726                         (when (> (length ref2) (length ref))
4727                           (setq ref ref2)))
4728                       ref)
4729                   (setq ref nil))))
4730             ;; Chars.
4731             (progn
4732               (goto-char p)
4733               (if (search-forward "\nchars: " nil t)
4734                   (if (numberp (setq chars (ignore-errors (read cur))))
4735                       chars 0)
4736                 0))
4737             ;; Lines.
4738             (progn
4739               (goto-char p)
4740               (if (search-forward "\nlines: " nil t)
4741                   (if (numberp (setq lines (ignore-errors (read cur))))
4742                       lines 0)
4743                 0))
4744             ;; Xref.
4745             (progn
4746               (goto-char p)
4747               (and (search-forward "\nxref: " nil t)
4748                    (buffer-substring (match-end 0) (std11-field-end))))
4749             ;; Extra.
4750             (when gnus-extra-headers
4751               (let ((extra gnus-extra-headers)
4752                     out)
4753                 (while extra
4754                   (goto-char p)
4755                   (when (search-forward
4756                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4757                     (push (cons (car extra)
4758                                 (buffer-substring (match-end 0)
4759                                                   (std11-field-end)))
4760                           out))
4761                   (pop extra))
4762                 out))))
4763           (goto-char p)
4764           (if (and (search-forward "\ncontent-type: " nil t)
4765                    (setq ctype
4766                          (buffer-substring (match-end 0) (std11-field-end))))
4767               (mime-entity-set-content-type-internal
4768                header (mime-parse-Content-Type ctype)))
4769           (when (equal id ref)
4770             (setq ref nil))
4771
4772           (when gnus-alter-header-function
4773             (funcall gnus-alter-header-function header)
4774             (setq id (mail-header-id header)
4775                   ref (gnus-parent-id (mail-header-references header))))
4776
4777           (when (setq header
4778                       (gnus-dependencies-add-header
4779                        header dependencies force-new))
4780             (push header headers))
4781           (goto-char (point-max))
4782           (widen))
4783         (nreverse headers)))))
4784
4785 ;; Goes through the xover lines and returns a list of vectors
4786 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4787                                                   force-new dependencies
4788                                                   group also-fetch-heads)
4789   "Parse the news overview data in the server buffer, and return a
4790 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4791   ;; Get the Xref when the users reads the articles since most/some
4792   ;; NNTP servers do not include Xrefs when using XOVER.
4793   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4794   (let ((mail-parse-charset gnus-newsgroup-charset)
4795         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4796         (cur nntp-server-buffer)
4797         (dependencies (or dependencies gnus-newsgroup-dependencies))
4798         number headers header)
4799     (save-excursion
4800       (set-buffer nntp-server-buffer)
4801       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4802       ;; Allow the user to mangle the headers before parsing them.
4803       (gnus-run-hooks 'gnus-parse-headers-hook)
4804       (goto-char (point-min))
4805       (while (not (eobp))
4806         (condition-case ()
4807             (while (and sequence (not (eobp)))
4808               (setq number (read cur))
4809               (while (and sequence
4810                           (< (car sequence) number))
4811                 (setq sequence (cdr sequence)))
4812               (and sequence
4813                    (eq number (car sequence))
4814                    (progn
4815                      (setq sequence (cdr sequence))
4816                      (setq header (inline
4817                                     (gnus-nov-parse-line
4818                                      number dependencies force-new))))
4819                    (push header headers))
4820               (forward-line 1))
4821           (error
4822            (gnus-error 4 "Strange nov line (%d)"
4823                        (count-lines (point-min) (point)))))
4824         (forward-line 1))
4825       ;; A common bug in inn is that if you have posted an article and
4826       ;; then retrieves the active file, it will answer correctly --
4827       ;; the new article is included.  However, a NOV entry for the
4828       ;; article may not have been generated yet, so this may fail.
4829       ;; We work around this problem by retrieving the last few
4830       ;; headers using HEAD.
4831       (if (or (not also-fetch-heads)
4832               (not sequence))
4833           ;; We (probably) got all the headers.
4834           (nreverse headers)
4835         (let ((gnus-nov-is-evil t))
4836           (nconc
4837            (nreverse headers)
4838            (gnus-retrieve-parsed-headers sequence group)
4839            ))))))
4840
4841 (defun gnus-article-get-xrefs ()
4842   "Fill in the Xref value in `gnus-current-headers', if necessary.
4843 This is meant to be called in `gnus-article-internal-prepare-hook'."
4844   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4845                                  gnus-current-headers)))
4846     (or (not gnus-use-cross-reference)
4847         (not headers)
4848         (and (mail-header-xref headers)
4849              (not (string= (mail-header-xref headers) "")))
4850         (let ((case-fold-search t)
4851               xref)
4852           (save-restriction
4853             (nnheader-narrow-to-headers)
4854             (goto-char (point-min))
4855             (when (or (and (eq (downcase (char-after)) ?x)
4856                            (looking-at "Xref:"))
4857                       (search-forward "\nXref:" nil t))
4858               (goto-char (1+ (match-end 0)))
4859               (setq xref (buffer-substring (point)
4860                                            (progn (end-of-line) (point))))
4861               (mail-header-set-xref headers xref)))))))
4862
4863 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4864   "Find article ID and insert the summary line for that article.
4865 OLD-HEADER can either be a header or a line number to insert
4866 the subject line on."
4867   (let* ((line (and (numberp old-header) old-header))
4868          (old-header (and (vectorp old-header) old-header))
4869          (header (cond ((and old-header use-old-header)
4870                         old-header)
4871                        ((and (numberp id)
4872                              (gnus-number-to-header id))
4873                         (gnus-number-to-header id))
4874                        (t
4875                         (gnus-read-header id))))
4876          (number (and (numberp id) id))
4877          d)
4878     (when header
4879       ;; Rebuild the thread that this article is part of and go to the
4880       ;; article we have fetched.
4881       (when (and (not gnus-show-threads)
4882                  old-header)
4883         (when (and number
4884                    (setq d (gnus-data-find (mail-header-number old-header))))
4885           (goto-char (gnus-data-pos d))
4886           (gnus-data-remove
4887            number
4888            (- (gnus-point-at-bol)
4889               (prog1
4890                   (1+ (gnus-point-at-eol))
4891                 (gnus-delete-line))))))
4892       (when old-header
4893         (mail-header-set-number header (mail-header-number old-header)))
4894       (setq gnus-newsgroup-sparse
4895             (delq (setq number (mail-header-number header))
4896                   gnus-newsgroup-sparse))
4897       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4898       (push number gnus-newsgroup-limit)
4899       (gnus-rebuild-thread (mail-header-id header) line)
4900       (gnus-summary-goto-subject number nil t))
4901     (when (and (numberp number)
4902                (> number 0))
4903       ;; We have to update the boundaries even if we can't fetch the
4904       ;; article if ID is a number -- so that the next `P' or `N'
4905       ;; command will fetch the previous (or next) article even
4906       ;; if the one we tried to fetch this time has been canceled.
4907       (when (> number gnus-newsgroup-end)
4908         (setq gnus-newsgroup-end number))
4909       (when (< number gnus-newsgroup-begin)
4910         (setq gnus-newsgroup-begin number))
4911       (setq gnus-newsgroup-unselected
4912             (delq number gnus-newsgroup-unselected)))
4913     ;; Report back a success?
4914     (and header (mail-header-number header))))
4915
4916 ;;; Process/prefix in the summary buffer
4917
4918 (defun gnus-summary-work-articles (n)
4919   "Return a list of articles to be worked upon.
4920 The prefix argument, the list of process marked articles, and the
4921 current article will be taken into consideration."
4922   (save-excursion
4923     (set-buffer gnus-summary-buffer)
4924     (cond
4925      (n
4926       ;; A numerical prefix has been given.
4927       (setq n (prefix-numeric-value n))
4928       (let ((backward (< n 0))
4929             (n (abs (prefix-numeric-value n)))
4930             articles article)
4931         (save-excursion
4932           (while
4933               (and (> n 0)
4934                    (push (setq article (gnus-summary-article-number))
4935                          articles)
4936                    (if backward
4937                        (gnus-summary-find-prev nil article)
4938                      (gnus-summary-find-next nil article)))
4939             (decf n)))
4940         (nreverse articles)))
4941      ((and (gnus-region-active-p) (mark))
4942       (message "region active")
4943       ;; Work on the region between point and mark.
4944       (let ((max (max (point) (mark)))
4945             articles article)
4946         (save-excursion
4947           (goto-char (min (point) (mark)))
4948           (while
4949               (and
4950                (push (setq article (gnus-summary-article-number)) articles)
4951                (gnus-summary-find-next nil article)
4952                (< (point) max)))
4953           (nreverse articles))))
4954      (gnus-newsgroup-processable
4955       ;; There are process-marked articles present.
4956       ;; Save current state.
4957       (gnus-summary-save-process-mark)
4958       ;; Return the list.
4959       (reverse gnus-newsgroup-processable))
4960      (t
4961       ;; Just return the current article.
4962       (list (gnus-summary-article-number))))))
4963
4964 (defmacro gnus-summary-iterate (arg &rest forms)
4965   "Iterate over the process/prefixed articles and do FORMS.
4966 ARG is the interactive prefix given to the command.  FORMS will be
4967 executed with point over the summary line of the articles."
4968   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4969     `(let ((,articles (gnus-summary-work-articles ,arg)))
4970        (while ,articles
4971          (gnus-summary-goto-subject (car ,articles))
4972          ,@forms
4973          (pop ,articles)))))
4974
4975 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4976 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4977
4978 (defun gnus-summary-save-process-mark ()
4979   "Push the current set of process marked articles on the stack."
4980   (interactive)
4981   (push (copy-sequence gnus-newsgroup-processable)
4982         gnus-newsgroup-process-stack))
4983
4984 (defun gnus-summary-kill-process-mark ()
4985   "Push the current set of process marked articles on the stack and unmark."
4986   (interactive)
4987   (gnus-summary-save-process-mark)
4988   (gnus-summary-unmark-all-processable))
4989
4990 (defun gnus-summary-yank-process-mark ()
4991   "Pop the last process mark state off the stack and restore it."
4992   (interactive)
4993   (unless gnus-newsgroup-process-stack
4994     (error "Empty mark stack"))
4995   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4996
4997 (defun gnus-summary-process-mark-set (set)
4998   "Make SET into the current process marked articles."
4999   (gnus-summary-unmark-all-processable)
5000   (while set
5001     (gnus-summary-set-process-mark (pop set))))
5002
5003 ;;; Searching and stuff
5004
5005 (defun gnus-summary-search-group (&optional backward use-level)
5006   "Search for next unread newsgroup.
5007 If optional argument BACKWARD is non-nil, search backward instead."
5008   (save-excursion
5009     (set-buffer gnus-group-buffer)
5010     (when (gnus-group-search-forward
5011            backward nil (if use-level (gnus-group-group-level) nil))
5012       (gnus-group-group-name))))
5013
5014 (defun gnus-summary-best-group (&optional exclude-group)
5015   "Find the name of the best unread group.
5016 If EXCLUDE-GROUP, do not go to this group."
5017   (save-excursion
5018     (set-buffer gnus-group-buffer)
5019     (save-excursion
5020       (gnus-group-best-unread-group exclude-group))))
5021
5022 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5023   (if backward (gnus-summary-find-prev)
5024     (let* ((dummy (gnus-summary-article-intangible-p))
5025            (article (or article (gnus-summary-article-number)))
5026            (arts (gnus-data-find-list article))
5027            result)
5028       (when (and (not dummy)
5029                  (or (not gnus-summary-check-current)
5030                      (not unread)
5031                      (not (gnus-data-unread-p (car arts)))))
5032         (setq arts (cdr arts)))
5033       (when (setq result
5034                   (if unread
5035                       (progn
5036                         (while arts
5037                           (when (or (and undownloaded
5038                                          (eq gnus-undownloaded-mark
5039                                              (gnus-data-mark (car arts))))
5040                                     (gnus-data-unread-p (car arts)))
5041                             (setq result (car arts)
5042                                   arts nil))
5043                           (setq arts (cdr arts)))
5044                         result)
5045                     (car arts)))
5046         (goto-char (gnus-data-pos result))
5047         (gnus-data-number result)))))
5048
5049 (defun gnus-summary-find-prev (&optional unread article)
5050   (let* ((eobp (eobp))
5051          (article (or article (gnus-summary-article-number)))
5052          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5053          result)
5054     (when (and (not eobp)
5055                (or (not gnus-summary-check-current)
5056                    (not unread)
5057                    (not (gnus-data-unread-p (car arts)))))
5058       (setq arts (cdr arts)))
5059     (when (setq result
5060                 (if unread
5061                     (progn
5062                       (while arts
5063                         (when (gnus-data-unread-p (car arts))
5064                           (setq result (car arts)
5065                                 arts nil))
5066                         (setq arts (cdr arts)))
5067                       result)
5068                   (car arts)))
5069       (goto-char (gnus-data-pos result))
5070       (gnus-data-number result))))
5071
5072 (defun gnus-summary-find-subject (subject &optional unread backward article)
5073   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5074          (article (or article (gnus-summary-article-number)))
5075          (articles (gnus-data-list backward))
5076          (arts (gnus-data-find-list article articles))
5077          result)
5078     (when (or (not gnus-summary-check-current)
5079               (not unread)
5080               (not (gnus-data-unread-p (car arts))))
5081       (setq arts (cdr arts)))
5082     (while arts
5083       (and (or (not unread)
5084                (gnus-data-unread-p (car arts)))
5085            (vectorp (gnus-data-header (car arts)))
5086            (gnus-subject-equal
5087             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5088            (setq result (car arts)
5089                  arts nil))
5090       (setq arts (cdr arts)))
5091     (and result
5092          (goto-char (gnus-data-pos result))
5093          (gnus-data-number result))))
5094
5095 (defun gnus-summary-search-forward (&optional unread subject backward)
5096   "Search forward for an article.
5097 If UNREAD, look for unread articles.  If SUBJECT, look for
5098 articles with that subject.  If BACKWARD, search backward instead."
5099   (cond (subject (gnus-summary-find-subject subject unread backward))
5100         (backward (gnus-summary-find-prev unread))
5101         (t (gnus-summary-find-next unread))))
5102
5103 (defun gnus-recenter (&optional n)
5104   "Center point in window and redisplay frame.
5105 Also do horizontal recentering."
5106   (interactive "P")
5107   (when (and gnus-auto-center-summary
5108              (not (eq gnus-auto-center-summary 'vertical)))
5109     (gnus-horizontal-recenter))
5110   (recenter n))
5111
5112 (defun gnus-summary-recenter ()
5113   "Center point in the summary window.
5114 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5115 displayed, no centering will be performed."
5116   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5117   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5118   (let* ((top (cond ((< (window-height) 4) 0)
5119                     ((< (window-height) 7) 1)
5120                     (t (if (numberp gnus-auto-center-summary)
5121                            gnus-auto-center-summary
5122                          2))))
5123          (height (1- (window-height)))
5124          (bottom (save-excursion (goto-char (point-max))
5125                                  (forward-line (- height))
5126                                  (point)))
5127          (window (get-buffer-window (current-buffer))))
5128     ;; The user has to want it.
5129     (when gnus-auto-center-summary
5130       (when (get-buffer-window gnus-article-buffer)
5131         ;; Only do recentering when the article buffer is displayed,
5132         ;; Set the window start to either `bottom', which is the biggest
5133         ;; possible valid number, or the second line from the top,
5134         ;; whichever is the least.
5135         (set-window-start
5136          window (min bottom (save-excursion
5137                               (forward-line (- top)) (point)))))
5138       ;; Do horizontal recentering while we're at it.
5139       (when (and (get-buffer-window (current-buffer) t)
5140                  (not (eq gnus-auto-center-summary 'vertical)))
5141         (let ((selected (selected-window)))
5142           (select-window (get-buffer-window (current-buffer) t))
5143           (gnus-summary-position-point)
5144           (gnus-horizontal-recenter)
5145           (select-window selected))))))
5146
5147 (defun gnus-summary-jump-to-group (newsgroup)
5148   "Move point to NEWSGROUP in group mode buffer."
5149   ;; Keep update point of group mode buffer if visible.
5150   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5151       (save-window-excursion
5152         ;; Take care of tree window mode.
5153         (when (get-buffer-window gnus-group-buffer)
5154           (pop-to-buffer gnus-group-buffer))
5155         (gnus-group-jump-to-group newsgroup))
5156     (save-excursion
5157       ;; Take care of tree window mode.
5158       (if (get-buffer-window gnus-group-buffer)
5159           (pop-to-buffer gnus-group-buffer)
5160         (set-buffer gnus-group-buffer))
5161       (gnus-group-jump-to-group newsgroup))))
5162
5163 ;; This function returns a list of article numbers based on the
5164 ;; difference between the ranges of read articles in this group and
5165 ;; the range of active articles.
5166 (defun gnus-list-of-unread-articles (group)
5167   (let* ((read (gnus-info-read (gnus-get-info group)))
5168          (active (or (gnus-active group) (gnus-activate-group group)))
5169          (last (cdr active))
5170          first nlast unread)
5171     ;; If none are read, then all are unread.
5172     (if (not read)
5173         (setq first (car active))
5174       ;; If the range of read articles is a single range, then the
5175       ;; first unread article is the article after the last read
5176       ;; article.  Sounds logical, doesn't it?
5177       (if (not (listp (cdr read)))
5178           (setq first (max (car active) (1+ (cdr read))))
5179         ;; `read' is a list of ranges.
5180         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5181                                   (caar read)))
5182                   1)
5183           (setq first (car active)))
5184         (while read
5185           (when first
5186             (while (< first nlast)
5187               (push first unread)
5188               (setq first (1+ first))))
5189           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5190           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5191           (setq read (cdr read)))))
5192     ;; And add the last unread articles.
5193     (while (<= first last)
5194       (push first unread)
5195       (setq first (1+ first)))
5196     ;; Return the list of unread articles.
5197     (delq 0 (nreverse unread))))
5198
5199 (defun gnus-list-of-read-articles (group)
5200   "Return a list of unread, unticked and non-dormant articles."
5201   (let* ((info (gnus-get-info group))
5202          (marked (gnus-info-marks info))
5203          (active (gnus-active group)))
5204     (and info active
5205          (gnus-set-difference
5206           (gnus-sorted-complement
5207            (gnus-uncompress-range active)
5208            (gnus-list-of-unread-articles group))
5209           (append
5210            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5211            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5212
5213 ;; Various summary commands
5214
5215 (defun gnus-summary-select-article-buffer ()
5216   "Reconfigure windows to show article buffer."
5217   (interactive)
5218   (if (not (gnus-buffer-live-p gnus-article-buffer))
5219       (error "There is no article buffer for this summary buffer")
5220     (gnus-configure-windows 'article)
5221     (select-window (get-buffer-window gnus-article-buffer))))
5222
5223 (defun gnus-summary-universal-argument (arg)
5224   "Perform any operation on all articles that are process/prefixed."
5225   (interactive "P")
5226   (let ((articles (gnus-summary-work-articles arg))
5227         func article)
5228     (if (eq
5229          (setq
5230           func
5231           (key-binding
5232            (read-key-sequence
5233             (substitute-command-keys
5234              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5235              ))))
5236          'undefined)
5237         (gnus-error 1 "Undefined key")
5238       (save-excursion
5239         (while articles
5240           (gnus-summary-goto-subject (setq article (pop articles)))
5241           (let (gnus-newsgroup-processable)
5242             (command-execute func))
5243           (gnus-summary-remove-process-mark article)))))
5244   (gnus-summary-position-point))
5245
5246 (defun gnus-summary-toggle-truncation (&optional arg)
5247   "Toggle truncation of summary lines.
5248 With arg, turn line truncation on iff arg is positive."
5249   (interactive "P")
5250   (setq truncate-lines
5251         (if (null arg) (not truncate-lines)
5252           (> (prefix-numeric-value arg) 0)))
5253   (redraw-display))
5254
5255 (defun gnus-summary-reselect-current-group (&optional all rescan)
5256   "Rescan the current newsgroup, exit and then reselect it.
5257 The prefix argument ALL means to select all articles."
5258   (interactive "P")
5259   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5260     (error "Ephemeral groups can't be reselected"))
5261   (let ((current-subject (gnus-summary-article-number))
5262         (group gnus-newsgroup-name))
5263     (save-excursion
5264       (set-buffer gnus-group-buffer)
5265       ;; We have to adjust the point of group mode buffer because
5266       ;; point was moved to the next unread newsgroup by exiting.
5267       (gnus-summary-jump-to-group group)
5268       (when rescan
5269         (save-excursion
5270           (gnus-group-get-new-news-this-group 1))))
5271     (setq gnus-newsgroup-begin nil)
5272     (gnus-summary-exit)
5273     (gnus-group-read-group all t group)
5274     (gnus-summary-goto-subject current-subject nil t)))
5275
5276 (defun gnus-summary-rescan-group (&optional all)
5277   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5278   (interactive "P")
5279   (gnus-summary-reselect-current-group all t))
5280
5281 (defun gnus-summary-update-info (&optional non-destructive)
5282   (save-excursion
5283     (let ((group gnus-newsgroup-name))
5284       (when group
5285         (when gnus-newsgroup-kill-headers
5286           (setq gnus-newsgroup-killed
5287                 (gnus-compress-sequence
5288                  (nconc
5289                   (gnus-set-sorted-intersection
5290                    (gnus-uncompress-range gnus-newsgroup-killed)
5291                    (setq gnus-newsgroup-unselected
5292                          (sort gnus-newsgroup-unselected '<)))
5293                   (setq gnus-newsgroup-unreads
5294                         (sort gnus-newsgroup-unreads '<)))
5295                  t)))
5296         (unless (listp (cdr gnus-newsgroup-killed))
5297           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5298         (let ((headers gnus-newsgroup-headers))
5299           ;; Set the new ranges of read articles.
5300           (save-excursion
5301             (set-buffer gnus-group-buffer)
5302             (gnus-undo-force-boundary))
5303           (gnus-update-read-articles
5304            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5305           ;; Set the current article marks.
5306           (let ((gnus-newsgroup-scored
5307                  (if (and (not gnus-save-score)
5308                           (not non-destructive))
5309                      nil
5310                    gnus-newsgroup-scored)))
5311             (save-excursion
5312               (gnus-update-marks)))
5313           ;; Do the cross-ref thing.
5314           (when gnus-use-cross-reference
5315             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5316           ;; Do not switch windows but change the buffer to work.
5317           (set-buffer gnus-group-buffer)
5318           (unless (gnus-ephemeral-group-p group)
5319             (gnus-group-update-group group)))))))
5320
5321 (defun gnus-summary-save-newsrc (&optional force)
5322   "Save the current number of read/marked articles in the dribble buffer.
5323 The dribble buffer will then be saved.
5324 If FORCE (the prefix), also save the .newsrc file(s)."
5325   (interactive "P")
5326   (gnus-summary-update-info t)
5327   (if force
5328       (gnus-save-newsrc-file)
5329     (gnus-dribble-save)))
5330
5331 (defun gnus-summary-exit (&optional temporary)
5332   "Exit reading current newsgroup, and then return to group selection mode.
5333 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5334   (interactive)
5335   (gnus-set-global-variables)
5336   (gnus-kill-save-kill-buffer)
5337   (gnus-async-halt-prefetch)
5338   (let* ((group gnus-newsgroup-name)
5339          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5340          (mode major-mode)
5341          (group-point nil)
5342          (buf (current-buffer)))
5343     (unless quit-config
5344       ;; Do adaptive scoring, and possibly save score files.
5345       (when gnus-newsgroup-adaptive
5346         (let ((gnus-newsgroup-adaptive gnus-use-adaptive-scoring))
5347           (gnus-score-adaptive)))
5348       (when gnus-use-scoring
5349         (gnus-score-save)))
5350     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5351     ;; If we have several article buffers, we kill them at exit.
5352     (unless gnus-single-article-buffer
5353       (gnus-kill-buffer gnus-original-article-buffer)
5354       (setq gnus-article-current nil))
5355     (when gnus-use-cache
5356       (gnus-cache-possibly-remove-articles)
5357       (gnus-cache-save-buffers))
5358     (gnus-async-prefetch-remove-group group)
5359     (when gnus-suppress-duplicates
5360       (gnus-dup-enter-articles))
5361     (when gnus-use-trees
5362       (gnus-tree-close group))
5363     ;; Remove entries for this group.
5364     (nnmail-purge-split-history (gnus-group-real-name group))
5365     ;; Make all changes in this group permanent.
5366     (unless quit-config
5367       (gnus-run-hooks 'gnus-exit-group-hook)
5368       (gnus-summary-update-info))
5369     (gnus-close-group group)
5370     ;; Make sure where we were, and go to next newsgroup.
5371     (set-buffer gnus-group-buffer)
5372     (unless quit-config
5373       (gnus-group-jump-to-group group))
5374     (gnus-run-hooks 'gnus-summary-exit-hook)
5375     (unless (or quit-config
5376                 ;; If this group has disappeared from the summary
5377                 ;; buffer, don't skip forwards.
5378                 (not (string= group (gnus-group-group-name))))
5379       (gnus-group-next-unread-group 1))
5380     (setq group-point (point))
5381     (if temporary
5382         nil                             ;Nothing to do.
5383       ;; If we have several article buffers, we kill them at exit.
5384       (unless gnus-single-article-buffer
5385         (gnus-kill-buffer gnus-article-buffer)
5386         (gnus-kill-buffer gnus-original-article-buffer)
5387         (setq gnus-article-current nil))
5388       (set-buffer buf)
5389       (if (not gnus-kill-summary-on-exit)
5390           (gnus-deaden-summary)
5391         ;; We set all buffer-local variables to nil.  It is unclear why
5392         ;; this is needed, but if we don't, buffer-local variables are
5393         ;; not garbage-collected, it seems.  This would the lead to en
5394         ;; ever-growing Emacs.
5395         (gnus-summary-clear-local-variables)
5396         (when (get-buffer gnus-article-buffer)
5397           (bury-buffer gnus-article-buffer))
5398         ;; We clear the global counterparts of the buffer-local
5399         ;; variables as well, just to be on the safe side.
5400         (set-buffer gnus-group-buffer)
5401         (gnus-summary-clear-local-variables)
5402         ;; Return to group mode buffer.
5403         (when (eq mode 'gnus-summary-mode)
5404           (gnus-kill-buffer buf)))
5405       (setq gnus-current-select-method gnus-select-method)
5406       (pop-to-buffer gnus-group-buffer)
5407       (if (not quit-config)
5408           (progn
5409             (goto-char group-point)
5410             (gnus-configure-windows 'group 'force))
5411         (gnus-handle-ephemeral-exit quit-config))
5412       ;; Clear the current group name.
5413       (unless quit-config
5414         (setq gnus-newsgroup-name nil)))))
5415
5416 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5417 (defun gnus-summary-exit-no-update (&optional no-questions)
5418   "Quit reading current newsgroup without updating read article info."
5419   (interactive)
5420   (let* ((group gnus-newsgroup-name)
5421          (quit-config (gnus-group-quit-config group)))
5422     (when (or no-questions
5423               gnus-expert-user
5424               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5425       (gnus-async-halt-prefetch)
5426       (mapcar 'funcall
5427               (delq 'gnus-summary-expire-articles
5428                     (copy-list gnus-summary-prepare-exit-hook)))
5429       ;; If we have several article buffers, we kill them at exit.
5430       (unless gnus-single-article-buffer
5431         (gnus-kill-buffer gnus-article-buffer)
5432         (gnus-kill-buffer gnus-original-article-buffer)
5433         (setq gnus-article-current nil))
5434       (if (not gnus-kill-summary-on-exit)
5435           (gnus-deaden-summary)
5436         (gnus-close-group group)
5437         (gnus-summary-clear-local-variables)
5438         (set-buffer gnus-group-buffer)
5439         (gnus-summary-clear-local-variables)
5440         (when (get-buffer gnus-summary-buffer)
5441           (kill-buffer gnus-summary-buffer)))
5442       (unless gnus-single-article-buffer
5443         (setq gnus-article-current nil))
5444       (when gnus-use-trees
5445         (gnus-tree-close group))
5446       (gnus-async-prefetch-remove-group group)
5447       (when (get-buffer gnus-article-buffer)
5448         (bury-buffer gnus-article-buffer))
5449       ;; Return to the group buffer.
5450       (gnus-configure-windows 'group 'force)
5451       ;; Clear the current group name.
5452       (setq gnus-newsgroup-name nil)
5453       (when (equal (gnus-group-group-name) group)
5454         (gnus-group-next-unread-group 1))
5455       (when quit-config
5456         (gnus-handle-ephemeral-exit quit-config)))))
5457
5458 (defun gnus-handle-ephemeral-exit (quit-config)
5459   "Handle movement when leaving an ephemeral group.
5460 The state which existed when entering the ephemeral is reset."
5461   (if (not (buffer-name (car quit-config)))
5462       (gnus-configure-windows 'group 'force)
5463     (set-buffer (car quit-config))
5464     (cond ((eq major-mode 'gnus-summary-mode)
5465            (gnus-set-global-variables))
5466           ((eq major-mode 'gnus-article-mode)
5467            (save-excursion
5468              ;; The `gnus-summary-buffer' variable may point
5469              ;; to the old summary buffer when using a single
5470              ;; article buffer.
5471              (unless (gnus-buffer-live-p gnus-summary-buffer)
5472                (set-buffer gnus-group-buffer))
5473              (set-buffer gnus-summary-buffer)
5474              (gnus-set-global-variables))))
5475     (if (or (eq (cdr quit-config) 'article)
5476             (eq (cdr quit-config) 'pick))
5477         (progn
5478           ;; The current article may be from the ephemeral group
5479           ;; thus it is best that we reload this article
5480           (gnus-summary-show-article)
5481           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5482               (gnus-configure-windows 'pick 'force)
5483             (gnus-configure-windows (cdr quit-config) 'force)))
5484       (gnus-configure-windows (cdr quit-config) 'force))
5485     (when (eq major-mode 'gnus-summary-mode)
5486       (gnus-summary-next-subject 1 nil t)
5487       (gnus-summary-recenter)
5488       (gnus-summary-position-point))))
5489
5490 (defun gnus-summary-preview-mime-message ()
5491   "MIME decode and play this message."
5492   (interactive)
5493   (let ((gnus-break-pages nil)
5494         (gnus-show-mime t))
5495     (gnus-summary-select-article gnus-show-all-headers t))
5496   (select-window (get-buffer-window gnus-article-buffer)))
5497
5498 ;;; Dead summaries.
5499
5500 (defvar gnus-dead-summary-mode-map nil)
5501
5502 (unless gnus-dead-summary-mode-map
5503   (setq gnus-dead-summary-mode-map (make-keymap))
5504   (suppress-keymap gnus-dead-summary-mode-map)
5505   (substitute-key-definition
5506    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5507   (let ((keys '("\C-d" "\r" "\177" [delete])))
5508     (while keys
5509       (define-key gnus-dead-summary-mode-map
5510         (pop keys) 'gnus-summary-wake-up-the-dead))))
5511
5512 (defvar gnus-dead-summary-mode nil
5513   "Minor mode for Gnus summary buffers.")
5514
5515 (defun gnus-dead-summary-mode (&optional arg)
5516   "Minor mode for Gnus summary buffers."
5517   (interactive "P")
5518   (when (eq major-mode 'gnus-summary-mode)
5519     (make-local-variable 'gnus-dead-summary-mode)
5520     (setq gnus-dead-summary-mode
5521           (if (null arg) (not gnus-dead-summary-mode)
5522             (> (prefix-numeric-value arg) 0)))
5523     (when gnus-dead-summary-mode
5524       (gnus-add-minor-mode
5525        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5526
5527 (defun gnus-deaden-summary ()
5528   "Make the current summary buffer into a dead summary buffer."
5529   ;; Kill any previous dead summary buffer.
5530   (when (and gnus-dead-summary
5531              (buffer-name gnus-dead-summary))
5532     (save-excursion
5533       (set-buffer gnus-dead-summary)
5534       (when gnus-dead-summary-mode
5535         (kill-buffer (current-buffer)))))
5536   ;; Make this the current dead summary.
5537   (setq gnus-dead-summary (current-buffer))
5538   (gnus-dead-summary-mode 1)
5539   (let ((name (buffer-name)))
5540     (when (string-match "Summary" name)
5541       (rename-buffer
5542        (concat (substring name 0 (match-beginning 0)) "Dead "
5543                (substring name (match-beginning 0)))
5544        t))))
5545
5546 (defun gnus-kill-or-deaden-summary (buffer)
5547   "Kill or deaden the summary BUFFER."
5548   (save-excursion
5549     (when (and (buffer-name buffer)
5550                (not gnus-single-article-buffer))
5551       (save-excursion
5552         (set-buffer buffer)
5553         (gnus-kill-buffer gnus-article-buffer)
5554         (gnus-kill-buffer gnus-original-article-buffer)))
5555     (cond (gnus-kill-summary-on-exit
5556            (when (and gnus-use-trees
5557                       (gnus-buffer-exists-p buffer))
5558              (save-excursion
5559                (set-buffer buffer)
5560                (gnus-tree-close gnus-newsgroup-name)))
5561            (gnus-kill-buffer buffer))
5562           ((gnus-buffer-exists-p buffer)
5563            (save-excursion
5564              (set-buffer buffer)
5565              (gnus-deaden-summary))))))
5566
5567 (defun gnus-summary-wake-up-the-dead (&rest args)
5568   "Wake up the dead summary buffer."
5569   (interactive)
5570   (gnus-dead-summary-mode -1)
5571   (let ((name (buffer-name)))
5572     (when (string-match "Dead " name)
5573       (rename-buffer
5574        (concat (substring name 0 (match-beginning 0))
5575                (substring name (match-end 0)))
5576        t)))
5577   (gnus-message 3 "This dead summary is now alive again"))
5578
5579 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5580 (defun gnus-summary-fetch-faq (&optional faq-dir)
5581   "Fetch the FAQ for the current group.
5582 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5583 in."
5584   (interactive
5585    (list
5586     (when current-prefix-arg
5587       (completing-read
5588        "Faq dir: " (and (listp gnus-group-faq-directory)
5589                         (mapcar (lambda (file) (list file))
5590                                 gnus-group-faq-directory))))))
5591   (let (gnus-faq-buffer)
5592     (when (setq gnus-faq-buffer
5593                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5594       (gnus-configure-windows 'summary-faq))))
5595
5596 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5597 (defun gnus-summary-describe-group (&optional force)
5598   "Describe the current newsgroup."
5599   (interactive "P")
5600   (gnus-group-describe-group force gnus-newsgroup-name))
5601
5602 (defun gnus-summary-describe-briefly ()
5603   "Describe summary mode commands briefly."
5604   (interactive)
5605   (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")))
5606
5607 ;; Walking around group mode buffer from summary mode.
5608
5609 (defun gnus-summary-next-group (&optional no-article target-group backward)
5610   "Exit current newsgroup and then select next unread newsgroup.
5611 If prefix argument NO-ARTICLE is non-nil, no article is selected
5612 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5613 previous group instead."
5614   (interactive "P")
5615   ;; Stop pre-fetching.
5616   (gnus-async-halt-prefetch)
5617   (let ((current-group gnus-newsgroup-name)
5618         (current-buffer (current-buffer))
5619         entered)
5620     ;; First we semi-exit this group to update Xrefs and all variables.
5621     ;; We can't do a real exit, because the window conf must remain
5622     ;; the same in case the user is prompted for info, and we don't
5623     ;; want the window conf to change before that...
5624     (gnus-summary-exit t)
5625     (while (not entered)
5626       ;; Then we find what group we are supposed to enter.
5627       (set-buffer gnus-group-buffer)
5628       (gnus-group-jump-to-group current-group)
5629       (setq target-group
5630             (or target-group
5631                 (if (eq gnus-keep-same-level 'best)
5632                     (gnus-summary-best-group gnus-newsgroup-name)
5633                   (gnus-summary-search-group backward gnus-keep-same-level))))
5634       (if (not target-group)
5635           ;; There are no further groups, so we return to the group
5636           ;; buffer.
5637           (progn
5638             (gnus-message 5 "Returning to the group buffer")
5639             (setq entered t)
5640             (when (gnus-buffer-live-p current-buffer)
5641               (set-buffer current-buffer)
5642               (gnus-summary-exit))
5643             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5644         ;; We try to enter the target group.
5645         (gnus-group-jump-to-group target-group)
5646         (let ((unreads (gnus-group-group-unread)))
5647           (if (and (or (eq t unreads)
5648                        (and unreads (not (zerop unreads))))
5649                    (gnus-summary-read-group
5650                     target-group nil no-article
5651                     (and (buffer-name current-buffer) current-buffer)
5652                     nil backward))
5653               (setq entered t)
5654             (setq current-group target-group
5655                   target-group nil)))))))
5656
5657 (defun gnus-summary-prev-group (&optional no-article)
5658   "Exit current newsgroup and then select previous unread newsgroup.
5659 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5660   (interactive "P")
5661   (gnus-summary-next-group no-article nil t))
5662
5663 ;; Walking around summary lines.
5664
5665 (defun gnus-summary-first-subject (&optional unread undownloaded)
5666   "Go to the first unread subject.
5667 If UNREAD is non-nil, go to the first unread article.
5668 Returns the article selected or nil if there are no unread articles."
5669   (interactive "P")
5670   (prog1
5671       (cond
5672        ;; Empty summary.
5673        ((null gnus-newsgroup-data)
5674         (gnus-message 3 "No articles in the group")
5675         nil)
5676        ;; Pick the first article.
5677        ((not unread)
5678         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5679         (gnus-data-number (car gnus-newsgroup-data)))
5680        ;; No unread articles.
5681        ((null gnus-newsgroup-unreads)
5682         (gnus-message 3 "No more unread articles")
5683         nil)
5684        ;; Find the first unread article.
5685        (t
5686         (let ((data gnus-newsgroup-data))
5687           (while (and data
5688                       (and (not (and undownloaded
5689                                      (eq gnus-undownloaded-mark
5690                                          (gnus-data-mark (car data)))))
5691                            (not (gnus-data-unread-p (car data)))))
5692             (setq data (cdr data)))
5693           (when data
5694             (goto-char (gnus-data-pos (car data)))
5695             (gnus-data-number (car data))))))
5696     (gnus-summary-position-point)))
5697
5698 (defun gnus-summary-next-subject (n &optional unread dont-display)
5699   "Go to next N'th summary line.
5700 If N is negative, go to the previous N'th subject line.
5701 If UNREAD is non-nil, only unread articles are selected.
5702 The difference between N and the actual number of steps taken is
5703 returned."
5704   (interactive "p")
5705   (let ((backward (< n 0))
5706         (n (abs n)))
5707     (while (and (> n 0)
5708                 (if backward
5709                     (gnus-summary-find-prev unread)
5710                   (gnus-summary-find-next unread)))
5711       (gnus-summary-show-thread)
5712       (setq n (1- n)))
5713     (when (/= 0 n)
5714       (gnus-message 7 "No more%s articles"
5715                     (if unread " unread" "")))
5716     (unless dont-display
5717       (gnus-summary-recenter)
5718       (gnus-summary-position-point))
5719     n))
5720
5721 (defun gnus-summary-next-unread-subject (n)
5722   "Go to next N'th unread summary line."
5723   (interactive "p")
5724   (gnus-summary-next-subject n t))
5725
5726 (defun gnus-summary-prev-subject (n &optional unread)
5727   "Go to previous N'th summary line.
5728 If optional argument UNREAD is non-nil, only unread article is selected."
5729   (interactive "p")
5730   (gnus-summary-next-subject (- n) unread))
5731
5732 (defun gnus-summary-prev-unread-subject (n)
5733   "Go to previous N'th unread summary line."
5734   (interactive "p")
5735   (gnus-summary-next-subject (- n) t))
5736
5737 (defun gnus-summary-goto-subject (article &optional force silent)
5738   "Go the subject line of ARTICLE.
5739 If FORCE, also allow jumping to articles not currently shown."
5740   (interactive "nArticle number: ")
5741   (let ((b (point))
5742         (data (gnus-data-find article)))
5743     ;; We read in the article if we have to.
5744     (and (not data)
5745          force
5746          (gnus-summary-insert-subject
5747           article
5748           (if (or (numberp force) (vectorp force)) force)
5749           t)
5750          (setq data (gnus-data-find article)))
5751     (goto-char b)
5752     (if (not data)
5753         (progn
5754           (unless silent
5755             (gnus-message 3 "Can't find article %d" article))
5756           nil)
5757       (goto-char (gnus-data-pos data))
5758       (gnus-summary-position-point)
5759       article)))
5760
5761 ;; Walking around summary lines with displaying articles.
5762
5763 (defun gnus-summary-expand-window (&optional arg)
5764   "Make the summary buffer take up the entire Emacs frame.
5765 Given a prefix, will force an `article' buffer configuration."
5766   (interactive "P")
5767   (if arg
5768       (gnus-configure-windows 'article 'force)
5769     (gnus-configure-windows 'summary 'force)))
5770
5771 (defun gnus-summary-display-article (article &optional all-header)
5772   "Display ARTICLE in article buffer."
5773   (gnus-set-global-variables)
5774   (if (null article)
5775       nil
5776     (prog1
5777         (if gnus-summary-display-article-function
5778             (funcall gnus-summary-display-article-function article all-header)
5779           (gnus-article-prepare article all-header))
5780       (with-current-buffer gnus-article-buffer
5781         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5782              nil))
5783       (gnus-run-hooks 'gnus-select-article-hook)
5784       (when (and gnus-current-article
5785                  (not (zerop gnus-current-article)))
5786         (gnus-summary-goto-subject gnus-current-article))
5787       (gnus-summary-recenter)
5788       (when (and gnus-use-trees gnus-show-threads)
5789         (gnus-possibly-generate-tree article)
5790         (gnus-highlight-selected-tree article))
5791       ;; Successfully display article.
5792       (gnus-article-set-window-start
5793        (cdr (assq article gnus-newsgroup-bookmarks))))))
5794
5795 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5796   "Select the current article.
5797 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5798 non-nil, the article will be re-fetched even if it already present in
5799 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5800 be displayed."
5801   ;; Make sure we are in the summary buffer to work around bbdb bug.
5802   (unless (eq major-mode 'gnus-summary-mode)
5803     (set-buffer gnus-summary-buffer))
5804   (let ((article (or article (gnus-summary-article-number)))
5805         (all-headers (not (not all-headers))) ;Must be T or NIL.
5806         gnus-summary-display-article-function
5807         did)
5808     (and (not pseudo)
5809          (gnus-summary-article-pseudo-p article)
5810          (error "This is a pseudo-article"))
5811     (prog1
5812         (save-excursion
5813           (set-buffer gnus-summary-buffer)
5814           (if (or (and gnus-single-article-buffer
5815                        (or (null gnus-current-article)
5816                            (null gnus-article-current)
5817                            (null (get-buffer gnus-article-buffer))
5818                            (not (eq article (cdr gnus-article-current)))
5819                            (not (equal (car gnus-article-current)
5820                                        gnus-newsgroup-name))))
5821                   (and (not gnus-single-article-buffer)
5822                        (or (null gnus-current-article)
5823                            (not (eq gnus-current-article article))))
5824                   force)
5825               ;; The requested article is different from the current article.
5826               (prog1
5827                   (gnus-summary-display-article article all-headers)
5828                 (setq did article)
5829                 (when (or all-headers gnus-show-all-headers)
5830                   (gnus-article-show-all-headers)))
5831             (when (or all-headers gnus-show-all-headers)
5832               (gnus-article-show-all-headers))
5833             'old))
5834       (when did
5835         (gnus-article-set-window-start
5836          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5837
5838 (defun gnus-summary-set-current-mark (&optional current-mark)
5839   "Obsolete function."
5840   nil)
5841
5842 (defun gnus-summary-next-article (&optional unread subject backward push)
5843   "Select the next article.
5844 If UNREAD, only unread articles are selected.
5845 If SUBJECT, only articles with SUBJECT are selected.
5846 If BACKWARD, the previous article is selected instead of the next."
5847   (interactive "P")
5848   (cond
5849    ;; Is there such an article?
5850    ((and (gnus-summary-search-forward unread subject backward)
5851          (or (gnus-summary-display-article (gnus-summary-article-number))
5852              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5853     (gnus-summary-position-point))
5854    ;; If not, we try the first unread, if that is wanted.
5855    ((and subject
5856          gnus-auto-select-same
5857          (gnus-summary-first-unread-article))
5858     (gnus-summary-position-point)
5859     (gnus-message 6 "Wrapped"))
5860    ;; Try to get next/previous article not displayed in this group.
5861    ((and gnus-auto-extend-newsgroup
5862          (not unread) (not subject))
5863     (gnus-summary-goto-article
5864      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5865      nil (count-lines (point-min) (point))))
5866    ;; Go to next/previous group.
5867    (t
5868     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5869       (gnus-summary-jump-to-group gnus-newsgroup-name))
5870     (let ((cmd last-command-char)
5871           (point
5872            (save-excursion
5873              (set-buffer gnus-group-buffer)
5874              (point)))
5875           (group
5876            (if (eq gnus-keep-same-level 'best)
5877                (gnus-summary-best-group gnus-newsgroup-name)
5878              (gnus-summary-search-group backward gnus-keep-same-level))))
5879       ;; For some reason, the group window gets selected.  We change
5880       ;; it back.
5881       (select-window (get-buffer-window (current-buffer)))
5882       ;; Select next unread newsgroup automagically.
5883       (cond
5884        ((or (not gnus-auto-select-next)
5885             (not cmd))
5886         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5887        ((or (eq gnus-auto-select-next 'quietly)
5888             (and (eq gnus-auto-select-next 'slightly-quietly)
5889                  push)
5890             (and (eq gnus-auto-select-next 'almost-quietly)
5891                  (gnus-summary-last-article-p)))
5892         ;; Select quietly.
5893         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5894             (gnus-summary-exit)
5895           (gnus-message 7 "No more%s articles (%s)..."
5896                         (if unread " unread" "")
5897                         (if group (concat "selecting " group)
5898                           "exiting"))
5899           (gnus-summary-next-group nil group backward)))
5900        (t
5901         (when (gnus-key-press-event-p last-input-event)
5902           (gnus-summary-walk-group-buffer
5903            gnus-newsgroup-name cmd unread backward point))))))))
5904
5905 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5906   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5907                       (?\C-p (gnus-group-prev-unread-group 1))))
5908         (cursor-in-echo-area t)
5909         keve key group ended)
5910     (save-excursion
5911       (set-buffer gnus-group-buffer)
5912       (goto-char start)
5913       (setq group
5914             (if (eq gnus-keep-same-level 'best)
5915                 (gnus-summary-best-group gnus-newsgroup-name)
5916               (gnus-summary-search-group backward gnus-keep-same-level))))
5917     (while (not ended)
5918       (gnus-message
5919        5 "No more%s articles%s" (if unread " unread" "")
5920        (if (and group
5921                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5922            (format " (Type %s for %s [%s])"
5923                    (single-key-description cmd) group
5924                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5925          (format " (Type %s to exit %s)"
5926                  (single-key-description cmd)
5927                  gnus-newsgroup-name)))
5928       ;; Confirm auto selection.
5929       (setq key (car (setq keve (gnus-read-event-char))))
5930       (setq ended t)
5931       (cond
5932        ((assq key keystrokes)
5933         (let ((obuf (current-buffer)))
5934           (switch-to-buffer gnus-group-buffer)
5935           (when group
5936             (gnus-group-jump-to-group group))
5937           (eval (cadr (assq key keystrokes)))
5938           (setq group (gnus-group-group-name))
5939           (switch-to-buffer obuf))
5940         (setq ended nil))
5941        ((equal key cmd)
5942         (if (or (not group)
5943                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5944             (gnus-summary-exit)
5945           (gnus-summary-next-group nil group backward)))
5946        (t
5947         (push (cdr keve) unread-command-events))))))
5948
5949 (defun gnus-summary-next-unread-article ()
5950   "Select unread article after current one."
5951   (interactive)
5952   (gnus-summary-next-article
5953    (or (not (eq gnus-summary-goto-unread 'never))
5954        (gnus-summary-last-article-p (gnus-summary-article-number)))
5955    (and gnus-auto-select-same
5956         (gnus-summary-article-subject))))
5957
5958 (defun gnus-summary-prev-article (&optional unread subject)
5959   "Select the article after the current one.
5960 If UNREAD is non-nil, only unread articles are selected."
5961   (interactive "P")
5962   (gnus-summary-next-article unread subject t))
5963
5964 (defun gnus-summary-prev-unread-article ()
5965   "Select unread article before current one."
5966   (interactive)
5967   (gnus-summary-prev-article
5968    (or (not (eq gnus-summary-goto-unread 'never))
5969        (gnus-summary-first-article-p (gnus-summary-article-number)))
5970    (and gnus-auto-select-same
5971         (gnus-summary-article-subject))))
5972
5973 (defun gnus-summary-next-page (&optional lines circular)
5974   "Show next page of the selected article.
5975 If at the end of the current article, select the next article.
5976 LINES says how many lines should be scrolled up.
5977
5978 If CIRCULAR is non-nil, go to the start of the article instead of
5979 selecting the next article when reaching the end of the current
5980 article."
5981   (interactive "P")
5982   (setq gnus-summary-buffer (current-buffer))
5983   (gnus-set-global-variables)
5984   (let ((article (gnus-summary-article-number))
5985         (article-window (get-buffer-window gnus-article-buffer t))
5986         endp)
5987     ;; If the buffer is empty, we have no article.
5988     (unless article
5989       (error "No article to select"))
5990     (gnus-configure-windows 'article)
5991     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5992         (if (and (eq gnus-summary-goto-unread 'never)
5993                  (not (gnus-summary-last-article-p article)))
5994             (gnus-summary-next-article)
5995           (gnus-summary-next-unread-article))
5996       (if (or (null gnus-current-article)
5997               (null gnus-article-current)
5998               (/= article (cdr gnus-article-current))
5999               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6000           ;; Selected subject is different from current article's.
6001           (gnus-summary-display-article article)
6002         (when article-window
6003           (gnus-eval-in-buffer-window gnus-article-buffer
6004             (setq endp (gnus-article-next-page lines)))
6005           (when endp
6006             (cond (circular
6007                    (gnus-summary-beginning-of-article))
6008                   (lines
6009                    (gnus-message 3 "End of message"))
6010                   ((null lines)
6011                    (if (and (eq gnus-summary-goto-unread 'never)
6012                             (not (gnus-summary-last-article-p article)))
6013                        (gnus-summary-next-article)
6014                      (gnus-summary-next-unread-article))))))))
6015     (gnus-summary-recenter)
6016     (gnus-summary-position-point)))
6017
6018 (defun gnus-summary-prev-page (&optional lines move)
6019   "Show previous page of selected article.
6020 Argument LINES specifies lines to be scrolled down.
6021 If MOVE, move to the previous unread article if point is at
6022 the beginning of the buffer."
6023   (interactive "P")
6024   (let ((article (gnus-summary-article-number))
6025         (article-window (get-buffer-window gnus-article-buffer t))
6026         endp)
6027     (gnus-configure-windows 'article)
6028     (if (or (null gnus-current-article)
6029             (null gnus-article-current)
6030             (/= article (cdr gnus-article-current))
6031             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6032         ;; Selected subject is different from current article's.
6033         (gnus-summary-display-article article)
6034       (gnus-summary-recenter)
6035       (when article-window
6036         (gnus-eval-in-buffer-window gnus-article-buffer
6037           (setq endp (gnus-article-prev-page lines)))
6038         (when (and move endp)
6039           (cond (lines
6040                  (gnus-message 3 "Beginning of message"))
6041                 ((null lines)
6042                  (if (and (eq gnus-summary-goto-unread 'never)
6043                           (not (gnus-summary-first-article-p article)))
6044                      (gnus-summary-prev-article)
6045                    (gnus-summary-prev-unread-article))))))))
6046   (gnus-summary-position-point))
6047
6048 (defun gnus-summary-prev-page-or-article (&optional lines)
6049   "Show previous page of selected article.
6050 Argument LINES specifies lines to be scrolled down.
6051 If at the beginning of the article, go to the next article."
6052   (interactive "P")
6053   (gnus-summary-prev-page lines t))
6054
6055 (defun gnus-summary-scroll-up (lines)
6056   "Scroll up (or down) one line current article.
6057 Argument LINES specifies lines to be scrolled up (or down if negative)."
6058   (interactive "p")
6059   (gnus-configure-windows 'article)
6060   (gnus-summary-show-thread)
6061   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6062     (gnus-eval-in-buffer-window gnus-article-buffer
6063       (cond ((> lines 0)
6064              (when (gnus-article-next-page lines)
6065                (gnus-message 3 "End of message")))
6066             ((< lines 0)
6067              (gnus-article-prev-page (- lines))))))
6068   (gnus-summary-recenter)
6069   (gnus-summary-position-point))
6070
6071 (defun gnus-summary-scroll-down (lines)
6072   "Scroll down (or up) one line current article.
6073 Argument LINES specifies lines to be scrolled down (or up if negative)."
6074   (interactive "p")
6075   (gnus-summary-scroll-up (- lines)))
6076
6077 (defun gnus-summary-next-same-subject ()
6078   "Select next article which has the same subject as current one."
6079   (interactive)
6080   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6081
6082 (defun gnus-summary-prev-same-subject ()
6083   "Select previous article which has the same subject as current one."
6084   (interactive)
6085   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6086
6087 (defun gnus-summary-next-unread-same-subject ()
6088   "Select next unread article which has the same subject as current one."
6089   (interactive)
6090   (gnus-summary-next-article t (gnus-summary-article-subject)))
6091
6092 (defun gnus-summary-prev-unread-same-subject ()
6093   "Select previous unread article which has the same subject as current one."
6094   (interactive)
6095   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6096
6097 (defun gnus-summary-first-unread-article ()
6098   "Select the first unread article.
6099 Return nil if there are no unread articles."
6100   (interactive)
6101   (prog1
6102       (when (gnus-summary-first-subject t)
6103         (gnus-summary-show-thread)
6104         (gnus-summary-first-subject t)
6105         (gnus-summary-display-article (gnus-summary-article-number)))
6106     (gnus-summary-position-point)))
6107
6108 (defun gnus-summary-first-unread-subject ()
6109   "Place the point on the subject line of the first unread article.
6110 Return nil if there are no unread articles."
6111   (interactive)
6112   (prog1
6113       (when (gnus-summary-first-subject t)
6114         (gnus-summary-show-thread)
6115         (gnus-summary-first-subject t))
6116     (gnus-summary-position-point)))
6117
6118 (defun gnus-summary-first-article ()
6119   "Select the first article.
6120 Return nil if there are no articles."
6121   (interactive)
6122   (prog1
6123       (when (gnus-summary-first-subject)
6124         (gnus-summary-show-thread)
6125         (gnus-summary-first-subject)
6126         (gnus-summary-display-article (gnus-summary-article-number)))
6127     (gnus-summary-position-point)))
6128
6129 (defun gnus-summary-best-unread-article ()
6130   "Select the unread article with the highest score."
6131   (interactive)
6132   (let ((best -1000000)
6133         (data gnus-newsgroup-data)
6134         article score)
6135     (while data
6136       (and (gnus-data-unread-p (car data))
6137            (> (setq score
6138                     (gnus-summary-article-score (gnus-data-number (car data))))
6139               best)
6140            (setq best score
6141                  article (gnus-data-number (car data))))
6142       (setq data (cdr data)))
6143     (prog1
6144         (if article
6145             (gnus-summary-goto-article article)
6146           (error "No unread articles"))
6147       (gnus-summary-position-point))))
6148
6149 (defun gnus-summary-last-subject ()
6150   "Go to the last displayed subject line in the group."
6151   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6152     (when article
6153       (gnus-summary-goto-subject article))))
6154
6155 (defun gnus-summary-goto-article (article &optional all-headers force)
6156   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6157 If ALL-HEADERS is non-nil, no header lines are hidden.
6158 If FORCE, go to the article even if it isn't displayed.  If FORCE
6159 is a number, it is the line the article is to be displayed on."
6160   (interactive
6161    (list
6162     (completing-read
6163      "Article number or Message-ID: "
6164      (mapcar (lambda (number) (list (int-to-string number)))
6165              gnus-newsgroup-limit))
6166     current-prefix-arg
6167     t))
6168   (prog1
6169       (if (and (stringp article)
6170                (string-match "@" article))
6171           (gnus-summary-refer-article article)
6172         (when (stringp article)
6173           (setq article (string-to-number article)))
6174         (if (gnus-summary-goto-subject article force)
6175             (gnus-summary-display-article article all-headers)
6176           (gnus-message 4 "Couldn't go to article %s" article) nil))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-goto-last-article ()
6180   "Go to the previously read article."
6181   (interactive)
6182   (prog1
6183       (when gnus-last-article
6184         (gnus-summary-goto-article gnus-last-article nil t))
6185     (gnus-summary-position-point)))
6186
6187 (defun gnus-summary-pop-article (number)
6188   "Pop one article off the history and go to the previous.
6189 NUMBER articles will be popped off."
6190   (interactive "p")
6191   (let (to)
6192     (setq gnus-newsgroup-history
6193           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6194     (if to
6195         (gnus-summary-goto-article (car to) nil t)
6196       (error "Article history empty")))
6197   (gnus-summary-position-point))
6198
6199 ;; Summary commands and functions for limiting the summary buffer.
6200
6201 (defun gnus-summary-limit-to-articles (n)
6202   "Limit the summary buffer to the next N articles.
6203 If not given a prefix, use the process marked articles instead."
6204   (interactive "P")
6205   (prog1
6206       (let ((articles (gnus-summary-work-articles n)))
6207         (setq gnus-newsgroup-processable nil)
6208         (gnus-summary-limit articles))
6209     (gnus-summary-position-point)))
6210
6211 (defun gnus-summary-pop-limit (&optional total)
6212   "Restore the previous limit.
6213 If given a prefix, remove all limits."
6214   (interactive "P")
6215   (when total
6216     (setq gnus-newsgroup-limits
6217           (list (mapcar (lambda (h) (mail-header-number h))
6218                         gnus-newsgroup-headers))))
6219   (unless gnus-newsgroup-limits
6220     (error "No limit to pop"))
6221   (prog1
6222       (gnus-summary-limit nil 'pop)
6223     (gnus-summary-position-point)))
6224
6225 (defun gnus-summary-limit-to-subject (subject &optional header)
6226   "Limit the summary buffer to articles that have subjects that match a regexp."
6227   (interactive "sLimit to subject (regexp): ")
6228   (unless header
6229     (setq header "subject"))
6230   (when (not (equal "" subject))
6231     (prog1
6232         (let ((articles (gnus-summary-find-matching
6233                          (or header "subject") subject 'all)))
6234           (unless articles
6235             (error "Found no matches for \"%s\"" subject))
6236           (gnus-summary-limit articles))
6237       (gnus-summary-position-point))))
6238
6239 (defun gnus-summary-limit-to-author (from)
6240   "Limit the summary buffer to articles that have authors that match a regexp."
6241   (interactive "sLimit to author (regexp): ")
6242   (gnus-summary-limit-to-subject from "from"))
6243
6244 (defun gnus-summary-limit-to-age (age &optional younger-p)
6245   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6246 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6247 articles that are younger than AGE days."
6248   (interactive
6249    (let ((younger current-prefix-arg)
6250          (days-got nil)
6251          days)
6252      (while (not days-got)
6253        (setq days (if younger
6254                       (read-string "Limit to articles within (in days): ")
6255                     (read-string "Limit to articles old than (in days): ")))
6256        (when (> (length days) 0)
6257          (setq days (read days)))
6258        (if (numberp days)
6259            (setq days-got t)
6260          (message "Please enter a number.")
6261          (sleep-for 1)))
6262      (list days younger)))
6263   (prog1
6264       (let ((data gnus-newsgroup-data)
6265             (cutoff (days-to-time age))
6266             articles d date is-younger)
6267         (while (setq d (pop data))
6268           (when (and (vectorp (gnus-data-header d))
6269                      (setq date (mail-header-date (gnus-data-header d))))
6270             (setq is-younger (time-less-p
6271                               (time-since (date-to-time date))
6272                               cutoff))
6273             (when (if younger-p
6274                       is-younger
6275                     (not is-younger))
6276               (push (gnus-data-number d) articles))))
6277         (gnus-summary-limit (nreverse articles)))
6278     (gnus-summary-position-point)))
6279
6280 (defun gnus-summary-limit-to-extra (header regexp)
6281   "Limit the summary buffer to articles that match an 'extra' header."
6282   (interactive
6283    (let ((header
6284           (intern
6285            (gnus-completing-read
6286             (symbol-name (car gnus-extra-headers))      
6287             "Limit extra header:"       
6288             (mapcar (lambda (x) 
6289                       (cons (symbol-name x) x))
6290                     gnus-extra-headers)
6291             nil                 
6292             t))))
6293      (list header
6294            (read-string (format "Limit to header %s (regexp): " header)))))
6295   (when (not (equal "" regexp))
6296     (prog1
6297         (let ((articles (gnus-summary-find-matching
6298                          (cons 'extra header) regexp 'all)))
6299           (unless articles
6300             (error "Found no matches for \"%s\"" regexp))
6301           (gnus-summary-limit articles))
6302       (gnus-summary-position-point))))
6303
6304 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6305 (make-obsolete
6306  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6307
6308 (defun gnus-summary-limit-to-unread (&optional all)
6309   "Limit the summary buffer to articles that are not marked as read.
6310 If ALL is non-nil, limit strictly to unread articles."
6311   (interactive "P")
6312   (if all
6313       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6314     (gnus-summary-limit-to-marks
6315      ;; Concat all the marks that say that an article is read and have
6316      ;; those removed.
6317      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6318            gnus-killed-mark gnus-kill-file-mark
6319            gnus-low-score-mark gnus-expirable-mark
6320            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6321            gnus-duplicate-mark gnus-souped-mark)
6322      'reverse)))
6323
6324 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6325 (make-obsolete 'gnus-summary-delete-marked-with
6326                'gnus-summary-limit-exlude-marks)
6327
6328 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6329   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6330 If REVERSE, limit the summary buffer to articles that are marked
6331 with MARKS.  MARKS can either be a string of marks or a list of marks.
6332 Returns how many articles were removed."
6333   (interactive "sMarks: ")
6334   (gnus-summary-limit-to-marks marks t))
6335
6336 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6337   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6338 If REVERSE (the prefix), limit the summary buffer to articles that are
6339 not marked with MARKS.  MARKS can either be a string of marks or a
6340 list of marks.
6341 Returns how many articles were removed."
6342   (interactive "sMarks: \nP")
6343   (prog1
6344       (let ((data gnus-newsgroup-data)
6345             (marks (if (listp marks) marks
6346                      (append marks nil))) ; Transform to list.
6347             articles)
6348         (while data
6349           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6350                   (memq (gnus-data-mark (car data)) marks))
6351             (push (gnus-data-number (car data)) articles))
6352           (setq data (cdr data)))
6353         (gnus-summary-limit articles))
6354     (gnus-summary-position-point)))
6355
6356 (defun gnus-summary-limit-to-score (&optional score)
6357   "Limit to articles with score at or above SCORE."
6358   (interactive "P")
6359   (setq score (if score
6360                   (prefix-numeric-value score)
6361                 (or gnus-summary-default-score 0)))
6362   (let ((data gnus-newsgroup-data)
6363         articles)
6364     (while data
6365       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6366                 score)
6367         (push (gnus-data-number (car data)) articles))
6368       (setq data (cdr data)))
6369     (prog1
6370         (gnus-summary-limit articles)
6371       (gnus-summary-position-point))))
6372
6373 (defun gnus-summary-limit-include-thread (id)
6374   "Display all the hidden articles that in the current thread."
6375   (interactive (list (mail-header-id (gnus-summary-article-header))))
6376   (let ((articles (gnus-articles-in-thread
6377                    (gnus-id-to-thread (gnus-root-id id)))))
6378     (prog1
6379         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6380       (gnus-summary-position-point))))
6381
6382 (defun gnus-summary-limit-include-dormant ()
6383   "Display all the hidden articles that are marked as dormant.
6384 Note that this command only works on a subset of the articles currently
6385 fetched for this group."
6386   (interactive)
6387   (unless gnus-newsgroup-dormant
6388     (error "There are no dormant articles in this group"))
6389   (prog1
6390       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6391     (gnus-summary-position-point)))
6392
6393 (defun gnus-summary-limit-exclude-dormant ()
6394   "Hide all dormant articles."
6395   (interactive)
6396   (prog1
6397       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6398     (gnus-summary-position-point)))
6399
6400 (defun gnus-summary-limit-exclude-childless-dormant ()
6401   "Hide all dormant articles that have no children."
6402   (interactive)
6403   (let ((data (gnus-data-list t))
6404         articles d children)
6405     ;; Find all articles that are either not dormant or have
6406     ;; children.
6407     (while (setq d (pop data))
6408       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6409                 (and (setq children
6410                            (gnus-article-children (gnus-data-number d)))
6411                      (let (found)
6412                        (while children
6413                          (when (memq (car children) articles)
6414                            (setq children nil
6415                                  found t))
6416                          (pop children))
6417                        found)))
6418         (push (gnus-data-number d) articles)))
6419     ;; Do the limiting.
6420     (prog1
6421         (gnus-summary-limit articles)
6422       (gnus-summary-position-point))))
6423
6424 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6425   "Mark all unread excluded articles as read.
6426 If ALL, mark even excluded ticked and dormants as read."
6427   (interactive "P")
6428   (let ((articles (gnus-sorted-complement
6429                    (sort
6430                     (mapcar (lambda (h) (mail-header-number h))
6431                             gnus-newsgroup-headers)
6432                     '<)
6433                    (sort gnus-newsgroup-limit '<)))
6434         article)
6435     (setq gnus-newsgroup-unreads
6436           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6437     (if all
6438         (setq gnus-newsgroup-dormant nil
6439               gnus-newsgroup-marked nil
6440               gnus-newsgroup-reads
6441               (nconc
6442                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6443                gnus-newsgroup-reads))
6444       (while (setq article (pop articles))
6445         (unless (or (memq article gnus-newsgroup-dormant)
6446                     (memq article gnus-newsgroup-marked))
6447           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6448
6449 (defun gnus-summary-limit (articles &optional pop)
6450   (if pop
6451       ;; We pop the previous limit off the stack and use that.
6452       (setq articles (car gnus-newsgroup-limits)
6453             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6454     ;; We use the new limit, so we push the old limit on the stack.
6455     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6456   ;; Set the limit.
6457   (setq gnus-newsgroup-limit articles)
6458   (let ((total (length gnus-newsgroup-data))
6459         (data (gnus-data-find-list (gnus-summary-article-number)))
6460         (gnus-summary-mark-below nil)   ; Inhibit this.
6461         found)
6462     ;; This will do all the work of generating the new summary buffer
6463     ;; according to the new limit.
6464     (gnus-summary-prepare)
6465     ;; Hide any threads, possibly.
6466     (and gnus-show-threads
6467          gnus-thread-hide-subtree
6468          (gnus-summary-hide-all-threads))
6469     ;; Try to return to the article you were at, or one in the
6470     ;; neighborhood.
6471     (when data
6472       ;; We try to find some article after the current one.
6473       (while data
6474         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6475           (setq data nil
6476                 found t))
6477         (setq data (cdr data))))
6478     (unless found
6479       ;; If there is no data, that means that we were after the last
6480       ;; article.  The same goes when we can't find any articles
6481       ;; after the current one.
6482       (goto-char (point-max))
6483       (gnus-summary-find-prev))
6484     (gnus-set-mode-line 'summary)
6485     ;; We return how many articles were removed from the summary
6486     ;; buffer as a result of the new limit.
6487     (- total (length gnus-newsgroup-data))))
6488
6489 (defsubst gnus-invisible-cut-children (threads)
6490   (let ((num 0))
6491     (while threads
6492       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6493         (incf num))
6494       (pop threads))
6495     (< num 2)))
6496
6497 (defsubst gnus-cut-thread (thread)
6498   "Go forwards in the thread until we find an article that we want to display."
6499   (when (or (eq gnus-fetch-old-headers 'some)
6500             (eq gnus-fetch-old-headers 'invisible)
6501             (eq gnus-build-sparse-threads 'some)
6502             (eq gnus-build-sparse-threads 'more))
6503     ;; Deal with old-fetched headers and sparse threads.
6504     (while (and
6505             thread
6506             (or
6507              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6508              (gnus-summary-article-ancient-p
6509               (mail-header-number (car thread))))
6510             (if (or (<= (length (cdr thread)) 1)
6511                     (eq gnus-fetch-old-headers 'invisible))
6512                 (setq gnus-newsgroup-limit
6513                       (delq (mail-header-number (car thread))
6514                             gnus-newsgroup-limit)
6515                       thread (cadr thread))
6516               (when (gnus-invisible-cut-children (cdr thread))
6517                 (let ((th (cdr thread)))
6518                   (while th
6519                     (if (memq (mail-header-number (caar th))
6520                               gnus-newsgroup-limit)
6521                         (setq thread (car th)
6522                               th nil)
6523                       (setq th (cdr th))))))))))
6524   thread)
6525
6526 (defun gnus-cut-threads (threads)
6527   "Cut off all uninteresting articles from the beginning of threads."
6528   (when (or (eq gnus-fetch-old-headers 'some)
6529             (eq gnus-fetch-old-headers 'invisible)
6530             (eq gnus-build-sparse-threads 'some)
6531             (eq gnus-build-sparse-threads 'more))
6532     (let ((th threads))
6533       (while th
6534         (setcar th (gnus-cut-thread (car th)))
6535         (setq th (cdr th)))))
6536   ;; Remove nixed out threads.
6537   (delq nil threads))
6538
6539 (defun gnus-summary-initial-limit (&optional show-if-empty)
6540   "Figure out what the initial limit is supposed to be on group entry.
6541 This entails weeding out unwanted dormants, low-scored articles,
6542 fetch-old-headers verbiage, and so on."
6543   ;; Most groups have nothing to remove.
6544   (if (or gnus-inhibit-limiting
6545           (and (null gnus-newsgroup-dormant)
6546                (not (eq gnus-fetch-old-headers 'some))
6547                (not (eq gnus-fetch-old-headers 'invisible))
6548                (null gnus-summary-expunge-below)
6549                (not (eq gnus-build-sparse-threads 'some))
6550                (not (eq gnus-build-sparse-threads 'more))
6551                (null gnus-thread-expunge-below)
6552                (not gnus-use-nocem)))
6553       ()                                ; Do nothing.
6554     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6555     (setq gnus-newsgroup-limit nil)
6556     (mapatoms
6557      (lambda (node)
6558        (unless (car (symbol-value node))
6559          ;; These threads have no parents -- they are roots.
6560          (let ((nodes (cdr (symbol-value node)))
6561                thread)
6562            (while nodes
6563              (if (and gnus-thread-expunge-below
6564                       (< (gnus-thread-total-score (car nodes))
6565                          gnus-thread-expunge-below))
6566                  (gnus-expunge-thread (pop nodes))
6567                (setq thread (pop nodes))
6568                (gnus-summary-limit-children thread))))))
6569      gnus-newsgroup-dependencies)
6570     ;; If this limitation resulted in an empty group, we might
6571     ;; pop the previous limit and use it instead.
6572     (when (and (not gnus-newsgroup-limit)
6573                show-if-empty)
6574       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6575     gnus-newsgroup-limit))
6576
6577 (defun gnus-summary-limit-children (thread)
6578   "Return 1 if this subthread is visible and 0 if it is not."
6579   ;; First we get the number of visible children to this thread.  This
6580   ;; is done by recursing down the thread using this function, so this
6581   ;; will really go down to a leaf article first, before slowly
6582   ;; working its way up towards the root.
6583   (when thread
6584     (let ((children
6585            (if (cdr thread)
6586                (apply '+ (mapcar 'gnus-summary-limit-children
6587                                  (cdr thread)))
6588              0))
6589           (number (mail-header-number (car thread)))
6590           score)
6591       (if (and
6592            (not (memq number gnus-newsgroup-marked))
6593            (or
6594             ;; If this article is dormant and has absolutely no visible
6595             ;; children, then this article isn't visible.
6596             (and (memq number gnus-newsgroup-dormant)
6597                  (zerop children))
6598             ;; If this is "fetch-old-headered" and there is no
6599             ;; visible children, then we don't want this article.
6600             (and (eq gnus-fetch-old-headers 'some)
6601                  (gnus-summary-article-ancient-p number)
6602                  (zerop children))
6603             ;; If this is "fetch-old-headered" and `invisible', then
6604             ;; we don't want this article.
6605             (and (eq gnus-fetch-old-headers 'invisible)
6606                  (gnus-summary-article-ancient-p number))
6607             ;; If this is a sparsely inserted article with no children,
6608             ;; we don't want it.
6609             (and (eq gnus-build-sparse-threads 'some)
6610                  (gnus-summary-article-sparse-p number)
6611                  (zerop children))
6612             ;; If we use expunging, and this article is really
6613             ;; low-scored, then we don't want this article.
6614             (when (and gnus-summary-expunge-below
6615                        (< (setq score
6616                                 (or (cdr (assq number gnus-newsgroup-scored))
6617                                     gnus-summary-default-score))
6618                           gnus-summary-expunge-below))
6619               ;; We increase the expunge-tally here, but that has
6620               ;; nothing to do with the limits, really.
6621               (incf gnus-newsgroup-expunged-tally)
6622               ;; We also mark as read here, if that's wanted.
6623               (when (and gnus-summary-mark-below
6624                          (< score gnus-summary-mark-below))
6625                 (setq gnus-newsgroup-unreads
6626                       (delq number gnus-newsgroup-unreads))
6627                 (if gnus-newsgroup-auto-expire
6628                     (push number gnus-newsgroup-expirable)
6629                   (push (cons number gnus-low-score-mark)
6630                         gnus-newsgroup-reads)))
6631               t)
6632             ;; Check NoCeM things.
6633             (if (and gnus-use-nocem
6634                      (gnus-nocem-unwanted-article-p
6635                       (mail-header-id (car thread))))
6636                 (progn
6637                   (setq gnus-newsgroup-unreads
6638                         (delq number gnus-newsgroup-unreads))
6639                   t))))
6640           ;; Nope, invisible article.
6641           0
6642         ;; Ok, this article is to be visible, so we add it to the limit
6643         ;; and return 1.
6644         (push number gnus-newsgroup-limit)
6645         1))))
6646
6647 (defun gnus-expunge-thread (thread)
6648   "Mark all articles in THREAD as read."
6649   (let* ((number (mail-header-number (car thread))))
6650     (incf gnus-newsgroup-expunged-tally)
6651     ;; We also mark as read here, if that's wanted.
6652     (setq gnus-newsgroup-unreads
6653           (delq number gnus-newsgroup-unreads))
6654     (if gnus-newsgroup-auto-expire
6655         (push number gnus-newsgroup-expirable)
6656       (push (cons number gnus-low-score-mark)
6657             gnus-newsgroup-reads)))
6658   ;; Go recursively through all subthreads.
6659   (mapcar 'gnus-expunge-thread (cdr thread)))
6660
6661 ;; Summary article oriented commands
6662
6663 (defun gnus-summary-refer-parent-article (n)
6664   "Refer parent article N times.
6665 If N is negative, go to ancestor -N instead.
6666 The difference between N and the number of articles fetched is returned."
6667   (interactive "p")
6668   (let ((skip 1)
6669         error header ref)
6670     (when (not (natnump n))
6671       (setq skip (abs n)
6672             n 1))
6673     (while (and (> n 0)
6674                 (not error))
6675       (setq header (gnus-summary-article-header))
6676       (if (and (eq (mail-header-number header)
6677                    (cdr gnus-article-current))
6678                (equal gnus-newsgroup-name
6679                       (car gnus-article-current)))
6680           ;; If we try to find the parent of the currently
6681           ;; displayed article, then we take a look at the actual
6682           ;; References header, since this is slightly more
6683           ;; reliable than the References field we got from the
6684           ;; server.
6685           (save-excursion
6686             (set-buffer gnus-original-article-buffer)
6687             (nnheader-narrow-to-headers)
6688             (unless (setq ref (message-fetch-field "references"))
6689               (setq ref (message-fetch-field "in-reply-to")))
6690             (widen))
6691         (setq ref
6692               ;; It's not the current article, so we take a bet on
6693               ;; the value we got from the server.
6694               (mail-header-references header)))
6695       (if (and ref
6696                (not (equal ref "")))
6697           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6698             (gnus-message 1 "Couldn't find parent"))
6699         (gnus-message 1 "No references in article %d"
6700                       (gnus-summary-article-number))
6701         (setq error t))
6702       (decf n))
6703     (gnus-summary-position-point)
6704     n))
6705
6706 (defun gnus-summary-refer-references ()
6707   "Fetch all articles mentioned in the References header.
6708 Return the number of articles fetched."
6709   (interactive)
6710   (let ((ref (mail-header-references (gnus-summary-article-header)))
6711         (current (gnus-summary-article-number))
6712         (n 0))
6713     (if (or (not ref)
6714             (equal ref ""))
6715         (error "No References in the current article")
6716       ;; For each Message-ID in the References header...
6717       (while (string-match "<[^>]*>" ref)
6718         (incf n)
6719         ;; ... fetch that article.
6720         (gnus-summary-refer-article
6721          (prog1 (match-string 0 ref)
6722            (setq ref (substring ref (match-end 0))))))
6723       (gnus-summary-goto-subject current)
6724       (gnus-summary-position-point)
6725       n)))
6726
6727 (defun gnus-summary-refer-thread (&optional limit)
6728   "Fetch all articles in the current thread.
6729 If LIMIT (the numerical prefix), fetch that many old headers instead
6730 of what's specified by the `gnus-refer-thread-limit' variable."
6731   (interactive "P")
6732   (let ((id (mail-header-id (gnus-summary-article-header)))
6733         (limit (if limit (prefix-numeric-value limit)
6734                  gnus-refer-thread-limit)))
6735     ;; We want to fetch LIMIT *old* headers, but we also have to
6736     ;; re-fetch all the headers in the current buffer, because many of
6737     ;; them may be undisplayed.  So we adjust LIMIT.
6738     (when (numberp limit)
6739       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6740     (unless (eq gnus-fetch-old-headers 'invisible)
6741       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6742       ;; Retrieve the headers and read them in.
6743       (if (eq (gnus-retrieve-headers
6744                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6745               'nov)
6746           (gnus-build-all-threads)
6747         (error "Can't fetch thread from backends that don't support NOV"))
6748       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6749     (gnus-summary-limit-include-thread id)))
6750
6751 (defun gnus-summary-refer-article (message-id &optional arg)
6752   "Fetch an article specified by MESSAGE-ID.
6753 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6754 or `gnus-select-method', no matter what backend the article comes from."
6755   (interactive "sMessage-ID: \nP")
6756   (when (and (stringp message-id)
6757              (not (zerop (length message-id))))
6758     ;; Construct the correct Message-ID if necessary.
6759     ;; Suggested by tale@pawl.rpi.edu.
6760     (unless (string-match "^<" message-id)
6761       (setq message-id (concat "<" message-id)))
6762     (unless (string-match ">$" message-id)
6763       (setq message-id (concat message-id ">")))
6764     (let* ((header (gnus-id-to-header message-id))
6765            (sparse (and header
6766                         (gnus-summary-article-sparse-p
6767                          (mail-header-number header))
6768                         (memq (mail-header-number header)
6769                               gnus-newsgroup-limit))))
6770       (cond
6771        ;; If the article is present in the buffer we just go to it.
6772        ((and header
6773              (or (not (gnus-summary-article-sparse-p
6774                        (mail-header-number header)))
6775                  sparse))
6776         (prog1
6777             (gnus-summary-goto-article
6778              (mail-header-number header) nil t)
6779           (when sparse
6780             (gnus-summary-update-article (mail-header-number header)))))
6781        (t
6782         ;; We fetch the article
6783         (let ((gnus-override-method
6784                (cond ((gnus-news-group-p gnus-newsgroup-name)
6785                       gnus-refer-article-method)
6786                      (arg
6787                       (or gnus-refer-article-method gnus-select-method))
6788                      (t nil)))
6789               number)
6790           ;; Start the special refer-article method, if necessary.
6791           (when (and gnus-refer-article-method
6792                      (gnus-news-group-p gnus-newsgroup-name))
6793             (gnus-check-server gnus-refer-article-method))
6794           ;; Fetch the header, and display the article.
6795           (if (setq number (gnus-summary-insert-subject message-id))
6796               (gnus-summary-select-article nil nil nil number)
6797             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6798
6799 (defun gnus-summary-edit-parameters ()
6800   "Edit the group parameters of the current group."
6801   (interactive)
6802   (gnus-group-edit-group gnus-newsgroup-name 'params))
6803
6804 (defun gnus-summary-customize-parameters ()
6805   "Customize the group parameters of the current group."
6806   (interactive)
6807   (gnus-group-customize gnus-newsgroup-name))
6808
6809 (defun gnus-summary-enter-digest-group (&optional force)
6810   "Enter an nndoc group based on the current article.
6811 If FORCE, force a digest interpretation.  If not, try
6812 to guess what the document format is."
6813   (interactive "P")
6814   (let ((conf gnus-current-window-configuration))
6815     (save-excursion
6816       (gnus-summary-select-article))
6817     (setq gnus-current-window-configuration conf)
6818     (let* ((name (format "%s-%d"
6819                          (gnus-group-prefixed-name
6820                           gnus-newsgroup-name (list 'nndoc ""))
6821                          (save-excursion
6822                            (set-buffer gnus-summary-buffer)
6823                            gnus-current-article)))
6824            (ogroup gnus-newsgroup-name)
6825            (params (append (gnus-info-params (gnus-get-info ogroup))
6826                            (list (cons 'to-group ogroup))
6827                            (list (cons 'save-article-group ogroup))))
6828            (case-fold-search t)
6829            (buf (current-buffer))
6830            dig)
6831       (save-excursion
6832         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6833         (insert-buffer-substring gnus-original-article-buffer)
6834         ;; Remove lines that may lead nndoc to misinterpret the
6835         ;; document type.
6836         (narrow-to-region
6837          (goto-char (point-min))
6838          (or (search-forward "\n\n" nil t) (point)))
6839         (goto-char (point-min))
6840         (delete-matching-lines "^Path:\\|^From ")
6841         (widen))
6842       (unwind-protect
6843           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6844                     (gnus-newsgroup-ephemeral-ignored-charsets
6845                      gnus-newsgroup-ignored-charsets))
6846                 (gnus-group-read-ephemeral-group
6847                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6848                               (nndoc-article-type
6849                                ,(if force 'digest 'guess))) t))
6850               ;; Make all postings to this group go to the parent group.
6851               (nconc (gnus-info-params (gnus-get-info name))
6852                      params)
6853             ;; Couldn't select this doc group.
6854             (switch-to-buffer buf)
6855             (gnus-set-global-variables)
6856             (gnus-configure-windows 'summary)
6857             (gnus-message 3 "Article couldn't be entered?"))
6858         (kill-buffer dig)))))
6859
6860 (defun gnus-summary-read-document (n)
6861   "Open a new group based on the current article(s).
6862 This will allow you to read digests and other similar
6863 documents as newsgroups.
6864 Obeys the standard process/prefix convention."
6865   (interactive "P")
6866   (let* ((articles (gnus-summary-work-articles n))
6867          (ogroup gnus-newsgroup-name)
6868          (params (append (gnus-info-params (gnus-get-info ogroup))
6869                          (list (cons 'to-group ogroup))))
6870          article group egroup groups vgroup)
6871     (while (setq article (pop articles))
6872       (setq group (format "%s-%d" gnus-newsgroup-name article))
6873       (gnus-summary-remove-process-mark article)
6874       (when (gnus-summary-display-article article)
6875         (save-excursion
6876           (with-temp-buffer
6877             (insert-buffer-substring gnus-original-article-buffer)
6878             ;; Remove some headers that may lead nndoc to make
6879             ;; the wrong guess.
6880             (message-narrow-to-head)
6881             (goto-char (point-min))
6882             (delete-matching-lines "^\\(Path\\):\\|^From ")
6883             (widen)
6884             (if (setq egroup
6885                       (gnus-group-read-ephemeral-group
6886                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6887                                      (nndoc-article-type guess))
6888                        t nil t))
6889                 (progn
6890                   ;; Make all postings to this group go to the parent group.
6891                   (nconc (gnus-info-params (gnus-get-info egroup))
6892                          params)
6893                   (push egroup groups))
6894               ;; Couldn't select this doc group.
6895               (gnus-error 3 "Article couldn't be entered"))))))
6896     ;; Now we have selected all the documents.
6897     (cond
6898      ((not groups)
6899       (error "None of the articles could be interpreted as documents"))
6900      ((gnus-group-read-ephemeral-group
6901        (setq vgroup (format
6902                      "nnvirtual:%s-%s" gnus-newsgroup-name
6903                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6904        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6905        t
6906        (cons (current-buffer) 'summary)))
6907      (t
6908       (error "Couldn't select virtual nndoc group")))))
6909
6910 (defun gnus-summary-isearch-article (&optional regexp-p)
6911   "Do incremental search forward on the current article.
6912 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6913   (interactive "P")
6914   (let* ((gnus-inhibit-treatment t)
6915          (old (gnus-summary-select-article)))
6916     (gnus-configure-windows 'article)
6917     (gnus-eval-in-buffer-window gnus-article-buffer
6918       (set (make-local-variable 'isearch-lazy-highlight) t)
6919       (save-restriction
6920         (widen)
6921         (when (eq 'old old)
6922           (gnus-article-show-all-headers))
6923         (goto-char (point-min))
6924         (isearch-forward regexp-p)))))
6925
6926 (defun gnus-summary-search-article-forward (regexp &optional backward)
6927   "Search for an article containing REGEXP forward.
6928 If BACKWARD, search backward instead."
6929   (interactive
6930    (list (read-string
6931           (format "Search article %s (regexp%s): "
6932                   (if current-prefix-arg "backward" "forward")
6933                   (if gnus-last-search-regexp
6934                       (concat ", default " gnus-last-search-regexp)
6935                     "")))
6936          current-prefix-arg))
6937   (if (string-equal regexp "")
6938       (setq regexp (or gnus-last-search-regexp ""))
6939     (setq gnus-last-search-regexp regexp))
6940   (if (gnus-summary-search-article regexp backward)
6941       (gnus-summary-show-thread)
6942     (error "Search failed: \"%s\"" regexp)))
6943
6944 (defun gnus-summary-search-article-backward (regexp)
6945   "Search for an article containing REGEXP backward."
6946   (interactive
6947    (list (read-string
6948           (format "Search article backward (regexp%s): "
6949                   (if gnus-last-search-regexp
6950                       (concat ", default " gnus-last-search-regexp)
6951                     "")))))
6952   (gnus-summary-search-article-forward regexp 'backward))
6953
6954 (defvar gnus-summary-search-article-matched-data)
6955 (eval-when-compile
6956   (defmacro gnus-summary-search-article-position-point (regexp backward)
6957     "Dehighlight the last matched text and goto the beginning position."
6958     (` (if (and gnus-summary-search-article-matched-data
6959                 (let ((text (caddr gnus-summary-search-article-matched-data))
6960                       (inhibit-read-only t)
6961                       buffer-read-only)
6962                   (delete-region
6963                    (goto-char (car gnus-summary-search-article-matched-data))
6964                    (cadr gnus-summary-search-article-matched-data))
6965                   (insert text)
6966                   (string-match (, regexp) text)))
6967            (if (, backward) (beginning-of-line) (end-of-line))
6968          (goto-char (if (, backward) (point-max) (point-min))))))
6969
6970   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
6971     "Place point where X-Face image is displayed."
6972     (if (featurep 'xemacs)
6973         (` (let ((end (if (search-forward "\n\n" nil t)
6974                           (goto-char (1- (point)))
6975                         (point-min)))
6976                  extent)
6977              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
6978              (unless (and (re-search-forward "^From:" end t)
6979                           (setq extent (extent-at (point)))
6980                           (extent-begin-glyph extent))
6981                (goto-char (, opoint)))))
6982       (` (let ((end (if (search-forward "\n\n" nil t)
6983                         (goto-char (1- (point)))
6984                       (point-min))))
6985            (goto-char
6986             (or (text-property-any (or (search-backward "\n\n" nil t)
6987                                        (point-min))
6988                                    end 'x-face-mule-bitmap-image t)
6989                 (, opoint)))))))
6990
6991   (defmacro gnus-summary-search-article-highlight-matched-text
6992     (backward treated x-face)
6993     "Highlight matched text in the function `gnus-summary-search-article'."
6994     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
6995              (end (set-marker (make-marker) (match-end 0)))
6996              (inhibit-read-only t)
6997              buffer-read-only)
6998          (unless treated
6999            (let ((,@
7000                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7001                     (mapcar
7002                      (lambda (item) (setq items (delq item items)))
7003                      '(gnus-treat-buttonize
7004                        gnus-treat-fill-article
7005                        gnus-treat-fill-long-lines
7006                        gnus-treat-emphasize
7007                        gnus-treat-highlight-headers
7008                        gnus-treat-highlight-citation
7009                        gnus-treat-highlight-signature
7010                        gnus-treat-overstrike
7011                        gnus-treat-display-xface
7012                        gnus-treat-buttonize-head
7013                        gnus-treat-decode-article-as-default-mime-charset))
7014                     items))
7015                  (gnus-treat-display-xface
7016                   (when (, x-face) gnus-treat-display-xface)))
7017              (gnus-article-prepare-mime-display)))
7018          (goto-char (if (, backward) start end))
7019          (when (, x-face)
7020            (gnus-summary-search-article-highlight-goto-x-face (point)))
7021          (setq gnus-summary-search-article-matched-data
7022                (list start end (buffer-substring start end)))
7023          (unless (eq start end);; matched text has been deleted. :-<
7024            (put-text-property start end 'face
7025                               (or (find-face 'isearch)
7026                                   'secondary-selection))))))
7027   )
7028
7029 (defun gnus-summary-search-article (regexp &optional backward)
7030   "Search for an article containing REGEXP.
7031 Optional argument BACKWARD means do search for backward.
7032 `gnus-select-article-hook' is not called during the search."
7033   ;; We have to require this here to make sure that the following
7034   ;; dynamic binding isn't shadowed by autoloading.
7035   (require 'gnus-async)
7036   (require 'gnus-art)
7037   (let ((gnus-select-article-hook nil)  ;Disable hook.
7038         (gnus-article-display-hook nil)
7039         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7040         (gnus-use-article-prefetch nil)
7041         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7042         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7043         (sum (current-buffer))
7044         (found nil)
7045         point treated)
7046     (gnus-save-hidden-threads
7047       (let ((gnus-inhibit-treatment t))
7048         (setq treated (eq 'old (gnus-summary-select-article))))
7049       (unless (and (gnus-buffer-live-p gnus-article-buffer)
7050                    (window-live-p (get-buffer-window gnus-article-buffer
7051                                                      t)))
7052         (setq treated (eq 'old (gnus-summary-select-article nil t))))
7053       (set-buffer gnus-article-buffer)
7054       (widen)
7055       (if treated
7056           (progn
7057             (gnus-article-show-all-headers)
7058             (gnus-summary-search-article-position-point regexp backward))
7059         (goto-char (if backward (point-max) (point-min))))
7060       (while (not found)
7061         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7062         (if (if backward
7063                 (re-search-backward regexp nil t)
7064               (re-search-forward regexp nil t))
7065             ;; We found the regexp.
7066             (progn
7067               (gnus-summary-search-article-highlight-matched-text
7068                backward treated (string-match "^\\^X-Face:" regexp))
7069               (setq found 'found)
7070               (forward-line
7071                (/ (- 2 (window-height
7072                         (get-buffer-window gnus-article-buffer t)))
7073                   2))
7074               (set-window-start
7075                (get-buffer-window (current-buffer))
7076                (point))
7077               (set-buffer sum)
7078               (setq point (point)))
7079           ;; We didn't find it, so we go to the next article.
7080           (set-buffer sum)
7081           (setq found 'not)
7082           (while (eq found 'not)
7083             (if (not (if backward (gnus-summary-find-prev)
7084                        (gnus-summary-find-next)))
7085                 ;; No more articles.
7086                 (setq found t)
7087               ;; Select the next article and adjust point.
7088               (unless (gnus-summary-article-sparse-p
7089                        (gnus-summary-article-number))
7090                 (setq found nil)
7091                 (let ((gnus-inhibit-treatment t))
7092                   (gnus-summary-select-article))
7093                 (setq treated nil)
7094                 (set-buffer gnus-article-buffer)
7095                 (widen)
7096                 (goto-char (if backward (point-max) (point-min))))))))
7097       (gnus-message 7 ""))
7098     ;; Return whether we found the regexp.
7099     (when (eq found 'found)
7100       (goto-char point)
7101       (gnus-summary-show-thread)
7102       (gnus-summary-goto-subject gnus-current-article)
7103       (gnus-summary-position-point)
7104       t)))
7105
7106 (defun gnus-summary-find-matching (header regexp &optional backward unread
7107                                           not-case-fold)
7108   "Return a list of all articles that match REGEXP on HEADER.
7109 The search stars on the current article and goes forwards unless
7110 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7111 If UNREAD is non-nil, only unread articles will
7112 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7113 in the comparisons."
7114   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7115                 (gnus-data-find-list
7116                  (gnus-summary-article-number) (gnus-data-list backward))))
7117         (case-fold-search (not not-case-fold))
7118         articles d func)
7119     (if (consp header)
7120         (if (eq (car header) 'extra)
7121             (setq func
7122                   `(lambda (h)
7123                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7124                          "")))
7125           (error "%s is an invalid header" header))
7126       (unless (fboundp (intern (concat "mail-header-" header)))
7127         (error "%s is not a valid header" header))
7128       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7129     (while data
7130       (setq d (car data))
7131       (and (or (not unread)             ; We want all articles...
7132                (gnus-data-unread-p d))  ; Or just unreads.
7133            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7134            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7135            (push (gnus-data-number d) articles)) ; Success!
7136       (setq data (cdr data)))
7137     (nreverse articles)))
7138
7139 (defun gnus-summary-execute-command (header regexp command &optional backward)
7140   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7141 If HEADER is an empty string (or nil), the match is done on the entire
7142 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7143   (interactive
7144    (list (let ((completion-ignore-case t))
7145            (completing-read
7146             "Header name: "
7147             (mapcar (lambda (string) (list string))
7148                     '("Number" "Subject" "From" "Lines" "Date"
7149                       "Message-ID" "Xref" "References" "Body"))
7150             nil 'require-match))
7151          (read-string "Regexp: ")
7152          (read-key-sequence "Command: ")
7153          current-prefix-arg))
7154   (when (equal header "Body")
7155     (setq header ""))
7156   ;; Hidden thread subtrees must be searched as well.
7157   (gnus-summary-show-all-threads)
7158   ;; We don't want to change current point nor window configuration.
7159   (save-excursion
7160     (save-window-excursion
7161       (gnus-message 6 "Executing %s..." (key-description command))
7162       ;; We'd like to execute COMMAND interactively so as to give arguments.
7163       (gnus-execute header regexp
7164                     `(call-interactively ',(key-binding command))
7165                     backward)
7166       (gnus-message 6 "Executing %s...done" (key-description command)))))
7167
7168 (defun gnus-summary-beginning-of-article ()
7169   "Scroll the article back to the beginning."
7170   (interactive)
7171   (gnus-summary-select-article)
7172   (gnus-configure-windows 'article)
7173   (gnus-eval-in-buffer-window gnus-article-buffer
7174     (widen)
7175     (goto-char (point-min))
7176     (when gnus-page-broken
7177       (gnus-narrow-to-page))))
7178
7179 (defun gnus-summary-end-of-article ()
7180   "Scroll to the end of the article."
7181   (interactive)
7182   (gnus-summary-select-article)
7183   (gnus-configure-windows 'article)
7184   (gnus-eval-in-buffer-window gnus-article-buffer
7185     (widen)
7186     (goto-char (point-max))
7187     (recenter -3)
7188     (when gnus-page-broken
7189       (gnus-narrow-to-page))))
7190
7191 (defun gnus-summary-print-article (&optional filename n)
7192   "Generate and print a PostScript image of the N next (mail) articles.
7193
7194 If N is negative, print the N previous articles.  If N is nil and articles
7195 have been marked with the process mark, print these instead.
7196
7197 If the optional first argument FILENAME is nil, send the image to the
7198 printer.  If FILENAME is a string, save the PostScript image in a file with
7199 that name.  If FILENAME is a number, prompt the user for the name of the file
7200 to save in."
7201   (interactive (list (ps-print-preprint current-prefix-arg)
7202                      current-prefix-arg))
7203   (dolist (article (gnus-summary-work-articles n))
7204     (gnus-summary-select-article nil nil 'pseudo article)
7205     (gnus-eval-in-buffer-window gnus-article-buffer
7206       (let ((buffer (generate-new-buffer " *print*")))
7207         (unwind-protect
7208             (progn
7209               (copy-to-buffer buffer (point-min) (point-max))
7210               (set-buffer buffer)
7211               (gnus-article-delete-invisible-text)
7212               (let ((ps-left-header
7213                      (list
7214                       (concat "("
7215                               (mail-header-subject gnus-current-headers) ")")
7216                       (concat "("
7217                               (mail-header-from gnus-current-headers) ")")))
7218                     (ps-right-header
7219                      (list
7220                       "/pagenumberstring load"
7221                       (concat "("
7222                               (mail-header-date gnus-current-headers) ")"))))
7223                 (gnus-run-hooks 'gnus-ps-print-hook)
7224                 (save-excursion
7225                   (ps-print-buffer-with-faces filename))))
7226           (kill-buffer buffer))))))
7227
7228 (defun gnus-summary-show-article (&optional arg)
7229   "Force re-fetching of the current article.
7230 If ARG (the prefix) is non-nil, show the raw article without any
7231 article massaging functions being run."
7232   (interactive "P")
7233   (if (not arg)
7234       ;; Select the article the normal way.
7235       (gnus-summary-select-article nil 'force)
7236     ;; We have to require this here to make sure that the following
7237     ;; dynamic binding isn't shadowed by autoloading.
7238     (require 'gnus-async)
7239     (require 'gnus-art)
7240     ;; Bind the article treatment functions to nil.
7241     (let ((gnus-have-all-headers t)
7242           gnus-article-display-hook
7243           gnus-article-prepare-hook
7244           gnus-article-decode-hook
7245           gnus-break-pages
7246           gnus-show-mime
7247           gnus-visual)
7248       (gnus-summary-select-article nil 'force)))
7249   (gnus-summary-goto-subject gnus-current-article)
7250   (gnus-summary-position-point))
7251
7252 (defun gnus-summary-verbose-headers (&optional arg)
7253   "Toggle permanent full header display.
7254 If ARG is a positive number, turn header display on.
7255 If ARG is a negative number, turn header display off."
7256   (interactive "P")
7257   (setq gnus-show-all-headers
7258         (cond ((or (not (numberp arg))
7259                    (zerop arg))
7260                (not gnus-show-all-headers))
7261               ((natnump arg)
7262                t)))
7263   (gnus-summary-show-article))
7264
7265 (defun gnus-summary-toggle-header (&optional arg)
7266   "Show the headers if they are hidden, or hide them if they are shown.
7267 If ARG is a positive number, show the entire header.
7268 If ARG is a negative number, hide the unwanted header lines."
7269   (interactive "P")
7270   (save-excursion
7271     (set-buffer gnus-article-buffer)
7272     (save-restriction
7273       (let* ((buffer-read-only nil)
7274              (inhibit-point-motion-hooks t)
7275              hidden e)
7276         (save-restriction 
7277           (article-narrow-to-head)
7278           (setq hidden (gnus-article-hidden-text-p 'headers)))
7279         (goto-char (point-min))
7280         (when (search-forward "\n\n" nil t)
7281           (delete-region (point-min) (1- (point))))
7282         (goto-char (point-min))
7283         (save-excursion
7284           (set-buffer gnus-original-article-buffer)
7285           (goto-char (point-min))
7286           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7287         (insert-buffer-substring gnus-original-article-buffer 1 e)
7288         (save-restriction
7289           (narrow-to-region (point-min) (point))
7290           (article-decode-encoded-words)
7291           (if (or hidden
7292                   (and (numberp arg) (< arg 0)))
7293               (let ((gnus-treat-hide-headers nil)
7294                     (gnus-treat-hide-boring-headers nil))
7295                 (gnus-treat-article 'head))
7296             (gnus-treat-article 'head)))))))
7297
7298 (defun gnus-summary-show-all-headers ()
7299   "Make all header lines visible."
7300   (interactive)
7301   (gnus-article-show-all-headers))
7302
7303 (defun gnus-summary-toggle-mime (&optional arg)
7304   "Toggle MIME processing.
7305 If ARG is a positive number, turn MIME processing on."
7306   (interactive "P")
7307   (setq gnus-show-mime
7308         (if (null arg)
7309             (not gnus-show-mime)
7310           (> (prefix-numeric-value arg) 0)))
7311   (gnus-summary-select-article t 'force))
7312
7313 (defun gnus-summary-caesar-message (&optional arg)
7314   "Caesar rotate the current article by 13.
7315 The numerical prefix specifies how many places to rotate each letter
7316 forward."
7317   (interactive "P")
7318   (gnus-summary-select-article)
7319   (let ((mail-header-separator ""))
7320     (gnus-eval-in-buffer-window gnus-article-buffer
7321       (save-restriction
7322         (widen)
7323         (let ((start (window-start))
7324               buffer-read-only)
7325           (message-caesar-buffer-body arg)
7326           (set-window-start (get-buffer-window (current-buffer)) start))))))
7327
7328 (defun gnus-summary-stop-page-breaking ()
7329   "Stop page breaking in the current article."
7330   (interactive)
7331   (gnus-summary-select-article)
7332   (gnus-eval-in-buffer-window gnus-article-buffer
7333     (widen)
7334     (when (gnus-visual-p 'page-marker)
7335       (let ((buffer-read-only nil))
7336         (gnus-remove-text-with-property 'gnus-prev)
7337         (gnus-remove-text-with-property 'gnus-next))
7338       (setq gnus-page-broken nil))))
7339
7340 (defun gnus-summary-move-article (&optional n to-newsgroup
7341                                             select-method action)
7342   "Move the current article to a different newsgroup.
7343 If N is a positive number, move the N next articles.
7344 If N is a negative number, move the N previous articles.
7345 If N is nil and any articles have been marked with the process mark,
7346 move those articles instead.
7347 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7348 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7349 re-spool using this method.
7350
7351 For this function to work, both the current newsgroup and the
7352 newsgroup that you want to move to have to support the `request-move'
7353 and `request-accept' functions."
7354   (interactive "P")
7355   (unless action
7356     (setq action 'move))
7357   ;; Disable marking as read.
7358   (let (gnus-mark-article-hook)
7359     (save-window-excursion
7360       (gnus-summary-select-article)))
7361   ;; Check whether the source group supports the required functions.
7362   (cond ((and (eq action 'move)
7363               (not (gnus-check-backend-function
7364                     'request-move-article gnus-newsgroup-name)))
7365          (error "The current group does not support article moving"))
7366         ((and (eq action 'crosspost)
7367               (not (gnus-check-backend-function
7368                     'request-replace-article gnus-newsgroup-name)))
7369          (error "The current group does not support article editing")))
7370   (let ((articles (gnus-summary-work-articles n))
7371         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7372         (names '((move "Move" "Moving")
7373                  (copy "Copy" "Copying")
7374                  (crosspost "Crosspost" "Crossposting")))
7375         (copy-buf (save-excursion
7376                     (nnheader-set-temp-buffer " *copy article*")))
7377         (default-marks gnus-article-mark-lists)
7378         (no-expire-marks (delete '(expirable . expire)
7379                                  (copy-sequence gnus-article-mark-lists)))
7380         art-group to-method new-xref article to-groups)
7381     (unless (assq action names)
7382       (error "Unknown action %s" action))
7383     ;; Read the newsgroup name.
7384     (when (and (not to-newsgroup)
7385                (not select-method))
7386       (setq to-newsgroup
7387             (gnus-read-move-group-name
7388              (cadr (assq action names))
7389              (symbol-value (intern (format "gnus-current-%s-group" action)))
7390              articles prefix))
7391       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7392     (setq to-method (or select-method
7393                         (gnus-group-name-to-method to-newsgroup)))
7394     ;; Check the method we are to move this article to...
7395     (unless (gnus-check-backend-function
7396              'request-accept-article (car to-method))
7397       (error "%s does not support article copying" (car to-method)))
7398     (unless (gnus-check-server to-method)
7399       (error "Can't open server %s" (car to-method)))
7400     (gnus-message 6 "%s to %s: %s..."
7401                   (caddr (assq action names))
7402                   (or (car select-method) to-newsgroup) articles)
7403     (while articles
7404       (setq article (pop articles))
7405       (setq
7406        art-group
7407        (cond
7408         ;; Move the article.
7409         ((eq action 'move)
7410          ;; Remove this article from future suppression.
7411          (gnus-dup-unsuppress-article article)
7412          (gnus-request-move-article
7413           article                       ; Article to move
7414           gnus-newsgroup-name           ; From newsgroup
7415           (nth 1 (gnus-find-method-for-group
7416                   gnus-newsgroup-name)) ; Server
7417           (list 'gnus-request-accept-article
7418                 to-newsgroup (list 'quote select-method)
7419                 (not articles) t)               ; Accept form
7420           (not articles)))              ; Only save nov last time
7421         ;; Copy the article.
7422         ((eq action 'copy)
7423          (save-excursion
7424            (set-buffer copy-buf)
7425            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7426              (gnus-request-accept-article
7427               to-newsgroup select-method (not articles) t))))
7428         ;; Crosspost the article.
7429         ((eq action 'crosspost)
7430          (let ((xref (message-tokenize-header
7431                       (mail-header-xref (gnus-summary-article-header article))
7432                       " ")))
7433            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7434                                   ":" article))
7435            (unless xref
7436              (setq xref (list (system-name))))
7437            (setq new-xref
7438                  (concat
7439                   (mapconcat 'identity
7440                              (delete "Xref:" (delete new-xref xref))
7441                              " ")
7442                   " " new-xref))
7443            (save-excursion
7444              (set-buffer copy-buf)
7445              ;; First put the article in the destination group.
7446              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7447              (when (consp (setq art-group
7448                                 (gnus-request-accept-article
7449                                  to-newsgroup select-method (not articles))))
7450                (setq new-xref (concat new-xref " " (car art-group)
7451                                       ":" (cdr art-group)))
7452                ;; Now we have the new Xrefs header, so we insert
7453                ;; it and replace the new article.
7454                (nnheader-replace-header "Xref" new-xref)
7455                (gnus-request-replace-article
7456                 (cdr art-group) to-newsgroup (current-buffer))
7457                art-group))))))
7458       (cond
7459        ((not art-group)
7460        (gnus-message 1 "Couldn't %s article %s: %s"
7461                      (cadr (assq action names)) article
7462                      (nnheader-get-report (car to-method))))
7463        ((and (eq art-group 'junk)
7464              (eq action 'move))
7465         (gnus-summary-mark-article article gnus-canceled-mark)
7466         (gnus-message 4 "Deleted article %s" article))
7467        (t
7468         (let* ((pto-group (gnus-group-prefixed-name
7469                            (car art-group) to-method))
7470                (entry
7471                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7472                (info (nth 2 entry))
7473                (to-group (gnus-info-group info))
7474                to-marks)
7475           ;; Update the group that has been moved to.
7476           (when (and info
7477                      (memq action '(move copy)))
7478             (unless (member to-group to-groups)
7479               (push to-group to-groups))
7480
7481             (unless (memq article gnus-newsgroup-unreads)
7482               (push 'read to-marks)
7483               (gnus-info-set-read
7484                info (gnus-add-to-range (gnus-info-read info)
7485                                        (list (cdr art-group)))))
7486
7487             ;; Copy any marks over to the new group.
7488             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7489                              default-marks
7490                            no-expire-marks))
7491                   (to-article (cdr art-group)))
7492
7493               ;; See whether the article is to be put in the cache.
7494               (when gnus-use-cache
7495                 (gnus-cache-possibly-enter-article
7496                  to-group to-article
7497                  (let ((header (copy-sequence
7498                                 (gnus-summary-article-header article))))
7499                    (mail-header-set-number header to-article)
7500                    header)
7501                  (memq article gnus-newsgroup-marked)
7502                  (memq article gnus-newsgroup-dormant)
7503                  (memq article gnus-newsgroup-unreads)))
7504
7505               (when (and (equal to-group gnus-newsgroup-name)
7506                          (not (memq article gnus-newsgroup-unreads)))
7507                 ;; Mark this article as read in this group.
7508                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7509                 (setcdr (gnus-active to-group) to-article)
7510                 (setcdr gnus-newsgroup-active to-article))
7511
7512               (while marks
7513                 (when (memq article (symbol-value
7514                                      (intern (format "gnus-newsgroup-%s"
7515                                                      (caar marks)))))
7516                   (push (cdar marks) to-marks)
7517                   ;; If the other group is the same as this group,
7518                   ;; then we have to add the mark to the list.
7519                   (when (equal to-group gnus-newsgroup-name)
7520                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7521                          (cons to-article
7522                                (symbol-value
7523                                 (intern (format "gnus-newsgroup-%s"
7524                                                 (caar marks)))))))
7525                   ;; Copy the marks to other group.
7526                   (gnus-add-marked-articles
7527                    to-group (cdar marks) (list to-article) info))
7528                 (setq marks (cdr marks)))
7529
7530               (gnus-request-set-mark to-group (list (list (list to-article)
7531                                                           'set
7532                                                           to-marks)))
7533
7534               (gnus-dribble-enter
7535                (concat "(gnus-group-set-info '"
7536                        (gnus-prin1-to-string (gnus-get-info to-group))
7537                        ")"))))
7538
7539           ;; Update the Xref header in this article to point to
7540           ;; the new crossposted article we have just created.
7541           (when (eq action 'crosspost)
7542             (save-excursion
7543               (set-buffer copy-buf)
7544               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7545               (nnheader-replace-header "Xref" new-xref)
7546               (gnus-request-replace-article
7547                article gnus-newsgroup-name (current-buffer)))))
7548
7549         ;;;!!!Why is this necessary?
7550         (set-buffer gnus-summary-buffer)
7551
7552         (gnus-summary-goto-subject article)
7553         (when (eq action 'move)
7554           (gnus-summary-mark-article article gnus-canceled-mark))))
7555       (gnus-summary-remove-process-mark article))
7556     ;; Re-activate all groups that have been moved to.
7557     (while to-groups
7558       (save-excursion
7559         (set-buffer gnus-group-buffer)
7560         (when (gnus-group-goto-group (car to-groups) t)
7561           (gnus-group-get-new-news-this-group 1 t))
7562         (pop to-groups)))
7563
7564     (gnus-kill-buffer copy-buf)
7565     (gnus-summary-position-point)
7566     (gnus-set-mode-line 'summary)))
7567
7568 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7569   "Move the current article to a different newsgroup.
7570 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7571 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7572 re-spool using this method."
7573   (interactive "P")
7574   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7575
7576 (defun gnus-summary-crosspost-article (&optional n)
7577   "Crosspost the current article to some other group."
7578   (interactive "P")
7579   (gnus-summary-move-article n nil nil 'crosspost))
7580
7581 (defcustom gnus-summary-respool-default-method nil
7582   "Default method for respooling an article.
7583 If nil, use to the current newsgroup method."
7584   :type '(choice (gnus-select-method :value (nnml ""))
7585                  (const nil))
7586   :group 'gnus-summary-mail)
7587
7588 (defun gnus-summary-respool-article (&optional n method)
7589   "Respool the current article.
7590 The article will be squeezed through the mail spooling process again,
7591 which means that it will be put in some mail newsgroup or other
7592 depending on `nnmail-split-methods'.
7593 If N is a positive number, respool the N next articles.
7594 If N is a negative number, respool the N previous articles.
7595 If N is nil and any articles have been marked with the process mark,
7596 respool those articles instead.
7597
7598 Respooling can be done both from mail groups and \"real\" newsgroups.
7599 In the former case, the articles in question will be moved from the
7600 current group into whatever groups they are destined to.  In the
7601 latter case, they will be copied into the relevant groups."
7602   (interactive
7603    (list current-prefix-arg
7604          (let* ((methods (gnus-methods-using 'respool))
7605                 (methname
7606                  (symbol-name (or gnus-summary-respool-default-method
7607                                   (car (gnus-find-method-for-group
7608                                         gnus-newsgroup-name)))))
7609                 (method
7610                  (gnus-completing-read
7611                   methname "What backend do you want to use when respooling?"
7612                   methods nil t nil 'gnus-mail-method-history))
7613                 ms)
7614            (cond
7615             ((zerop (length (setq ms (gnus-servers-using-backend
7616                                       (intern method)))))
7617              (list (intern method) ""))
7618             ((= 1 (length ms))
7619              (car ms))
7620             (t
7621              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7622                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7623                            ms-alist))))))))
7624   (unless method
7625     (error "No method given for respooling"))
7626   (if (assoc (symbol-name
7627               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7628              (gnus-methods-using 'respool))
7629       (gnus-summary-move-article n nil method)
7630     (gnus-summary-copy-article n nil method)))
7631
7632 (defun gnus-summary-import-article (file)
7633   "Import an arbitrary file into a mail newsgroup."
7634   (interactive "fImport file: ")
7635   (let ((group gnus-newsgroup-name)
7636         (now (current-time))
7637         atts lines)
7638     (unless (gnus-check-backend-function 'request-accept-article group)
7639       (error "%s does not support article importing" group))
7640     (or (file-readable-p file)
7641         (not (file-regular-p file))
7642         (error "Can't read %s" file))
7643     (save-excursion
7644       (set-buffer (gnus-get-buffer-create " *import file*"))
7645       (erase-buffer)
7646       (nnheader-insert-file-contents file)
7647       (goto-char (point-min))
7648       (unless (nnheader-article-p)
7649         ;; This doesn't look like an article, so we fudge some headers.
7650         (setq atts (file-attributes file)
7651               lines (count-lines (point-min) (point-max)))
7652         (insert "From: " (read-string "From: ") "\n"
7653                 "Subject: " (read-string "Subject: ") "\n"
7654                 "Date: " (message-make-date (nth 5 atts))
7655                 "\n"
7656                 "Message-ID: " (message-make-message-id) "\n"
7657                 "Lines: " (int-to-string lines) "\n"
7658                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7659       (gnus-request-accept-article group nil t)
7660       (kill-buffer (current-buffer)))))
7661
7662 (defun gnus-summary-article-posted-p ()
7663   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7664 This will be the case if the article has both been mailed and posted."
7665   (interactive)
7666   (let ((id (mail-header-references (gnus-summary-article-header)))
7667         (gnus-override-method
7668          (or gnus-refer-article-method gnus-select-method)))
7669     (if (gnus-request-head id "")
7670         (gnus-message 2 "The current message was found on %s"
7671                       gnus-override-method)
7672       (gnus-message 2 "The current message couldn't be found on %s"
7673                     gnus-override-method)
7674       nil)))
7675
7676 (defun gnus-summary-expire-articles (&optional now)
7677   "Expire all articles that are marked as expirable in the current group."
7678   (interactive)
7679   (when (gnus-check-backend-function
7680          'request-expire-articles gnus-newsgroup-name)
7681     ;; This backend supports expiry.
7682     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7683            (expirable (if total
7684                           (progn
7685                             ;; We need to update the info for
7686                             ;; this group for `gnus-list-of-read-articles'
7687                             ;; to give us the right answer.
7688                             (gnus-run-hooks 'gnus-exit-group-hook)
7689                             (gnus-summary-update-info)
7690                             (gnus-list-of-read-articles gnus-newsgroup-name))
7691                         (setq gnus-newsgroup-expirable
7692                               (sort gnus-newsgroup-expirable '<))))
7693            (expiry-wait (if now 'immediate
7694                           (gnus-group-find-parameter
7695                            gnus-newsgroup-name 'expiry-wait)))
7696            es)
7697       (when expirable
7698         ;; There are expirable articles in this group, so we run them
7699         ;; through the expiry process.
7700         (gnus-message 6 "Expiring articles...")
7701         (unless (gnus-check-group gnus-newsgroup-name)
7702           (error "Can't open server for %s" gnus-newsgroup-name))
7703         ;; The list of articles that weren't expired is returned.
7704         (save-excursion
7705           (if expiry-wait
7706               (let ((nnmail-expiry-wait-function nil)
7707                     (nnmail-expiry-wait expiry-wait))
7708                 (setq es (gnus-request-expire-articles
7709                           expirable gnus-newsgroup-name)))
7710             (setq es (gnus-request-expire-articles
7711                       expirable gnus-newsgroup-name))))
7712         (unless total
7713           (setq gnus-newsgroup-expirable es))
7714         ;; We go through the old list of expirable, and mark all
7715         ;; really expired articles as nonexistent.
7716         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7717           (let ((gnus-use-cache nil))
7718             (while expirable
7719               (unless (memq (car expirable) es)
7720                 (when (gnus-data-find (car expirable))
7721                   (gnus-summary-mark-article
7722                    (car expirable) gnus-canceled-mark)))
7723               (setq expirable (cdr expirable)))))
7724         (gnus-message 6 "Expiring articles...done")))))
7725
7726 (defun gnus-summary-expire-articles-now ()
7727   "Expunge all expirable articles in the current group.
7728 This means that *all* articles that are marked as expirable will be
7729 deleted forever, right now."
7730   (interactive)
7731   (or gnus-expert-user
7732       (gnus-yes-or-no-p
7733        "Are you really, really, really sure you want to delete all these messages? ")
7734       (error "Phew!"))
7735   (gnus-summary-expire-articles t))
7736
7737 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7738 (defun gnus-summary-delete-article (&optional n)
7739   "Delete the N next (mail) articles.
7740 This command actually deletes articles.  This is not a marking
7741 command.  The article will disappear forever from your life, never to
7742 return.
7743 If N is negative, delete backwards.
7744 If N is nil and articles have been marked with the process mark,
7745 delete these instead."
7746   (interactive "P")
7747   (unless (gnus-check-backend-function 'request-expire-articles
7748                                        gnus-newsgroup-name)
7749     (error "The current newsgroup does not support article deletion"))
7750   ;; Compute the list of articles to delete.
7751   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7752         not-deleted)
7753     (if (and gnus-novice-user
7754              (not (gnus-yes-or-no-p
7755                    (format "Do you really want to delete %s forever? "
7756                            (if (> (length articles) 1)
7757                                (format "these %s articles" (length articles))
7758                              "this article")))))
7759         ()
7760       ;; Delete the articles.
7761       (setq not-deleted (gnus-request-expire-articles
7762                          articles gnus-newsgroup-name 'force))
7763       (while articles
7764         (gnus-summary-remove-process-mark (car articles))
7765         ;; The backend might not have been able to delete the article
7766         ;; after all.
7767         (unless (memq (car articles) not-deleted)
7768           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7769         (setq articles (cdr articles)))
7770       (when not-deleted
7771         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7772     (gnus-summary-position-point)
7773     (gnus-set-mode-line 'summary)
7774     not-deleted))
7775
7776 (defun gnus-summary-edit-article (&optional force)
7777   "Edit the current article.
7778 This will have permanent effect only in mail groups.
7779 If FORCE is non-nil, allow editing of articles even in read-only
7780 groups."
7781   (interactive "P")
7782   (save-excursion
7783     (set-buffer gnus-summary-buffer)
7784     (let ((mail-parse-charset gnus-newsgroup-charset)
7785           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7786       (gnus-set-global-variables)
7787       (when (and (not force)
7788                  (gnus-group-read-only-p))
7789         (error "The current newsgroup does not support article editing"))
7790       (gnus-summary-show-article t)
7791       (gnus-article-edit-article
7792        'ignore
7793        `(lambda (no-highlight)
7794           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7795                 (mail-parse-ignored-charsets
7796                  ',gnus-newsgroup-ignored-charsets))
7797             (gnus-summary-edit-article-done
7798              ,(or (mail-header-references gnus-current-headers) "")
7799              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7800
7801 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7802
7803 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7804                                                  no-highlight)
7805   "Make edits to the current article permanent."
7806   (interactive)
7807   ;; Replace the article.
7808   (let ((buf (current-buffer)))
7809     (with-temp-buffer
7810       (insert-buffer-substring buf)
7811       (if (and (not read-only)
7812                (not (gnus-request-replace-article
7813                      (cdr gnus-article-current) (car gnus-article-current)
7814                      (current-buffer) t)))
7815           (error "Couldn't replace article")
7816         ;; Update the summary buffer.
7817         (if (and references
7818                  (equal (message-tokenize-header references " ")
7819                         (message-tokenize-header
7820                          (or (message-fetch-field "references") "") " ")))
7821             ;; We only have to update this line.
7822             (save-excursion
7823               (save-restriction
7824                 (message-narrow-to-head)
7825                 (let ((head (buffer-string))
7826                       header)
7827                   (with-temp-buffer
7828                     (insert (format "211 %d Article retrieved.\n"
7829                                     (cdr gnus-article-current)))
7830                     (insert head)
7831                     (insert ".\n")
7832                     (let ((nntp-server-buffer (current-buffer)))
7833                       (setq header (car (gnus-get-newsgroup-headers
7834                                          (save-excursion
7835                                            (set-buffer gnus-summary-buffer)
7836                                            gnus-newsgroup-dependencies)
7837                                          t))))
7838                     (save-excursion
7839                       (set-buffer gnus-summary-buffer)
7840                       (gnus-data-set-header
7841                        (gnus-data-find (cdr gnus-article-current))
7842                        header)
7843                       (gnus-summary-update-article-line
7844                        (cdr gnus-article-current) header))))))
7845           ;; Update threads.
7846           (set-buffer (or buffer gnus-summary-buffer))
7847           (gnus-summary-update-article (cdr gnus-article-current)))
7848         ;; Prettify the article buffer again.
7849         (unless no-highlight
7850           (save-excursion
7851             (set-buffer gnus-article-buffer)
7852             ;;;!!! Fix this -- article should be rehighlighted.
7853             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7854             (set-buffer gnus-original-article-buffer)
7855             (gnus-request-article
7856              (cdr gnus-article-current)
7857              (car gnus-article-current) (current-buffer))))
7858         ;; Prettify the summary buffer line.
7859         (when (gnus-visual-p 'summary-highlight 'highlight)
7860           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7861
7862 (defun gnus-summary-edit-wash (key)
7863   "Perform editing command KEY in the article buffer."
7864   (interactive
7865    (list
7866     (progn
7867       (message "%s" (concat (this-command-keys) "- "))
7868       (read-char))))
7869   (message "")
7870   (gnus-summary-edit-article)
7871   (execute-kbd-macro (concat (this-command-keys) key))
7872   (gnus-article-edit-done))
7873
7874 ;;; Respooling
7875
7876 (defun gnus-summary-respool-query (&optional silent trace)
7877   "Query where the respool algorithm would put this article."
7878   (interactive)
7879   (let (gnus-mark-article-hook)
7880     (gnus-summary-select-article)
7881     (save-excursion
7882       (set-buffer gnus-original-article-buffer)
7883       (save-restriction
7884         (message-narrow-to-head)
7885         (let ((groups (nnmail-article-group 'identity trace)))
7886           (unless silent
7887             (if groups
7888                 (message "This message would go to %s"
7889                          (mapconcat 'car groups ", "))
7890               (message "This message would go to no groups"))
7891             groups))))))
7892
7893 (defun gnus-summary-respool-trace ()
7894   "Trace where the respool algorithm would put this article.
7895 Display a buffer showing all fancy splitting patterns which matched."
7896   (interactive)
7897   (gnus-summary-respool-query nil t))
7898
7899 ;; Summary marking commands.
7900
7901 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7902   "Mark articles which has the same subject as read, and then select the next.
7903 If UNMARK is positive, remove any kind of mark.
7904 If UNMARK is negative, tick articles."
7905   (interactive "P")
7906   (when unmark
7907     (setq unmark (prefix-numeric-value unmark)))
7908   (let ((count
7909          (gnus-summary-mark-same-subject
7910           (gnus-summary-article-subject) unmark)))
7911     ;; Select next unread article.  If auto-select-same mode, should
7912     ;; select the first unread article.
7913     (gnus-summary-next-article t (and gnus-auto-select-same
7914                                       (gnus-summary-article-subject)))
7915     (gnus-message 7 "%d article%s marked as %s"
7916                   count (if (= count 1) " is" "s are")
7917                   (if unmark "unread" "read"))))
7918
7919 (defun gnus-summary-kill-same-subject (&optional unmark)
7920   "Mark articles which has the same subject as read.
7921 If UNMARK is positive, remove any kind of mark.
7922 If UNMARK is negative, tick articles."
7923   (interactive "P")
7924   (when unmark
7925     (setq unmark (prefix-numeric-value unmark)))
7926   (let ((count
7927          (gnus-summary-mark-same-subject
7928           (gnus-summary-article-subject) unmark)))
7929     ;; If marked as read, go to next unread subject.
7930     (when (null unmark)
7931       ;; Go to next unread subject.
7932       (gnus-summary-next-subject 1 t))
7933     (gnus-message 7 "%d articles are marked as %s"
7934                   count (if unmark "unread" "read"))))
7935
7936 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7937   "Mark articles with same SUBJECT as read, and return marked number.
7938 If optional argument UNMARK is positive, remove any kinds of marks.
7939 If optional argument UNMARK is negative, mark articles as unread instead."
7940   (let ((count 1))
7941     (save-excursion
7942       (cond
7943        ((null unmark)                   ; Mark as read.
7944         (while (and
7945                 (progn
7946                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7947                   (gnus-summary-show-thread) t)
7948                 (gnus-summary-find-subject subject))
7949           (setq count (1+ count))))
7950        ((> unmark 0)                    ; Tick.
7951         (while (and
7952                 (progn
7953                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7954                   (gnus-summary-show-thread) t)
7955                 (gnus-summary-find-subject subject))
7956           (setq count (1+ count))))
7957        (t                               ; Mark as unread.
7958         (while (and
7959                 (progn
7960                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7961                   (gnus-summary-show-thread) t)
7962                 (gnus-summary-find-subject subject))
7963           (setq count (1+ count)))))
7964       (gnus-set-mode-line 'summary)
7965       ;; Return the number of marked articles.
7966       count)))
7967
7968 (defun gnus-summary-mark-as-processable (n &optional unmark)
7969   "Set the process mark on the next N articles.
7970 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7971 the process mark instead.  The difference between N and the actual
7972 number of articles marked is returned."
7973   (interactive "p")
7974   (let ((backward (< n 0))
7975         (n (abs n)))
7976     (while (and
7977             (> n 0)
7978             (if unmark
7979                 (gnus-summary-remove-process-mark
7980                  (gnus-summary-article-number))
7981               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7982             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7983       (setq n (1- n)))
7984     (when (/= 0 n)
7985       (gnus-message 7 "No more articles"))
7986     (gnus-summary-recenter)
7987     (gnus-summary-position-point)
7988     n))
7989
7990 (defun gnus-summary-unmark-as-processable (n)
7991   "Remove the process mark from the next N articles.
7992 If N is negative, unmark backward instead.  The difference between N and
7993 the actual number of articles unmarked is returned."
7994   (interactive "p")
7995   (gnus-summary-mark-as-processable n t))
7996
7997 (defun gnus-summary-unmark-all-processable ()
7998   "Remove the process mark from all articles."
7999   (interactive)
8000   (save-excursion
8001     (while gnus-newsgroup-processable
8002       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8003   (gnus-summary-position-point))
8004
8005 (defun gnus-summary-mark-as-expirable (n)
8006   "Mark N articles forward as expirable.
8007 If N is negative, mark backward instead.  The difference between N and
8008 the actual number of articles marked is returned."
8009   (interactive "p")
8010   (gnus-summary-mark-forward n gnus-expirable-mark))
8011
8012 (defun gnus-summary-mark-article-as-replied (article)
8013   "Mark ARTICLE replied and update the summary line."
8014   (push article gnus-newsgroup-replied)
8015   (let ((buffer-read-only nil))
8016     (when (gnus-summary-goto-subject article nil t)
8017       (gnus-summary-update-secondary-mark article))))
8018
8019 (defun gnus-summary-set-bookmark (article)
8020   "Set a bookmark in current article."
8021   (interactive (list (gnus-summary-article-number)))
8022   (when (or (not (get-buffer gnus-article-buffer))
8023             (not gnus-current-article)
8024             (not gnus-article-current)
8025             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8026     (error "No current article selected"))
8027   ;; Remove old bookmark, if one exists.
8028   (let ((old (assq article gnus-newsgroup-bookmarks)))
8029     (when old
8030       (setq gnus-newsgroup-bookmarks
8031             (delq old gnus-newsgroup-bookmarks))))
8032   ;; Set the new bookmark, which is on the form
8033   ;; (article-number . line-number-in-body).
8034   (push
8035    (cons article
8036          (save-excursion
8037            (set-buffer gnus-article-buffer)
8038            (count-lines
8039             (min (point)
8040                  (save-excursion
8041                    (goto-char (point-min))
8042                    (search-forward "\n\n" nil t)
8043                    (point)))
8044             (point))))
8045    gnus-newsgroup-bookmarks)
8046   (gnus-message 6 "A bookmark has been added to the current article."))
8047
8048 (defun gnus-summary-remove-bookmark (article)
8049   "Remove the bookmark from the current article."
8050   (interactive (list (gnus-summary-article-number)))
8051   ;; Remove old bookmark, if one exists.
8052   (let ((old (assq article gnus-newsgroup-bookmarks)))
8053     (if old
8054         (progn
8055           (setq gnus-newsgroup-bookmarks
8056                 (delq old gnus-newsgroup-bookmarks))
8057           (gnus-message 6 "Removed bookmark."))
8058       (gnus-message 6 "No bookmark in current article."))))
8059
8060 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8061 (defun gnus-summary-mark-as-dormant (n)
8062   "Mark N articles forward as dormant.
8063 If N is negative, mark backward instead.  The difference between N and
8064 the actual number of articles marked is returned."
8065   (interactive "p")
8066   (gnus-summary-mark-forward n gnus-dormant-mark))
8067
8068 (defun gnus-summary-set-process-mark (article)
8069   "Set the process mark on ARTICLE and update the summary line."
8070   (setq gnus-newsgroup-processable
8071         (cons article
8072               (delq article gnus-newsgroup-processable)))
8073   (when (gnus-summary-goto-subject article)
8074     (gnus-summary-show-thread)
8075     (gnus-summary-goto-subject article)
8076     (gnus-summary-update-secondary-mark article)))
8077
8078 (defun gnus-summary-remove-process-mark (article)
8079   "Remove the process mark from ARTICLE and update the summary line."
8080   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8081   (when (gnus-summary-goto-subject article)
8082     (gnus-summary-show-thread)
8083     (gnus-summary-goto-subject article)
8084     (gnus-summary-update-secondary-mark article)))
8085
8086 (defun gnus-summary-set-saved-mark (article)
8087   "Set the process mark on ARTICLE and update the summary line."
8088   (push article gnus-newsgroup-saved)
8089   (when (gnus-summary-goto-subject article)
8090     (gnus-summary-update-secondary-mark article)))
8091
8092 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8093   "Mark N articles as read forwards.
8094 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8095 The difference between N and the actual number of articles marked is
8096 returned."
8097   (interactive "p")
8098   (gnus-summary-show-thread)
8099   (let ((backward (< n 0))
8100         (gnus-summary-goto-unread
8101          (and gnus-summary-goto-unread
8102               (not (eq gnus-summary-goto-unread 'never))
8103               (not (memq mark (list gnus-unread-mark
8104                                     gnus-ticked-mark gnus-dormant-mark)))))
8105         (n (abs n))
8106         (mark (or mark gnus-del-mark)))
8107     (while (and (> n 0)
8108                 (gnus-summary-mark-article nil mark no-expire)
8109                 (zerop (gnus-summary-next-subject
8110                         (if backward -1 1)
8111                         (and gnus-summary-goto-unread
8112                              (not (eq gnus-summary-goto-unread 'never)))
8113                         t)))
8114       (setq n (1- n)))
8115     (when (/= 0 n)
8116       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8117     (gnus-summary-recenter)
8118     (gnus-summary-position-point)
8119     (gnus-set-mode-line 'summary)
8120     n))
8121
8122 (defun gnus-summary-mark-article-as-read (mark)
8123   "Mark the current article quickly as read with MARK."
8124   (let ((article (gnus-summary-article-number)))
8125     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8126     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8127     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8128     (push (cons article mark) gnus-newsgroup-reads)
8129     ;; Possibly remove from cache, if that is used.
8130     (when gnus-use-cache
8131       (gnus-cache-enter-remove-article article))
8132     ;; Allow the backend to change the mark.
8133     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8134     ;; Check for auto-expiry.
8135     (when (and gnus-newsgroup-auto-expire
8136                (memq mark gnus-auto-expirable-marks))
8137       (setq mark gnus-expirable-mark)
8138       ;; Let the backend know about the mark change.
8139       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8140       (push article gnus-newsgroup-expirable))
8141     ;; Set the mark in the buffer.
8142     (gnus-summary-update-mark mark 'unread)
8143     t))
8144
8145 (defun gnus-summary-mark-article-as-unread (mark)
8146   "Mark the current article quickly as unread with MARK."
8147   (let* ((article (gnus-summary-article-number))
8148          (old-mark (gnus-summary-article-mark article)))
8149     ;; Allow the backend to change the mark.
8150     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8151     (if (eq mark old-mark)
8152         t
8153       (if (<= article 0)
8154           (progn
8155             (gnus-error 1 "Can't mark negative article numbers")
8156             nil)
8157         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8158         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8159         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8160         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8161         (cond ((= mark gnus-ticked-mark)
8162                (push article gnus-newsgroup-marked))
8163               ((= mark gnus-dormant-mark)
8164                (push article gnus-newsgroup-dormant))
8165               (t
8166                (push article gnus-newsgroup-unreads)))
8167         (gnus-pull article gnus-newsgroup-reads)
8168
8169         ;; See whether the article is to be put in the cache.
8170         (and gnus-use-cache
8171              (vectorp (gnus-summary-article-header article))
8172              (save-excursion
8173                (gnus-cache-possibly-enter-article
8174                 gnus-newsgroup-name article
8175                 (gnus-summary-article-header article)
8176                 (= mark gnus-ticked-mark)
8177                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8178
8179         ;; Fix the mark.
8180         (gnus-summary-update-mark mark 'unread)
8181         t))))
8182
8183 (defun gnus-summary-mark-article (&optional article mark no-expire)
8184   "Mark ARTICLE with MARK.  MARK can be any character.
8185 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8186 `??' (dormant) and `?E' (expirable).
8187 If MARK is nil, then the default character `?r' is used.
8188 If ARTICLE is nil, then the article on the current line will be
8189 marked."
8190   ;; The mark might be a string.
8191   (when (stringp mark)
8192     (setq mark (aref mark 0)))
8193   ;; If no mark is given, then we check auto-expiring.
8194   (when (null mark)
8195     (setq mark gnus-del-mark))
8196   (when (and (not no-expire)
8197              gnus-newsgroup-auto-expire
8198              (memq mark gnus-auto-expirable-marks))
8199     (setq mark gnus-expirable-mark))
8200   (let ((article (or article (gnus-summary-article-number)))
8201         (old-mark (gnus-summary-article-mark article)))
8202     ;; Allow the backend to change the mark.
8203     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8204     (if (eq mark old-mark)
8205         t
8206       (unless article
8207         (error "No article on current line"))
8208       (if (not (if (or (= mark gnus-unread-mark)
8209                        (= mark gnus-ticked-mark)
8210                        (= mark gnus-dormant-mark))
8211                    (gnus-mark-article-as-unread article mark)
8212                  (gnus-mark-article-as-read article mark)))
8213           t
8214         ;; See whether the article is to be put in the cache.
8215         (and gnus-use-cache
8216              (not (= mark gnus-canceled-mark))
8217              (vectorp (gnus-summary-article-header article))
8218              (save-excursion
8219                (gnus-cache-possibly-enter-article
8220                 gnus-newsgroup-name article
8221                 (gnus-summary-article-header article)
8222                 (= mark gnus-ticked-mark)
8223                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8224
8225         (when (gnus-summary-goto-subject article nil t)
8226           (let ((buffer-read-only nil))
8227             (gnus-summary-show-thread)
8228             ;; Fix the mark.
8229             (gnus-summary-update-mark mark 'unread)
8230             t))))))
8231
8232 (defun gnus-summary-update-secondary-mark (article)
8233   "Update the secondary (read, process, cache) mark."
8234   (gnus-summary-update-mark
8235    (cond ((memq article gnus-newsgroup-processable)
8236           gnus-process-mark)
8237          ((memq article gnus-newsgroup-cached)
8238           gnus-cached-mark)
8239          ((memq article gnus-newsgroup-replied)
8240           gnus-replied-mark)
8241          ((memq article gnus-newsgroup-saved)
8242           gnus-saved-mark)
8243          (t gnus-unread-mark))
8244    'replied)
8245   (when (gnus-visual-p 'summary-highlight 'highlight)
8246     (gnus-run-hooks 'gnus-summary-update-hook))
8247   t)
8248
8249 (defun gnus-summary-update-mark (mark type)
8250   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8251         (buffer-read-only nil))
8252     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8253     (when forward
8254       (when (looking-at "\r")
8255         (incf forward))
8256       (when (<= (+ forward (point)) (point-max))
8257         ;; Go to the right position on the line.
8258         (goto-char (+ forward (point)))
8259         ;; Replace the old mark with the new mark.
8260         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8261         ;; Optionally update the marks by some user rule.
8262         (when (eq type 'unread)
8263           (gnus-data-set-mark
8264            (gnus-data-find (gnus-summary-article-number)) mark)
8265           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8266
8267 (defun gnus-mark-article-as-read (article &optional mark)
8268   "Enter ARTICLE in the pertinent lists and remove it from others."
8269   ;; Make the article expirable.
8270   (let ((mark (or mark gnus-del-mark)))
8271     (if (= mark gnus-expirable-mark)
8272         (push article gnus-newsgroup-expirable)
8273       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8274     ;; Remove from unread and marked lists.
8275     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8276     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8277     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8278     (push (cons article mark) gnus-newsgroup-reads)
8279     ;; Possibly remove from cache, if that is used.
8280     (when gnus-use-cache
8281       (gnus-cache-enter-remove-article article))
8282     t))
8283
8284 (defun gnus-mark-article-as-unread (article &optional mark)
8285   "Enter ARTICLE in the pertinent lists and remove it from others."
8286   (let ((mark (or mark gnus-ticked-mark)))
8287     (if (<= article 0)
8288         (progn
8289           (gnus-error 1 "Can't mark negative article numbers")
8290           nil)
8291       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8292             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8293             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8294             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8295
8296       ;; Unsuppress duplicates?
8297       (when gnus-suppress-duplicates
8298         (gnus-dup-unsuppress-article article))
8299
8300       (cond ((= mark gnus-ticked-mark)
8301              (push article gnus-newsgroup-marked))
8302             ((= mark gnus-dormant-mark)
8303              (push article gnus-newsgroup-dormant))
8304             (t
8305              (push article gnus-newsgroup-unreads)))
8306       (gnus-pull article gnus-newsgroup-reads)
8307       t)))
8308
8309 (defalias 'gnus-summary-mark-as-unread-forward
8310   'gnus-summary-tick-article-forward)
8311 (make-obsolete 'gnus-summary-mark-as-unread-forward
8312                'gnus-summary-tick-article-forward)
8313 (defun gnus-summary-tick-article-forward (n)
8314   "Tick N articles forwards.
8315 If N is negative, tick backwards instead.
8316 The difference between N and the number of articles ticked is returned."
8317   (interactive "p")
8318   (gnus-summary-mark-forward n gnus-ticked-mark))
8319
8320 (defalias 'gnus-summary-mark-as-unread-backward
8321   'gnus-summary-tick-article-backward)
8322 (make-obsolete 'gnus-summary-mark-as-unread-backward
8323                'gnus-summary-tick-article-backward)
8324 (defun gnus-summary-tick-article-backward (n)
8325   "Tick N articles backwards.
8326 The difference between N and the number of articles ticked is returned."
8327   (interactive "p")
8328   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8329
8330 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8331 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8332 (defun gnus-summary-tick-article (&optional article clear-mark)
8333   "Mark current article as unread.
8334 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8335 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8336   (interactive)
8337   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8338                                        gnus-ticked-mark)))
8339
8340 (defun gnus-summary-mark-as-read-forward (n)
8341   "Mark N articles as read forwards.
8342 If N is negative, mark backwards instead.
8343 The difference between N and the actual number of articles marked is
8344 returned."
8345   (interactive "p")
8346   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8347
8348 (defun gnus-summary-mark-as-read-backward (n)
8349   "Mark the N articles as read backwards.
8350 The difference between N and the actual number of articles marked is
8351 returned."
8352   (interactive "p")
8353   (gnus-summary-mark-forward
8354    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8355
8356 (defun gnus-summary-mark-as-read (&optional article mark)
8357   "Mark current article as read.
8358 ARTICLE specifies the article to be marked as read.
8359 MARK specifies a string to be inserted at the beginning of the line."
8360   (gnus-summary-mark-article article mark))
8361
8362 (defun gnus-summary-clear-mark-forward (n)
8363   "Clear marks from N articles forward.
8364 If N is negative, clear backward instead.
8365 The difference between N and the number of marks cleared is returned."
8366   (interactive "p")
8367   (gnus-summary-mark-forward n gnus-unread-mark))
8368
8369 (defun gnus-summary-clear-mark-backward (n)
8370   "Clear marks from N articles backward.
8371 The difference between N and the number of marks cleared is returned."
8372   (interactive "p")
8373   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8374
8375 (defun gnus-summary-mark-unread-as-read ()
8376   "Intended to be used by `gnus-summary-mark-article-hook'."
8377   (when (memq gnus-current-article gnus-newsgroup-unreads)
8378     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8379
8380 (defun gnus-summary-mark-read-and-unread-as-read ()
8381   "Intended to be used by `gnus-summary-mark-article-hook'."
8382   (let ((mark (gnus-summary-article-mark)))
8383     (when (or (gnus-unread-mark-p mark)
8384               (gnus-read-mark-p mark))
8385       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8386
8387 (defun gnus-summary-mark-region-as-read (point mark all)
8388   "Mark all unread articles between point and mark as read.
8389 If given a prefix, mark all articles between point and mark as read,
8390 even ticked and dormant ones."
8391   (interactive "r\nP")
8392   (save-excursion
8393     (let (article)
8394       (goto-char point)
8395       (beginning-of-line)
8396       (while (and
8397               (< (point) mark)
8398               (progn
8399                 (when (or all
8400                           (memq (setq article (gnus-summary-article-number))
8401                                 gnus-newsgroup-unreads))
8402                   (gnus-summary-mark-article article gnus-del-mark))
8403                 t)
8404               (gnus-summary-find-next))))))
8405
8406 (defun gnus-summary-mark-below (score mark)
8407   "Mark articles with score less than SCORE with MARK."
8408   (interactive "P\ncMark: ")
8409   (setq score (if score
8410                   (prefix-numeric-value score)
8411                 (or gnus-summary-default-score 0)))
8412   (save-excursion
8413     (set-buffer gnus-summary-buffer)
8414     (goto-char (point-min))
8415     (while
8416         (progn
8417           (and (< (gnus-summary-article-score) score)
8418                (gnus-summary-mark-article nil mark))
8419           (gnus-summary-find-next)))))
8420
8421 (defun gnus-summary-kill-below (&optional score)
8422   "Mark articles with score below SCORE as read."
8423   (interactive "P")
8424   (gnus-summary-mark-below score gnus-killed-mark))
8425
8426 (defun gnus-summary-clear-above (&optional score)
8427   "Clear all marks from articles with score above SCORE."
8428   (interactive "P")
8429   (gnus-summary-mark-above score gnus-unread-mark))
8430
8431 (defun gnus-summary-tick-above (&optional score)
8432   "Tick all articles with score above SCORE."
8433   (interactive "P")
8434   (gnus-summary-mark-above score gnus-ticked-mark))
8435
8436 (defun gnus-summary-mark-above (score mark)
8437   "Mark articles with score over SCORE with MARK."
8438   (interactive "P\ncMark: ")
8439   (setq score (if score
8440                   (prefix-numeric-value score)
8441                 (or gnus-summary-default-score 0)))
8442   (save-excursion
8443     (set-buffer gnus-summary-buffer)
8444     (goto-char (point-min))
8445     (while (and (progn
8446                   (when (> (gnus-summary-article-score) score)
8447                     (gnus-summary-mark-article nil mark))
8448                   t)
8449                 (gnus-summary-find-next)))))
8450
8451 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8452 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8453 (defun gnus-summary-limit-include-expunged (&optional no-error)
8454   "Display all the hidden articles that were expunged for low scores."
8455   (interactive)
8456   (let ((buffer-read-only nil))
8457     (let ((scored gnus-newsgroup-scored)
8458           headers h)
8459       (while scored
8460         (unless (gnus-summary-goto-subject (caar scored))
8461           (and (setq h (gnus-summary-article-header (caar scored)))
8462                (< (cdar scored) gnus-summary-expunge-below)
8463                (push h headers)))
8464         (setq scored (cdr scored)))
8465       (if (not headers)
8466           (when (not no-error)
8467             (error "No expunged articles hidden"))
8468         (goto-char (point-min))
8469         (gnus-summary-prepare-unthreaded (nreverse headers))
8470         (goto-char (point-min))
8471         (gnus-summary-position-point)
8472         t))))
8473
8474 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8475   "Mark all unread articles in this newsgroup as read.
8476 If prefix argument ALL is non-nil, ticked and dormant articles will
8477 also be marked as read.
8478 If QUIETLY is non-nil, no questions will be asked.
8479 If TO-HERE is non-nil, it should be a point in the buffer.  All
8480 articles before this point will be marked as read.
8481 Note that this function will only catch up the unread article
8482 in the current summary buffer limitation.
8483 The number of articles marked as read is returned."
8484   (interactive "P")
8485   (prog1
8486       (save-excursion
8487         (when (or quietly
8488                   (not gnus-interactive-catchup) ;Without confirmation?
8489                   gnus-expert-user
8490                   (gnus-y-or-n-p
8491                    (if all
8492                        "Mark absolutely all articles as read? "
8493                      "Mark all unread articles as read? ")))
8494           (if (and not-mark
8495                    (not gnus-newsgroup-adaptive)
8496                    (not gnus-newsgroup-auto-expire)
8497                    (not gnus-suppress-duplicates)
8498                    (or (not gnus-use-cache)
8499                        (eq gnus-use-cache 'passive)))
8500               (progn
8501                 (when all
8502                   (setq gnus-newsgroup-marked nil
8503                         gnus-newsgroup-dormant nil))
8504                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8505             ;; We actually mark all articles as canceled, which we
8506             ;; have to do when using auto-expiry or adaptive scoring.
8507             (gnus-summary-show-all-threads)
8508             (when (gnus-summary-first-subject (not all) t)
8509               (while (and
8510                       (if to-here (< (point) to-here) t)
8511                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8512                       (gnus-summary-find-next (not all) nil nil t))))
8513             (gnus-set-mode-line 'summary))
8514           t))
8515     (gnus-summary-position-point)))
8516
8517 (defun gnus-summary-catchup-to-here (&optional all)
8518   "Mark all unticked articles before the current one as read.
8519 If ALL is non-nil, also mark ticked and dormant articles as read."
8520   (interactive "P")
8521   (save-excursion
8522     (gnus-save-hidden-threads
8523       (let ((beg (point)))
8524         ;; We check that there are unread articles.
8525         (when (or all (gnus-summary-find-prev))
8526           (gnus-summary-catchup all t beg)))))
8527   (gnus-summary-position-point))
8528
8529 (defun gnus-summary-catchup-all (&optional quietly)
8530   "Mark all articles in this newsgroup as read."
8531   (interactive "P")
8532   (gnus-summary-catchup t quietly))
8533
8534 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8535   "Mark all unread articles in this group as read, then exit.
8536 If prefix argument ALL is non-nil, all articles are marked as read."
8537   (interactive "P")
8538   (when (gnus-summary-catchup all quietly nil 'fast)
8539     ;; Select next newsgroup or exit.
8540     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8541              (eq gnus-auto-select-next 'quietly))
8542         (gnus-summary-next-group nil)
8543       (gnus-summary-exit))))
8544
8545 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8546   "Mark all articles in this newsgroup as read, and then exit."
8547   (interactive "P")
8548   (gnus-summary-catchup-and-exit t quietly))
8549
8550 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8551 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8552   "Mark all articles in this group as read and select the next group.
8553 If given a prefix, mark all articles, unread as well as ticked, as
8554 read."
8555   (interactive "P")
8556   (save-excursion
8557     (gnus-summary-catchup all))
8558   (gnus-summary-next-article t nil nil t))
8559
8560 ;; Thread-based commands.
8561
8562 (defun gnus-summary-articles-in-thread (&optional article)
8563   "Return a list of all articles in the current thread.
8564 If ARTICLE is non-nil, return all articles in the thread that starts
8565 with that article."
8566   (let* ((article (or article (gnus-summary-article-number)))
8567          (data (gnus-data-find-list article))
8568          (top-level (gnus-data-level (car data)))
8569          (top-subject
8570           (cond ((null gnus-thread-operation-ignore-subject)
8571                  (gnus-simplify-subject-re
8572                   (mail-header-subject (gnus-data-header (car data)))))
8573                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8574                  (gnus-simplify-subject-fuzzy
8575                   (mail-header-subject (gnus-data-header (car data)))))
8576                 (t nil)))
8577          (end-point (save-excursion
8578                       (if (gnus-summary-go-to-next-thread)
8579                           (point) (point-max))))
8580          articles)
8581     (while (and data
8582                 (< (gnus-data-pos (car data)) end-point))
8583       (when (or (not top-subject)
8584                 (string= top-subject
8585                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8586                              (gnus-simplify-subject-fuzzy
8587                               (mail-header-subject
8588                                (gnus-data-header (car data))))
8589                            (gnus-simplify-subject-re
8590                             (mail-header-subject
8591                              (gnus-data-header (car data)))))))
8592         (push (gnus-data-number (car data)) articles))
8593       (unless (and (setq data (cdr data))
8594                    (> (gnus-data-level (car data)) top-level))
8595         (setq data nil)))
8596     ;; Return the list of articles.
8597     (nreverse articles)))
8598
8599 (defun gnus-summary-rethread-current ()
8600   "Rethread the thread the current article is part of."
8601   (interactive)
8602   (let* ((gnus-show-threads t)
8603          (article (gnus-summary-article-number))
8604          (id (mail-header-id (gnus-summary-article-header)))
8605          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8606     (unless id
8607       (error "No article on the current line"))
8608     (gnus-rebuild-thread id)
8609     (gnus-summary-goto-subject article)))
8610
8611 (defun gnus-summary-reparent-thread ()
8612   "Make the current article child of the marked (or previous) article.
8613
8614 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8615 is non-nil or the Subject: of both articles are the same."
8616   (interactive)
8617   (unless (not (gnus-group-read-only-p))
8618     (error "The current newsgroup does not support article editing"))
8619   (unless (<= (length gnus-newsgroup-processable) 1)
8620     (error "No more than one article may be marked"))
8621   (save-window-excursion
8622     (let ((gnus-article-buffer " *reparent*")
8623           (current-article (gnus-summary-article-number))
8624           ;; First grab the marked article, otherwise one line up.
8625           (parent-article (if (not (null gnus-newsgroup-processable))
8626                               (car gnus-newsgroup-processable)
8627                             (save-excursion
8628                               (if (eq (forward-line -1) 0)
8629                                   (gnus-summary-article-number)
8630                                 (error "Beginning of summary buffer"))))))
8631       (unless (not (eq current-article parent-article))
8632         (error "An article may not be self-referential"))
8633       (let ((message-id (mail-header-id
8634                          (gnus-summary-article-header parent-article))))
8635         (unless (and message-id (not (equal message-id "")))
8636           (error "No message-id in desired parent"))
8637         (gnus-with-article current-article
8638           (goto-char (point-min))
8639           (if (re-search-forward "^References: " nil t)
8640               (progn
8641                 (re-search-forward "^[^ \t]" nil t)
8642                 (forward-line -1)
8643                 (end-of-line)
8644                 (insert " " message-id))
8645             (insert "References: " message-id "\n")))
8646         (set-buffer gnus-summary-buffer)
8647         (gnus-summary-unmark-all-processable)
8648         (gnus-summary-update-article current-article)
8649         (gnus-summary-rethread-current)
8650         (gnus-message 3 "Article %d is now the child of article %d"
8651                       current-article parent-article)))))
8652
8653 (defun gnus-summary-toggle-threads (&optional arg)
8654   "Toggle showing conversation threads.
8655 If ARG is positive number, turn showing conversation threads on."
8656   (interactive "P")
8657   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8658     (setq gnus-show-threads
8659           (if (null arg) (not gnus-show-threads)
8660             (> (prefix-numeric-value arg) 0)))
8661     (gnus-summary-prepare)
8662     (gnus-summary-goto-subject current)
8663     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8664     (gnus-summary-position-point)))
8665
8666 (defun gnus-summary-show-all-threads ()
8667   "Show all threads."
8668   (interactive)
8669   (save-excursion
8670     (let ((buffer-read-only nil))
8671       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8672   (gnus-summary-position-point))
8673
8674 (defun gnus-summary-show-thread ()
8675   "Show thread subtrees.
8676 Returns nil if no thread was there to be shown."
8677   (interactive)
8678   (let ((buffer-read-only nil)
8679         (orig (point))
8680         ;; first goto end then to beg, to have point at beg after let
8681         (end (progn (end-of-line) (point)))
8682         (beg (progn (beginning-of-line) (point))))
8683     (prog1
8684         ;; Any hidden lines here?
8685         (search-forward "\r" end t)
8686       (subst-char-in-region beg end ?\^M ?\n t)
8687       (goto-char orig)
8688       (gnus-summary-position-point))))
8689
8690 (defun gnus-summary-hide-all-threads ()
8691   "Hide all thread subtrees."
8692   (interactive)
8693   (save-excursion
8694     (goto-char (point-min))
8695     (gnus-summary-hide-thread)
8696     (while (zerop (gnus-summary-next-thread 1 t))
8697       (gnus-summary-hide-thread)))
8698   (gnus-summary-position-point))
8699
8700 (defun gnus-summary-hide-thread ()
8701   "Hide thread subtrees.
8702 Returns nil if no threads were there to be hidden."
8703   (interactive)
8704   (let ((buffer-read-only nil)
8705         (start (point))
8706         (article (gnus-summary-article-number)))
8707     (goto-char start)
8708     ;; Go forward until either the buffer ends or the subthread
8709     ;; ends.
8710     (when (and (not (eobp))
8711                (or (zerop (gnus-summary-next-thread 1 t))
8712                    (goto-char (point-max))))
8713       (prog1
8714           (if (and (> (point) start)
8715                    (search-backward "\n" start t))
8716               (progn
8717                 (subst-char-in-region start (point) ?\n ?\^M)
8718                 (gnus-summary-goto-subject article))
8719             (goto-char start)
8720             nil)
8721         ;;(gnus-summary-position-point)
8722         ))))
8723
8724 (defun gnus-summary-go-to-next-thread (&optional previous)
8725   "Go to the same level (or less) next thread.
8726 If PREVIOUS is non-nil, go to previous thread instead.
8727 Return the article number moved to, or nil if moving was impossible."
8728   (let ((level (gnus-summary-thread-level))
8729         (way (if previous -1 1))
8730         (beg (point)))
8731     (forward-line way)
8732     (while (and (not (eobp))
8733                 (< level (gnus-summary-thread-level)))
8734       (forward-line way))
8735     (if (eobp)
8736         (progn
8737           (goto-char beg)
8738           nil)
8739       (setq beg (point))
8740       (prog1
8741           (gnus-summary-article-number)
8742         (goto-char beg)))))
8743
8744 (defun gnus-summary-next-thread (n &optional silent)
8745   "Go to the same level next N'th thread.
8746 If N is negative, search backward instead.
8747 Returns the difference between N and the number of skips actually
8748 done.
8749
8750 If SILENT, don't output messages."
8751   (interactive "p")
8752   (let ((backward (< n 0))
8753         (n (abs n)))
8754     (while (and (> n 0)
8755                 (gnus-summary-go-to-next-thread backward))
8756       (decf n))
8757     (unless silent
8758       (gnus-summary-position-point))
8759     (when (and (not silent) (/= 0 n))
8760       (gnus-message 7 "No more threads"))
8761     n))
8762
8763 (defun gnus-summary-prev-thread (n)
8764   "Go to the same level previous N'th thread.
8765 Returns the difference between N and the number of skips actually
8766 done."
8767   (interactive "p")
8768   (gnus-summary-next-thread (- n)))
8769
8770 (defun gnus-summary-go-down-thread ()
8771   "Go down one level in the current thread."
8772   (let ((children (gnus-summary-article-children)))
8773     (when children
8774       (gnus-summary-goto-subject (car children)))))
8775
8776 (defun gnus-summary-go-up-thread ()
8777   "Go up one level in the current thread."
8778   (let ((parent (gnus-summary-article-parent)))
8779     (when parent
8780       (gnus-summary-goto-subject parent))))
8781
8782 (defun gnus-summary-down-thread (n)
8783   "Go down thread N steps.
8784 If N is negative, go up instead.
8785 Returns the difference between N and how many steps down that were
8786 taken."
8787   (interactive "p")
8788   (let ((up (< n 0))
8789         (n (abs n)))
8790     (while (and (> n 0)
8791                 (if up (gnus-summary-go-up-thread)
8792                   (gnus-summary-go-down-thread)))
8793       (setq n (1- n)))
8794     (gnus-summary-position-point)
8795     (when (/= 0 n)
8796       (gnus-message 7 "Can't go further"))
8797     n))
8798
8799 (defun gnus-summary-up-thread (n)
8800   "Go up thread N steps.
8801 If N is negative, go up instead.
8802 Returns the difference between N and how many steps down that were
8803 taken."
8804   (interactive "p")
8805   (gnus-summary-down-thread (- n)))
8806
8807 (defun gnus-summary-top-thread ()
8808   "Go to the top of the thread."
8809   (interactive)
8810   (while (gnus-summary-go-up-thread))
8811   (gnus-summary-article-number))
8812
8813 (defun gnus-summary-kill-thread (&optional unmark)
8814   "Mark articles under current thread as read.
8815 If the prefix argument is positive, remove any kinds of marks.
8816 If the prefix argument is negative, tick articles instead."
8817   (interactive "P")
8818   (when unmark
8819     (setq unmark (prefix-numeric-value unmark)))
8820   (let ((articles (gnus-summary-articles-in-thread)))
8821     (save-excursion
8822       ;; Expand the thread.
8823       (gnus-summary-show-thread)
8824       ;; Mark all the articles.
8825       (while articles
8826         (gnus-summary-goto-subject (car articles))
8827         (cond ((null unmark)
8828                (gnus-summary-mark-article-as-read gnus-killed-mark))
8829               ((> unmark 0)
8830                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8831               (t
8832                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8833         (setq articles (cdr articles))))
8834     ;; Hide killed subtrees.
8835     (and (null unmark)
8836          gnus-thread-hide-killed
8837          (gnus-summary-hide-thread))
8838     ;; If marked as read, go to next unread subject.
8839     (when (null unmark)
8840       ;; Go to next unread subject.
8841       (gnus-summary-next-subject 1 t)))
8842   (gnus-set-mode-line 'summary))
8843
8844 ;; Summary sorting commands
8845
8846 (defun gnus-summary-sort-by-number (&optional reverse)
8847   "Sort the summary buffer by article number.
8848 Argument REVERSE means reverse order."
8849   (interactive "P")
8850   (gnus-summary-sort 'number reverse))
8851
8852 (defun gnus-summary-sort-by-author (&optional reverse)
8853   "Sort the summary buffer by author name alphabetically.
8854 If case-fold-search is non-nil, case of letters is ignored.
8855 Argument REVERSE means reverse order."
8856   (interactive "P")
8857   (gnus-summary-sort 'author reverse))
8858
8859 (defun gnus-summary-sort-by-subject (&optional reverse)
8860   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8861 If case-fold-search is non-nil, case of letters is ignored.
8862 Argument REVERSE means reverse order."
8863   (interactive "P")
8864   (gnus-summary-sort 'subject reverse))
8865
8866 (defun gnus-summary-sort-by-date (&optional reverse)
8867   "Sort the summary buffer by date.
8868 Argument REVERSE means reverse order."
8869   (interactive "P")
8870   (gnus-summary-sort 'date reverse))
8871
8872 (defun gnus-summary-sort-by-score (&optional reverse)
8873   "Sort the summary buffer by score.
8874 Argument REVERSE means reverse order."
8875   (interactive "P")
8876   (gnus-summary-sort 'score reverse))
8877
8878 (defun gnus-summary-sort-by-lines (&optional reverse)
8879   "Sort the summary buffer by the number of lines.
8880 Argument REVERSE means reverse order."
8881   (interactive "P")
8882   (gnus-summary-sort 'lines reverse))
8883
8884 (defun gnus-summary-sort-by-chars (&optional reverse)
8885   "Sort the summary buffer by article length.
8886 Argument REVERSE means reverse order."
8887   (interactive "P")
8888   (gnus-summary-sort 'chars reverse))   
8889
8890 (defun gnus-summary-sort (predicate reverse)
8891   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8892   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8893          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8894          (gnus-thread-sort-functions
8895           (if (not reverse)
8896               thread
8897             `(lambda (t1 t2)
8898                (,thread t2 t1))))
8899          (gnus-article-sort-functions
8900           (if (not reverse)
8901               article
8902             `(lambda (t1 t2)
8903                (,article t2 t1))))
8904          (buffer-read-only)
8905          (gnus-summary-prepare-hook nil))
8906     ;; We do the sorting by regenerating the threads.
8907     (gnus-summary-prepare)
8908     ;; Hide subthreads if needed.
8909     (when (and gnus-show-threads gnus-thread-hide-subtree)
8910       (gnus-summary-hide-all-threads))))
8911
8912 ;; Summary saving commands.
8913
8914 (defun gnus-summary-save-article (&optional n not-saved)
8915   "Save the current article using the default saver function.
8916 If N is a positive number, save the N next articles.
8917 If N is a negative number, save the N previous articles.
8918 If N is nil and any articles have been marked with the process mark,
8919 save those articles instead.
8920 The variable `gnus-default-article-saver' specifies the saver function."
8921   (interactive "P")
8922   (let* ((articles (gnus-summary-work-articles n))
8923          (save-buffer (save-excursion
8924                         (nnheader-set-temp-buffer " *Gnus Save*")))
8925          (num (length articles))
8926          header file)
8927     (dolist (article articles)
8928       (setq header (gnus-summary-article-header article))
8929       (if (not (vectorp header))
8930           ;; This is a pseudo-article.
8931           (if (assq 'name header)
8932               (gnus-copy-file (cdr (assq 'name header)))
8933             (gnus-message 1 "Article %d is unsaveable" article))
8934         ;; This is a real article.
8935         (save-window-excursion
8936           (gnus-summary-select-article t nil nil article))
8937         (save-excursion
8938           (set-buffer save-buffer)
8939           (erase-buffer)
8940           (insert-buffer-substring gnus-original-article-buffer))
8941         (setq file (gnus-article-save save-buffer file num))
8942         (gnus-summary-remove-process-mark article)
8943         (unless not-saved
8944           (gnus-summary-set-saved-mark article))))
8945     (gnus-kill-buffer save-buffer)
8946     (gnus-summary-position-point)
8947     (gnus-set-mode-line 'summary)
8948     n))
8949
8950 (defun gnus-summary-pipe-output (&optional arg)
8951   "Pipe the current article to a subprocess.
8952 If N is a positive number, pipe the N next articles.
8953 If N is a negative number, pipe the N previous articles.
8954 If N is nil and any articles have been marked with the process mark,
8955 pipe those articles instead."
8956   (interactive "P")
8957   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8958     (gnus-summary-save-article arg t))
8959   (gnus-configure-windows 'pipe))
8960
8961 (defun gnus-summary-save-article-mail (&optional arg)
8962   "Append the current article to an mail file.
8963 If N is a positive number, save the N next articles.
8964 If N is a negative number, save the N previous articles.
8965 If N is nil and any articles have been marked with the process mark,
8966 save those articles instead."
8967   (interactive "P")
8968   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8969     (gnus-summary-save-article arg)))
8970
8971 (defun gnus-summary-save-article-rmail (&optional arg)
8972   "Append the current article to an rmail file.
8973 If N is a positive number, save the N next articles.
8974 If N is a negative number, save the N previous articles.
8975 If N is nil and any articles have been marked with the process mark,
8976 save those articles instead."
8977   (interactive "P")
8978   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8979     (gnus-summary-save-article arg)))
8980
8981 (defun gnus-summary-save-article-file (&optional arg)
8982   "Append the current article to a file.
8983 If N is a positive number, save the N next articles.
8984 If N is a negative number, save the N previous articles.
8985 If N is nil and any articles have been marked with the process mark,
8986 save those articles instead."
8987   (interactive "P")
8988   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8989     (gnus-summary-save-article arg)))
8990
8991 (defun gnus-summary-write-article-file (&optional arg)
8992   "Write the current article to a file, deleting the previous file.
8993 If N is a positive number, save the N next articles.
8994 If N is a negative number, save the N previous articles.
8995 If N is nil and any articles have been marked with the process mark,
8996 save those articles instead."
8997   (interactive "P")
8998   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8999     (gnus-summary-save-article arg)))
9000
9001 (defun gnus-summary-save-article-body-file (&optional arg)
9002   "Append the current article body to a file.
9003 If N is a positive number, save the N next articles.
9004 If N is a negative number, save the N previous articles.
9005 If N is nil and any articles have been marked with the process mark,
9006 save those articles instead."
9007   (interactive "P")
9008   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9009     (gnus-summary-save-article arg)))
9010
9011 (defun gnus-summary-pipe-message (program)
9012   "Pipe the current article through PROGRAM."
9013   (interactive "sProgram: ")
9014   (gnus-summary-select-article)
9015   (let ((mail-header-separator ""))
9016     (gnus-eval-in-buffer-window gnus-article-buffer
9017       (save-restriction
9018         (widen)
9019         (let ((start (window-start))
9020               buffer-read-only)
9021           (message-pipe-buffer-body program)
9022           (set-window-start (get-buffer-window (current-buffer)) start))))))
9023
9024 (defun gnus-get-split-value (methods)
9025   "Return a value based on the split METHODS."
9026   (let (split-name method result match)
9027     (when methods
9028       (save-excursion
9029         (set-buffer gnus-original-article-buffer)
9030         (save-restriction
9031           (nnheader-narrow-to-headers)
9032           (while methods
9033             (goto-char (point-min))
9034             (setq method (pop methods))
9035             (setq match (car method))
9036             (when (cond
9037                    ((stringp match)
9038                     ;; Regular expression.
9039                     (ignore-errors
9040                       (re-search-forward match nil t)))
9041                    ((gnus-functionp match)
9042                     ;; Function.
9043                     (save-restriction
9044                       (widen)
9045                       (setq result (funcall match gnus-newsgroup-name))))
9046                    ((consp match)
9047                     ;; Form.
9048                     (save-restriction
9049                       (widen)
9050                       (setq result (eval match)))))
9051               (setq split-name (append (cdr method) split-name))
9052               (cond ((stringp result)
9053                      (push (expand-file-name
9054                             result gnus-article-save-directory)
9055                            split-name))
9056                     ((consp result)
9057                      (setq split-name (append result split-name)))))))))
9058     (nreverse split-name)))
9059
9060 (defun gnus-valid-move-group-p (group)
9061   (and (boundp group)
9062        (symbol-name group)
9063        (symbol-value group)
9064        (gnus-get-function (gnus-find-method-for-group
9065                            (symbol-name group)) 'request-accept-article t)))
9066
9067 (defun gnus-read-move-group-name (prompt default articles prefix)
9068   "Read a group name."
9069   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9070          (minibuffer-confirm-incomplete nil) ; XEmacs
9071          (prom
9072           (format "%s %s to:"
9073                   prompt
9074                   (if (> (length articles) 1)
9075                       (format "these %d articles" (length articles))
9076                     "this article")))
9077          (to-newsgroup
9078           (cond
9079            ((null split-name)
9080             (gnus-completing-read default prom
9081                                   gnus-active-hashtb
9082                                   'gnus-valid-move-group-p
9083                                   nil prefix
9084                                   'gnus-group-history))
9085            ((= 1 (length split-name))
9086             (gnus-completing-read (car split-name) prom
9087                                   gnus-active-hashtb
9088                                   'gnus-valid-move-group-p
9089                                   nil nil
9090                                   'gnus-group-history))
9091            (t
9092             (gnus-completing-read nil prom
9093                                   (mapcar (lambda (el) (list el))
9094                                           (nreverse split-name))
9095                                   nil nil nil
9096                                   'gnus-group-history)))))
9097     (when to-newsgroup
9098       (if (or (string= to-newsgroup "")
9099               (string= to-newsgroup prefix))
9100           (setq to-newsgroup default))
9101       (unless to-newsgroup
9102         (error "No group name entered"))
9103       (or (gnus-active to-newsgroup)
9104           (gnus-activate-group to-newsgroup)
9105           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9106                                      to-newsgroup))
9107               (or (and (gnus-request-create-group
9108                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9109                        (gnus-activate-group to-newsgroup nil nil
9110                                             (gnus-group-name-to-method
9111                                              to-newsgroup)))
9112                   (error "Couldn't create group %s" to-newsgroup)))
9113           (error "No such group: %s" to-newsgroup)))
9114     to-newsgroup))
9115
9116 (defun gnus-summary-save-parts (type dir n reverse)
9117   "Save parts matching TYPE to DIR.
9118 If REVERSE, save parts that do not match TYPE."
9119   (interactive
9120    (list (read-string "Save parts of type: " "image/.*")
9121          (read-file-name "Save to directory: " t nil t)
9122          current-prefix-arg))
9123   (gnus-summary-iterate n
9124     (let ((gnus-display-mime-function nil)
9125           (gnus-inhibit-treatment t))
9126       (gnus-summary-select-article))
9127     (save-excursion
9128       (set-buffer gnus-article-buffer)
9129       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9130         (when handles
9131           (gnus-summary-save-parts-1 type dir handles reverse)
9132           (mm-destroy-parts handles))))))
9133
9134 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9135   (if (stringp (car handle))
9136       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9137               (cdr handle))
9138     (when (if reverse
9139               (not (string-match type (mm-handle-media-type handle)))
9140             (string-match type (mm-handle-media-type handle)))
9141       (let ((file (expand-file-name
9142                    (file-name-nondirectory
9143                     (or
9144                      (mail-content-type-get
9145                       (mm-handle-disposition handle) 'filename)
9146                      (concat gnus-newsgroup-name "." gnus-current-article)))
9147                    dir)))
9148         (unless (file-exists-p file)
9149           (mm-save-part-to-file handle file))))))
9150
9151 ;; Summary extract commands
9152
9153 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9154   (let ((buffer-read-only nil)
9155         (article (gnus-summary-article-number))
9156         after-article b e)
9157     (unless (gnus-summary-goto-subject article)
9158       (error "No such article: %d" article))
9159     (gnus-summary-position-point)
9160     ;; If all commands are to be bunched up on one line, we collect
9161     ;; them here.
9162     (unless gnus-view-pseudos-separately
9163       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9164             files action)
9165         (while ps
9166           (setq action (cdr (assq 'action (car ps))))
9167           (setq files (list (cdr (assq 'name (car ps)))))
9168           (while (and ps (cdr ps)
9169                       (string= (or action "1")
9170                                (or (cdr (assq 'action (cadr ps))) "2")))
9171             (push (cdr (assq 'name (cadr ps))) files)
9172             (setcdr ps (cddr ps)))
9173           (when files
9174             (when (not (string-match "%s" action))
9175               (push " " files))
9176             (push " " files)
9177             (when (assq 'execute (car ps))
9178               (setcdr (assq 'execute (car ps))
9179                       (funcall (if (string-match "%s" action)
9180                                    'format 'concat)
9181                                action
9182                                (mapconcat
9183                                 (lambda (f)
9184                                   (if (equal f " ")
9185                                       f
9186                                     (gnus-quote-arg-for-sh-or-csh f)))
9187                                 files " ")))))
9188           (setq ps (cdr ps)))))
9189     (if (and gnus-view-pseudos (not not-view))
9190         (while pslist
9191           (when (assq 'execute (car pslist))
9192             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9193                                   (eq gnus-view-pseudos 'not-confirm)))
9194           (setq pslist (cdr pslist)))
9195       (save-excursion
9196         (while pslist
9197           (setq after-article (or (cdr (assq 'article (car pslist)))
9198                                   (gnus-summary-article-number)))
9199           (gnus-summary-goto-subject after-article)
9200           (forward-line 1)
9201           (setq b (point))
9202           (insert "    " (file-name-nondirectory
9203                           (cdr (assq 'name (car pslist))))
9204                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9205           (setq e (point))
9206           (forward-line -1)             ; back to `b'
9207           (gnus-add-text-properties
9208            b (1- e) (list 'gnus-number gnus-reffed-article-number
9209                           gnus-mouse-face-prop gnus-mouse-face))
9210           (gnus-data-enter
9211            after-article gnus-reffed-article-number
9212            gnus-unread-mark b (car pslist) 0 (- e b))
9213           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9214           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9215           (setq pslist (cdr pslist)))))))
9216
9217 (defun gnus-pseudos< (p1 p2)
9218   (let ((c1 (cdr (assq 'action p1)))
9219         (c2 (cdr (assq 'action p2))))
9220     (and c1 c2 (string< c1 c2))))
9221
9222 (defun gnus-request-pseudo-article (props)
9223   (cond ((assq 'execute props)
9224          (gnus-execute-command (cdr (assq 'execute props)))))
9225   (let ((gnus-current-article (gnus-summary-article-number)))
9226     (gnus-run-hooks 'gnus-mark-article-hook)))
9227
9228 (defun gnus-execute-command (command &optional automatic)
9229   (save-excursion
9230     (gnus-article-setup-buffer)
9231     (set-buffer gnus-article-buffer)
9232     (setq buffer-read-only nil)
9233     (let ((command (if automatic command
9234                      (read-string "Command: " (cons command 0)))))
9235       (erase-buffer)
9236       (insert "$ " command "\n\n")
9237       (if gnus-view-pseudo-asynchronously
9238           (start-process "gnus-execute" (current-buffer) shell-file-name
9239                          shell-command-switch command)
9240         (call-process shell-file-name nil t nil
9241                       shell-command-switch command)))))
9242
9243 ;; Summary kill commands.
9244
9245 (defun gnus-summary-edit-global-kill (article)
9246   "Edit the \"global\" kill file."
9247   (interactive (list (gnus-summary-article-number)))
9248   (gnus-group-edit-global-kill article))
9249
9250 (defun gnus-summary-edit-local-kill ()
9251   "Edit a local kill file applied to the current newsgroup."
9252   (interactive)
9253   (setq gnus-current-headers (gnus-summary-article-header))
9254   (gnus-group-edit-local-kill
9255    (gnus-summary-article-number) gnus-newsgroup-name))
9256
9257 ;;; Header reading.
9258
9259 (defun gnus-read-header (id &optional header)
9260   "Read the headers of article ID and enter them into the Gnus system."
9261   (let ((group gnus-newsgroup-name)
9262         (gnus-override-method
9263          (and (gnus-news-group-p gnus-newsgroup-name)
9264               gnus-refer-article-method))
9265         where)
9266     ;; First we check to see whether the header in question is already
9267     ;; fetched.
9268     (if (stringp id)
9269         ;; This is a Message-ID.
9270         (setq header (or header (gnus-id-to-header id)))
9271       ;; This is an article number.
9272       (setq header (or header (gnus-summary-article-header id))))
9273     (if (and header
9274              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9275         ;; We have found the header.
9276         header
9277       ;; If this is a sparse article, we have to nix out its
9278       ;; previous entry in the thread hashtb.
9279       (when (and header
9280                  (gnus-summary-article-sparse-p (mail-header-number header)))
9281         (let* ((parent (gnus-parent-id (mail-header-references header)))
9282                (thread (and parent (gnus-id-to-thread parent))))
9283           (when thread
9284             (delq (assq header thread) thread))))
9285       ;; We have to really fetch the header to this article.
9286       (save-excursion
9287         (set-buffer nntp-server-buffer)
9288         (when (setq where (gnus-request-head id group))
9289           (nnheader-fold-continuation-lines)
9290           (goto-char (point-max))
9291           (insert ".\n")
9292           (goto-char (point-min))
9293           (insert "211 ")
9294           (princ (cond
9295                   ((numberp id) id)
9296                   ((cdr where) (cdr where))
9297                   (header (mail-header-number header))
9298                   (t gnus-reffed-article-number))
9299                  (current-buffer))
9300           (insert " Article retrieved.\n"))
9301         (if (or (not where)
9302                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9303             ()                          ; Malformed head.
9304           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9305             (when (and (stringp id)
9306                        (not (string= (gnus-group-real-name group)
9307                                      (car where))))
9308               ;; If we fetched by Message-ID and the article came
9309               ;; from a different group, we fudge some bogus article
9310               ;; numbers for this article.
9311               (mail-header-set-number header gnus-reffed-article-number))
9312             (save-excursion
9313               (set-buffer gnus-summary-buffer)
9314               (decf gnus-reffed-article-number)
9315               (gnus-remove-header (mail-header-number header))
9316               (push header gnus-newsgroup-headers)
9317               (setq gnus-current-headers header)
9318               (push (mail-header-number header) gnus-newsgroup-limit)))
9319           header)))))
9320
9321 (defun gnus-remove-header (number)
9322   "Remove header NUMBER from `gnus-newsgroup-headers'."
9323   (if (and gnus-newsgroup-headers
9324            (= number (mail-header-number (car gnus-newsgroup-headers))))
9325       (pop gnus-newsgroup-headers)
9326     (let ((headers gnus-newsgroup-headers))
9327       (while (and (cdr headers)
9328                   (not (= number (mail-header-number (cadr headers)))))
9329         (pop headers))
9330       (when (cdr headers)
9331         (setcdr headers (cddr headers))))))
9332
9333 ;;;
9334 ;;; summary highlights
9335 ;;;
9336
9337 (defun gnus-highlight-selected-summary ()
9338   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9339   ;; Highlight selected article in summary buffer
9340   (when gnus-summary-selected-face
9341     (save-excursion
9342       (let* ((beg (progn (beginning-of-line) (point)))
9343              (end (progn (end-of-line) (point)))
9344              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9345              (from (if (get-text-property beg gnus-mouse-face-prop)
9346                        beg
9347                      (or (next-single-property-change
9348                           beg gnus-mouse-face-prop nil end)
9349                          beg)))
9350              (to
9351               (if (= from end)
9352                   (- from 2)
9353                 (or (next-single-property-change
9354                      from gnus-mouse-face-prop nil end)
9355                     end))))
9356         ;; If no mouse-face prop on line we will have to = from = end,
9357         ;; so we highlight the entire line instead.
9358         (when (= (+ to 2) from)
9359           (setq from beg)
9360           (setq to end))
9361         (if gnus-newsgroup-selected-overlay
9362             ;; Move old overlay.
9363             (gnus-move-overlay
9364              gnus-newsgroup-selected-overlay from to (current-buffer))
9365           ;; Create new overlay.
9366           (gnus-overlay-put
9367            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9368            'face gnus-summary-selected-face))))))
9369
9370 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9371 (defun gnus-summary-highlight-line ()
9372   "Highlight current line according to `gnus-summary-highlight'."
9373   (let* ((list gnus-summary-highlight)
9374          (p (point))
9375          (end (progn (end-of-line) (point)))
9376          ;; now find out where the line starts and leave point there.
9377          (beg (progn (beginning-of-line) (point)))
9378          (article (gnus-summary-article-number))
9379          (score (or (cdr (assq (or article gnus-current-article)
9380                                gnus-newsgroup-scored))
9381                     gnus-summary-default-score 0))
9382          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9383          (inhibit-read-only t))
9384     ;; Eval the cars of the lists until we find a match.
9385     (let ((default gnus-summary-default-score))
9386       (while (and list
9387                   (not (eval (caar list))))
9388         (setq list (cdr list))))
9389     (let ((face (cdar list)))
9390       (unless (eq face (get-text-property beg 'face))
9391         (gnus-put-text-property-excluding-characters-with-faces
9392          beg end 'face
9393          (setq face (if (boundp face) (symbol-value face) face)))
9394         (when gnus-summary-highlight-line-function
9395           (funcall gnus-summary-highlight-line-function article face))))
9396     (goto-char p)))
9397
9398 (defun gnus-update-read-articles (group unread &optional compute)
9399   "Update the list of read articles in GROUP."
9400   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9401          (entry (gnus-gethash group gnus-newsrc-hashtb))
9402          (info (nth 2 entry))
9403          (prev 1)
9404          (unread (sort (copy-sequence unread) '<))
9405          read)
9406     (if (or (not info) (not active))
9407         ;; There is no info on this group if it was, in fact,
9408         ;; killed.  Gnus stores no information on killed groups, so
9409         ;; there's nothing to be done.
9410         ;; One could store the information somewhere temporarily,
9411         ;; perhaps...  Hmmm...
9412         ()
9413       ;; Remove any negative articles numbers.
9414       (while (and unread (< (car unread) 0))
9415         (setq unread (cdr unread)))
9416       ;; Remove any expired article numbers
9417       (while (and unread (< (car unread) (car active)))
9418         (setq unread (cdr unread)))
9419       ;; Compute the ranges of read articles by looking at the list of
9420       ;; unread articles.
9421       (while unread
9422         (when (/= (car unread) prev)
9423           (push (if (= prev (1- (car unread))) prev
9424                   (cons prev (1- (car unread))))
9425                 read))
9426         (setq prev (1+ (car unread)))
9427         (setq unread (cdr unread)))
9428       (when (<= prev (cdr active))
9429         (push (cons prev (cdr active)) read))
9430       (setq read (if (> (length read) 1) (nreverse read) read))
9431       (if compute
9432           read
9433         (save-excursion
9434           (set-buffer gnus-group-buffer)
9435           (gnus-undo-register
9436             `(progn
9437                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9438                (gnus-info-set-read ',info ',(gnus-info-read info))
9439                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9440                (gnus-group-update-group ,group t))))
9441        ;; Propagate the read marks to the backend.
9442        (if (gnus-check-backend-function 'request-set-mark group)
9443            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9444                  (add (gnus-remove-from-range read (gnus-info-read info))))
9445              (when (or add del)
9446                (unless (gnus-check-group group)
9447                  (error "Can't open server for %s" group))
9448                (gnus-request-set-mark
9449                 group (delq nil (list (if add (list add 'add '(read)))
9450                                       (if del (list del 'del '(read)))))))))
9451         ;; Enter this list into the group info.
9452         (gnus-info-set-read info read)
9453         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9454         (gnus-get-unread-articles-in-group info (gnus-active group))
9455         t))))
9456
9457 (defun gnus-offer-save-summaries ()
9458   "Offer to save all active summary buffers."
9459   (save-excursion
9460     (let ((buflist (buffer-list))
9461           buffers bufname)
9462       ;; Go through all buffers and find all summaries.
9463       (while buflist
9464         (and (setq bufname (buffer-name (car buflist)))
9465              (string-match "Summary" bufname)
9466              (save-excursion
9467                (set-buffer bufname)
9468                ;; We check that this is, indeed, a summary buffer.
9469                (and (eq major-mode 'gnus-summary-mode)
9470                     ;; Also make sure this isn't bogus.
9471                     gnus-newsgroup-prepared
9472                     ;; Also make sure that this isn't a dead summary buffer.
9473                     (not gnus-dead-summary-mode)))
9474              (push bufname buffers))
9475         (setq buflist (cdr buflist)))
9476       ;; Go through all these summary buffers and offer to save them.
9477       (when buffers
9478         (map-y-or-n-p
9479          "Update summary buffer %s? "
9480          (lambda (buf)
9481            (switch-to-buffer buf)
9482            (gnus-summary-exit))
9483          buffers)))))
9484
9485
9486 ;;; @ for mime-partial
9487 ;;;
9488
9489 (defun gnus-request-partial-message ()
9490   (save-excursion
9491     (let ((number (gnus-summary-article-number))
9492           (group gnus-newsgroup-name)
9493           (mother gnus-article-buffer))
9494       (set-buffer (get-buffer-create " *Partial Article*"))
9495       (erase-buffer)
9496       (setq mime-preview-buffer mother)
9497       (gnus-request-article-this-buffer number group)
9498       (mime-parse-buffer)
9499       )))
9500
9501 (autoload 'mime-combine-message/partial-pieces-automatically
9502   "mime-partial"
9503   "Internal method to combine message/partial messages automatically.")
9504
9505 (mime-add-condition
9506  'action '((type . message)(subtype . partial)
9507            (major-mode . gnus-original-article-mode)
9508            (method . mime-combine-message/partial-pieces-automatically)
9509            (summary-buffer-exp . gnus-summary-buffer)
9510            (request-partial-message-method . gnus-request-partial-message)
9511            ))
9512
9513
9514 ;;; @ for message/rfc822
9515 ;;;
9516
9517 (defun gnus-mime-extract-message/rfc822 (entity situation)
9518   (let (group article num cwin swin cur)
9519     (with-current-buffer (mime-entity-buffer entity)
9520       (save-restriction
9521         (narrow-to-region (mime-entity-body-start entity)
9522                           (mime-entity-body-end entity))
9523         (setq group (or (cdr (assq 'group situation))
9524                         (completing-read "Group: "
9525                                          gnus-active-hashtb
9526                                          nil
9527                                          (gnus-read-active-file-p)
9528                                          gnus-newsgroup-name))
9529               article (gnus-request-accept-article group)
9530               )
9531         ))
9532     (when (and (consp article)
9533                (numberp (setq article (cdr article))))
9534       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9535             cwin (get-buffer-window (current-buffer) t)
9536             )
9537       (save-window-excursion
9538         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9539             (select-window swin)
9540           (set-buffer gnus-summary-buffer)
9541           )
9542         (setq cur gnus-current-article)
9543         (forward-line num)
9544         (let (gnus-show-threads)
9545           (gnus-summary-goto-subject article t)
9546           )
9547         (gnus-summary-clear-mark-forward 1)
9548         (gnus-summary-goto-subject cur)
9549         )
9550       (when (and cwin (window-frame cwin))
9551         (select-frame (window-frame cwin))
9552         )
9553       (when (boundp 'mime-acting-situation-to-override)
9554         (set-alist 'mime-acting-situation-to-override
9555                    'group
9556                    group)
9557         (set-alist 'mime-acting-situation-to-override
9558                    'after-method
9559                    `(progn
9560                       (save-current-buffer
9561                         (set-buffer gnus-group-buffer)
9562                         (gnus-activate-group ,group)
9563                         )
9564                       (gnus-summary-goto-article ,cur
9565                                                  gnus-show-all-headers)
9566                       ))
9567         (set-alist 'mime-acting-situation-to-override
9568                    'number num)
9569         )
9570       )))
9571
9572 (mime-add-condition
9573  'action '((type . message)(subtype . rfc822)
9574            (major-mode . gnus-original-article-mode)
9575            (method . gnus-mime-extract-message/rfc822)
9576            (mode . "extract")
9577            ))
9578
9579 (mime-add-condition
9580  'action '((type . message)(subtype . news)
9581            (major-mode . gnus-original-article-mode)
9582            (method . gnus-mime-extract-message/rfc822)
9583            (mode . "extract")
9584            ))
9585
9586 (defun gnus-mime-extract-multipart (entity situation)
9587   (let ((children (mime-entity-children entity))
9588         mime-acting-situation-to-override
9589         f)
9590     (while children
9591       (mime-play-entity (car children)
9592                         (cons (assq 'mode situation)
9593                               mime-acting-situation-to-override))
9594       (setq children (cdr children)))
9595     (if (setq f (cdr (assq 'after-method
9596                            mime-acting-situation-to-override)))
9597         (eval f)
9598       )))  
9599
9600 (mime-add-condition
9601  'action '((type . multipart)
9602            (method . gnus-mime-extract-multipart)
9603            (mode . "extract")
9604            )
9605  'with-default)
9606
9607
9608 ;;; @ end
9609 ;;;
9610
9611 (defun gnus-summary-setup-default-charset ()
9612   "Setup newsgroup default charset."
9613   (if (equal gnus-newsgroup-name "nndraft:drafts")
9614       (setq gnus-newsgroup-charset nil)
9615   (let* ((name (and gnus-newsgroup-name
9616                    (gnus-group-real-name gnus-newsgroup-name)))
9617          (ignored-charsets 
9618           (or gnus-newsgroup-ephemeral-ignored-charsets
9619               (append
9620                (and gnus-newsgroup-name
9621                     (or (gnus-group-find-parameter gnus-newsgroup-name
9622                                                    'ignored-charsets t)
9623                         (let ((alist gnus-group-ignored-charsets-alist)
9624                            elem (charsets nil))
9625                           (while (setq elem (pop alist))
9626                             (when (and name
9627                                        (string-match (car elem) name))
9628                               (setq alist nil
9629                                     charsets (cdr elem))))
9630                           charsets))))
9631               gnus-newsgroup-ignored-charsets)))
9632     (setq gnus-newsgroup-charset
9633           (or gnus-newsgroup-ephemeral-charset
9634               (and gnus-newsgroup-name
9635                    (or (gnus-group-find-parameter gnus-newsgroup-name
9636                                                   'charset)
9637                        (let ((alist gnus-group-charset-alist)
9638                              elem (charset nil))
9639                          (while (setq elem (pop alist))
9640                            (when (and name
9641                                       (string-match (car elem) name))
9642                              (setq alist nil
9643                                    charset (cadr elem))))
9644                          charset)))
9645               gnus-default-charset))
9646     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9647          ignored-charsets))))
9648
9649 ;;;
9650 ;;; Mime Commands
9651 ;;;
9652
9653 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9654   "Display the current article buffer fully MIME-buttonized.
9655 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9656 treated as multipart/mixed."
9657   (interactive "P")
9658   (require 'gnus-art)
9659   (let ((gnus-unbuttonized-mime-types nil)
9660         (gnus-mime-display-multipart-as-mixed show-all-parts))
9661     (gnus-summary-show-article)))
9662
9663 (defun gnus-summary-repair-multipart (article)
9664   "Add a Content-Type header to a multipart article without one."
9665   (interactive (list (gnus-summary-article-number)))
9666   (gnus-with-article article
9667     (message-narrow-to-head)
9668     (goto-char (point-max))
9669     (widen)
9670     (when (search-forward "\n--" nil t)
9671       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9672         (message-narrow-to-head)
9673         (message-remove-header "Mime-Version")
9674         (message-remove-header "Content-Type")
9675         (goto-char (point-max))
9676         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9677                         separator))
9678         (insert "Mime-Version: 1.0\n")
9679         (widen))))
9680   (let (gnus-mark-article-hook)
9681     (gnus-summary-select-article t t nil article)))
9682
9683 (defun gnus-summary-toggle-display-buttonized ()
9684   "Toggle the buttonizing of the article buffer."
9685   (interactive)
9686   (require 'gnus-art)
9687   (if (setq gnus-inhibit-mime-unbuttonizing
9688             (not gnus-inhibit-mime-unbuttonizing))
9689       (let ((gnus-unbuttonized-mime-types nil))
9690         (gnus-summary-show-article))
9691     (gnus-summary-show-article)))
9692
9693 ;;;
9694 ;;; Intelli-mouse commmands
9695 ;;;
9696
9697 (defun gnus-wheel-summary-scroll (event)
9698   (interactive "e")
9699   (let ((amount (if (memq 'shift (event-modifiers event))
9700                     (car gnus-wheel-scroll-amount)
9701                   (cdr gnus-wheel-scroll-amount)))
9702         (direction (- (* (static-if (featurep 'xemacs)
9703                              (event-button event)
9704                            (cond ((eq 'mouse-4 (event-basic-type event))
9705                                   4)
9706                                  ((eq 'mouse-5 (event-basic-type event))
9707                                   5)))
9708                          2) 9))
9709         edge)
9710     (gnus-summary-scroll-up (* amount direction))
9711     (when (gnus-eval-in-buffer-window gnus-article-buffer
9712             (save-restriction
9713               (widen)
9714               (and (if (< 0 direction)
9715                        (gnus-article-next-page 0)
9716                      (gnus-article-prev-page 0)
9717                      (bobp))
9718                    (if (setq edge (get-text-property
9719                                    (point-min) 'gnus-wheel-edge))
9720                        (setq edge (* edge direction))
9721                      (setq edge -1))
9722                    (or (plusp edge)
9723                        (let ((buffer-read-only nil)
9724                              (inhibit-read-only t))
9725                          (put-text-property (point-min) (point-max)
9726                                             'gnus-wheel-edge direction)
9727                          nil))
9728                    (or (> edge gnus-wheel-edge-resistance)
9729                        (let ((buffer-read-only nil)
9730                              (inhibit-read-only t))
9731                          (put-text-property (point-min) (point-max)
9732                                             'gnus-wheel-edge
9733                                             (* (1+ edge) direction))
9734                          nil))
9735                    (eq last-command 'gnus-wheel-summary-scroll))
9736               ))
9737       (gnus-summary-next-article nil nil (minusp direction)))
9738     ))
9739
9740 (defun gnus-wheel-install ()
9741   "Enable mouse wheel support on summary window."
9742   (when gnus-use-wheel
9743     (let ((keys 
9744            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9745       (dolist (key keys)
9746         (define-key gnus-summary-mode-map key
9747           'gnus-wheel-summary-scroll)))))
9748
9749 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9750
9751 ;;;
9752 ;;; with article
9753 ;;;
9754
9755 (defmacro gnus-with-article (article &rest forms)
9756   "Select ARTICLE and perform FORMS in the original article buffer.
9757 Then replace the article with the result."
9758   `(progn
9759      ;; We don't want the article to be marked as read.
9760      (let (gnus-mark-article-hook)
9761        (gnus-summary-select-article t t nil ,article))
9762      (set-buffer gnus-original-article-buffer)
9763      ,@forms
9764      (if (not (gnus-check-backend-function
9765                'request-replace-article (car gnus-article-current)))
9766          (gnus-message 5 "Read-only group; not replacing")
9767        (unless (gnus-request-replace-article
9768                 ,article (car gnus-article-current)
9769                 (current-buffer) t)
9770          (error "Couldn't replace article")))
9771      ;; The cache and backlog have to be flushed somewhat.
9772      (when gnus-keep-backlog
9773        (gnus-backlog-remove-article
9774         (car gnus-article-current) (cdr gnus-article-current)))
9775      (when gnus-use-cache
9776        (gnus-cache-update-article
9777         (car gnus-article-current) (cdr gnus-article-current)))))
9778
9779 (put 'gnus-with-article 'lisp-indent-function 1)
9780 (put 'gnus-with-article 'edebug-form-spec '(form body))
9781
9782 ;;;
9783 ;;; Generic summary marking commands
9784 ;;;
9785
9786 (defvar gnus-summary-marking-alist
9787   '((read gnus-del-mark "d")
9788     (unread gnus-unread-mark "u")
9789     (ticked gnus-ticked-mark "!")
9790     (dormant gnus-dormant-mark "?")
9791     (expirable gnus-expirable-mark "e"))
9792   "An alist of names/marks/keystrokes.")
9793
9794 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9795 (defvar gnus-summary-mark-map)
9796
9797 (defun gnus-summary-make-all-marking-commands ()
9798   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9799   (dolist (elem gnus-summary-marking-alist)
9800     (apply 'gnus-summary-make-marking-command elem)))
9801
9802 (defun gnus-summary-make-marking-command (name mark keystroke)
9803   (let ((map (make-sparse-keymap)))
9804     (define-key gnus-summary-generic-mark-map keystroke map)
9805     (dolist (lway `((next "next" next nil "n")
9806                     (next-unread "next unread" next t "N")
9807                     (prev "previous" prev nil "p")
9808                     (prev-unread "previous unread" prev t "P")
9809                     (nomove "" nil nil ,keystroke)))
9810       (let ((func (gnus-summary-make-marking-command-1
9811                    mark (car lway) lway name)))
9812         (setq func (eval func))
9813         (define-key map (nth 4 lway) func)))))
9814       
9815 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9816   `(defun ,(intern
9817             (format "gnus-summary-put-mark-as-%s%s"
9818                     name (if (eq way 'nomove)
9819                              ""
9820                            (concat "-" (symbol-name way)))))
9821      (n)
9822      ,(format
9823        "Mark the current article as %s%s.
9824 If N, the prefix, then repeat N times.
9825 If N is negative, move in reverse order.
9826 The difference between N and the actual number of articles marked is
9827 returned."
9828        name (cadr lway))
9829      (interactive "p")
9830      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9831     
9832 (defun gnus-summary-generic-mark (n mark move unread)
9833   "Mark N articles with MARK."
9834   (unless (eq major-mode 'gnus-summary-mode)
9835     (error "This command can only be used in the summary buffer"))
9836   (gnus-summary-show-thread)
9837   (let ((nummove
9838          (cond
9839           ((eq move 'next) 1)
9840           ((eq move 'prev) -1)
9841           (t 0))))
9842     (if (zerop nummove)
9843         (setq n 1)
9844       (when (< n 0)
9845         (setq n (abs n)
9846               nummove (* -1 nummove))))
9847     (while (and (> n 0)
9848                 (gnus-summary-mark-article nil mark)
9849                 (zerop (gnus-summary-next-subject nummove unread t)))
9850       (setq n (1- n)))
9851     (when (/= 0 n)
9852       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9853     (gnus-summary-recenter)
9854     (gnus-summary-position-point)
9855     (gnus-set-mode-line 'summary)
9856     n))
9857
9858 (gnus-summary-make-all-marking-commands)
9859
9860 (gnus-ems-redefine)
9861
9862 (provide 'gnus-sum)
9863
9864 (run-hooks 'gnus-sum-load-hook)
9865
9866 ;;; gnus-sum.el ends here