Save save-parts history.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40
41 (defcustom gnus-kill-summary-on-exit t
42   "*If non-nil, kill the summary buffer when you exit from it.
43 If nil, the summary will become a \"*Dead Summary*\" buffer, and
44 it will be killed sometime later."
45   :group 'gnus-summary-exit
46   :type 'boolean)
47
48 (defcustom gnus-fetch-old-headers nil
49   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
50 If an unread article in the group refers to an older, already read (or
51 just marked as read) article, the old article will not normally be
52 displayed in the Summary buffer.  If this variable is non-nil, Gnus
53 will attempt to grab the headers to the old articles, and thereby
54 build complete threads.  If it has the value `some', only enough
55 headers to connect otherwise loose threads will be displayed.  This
56 variable can also be a number.  In that case, no more than that number
57 of old headers will be fetched.  If it has the value `invisible', all
58 old headers will be fetched, but none will be displayed.
59
60 The server has to support NOV for any of this to work."
61   :group 'gnus-thread
62   :type '(choice (const :tag "off" nil)
63                  (const some)
64                  number
65                  (sexp :menu-tag "other" t)))
66
67 (defcustom gnus-refer-thread-limit 200
68   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
69 If t, fetch all the available old headers."
70   :group 'gnus-thread
71   :type '(choice number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-summary-make-false-root 'adopt
75   "*nil means that Gnus won't gather loose threads.
76 If the root of a thread has expired or been read in a previous
77 session, the information necessary to build a complete thread has been
78 lost.  Instead of having many small sub-threads from this original thread
79 scattered all over the summary buffer, Gnus can gather them.
80
81 If non-nil, Gnus will try to gather all loose sub-threads from an
82 original thread into one large thread.
83
84 If this variable is non-nil, it should be one of `none', `adopt',
85 `dummy' or `empty'.
86
87 If this variable is `none', Gnus will not make a false root, but just
88 present the sub-threads after another.
89 If this variable is `dummy', Gnus will create a dummy root that will
90 have all the sub-threads as children.
91 If this variable is `adopt', Gnus will make one of the \"children\"
92 the parent and mark all the step-children as such.
93 If this variable is `empty', the \"children\" are printed with empty
94 subject fields.  (Or rather, they will be printed with a string
95 given by the `gnus-summary-same-subject' variable.)"
96   :group 'gnus-thread
97   :type '(choice (const :tag "off" nil)
98                  (const none)
99                  (const dummy)
100                  (const adopt)
101                  (const empty)))
102
103 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
104   "*A regexp to match subjects to be excluded from loose thread gathering.
105 As loose thread gathering is done on subjects only, that means that
106 there can be many false gatherings performed.  By rooting out certain
107 common subjects, gathering might become saner."
108   :group 'gnus-thread
109   :type 'regexp)
110
111 (defcustom gnus-summary-gather-subject-limit nil
112   "*Maximum length of subject comparisons when gathering loose threads.
113 Use nil to compare full subjects.  Setting this variable to a low
114 number will help gather threads that have been corrupted by
115 newsreaders chopping off subject lines, but it might also mean that
116 unrelated articles that have subject that happen to begin with the
117 same few characters will be incorrectly gathered.
118
119 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
120 comparing subjects."
121   :group 'gnus-thread
122   :type '(choice (const :tag "off" nil)
123                  (const fuzzy)
124                  (sexp :menu-tag "on" t)))
125
126 (defcustom gnus-simplify-subject-functions nil
127   "List of functions taking a string argument that simplify subjects.
128 The functions are applied recursively.
129
130 Useful functions to put in this list include: `gnus-simplify-subject-re',
131 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
132   :group 'gnus-thread
133   :type '(repeat function))
134
135 (defcustom gnus-simplify-ignored-prefixes nil
136   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
137   :group 'gnus-thread
138   :type '(choice (const :tag "off" nil)
139                  regexp))
140
141 (defcustom gnus-build-sparse-threads nil
142   "*If non-nil, fill in the gaps in threads.
143 If `some', only fill in the gaps that are needed to tie loose threads
144 together.  If `more', fill in all leaf nodes that Gnus can find.  If
145 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  (const some)
149                  (const more)
150                  (sexp :menu-tag "all" t)))
151
152 (defcustom gnus-summary-thread-gathering-function
153   'gnus-gather-threads-by-subject
154   "*Function used for gathering loose threads.
155 There are two pre-defined functions: `gnus-gather-threads-by-subject',
156 which only takes Subjects into consideration; and
157 `gnus-gather-threads-by-references', which compared the References
158 headers of the articles to find matches."
159   :group 'gnus-thread
160   :type '(radio (function-item gnus-gather-threads-by-subject)
161                 (function-item gnus-gather-threads-by-references)
162                 (function :tag "other")))
163
164 (defcustom gnus-summary-same-subject ""
165   "*String indicating that the current article has the same subject as the previous.
166 This variable will only be used if the value of
167 `gnus-summary-make-false-root' is `empty'."
168   :group 'gnus-summary-format
169   :type 'string)
170
171 (defcustom gnus-summary-goto-unread t
172   "*If t, many commands will go to the next unread article.
173 This applies to marking commands as well as other commands that
174 \"naturally\" select the next article, like, for instance, `SPC' at
175 the end of an article.
176
177 If nil, the marking commands do NOT go to the next unread article
178 (they go to the next article instead).  If `never', commands that
179 usually go to the next unread article, will go to the next article,
180 whether it is read or not."
181   :group 'gnus-summary-marks
182   :link '(custom-manual "(gnus)Setting Marks")
183   :type '(choice (const :tag "off" nil)
184                  (const never)
185                  (sexp :menu-tag "on" t)))
186
187 (defcustom gnus-summary-default-score 0
188   "*Default article score level.
189 All scores generated by the score files will be added to this score.
190 If this variable is nil, scoring will be disabled."
191   :group 'gnus-score-default
192   :type '(choice (const :tag "disable")
193                  integer))
194
195 (defcustom gnus-summary-zcore-fuzz 0
196   "*Fuzziness factor for the zcore in the summary buffer.
197 Articles with scores closer than this to `gnus-summary-default-score'
198 will not be marked."
199   :group 'gnus-summary-format
200   :type 'integer)
201
202 (defcustom gnus-simplify-subject-fuzzy-regexp nil
203   "*Strings to be removed when doing fuzzy matches.
204 This can either be a regular expression or list of regular expressions
205 that will be removed from subject strings if fuzzy subject
206 simplification is selected."
207   :group 'gnus-thread
208   :type '(repeat regexp))
209
210 (defcustom gnus-show-threads t
211   "*If non-nil, display threads in summary mode."
212   :group 'gnus-thread
213   :type 'boolean)
214
215 (defcustom gnus-thread-hide-subtree nil
216   "*If non-nil, hide all threads initially.
217 If threads are hidden, you have to run the command
218 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
219 to expose hidden threads."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-killed t
224   "*If non-nil, hide killed threads automatically."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-ignore-subject t
229   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
230 If nil, articles that have different subjects from their parents will
231 start separate threads."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-operation-ignore-subject t
236   "*If non-nil, subjects will be ignored when doing thread commands.
237 This affects commands like `gnus-summary-kill-thread' and
238 `gnus-summary-lower-thread'.
239
240 If this variable is nil, articles in the same thread with different
241 subjects will not be included in the operation in question.  If this
242 variable is `fuzzy', only articles that have subjects that are fuzzily
243 equal will be included."
244   :group 'gnus-thread
245   :type '(choice (const :tag "off" nil)
246                  (const fuzzy)
247                  (sexp :tag "on" t)))
248
249 (defcustom gnus-thread-indent-level 4
250   "*Number that says how much each sub-thread should be indented."
251   :group 'gnus-thread
252   :type 'integer)
253
254 (defcustom gnus-auto-extend-newsgroup t
255   "*If non-nil, extend newsgroup forward and backward when requested."
256   :group 'gnus-summary-choose
257   :type 'boolean)
258
259 (defcustom gnus-auto-select-first t
260   "*If nil, don't select the first unread article when entering a group.
261 If this variable is `best', select the highest-scored unread article
262 in the group.  If t, select the first unread article.
263
264 This variable can also be a function to place point on a likely
265 subject line.  Useful values include `gnus-summary-first-unread-subject',
266 `gnus-summary-first-unread-article' and
267 `gnus-summary-best-unread-article'.
268
269 If you want to prevent automatic selection of the first unread article
270 in some newsgroups, set the variable to nil in
271 `gnus-select-group-hook'."
272   :group 'gnus-group-select
273   :type '(choice (const :tag "none" nil)
274                  (const best)
275                  (sexp :menu-tag "first" t)
276                  (function-item gnus-summary-first-unread-subject)
277                  (function-item gnus-summary-first-unread-article)
278                  (function-item gnus-summary-best-unread-article)))
279
280 (defcustom gnus-auto-select-next t
281   "*If non-nil, offer to go to the next group from the end of the previous.
282 If the value is t and the next newsgroup is empty, Gnus will exit
283 summary mode and go back to group mode.  If the value is neither nil
284 nor t, Gnus will select the following unread newsgroup.  In
285 particular, if the value is the symbol `quietly', the next unread
286 newsgroup will be selected without any confirmation, and if it is
287 `almost-quietly', the next group will be selected without any
288 confirmation if you are located on the last article in the group.
289 Finally, if this variable is `slightly-quietly', the `Z n' command
290 will go to the next group without confirmation."
291   :group 'gnus-summary-maneuvering
292   :type '(choice (const :tag "off" nil)
293                  (const quietly)
294                  (const almost-quietly)
295                  (const slightly-quietly)
296                  (sexp :menu-tag "on" t)))
297
298 (defcustom gnus-auto-select-same nil
299   "*If non-nil, select the next article with the same subject.
300 If there are no more articles with the same subject, go to
301 the first unread article."
302   :group 'gnus-summary-maneuvering
303   :type 'boolean)
304
305 (defcustom gnus-summary-check-current nil
306   "*If non-nil, consider the current article when moving.
307 The \"unread\" movement commands will stay on the same line if the
308 current article is unread."
309   :group 'gnus-summary-maneuvering
310   :type 'boolean)
311
312 (defcustom gnus-auto-center-summary t
313   "*If non-nil, always center the current summary buffer.
314 In particular, if `vertical' do only vertical recentering.  If non-nil
315 and non-`vertical', do both horizontal and vertical recentering."
316   :group 'gnus-summary-maneuvering
317   :type '(choice (const :tag "none" nil)
318                  (const vertical)
319                  (integer :tag "height")
320                  (sexp :menu-tag "both" t)))
321
322 (defcustom gnus-show-all-headers nil
323   "*If non-nil, don't hide any headers."
324   :group 'gnus-article-hiding
325   :group 'gnus-article-headers
326   :type 'boolean)
327
328 (defcustom gnus-summary-ignore-duplicates nil
329   "*If non-nil, ignore articles with identical Message-ID headers."
330   :group 'gnus-summary
331   :type 'boolean)
332
333 (defcustom gnus-single-article-buffer t
334   "*If non-nil, display all articles in the same buffer.
335 If nil, each group will get its own article buffer."
336   :group 'gnus-article-various
337   :type 'boolean)
338
339 (defcustom gnus-break-pages t
340   "*If non-nil, do page breaking on articles.
341 The page delimiter is specified by the `gnus-page-delimiter'
342 variable."
343   :group 'gnus-article-various
344   :type 'boolean)
345
346 (defcustom gnus-move-split-methods nil
347   "*Variable used to suggest where articles are to be moved to.
348 It uses the same syntax as the `gnus-split-methods' variable."
349   :group 'gnus-summary-mail
350   :type '(repeat (choice (list :value (fun) function)
351                          (cons :value ("" "") regexp (repeat string))
352                          (sexp :value nil))))
353
354 (defcustom gnus-unread-mark ?  ;Whitespace
355   "*Mark used for unread articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-ticked-mark ?!
360   "*Mark used for ticked articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-dormant-mark ??
365   "*Mark used for dormant articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-del-mark ?r
370   "*Mark used for del'd articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-read-mark ?R
375   "*Mark used for read articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-expirable-mark ?E
380   "*Mark used for expirable articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-killed-mark ?K
385   "*Mark used for killed articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-souped-mark ?F
390   "*Mark used for killed articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-kill-file-mark ?X
395   "*Mark used for articles killed by kill files."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-low-score-mark ?Y
400   "*Mark used for articles with a low score."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-catchup-mark ?C
405   "*Mark used for articles that are caught up."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-replied-mark ?A
410   "*Mark used for articles that have been replied to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-cached-mark ?*
415   "*Mark used for articles that are in the cache."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-saved-mark ?S
420   "*Mark used for articles that have been saved to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ancient-mark ?O
425   "*Mark used for ancient articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-sparse-mark ?Q
430   "*Mark used for sparsely reffed articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-canceled-mark ?G
435   "*Mark used for canceled articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-duplicate-mark ?M
440   "*Mark used for duplicate articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-undownloaded-mark ?@
445   "*Mark used for articles that weren't downloaded."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-downloadable-mark ?%
450   "*Mark used for articles that are to be downloaded."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-unsendable-mark ?=
455   "*Mark used for articles that won't be sent."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-score-over-mark ?+
460   "*Score mark used for articles with high scores."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-score-below-mark ?-
465   "*Score mark used for articles with low scores."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-empty-thread-mark ?  ;Whitespace
470   "*There is no thread under the article."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-not-empty-thread-mark ?=
475   "*There is a thread under the article."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-view-pseudo-asynchronously nil
480   "*If non-nil, Gnus will view pseudo-articles asynchronously."
481   :group 'gnus-extract-view
482   :type 'boolean)
483
484 (defcustom gnus-auto-expirable-marks
485   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
486         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
487         gnus-souped-mark gnus-duplicate-mark)
488   "*The list of marks converted into expiration if a group is auto-expirable."
489   :group 'gnus-summary
490   :type '(repeat character))
491
492 (defcustom gnus-inhibit-user-auto-expire t
493   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
494   :group 'gnus-summary
495   :type 'boolean)
496
497 (defcustom gnus-view-pseudos nil
498   "*If `automatic', pseudo-articles will be viewed automatically.
499 If `not-confirm', pseudos will be viewed automatically, and the user
500 will not be asked to confirm the command."
501   :group 'gnus-extract-view
502   :type '(choice (const :tag "off" nil)
503                  (const automatic)
504                  (const not-confirm)))
505
506 (defcustom gnus-view-pseudos-separately t
507   "*If non-nil, one pseudo-article will be created for each file to be viewed.
508 If nil, all files that use the same viewing command will be given as a
509 list of parameters to that command."
510   :group 'gnus-extract-view
511   :type 'boolean)
512
513 (defcustom gnus-insert-pseudo-articles t
514   "*If non-nil, insert pseudo-articles when decoding articles."
515   :group 'gnus-extract-view
516   :type 'boolean)
517
518 (defcustom gnus-summary-dummy-line-format
519   "  %(:                          :%) %S\n"
520   "*The format specification for the dummy roots in the summary buffer.
521 It works along the same lines as a normal formatting string,
522 with some simple extensions.
523
524 %S  The subject"
525   :group 'gnus-threading
526   :type 'string)
527
528 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
529   "*The format specification for the summary mode line.
530 It works along the same lines as a normal formatting string,
531 with some simple extensions:
532
533 %G  Group name
534 %p  Unprefixed group name
535 %A  Current article number
536 %z  Current article score
537 %V  Gnus version
538 %U  Number of unread articles in the group
539 %e  Number of unselected articles in the group
540 %Z  A string with unread/unselected article counts
541 %g  Shortish group name
542 %S  Subject of the current article
543 %u  User-defined spec
544 %s  Current score file name
545 %d  Number of dormant articles
546 %r  Number of articles that have been marked as read in this session
547 %E  Number of articles expunged by the score files"
548   :group 'gnus-summary-format
549   :type 'string)
550
551 (defcustom gnus-list-identifiers nil
552   "Regexp that matches list identifiers to be removed from subject.
553 This can also be a list of regexps."
554   :group 'gnus-summary-format
555   :group 'gnus-article-hiding
556   :type '(choice (const :tag "none" nil)
557                  (regexp :value ".*")
558                  (repeat :value (".*") regexp)))
559
560 (defcustom gnus-summary-mark-below 0
561   "*Mark all articles with a score below this variable as read.
562 This variable is local to each summary buffer and usually set by the
563 score file."
564   :group 'gnus-score-default
565   :type 'integer)
566
567 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
568   "*List of functions used for sorting articles in the summary buffer.
569 This variable is only used when not using a threaded display."
570   :group 'gnus-summary-sort
571   :type '(repeat (choice (function-item gnus-article-sort-by-number)
572                          (function-item gnus-article-sort-by-author)
573                          (function-item gnus-article-sort-by-subject)
574                          (function-item gnus-article-sort-by-date)
575                          (function-item gnus-article-sort-by-score)
576                          (function :tag "other"))))
577
578 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
579   "*List of functions used for sorting threads in the summary buffer.
580 By default, threads are sorted by article number.
581
582 Each function takes two threads and return non-nil if the first thread
583 should be sorted before the other.  If you use more than one function,
584 the primary sort function should be the last.  You should probably
585 always include `gnus-thread-sort-by-number' in the list of sorting
586 functions -- preferably first.
587
588 Ready-made functions include `gnus-thread-sort-by-number',
589 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
590 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
591 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
592   :group 'gnus-summary-sort
593   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
594                          (function-item gnus-thread-sort-by-author)
595                          (function-item gnus-thread-sort-by-subject)
596                          (function-item gnus-thread-sort-by-date)
597                          (function-item gnus-thread-sort-by-score)
598                          (function-item gnus-thread-sort-by-total-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-score-function '+
602   "*Function used for calculating the total score of a thread.
603
604 The function is called with the scores of the article and each
605 subthread and should then return the score of the thread.
606
607 Some functions you can use are `+', `max', or `min'."
608   :group 'gnus-summary-sort
609   :type 'function)
610
611 (defcustom gnus-summary-expunge-below nil
612   "All articles that have a score less than this variable will be expunged.
613 This variable is local to the summary buffers."
614   :group 'gnus-score-default
615   :type '(choice (const :tag "off" nil)
616                  integer))
617
618 (defcustom gnus-thread-expunge-below nil
619   "All threads that have a total score less than this variable will be expunged.
620 See `gnus-thread-score-function' for en explanation of what a
621 \"thread score\" is.
622
623 This variable is local to the summary buffers."
624   :group 'gnus-threading
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-summary-mode-hook nil
630   "*A hook for Gnus summary mode.
631 This hook is run before any variables are set in the summary buffer."
632   :group 'gnus-summary-various
633   :type 'hook)
634
635 (defcustom gnus-summary-menu-hook nil
636   "*Hook run after the creation of the summary mode menu."
637   :group 'gnus-summary-visual
638   :type 'hook)
639
640 (defcustom gnus-summary-exit-hook nil
641   "*A hook called on exit from the summary buffer.
642 It will be called with point in the group buffer."
643   :group 'gnus-summary-exit
644   :type 'hook)
645
646 (defcustom gnus-summary-prepare-hook nil
647   "*A hook called after the summary buffer has been generated.
648 If you want to modify the summary buffer, you can use this hook."
649   :group 'gnus-summary-various
650   :type 'hook)
651
652 (defcustom gnus-summary-prepared-hook nil
653   "*A hook called as the last thing after the summary buffer has been generated."
654   :group 'gnus-summary-various
655   :type 'hook)
656
657 (defcustom gnus-summary-generate-hook nil
658   "*A hook run just before generating the summary buffer.
659 This hook is commonly used to customize threading variables and the
660 like."
661   :group 'gnus-summary-various
662   :type 'hook)
663
664 (defcustom gnus-select-group-hook nil
665   "*A hook called when a newsgroup is selected.
666
667 If you'd like to simplify subjects like the
668 `gnus-summary-next-same-subject' command does, you can use the
669 following hook:
670
671  (setq gnus-select-group-hook
672       (list
673         (lambda ()
674           (mapcar (lambda (header)
675                      (mail-header-set-subject
676                       header
677                       (gnus-simplify-subject
678                        (mail-header-subject header) 're-only)))
679                   gnus-newsgroup-headers))))"
680   :group 'gnus-group-select
681   :type 'hook)
682
683 (defcustom gnus-select-article-hook nil
684   "*A hook called when an article is selected."
685   :group 'gnus-summary-choose
686   :type 'hook)
687
688 (defcustom gnus-visual-mark-article-hook
689   (list 'gnus-highlight-selected-summary)
690   "*Hook run after selecting an article in the summary buffer.
691 It is meant to be used for highlighting the article in some way.  It
692 is not run if `gnus-visual' is nil."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-parse-headers-hook nil
697   "*A hook called before parsing the headers."
698   :group 'gnus-various
699   :type 'hook)
700
701 (defcustom gnus-exit-group-hook nil
702   "*A hook called when exiting summary mode.
703 This hook is not called from the non-updating exit commands like `Q'."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
803   "Variable that says which function should be used to decode a string with encoded words.")
804
805 (defcustom gnus-extra-headers nil
806   "*Extra headers to parse."
807   :group 'gnus-summary
808   :type '(repeat symbol))
809
810 (defcustom gnus-ignored-from-addresses
811   (and user-mail-address (regexp-quote user-mail-address))
812   "*Regexp of From headers that may be suppressed in favor of To headers."
813   :group 'gnus-summary
814   :type 'regexp)
815
816 (defcustom gnus-group-charset-alist
817   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
818     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
819     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
820     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
821     ("^relcom\\>" koi8-r)
822     ("^fido7\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^alt.chinese.text.big5\\>" chinese-big5)
827     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
828     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
829     (".*" iso-8859-1))
830   "Alist of regexps (to match group names) and default charsets to be used when reading."
831   :type '(repeat (list (regexp :tag "Group")
832                        (symbol :tag "Charset")))
833   :group 'gnus-charset)
834
835 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
836   "List of charsets that should be ignored.
837 When these charsets are used in the \"charset\" parameter, the
838 default charset will be used instead."
839   :type '(repeat symbol)
840   :group 'gnus-charset)
841
842 (defcustom gnus-group-ignored-charsets-alist
843   '(("alt\\.chinese\\.text" iso-8859-1))
844   "Alist of regexps (to match group names) and charsets that should be ignored.
845 When these charsets are used in the \"charset\" parameter, the
846 default charset will be used instead."
847   :type '(repeat (cons (regexp :tag "Group")
848                        (repeat symbol)))
849   :group 'gnus-charset)
850
851 (defcustom gnus-group-highlight-words-alist nil
852   "Alist of group regexps and highlight regexps.
853 This variable uses the same syntax as `gnus-emphasis-alist'."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat (list (regexp :tag "Highlight regexp")
856                                      (number :tag "Group for entire word" 0)
857                                      (number :tag "Group for displayed part" 0)
858                                      (symbol :tag "Face"
859                                              gnus-emphasis-highlight-words)))))
860   :group 'gnus-summary-visual)
861
862 (defcustom gnus-summary-show-article-charset-alist
863   nil
864   "Alist of number and charset.
865 The article will be shown with the charset corresponding to the
866 numbered argument.
867 For example: ((1 . cn-gb-2312) (2 . big5))."
868   :type '(repeat (cons (number :tag "Argument" 1)
869                        (symbol :tag "Charset")))
870   :group 'gnus-charset)
871
872 (defcustom gnus-preserve-marks t
873   "Whether marks are preserved when moving, copying and respooling messages."
874   :type 'boolean
875   :group 'gnus-summary-marks)
876
877 (defcustom gnus-alter-articles-to-read-function nil
878   "Function to be called to alter the list of articles to be selected."
879   :type 'function
880   :group 'gnus-summary)
881
882 (defcustom gnus-orphan-score nil
883   "*All orphans get this score added.  Set in the score file."
884   :group 'gnus-score-default
885   :type '(choice (const nil)
886                  integer))
887
888 (defcustom gnus-summary-save-parts-default-mime "image/.*"
889   "*A regexp to match MIME parts when saving multiple parts of a message
890 with gnus-summary-save-parts (X m). This regexp will be used by default
891 when prompting the user for which type of files to save."
892   :group 'gnus-summary
893   :type 'regexp)
894
895
896 ;;; Internal variables
897
898 (defvar gnus-article-mime-handles nil)
899 (defvar gnus-article-decoded-p nil)
900 (defvar gnus-scores-exclude-files nil)
901 (defvar gnus-page-broken nil)
902 (defvar gnus-inhibit-mime-unbuttonizing nil)
903
904 (defvar gnus-original-article nil)
905 (defvar gnus-article-internal-prepare-hook nil)
906 (defvar gnus-newsgroup-process-stack nil)
907
908 (defvar gnus-thread-indent-array nil)
909 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
910 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
911   "Function called to sort the articles within a thread after it has been gathered together.")
912
913 (defvar gnus-summary-save-parts-type-history nil)
914 (defvar gnus-summary-save-parts-last-directory nil)
915
916 ;; Avoid highlighting in kill files.
917 (defvar gnus-summary-inhibit-highlight nil)
918 (defvar gnus-newsgroup-selected-overlay nil)
919 (defvar gnus-inhibit-limiting nil)
920 (defvar gnus-newsgroup-adaptive-score-file nil)
921 (defvar gnus-current-score-file nil)
922 (defvar gnus-current-move-group nil)
923 (defvar gnus-current-copy-group nil)
924 (defvar gnus-current-crosspost-group nil)
925
926 (defvar gnus-newsgroup-dependencies nil)
927 (defvar gnus-newsgroup-adaptive nil)
928 (defvar gnus-summary-display-article-function nil)
929 (defvar gnus-summary-highlight-line-function nil
930   "Function called after highlighting a summary line.")
931
932 (defvar gnus-summary-line-format-alist
933   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
934     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
935     (?s gnus-tmp-subject-or-nil ?s)
936     (?n gnus-tmp-name ?s)
937     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
938         ?s)
939     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
940             gnus-tmp-from) ?s)
941     (?F gnus-tmp-from ?s)
942     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
943     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
944     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
945     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
946     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
947     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
948     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
949     (?L gnus-tmp-lines ?d)
950     (?I gnus-tmp-indentation ?s)
951     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
952     (?R gnus-tmp-replied ?c)
953     (?\[ gnus-tmp-opening-bracket ?c)
954     (?\] gnus-tmp-closing-bracket ?c)
955     (?\> (make-string gnus-tmp-level ? ) ?s)
956     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
957     (?i gnus-tmp-score ?d)
958     (?z gnus-tmp-score-char ?c)
959     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
960     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
961     (?U gnus-tmp-unread ?c)
962     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
963     (?t (gnus-summary-number-of-articles-in-thread
964          (and (boundp 'thread) (car thread)) gnus-tmp-level)
965         ?d)
966     (?e (gnus-summary-number-of-articles-in-thread
967          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
968         ?c)
969     (?u gnus-tmp-user-defined ?s)
970     (?P (gnus-pick-line-number) ?d))
971   "An alist of format specifications that can appear in summary lines.
972 These are paired with what variables they correspond with, along with
973 the type of the variable (string, integer, character, etc).")
974
975 (defvar gnus-summary-dummy-line-format-alist
976   `((?S gnus-tmp-subject ?s)
977     (?N gnus-tmp-number ?d)
978     (?u gnus-tmp-user-defined ?s)))
979
980 (defvar gnus-summary-mode-line-format-alist
981   `((?G gnus-tmp-group-name ?s)
982     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
983     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
984     (?A gnus-tmp-article-number ?d)
985     (?Z gnus-tmp-unread-and-unselected ?s)
986     (?V gnus-version ?s)
987     (?U gnus-tmp-unread-and-unticked ?d)
988     (?S gnus-tmp-subject ?s)
989     (?e gnus-tmp-unselected ?d)
990     (?u gnus-tmp-user-defined ?s)
991     (?d (length gnus-newsgroup-dormant) ?d)
992     (?t (length gnus-newsgroup-marked) ?d)
993     (?r (length gnus-newsgroup-reads) ?d)
994     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
995     (?E gnus-newsgroup-expunged-tally ?d)
996     (?s (gnus-current-score-file-nondirectory) ?s)))
997
998 (defvar gnus-last-search-regexp nil
999   "Default regexp for article search command.")
1000
1001 (defvar gnus-last-shell-command nil
1002   "Default shell command on article.")
1003
1004 (defvar gnus-newsgroup-begin nil)
1005 (defvar gnus-newsgroup-end nil)
1006 (defvar gnus-newsgroup-last-rmail nil)
1007 (defvar gnus-newsgroup-last-mail nil)
1008 (defvar gnus-newsgroup-last-folder nil)
1009 (defvar gnus-newsgroup-last-file nil)
1010 (defvar gnus-newsgroup-auto-expire nil)
1011 (defvar gnus-newsgroup-active nil)
1012
1013 (defvar gnus-newsgroup-data nil)
1014 (defvar gnus-newsgroup-data-reverse nil)
1015 (defvar gnus-newsgroup-limit nil)
1016 (defvar gnus-newsgroup-limits nil)
1017
1018 (defvar gnus-newsgroup-unreads nil
1019   "List of unread articles in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-unselected nil
1022   "List of unselected unread articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-reads nil
1025   "Alist of read articles and article marks in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-expunged-tally nil)
1028
1029 (defvar gnus-newsgroup-marked nil
1030   "List of ticked articles in the current newsgroup (a subset of unread art).")
1031
1032 (defvar gnus-newsgroup-killed nil
1033   "List of ranges of articles that have been through the scoring process.")
1034
1035 (defvar gnus-newsgroup-cached nil
1036   "List of articles that come from the article cache.")
1037
1038 (defvar gnus-newsgroup-saved nil
1039   "List of articles that have been saved.")
1040
1041 (defvar gnus-newsgroup-kill-headers nil)
1042
1043 (defvar gnus-newsgroup-replied nil
1044   "List of articles that have been replied to in the current newsgroup.")
1045
1046 (defvar gnus-newsgroup-expirable nil
1047   "List of articles in the current newsgroup that can be expired.")
1048
1049 (defvar gnus-newsgroup-processable nil
1050   "List of articles in the current newsgroup that can be processed.")
1051
1052 (defvar gnus-newsgroup-downloadable nil
1053   "List of articles in the current newsgroup that can be processed.")
1054
1055 (defvar gnus-newsgroup-undownloaded nil
1056   "List of articles in the current newsgroup that haven't been downloaded..")
1057
1058 (defvar gnus-newsgroup-unsendable nil
1059   "List of articles in the current newsgroup that won't be sent.")
1060
1061 (defvar gnus-newsgroup-bookmarks nil
1062   "List of articles in the current newsgroup that have bookmarks.")
1063
1064 (defvar gnus-newsgroup-dormant nil
1065   "List of dormant articles in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-scored nil
1068   "List of scored articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-headers nil
1071   "List of article headers in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-threads nil)
1074
1075 (defvar gnus-newsgroup-prepared nil
1076   "Whether the current group has been prepared properly.")
1077
1078 (defvar gnus-newsgroup-ancient nil
1079   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1080
1081 (defvar gnus-newsgroup-sparse nil)
1082
1083 (defvar gnus-current-article nil)
1084 (defvar gnus-article-current nil)
1085 (defvar gnus-current-headers nil)
1086 (defvar gnus-have-all-headers nil)
1087 (defvar gnus-last-article nil)
1088 (defvar gnus-newsgroup-history nil)
1089 (defvar gnus-newsgroup-charset nil)
1090 (defvar gnus-newsgroup-ephemeral-charset nil)
1091 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1092
1093 (defconst gnus-summary-local-variables
1094   '(gnus-newsgroup-name
1095     gnus-newsgroup-begin gnus-newsgroup-end
1096     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1097     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1098     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1099     gnus-newsgroup-unselected gnus-newsgroup-marked
1100     gnus-newsgroup-reads gnus-newsgroup-saved
1101     gnus-newsgroup-replied gnus-newsgroup-expirable
1102     gnus-newsgroup-processable gnus-newsgroup-killed
1103     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1104     gnus-newsgroup-unsendable
1105     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1106     gnus-newsgroup-headers gnus-newsgroup-threads
1107     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1108     gnus-current-article gnus-current-headers gnus-have-all-headers
1109     gnus-last-article gnus-article-internal-prepare-hook
1110     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1111     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1112     gnus-thread-expunge-below
1113     gnus-score-alist gnus-current-score-file
1114     (gnus-summary-expunge-below . global)
1115     (gnus-summary-mark-below . global)
1116     (gnus-orphan-score . global)
1117     gnus-newsgroup-active gnus-scores-exclude-files
1118     gnus-newsgroup-history gnus-newsgroup-ancient
1119     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1120     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1121     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1122     (gnus-newsgroup-expunged-tally . 0)
1123     gnus-cache-removable-articles gnus-newsgroup-cached
1124     gnus-newsgroup-data gnus-newsgroup-data-reverse
1125     gnus-newsgroup-limit gnus-newsgroup-limits
1126     gnus-newsgroup-charset)
1127   "Variables that are buffer-local to the summary buffers.")
1128
1129 ;; Byte-compiler warning.
1130 (defvar gnus-article-mode-map)
1131
1132 ;; MIME stuff.
1133
1134 (defvar gnus-decode-encoded-word-methods
1135   '(mail-decode-encoded-word-string)
1136   "List of methods used to decode encoded words.
1137
1138 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1139 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1140 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1141 whose names match REGEXP.
1142
1143 For example:
1144 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1145  mail-decode-encoded-word-string
1146  (\"chinese\" . rfc1843-decode-string))")
1147
1148 (defvar gnus-decode-encoded-word-methods-cache nil)
1149
1150 (defun gnus-multi-decode-encoded-word-string (string)
1151   "Apply the functions from `gnus-encoded-word-methods' that match."
1152   (unless (and gnus-decode-encoded-word-methods-cache
1153                (eq gnus-newsgroup-name
1154                    (car gnus-decode-encoded-word-methods-cache)))
1155     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1156     (mapcar (lambda (x)
1157               (if (symbolp x)
1158                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1159                 (if (and gnus-newsgroup-name
1160                          (string-match (car x) gnus-newsgroup-name))
1161                     (nconc gnus-decode-encoded-word-methods-cache
1162                            (list (cdr x))))))
1163           gnus-decode-encoded-word-methods))
1164   (let ((xlist gnus-decode-encoded-word-methods-cache))
1165     (pop xlist)
1166     (while xlist
1167       (setq string (funcall (pop xlist) string))))
1168   string)
1169
1170 ;; Subject simplification.
1171
1172 (defun gnus-simplify-whitespace (str)
1173   "Remove excessive whitespace from STR."
1174   (let ((mystr str))
1175     ;; Multiple spaces.
1176     (while (string-match "[ \t][ \t]+" mystr)
1177       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1178                           " "
1179                           (substring mystr (match-end 0)))))
1180     ;; Leading spaces.
1181     (when (string-match "^[ \t]+" mystr)
1182       (setq mystr (substring mystr (match-end 0))))
1183     ;; Trailing spaces.
1184     (when (string-match "[ \t]+$" mystr)
1185       (setq mystr (substring mystr 0 (match-beginning 0))))
1186     mystr))
1187
1188 (defsubst gnus-simplify-subject-re (subject)
1189   "Remove \"Re:\" from subject lines."
1190   (if (string-match "^[Rr][Ee]: *" subject)
1191       (substring subject (match-end 0))
1192     subject))
1193
1194 (defun gnus-simplify-subject (subject &optional re-only)
1195   "Remove `Re:' and words in parentheses.
1196 If RE-ONLY is non-nil, strip leading `Re:'s only."
1197   (let ((case-fold-search t))           ;Ignore case.
1198     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1199     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1200       (setq subject (substring subject (match-end 0))))
1201     ;; Remove uninteresting prefixes.
1202     (when (and (not re-only)
1203                gnus-simplify-ignored-prefixes
1204                (string-match gnus-simplify-ignored-prefixes subject))
1205       (setq subject (substring subject (match-end 0))))
1206     ;; Remove words in parentheses from end.
1207     (unless re-only
1208       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1209         (setq subject (substring subject 0 (match-beginning 0)))))
1210     ;; Return subject string.
1211     subject))
1212
1213 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1214 ;; all whitespace.
1215 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1216   (goto-char (point-min))
1217   (while (re-search-forward regexp nil t)
1218     (replace-match (or newtext ""))))
1219
1220 (defun gnus-simplify-buffer-fuzzy ()
1221   "Simplify string in the buffer fuzzily.
1222 The string in the accessible portion of the current buffer is simplified.
1223 It is assumed to be a single-line subject.
1224 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1225 matter is removed.  Additional things can be deleted by setting
1226 `gnus-simplify-subject-fuzzy-regexp'."
1227   (let ((case-fold-search t)
1228         (modified-tick))
1229     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1230
1231     (while (not (eq modified-tick (buffer-modified-tick)))
1232       (setq modified-tick (buffer-modified-tick))
1233       (cond
1234        ((listp gnus-simplify-subject-fuzzy-regexp)
1235         (mapcar 'gnus-simplify-buffer-fuzzy-step
1236                 gnus-simplify-subject-fuzzy-regexp))
1237        (gnus-simplify-subject-fuzzy-regexp
1238         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1239       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1240       (gnus-simplify-buffer-fuzzy-step
1241        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1242       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1243
1244     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1245     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1246     (gnus-simplify-buffer-fuzzy-step " $")
1247     (gnus-simplify-buffer-fuzzy-step "^ +")))
1248
1249 (defun gnus-simplify-subject-fuzzy (subject)
1250   "Simplify a subject string fuzzily.
1251 See `gnus-simplify-buffer-fuzzy' for details."
1252   (save-excursion
1253     (gnus-set-work-buffer)
1254     (let ((case-fold-search t))
1255       ;; Remove uninteresting prefixes.
1256       (when (and gnus-simplify-ignored-prefixes
1257                  (string-match gnus-simplify-ignored-prefixes subject))
1258         (setq subject (substring subject (match-end 0))))
1259       (insert subject)
1260       (inline (gnus-simplify-buffer-fuzzy))
1261       (buffer-string))))
1262
1263 (defsubst gnus-simplify-subject-fully (subject)
1264   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1265   (cond
1266    (gnus-simplify-subject-functions
1267     (gnus-map-function gnus-simplify-subject-functions subject))
1268    ((null gnus-summary-gather-subject-limit)
1269     (gnus-simplify-subject-re subject))
1270    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1271     (gnus-simplify-subject-fuzzy subject))
1272    ((numberp gnus-summary-gather-subject-limit)
1273     (gnus-limit-string (gnus-simplify-subject-re subject)
1274                        gnus-summary-gather-subject-limit))
1275    (t
1276     subject)))
1277
1278 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1279   "Check whether two subjects are equal.
1280 If optional argument simple-first is t, first argument is already
1281 simplified."
1282   (cond
1283    ((null simple-first)
1284     (equal (gnus-simplify-subject-fully s1)
1285            (gnus-simplify-subject-fully s2)))
1286    (t
1287     (equal s1
1288            (gnus-simplify-subject-fully s2)))))
1289
1290 (defun gnus-summary-bubble-group ()
1291   "Increase the score of the current group.
1292 This is a handy function to add to `gnus-summary-exit-hook' to
1293 increase the score of each group you read."
1294   (gnus-group-add-score gnus-newsgroup-name))
1295
1296 \f
1297 ;;;
1298 ;;; Gnus summary mode
1299 ;;;
1300
1301 (put 'gnus-summary-mode 'mode-class 'special)
1302
1303 (when t
1304   ;; Non-orthogonal keys
1305
1306   (gnus-define-keys gnus-summary-mode-map
1307     " " gnus-summary-next-page
1308     "\177" gnus-summary-prev-page
1309     [delete] gnus-summary-prev-page
1310     [backspace] gnus-summary-prev-page
1311     "\r" gnus-summary-scroll-up
1312     "\M-\r" gnus-summary-scroll-down
1313     "n" gnus-summary-next-unread-article
1314     "p" gnus-summary-prev-unread-article
1315     "N" gnus-summary-next-article
1316     "P" gnus-summary-prev-article
1317     "\M-\C-n" gnus-summary-next-same-subject
1318     "\M-\C-p" gnus-summary-prev-same-subject
1319     "\M-n" gnus-summary-next-unread-subject
1320     "\M-p" gnus-summary-prev-unread-subject
1321     "." gnus-summary-first-unread-article
1322     "," gnus-summary-best-unread-article
1323     "\M-s" gnus-summary-search-article-forward
1324     "\M-r" gnus-summary-search-article-backward
1325     "<" gnus-summary-beginning-of-article
1326     ">" gnus-summary-end-of-article
1327     "j" gnus-summary-goto-article
1328     "^" gnus-summary-refer-parent-article
1329     "\M-^" gnus-summary-refer-article
1330     "u" gnus-summary-tick-article-forward
1331     "!" gnus-summary-tick-article-forward
1332     "U" gnus-summary-tick-article-backward
1333     "d" gnus-summary-mark-as-read-forward
1334     "D" gnus-summary-mark-as-read-backward
1335     "E" gnus-summary-mark-as-expirable
1336     "\M-u" gnus-summary-clear-mark-forward
1337     "\M-U" gnus-summary-clear-mark-backward
1338     "k" gnus-summary-kill-same-subject-and-select
1339     "\C-k" gnus-summary-kill-same-subject
1340     "\M-\C-k" gnus-summary-kill-thread
1341     "\M-\C-l" gnus-summary-lower-thread
1342     "e" gnus-summary-edit-article
1343     "#" gnus-summary-mark-as-processable
1344     "\M-#" gnus-summary-unmark-as-processable
1345     "\M-\C-t" gnus-summary-toggle-threads
1346     "\M-\C-s" gnus-summary-show-thread
1347     "\M-\C-h" gnus-summary-hide-thread
1348     "\M-\C-f" gnus-summary-next-thread
1349     "\M-\C-b" gnus-summary-prev-thread
1350     [(meta down)] gnus-summary-next-thread
1351     [(meta up)] gnus-summary-prev-thread
1352     "\M-\C-u" gnus-summary-up-thread
1353     "\M-\C-d" gnus-summary-down-thread
1354     "&" gnus-summary-execute-command
1355     "c" gnus-summary-catchup-and-exit
1356     "\C-w" gnus-summary-mark-region-as-read
1357     "\C-t" gnus-summary-toggle-truncation
1358     "?" gnus-summary-mark-as-dormant
1359     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1360     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1361     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1362     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1363     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1364     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1365     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1366     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1367     "=" gnus-summary-expand-window
1368     "\C-x\C-s" gnus-summary-reselect-current-group
1369     "\M-g" gnus-summary-rescan-group
1370     "w" gnus-summary-stop-page-breaking
1371     "\C-c\C-r" gnus-summary-caesar-message
1372     "f" gnus-summary-followup
1373     "F" gnus-summary-followup-with-original
1374     "C" gnus-summary-cancel-article
1375     "r" gnus-summary-reply
1376     "R" gnus-summary-reply-with-original
1377     "\C-c\C-f" gnus-summary-mail-forward
1378     "o" gnus-summary-save-article
1379     "\C-o" gnus-summary-save-article-mail
1380     "|" gnus-summary-pipe-output
1381     "\M-k" gnus-summary-edit-local-kill
1382     "\M-K" gnus-summary-edit-global-kill
1383     ;; "V" gnus-version
1384     "\C-c\C-d" gnus-summary-describe-group
1385     "q" gnus-summary-exit
1386     "Q" gnus-summary-exit-no-update
1387     "\C-c\C-i" gnus-info-find-node
1388     gnus-mouse-2 gnus-mouse-pick-article
1389     "m" gnus-summary-mail-other-window
1390     "a" gnus-summary-post-news
1391     "x" gnus-summary-limit-to-unread
1392     "s" gnus-summary-isearch-article
1393     "t" gnus-summary-toggle-header
1394     "g" gnus-summary-show-article
1395     "l" gnus-summary-goto-last-article
1396     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1397     "\C-d" gnus-summary-enter-digest-group
1398     "\M-\C-d" gnus-summary-read-document
1399     "\M-\C-e" gnus-summary-edit-parameters
1400     "\M-\C-a" gnus-summary-customize-parameters
1401     "\C-c\C-b" gnus-bug
1402     "*" gnus-cache-enter-article
1403     "\M-*" gnus-cache-remove-article
1404     "\M-&" gnus-summary-universal-argument
1405     "\C-l" gnus-recenter
1406     "I" gnus-summary-increase-score
1407     "L" gnus-summary-lower-score
1408     "\M-i" gnus-symbolic-argument
1409     "h" gnus-summary-select-article-buffer
1410
1411     "b" gnus-article-view-part
1412     "\M-t" gnus-summary-toggle-display-buttonized
1413
1414     "V" gnus-summary-score-map
1415     "X" gnus-uu-extract-map
1416     "S" gnus-summary-send-map)
1417
1418   ;; Sort of orthogonal keymap
1419   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1420     "t" gnus-summary-tick-article-forward
1421     "!" gnus-summary-tick-article-forward
1422     "d" gnus-summary-mark-as-read-forward
1423     "r" gnus-summary-mark-as-read-forward
1424     "c" gnus-summary-clear-mark-forward
1425     " " gnus-summary-clear-mark-forward
1426     "e" gnus-summary-mark-as-expirable
1427     "x" gnus-summary-mark-as-expirable
1428     "?" gnus-summary-mark-as-dormant
1429     "b" gnus-summary-set-bookmark
1430     "B" gnus-summary-remove-bookmark
1431     "#" gnus-summary-mark-as-processable
1432     "\M-#" gnus-summary-unmark-as-processable
1433     "S" gnus-summary-limit-include-expunged
1434     "C" gnus-summary-catchup
1435     "H" gnus-summary-catchup-to-here
1436     "\C-c" gnus-summary-catchup-all
1437     "k" gnus-summary-kill-same-subject-and-select
1438     "K" gnus-summary-kill-same-subject
1439     "P" gnus-uu-mark-map)
1440
1441   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1442     "c" gnus-summary-clear-above
1443     "u" gnus-summary-tick-above
1444     "m" gnus-summary-mark-above
1445     "k" gnus-summary-kill-below)
1446
1447   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1448     "/" gnus-summary-limit-to-subject
1449     "n" gnus-summary-limit-to-articles
1450     "w" gnus-summary-pop-limit
1451     "s" gnus-summary-limit-to-subject
1452     "a" gnus-summary-limit-to-author
1453     "u" gnus-summary-limit-to-unread
1454     "m" gnus-summary-limit-to-marks
1455     "M" gnus-summary-limit-exclude-marks
1456     "v" gnus-summary-limit-to-score
1457     "*" gnus-summary-limit-include-cached
1458     "D" gnus-summary-limit-include-dormant
1459     "T" gnus-summary-limit-include-thread
1460     "d" gnus-summary-limit-exclude-dormant
1461     "t" gnus-summary-limit-to-age
1462     "x" gnus-summary-limit-to-extra
1463     "E" gnus-summary-limit-include-expunged
1464     "c" gnus-summary-limit-exclude-childless-dormant
1465     "C" gnus-summary-limit-mark-excluded-as-read)
1466
1467   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1468     "n" gnus-summary-next-unread-article
1469     "p" gnus-summary-prev-unread-article
1470     "N" gnus-summary-next-article
1471     "P" gnus-summary-prev-article
1472     "\C-n" gnus-summary-next-same-subject
1473     "\C-p" gnus-summary-prev-same-subject
1474     "\M-n" gnus-summary-next-unread-subject
1475     "\M-p" gnus-summary-prev-unread-subject
1476     "f" gnus-summary-first-unread-article
1477     "b" gnus-summary-best-unread-article
1478     "j" gnus-summary-goto-article
1479     "g" gnus-summary-goto-subject
1480     "l" gnus-summary-goto-last-article
1481     "o" gnus-summary-pop-article)
1482
1483   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1484     "k" gnus-summary-kill-thread
1485     "l" gnus-summary-lower-thread
1486     "i" gnus-summary-raise-thread
1487     "T" gnus-summary-toggle-threads
1488     "t" gnus-summary-rethread-current
1489     "^" gnus-summary-reparent-thread
1490     "s" gnus-summary-show-thread
1491     "S" gnus-summary-show-all-threads
1492     "h" gnus-summary-hide-thread
1493     "H" gnus-summary-hide-all-threads
1494     "n" gnus-summary-next-thread
1495     "p" gnus-summary-prev-thread
1496     "u" gnus-summary-up-thread
1497     "o" gnus-summary-top-thread
1498     "d" gnus-summary-down-thread
1499     "#" gnus-uu-mark-thread
1500     "\M-#" gnus-uu-unmark-thread)
1501
1502   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1503     "g" gnus-summary-prepare
1504     "c" gnus-summary-insert-cached-articles)
1505
1506   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1507     "c" gnus-summary-catchup-and-exit
1508     "C" gnus-summary-catchup-all-and-exit
1509     "E" gnus-summary-exit-no-update
1510     "Q" gnus-summary-exit
1511     "Z" gnus-summary-exit
1512     "n" gnus-summary-catchup-and-goto-next-group
1513     "R" gnus-summary-reselect-current-group
1514     "G" gnus-summary-rescan-group
1515     "N" gnus-summary-next-group
1516     "s" gnus-summary-save-newsrc
1517     "P" gnus-summary-prev-group)
1518
1519   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1520     " " gnus-summary-next-page
1521     "n" gnus-summary-next-page
1522     "\177" gnus-summary-prev-page
1523     [delete] gnus-summary-prev-page
1524     "p" gnus-summary-prev-page
1525     "\r" gnus-summary-scroll-up
1526     "\M-\r" gnus-summary-scroll-down
1527     "<" gnus-summary-beginning-of-article
1528     ">" gnus-summary-end-of-article
1529     "b" gnus-summary-beginning-of-article
1530     "e" gnus-summary-end-of-article
1531     "^" gnus-summary-refer-parent-article
1532     "r" gnus-summary-refer-parent-article
1533     "D" gnus-summary-enter-digest-group
1534     "R" gnus-summary-refer-references
1535     "T" gnus-summary-refer-thread
1536     "g" gnus-summary-show-article
1537     "s" gnus-summary-isearch-article
1538     "P" gnus-summary-print-article
1539     "t" gnus-article-babel)
1540
1541   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1542     "b" gnus-article-add-buttons
1543     "B" gnus-article-add-buttons-to-head
1544     "o" gnus-article-treat-overstrike
1545     "e" gnus-article-emphasize
1546     "w" gnus-article-fill-cited-article
1547     "Q" gnus-article-fill-long-lines
1548     "C" gnus-article-capitalize-sentences
1549     "c" gnus-article-remove-cr
1550     "q" gnus-article-de-quoted-unreadable
1551     "6" gnus-article-de-base64-unreadable
1552     "Z" gnus-article-decode-HZ
1553     "h" gnus-article-wash-html
1554     "f" gnus-article-display-x-face
1555     "l" gnus-summary-stop-page-breaking
1556     "r" gnus-summary-caesar-message
1557     "t" gnus-summary-toggle-header
1558     "v" gnus-summary-verbose-headers
1559     "H" gnus-article-strip-headers-in-body
1560     "d" gnus-article-treat-dumbquotes)
1561
1562   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1563     "a" gnus-article-hide
1564     "h" gnus-article-hide-headers
1565     "b" gnus-article-hide-boring-headers
1566     "s" gnus-article-hide-signature
1567     "c" gnus-article-hide-citation
1568     "C" gnus-article-hide-citation-in-followups
1569     "l" gnus-article-hide-list-identifiers
1570     "p" gnus-article-hide-pgp
1571     "B" gnus-article-strip-banner
1572     "P" gnus-article-hide-pem
1573     "\C-c" gnus-article-hide-citation-maybe)
1574
1575   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1576     "a" gnus-article-highlight
1577     "h" gnus-article-highlight-headers
1578     "c" gnus-article-highlight-citation
1579     "s" gnus-article-highlight-signature)
1580
1581   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1582     "w" gnus-article-decode-mime-words
1583     "c" gnus-article-decode-charset
1584     "v" gnus-mime-view-all-parts
1585     "b" gnus-article-view-part)
1586
1587   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1588     "z" gnus-article-date-ut
1589     "u" gnus-article-date-ut
1590     "l" gnus-article-date-local
1591     "e" gnus-article-date-lapsed
1592     "o" gnus-article-date-original
1593     "i" gnus-article-date-iso8601
1594     "s" gnus-article-date-user)
1595
1596   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1597     "t" gnus-article-remove-trailing-blank-lines
1598     "l" gnus-article-strip-leading-blank-lines
1599     "m" gnus-article-strip-multiple-blank-lines
1600     "a" gnus-article-strip-blank-lines
1601     "A" gnus-article-strip-all-blank-lines
1602     "s" gnus-article-strip-leading-space
1603     "e" gnus-article-strip-trailing-space)
1604
1605   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1606     "v" gnus-version
1607     "f" gnus-summary-fetch-faq
1608     "d" gnus-summary-describe-group
1609     "h" gnus-summary-describe-briefly
1610     "i" gnus-info-find-node)
1611
1612   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1613     "e" gnus-summary-expire-articles
1614     "\M-\C-e" gnus-summary-expire-articles-now
1615     "\177" gnus-summary-delete-article
1616     [delete] gnus-summary-delete-article
1617     [backspace] gnus-summary-delete-article
1618     "m" gnus-summary-move-article
1619     "r" gnus-summary-respool-article
1620     "w" gnus-summary-edit-article
1621     "c" gnus-summary-copy-article
1622     "B" gnus-summary-crosspost-article
1623     "q" gnus-summary-respool-query
1624     "t" gnus-summary-respool-trace
1625     "i" gnus-summary-import-article
1626     "p" gnus-summary-article-posted-p)
1627
1628   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1629     "o" gnus-summary-save-article
1630     "m" gnus-summary-save-article-mail
1631     "F" gnus-summary-write-article-file
1632     "r" gnus-summary-save-article-rmail
1633     "f" gnus-summary-save-article-file
1634     "b" gnus-summary-save-article-body-file
1635     "h" gnus-summary-save-article-folder
1636     "v" gnus-summary-save-article-vm
1637     "p" gnus-summary-pipe-output
1638     "s" gnus-soup-add-article)
1639
1640   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1641     "b" gnus-summary-display-buttonized
1642     "m" gnus-summary-repair-multipart
1643     "v" gnus-article-view-part
1644     "o" gnus-article-save-part
1645     "c" gnus-article-copy-part
1646     "e" gnus-article-externalize-part
1647     "i" gnus-article-inline-part
1648     "|" gnus-article-pipe-part))
1649
1650 (defun gnus-summary-make-menu-bar ()
1651   (gnus-turn-off-edit-menu 'summary)
1652
1653   (unless (boundp 'gnus-summary-misc-menu)
1654
1655     (easy-menu-define
1656      gnus-summary-kill-menu gnus-summary-mode-map ""
1657      (cons
1658       "Score"
1659       (nconc
1660        (list
1661         ["Enter score..." gnus-summary-score-entry t]
1662         ["Customize" gnus-score-customize t])
1663        (gnus-make-score-map 'increase)
1664        (gnus-make-score-map 'lower)
1665        '(("Mark"
1666           ["Kill below" gnus-summary-kill-below t]
1667           ["Mark above" gnus-summary-mark-above t]
1668           ["Tick above" gnus-summary-tick-above t]
1669           ["Clear above" gnus-summary-clear-above t])
1670          ["Current score" gnus-summary-current-score t]
1671          ["Set score" gnus-summary-set-score t]
1672          ["Switch current score file..." gnus-score-change-score-file t]
1673          ["Set mark below..." gnus-score-set-mark-below t]
1674          ["Set expunge below..." gnus-score-set-expunge-below t]
1675          ["Edit current score file" gnus-score-edit-current-scores t]
1676          ["Edit score file" gnus-score-edit-file t]
1677          ["Trace score" gnus-score-find-trace t]
1678          ["Find words" gnus-score-find-favourite-words t]
1679          ["Rescore buffer" gnus-summary-rescore t]
1680          ["Increase score..." gnus-summary-increase-score t]
1681          ["Lower score..." gnus-summary-lower-score t]))))
1682
1683     ;; Define both the Article menu in the summary buffer and the equivalent
1684     ;; Commands menu in the article buffer here for consistency.
1685     (let ((innards
1686            '(("Hide"
1687               ["All" gnus-article-hide t]
1688               ["Headers" gnus-article-hide-headers t]
1689               ["Signature" gnus-article-hide-signature t]
1690               ["Citation" gnus-article-hide-citation t]
1691               ["List identifiers" gnus-article-hide-list-identifiers t]
1692               ["PGP" gnus-article-hide-pgp t]
1693               ["Banner" gnus-article-strip-banner t]
1694               ["Boring headers" gnus-article-hide-boring-headers t])
1695              ("Highlight"
1696               ["All" gnus-article-highlight t]
1697               ["Headers" gnus-article-highlight-headers t]
1698               ["Signature" gnus-article-highlight-signature t]
1699               ["Citation" gnus-article-highlight-citation t])
1700              ("MIME"
1701               ["Words" gnus-article-decode-mime-words t]
1702               ["Charset" gnus-article-decode-charset t]
1703               ["QP" gnus-article-de-quoted-unreadable t]
1704               ["Base64" gnus-article-de-base64-unreadable t]
1705               ["View all" gnus-mime-view-all-parts t])
1706              ("Date"
1707               ["Local" gnus-article-date-local t]
1708               ["ISO8601" gnus-article-date-iso8601 t]
1709               ["UT" gnus-article-date-ut t]
1710               ["Original" gnus-article-date-original t]
1711               ["Lapsed" gnus-article-date-lapsed t]
1712               ["User-defined" gnus-article-date-user t])
1713              ("Washing"
1714               ("Remove Blanks"
1715                ["Leading" gnus-article-strip-leading-blank-lines t]
1716                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1717                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1718                ["All of the above" gnus-article-strip-blank-lines t]
1719                ["All" gnus-article-strip-all-blank-lines t]
1720                ["Leading space" gnus-article-strip-leading-space t]
1721                ["Trailing space" gnus-article-strip-trailing-space t])
1722               ["Overstrike" gnus-article-treat-overstrike t]
1723               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1724               ["Emphasis" gnus-article-emphasize t]
1725               ["Word wrap" gnus-article-fill-cited-article t]
1726               ["Fill long lines" gnus-article-fill-long-lines t]
1727               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1728               ["CR" gnus-article-remove-cr t]
1729               ["Show X-Face" gnus-article-display-x-face t]
1730               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1731               ["Base64" gnus-article-de-base64-unreadable t]
1732               ["Rot 13" gnus-summary-caesar-message t]
1733               ["Unix pipe" gnus-summary-pipe-message t]
1734               ["Add buttons" gnus-article-add-buttons t]
1735               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1736               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1737               ["Verbose header" gnus-summary-verbose-headers t]
1738               ["Toggle header" gnus-summary-toggle-header t]
1739               ["Html" gnus-article-wash-html t]
1740               ["HZ" gnus-article-decode-HZ t])
1741              ("Output"
1742               ["Save in default format" gnus-summary-save-article t]
1743               ["Save in file" gnus-summary-save-article-file t]
1744               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1745               ["Save in MH folder" gnus-summary-save-article-folder t]
1746               ["Save in VM folder" gnus-summary-save-article-vm t]
1747               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1748               ["Save body in file" gnus-summary-save-article-body-file t]
1749               ["Pipe through a filter" gnus-summary-pipe-output t]
1750               ["Add to SOUP packet" gnus-soup-add-article t]
1751               ["Print" gnus-summary-print-article t])
1752              ("Backend"
1753               ["Respool article..." gnus-summary-respool-article t]
1754               ["Move article..." gnus-summary-move-article
1755                (gnus-check-backend-function
1756                 'request-move-article gnus-newsgroup-name)]
1757               ["Copy article..." gnus-summary-copy-article t]
1758               ["Crosspost article..." gnus-summary-crosspost-article
1759                (gnus-check-backend-function
1760                 'request-replace-article gnus-newsgroup-name)]
1761               ["Import file..." gnus-summary-import-article t]
1762               ["Check if posted" gnus-summary-article-posted-p t]
1763               ["Edit article" gnus-summary-edit-article
1764                (not (gnus-group-read-only-p))]
1765               ["Delete article" gnus-summary-delete-article
1766                (gnus-check-backend-function
1767                 'request-expire-articles gnus-newsgroup-name)]
1768               ["Query respool" gnus-summary-respool-query t]
1769               ["Trace respool" gnus-summary-respool-trace t]
1770               ["Delete expirable articles" gnus-summary-expire-articles-now
1771                (gnus-check-backend-function
1772                 'request-expire-articles gnus-newsgroup-name)])
1773              ("Extract"
1774               ["Uudecode" gnus-uu-decode-uu t]
1775               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1776               ["Unshar" gnus-uu-decode-unshar t]
1777               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1778               ["Save" gnus-uu-decode-save t]
1779               ["Binhex" gnus-uu-decode-binhex t]
1780               ["Postscript" gnus-uu-decode-postscript t])
1781              ("Cache"
1782               ["Enter article" gnus-cache-enter-article t]
1783               ["Remove article" gnus-cache-remove-article t])
1784              ["Translate" gnus-article-babel t]
1785              ["Select article buffer" gnus-summary-select-article-buffer t]
1786              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1787              ["Isearch article..." gnus-summary-isearch-article t]
1788              ["Beginning of the article" gnus-summary-beginning-of-article t]
1789              ["End of the article" gnus-summary-end-of-article t]
1790              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1791              ["Fetch referenced articles" gnus-summary-refer-references t]
1792              ["Fetch current thread" gnus-summary-refer-thread t]
1793              ["Fetch article with id..." gnus-summary-refer-article t]
1794              ["Redisplay" gnus-summary-show-article t])))
1795       (easy-menu-define
1796        gnus-summary-article-menu gnus-summary-mode-map ""
1797        (cons "Article" innards))
1798
1799       (easy-menu-define
1800        gnus-article-commands-menu gnus-article-mode-map ""
1801        (cons "Commands" innards)))
1802
1803     (easy-menu-define
1804      gnus-summary-thread-menu gnus-summary-mode-map ""
1805      '("Threads"
1806        ["Toggle threading" gnus-summary-toggle-threads t]
1807        ["Hide threads" gnus-summary-hide-all-threads t]
1808        ["Show threads" gnus-summary-show-all-threads t]
1809        ["Hide thread" gnus-summary-hide-thread t]
1810        ["Show thread" gnus-summary-show-thread t]
1811        ["Go to next thread" gnus-summary-next-thread t]
1812        ["Go to previous thread" gnus-summary-prev-thread t]
1813        ["Go down thread" gnus-summary-down-thread t]
1814        ["Go up thread" gnus-summary-up-thread t]
1815        ["Top of thread" gnus-summary-top-thread t]
1816        ["Mark thread as read" gnus-summary-kill-thread t]
1817        ["Lower thread score" gnus-summary-lower-thread t]
1818        ["Raise thread score" gnus-summary-raise-thread t]
1819        ["Rethread current" gnus-summary-rethread-current t]))
1820
1821     (easy-menu-define
1822      gnus-summary-post-menu gnus-summary-mode-map ""
1823      '("Post"
1824        ["Post an article" gnus-summary-post-news t]
1825        ["Followup" gnus-summary-followup t]
1826        ["Followup and yank" gnus-summary-followup-with-original t]
1827        ["Supersede article" gnus-summary-supersede-article t]
1828        ["Cancel article" gnus-summary-cancel-article t]
1829        ["Reply" gnus-summary-reply t]
1830        ["Reply and yank" gnus-summary-reply-with-original t]
1831        ["Wide reply" gnus-summary-wide-reply t]
1832        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1833        ["Mail forward" gnus-summary-mail-forward t]
1834        ["Post forward" gnus-summary-post-forward t]
1835        ["Digest and mail" gnus-uu-digest-mail-forward t]
1836        ["Digest and post" gnus-uu-digest-post-forward t]
1837        ["Resend message" gnus-summary-resend-message t]
1838        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1839        ["Send a mail" gnus-summary-mail-other-window t]
1840        ["Uuencode and post" gnus-uu-post-news t]
1841        ["Followup via news" gnus-summary-followup-to-mail t]
1842        ["Followup via news and yank"
1843         gnus-summary-followup-to-mail-with-original t]
1844        ;;("Draft"
1845        ;;["Send" gnus-summary-send-draft t]
1846        ;;["Send bounced" gnus-resend-bounced-mail t])
1847        ))
1848
1849     (easy-menu-define
1850      gnus-summary-misc-menu gnus-summary-mode-map ""
1851      '("Misc"
1852        ("Mark Read"
1853         ["Mark as read" gnus-summary-mark-as-read-forward t]
1854         ["Mark same subject and select"
1855          gnus-summary-kill-same-subject-and-select t]
1856         ["Mark same subject" gnus-summary-kill-same-subject t]
1857         ["Catchup" gnus-summary-catchup t]
1858         ["Catchup all" gnus-summary-catchup-all t]
1859         ["Catchup to here" gnus-summary-catchup-to-here t]
1860         ["Catchup region" gnus-summary-mark-region-as-read t]
1861         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1862        ("Mark Various"
1863         ["Tick" gnus-summary-tick-article-forward t]
1864         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1865         ["Remove marks" gnus-summary-clear-mark-forward t]
1866         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1867         ["Set bookmark" gnus-summary-set-bookmark t]
1868         ["Remove bookmark" gnus-summary-remove-bookmark t])
1869        ("Mark Limit"
1870         ["Marks..." gnus-summary-limit-to-marks t]
1871         ["Subject..." gnus-summary-limit-to-subject t]
1872         ["Author..." gnus-summary-limit-to-author t]
1873         ["Age..." gnus-summary-limit-to-age t]
1874         ["Extra..." gnus-summary-limit-to-extra t]
1875         ["Score" gnus-summary-limit-to-score t]
1876         ["Unread" gnus-summary-limit-to-unread t]
1877         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1878         ["Articles" gnus-summary-limit-to-articles t]
1879         ["Pop limit" gnus-summary-pop-limit t]
1880         ["Show dormant" gnus-summary-limit-include-dormant t]
1881         ["Hide childless dormant"
1882          gnus-summary-limit-exclude-childless-dormant t]
1883         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1884         ["Hide marked" gnus-summary-limit-exclude-marks t]
1885         ["Show expunged" gnus-summary-show-all-expunged t])
1886        ("Process Mark"
1887         ["Set mark" gnus-summary-mark-as-processable t]
1888         ["Remove mark" gnus-summary-unmark-as-processable t]
1889         ["Remove all marks" gnus-summary-unmark-all-processable t]
1890         ["Mark above" gnus-uu-mark-over t]
1891         ["Mark series" gnus-uu-mark-series t]
1892         ["Mark region" gnus-uu-mark-region t]
1893         ["Unmark region" gnus-uu-unmark-region t]
1894         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1895         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1896         ["Mark all" gnus-uu-mark-all t]
1897         ["Mark buffer" gnus-uu-mark-buffer t]
1898         ["Mark sparse" gnus-uu-mark-sparse t]
1899         ["Mark thread" gnus-uu-mark-thread t]
1900         ["Unmark thread" gnus-uu-unmark-thread t]
1901         ("Process Mark Sets"
1902          ["Kill" gnus-summary-kill-process-mark t]
1903          ["Yank" gnus-summary-yank-process-mark
1904           gnus-newsgroup-process-stack]
1905          ["Save" gnus-summary-save-process-mark t]))
1906        ("Scroll article"
1907         ["Page forward" gnus-summary-next-page t]
1908         ["Page backward" gnus-summary-prev-page t]
1909         ["Line forward" gnus-summary-scroll-up t])
1910        ("Move"
1911         ["Next unread article" gnus-summary-next-unread-article t]
1912         ["Previous unread article" gnus-summary-prev-unread-article t]
1913         ["Next article" gnus-summary-next-article t]
1914         ["Previous article" gnus-summary-prev-article t]
1915         ["Next unread subject" gnus-summary-next-unread-subject t]
1916         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1917         ["Next article same subject" gnus-summary-next-same-subject t]
1918         ["Previous article same subject" gnus-summary-prev-same-subject t]
1919         ["First unread article" gnus-summary-first-unread-article t]
1920         ["Best unread article" gnus-summary-best-unread-article t]
1921         ["Go to subject number..." gnus-summary-goto-subject t]
1922         ["Go to article number..." gnus-summary-goto-article t]
1923         ["Go to the last article" gnus-summary-goto-last-article t]
1924         ["Pop article off history" gnus-summary-pop-article t])
1925        ("Sort"
1926         ["Sort by number" gnus-summary-sort-by-number t]
1927         ["Sort by author" gnus-summary-sort-by-author t]
1928         ["Sort by subject" gnus-summary-sort-by-subject t]
1929         ["Sort by date" gnus-summary-sort-by-date t]
1930         ["Sort by score" gnus-summary-sort-by-score t]
1931         ["Sort by lines" gnus-summary-sort-by-lines t]
1932         ["Sort by characters" gnus-summary-sort-by-chars t])
1933        ("Help"
1934         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1935         ["Describe group" gnus-summary-describe-group t]
1936         ["Read manual" gnus-info-find-node t])
1937        ("Modes"
1938         ["Pick and read" gnus-pick-mode t]
1939         ["Binary" gnus-binary-mode t])
1940        ("Regeneration"
1941         ["Regenerate" gnus-summary-prepare t]
1942         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1943         ["Toggle threading" gnus-summary-toggle-threads t])
1944        ["Filter articles..." gnus-summary-execute-command t]
1945        ["Run command on subjects..." gnus-summary-universal-argument t]
1946        ["Search articles forward..." gnus-summary-search-article-forward t]
1947        ["Search articles backward..." gnus-summary-search-article-backward t]
1948        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1949        ["Expand window" gnus-summary-expand-window t]
1950        ["Expire expirable articles" gnus-summary-expire-articles
1951         (gnus-check-backend-function
1952          'request-expire-articles gnus-newsgroup-name)]
1953        ["Edit local kill file" gnus-summary-edit-local-kill t]
1954        ["Edit main kill file" gnus-summary-edit-global-kill t]
1955        ["Edit group parameters" gnus-summary-edit-parameters t]
1956        ["Customize group parameters" gnus-summary-customize-parameters t]
1957        ["Send a bug report" gnus-bug t]
1958        ("Exit"
1959         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1960         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1961         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1962         ["Exit group" gnus-summary-exit t]
1963         ["Exit group without updating" gnus-summary-exit-no-update t]
1964         ["Exit and goto next group" gnus-summary-next-group t]
1965         ["Exit and goto prev group" gnus-summary-prev-group t]
1966         ["Reselect group" gnus-summary-reselect-current-group t]
1967         ["Rescan group" gnus-summary-rescan-group t]
1968         ["Update dribble" gnus-summary-save-newsrc t])))
1969
1970     (gnus-run-hooks 'gnus-summary-menu-hook)))
1971
1972 (defun gnus-score-set-default (var value)
1973   "A version of set that updates the GNU Emacs menu-bar."
1974   (set var value)
1975   ;; It is the message that forces the active status to be updated.
1976   (message ""))
1977
1978 (defun gnus-make-score-map (type)
1979   "Make a summary score map of type TYPE."
1980   (if t
1981       nil
1982     (let ((headers '(("author" "from" string)
1983                      ("subject" "subject" string)
1984                      ("article body" "body" string)
1985                      ("article head" "head" string)
1986                      ("xref" "xref" string)
1987                      ("extra header" "extra" string)
1988                      ("lines" "lines" number)
1989                      ("followups to author" "followup" string)))
1990           (types '((number ("less than" <)
1991                            ("greater than" >)
1992                            ("equal" =))
1993                    (string ("substring" s)
1994                            ("exact string" e)
1995                            ("fuzzy string" f)
1996                            ("regexp" r))))
1997           (perms '(("temporary" (current-time-string))
1998                    ("permanent" nil)
1999                    ("immediate" now)))
2000           header)
2001       (list
2002        (apply
2003         'nconc
2004         (list
2005          (if (eq type 'lower)
2006              "Lower score"
2007            "Increase score"))
2008         (let (outh)
2009           (while headers
2010             (setq header (car headers))
2011             (setq outh
2012                   (cons
2013                    (apply
2014                     'nconc
2015                     (list (car header))
2016                     (let ((ts (cdr (assoc (nth 2 header) types)))
2017                           outt)
2018                       (while ts
2019                         (setq outt
2020                               (cons
2021                                (apply
2022                                 'nconc
2023                                 (list (caar ts))
2024                                 (let ((ps perms)
2025                                       outp)
2026                                   (while ps
2027                                     (setq outp
2028                                           (cons
2029                                            (vector
2030                                             (caar ps)
2031                                             (list
2032                                              'gnus-summary-score-entry
2033                                              (nth 1 header)
2034                                              (if (or (string= (nth 1 header)
2035                                                               "head")
2036                                                      (string= (nth 1 header)
2037                                                               "body"))
2038                                                  ""
2039                                                (list 'gnus-summary-header
2040                                                      (nth 1 header)))
2041                                              (list 'quote (nth 1 (car ts)))
2042                                              (list 'gnus-score-delta-default
2043                                                    nil)
2044                                              (nth 1 (car ps))
2045                                              t)
2046                                             t)
2047                                            outp))
2048                                     (setq ps (cdr ps)))
2049                                   (list (nreverse outp))))
2050                                outt))
2051                         (setq ts (cdr ts)))
2052                       (list (nreverse outt))))
2053                    outh))
2054             (setq headers (cdr headers)))
2055           (list (nreverse outh))))))))
2056
2057 \f
2058
2059 (defun gnus-summary-mode (&optional group)
2060   "Major mode for reading articles.
2061
2062 All normal editing commands are switched off.
2063 \\<gnus-summary-mode-map>
2064 Each line in this buffer represents one article.  To read an
2065 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2066 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2067 respectively.
2068
2069 You can also post articles and send mail from this buffer.  To
2070 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2071 of an article, type `\\[gnus-summary-reply]'.
2072
2073 There are approx. one gazillion commands you can execute in this
2074 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2075
2076 The following commands are available:
2077
2078 \\{gnus-summary-mode-map}"
2079   (interactive)
2080   (when (gnus-visual-p 'summary-menu 'menu)
2081     (gnus-summary-make-menu-bar))
2082   (kill-all-local-variables)
2083   (gnus-summary-make-local-variables)
2084   (gnus-make-thread-indent-array)
2085   (gnus-simplify-mode-line)
2086   (setq major-mode 'gnus-summary-mode)
2087   (setq mode-name "Summary")
2088   (make-local-variable 'minor-mode-alist)
2089   (use-local-map gnus-summary-mode-map)
2090   (buffer-disable-undo)
2091   (setq buffer-read-only t)             ;Disable modification
2092   (setq truncate-lines t)
2093   (setq selective-display t)
2094   (setq selective-display-ellipses t)   ;Display `...'
2095   (gnus-summary-set-display-table)
2096   (gnus-set-default-directory)
2097   (setq gnus-newsgroup-name group)
2098   (make-local-variable 'gnus-summary-line-format)
2099   (make-local-variable 'gnus-summary-line-format-spec)
2100   (make-local-variable 'gnus-summary-dummy-line-format)
2101   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2102   (make-local-variable 'gnus-summary-mark-positions)
2103   (make-local-hook 'pre-command-hook)
2104   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2105   (gnus-run-hooks 'gnus-summary-mode-hook)
2106   (mm-enable-multibyte)
2107   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2108   (gnus-update-summary-mark-positions))
2109
2110 (defun gnus-summary-make-local-variables ()
2111   "Make all the local summary buffer variables."
2112   (let (global)
2113     (dolist (local gnus-summary-local-variables)
2114       (if (consp local)
2115           (progn
2116             (if (eq (cdr local) 'global)
2117                 ;; Copy the global value of the variable.
2118                 (setq global (symbol-value (car local)))
2119               ;; Use the value from the list.
2120               (setq global (eval (cdr local))))
2121             (set (make-local-variable (car local)) global))
2122         ;; Simple nil-valued local variable.
2123         (set (make-local-variable local) nil)))))
2124
2125 (defun gnus-summary-clear-local-variables ()
2126   (let ((locals gnus-summary-local-variables))
2127     (while locals
2128       (if (consp (car locals))
2129           (and (vectorp (caar locals))
2130                (set (caar locals) nil))
2131         (and (vectorp (car locals))
2132              (set (car locals) nil)))
2133       (setq locals (cdr locals)))))
2134
2135 ;; Summary data functions.
2136
2137 (defmacro gnus-data-number (data)
2138   `(car ,data))
2139
2140 (defmacro gnus-data-set-number (data number)
2141   `(setcar ,data ,number))
2142
2143 (defmacro gnus-data-mark (data)
2144   `(nth 1 ,data))
2145
2146 (defmacro gnus-data-set-mark (data mark)
2147   `(setcar (nthcdr 1 ,data) ,mark))
2148
2149 (defmacro gnus-data-pos (data)
2150   `(nth 2 ,data))
2151
2152 (defmacro gnus-data-set-pos (data pos)
2153   `(setcar (nthcdr 2 ,data) ,pos))
2154
2155 (defmacro gnus-data-header (data)
2156   `(nth 3 ,data))
2157
2158 (defmacro gnus-data-set-header (data header)
2159   `(setf (nth 3 ,data) ,header))
2160
2161 (defmacro gnus-data-level (data)
2162   `(nth 4 ,data))
2163
2164 (defmacro gnus-data-unread-p (data)
2165   `(= (nth 1 ,data) gnus-unread-mark))
2166
2167 (defmacro gnus-data-read-p (data)
2168   `(/= (nth 1 ,data) gnus-unread-mark))
2169
2170 (defmacro gnus-data-pseudo-p (data)
2171   `(consp (nth 3 ,data)))
2172
2173 (defmacro gnus-data-find (number)
2174   `(assq ,number gnus-newsgroup-data))
2175
2176 (defmacro gnus-data-find-list (number &optional data)
2177   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2178      (memq (assq ,number bdata)
2179            bdata)))
2180
2181 (defmacro gnus-data-make (number mark pos header level)
2182   `(list ,number ,mark ,pos ,header ,level))
2183
2184 (defun gnus-data-enter (after-article number mark pos header level offset)
2185   (let ((data (gnus-data-find-list after-article)))
2186     (unless data
2187       (error "No such article: %d" after-article))
2188     (setcdr data (cons (gnus-data-make number mark pos header level)
2189                        (cdr data)))
2190     (setq gnus-newsgroup-data-reverse nil)
2191     (gnus-data-update-list (cddr data) offset)))
2192
2193 (defun gnus-data-enter-list (after-article list &optional offset)
2194   (when list
2195     (let ((data (and after-article (gnus-data-find-list after-article)))
2196           (ilist list))
2197       (if (not (or data
2198                    after-article))
2199           (let ((odata gnus-newsgroup-data))
2200             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2201             (when offset
2202               (gnus-data-update-list odata offset)))
2203         ;; Find the last element in the list to be spliced into the main
2204         ;; list.
2205         (while (cdr list)
2206           (setq list (cdr list)))
2207         (if (not data)
2208             (progn
2209               (setcdr list gnus-newsgroup-data)
2210               (setq gnus-newsgroup-data ilist)
2211               (when offset
2212                 (gnus-data-update-list (cdr list) offset)))
2213           (setcdr list (cdr data))
2214           (setcdr data ilist)
2215           (when offset
2216             (gnus-data-update-list (cdr list) offset))))
2217       (setq gnus-newsgroup-data-reverse nil))))
2218
2219 (defun gnus-data-remove (article &optional offset)
2220   (let ((data gnus-newsgroup-data))
2221     (if (= (gnus-data-number (car data)) article)
2222         (progn
2223           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2224                 gnus-newsgroup-data-reverse nil)
2225           (when offset
2226             (gnus-data-update-list gnus-newsgroup-data offset)))
2227       (while (cdr data)
2228         (when (= (gnus-data-number (cadr data)) article)
2229           (setcdr data (cddr data))
2230           (when offset
2231             (gnus-data-update-list (cdr data) offset))
2232           (setq data nil
2233                 gnus-newsgroup-data-reverse nil))
2234         (setq data (cdr data))))))
2235
2236 (defmacro gnus-data-list (backward)
2237   `(if ,backward
2238        (or gnus-newsgroup-data-reverse
2239            (setq gnus-newsgroup-data-reverse
2240                  (reverse gnus-newsgroup-data)))
2241      gnus-newsgroup-data))
2242
2243 (defun gnus-data-update-list (data offset)
2244   "Add OFFSET to the POS of all data entries in DATA."
2245   (setq gnus-newsgroup-data-reverse nil)
2246   (while data
2247     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2248     (setq data (cdr data))))
2249
2250 (defun gnus-summary-article-pseudo-p (article)
2251   "Say whether this article is a pseudo article or not."
2252   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2253
2254 (defmacro gnus-summary-article-sparse-p (article)
2255   "Say whether this article is a sparse article or not."
2256   `(memq ,article gnus-newsgroup-sparse))
2257
2258 (defmacro gnus-summary-article-ancient-p (article)
2259   "Say whether this article is a sparse article or not."
2260   `(memq ,article gnus-newsgroup-ancient))
2261
2262 (defun gnus-article-parent-p (number)
2263   "Say whether this article is a parent or not."
2264   (let ((data (gnus-data-find-list number)))
2265     (and (cdr data)                     ; There has to be an article after...
2266          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2267             (gnus-data-level (nth 1 data))))))
2268
2269 (defun gnus-article-children (number)
2270   "Return a list of all children to NUMBER."
2271   (let* ((data (gnus-data-find-list number))
2272          (level (gnus-data-level (car data)))
2273          children)
2274     (setq data (cdr data))
2275     (while (and data
2276                 (= (gnus-data-level (car data)) (1+ level)))
2277       (push (gnus-data-number (car data)) children)
2278       (setq data (cdr data)))
2279     children))
2280
2281 (defmacro gnus-summary-skip-intangible ()
2282   "If the current article is intangible, then jump to a different article."
2283   '(let ((to (get-text-property (point) 'gnus-intangible)))
2284      (and to (gnus-summary-goto-subject to))))
2285
2286 (defmacro gnus-summary-article-intangible-p ()
2287   "Say whether this article is intangible or not."
2288   '(get-text-property (point) 'gnus-intangible))
2289
2290 (defun gnus-article-read-p (article)
2291   "Say whether ARTICLE is read or not."
2292   (not (or (memq article gnus-newsgroup-marked)
2293            (memq article gnus-newsgroup-unreads)
2294            (memq article gnus-newsgroup-unselected)
2295            (memq article gnus-newsgroup-dormant))))
2296
2297 ;; Some summary mode macros.
2298
2299 (defmacro gnus-summary-article-number ()
2300   "The article number of the article on the current line.
2301 If there isn's an article number here, then we return the current
2302 article number."
2303   '(progn
2304      (gnus-summary-skip-intangible)
2305      (or (get-text-property (point) 'gnus-number)
2306          (gnus-summary-last-subject))))
2307
2308 (defmacro gnus-summary-article-header (&optional number)
2309   "Return the header of article NUMBER."
2310   `(gnus-data-header (gnus-data-find
2311                       ,(or number '(gnus-summary-article-number)))))
2312
2313 (defmacro gnus-summary-thread-level (&optional number)
2314   "Return the level of thread that starts with article NUMBER."
2315   `(if (and (eq gnus-summary-make-false-root 'dummy)
2316             (get-text-property (point) 'gnus-intangible))
2317        0
2318      (gnus-data-level (gnus-data-find
2319                        ,(or number '(gnus-summary-article-number))))))
2320
2321 (defmacro gnus-summary-article-mark (&optional number)
2322   "Return the mark of article NUMBER."
2323   `(gnus-data-mark (gnus-data-find
2324                     ,(or number '(gnus-summary-article-number)))))
2325
2326 (defmacro gnus-summary-article-pos (&optional number)
2327   "Return the position of the line of article NUMBER."
2328   `(gnus-data-pos (gnus-data-find
2329                    ,(or number '(gnus-summary-article-number)))))
2330
2331 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2332 (defmacro gnus-summary-article-subject (&optional number)
2333   "Return current subject string or nil if nothing."
2334   `(let ((headers
2335           ,(if number
2336                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2337              '(gnus-data-header (assq (gnus-summary-article-number)
2338                                       gnus-newsgroup-data)))))
2339      (and headers
2340           (vectorp headers)
2341           (mail-header-subject headers))))
2342
2343 (defmacro gnus-summary-article-score (&optional number)
2344   "Return current article score."
2345   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2346                   gnus-newsgroup-scored))
2347        gnus-summary-default-score 0))
2348
2349 (defun gnus-summary-article-children (&optional number)
2350   "Return a list of article numbers that are children of article NUMBER."
2351   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2352          (level (gnus-data-level (car data)))
2353          l children)
2354     (while (and (setq data (cdr data))
2355                 (> (setq l (gnus-data-level (car data))) level))
2356       (and (= (1+ level) l)
2357            (push (gnus-data-number (car data))
2358                  children)))
2359     (nreverse children)))
2360
2361 (defun gnus-summary-article-parent (&optional number)
2362   "Return the article number of the parent of article NUMBER."
2363   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2364                                     (gnus-data-list t)))
2365          (level (gnus-data-level (car data))))
2366     (if (zerop level)
2367         ()                              ; This is a root.
2368       ;; We search until we find an article with a level less than
2369       ;; this one.  That function has to be the parent.
2370       (while (and (setq data (cdr data))
2371                   (not (< (gnus-data-level (car data)) level))))
2372       (and data (gnus-data-number (car data))))))
2373
2374 (defun gnus-unread-mark-p (mark)
2375   "Say whether MARK is the unread mark."
2376   (= mark gnus-unread-mark))
2377
2378 (defun gnus-read-mark-p (mark)
2379   "Say whether MARK is one of the marks that mark as read.
2380 This is all marks except unread, ticked, dormant, and expirable."
2381   (not (or (= mark gnus-unread-mark)
2382            (= mark gnus-ticked-mark)
2383            (= mark gnus-dormant-mark)
2384            (= mark gnus-expirable-mark))))
2385
2386 (defmacro gnus-article-mark (number)
2387   "Return the MARK of article NUMBER.
2388 This macro should only be used when computing the mark the \"first\"
2389 time; i.e., when generating the summary lines.  After that,
2390 `gnus-summary-article-mark' should be used to examine the
2391 marks of articles."
2392   `(cond
2393     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2394     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2395     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2396     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2397     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2398     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2399     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2400     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2401            gnus-ancient-mark))))
2402
2403 ;; Saving hidden threads.
2404
2405 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2406 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2407
2408 (defmacro gnus-save-hidden-threads (&rest forms)
2409   "Save hidden threads, eval FORMS, and restore the hidden threads."
2410   (let ((config (make-symbol "config")))
2411     `(let ((,config (gnus-hidden-threads-configuration)))
2412        (unwind-protect
2413            (save-excursion
2414              ,@forms)
2415          (gnus-restore-hidden-threads-configuration ,config)))))
2416
2417 (defun gnus-data-compute-positions ()
2418   "Compute the positions of all articles."
2419   (setq gnus-newsgroup-data-reverse nil)
2420   (let ((data gnus-newsgroup-data))
2421     (save-excursion
2422       (gnus-save-hidden-threads
2423         (gnus-summary-show-all-threads)
2424         (goto-char (point-min))
2425         (while data
2426           (while (get-text-property (point) 'gnus-intangible)
2427             (forward-line 1))
2428           (gnus-data-set-pos (car data) (+ (point) 3))
2429           (setq data (cdr data))
2430           (forward-line 1))))))
2431
2432 (defun gnus-hidden-threads-configuration ()
2433   "Return the current hidden threads configuration."
2434   (save-excursion
2435     (let (config)
2436       (goto-char (point-min))
2437       (while (search-forward "\r" nil t)
2438         (push (1- (point)) config))
2439       config)))
2440
2441 (defun gnus-restore-hidden-threads-configuration (config)
2442   "Restore hidden threads configuration from CONFIG."
2443   (save-excursion
2444     (let (point buffer-read-only)
2445       (while (setq point (pop config))
2446         (when (and (< point (point-max))
2447                    (goto-char point)
2448                    (eq (char-after) ?\n))
2449           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2450
2451 ;; Various summary mode internalish functions.
2452
2453 (defun gnus-mouse-pick-article (e)
2454   (interactive "e")
2455   (mouse-set-point e)
2456   (gnus-summary-next-page nil t))
2457
2458 (defun gnus-summary-set-display-table ()
2459   "Change the display table.
2460 Odd characters have a tendency to mess
2461 up nicely formatted displays - we make all possible glyphs
2462 display only a single character."
2463
2464   ;; We start from the standard display table, if any.
2465   (let ((table (or (copy-sequence standard-display-table)
2466                    (make-display-table)))
2467         (i 32))
2468     ;; Nix out all the control chars...
2469     (while (>= (setq i (1- i)) 0)
2470       (aset table i [??]))
2471     ;; ... but not newline and cr, of course.  (cr is necessary for the
2472     ;; selective display).
2473     (aset table ?\n nil)
2474     (aset table ?\r nil)
2475     ;; We keep TAB as well.
2476     (aset table ?\t nil)
2477     ;; We nix out any glyphs over 126 that are not set already.
2478     (let ((i 256))
2479       (while (>= (setq i (1- i)) 127)
2480         ;; Only modify if the entry is nil.
2481         (unless (aref table i)
2482           (aset table i [??]))))
2483     (setq buffer-display-table table)))
2484
2485 (defun gnus-summary-setup-buffer (group)
2486   "Initialize summary buffer."
2487   (let ((buffer (concat "*Summary " group "*")))
2488     (if (get-buffer buffer)
2489         (progn
2490           (set-buffer buffer)
2491           (setq gnus-summary-buffer (current-buffer))
2492           (not gnus-newsgroup-prepared))
2493       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2494       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2495       (gnus-summary-mode group)
2496       (when gnus-carpal
2497         (gnus-carpal-setup-buffer 'summary))
2498       (unless gnus-single-article-buffer
2499         (make-local-variable 'gnus-article-buffer)
2500         (make-local-variable 'gnus-article-current)
2501         (make-local-variable 'gnus-original-article-buffer))
2502       (setq gnus-newsgroup-name group)
2503       t)))
2504
2505 (defun gnus-set-global-variables ()
2506   "Set the global equivalents of the buffer-local variables.
2507 They are set to the latest values they had.  These reflect the summary
2508 buffer that was in action when the last article was fetched."
2509   (when (eq major-mode 'gnus-summary-mode)
2510     (setq gnus-summary-buffer (current-buffer))
2511     (let ((name gnus-newsgroup-name)
2512           (marked gnus-newsgroup-marked)
2513           (unread gnus-newsgroup-unreads)
2514           (headers gnus-current-headers)
2515           (data gnus-newsgroup-data)
2516           (summary gnus-summary-buffer)
2517           (article-buffer gnus-article-buffer)
2518           (original gnus-original-article-buffer)
2519           (gac gnus-article-current)
2520           (reffed gnus-reffed-article-number)
2521           (score-file gnus-current-score-file)
2522           (default-charset gnus-newsgroup-charset))
2523       (save-excursion
2524         (set-buffer gnus-group-buffer)
2525         (setq gnus-newsgroup-name name
2526               gnus-newsgroup-marked marked
2527               gnus-newsgroup-unreads unread
2528               gnus-current-headers headers
2529               gnus-newsgroup-data data
2530               gnus-article-current gac
2531               gnus-summary-buffer summary
2532               gnus-article-buffer article-buffer
2533               gnus-original-article-buffer original
2534               gnus-reffed-article-number reffed
2535               gnus-current-score-file score-file
2536               gnus-newsgroup-charset default-charset)
2537         ;; The article buffer also has local variables.
2538         (when (gnus-buffer-live-p gnus-article-buffer)
2539           (set-buffer gnus-article-buffer)
2540           (setq gnus-summary-buffer summary))))))
2541
2542 (defun gnus-summary-article-unread-p (article)
2543   "Say whether ARTICLE is unread or not."
2544   (memq article gnus-newsgroup-unreads))
2545
2546 (defun gnus-summary-first-article-p (&optional article)
2547   "Return whether ARTICLE is the first article in the buffer."
2548   (if (not (setq article (or article (gnus-summary-article-number))))
2549       nil
2550     (eq article (caar gnus-newsgroup-data))))
2551
2552 (defun gnus-summary-last-article-p (&optional article)
2553   "Return whether ARTICLE is the last article in the buffer."
2554   (if (not (setq article (or article (gnus-summary-article-number))))
2555       ;; All non-existent numbers are the last article.  :-)
2556       t
2557     (not (cdr (gnus-data-find-list article)))))
2558
2559 (defun gnus-make-thread-indent-array ()
2560   (let ((n 200))
2561     (unless (and gnus-thread-indent-array
2562                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2563       (setq gnus-thread-indent-array (make-vector 201 "")
2564             gnus-thread-indent-array-level gnus-thread-indent-level)
2565       (while (>= n 0)
2566         (aset gnus-thread-indent-array n
2567               (make-string (* n gnus-thread-indent-level) ? ))
2568         (setq n (1- n))))))
2569
2570 (defun gnus-update-summary-mark-positions ()
2571   "Compute where the summary marks are to go."
2572   (save-excursion
2573     (when (gnus-buffer-exists-p gnus-summary-buffer)
2574       (set-buffer gnus-summary-buffer))
2575     (let ((gnus-replied-mark 129)
2576           (gnus-score-below-mark 130)
2577           (gnus-score-over-mark 130)
2578           (gnus-download-mark 131)
2579           (spec gnus-summary-line-format-spec)
2580           gnus-visual pos)
2581       (save-excursion
2582         (gnus-set-work-buffer)
2583         (let ((gnus-summary-line-format-spec spec)
2584               (gnus-newsgroup-downloadable '((0 . t))))
2585           (gnus-summary-insert-line
2586            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2587           (goto-char (point-min))
2588           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2589                                              (- (point) 2)))))
2590           (goto-char (point-min))
2591           (push (cons 'replied (and (search-forward "\201" nil t)
2592                                     (- (point) 2)))
2593                 pos)
2594           (goto-char (point-min))
2595           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2596                 pos)
2597           (goto-char (point-min))
2598           (push (cons 'download
2599                       (and (search-forward "\203" nil t) (- (point) 2)))
2600                 pos)))
2601       (setq gnus-summary-mark-positions pos))))
2602
2603 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2604   "Insert a dummy root in the summary buffer."
2605   (beginning-of-line)
2606   (gnus-add-text-properties
2607    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2608    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2609
2610 (defun gnus-summary-from-or-to-or-newsgroups (header)
2611   (let ((to (cdr (assq 'To (mail-header-extra header))))
2612         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2613         (mail-parse-charset gnus-newsgroup-charset)
2614         (mail-parse-ignored-charsets
2615          (save-excursion (set-buffer gnus-summary-buffer)
2616                          gnus-newsgroup-ignored-charsets)))
2617     (cond
2618      ((and to
2619            gnus-ignored-from-addresses
2620            (string-match gnus-ignored-from-addresses
2621                          (mail-header-from header)))
2622       (concat "-> "
2623               (or (car (funcall gnus-extract-address-components
2624                                 (funcall
2625                                  gnus-decode-encoded-word-function to)))
2626                   (funcall gnus-decode-encoded-word-function to))))
2627      ((and newsgroups
2628            gnus-ignored-from-addresses
2629            (string-match gnus-ignored-from-addresses
2630                          (mail-header-from header)))
2631       (concat "=> " newsgroups))
2632      (t
2633       (or (car (funcall gnus-extract-address-components
2634                         (mail-header-from header)))
2635           (mail-header-from header))))))
2636
2637 (defun gnus-summary-insert-line (gnus-tmp-header
2638                                  gnus-tmp-level gnus-tmp-current
2639                                  gnus-tmp-unread gnus-tmp-replied
2640                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2641                                  &optional gnus-tmp-dummy gnus-tmp-score
2642                                  gnus-tmp-process)
2643   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2644          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2645          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2646          (gnus-tmp-score-char
2647           (if (or (null gnus-summary-default-score)
2648                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2649                       gnus-summary-zcore-fuzz))
2650               ?  ;Whitespace
2651             (if (< gnus-tmp-score gnus-summary-default-score)
2652                 gnus-score-below-mark gnus-score-over-mark)))
2653          (gnus-tmp-replied
2654           (cond (gnus-tmp-process gnus-process-mark)
2655                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2656                  gnus-cached-mark)
2657                 (gnus-tmp-replied gnus-replied-mark)
2658                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2659                  gnus-saved-mark)
2660                 (t gnus-unread-mark)))
2661          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2662          (gnus-tmp-name
2663           (cond
2664            ((string-match "<[^>]+> *$" gnus-tmp-from)
2665             (let ((beg (match-beginning 0)))
2666               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2667                        (substring gnus-tmp-from (1+ (match-beginning 0))
2668                                   (1- (match-end 0))))
2669                   (substring gnus-tmp-from 0 beg))))
2670            ((string-match "(.+)" gnus-tmp-from)
2671             (substring gnus-tmp-from
2672                        (1+ (match-beginning 0)) (1- (match-end 0))))
2673            (t gnus-tmp-from)))
2674          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2675          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2676          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2677          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2678          (buffer-read-only nil))
2679     (when (string= gnus-tmp-name "")
2680       (setq gnus-tmp-name gnus-tmp-from))
2681     (unless (numberp gnus-tmp-lines)
2682       (setq gnus-tmp-lines 0))
2683     (gnus-put-text-property
2684      (point)
2685      (progn (eval gnus-summary-line-format-spec) (point))
2686      'gnus-number gnus-tmp-number)
2687     (when (gnus-visual-p 'summary-highlight 'highlight)
2688       (forward-line -1)
2689       (gnus-run-hooks 'gnus-summary-update-hook)
2690       (forward-line 1))))
2691
2692 (defun gnus-summary-update-line (&optional dont-update)
2693   "Update summary line after change."
2694   (when (and gnus-summary-default-score
2695              (not gnus-summary-inhibit-highlight))
2696     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2697            (article (gnus-summary-article-number))
2698            (score (gnus-summary-article-score article)))
2699       (unless dont-update
2700         (if (and gnus-summary-mark-below
2701                  (< (gnus-summary-article-score)
2702                     gnus-summary-mark-below))
2703             ;; This article has a low score, so we mark it as read.
2704             (when (memq article gnus-newsgroup-unreads)
2705               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2706           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2707             ;; This article was previously marked as read on account
2708             ;; of a low score, but now it has risen, so we mark it as
2709             ;; unread.
2710             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2711         (gnus-summary-update-mark
2712          (if (or (null gnus-summary-default-score)
2713                  (<= (abs (- score gnus-summary-default-score))
2714                      gnus-summary-zcore-fuzz))
2715              ?  ;Whitespace
2716            (if (< score gnus-summary-default-score)
2717                gnus-score-below-mark gnus-score-over-mark))
2718          'score))
2719       ;; Do visual highlighting.
2720       (when (gnus-visual-p 'summary-highlight 'highlight)
2721         (gnus-run-hooks 'gnus-summary-update-hook)))))
2722
2723 (defvar gnus-tmp-new-adopts nil)
2724
2725 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2726   "Return the number of articles in THREAD.
2727 This may be 0 in some cases -- if none of the articles in
2728 the thread are to be displayed."
2729   (let* ((number
2730           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2731           (cond
2732            ((not (listp thread))
2733             1)
2734            ((and (consp thread) (cdr thread))
2735             (apply
2736              '+ 1 (mapcar
2737                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2738            ((null thread)
2739             1)
2740            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2741             1)
2742            (t 0))))
2743     (when (and level (zerop level) gnus-tmp-new-adopts)
2744       (incf number
2745             (apply '+ (mapcar
2746                        'gnus-summary-number-of-articles-in-thread
2747                        gnus-tmp-new-adopts))))
2748     (if char
2749         (if (> number 1) gnus-not-empty-thread-mark
2750           gnus-empty-thread-mark)
2751       number)))
2752
2753 (defun gnus-summary-set-local-parameters (group)
2754   "Go through the local params of GROUP and set all variable specs in that list."
2755   (let ((params (gnus-group-find-parameter group))
2756         elem)
2757     (while params
2758       (setq elem (car params)
2759             params (cdr params))
2760       (and (consp elem)                 ; Has to be a cons.
2761            (consp (cdr elem))           ; The cdr has to be a list.
2762            (symbolp (car elem))         ; Has to be a symbol in there.
2763            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2764            (ignore-errors               ; So we set it.
2765              (make-local-variable (car elem))
2766              (set (car elem) (eval (nth 1 elem))))))))
2767
2768 (defun gnus-summary-read-group (group &optional show-all no-article
2769                                       kill-buffer no-display backward
2770                                       select-articles)
2771   "Start reading news in newsgroup GROUP.
2772 If SHOW-ALL is non-nil, already read articles are also listed.
2773 If NO-ARTICLE is non-nil, no article is selected initially.
2774 If NO-DISPLAY, don't generate a summary buffer."
2775   (let (result)
2776     (while (and group
2777                 (null (setq result
2778                             (let ((gnus-auto-select-next nil))
2779                               (or (gnus-summary-read-group-1
2780                                    group show-all no-article
2781                                    kill-buffer no-display
2782                                    select-articles)
2783                                   (setq show-all nil
2784                                         select-articles nil)))))
2785                 (eq gnus-auto-select-next 'quietly))
2786       (set-buffer gnus-group-buffer)
2787       ;; The entry function called above goes to the next
2788       ;; group automatically, so we go two groups back
2789       ;; if we are searching for the previous group.
2790       (when backward
2791         (gnus-group-prev-unread-group 2))
2792       (if (not (equal group (gnus-group-group-name)))
2793           (setq group (gnus-group-group-name))
2794         (setq group nil)))
2795     result))
2796
2797 (defun gnus-summary-read-group-1 (group show-all no-article
2798                                         kill-buffer no-display
2799                                         &optional select-articles)
2800   ;; Killed foreign groups can't be entered.
2801   (when (and (not (gnus-group-native-p group))
2802              (not (gnus-gethash group gnus-newsrc-hashtb)))
2803     (error "Dead non-native groups can't be entered"))
2804   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2805   (let* ((new-group (gnus-summary-setup-buffer group))
2806          (quit-config (gnus-group-quit-config group))
2807          (did-select (and new-group (gnus-select-newsgroup
2808                                      group show-all select-articles))))
2809     (cond
2810      ;; This summary buffer exists already, so we just select it.
2811      ((not new-group)
2812       (gnus-set-global-variables)
2813       (when kill-buffer
2814         (gnus-kill-or-deaden-summary kill-buffer))
2815       (gnus-configure-windows 'summary 'force)
2816       (gnus-set-mode-line 'summary)
2817       (gnus-summary-position-point)
2818       (message "")
2819       t)
2820      ;; We couldn't select this group.
2821      ((null did-select)
2822       (when (and (eq major-mode 'gnus-summary-mode)
2823                  (not (equal (current-buffer) kill-buffer)))
2824         (kill-buffer (current-buffer))
2825         (if (not quit-config)
2826             (progn
2827               ;; Update the info -- marks might need to be removed,
2828               ;; for instance.
2829               (gnus-summary-update-info)
2830               (set-buffer gnus-group-buffer)
2831               (gnus-group-jump-to-group group)
2832               (gnus-group-next-unread-group 1))
2833           (gnus-handle-ephemeral-exit quit-config)))
2834       (gnus-message 3 "Can't select group")
2835       nil)
2836      ;; The user did a `C-g' while prompting for number of articles,
2837      ;; so we exit this group.
2838      ((eq did-select 'quit)
2839       (and (eq major-mode 'gnus-summary-mode)
2840            (not (equal (current-buffer) kill-buffer))
2841            (kill-buffer (current-buffer)))
2842       (when kill-buffer
2843         (gnus-kill-or-deaden-summary kill-buffer))
2844       (if (not quit-config)
2845           (progn
2846             (set-buffer gnus-group-buffer)
2847             (gnus-group-jump-to-group group)
2848             (gnus-group-next-unread-group 1)
2849             (gnus-configure-windows 'group 'force))
2850         (gnus-handle-ephemeral-exit quit-config))
2851       ;; Finally signal the quit.
2852       (signal 'quit nil))
2853      ;; The group was successfully selected.
2854      (t
2855       (gnus-set-global-variables)
2856       ;; Save the active value in effect when the group was entered.
2857       (setq gnus-newsgroup-active
2858             (gnus-copy-sequence
2859              (gnus-active gnus-newsgroup-name)))
2860       ;; You can change the summary buffer in some way with this hook.
2861       (gnus-run-hooks 'gnus-select-group-hook)
2862       ;; Set any local variables in the group parameters.
2863       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2864       (gnus-update-format-specifications
2865        nil 'summary 'summary-mode 'summary-dummy)
2866       (gnus-update-summary-mark-positions)
2867       ;; Do score processing.
2868       (when gnus-use-scoring
2869         (gnus-possibly-score-headers))
2870       ;; Check whether to fill in the gaps in the threads.
2871       (when gnus-build-sparse-threads
2872         (gnus-build-sparse-threads))
2873       ;; Find the initial limit.
2874       (if gnus-show-threads
2875           (if show-all
2876               (let ((gnus-newsgroup-dormant nil))
2877                 (gnus-summary-initial-limit show-all))
2878             (gnus-summary-initial-limit show-all))
2879         ;; When untreaded, all articles are always shown.
2880         (setq gnus-newsgroup-limit
2881               (mapcar
2882                (lambda (header) (mail-header-number header))
2883                gnus-newsgroup-headers)))
2884       ;; Generate the summary buffer.
2885       (unless no-display
2886         (gnus-summary-prepare))
2887       (when gnus-use-trees
2888         (gnus-tree-open group)
2889         (setq gnus-summary-highlight-line-function
2890               'gnus-tree-highlight-article))
2891       ;; If the summary buffer is empty, but there are some low-scored
2892       ;; articles or some excluded dormants, we include these in the
2893       ;; buffer.
2894       (when (and (zerop (buffer-size))
2895                  (not no-display))
2896         (cond (gnus-newsgroup-dormant
2897                (gnus-summary-limit-include-dormant))
2898               ((and gnus-newsgroup-scored show-all)
2899                (gnus-summary-limit-include-expunged t))))
2900       ;; Function `gnus-apply-kill-file' must be called in this hook.
2901       (gnus-run-hooks 'gnus-apply-kill-hook)
2902       (if (and (zerop (buffer-size))
2903                (not no-display))
2904           (progn
2905             ;; This newsgroup is empty.
2906             (gnus-summary-catchup-and-exit nil t)
2907             (gnus-message 6 "No unread news")
2908             (when kill-buffer
2909               (gnus-kill-or-deaden-summary kill-buffer))
2910             ;; Return nil from this function.
2911             nil)
2912         ;; Hide conversation thread subtrees.  We cannot do this in
2913         ;; gnus-summary-prepare-hook since kill processing may not
2914         ;; work with hidden articles.
2915         (and gnus-show-threads
2916              gnus-thread-hide-subtree
2917              (gnus-summary-hide-all-threads))
2918         (when kill-buffer
2919           (gnus-kill-or-deaden-summary kill-buffer))
2920         ;; Show first unread article if requested.
2921         (if (and (not no-article)
2922                  (not no-display)
2923                  gnus-newsgroup-unreads
2924                  gnus-auto-select-first)
2925             (progn
2926               (gnus-configure-windows 'summary)
2927               (cond
2928                ((eq gnus-auto-select-first 'best)
2929                 (gnus-summary-best-unread-article))
2930                ((eq gnus-auto-select-first t)
2931                 (gnus-summary-first-unread-article))
2932                ((gnus-functionp gnus-auto-select-first)
2933                 (funcall gnus-auto-select-first))))
2934           ;; Don't select any articles, just move point to the first
2935           ;; article in the group.
2936           (goto-char (point-min))
2937           (gnus-summary-position-point)
2938           (gnus-configure-windows 'summary 'force)
2939           (gnus-set-mode-line 'summary))
2940         (when (get-buffer-window gnus-group-buffer t)
2941           ;; Gotta use windows, because recenter does weird stuff if
2942           ;; the current buffer ain't the displayed window.
2943           (let ((owin (selected-window)))
2944             (select-window (get-buffer-window gnus-group-buffer t))
2945             (when (gnus-group-goto-group group)
2946               (recenter))
2947             (select-window owin)))
2948         ;; Mark this buffer as "prepared".
2949         (setq gnus-newsgroup-prepared t)
2950         (gnus-run-hooks 'gnus-summary-prepared-hook)
2951         t)))))
2952
2953 (defun gnus-summary-prepare ()
2954   "Generate the summary buffer."
2955   (interactive)
2956   (let ((buffer-read-only nil))
2957     (erase-buffer)
2958     (setq gnus-newsgroup-data nil
2959           gnus-newsgroup-data-reverse nil)
2960     (gnus-run-hooks 'gnus-summary-generate-hook)
2961     ;; Generate the buffer, either with threads or without.
2962     (when gnus-newsgroup-headers
2963       (gnus-summary-prepare-threads
2964        (if gnus-show-threads
2965            (gnus-sort-gathered-threads
2966             (funcall gnus-summary-thread-gathering-function
2967                      (gnus-sort-threads
2968                       (gnus-cut-threads (gnus-make-threads)))))
2969          ;; Unthreaded display.
2970          (gnus-sort-articles gnus-newsgroup-headers))))
2971     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2972     ;; Call hooks for modifying summary buffer.
2973     (goto-char (point-min))
2974     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2975
2976 (defsubst gnus-general-simplify-subject (subject)
2977   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2978   (setq subject
2979         (cond
2980          ;; Truncate the subject.
2981          (gnus-simplify-subject-functions
2982           (gnus-map-function gnus-simplify-subject-functions subject))
2983          ((numberp gnus-summary-gather-subject-limit)
2984           (setq subject (gnus-simplify-subject-re subject))
2985           (if (> (length subject) gnus-summary-gather-subject-limit)
2986               (substring subject 0 gnus-summary-gather-subject-limit)
2987             subject))
2988          ;; Fuzzily simplify it.
2989          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2990           (gnus-simplify-subject-fuzzy subject))
2991          ;; Just remove the leading "Re:".
2992          (t
2993           (gnus-simplify-subject-re subject))))
2994
2995   (if (and gnus-summary-gather-exclude-subject
2996            (string-match gnus-summary-gather-exclude-subject subject))
2997       nil                               ; This article shouldn't be gathered
2998     subject))
2999
3000 (defun gnus-summary-simplify-subject-query ()
3001   "Query where the respool algorithm would put this article."
3002   (interactive)
3003   (gnus-summary-select-article)
3004   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3005
3006 (defun gnus-gather-threads-by-subject (threads)
3007   "Gather threads by looking at Subject headers."
3008   (if (not gnus-summary-make-false-root)
3009       threads
3010     (let ((hashtb (gnus-make-hashtable 1024))
3011           (prev threads)
3012           (result threads)
3013           subject hthread whole-subject)
3014       (while threads
3015         (setq subject (gnus-general-simplify-subject
3016                        (setq whole-subject (mail-header-subject
3017                                             (caar threads)))))
3018         (when subject
3019           (if (setq hthread (gnus-gethash subject hashtb))
3020               (progn
3021                 ;; We enter a dummy root into the thread, if we
3022                 ;; haven't done that already.
3023                 (unless (stringp (caar hthread))
3024                   (setcar hthread (list whole-subject (car hthread))))
3025                 ;; We add this new gathered thread to this gathered
3026                 ;; thread.
3027                 (setcdr (car hthread)
3028                         (nconc (cdar hthread) (list (car threads))))
3029                 ;; Remove it from the list of threads.
3030                 (setcdr prev (cdr threads))
3031                 (setq threads prev))
3032             ;; Enter this thread into the hash table.
3033             (gnus-sethash subject threads hashtb)))
3034         (setq prev threads)
3035         (setq threads (cdr threads)))
3036       result)))
3037
3038 (defun gnus-gather-threads-by-references (threads)
3039   "Gather threads by looking at References headers."
3040   (let ((idhashtb (gnus-make-hashtable 1024))
3041         (thhashtb (gnus-make-hashtable 1024))
3042         (prev threads)
3043         (result threads)
3044         ids references id gthread gid entered ref)
3045     (while threads
3046       (when (setq references (mail-header-references (caar threads)))
3047         (setq id (mail-header-id (caar threads))
3048               ids (gnus-split-references references)
3049               entered nil)
3050         (while (setq ref (pop ids))
3051           (setq ids (delete ref ids))
3052           (if (not (setq gid (gnus-gethash ref idhashtb)))
3053               (progn
3054                 (gnus-sethash ref id idhashtb)
3055                 (gnus-sethash id threads thhashtb))
3056             (setq gthread (gnus-gethash gid thhashtb))
3057             (unless entered
3058               ;; We enter a dummy root into the thread, if we
3059               ;; haven't done that already.
3060               (unless (stringp (caar gthread))
3061                 (setcar gthread (list (mail-header-subject (caar gthread))
3062                                       (car gthread))))
3063               ;; We add this new gathered thread to this gathered
3064               ;; thread.
3065               (setcdr (car gthread)
3066                       (nconc (cdar gthread) (list (car threads)))))
3067             ;; Add it into the thread hash table.
3068             (gnus-sethash id gthread thhashtb)
3069             (setq entered t)
3070             ;; Remove it from the list of threads.
3071             (setcdr prev (cdr threads))
3072             (setq threads prev))))
3073       (setq prev threads)
3074       (setq threads (cdr threads)))
3075     result))
3076
3077 (defun gnus-sort-gathered-threads (threads)
3078   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3079   (let ((result threads))
3080     (while threads
3081       (when (stringp (caar threads))
3082         (setcdr (car threads)
3083                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3084       (setq threads (cdr threads)))
3085     result))
3086
3087 (defun gnus-thread-loop-p (root thread)
3088   "Say whether ROOT is in THREAD."
3089   (let ((stack (list thread))
3090         (infloop 0)
3091         th)
3092     (while (setq thread (pop stack))
3093       (setq th (cdr thread))
3094       (while (and th
3095                   (not (eq (caar th) root)))
3096         (pop th))
3097       (if th
3098           ;; We have found a loop.
3099           (let (ref-dep)
3100             (setcdr thread (delq (car th) (cdr thread)))
3101             (if (boundp (setq ref-dep (intern "none"
3102                                               gnus-newsgroup-dependencies)))
3103                 (setcdr (symbol-value ref-dep)
3104                         (nconc (cdr (symbol-value ref-dep))
3105                                (list (car th))))
3106               (set ref-dep (list nil (car th))))
3107             (setq infloop 1
3108                   stack nil))
3109         ;; Push all the subthreads onto the stack.
3110         (push (cdr thread) stack)))
3111     infloop))
3112
3113 (defun gnus-make-threads ()
3114   "Go through the dependency hashtb and find the roots.  Return all threads."
3115   (let (threads)
3116     (while (catch 'infloop
3117              (mapatoms
3118               (lambda (refs)
3119                 ;; Deal with self-referencing References loops.
3120                 (when (and (car (symbol-value refs))
3121                            (not (zerop
3122                                  (apply
3123                                   '+
3124                                   (mapcar
3125                                    (lambda (thread)
3126                                      (gnus-thread-loop-p
3127                                       (car (symbol-value refs)) thread))
3128                                    (cdr (symbol-value refs)))))))
3129                   (setq threads nil)
3130                   (throw 'infloop t))
3131                 (unless (car (symbol-value refs))
3132                   ;; These threads do not refer back to any other articles,
3133                   ;; so they're roots.
3134                   (setq threads (append (cdr (symbol-value refs)) threads))))
3135               gnus-newsgroup-dependencies)))
3136     threads))
3137
3138 ;; Build the thread tree.
3139 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3140   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3141
3142 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3143 if it was already present.
3144
3145 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3146 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3147 Message-IDs will be renamed be renamed to a unique Message-ID before
3148 being entered.
3149
3150 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3151   (let* ((id (mail-header-id header))
3152          (id-dep (and id (intern id dependencies)))
3153          ref ref-dep ref-header)
3154     ;; Enter this `header' in the `dependencies' table.
3155     (cond
3156      ((not id-dep)
3157       (setq header nil))
3158      ;; The first two cases do the normal part: enter a new `header'
3159      ;; in the `dependencies' table.
3160      ((not (boundp id-dep))
3161       (set id-dep (list header)))
3162      ((null (car (symbol-value id-dep)))
3163       (setcar (symbol-value id-dep) header))
3164
3165      ;; From here the `header' was already present in the
3166      ;; `dependencies' table.
3167      (force-new
3168       ;; Overrides an existing entry;
3169       ;; just set the header part of the entry.
3170       (setcar (symbol-value id-dep) header))
3171
3172      ;; Renames the existing `header' to a unique Message-ID.
3173      ((not gnus-summary-ignore-duplicates)
3174       ;; An article with this Message-ID has already been seen.
3175       ;; We rename the Message-ID.
3176       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3177            (list header))
3178       (mail-header-set-id header id))
3179
3180      ;; The last case ignores an existing entry, except it adds any
3181      ;; additional Xrefs (in case the two articles came from different
3182      ;; servers.
3183      ;; Also sets `header' to `nil' meaning that the `dependencies'
3184      ;; table was *not* modified.
3185      (t
3186       (mail-header-set-xref
3187        (car (symbol-value id-dep))
3188        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3189                    "")
3190                (or (mail-header-xref header) "")))
3191       (setq header nil)))
3192
3193     (when header
3194       ;; First check if that we are not creating a References loop.
3195       (setq ref (gnus-parent-id (mail-header-references header)))
3196       (while (and ref
3197                   (setq ref-dep (intern-soft ref dependencies))
3198                   (boundp ref-dep)
3199                   (setq ref-header (car (symbol-value ref-dep))))
3200         (if (string= id ref)
3201             ;; Yuk!  This is a reference loop.  Make the article be a
3202             ;; root article.
3203             (progn
3204               (mail-header-set-references (car (symbol-value id-dep)) "none")
3205               (setq ref nil))
3206           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3207       (setq ref (gnus-parent-id (mail-header-references header)))
3208       (setq ref-dep (intern (or ref "none") dependencies))
3209       (if (boundp ref-dep)
3210           (setcdr (symbol-value ref-dep)
3211                   (nconc (cdr (symbol-value ref-dep))
3212                          (list (symbol-value id-dep))))
3213         (set ref-dep (list nil (symbol-value id-dep)))))
3214     header))
3215
3216 (defun gnus-build-sparse-threads ()
3217   (let ((headers gnus-newsgroup-headers)
3218         (mail-parse-charset gnus-newsgroup-charset)
3219         (gnus-summary-ignore-duplicates t)
3220         header references generation relations
3221         subject child end new-child date)
3222     ;; First we create an alist of generations/relations, where
3223     ;; generations is how much we trust the relation, and the relation
3224     ;; is parent/child.
3225     (gnus-message 7 "Making sparse threads...")
3226     (save-excursion
3227       (nnheader-set-temp-buffer " *gnus sparse threads*")
3228       (while (setq header (pop headers))
3229         (when (and (setq references (mail-header-references header))
3230                    (not (string= references "")))
3231           (insert references)
3232           (setq child (mail-header-id header)
3233                 subject (mail-header-subject header)
3234                 date (mail-header-date header)
3235                 generation 0)
3236           (while (search-backward ">" nil t)
3237             (setq end (1+ (point)))
3238             (when (search-backward "<" nil t)
3239               (setq new-child (buffer-substring (point) end))
3240               (push (list (incf generation)
3241                           child (setq child new-child)
3242                           subject date)
3243                     relations)))
3244           (when child
3245             (push (list (1+ generation) child nil subject) relations))
3246           (erase-buffer)))
3247       (kill-buffer (current-buffer)))
3248     ;; Sort over trustworthiness.
3249     (mapcar
3250      (lambda (relation)
3251        (when (gnus-dependencies-add-header
3252               (make-full-mail-header
3253                gnus-reffed-article-number
3254                (nth 3 relation) "" (or (nth 4 relation) "")
3255                (nth 1 relation)
3256                (or (nth 2 relation) "") 0 0 "")
3257               gnus-newsgroup-dependencies nil)
3258          (push gnus-reffed-article-number gnus-newsgroup-limit)
3259          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3260          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3261                gnus-newsgroup-reads)
3262          (decf gnus-reffed-article-number)))
3263      (sort relations 'car-less-than-car))
3264     (gnus-message 7 "Making sparse threads...done")))
3265
3266 (defun gnus-build-old-threads ()
3267   ;; Look at all the articles that refer back to old articles, and
3268   ;; fetch the headers for the articles that aren't there.  This will
3269   ;; build complete threads - if the roots haven't been expired by the
3270   ;; server, that is.
3271   (let ((mail-parse-charset gnus-newsgroup-charset)
3272         id heads)
3273     (mapatoms
3274      (lambda (refs)
3275        (when (not (car (symbol-value refs)))
3276          (setq heads (cdr (symbol-value refs)))
3277          (while heads
3278            (if (memq (mail-header-number (caar heads))
3279                      gnus-newsgroup-dormant)
3280                (setq heads (cdr heads))
3281              (setq id (symbol-name refs))
3282              (while (and (setq id (gnus-build-get-header id))
3283                          (not (car (gnus-id-to-thread id)))))
3284              (setq heads nil)))))
3285      gnus-newsgroup-dependencies)))
3286
3287 ;; This function has to be called with point after the article number
3288 ;; on the beginning of the line.
3289 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3290   (let ((eol (gnus-point-at-eol))
3291         (buffer (current-buffer))
3292         header)
3293
3294     ;; overview: [num subject from date id refs chars lines misc]
3295     (unwind-protect
3296         (progn
3297           (narrow-to-region (point) eol)
3298           (unless (eobp)
3299             (forward-char))
3300
3301           (setq header
3302                 (make-full-mail-header
3303                  number                 ; number
3304                  (funcall gnus-decode-encoded-word-function
3305                           (nnheader-nov-field)) ; subject
3306                  (funcall gnus-decode-encoded-word-function
3307                           (nnheader-nov-field)) ; from
3308                  (nnheader-nov-field)   ; date
3309                  (nnheader-nov-read-message-id) ; id
3310                  (nnheader-nov-field)   ; refs
3311                  (nnheader-nov-read-integer) ; chars
3312                  (nnheader-nov-read-integer) ; lines
3313                  (unless (eobp)
3314                    (nnheader-nov-field)) ; misc
3315                  (nnheader-nov-parse-extra)))) ; extra
3316
3317       (widen))
3318
3319     (when gnus-alter-header-function
3320       (funcall gnus-alter-header-function header))
3321     (gnus-dependencies-add-header header dependencies force-new)))
3322
3323 (defun gnus-build-get-header (id)
3324   "Look through the buffer of NOV lines and find the header to ID.
3325 Enter this line into the dependencies hash table, and return
3326 the id of the parent article (if any)."
3327   (let ((deps gnus-newsgroup-dependencies)
3328         found header)
3329     (prog1
3330         (save-excursion
3331           (set-buffer nntp-server-buffer)
3332           (let ((case-fold-search nil))
3333             (goto-char (point-min))
3334             (while (and (not found)
3335                         (search-forward id nil t))
3336               (beginning-of-line)
3337               (setq found (looking-at
3338                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3339                                    (regexp-quote id))))
3340               (or found (beginning-of-line 2)))
3341             (when found
3342               (beginning-of-line)
3343               (and
3344                (setq header (gnus-nov-parse-line
3345                              (read (current-buffer)) deps))
3346                (gnus-parent-id (mail-header-references header))))))
3347       (when header
3348         (let ((number (mail-header-number header)))
3349           (push number gnus-newsgroup-limit)
3350           (push header gnus-newsgroup-headers)
3351           (if (memq number gnus-newsgroup-unselected)
3352               (progn
3353                 (push number gnus-newsgroup-unreads)
3354                 (setq gnus-newsgroup-unselected
3355                       (delq number gnus-newsgroup-unselected)))
3356             (push number gnus-newsgroup-ancient)))))))
3357
3358 (defun gnus-build-all-threads ()
3359   "Read all the headers."
3360   (let ((gnus-summary-ignore-duplicates t)
3361         (mail-parse-charset gnus-newsgroup-charset)
3362         (dependencies gnus-newsgroup-dependencies)
3363         header article)
3364     (save-excursion
3365       (set-buffer nntp-server-buffer)
3366       (let ((case-fold-search nil))
3367         (goto-char (point-min))
3368         (while (not (eobp))
3369           (ignore-errors
3370             (setq article (read (current-buffer))
3371                   header (gnus-nov-parse-line article dependencies)))
3372           (when header
3373             (save-excursion
3374               (set-buffer gnus-summary-buffer)
3375               (push header gnus-newsgroup-headers)
3376               (if (memq (setq article (mail-header-number header))
3377                         gnus-newsgroup-unselected)
3378                   (progn
3379                     (push article gnus-newsgroup-unreads)
3380                     (setq gnus-newsgroup-unselected
3381                           (delq article gnus-newsgroup-unselected)))
3382                 (push article gnus-newsgroup-ancient)))
3383             (forward-line 1)))))))
3384
3385 (defun gnus-summary-update-article-line (article header)
3386   "Update the line for ARTICLE using HEADERS."
3387   (let* ((id (mail-header-id header))
3388          (thread (gnus-id-to-thread id)))
3389     (unless thread
3390       (error "Article in no thread"))
3391     ;; Update the thread.
3392     (setcar thread header)
3393     (gnus-summary-goto-subject article)
3394     (let* ((datal (gnus-data-find-list article))
3395            (data (car datal))
3396            (length (when (cdr datal)
3397                      (- (gnus-data-pos data)
3398                         (gnus-data-pos (cadr datal)))))
3399            (buffer-read-only nil)
3400            (level (gnus-summary-thread-level)))
3401       (gnus-delete-line)
3402       (gnus-summary-insert-line
3403        header level nil (gnus-article-mark article)
3404        (memq article gnus-newsgroup-replied)
3405        (memq article gnus-newsgroup-expirable)
3406        ;; Only insert the Subject string when it's different
3407        ;; from the previous Subject string.
3408        (if (and
3409             gnus-show-threads
3410             (gnus-subject-equal
3411              (condition-case ()
3412                  (mail-header-subject
3413                   (gnus-data-header
3414                    (cadr
3415                     (gnus-data-find-list
3416                      article
3417                      (gnus-data-list t)))))
3418                ;; Error on the side of excessive subjects.
3419                (error ""))
3420              (mail-header-subject header)))
3421            ""
3422          (mail-header-subject header))
3423        nil (cdr (assq article gnus-newsgroup-scored))
3424        (memq article gnus-newsgroup-processable))
3425       (when length
3426         (gnus-data-update-list
3427          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3428
3429 (defun gnus-summary-update-article (article &optional iheader)
3430   "Update ARTICLE in the summary buffer."
3431   (set-buffer gnus-summary-buffer)
3432   (let* ((header (gnus-summary-article-header article))
3433          (id (mail-header-id header))
3434          (data (gnus-data-find article))
3435          (thread (gnus-id-to-thread id))
3436          (references (mail-header-references header))
3437          (parent
3438           (gnus-id-to-thread
3439            (or (gnus-parent-id
3440                 (when (and references
3441                            (not (equal "" references)))
3442                   references))
3443                "none")))
3444          (buffer-read-only nil)
3445          (old (car thread)))
3446     (when thread
3447       (unless iheader
3448         (setcar thread nil)
3449         (when parent
3450           (delq thread parent)))
3451       (if (gnus-summary-insert-subject id header)
3452           ;; Set the (possibly) new article number in the data structure.
3453           (gnus-data-set-number data (gnus-id-to-article id))
3454         (setcar thread old)
3455         nil))))
3456
3457 (defun gnus-rebuild-thread (id &optional line)
3458   "Rebuild the thread containing ID.
3459 If LINE, insert the rebuilt thread starting on line LINE."
3460   (let ((buffer-read-only nil)
3461         old-pos current thread data)
3462     (if (not gnus-show-threads)
3463         (setq thread (list (car (gnus-id-to-thread id))))
3464       ;; Get the thread this article is part of.
3465       (setq thread (gnus-remove-thread id)))
3466     (setq old-pos (gnus-point-at-bol))
3467     (setq current (save-excursion
3468                     (and (zerop (forward-line -1))
3469                          (gnus-summary-article-number))))
3470     ;; If this is a gathered thread, we have to go some re-gathering.
3471     (when (stringp (car thread))
3472       (let ((subject (car thread))
3473             roots thr)
3474         (setq thread (cdr thread))
3475         (while thread
3476           (unless (memq (setq thr (gnus-id-to-thread
3477                                    (gnus-root-id
3478                                     (mail-header-id (caar thread)))))
3479                         roots)
3480             (push thr roots))
3481           (setq thread (cdr thread)))
3482         ;; We now have all (unique) roots.
3483         (if (= (length roots) 1)
3484             ;; All the loose roots are now one solid root.
3485             (setq thread (car roots))
3486           (setq thread (cons subject (gnus-sort-threads roots))))))
3487     (let (threads)
3488       ;; We then insert this thread into the summary buffer.
3489       (when line
3490         (goto-char (point-min))
3491         (forward-line (1- line)))
3492       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3493         (if gnus-show-threads
3494             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3495           (gnus-summary-prepare-unthreaded thread))
3496         (setq data (nreverse gnus-newsgroup-data))
3497         (setq threads gnus-newsgroup-threads))
3498       ;; We splice the new data into the data structure.
3499       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3500       ;;!!! then we want to insert at the beginning of the buffer.
3501       ;;!!! That happens to be true with Gnus now, but that may
3502       ;;!!! change in the future.  Perhaps.
3503       (gnus-data-enter-list
3504        (if line nil current) data (- (point) old-pos))
3505       (setq gnus-newsgroup-threads
3506             (nconc threads gnus-newsgroup-threads))
3507       (gnus-data-compute-positions))))
3508
3509 (defun gnus-number-to-header (number)
3510   "Return the header for article NUMBER."
3511   (let ((headers gnus-newsgroup-headers))
3512     (while (and headers
3513                 (not (= number (mail-header-number (car headers)))))
3514       (pop headers))
3515     (when headers
3516       (car headers))))
3517
3518 (defun gnus-parent-headers (in-headers &optional generation)
3519   "Return the headers of the GENERATIONeth parent of HEADERS."
3520   (unless generation
3521     (setq generation 1))
3522   (let ((parent t)
3523         (headers in-headers)
3524         references)
3525     (while (and parent
3526                 (not (zerop generation))
3527                 (setq references (mail-header-references headers)))
3528       (setq headers (if (and references
3529                              (setq parent (gnus-parent-id references)))
3530                         (car (gnus-id-to-thread parent))
3531                       nil))
3532       (decf generation))
3533     (and (not (eq headers in-headers))
3534          headers)))
3535
3536 (defun gnus-id-to-thread (id)
3537   "Return the (sub-)thread where ID appears."
3538   (gnus-gethash id gnus-newsgroup-dependencies))
3539
3540 (defun gnus-id-to-article (id)
3541   "Return the article number of ID."
3542   (let ((thread (gnus-id-to-thread id)))
3543     (when (and thread
3544                (car thread))
3545       (mail-header-number (car thread)))))
3546
3547 (defun gnus-id-to-header (id)
3548   "Return the article headers of ID."
3549   (car (gnus-id-to-thread id)))
3550
3551 (defun gnus-article-displayed-root-p (article)
3552   "Say whether ARTICLE is a root(ish) article."
3553   (let ((level (gnus-summary-thread-level article))
3554         (refs (mail-header-references  (gnus-summary-article-header article)))
3555         particle)
3556     (cond
3557      ((null level) nil)
3558      ((zerop level) t)
3559      ((null refs) t)
3560      ((null (gnus-parent-id refs)) t)
3561      ((and (= 1 level)
3562            (null (setq particle (gnus-id-to-article
3563                                  (gnus-parent-id refs))))
3564            (null (gnus-summary-thread-level particle)))))))
3565
3566 (defun gnus-root-id (id)
3567   "Return the id of the root of the thread where ID appears."
3568   (let (last-id prev)
3569     (while (and id (setq prev (car (gnus-id-to-thread id))))
3570       (setq last-id id
3571             id (gnus-parent-id (mail-header-references prev))))
3572     last-id))
3573
3574 (defun gnus-articles-in-thread (thread)
3575   "Return the list of articles in THREAD."
3576   (cons (mail-header-number (car thread))
3577         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3578
3579 (defun gnus-remove-thread (id &optional dont-remove)
3580   "Remove the thread that has ID in it."
3581   (let (headers thread last-id)
3582     ;; First go up in this thread until we find the root.
3583     (setq last-id (gnus-root-id id)
3584           headers (message-flatten-list (gnus-id-to-thread last-id)))
3585     ;; We have now found the real root of this thread.  It might have
3586     ;; been gathered into some loose thread, so we have to search
3587     ;; through the threads to find the thread we wanted.
3588     (let ((threads gnus-newsgroup-threads)
3589           sub)
3590       (while threads
3591         (setq sub (car threads))
3592         (if (stringp (car sub))
3593             ;; This is a gathered thread, so we look at the roots
3594             ;; below it to find whether this article is in this
3595             ;; gathered root.
3596             (progn
3597               (setq sub (cdr sub))
3598               (while sub
3599                 (when (member (caar sub) headers)
3600                   (setq thread (car threads)
3601                         threads nil
3602                         sub nil))
3603                 (setq sub (cdr sub))))
3604           ;; It's an ordinary thread, so we check it.
3605           (when (eq (car sub) (car headers))
3606             (setq thread sub
3607                   threads nil)))
3608         (setq threads (cdr threads)))
3609       ;; If this article is in no thread, then it's a root.
3610       (if thread
3611           (unless dont-remove
3612             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3613         (setq thread (gnus-id-to-thread last-id)))
3614       (when thread
3615         (prog1
3616             thread                      ; We return this thread.
3617           (unless dont-remove
3618             (if (stringp (car thread))
3619                 (progn
3620                   ;; If we use dummy roots, then we have to remove the
3621                   ;; dummy root as well.
3622                   (when (eq gnus-summary-make-false-root 'dummy)
3623                     ;; We go to the dummy root by going to
3624                     ;; the first sub-"thread", and then one line up.
3625                     (gnus-summary-goto-article
3626                      (mail-header-number (caadr thread)))
3627                     (forward-line -1)
3628                     (gnus-delete-line)
3629                     (gnus-data-compute-positions))
3630                   (setq thread (cdr thread))
3631                   (while thread
3632                     (gnus-remove-thread-1 (car thread))
3633                     (setq thread (cdr thread))))
3634               (gnus-remove-thread-1 thread))))))))
3635
3636 (defun gnus-remove-thread-1 (thread)
3637   "Remove the thread THREAD recursively."
3638   (let ((number (mail-header-number (pop thread)))
3639         d)
3640     (setq thread (reverse thread))
3641     (while thread
3642       (gnus-remove-thread-1 (pop thread)))
3643     (when (setq d (gnus-data-find number))
3644       (goto-char (gnus-data-pos d))
3645       (gnus-summary-show-thread)
3646       (gnus-data-remove
3647        number
3648        (- (gnus-point-at-bol)
3649           (prog1
3650               (1+ (gnus-point-at-eol))
3651             (gnus-delete-line)))))))
3652
3653 (defun gnus-sort-threads-1 (threads func)
3654   (sort (mapcar (lambda (thread)
3655                   (cons (car thread)
3656                         (and (cdr thread)
3657                              (gnus-sort-threads-1 (cdr thread) func))))
3658                 threads) func))
3659
3660 (defun gnus-sort-threads (threads)
3661   "Sort THREADS."
3662   (if (not gnus-thread-sort-functions)
3663       threads
3664     (gnus-message 8 "Sorting threads...")
3665     (prog1
3666         (gnus-sort-threads-1 
3667          threads 
3668          (gnus-make-sort-function gnus-thread-sort-functions))
3669       (gnus-message 8 "Sorting threads...done"))))
3670
3671 (defun gnus-sort-articles (articles)
3672   "Sort ARTICLES."
3673   (when gnus-article-sort-functions
3674     (gnus-message 7 "Sorting articles...")
3675     (prog1
3676         (setq gnus-newsgroup-headers
3677               (sort articles (gnus-make-sort-function
3678                               gnus-article-sort-functions)))
3679       (gnus-message 7 "Sorting articles...done"))))
3680
3681 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3682 (defmacro gnus-thread-header (thread)
3683   "Return header of first article in THREAD.
3684 Note that THREAD must never, ever be anything else than a variable -
3685 using some other form will lead to serious barfage."
3686   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3687   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3688   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3689         (vector thread) 2))
3690
3691 (defsubst gnus-article-sort-by-number (h1 h2)
3692   "Sort articles by article number."
3693   (< (mail-header-number h1)
3694      (mail-header-number h2)))
3695
3696 (defun gnus-thread-sort-by-number (h1 h2)
3697   "Sort threads by root article number."
3698   (gnus-article-sort-by-number
3699    (gnus-thread-header h1) (gnus-thread-header h2)))
3700
3701 (defsubst gnus-article-sort-by-lines (h1 h2)
3702   "Sort articles by article Lines header."
3703   (< (mail-header-lines h1)
3704      (mail-header-lines h2)))
3705
3706 (defun gnus-thread-sort-by-lines (h1 h2)
3707   "Sort threads by root article Lines header."
3708   (gnus-article-sort-by-lines
3709    (gnus-thread-header h1) (gnus-thread-header h2)))
3710
3711 (defsubst gnus-article-sort-by-chars (h1 h2)
3712   "Sort articles by octet length."
3713   (< (mail-header-chars h1)
3714      (mail-header-chars h2)))
3715
3716 (defun gnus-thread-sort-by-chars (h1 h2)
3717   "Sort threads by root article octet length."
3718   (gnus-article-sort-by-chars
3719    (gnus-thread-header h1) (gnus-thread-header h2)))
3720
3721 (defsubst gnus-article-sort-by-author (h1 h2)
3722   "Sort articles by root author."
3723   (string-lessp
3724    (let ((extract (funcall
3725                    gnus-extract-address-components
3726                    (mail-header-from h1))))
3727      (or (car extract) (cadr extract) ""))
3728    (let ((extract (funcall
3729                    gnus-extract-address-components
3730                    (mail-header-from h2))))
3731      (or (car extract) (cadr extract) ""))))
3732
3733 (defun gnus-thread-sort-by-author (h1 h2)
3734   "Sort threads by root author."
3735   (gnus-article-sort-by-author
3736    (gnus-thread-header h1)  (gnus-thread-header h2)))
3737
3738 (defsubst gnus-article-sort-by-subject (h1 h2)
3739   "Sort articles by root subject."
3740   (string-lessp
3741    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3742    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3743
3744 (defun gnus-thread-sort-by-subject (h1 h2)
3745   "Sort threads by root subject."
3746   (gnus-article-sort-by-subject
3747    (gnus-thread-header h1) (gnus-thread-header h2)))
3748
3749 (defsubst gnus-article-sort-by-date (h1 h2)
3750   "Sort articles by root article date."
3751   (time-less-p
3752    (gnus-date-get-time (mail-header-date h1))
3753    (gnus-date-get-time (mail-header-date h2))))
3754
3755 (defun gnus-thread-sort-by-date (h1 h2)
3756   "Sort threads by root article date."
3757   (gnus-article-sort-by-date
3758    (gnus-thread-header h1) (gnus-thread-header h2)))
3759
3760 (defsubst gnus-article-sort-by-score (h1 h2)
3761   "Sort articles by root article score.
3762 Unscored articles will be counted as having a score of zero."
3763   (> (or (cdr (assq (mail-header-number h1)
3764                     gnus-newsgroup-scored))
3765          gnus-summary-default-score 0)
3766      (or (cdr (assq (mail-header-number h2)
3767                     gnus-newsgroup-scored))
3768          gnus-summary-default-score 0)))
3769
3770 (defun gnus-thread-sort-by-score (h1 h2)
3771   "Sort threads by root article score."
3772   (gnus-article-sort-by-score
3773    (gnus-thread-header h1) (gnus-thread-header h2)))
3774
3775 (defun gnus-thread-sort-by-total-score (h1 h2)
3776   "Sort threads by the sum of all scores in the thread.
3777 Unscored articles will be counted as having a score of zero."
3778   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3779
3780 (defun gnus-thread-total-score (thread)
3781   ;; This function find the total score of THREAD.
3782   (cond ((null thread)
3783          0)
3784         ((consp thread)
3785          (if (stringp (car thread))
3786              (apply gnus-thread-score-function 0
3787                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3788            (gnus-thread-total-score-1 thread)))
3789         (t
3790          (gnus-thread-total-score-1 (list thread)))))
3791
3792 (defun gnus-thread-total-score-1 (root)
3793   ;; This function find the total score of the thread below ROOT.
3794   (setq root (car root))
3795   (apply gnus-thread-score-function
3796          (or (append
3797               (mapcar 'gnus-thread-total-score
3798                       (cdr (gnus-id-to-thread (mail-header-id root))))
3799               (when (> (mail-header-number root) 0)
3800                 (list (or (cdr (assq (mail-header-number root)
3801                                      gnus-newsgroup-scored))
3802                           gnus-summary-default-score 0))))
3803              (list gnus-summary-default-score)
3804              '(0))))
3805
3806 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3807 (defvar gnus-tmp-prev-subject nil)
3808 (defvar gnus-tmp-false-parent nil)
3809 (defvar gnus-tmp-root-expunged nil)
3810 (defvar gnus-tmp-dummy-line nil)
3811
3812 (defvar gnus-tmp-header)
3813 (defun gnus-extra-header (type &optional header)
3814   "Return the extra header of TYPE."
3815   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3816       ""))
3817
3818 (defun gnus-summary-prepare-threads (threads)
3819   "Prepare summary buffer from THREADS and indentation LEVEL.
3820 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3821 or a straight list of headers."
3822   (gnus-message 7 "Generating summary...")
3823
3824   (setq gnus-newsgroup-threads threads)
3825   (beginning-of-line)
3826
3827   (let ((gnus-tmp-level 0)
3828         (default-score (or gnus-summary-default-score 0))
3829         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3830         thread number subject stack state gnus-tmp-gathered beg-match
3831         new-roots gnus-tmp-new-adopts thread-end
3832         gnus-tmp-header gnus-tmp-unread
3833         gnus-tmp-replied gnus-tmp-subject-or-nil
3834         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3835         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3836         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3837
3838     (setq gnus-tmp-prev-subject nil)
3839
3840     (if (vectorp (car threads))
3841         ;; If this is a straight (sic) list of headers, then a
3842         ;; threaded summary display isn't required, so we just create
3843         ;; an unthreaded one.
3844         (gnus-summary-prepare-unthreaded threads)
3845
3846       ;; Do the threaded display.
3847
3848       (while (or threads stack gnus-tmp-new-adopts new-roots)
3849
3850         (if (and (= gnus-tmp-level 0)
3851                  (or (not stack)
3852                      (= (caar stack) 0))
3853                  (not gnus-tmp-false-parent)
3854                  (or gnus-tmp-new-adopts new-roots))
3855             (if gnus-tmp-new-adopts
3856                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3857                       thread (list (car gnus-tmp-new-adopts))
3858                       gnus-tmp-header (caar thread)
3859                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3860               (when new-roots
3861                 (setq thread (list (car new-roots))
3862                       gnus-tmp-header (caar thread)
3863                       new-roots (cdr new-roots))))
3864
3865           (if threads
3866               ;; If there are some threads, we do them before the
3867               ;; threads on the stack.
3868               (setq thread threads
3869                     gnus-tmp-header (caar thread))
3870             ;; There were no current threads, so we pop something off
3871             ;; the stack.
3872             (setq state (car stack)
3873                   gnus-tmp-level (car state)
3874                   thread (cdr state)
3875                   stack (cdr stack)
3876                   gnus-tmp-header (caar thread))))
3877
3878         (setq gnus-tmp-false-parent nil)
3879         (setq gnus-tmp-root-expunged nil)
3880         (setq thread-end nil)
3881
3882         (if (stringp gnus-tmp-header)
3883             ;; The header is a dummy root.
3884             (cond
3885              ((eq gnus-summary-make-false-root 'adopt)
3886               ;; We let the first article adopt the rest.
3887               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3888                                                (cddar thread)))
3889               (setq gnus-tmp-gathered
3890                     (nconc (mapcar
3891                             (lambda (h) (mail-header-number (car h)))
3892                             (cddar thread))
3893                            gnus-tmp-gathered))
3894               (setq thread (cons (list (caar thread)
3895                                        (cadar thread))
3896                                  (cdr thread)))
3897               (setq gnus-tmp-level -1
3898                     gnus-tmp-false-parent t))
3899              ((eq gnus-summary-make-false-root 'empty)
3900               ;; We print adopted articles with empty subject fields.
3901               (setq gnus-tmp-gathered
3902                     (nconc (mapcar
3903                             (lambda (h) (mail-header-number (car h)))
3904                             (cddar thread))
3905                            gnus-tmp-gathered))
3906               (setq gnus-tmp-level -1))
3907              ((eq gnus-summary-make-false-root 'dummy)
3908               ;; We remember that we probably want to output a dummy
3909               ;; root.
3910               (setq gnus-tmp-dummy-line gnus-tmp-header)
3911               (setq gnus-tmp-prev-subject gnus-tmp-header))
3912              (t
3913               ;; We do not make a root for the gathered
3914               ;; sub-threads at all.
3915               (setq gnus-tmp-level -1)))
3916
3917           (setq number (mail-header-number gnus-tmp-header)
3918                 subject (mail-header-subject gnus-tmp-header))
3919
3920           (cond
3921            ;; If the thread has changed subject, we might want to make
3922            ;; this subthread into a root.
3923            ((and (null gnus-thread-ignore-subject)
3924                  (not (zerop gnus-tmp-level))
3925                  gnus-tmp-prev-subject
3926                  (not (inline
3927                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3928             (setq new-roots (nconc new-roots (list (car thread)))
3929                   thread-end t
3930                   gnus-tmp-header nil))
3931            ;; If the article lies outside the current limit,
3932            ;; then we do not display it.
3933            ((not (memq number gnus-newsgroup-limit))
3934             (setq gnus-tmp-gathered
3935                   (nconc (mapcar
3936                           (lambda (h) (mail-header-number (car h)))
3937                           (cdar thread))
3938                          gnus-tmp-gathered))
3939             (setq gnus-tmp-new-adopts (if (cdar thread)
3940                                           (append gnus-tmp-new-adopts
3941                                                   (cdar thread))
3942                                         gnus-tmp-new-adopts)
3943                   thread-end t
3944                   gnus-tmp-header nil)
3945             (when (zerop gnus-tmp-level)
3946               (setq gnus-tmp-root-expunged t)))
3947            ;; Perhaps this article is to be marked as read?
3948            ((and gnus-summary-mark-below
3949                  (< (or (cdr (assq number gnus-newsgroup-scored))
3950                         default-score)
3951                     gnus-summary-mark-below)
3952                  ;; Don't touch sparse articles.
3953                  (not (gnus-summary-article-sparse-p number))
3954                  (not (gnus-summary-article-ancient-p number)))
3955             (setq gnus-newsgroup-unreads
3956                   (delq number gnus-newsgroup-unreads))
3957             (if gnus-newsgroup-auto-expire
3958                 (push number gnus-newsgroup-expirable)
3959               (push (cons number gnus-low-score-mark)
3960                     gnus-newsgroup-reads))))
3961
3962           (when gnus-tmp-header
3963             ;; We may have an old dummy line to output before this
3964             ;; article.
3965             (when (and gnus-tmp-dummy-line
3966                        (gnus-subject-equal
3967                         gnus-tmp-dummy-line
3968                         (mail-header-subject gnus-tmp-header)))
3969               (gnus-summary-insert-dummy-line
3970                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3971               (setq gnus-tmp-dummy-line nil))
3972
3973             ;; Compute the mark.
3974             (setq gnus-tmp-unread (gnus-article-mark number))
3975
3976             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3977                                   gnus-tmp-header gnus-tmp-level)
3978                   gnus-newsgroup-data)
3979
3980             ;; Actually insert the line.
3981             (setq
3982              gnus-tmp-subject-or-nil
3983              (cond
3984               ((and gnus-thread-ignore-subject
3985                     gnus-tmp-prev-subject
3986                     (not (inline (gnus-subject-equal
3987                                   gnus-tmp-prev-subject subject))))
3988                subject)
3989               ((zerop gnus-tmp-level)
3990                (if (and (eq gnus-summary-make-false-root 'empty)
3991                         (memq number gnus-tmp-gathered)
3992                         gnus-tmp-prev-subject
3993                         (inline (gnus-subject-equal
3994                                  gnus-tmp-prev-subject subject)))
3995                    gnus-summary-same-subject
3996                  subject))
3997               (t gnus-summary-same-subject)))
3998             (if (and (eq gnus-summary-make-false-root 'adopt)
3999                      (= gnus-tmp-level 1)
4000                      (memq number gnus-tmp-gathered))
4001                 (setq gnus-tmp-opening-bracket ?\<
4002                       gnus-tmp-closing-bracket ?\>)
4003               (setq gnus-tmp-opening-bracket ?\[
4004                     gnus-tmp-closing-bracket ?\]))
4005             (setq
4006              gnus-tmp-indentation
4007              (aref gnus-thread-indent-array gnus-tmp-level)
4008              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4009              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4010                                 gnus-summary-default-score 0)
4011              gnus-tmp-score-char
4012              (if (or (null gnus-summary-default-score)
4013                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4014                          gnus-summary-zcore-fuzz))
4015                  ?  ;Whitespace
4016                (if (< gnus-tmp-score gnus-summary-default-score)
4017                    gnus-score-below-mark gnus-score-over-mark))
4018              gnus-tmp-replied
4019              (cond ((memq number gnus-newsgroup-processable)
4020                     gnus-process-mark)
4021                    ((memq number gnus-newsgroup-cached)
4022                     gnus-cached-mark)
4023                    ((memq number gnus-newsgroup-replied)
4024                     gnus-replied-mark)
4025                    ((memq number gnus-newsgroup-saved)
4026                     gnus-saved-mark)
4027                    (t gnus-unread-mark))
4028              gnus-tmp-from (mail-header-from gnus-tmp-header)
4029              gnus-tmp-name
4030              (cond
4031               ((string-match "<[^>]+> *$" gnus-tmp-from)
4032                (setq beg-match (match-beginning 0))
4033                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4034                         (substring gnus-tmp-from (1+ (match-beginning 0))
4035                                    (1- (match-end 0))))
4036                    (substring gnus-tmp-from 0 beg-match)))
4037               ((string-match "(.+)" gnus-tmp-from)
4038                (substring gnus-tmp-from
4039                           (1+ (match-beginning 0)) (1- (match-end 0))))
4040               (t gnus-tmp-from)))
4041             (when (string= gnus-tmp-name "")
4042               (setq gnus-tmp-name gnus-tmp-from))
4043             (unless (numberp gnus-tmp-lines)
4044               (setq gnus-tmp-lines 0))
4045             (gnus-put-text-property
4046              (point)
4047              (progn (eval gnus-summary-line-format-spec) (point))
4048              'gnus-number number)
4049             (when gnus-visual-p
4050               (forward-line -1)
4051               (gnus-run-hooks 'gnus-summary-update-hook)
4052               (forward-line 1))
4053
4054             (setq gnus-tmp-prev-subject subject)))
4055
4056         (when (nth 1 thread)
4057           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4058         (incf gnus-tmp-level)
4059         (setq threads (if thread-end nil (cdar thread)))
4060         (unless threads
4061           (setq gnus-tmp-level 0)))))
4062   (gnus-message 7 "Generating summary...done"))
4063
4064 (defun gnus-summary-prepare-unthreaded (headers)
4065   "Generate an unthreaded summary buffer based on HEADERS."
4066   (let (header number mark)
4067
4068     (beginning-of-line)
4069
4070     (while headers
4071       ;; We may have to root out some bad articles...
4072       (when (memq (setq number (mail-header-number
4073                                 (setq header (pop headers))))
4074                   gnus-newsgroup-limit)
4075         ;; Mark article as read when it has a low score.
4076         (when (and gnus-summary-mark-below
4077                    (< (or (cdr (assq number gnus-newsgroup-scored))
4078                           gnus-summary-default-score 0)
4079                       gnus-summary-mark-below)
4080                    (not (gnus-summary-article-ancient-p number)))
4081           (setq gnus-newsgroup-unreads
4082                 (delq number gnus-newsgroup-unreads))
4083           (if gnus-newsgroup-auto-expire
4084               (push number gnus-newsgroup-expirable)
4085             (push (cons number gnus-low-score-mark)
4086                   gnus-newsgroup-reads)))
4087
4088         (setq mark (gnus-article-mark number))
4089         (push (gnus-data-make number mark (1+ (point)) header 0)
4090               gnus-newsgroup-data)
4091         (gnus-summary-insert-line
4092          header 0 number
4093          mark (memq number gnus-newsgroup-replied)
4094          (memq number gnus-newsgroup-expirable)
4095          (mail-header-subject header) nil
4096          (cdr (assq number gnus-newsgroup-scored))
4097          (memq number gnus-newsgroup-processable))))))
4098
4099 (defun gnus-summary-remove-list-identifiers ()
4100   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4101   (let ((regexp (if (stringp gnus-list-identifiers)
4102                     gnus-list-identifiers
4103                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4104     (dolist (header gnus-newsgroup-headers)
4105       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4106                                   " *\\)\\)+\\(Re: +\\)?\\)")
4107                           (mail-header-subject header))
4108         (mail-header-set-subject
4109          header (concat (substring (mail-header-subject header)
4110                                    0 (match-beginning 1))
4111                         (or
4112                          (match-string 3 (mail-header-subject header))
4113                          (match-string 5 (mail-header-subject header)))
4114                         (substring (mail-header-subject header)
4115                                    (match-end 1))))))))
4116
4117 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4118   "Select newsgroup GROUP.
4119 If READ-ALL is non-nil, all articles in the group are selected.
4120 If SELECT-ARTICLES, only select those articles from GROUP."
4121   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4122          ;;!!! Dirty hack; should be removed.
4123          (gnus-summary-ignore-duplicates
4124           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4125               t
4126             gnus-summary-ignore-duplicates))
4127          (info (nth 2 entry))
4128          articles fetched-articles cached)
4129
4130     (unless (gnus-check-server
4131              (setq gnus-current-select-method
4132                    (gnus-find-method-for-group group)))
4133       (error "Couldn't open server"))
4134
4135     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4136         (gnus-activate-group group)     ; Or we can activate it...
4137         (progn                          ; Or we bug out.
4138           (when (equal major-mode 'gnus-summary-mode)
4139             (kill-buffer (current-buffer)))
4140           (error "Couldn't request group %s: %s"
4141                  group (gnus-status-message group))))
4142
4143     (unless (gnus-request-group group t)
4144       (when (equal major-mode 'gnus-summary-mode)
4145         (kill-buffer (current-buffer)))
4146       (error "Couldn't request group %s: %s"
4147              group (gnus-status-message group)))
4148
4149     (setq gnus-newsgroup-name group)
4150     (setq gnus-newsgroup-unselected nil)
4151     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4152     (gnus-summary-setup-default-charset)
4153
4154     ;; Adjust and set lists of article marks.
4155     (when info
4156       (gnus-adjust-marked-articles info))
4157
4158     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4159     (when (gnus-virtual-group-p group)
4160       (setq cached gnus-newsgroup-cached))
4161
4162     (setq gnus-newsgroup-unreads
4163           (gnus-set-difference
4164            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4165            gnus-newsgroup-dormant))
4166
4167     (setq gnus-newsgroup-processable nil)
4168
4169     (gnus-update-read-articles group gnus-newsgroup-unreads)
4170
4171     (if (setq articles select-articles)
4172         (setq gnus-newsgroup-unselected
4173               (gnus-sorted-intersection
4174                gnus-newsgroup-unreads
4175                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4176       (setq articles (gnus-articles-to-read group read-all)))
4177
4178     (cond
4179      ((null articles)
4180       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4181       'quit)
4182      ((eq articles 0) nil)
4183      (t
4184       ;; Init the dependencies hash table.
4185       (setq gnus-newsgroup-dependencies
4186             (gnus-make-hashtable (length articles)))
4187       (gnus-set-global-variables)
4188       ;; Retrieve the headers and read them in.
4189       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4190       (setq gnus-newsgroup-headers
4191             (if (eq 'nov
4192                     (setq gnus-headers-retrieved-by
4193                           (gnus-retrieve-headers
4194                            articles gnus-newsgroup-name
4195                            ;; We might want to fetch old headers, but
4196                            ;; not if there is only 1 article.
4197                            (and (or (and
4198                                      (not (eq gnus-fetch-old-headers 'some))
4199                                      (not (numberp gnus-fetch-old-headers)))
4200                                     (> (length articles) 1))
4201                                 gnus-fetch-old-headers))))
4202                 (gnus-get-newsgroup-headers-xover
4203                  articles nil nil gnus-newsgroup-name t)
4204               (gnus-get-newsgroup-headers)))
4205       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4206
4207       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4208       (when cached
4209         (setq gnus-newsgroup-cached cached))
4210
4211       ;; Suppress duplicates?
4212       (when gnus-suppress-duplicates
4213         (gnus-dup-suppress-articles))
4214
4215       ;; Set the initial limit.
4216       (setq gnus-newsgroup-limit (copy-sequence articles))
4217       ;; Remove canceled articles from the list of unread articles.
4218       (setq gnus-newsgroup-unreads
4219             (gnus-set-sorted-intersection
4220              gnus-newsgroup-unreads
4221              (setq fetched-articles
4222                    (mapcar (lambda (headers) (mail-header-number headers))
4223                            gnus-newsgroup-headers))))
4224       ;; Removed marked articles that do not exist.
4225       (gnus-update-missing-marks
4226        (gnus-sorted-complement fetched-articles articles))
4227       ;; We might want to build some more threads first.
4228       (when (and gnus-fetch-old-headers
4229                  (eq gnus-headers-retrieved-by 'nov))
4230         (if (eq gnus-fetch-old-headers 'invisible)
4231             (gnus-build-all-threads)
4232           (gnus-build-old-threads)))
4233       ;; Let the Gnus agent mark articles as read.
4234       (when gnus-agent
4235         (gnus-agent-get-undownloaded-list))
4236       ;; Remove list identifiers from subject
4237       (when gnus-list-identifiers
4238         (gnus-summary-remove-list-identifiers))
4239       ;; Check whether auto-expire is to be done in this group.
4240       (setq gnus-newsgroup-auto-expire
4241             (gnus-group-auto-expirable-p group))
4242       ;; Set up the article buffer now, if necessary.
4243       (unless gnus-single-article-buffer
4244         (gnus-article-setup-buffer))
4245       ;; First and last article in this newsgroup.
4246       (when gnus-newsgroup-headers
4247         (setq gnus-newsgroup-begin
4248               (mail-header-number (car gnus-newsgroup-headers))
4249               gnus-newsgroup-end
4250               (mail-header-number
4251                (gnus-last-element gnus-newsgroup-headers))))
4252       ;; GROUP is successfully selected.
4253       (or gnus-newsgroup-headers t)))))
4254
4255 (defun gnus-articles-to-read (group &optional read-all)
4256   "Find out what articles the user wants to read."
4257   (let* ((articles
4258           ;; Select all articles if `read-all' is non-nil, or if there
4259           ;; are no unread articles.
4260           (if (or read-all
4261                   (and (zerop (length gnus-newsgroup-marked))
4262                        (zerop (length gnus-newsgroup-unreads)))
4263                   (eq (gnus-group-find-parameter group 'display)
4264                       'all))
4265               (or
4266                (gnus-uncompress-range (gnus-active group))
4267                (gnus-cache-articles-in-group group))
4268             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4269                           (copy-sequence gnus-newsgroup-unreads))
4270                   '<)))
4271          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4272          (scored (length scored-list))
4273          (number (length articles))
4274          (marked (+ (length gnus-newsgroup-marked)
4275                     (length gnus-newsgroup-dormant)))
4276          (select
4277           (cond
4278            ((numberp read-all)
4279             read-all)
4280            (t
4281             (condition-case ()
4282                 (cond
4283                  ((and (or (<= scored marked) (= scored number))
4284                        (numberp gnus-large-newsgroup)
4285                        (> number gnus-large-newsgroup))
4286                   (let ((input
4287                          (read-string
4288                           (format
4289                            "How many articles from %s (default %d): "
4290                            (gnus-limit-string gnus-newsgroup-name 35)
4291                            number))))
4292                     (if (string-match "^[ \t]*$" input) number input)))
4293                  ((and (> scored marked) (< scored number)
4294                        (> (- scored number) 20))
4295                   (let ((input
4296                          (read-string
4297                           (format "%s %s (%d scored, %d total): "
4298                                   "How many articles from"
4299                                   group scored number))))
4300                     (if (string-match "^[ \t]*$" input)
4301                         number input)))
4302                  (t number))
4303               (quit nil))))))
4304     (setq select (if (stringp select) (string-to-number select) select))
4305     (if (or (null select) (zerop select))
4306         select
4307       (if (and (not (zerop scored)) (<= (abs select) scored))
4308           (progn
4309             (setq articles (sort scored-list '<))
4310             (setq number (length articles)))
4311         (setq articles (copy-sequence articles)))
4312
4313       (when (< (abs select) number)
4314         (if (< select 0)
4315             ;; Select the N oldest articles.
4316             (setcdr (nthcdr (1- (abs select)) articles) nil)
4317           ;; Select the N most recent articles.
4318           (setq articles (nthcdr (- number select) articles))))
4319       (setq gnus-newsgroup-unselected
4320             (gnus-sorted-intersection
4321              gnus-newsgroup-unreads
4322              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4323       (when gnus-alter-articles-to-read-function
4324         (setq gnus-newsgroup-unreads
4325               (sort 
4326                (funcall gnus-alter-articles-to-read-function
4327                         gnus-newsgroup-name gnus-newsgroup-unreads)
4328                '<)))
4329       articles)))
4330
4331 (defun gnus-killed-articles (killed articles)
4332   (let (out)
4333     (while articles
4334       (when (inline (gnus-member-of-range (car articles) killed))
4335         (push (car articles) out))
4336       (setq articles (cdr articles)))
4337     out))
4338
4339 (defun gnus-uncompress-marks (marks)
4340   "Uncompress the mark ranges in MARKS."
4341   (let ((uncompressed '(score bookmark))
4342         out)
4343     (while marks
4344       (if (memq (caar marks) uncompressed)
4345           (push (car marks) out)
4346         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4347       (setq marks (cdr marks)))
4348     out))
4349
4350 (defun gnus-adjust-marked-articles (info)
4351   "Set all article lists and remove all marks that are no longer valid."
4352   (let* ((marked-lists (gnus-info-marks info))
4353          (active (gnus-active (gnus-info-group info)))
4354          (min (car active))
4355          (max (cdr active))
4356          (types gnus-article-mark-lists)
4357          (uncompressed '(score bookmark killed))
4358          marks var articles article mark)
4359
4360     (while marked-lists
4361       (setq marks (pop marked-lists))
4362       (set (setq var (intern (format "gnus-newsgroup-%s"
4363                                      (car (rassq (setq mark (car marks))
4364                                                  types)))))
4365            (if (memq (car marks) uncompressed) (cdr marks)
4366              (gnus-uncompress-range (cdr marks))))
4367
4368       (setq articles (symbol-value var))
4369
4370       ;; All articles have to be subsets of the active articles.
4371       (cond
4372        ;; Adjust "simple" lists.
4373        ((memq mark '(tick dormant expire reply save))
4374         (while articles
4375           (when (or (< (setq article (pop articles)) min) (> article max))
4376             (set var (delq article (symbol-value var))))))
4377        ;; Adjust assocs.
4378        ((memq mark uncompressed)
4379         (when (not (listp (cdr (symbol-value var))))
4380           (set var (list (symbol-value var))))
4381         (when (not (listp (cdr articles)))
4382           (setq articles (list articles)))
4383         (while articles
4384           (when (or (not (consp (setq article (pop articles))))
4385                     (< (car article) min)
4386                     (> (car article) max))
4387             (set var (delq article (symbol-value var))))))))))
4388
4389 (defun gnus-update-missing-marks (missing)
4390   "Go through the list of MISSING articles and remove them from the mark lists."
4391   (when missing
4392     (let ((types gnus-article-mark-lists)
4393           var m)
4394       ;; Go through all types.
4395       (while types
4396         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4397         (when (symbol-value var)
4398           ;; This list has articles.  So we delete all missing articles
4399           ;; from it.
4400           (setq m missing)
4401           (while m
4402             (set var (delq (pop m) (symbol-value var)))))))))
4403
4404 (defun gnus-update-marks ()
4405   "Enter the various lists of marked articles into the newsgroup info list."
4406   (let ((types gnus-article-mark-lists)
4407         (info (gnus-get-info gnus-newsgroup-name))
4408         (uncompressed '(score bookmark killed))
4409         type list newmarked symbol delta-marks)
4410     (when info
4411       ;; Add all marks lists to the list of marks lists.
4412       (while (setq type (pop types))
4413         (setq list (symbol-value
4414                     (setq symbol
4415                           (intern (format "gnus-newsgroup-%s"
4416                                           (car type))))))
4417
4418         (when list
4419           ;; Get rid of the entries of the articles that have the
4420           ;; default score.
4421           (when (and (eq (cdr type) 'score)
4422                      gnus-save-score
4423                      list)
4424             (let* ((arts list)
4425                    (prev (cons nil list))
4426                    (all prev))
4427               (while arts
4428                 (if (or (not (consp (car arts)))
4429                         (= (cdar arts) gnus-summary-default-score))
4430                     (setcdr prev (cdr arts))
4431                   (setq prev arts))
4432                 (setq arts (cdr arts)))
4433               (setq list (cdr all)))))
4434
4435         (unless (memq (cdr type) uncompressed)
4436           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4437        
4438         (when (gnus-check-backend-function
4439                'request-set-mark gnus-newsgroup-name)
4440           ;; propagate flags to server, with the following exceptions:
4441           ;; uncompressed:s are not proper flags (they are cons cells)
4442           ;; cache is a internal gnus flag
4443           ;; download are local to one gnus installation (well)
4444           ;; unsend are for nndraft groups only
4445           ;; xxx: generality of this?  this suits nnimap anyway
4446           (unless (memq (cdr type) (append '(cache download unsend)
4447                                            uncompressed))
4448             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4449                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4450                    (add (gnus-remove-from-range
4451                          (gnus-copy-sequence list) old)))
4452               (when add
4453                 (push (list add 'add (list (cdr type))) delta-marks))
4454               (when del
4455                 (push (list del 'del (list (cdr type))) delta-marks)))))
4456           
4457         (when list
4458           (push (cons (cdr type) list) newmarked)))
4459
4460       (when delta-marks
4461         (unless (gnus-check-group gnus-newsgroup-name)
4462           (error "Can't open server for %s" gnus-newsgroup-name))
4463         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4464           
4465       ;; Enter these new marks into the info of the group.
4466       (if (nthcdr 3 info)
4467           (setcar (nthcdr 3 info) newmarked)
4468         ;; Add the marks lists to the end of the info.
4469         (when newmarked
4470           (setcdr (nthcdr 2 info) (list newmarked))))
4471
4472       ;; Cut off the end of the info if there's nothing else there.
4473       (let ((i 5))
4474         (while (and (> i 2)
4475                     (not (nth i info)))
4476           (when (nthcdr (decf i) info)
4477             (setcdr (nthcdr i info) nil)))))))
4478
4479 (defun gnus-set-mode-line (where)
4480   "Set the mode line of the article or summary buffers.
4481 If WHERE is `summary', the summary mode line format will be used."
4482   ;; Is this mode line one we keep updated?
4483   (when (and (memq where gnus-updated-mode-lines)
4484              (symbol-value
4485               (intern (format "gnus-%s-mode-line-format-spec" where))))
4486     (let (mode-string)
4487       (save-excursion
4488         ;; We evaluate this in the summary buffer since these
4489         ;; variables are buffer-local to that buffer.
4490         (set-buffer gnus-summary-buffer)
4491         ;; We bind all these variables that are used in the `eval' form
4492         ;; below.
4493         (let* ((mformat (symbol-value
4494                          (intern
4495                           (format "gnus-%s-mode-line-format-spec" where))))
4496                (gnus-tmp-group-name (gnus-group-name-decode 
4497                                      gnus-newsgroup-name
4498                                      (gnus-group-name-charset 
4499                                       nil
4500                                       gnus-newsgroup-name)))
4501                (gnus-tmp-article-number (or gnus-current-article 0))
4502                (gnus-tmp-unread gnus-newsgroup-unreads)
4503                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4504                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4505                (gnus-tmp-unread-and-unselected
4506                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4507                             (zerop gnus-tmp-unselected))
4508                        "")
4509                       ((zerop gnus-tmp-unselected)
4510                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4511                       (t (format "{%d(+%d) more}"
4512                                  gnus-tmp-unread-and-unticked
4513                                  gnus-tmp-unselected))))
4514                (gnus-tmp-subject
4515                 (if (and gnus-current-headers
4516                          (vectorp gnus-current-headers))
4517                     (gnus-mode-string-quote
4518                      (mail-header-subject gnus-current-headers))
4519                   ""))
4520                bufname-length max-len
4521                gnus-tmp-header);; passed as argument to any user-format-funcs
4522           (setq mode-string (eval mformat))
4523           (setq bufname-length (if (string-match "%b" mode-string)
4524                                    (- (length
4525                                        (buffer-name
4526                                         (if (eq where 'summary)
4527                                             nil
4528                                           (get-buffer gnus-article-buffer))))
4529                                       2)
4530                                  0))
4531           (setq max-len (max 4 (if gnus-mode-non-string-length
4532                                    (- (window-width)
4533                                       gnus-mode-non-string-length
4534                                       bufname-length)
4535                                  (length mode-string))))
4536           ;; We might have to chop a bit of the string off...
4537           (when (> (length mode-string) max-len)
4538             (setq mode-string
4539                   (concat (truncate-string-to-width mode-string (- max-len 3))
4540                           "...")))
4541           ;; Pad the mode string a bit.
4542           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4543       ;; Update the mode line.
4544       (setq mode-line-buffer-identification
4545             (gnus-mode-line-buffer-identification (list mode-string)))
4546       (set-buffer-modified-p t))))
4547
4548 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4549   "Go through the HEADERS list and add all Xrefs to a hash table.
4550 The resulting hash table is returned, or nil if no Xrefs were found."
4551   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4552          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4553          (xref-hashtb (gnus-make-hashtable))
4554          start group entry number xrefs header)
4555     (while headers
4556       (setq header (pop headers))
4557       (when (and (setq xrefs (mail-header-xref header))
4558                  (not (memq (setq number (mail-header-number header))
4559                             unreads)))
4560         (setq start 0)
4561         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4562           (setq start (match-end 0))
4563           (setq group (if prefix
4564                           (concat prefix (substring xrefs (match-beginning 1)
4565                                                     (match-end 1)))
4566                         (substring xrefs (match-beginning 1) (match-end 1))))
4567           (setq number
4568                 (string-to-int (substring xrefs (match-beginning 2)
4569                                           (match-end 2))))
4570           (if (setq entry (gnus-gethash group xref-hashtb))
4571               (setcdr entry (cons number (cdr entry)))
4572             (gnus-sethash group (cons number nil) xref-hashtb)))))
4573     (and start xref-hashtb)))
4574
4575 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4576   "Look through all the headers and mark the Xrefs as read."
4577   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4578         name entry info xref-hashtb idlist method nth4)
4579     (save-excursion
4580       (set-buffer gnus-group-buffer)
4581       (when (setq xref-hashtb
4582                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4583         (mapatoms
4584          (lambda (group)
4585            (unless (string= from-newsgroup (setq name (symbol-name group)))
4586              (setq idlist (symbol-value group))
4587              ;; Dead groups are not updated.
4588              (and (prog1
4589                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4590                             info (nth 2 entry))
4591                     (when (stringp (setq nth4 (gnus-info-method info)))
4592                       (setq nth4 (gnus-server-to-method nth4))))
4593                   ;; Only do the xrefs if the group has the same
4594                   ;; select method as the group we have just read.
4595                   (or (gnus-methods-equal-p
4596                        nth4 (gnus-find-method-for-group from-newsgroup))
4597                       virtual
4598                       (equal nth4 (setq method (gnus-find-method-for-group
4599                                                 from-newsgroup)))
4600                       (and (equal (car nth4) (car method))
4601                            (equal (nth 1 nth4) (nth 1 method))))
4602                   gnus-use-cross-reference
4603                   (or (not (eq gnus-use-cross-reference t))
4604                       virtual
4605                       ;; Only do cross-references on subscribed
4606                       ;; groups, if that is what is wanted.
4607                       (<= (gnus-info-level info) gnus-level-subscribed))
4608                   (gnus-group-make-articles-read name idlist))))
4609          xref-hashtb)))))
4610
4611 (defun gnus-compute-read-articles (group articles)
4612   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4613          (info (nth 2 entry))
4614          (active (gnus-active group))
4615          ninfo)
4616     (when entry
4617       ;; First peel off all invalid article numbers.
4618       (when active
4619         (let ((ids articles)
4620               id first)
4621           (while (setq id (pop ids))
4622             (when (and first (> id (cdr active)))
4623               ;; We'll end up in this situation in one particular
4624               ;; obscure situation.  If you re-scan a group and get
4625               ;; a new article that is cross-posted to a different
4626               ;; group that has not been re-scanned, you might get
4627               ;; crossposted article that has a higher number than
4628               ;; Gnus believes possible.  So we re-activate this
4629               ;; group as well.  This might mean doing the
4630               ;; crossposting thingy will *increase* the number
4631               ;; of articles in some groups.  Tsk, tsk.
4632               (setq active (or (gnus-activate-group group) active)))
4633             (when (or (> id (cdr active))
4634                       (< id (car active)))
4635               (setq articles (delq id articles))))))
4636       ;; If the read list is nil, we init it.
4637       (if (and active
4638                (null (gnus-info-read info))
4639                (> (car active) 1))
4640           (setq ninfo (cons 1 (1- (car active))))
4641         (setq ninfo (gnus-info-read info)))
4642       ;; Then we add the read articles to the range.
4643       (gnus-add-to-range
4644        ninfo (setq articles (sort articles '<))))))
4645
4646 (defun gnus-group-make-articles-read (group articles)
4647   "Update the info of GROUP to say that ARTICLES are read."
4648   (let* ((num 0)
4649          (entry (gnus-gethash group gnus-newsrc-hashtb))
4650          (info (nth 2 entry))
4651          (active (gnus-active group))
4652          range)
4653     (when entry
4654       (setq range (gnus-compute-read-articles group articles))
4655       (save-excursion
4656         (set-buffer gnus-group-buffer)
4657         (gnus-undo-register
4658           `(progn
4659              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4660              (gnus-info-set-read ',info ',(gnus-info-read info))
4661              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4662              (gnus-group-update-group ,group t))))
4663       ;; Add the read articles to the range.
4664       (gnus-info-set-read info range)
4665       ;; Then we have to re-compute how many unread
4666       ;; articles there are in this group.
4667       (when active
4668         (cond
4669          ((not range)
4670           (setq num (- (1+ (cdr active)) (car active))))
4671          ((not (listp (cdr range)))
4672           (setq num (- (cdr active) (- (1+ (cdr range))
4673                                        (car range)))))
4674          (t
4675           (while range
4676             (if (numberp (car range))
4677                 (setq num (1+ num))
4678               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4679             (setq range (cdr range)))
4680           (setq num (- (cdr active) num))))
4681         ;; Update the number of unread articles.
4682         (setcar entry num)
4683         ;; Update the group buffer.
4684         (gnus-group-update-group group t)))))
4685
4686 (defvar gnus-newsgroup-none-id 0)
4687
4688 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4689   (let ((cur nntp-server-buffer)
4690         (dependencies
4691          (or dependencies
4692              (save-excursion (set-buffer gnus-summary-buffer)
4693                              gnus-newsgroup-dependencies)))
4694         headers id end ref
4695         (mail-parse-charset gnus-newsgroup-charset)
4696         (mail-parse-ignored-charsets
4697          (save-excursion (condition-case nil
4698                              (set-buffer gnus-summary-buffer)
4699                            (error))
4700                          gnus-newsgroup-ignored-charsets)))
4701     (save-excursion
4702       (set-buffer nntp-server-buffer)
4703       ;; Translate all TAB characters into SPACE characters.
4704       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4705       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4706       (gnus-run-hooks 'gnus-parse-headers-hook)
4707       (let ((case-fold-search t)
4708             in-reply-to header p lines chars)
4709         (goto-char (point-min))
4710         ;; Search to the beginning of the next header.  Error messages
4711         ;; do not begin with 2 or 3.
4712         (while (re-search-forward "^[23][0-9]+ " nil t)
4713           (setq id nil
4714                 ref nil)
4715           ;; This implementation of this function, with nine
4716           ;; search-forwards instead of the one re-search-forward and
4717           ;; a case (which basically was the old function) is actually
4718           ;; about twice as fast, even though it looks messier.  You
4719           ;; can't have everything, I guess.  Speed and elegance
4720           ;; doesn't always go hand in hand.
4721           (setq
4722            header
4723            (vector
4724             ;; Number.
4725             (prog1
4726                 (read cur)
4727               (end-of-line)
4728               (setq p (point))
4729               (narrow-to-region (point)
4730                                 (or (and (search-forward "\n.\n" nil t)
4731                                          (- (point) 2))
4732                                     (point))))
4733             ;; Subject.
4734             (progn
4735               (goto-char p)
4736               (if (search-forward "\nsubject: " nil t)
4737                   (funcall gnus-decode-encoded-word-function
4738                            (nnheader-header-value))
4739                 "(none)"))
4740             ;; From.
4741             (progn
4742               (goto-char p)
4743               (if (search-forward "\nfrom: " nil t)
4744                   (funcall gnus-decode-encoded-word-function
4745                            (nnheader-header-value))
4746                 "(nobody)"))
4747             ;; Date.
4748             (progn
4749               (goto-char p)
4750               (if (search-forward "\ndate: " nil t)
4751                   (nnheader-header-value) ""))
4752             ;; Message-ID.
4753             (progn
4754               (goto-char p)
4755               (setq id (if (re-search-forward
4756                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4757                            ;; We do it this way to make sure the Message-ID
4758                            ;; is (somewhat) syntactically valid.
4759                            (buffer-substring (match-beginning 1)
4760                                              (match-end 1))
4761                          ;; If there was no message-id, we just fake one
4762                          ;; to make subsequent routines simpler.
4763                          (nnheader-generate-fake-message-id))))
4764             ;; References.
4765             (progn
4766               (goto-char p)
4767               (if (search-forward "\nreferences: " nil t)
4768                   (progn
4769                     (setq end (point))
4770                     (prog1
4771                         (nnheader-header-value)
4772                       (setq ref
4773                             (buffer-substring
4774                              (progn
4775                                (end-of-line)
4776                                (search-backward ">" end t)
4777                                (1+ (point)))
4778                              (progn
4779                                (search-backward "<" end t)
4780                                (point))))))
4781                 ;; Get the references from the in-reply-to header if there
4782                 ;; were no references and the in-reply-to header looks
4783                 ;; promising.
4784                 (if (and (search-forward "\nin-reply-to: " nil t)
4785                          (setq in-reply-to (nnheader-header-value))
4786                          (string-match "<[^>]+>" in-reply-to))
4787                     (let (ref2)
4788                       (setq ref (substring in-reply-to (match-beginning 0)
4789                                            (match-end 0)))
4790                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4791                         (setq ref2 (substring in-reply-to (match-beginning 0)
4792                                               (match-end 0)))
4793                         (when (> (length ref2) (length ref))
4794                           (setq ref ref2)))
4795                       ref)
4796                   (setq ref nil))))
4797             ;; Chars.
4798             (progn
4799               (goto-char p)
4800               (if (search-forward "\nchars: " nil t)
4801                   (if (numberp (setq chars (ignore-errors (read cur))))
4802                       chars 0)
4803                 0))
4804             ;; Lines.
4805             (progn
4806               (goto-char p)
4807               (if (search-forward "\nlines: " nil t)
4808                   (if (numberp (setq lines (ignore-errors (read cur))))
4809                       lines 0)
4810                 0))
4811             ;; Xref.
4812             (progn
4813               (goto-char p)
4814               (and (search-forward "\nxref: " nil t)
4815                    (nnheader-header-value)))
4816             ;; Extra.
4817             (when gnus-extra-headers
4818               (let ((extra gnus-extra-headers)
4819                     out)
4820                 (while extra
4821                   (goto-char p)
4822                   (when (search-forward
4823                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4824                     (push (cons (car extra) (nnheader-header-value))
4825                           out))
4826                   (pop extra))
4827                 out))))
4828           (when (equal id ref)
4829             (setq ref nil))
4830
4831           (when gnus-alter-header-function
4832             (funcall gnus-alter-header-function header)
4833             (setq id (mail-header-id header)
4834                   ref (gnus-parent-id (mail-header-references header))))
4835
4836           (when (setq header
4837                       (gnus-dependencies-add-header
4838                        header dependencies force-new))
4839             (push header headers))
4840           (goto-char (point-max))
4841           (widen))
4842         (nreverse headers)))))
4843
4844 ;; Goes through the xover lines and returns a list of vectors
4845 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4846                                                   force-new dependencies
4847                                                   group also-fetch-heads)
4848   "Parse the news overview data in the server buffer.
4849 Return a list of headers that match SEQUENCE (see
4850 `nntp-retrieve-headers')."
4851   ;; Get the Xref when the users reads the articles since most/some
4852   ;; NNTP servers do not include Xrefs when using XOVER.
4853   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4854   (let ((mail-parse-charset gnus-newsgroup-charset)
4855         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4856         (cur nntp-server-buffer)
4857         (dependencies (or dependencies gnus-newsgroup-dependencies))
4858         number headers header)
4859     (save-excursion
4860       (set-buffer nntp-server-buffer)
4861       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4862       ;; Allow the user to mangle the headers before parsing them.
4863       (gnus-run-hooks 'gnus-parse-headers-hook)
4864       (goto-char (point-min))
4865       (while (not (eobp))
4866         (condition-case ()
4867             (while (and sequence (not (eobp)))
4868               (setq number (read cur))
4869               (while (and sequence
4870                           (< (car sequence) number))
4871                 (setq sequence (cdr sequence)))
4872               (and sequence
4873                    (eq number (car sequence))
4874                    (progn
4875                      (setq sequence (cdr sequence))
4876                      (setq header (inline
4877                                     (gnus-nov-parse-line
4878                                      number dependencies force-new))))
4879                    (push header headers))
4880               (forward-line 1))
4881           (error
4882            (gnus-error 4 "Strange nov line (%d)"
4883                        (count-lines (point-min) (point)))))
4884         (forward-line 1))
4885       ;; A common bug in inn is that if you have posted an article and
4886       ;; then retrieves the active file, it will answer correctly --
4887       ;; the new article is included.  However, a NOV entry for the
4888       ;; article may not have been generated yet, so this may fail.
4889       ;; We work around this problem by retrieving the last few
4890       ;; headers using HEAD.
4891       (if (or (not also-fetch-heads)
4892               (not sequence))
4893           ;; We (probably) got all the headers.
4894           (nreverse headers)
4895         (let ((gnus-nov-is-evil t))
4896           (nconc
4897            (nreverse headers)
4898            (when (gnus-retrieve-headers sequence group)
4899              (gnus-get-newsgroup-headers))))))))
4900
4901 (defun gnus-article-get-xrefs ()
4902   "Fill in the Xref value in `gnus-current-headers', if necessary.
4903 This is meant to be called in `gnus-article-internal-prepare-hook'."
4904   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4905                                  gnus-current-headers)))
4906     (or (not gnus-use-cross-reference)
4907         (not headers)
4908         (and (mail-header-xref headers)
4909              (not (string= (mail-header-xref headers) "")))
4910         (let ((case-fold-search t)
4911               xref)
4912           (save-restriction
4913             (nnheader-narrow-to-headers)
4914             (goto-char (point-min))
4915             (when (or (and (not (eobp))
4916                            (eq (downcase (char-after)) ?x)
4917                            (looking-at "Xref:"))
4918                       (search-forward "\nXref:" nil t))
4919               (goto-char (1+ (match-end 0)))
4920               (setq xref (buffer-substring (point)
4921                                            (progn (end-of-line) (point))))
4922               (mail-header-set-xref headers xref)))))))
4923
4924 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4925   "Find article ID and insert the summary line for that article.
4926 OLD-HEADER can either be a header or a line number to insert
4927 the subject line on."
4928   (let* ((line (and (numberp old-header) old-header))
4929          (old-header (and (vectorp old-header) old-header))
4930          (header (cond ((and old-header use-old-header)
4931                         old-header)
4932                        ((and (numberp id)
4933                              (gnus-number-to-header id))
4934                         (gnus-number-to-header id))
4935                        (t
4936                         (gnus-read-header id))))
4937          (number (and (numberp id) id))
4938          d)
4939     (when header
4940       ;; Rebuild the thread that this article is part of and go to the
4941       ;; article we have fetched.
4942       (when (and (not gnus-show-threads)
4943                  old-header)
4944         (when (and number
4945                    (setq d (gnus-data-find (mail-header-number old-header))))
4946           (goto-char (gnus-data-pos d))
4947           (gnus-data-remove
4948            number
4949            (- (gnus-point-at-bol)
4950               (prog1
4951                   (1+ (gnus-point-at-eol))
4952                 (gnus-delete-line))))))
4953       (when old-header
4954         (mail-header-set-number header (mail-header-number old-header)))
4955       (setq gnus-newsgroup-sparse
4956             (delq (setq number (mail-header-number header))
4957                   gnus-newsgroup-sparse))
4958       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4959       (push number gnus-newsgroup-limit)
4960       (gnus-rebuild-thread (mail-header-id header) line)
4961       (gnus-summary-goto-subject number nil t))
4962     (when (and (numberp number)
4963                (> number 0))
4964       ;; We have to update the boundaries even if we can't fetch the
4965       ;; article if ID is a number -- so that the next `P' or `N'
4966       ;; command will fetch the previous (or next) article even
4967       ;; if the one we tried to fetch this time has been canceled.
4968       (when (> number gnus-newsgroup-end)
4969         (setq gnus-newsgroup-end number))
4970       (when (< number gnus-newsgroup-begin)
4971         (setq gnus-newsgroup-begin number))
4972       (setq gnus-newsgroup-unselected
4973             (delq number gnus-newsgroup-unselected)))
4974     ;; Report back a success?
4975     (and header (mail-header-number header))))
4976
4977 ;;; Process/prefix in the summary buffer
4978
4979 (defun gnus-summary-work-articles (n)
4980   "Return a list of articles to be worked upon.
4981 The prefix argument, the list of process marked articles, and the
4982 current article will be taken into consideration."
4983   (save-excursion
4984     (set-buffer gnus-summary-buffer)
4985     (cond
4986      (n
4987       ;; A numerical prefix has been given.
4988       (setq n (prefix-numeric-value n))
4989       (let ((backward (< n 0))
4990             (n (abs (prefix-numeric-value n)))
4991             articles article)
4992         (save-excursion
4993           (while
4994               (and (> n 0)
4995                    (push (setq article (gnus-summary-article-number))
4996                          articles)
4997                    (if backward
4998                        (gnus-summary-find-prev nil article)
4999                      (gnus-summary-find-next nil article)))
5000             (decf n)))
5001         (nreverse articles)))
5002      ((and (gnus-region-active-p) (mark))
5003       (message "region active")
5004       ;; Work on the region between point and mark.
5005       (let ((max (max (point) (mark)))
5006             articles article)
5007         (save-excursion
5008           (goto-char (min (min (point) (mark))))
5009           (while
5010               (and
5011                (push (setq article (gnus-summary-article-number)) articles)
5012                (gnus-summary-find-next nil article)
5013                (< (point) max)))
5014           (nreverse articles))))
5015      (gnus-newsgroup-processable
5016       ;; There are process-marked articles present.
5017       ;; Save current state.
5018       (gnus-summary-save-process-mark)
5019       ;; Return the list.
5020       (reverse gnus-newsgroup-processable))
5021      (t
5022       ;; Just return the current article.
5023       (list (gnus-summary-article-number))))))
5024
5025 (defmacro gnus-summary-iterate (arg &rest forms)
5026   "Iterate over the process/prefixed articles and do FORMS.
5027 ARG is the interactive prefix given to the command.  FORMS will be
5028 executed with point over the summary line of the articles."
5029   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5030     `(let ((,articles (gnus-summary-work-articles ,arg)))
5031        (while ,articles
5032          (gnus-summary-goto-subject (car ,articles))
5033          ,@forms
5034          (pop ,articles)))))
5035
5036 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5037 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5038
5039 (defun gnus-summary-save-process-mark ()
5040   "Push the current set of process marked articles on the stack."
5041   (interactive)
5042   (push (copy-sequence gnus-newsgroup-processable)
5043         gnus-newsgroup-process-stack))
5044
5045 (defun gnus-summary-kill-process-mark ()
5046   "Push the current set of process marked articles on the stack and unmark."
5047   (interactive)
5048   (gnus-summary-save-process-mark)
5049   (gnus-summary-unmark-all-processable))
5050
5051 (defun gnus-summary-yank-process-mark ()
5052   "Pop the last process mark state off the stack and restore it."
5053   (interactive)
5054   (unless gnus-newsgroup-process-stack
5055     (error "Empty mark stack"))
5056   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5057
5058 (defun gnus-summary-process-mark-set (set)
5059   "Make SET into the current process marked articles."
5060   (gnus-summary-unmark-all-processable)
5061   (while set
5062     (gnus-summary-set-process-mark (pop set))))
5063
5064 ;;; Searching and stuff
5065
5066 (defun gnus-summary-search-group (&optional backward use-level)
5067   "Search for next unread newsgroup.
5068 If optional argument BACKWARD is non-nil, search backward instead."
5069   (save-excursion
5070     (set-buffer gnus-group-buffer)
5071     (when (gnus-group-search-forward
5072            backward nil (if use-level (gnus-group-group-level) nil))
5073       (gnus-group-group-name))))
5074
5075 (defun gnus-summary-best-group (&optional exclude-group)
5076   "Find the name of the best unread group.
5077 If EXCLUDE-GROUP, do not go to this group."
5078   (save-excursion
5079     (set-buffer gnus-group-buffer)
5080     (save-excursion
5081       (gnus-group-best-unread-group exclude-group))))
5082
5083 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5084   (if backward (gnus-summary-find-prev)
5085     (let* ((dummy (gnus-summary-article-intangible-p))
5086            (article (or article (gnus-summary-article-number)))
5087            (arts (gnus-data-find-list article))
5088            result)
5089       (when (and (not dummy)
5090                  (or (not gnus-summary-check-current)
5091                      (not unread)
5092                      (not (gnus-data-unread-p (car arts)))))
5093         (setq arts (cdr arts)))
5094       (when (setq result
5095                   (if unread
5096                       (progn
5097                         (while arts
5098                           (when (or (and undownloaded
5099                                          (eq gnus-undownloaded-mark
5100                                              (gnus-data-mark (car arts))))
5101                                     (gnus-data-unread-p (car arts)))
5102                             (setq result (car arts)
5103                                   arts nil))
5104                           (setq arts (cdr arts)))
5105                         result)
5106                     (car arts)))
5107         (goto-char (gnus-data-pos result))
5108         (gnus-data-number result)))))
5109
5110 (defun gnus-summary-find-prev (&optional unread article)
5111   (let* ((eobp (eobp))
5112          (article (or article (gnus-summary-article-number)))
5113          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5114          result)
5115     (when (and (not eobp)
5116                (or (not gnus-summary-check-current)
5117                    (not unread)
5118                    (not (gnus-data-unread-p (car arts)))))
5119       (setq arts (cdr arts)))
5120     (when (setq result
5121                 (if unread
5122                     (progn
5123                       (while arts
5124                         (when (gnus-data-unread-p (car arts))
5125                           (setq result (car arts)
5126                                 arts nil))
5127                         (setq arts (cdr arts)))
5128                       result)
5129                   (car arts)))
5130       (goto-char (gnus-data-pos result))
5131       (gnus-data-number result))))
5132
5133 (defun gnus-summary-find-subject (subject &optional unread backward article)
5134   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5135          (article (or article (gnus-summary-article-number)))
5136          (articles (gnus-data-list backward))
5137          (arts (gnus-data-find-list article articles))
5138          result)
5139     (when (or (not gnus-summary-check-current)
5140               (not unread)
5141               (not (gnus-data-unread-p (car arts))))
5142       (setq arts (cdr arts)))
5143     (while arts
5144       (and (or (not unread)
5145                (gnus-data-unread-p (car arts)))
5146            (vectorp (gnus-data-header (car arts)))
5147            (gnus-subject-equal
5148             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5149            (setq result (car arts)
5150                  arts nil))
5151       (setq arts (cdr arts)))
5152     (and result
5153          (goto-char (gnus-data-pos result))
5154          (gnus-data-number result))))
5155
5156 (defun gnus-summary-search-forward (&optional unread subject backward)
5157   "Search forward for an article.
5158 If UNREAD, look for unread articles.  If SUBJECT, look for
5159 articles with that subject.  If BACKWARD, search backward instead."
5160   (cond (subject (gnus-summary-find-subject subject unread backward))
5161         (backward (gnus-summary-find-prev unread))
5162         (t (gnus-summary-find-next unread))))
5163
5164 (defun gnus-recenter (&optional n)
5165   "Center point in window and redisplay frame.
5166 Also do horizontal recentering."
5167   (interactive "P")
5168   (when (and gnus-auto-center-summary
5169              (not (eq gnus-auto-center-summary 'vertical)))
5170     (gnus-horizontal-recenter))
5171   (recenter n))
5172
5173 (defun gnus-summary-recenter ()
5174   "Center point in the summary window.
5175 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5176 displayed, no centering will be performed."
5177   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5178   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5179   (interactive)
5180   (let* ((top (cond ((< (window-height) 4) 0)
5181                     ((< (window-height) 7) 1)
5182                     (t (if (numberp gnus-auto-center-summary)
5183                            gnus-auto-center-summary
5184                          2))))
5185          (height (1- (window-height)))
5186          (bottom (save-excursion (goto-char (point-max))
5187                                  (forward-line (- height))
5188                                  (point)))
5189          (window (get-buffer-window (current-buffer))))
5190     ;; The user has to want it.
5191     (when gnus-auto-center-summary
5192       (when (get-buffer-window gnus-article-buffer)
5193         ;; Only do recentering when the article buffer is displayed,
5194         ;; Set the window start to either `bottom', which is the biggest
5195         ;; possible valid number, or the second line from the top,
5196         ;; whichever is the least.
5197         (set-window-start
5198          window (min bottom (save-excursion
5199                               (forward-line (- top)) (point)))
5200          t))
5201       ;; Do horizontal recentering while we're at it.
5202       (when (and (get-buffer-window (current-buffer) t)
5203                  (not (eq gnus-auto-center-summary 'vertical)))
5204         (let ((selected (selected-window)))
5205           (select-window (get-buffer-window (current-buffer) t))
5206           (gnus-summary-position-point)
5207           (gnus-horizontal-recenter)
5208           (select-window selected))))))
5209
5210 (defun gnus-summary-jump-to-group (newsgroup)
5211   "Move point to NEWSGROUP in group mode buffer."
5212   ;; Keep update point of group mode buffer if visible.
5213   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5214       (save-window-excursion
5215         ;; Take care of tree window mode.
5216         (when (get-buffer-window gnus-group-buffer)
5217           (pop-to-buffer gnus-group-buffer))
5218         (gnus-group-jump-to-group newsgroup))
5219     (save-excursion
5220       ;; Take care of tree window mode.
5221       (if (get-buffer-window gnus-group-buffer)
5222           (pop-to-buffer gnus-group-buffer)
5223         (set-buffer gnus-group-buffer))
5224       (gnus-group-jump-to-group newsgroup))))
5225
5226 ;; This function returns a list of article numbers based on the
5227 ;; difference between the ranges of read articles in this group and
5228 ;; the range of active articles.
5229 (defun gnus-list-of-unread-articles (group)
5230   (let* ((read (gnus-info-read (gnus-get-info group)))
5231          (active (or (gnus-active group) (gnus-activate-group group)))
5232          (last (cdr active))
5233          first nlast unread)
5234     ;; If none are read, then all are unread.
5235     (if (not read)
5236         (setq first (car active))
5237       ;; If the range of read articles is a single range, then the
5238       ;; first unread article is the article after the last read
5239       ;; article.  Sounds logical, doesn't it?
5240       (if (and (not (listp (cdr read)))
5241                (or (< (car read) (car active))
5242                    (progn (setq read (list read))
5243                           nil)))
5244           (setq first (max (car active) (1+ (cdr read))))
5245         ;; `read' is a list of ranges.
5246         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5247                                   (caar read)))
5248                   1)
5249           (setq first (car active)))
5250         (while read
5251           (when first
5252             (while (< first nlast)
5253               (push first unread)
5254               (setq first (1+ first))))
5255           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5256           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5257           (setq read (cdr read)))))
5258     ;; And add the last unread articles.
5259     (while (<= first last)
5260       (push first unread)
5261       (setq first (1+ first)))
5262     ;; Return the list of unread articles.
5263     (delq 0 (nreverse unread))))
5264
5265 (defun gnus-list-of-read-articles (group)
5266   "Return a list of unread, unticked and non-dormant articles."
5267   (let* ((info (gnus-get-info group))
5268          (marked (gnus-info-marks info))
5269          (active (gnus-active group)))
5270     (and info active
5271          (gnus-set-difference
5272           (gnus-sorted-complement
5273            (gnus-uncompress-range active)
5274            (gnus-list-of-unread-articles group))
5275           (append
5276            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5277            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5278
5279 ;; Various summary commands
5280
5281 (defun gnus-summary-select-article-buffer ()
5282   "Reconfigure windows to show article buffer."
5283   (interactive)
5284   (if (not (gnus-buffer-live-p gnus-article-buffer))
5285       (error "There is no article buffer for this summary buffer")
5286     (gnus-configure-windows 'article)
5287     (select-window (get-buffer-window gnus-article-buffer))))
5288
5289 (defun gnus-summary-universal-argument (arg)
5290   "Perform any operation on all articles that are process/prefixed."
5291   (interactive "P")
5292   (let ((articles (gnus-summary-work-articles arg))
5293         func article)
5294     (if (eq
5295          (setq
5296           func
5297           (key-binding
5298            (read-key-sequence
5299             (substitute-command-keys
5300              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5301          'undefined)
5302         (gnus-error 1 "Undefined key")
5303       (save-excursion
5304         (while articles
5305           (gnus-summary-goto-subject (setq article (pop articles)))
5306           (let (gnus-newsgroup-processable)
5307             (command-execute func))
5308           (gnus-summary-remove-process-mark article)))))
5309   (gnus-summary-position-point))
5310
5311 (defun gnus-summary-toggle-truncation (&optional arg)
5312   "Toggle truncation of summary lines.
5313 With arg, turn line truncation on iff arg is positive."
5314   (interactive "P")
5315   (setq truncate-lines
5316         (if (null arg) (not truncate-lines)
5317           (> (prefix-numeric-value arg) 0)))
5318   (redraw-display))
5319
5320 (defun gnus-summary-reselect-current-group (&optional all rescan)
5321   "Exit and then reselect the current newsgroup.
5322 The prefix argument ALL means to select all articles."
5323   (interactive "P")
5324   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5325     (error "Ephemeral groups can't be reselected"))
5326   (let ((current-subject (gnus-summary-article-number))
5327         (group gnus-newsgroup-name))
5328     (setq gnus-newsgroup-begin nil)
5329     (gnus-summary-exit)
5330     ;; We have to adjust the point of group mode buffer because
5331     ;; point was moved to the next unread newsgroup by exiting.
5332     (gnus-summary-jump-to-group group)
5333     (when rescan
5334       (save-excursion
5335         (gnus-group-get-new-news-this-group 1)))
5336     (gnus-group-read-group all t)
5337     (gnus-summary-goto-subject current-subject nil t)))
5338
5339 (defun gnus-summary-rescan-group (&optional all)
5340   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5341   (interactive "P")
5342   (gnus-summary-reselect-current-group all t))
5343
5344 (defun gnus-summary-update-info (&optional non-destructive)
5345   (save-excursion
5346     (let ((group gnus-newsgroup-name))
5347       (when group
5348         (when gnus-newsgroup-kill-headers
5349           (setq gnus-newsgroup-killed
5350                 (gnus-compress-sequence
5351                  (nconc
5352                   (gnus-set-sorted-intersection
5353                    (gnus-uncompress-range gnus-newsgroup-killed)
5354                    (setq gnus-newsgroup-unselected
5355                          (sort gnus-newsgroup-unselected '<)))
5356                   (setq gnus-newsgroup-unreads
5357                         (sort gnus-newsgroup-unreads '<)))
5358                  t)))
5359         (unless (listp (cdr gnus-newsgroup-killed))
5360           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5361         (let ((headers gnus-newsgroup-headers))
5362           ;; Set the new ranges of read articles.
5363           (save-excursion
5364             (set-buffer gnus-group-buffer)
5365             (gnus-undo-force-boundary))
5366           (gnus-update-read-articles
5367            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5368           ;; Set the current article marks.
5369           (let ((gnus-newsgroup-scored
5370                  (if (and (not gnus-save-score)
5371                           (not non-destructive))
5372                      nil
5373                    gnus-newsgroup-scored)))
5374             (save-excursion
5375               (gnus-update-marks)))
5376           ;; Do the cross-ref thing.
5377           (when gnus-use-cross-reference
5378             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5379           ;; Do not switch windows but change the buffer to work.
5380           (set-buffer gnus-group-buffer)
5381           (unless (gnus-ephemeral-group-p group)
5382             (gnus-group-update-group group)))))))
5383
5384 (defun gnus-summary-save-newsrc (&optional force)
5385   "Save the current number of read/marked articles in the dribble buffer.
5386 The dribble buffer will then be saved.
5387 If FORCE (the prefix), also save the .newsrc file(s)."
5388   (interactive "P")
5389   (gnus-summary-update-info t)
5390   (if force
5391       (gnus-save-newsrc-file)
5392     (gnus-dribble-save)))
5393
5394 (defun gnus-summary-exit (&optional temporary)
5395   "Exit reading current newsgroup, and then return to group selection mode.
5396 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5397   (interactive)
5398   (gnus-set-global-variables)
5399   (when (gnus-buffer-live-p gnus-article-buffer)
5400     (save-excursion
5401       (set-buffer gnus-article-buffer)
5402       (mm-destroy-parts gnus-article-mime-handles)
5403       ;; Set it to nil for safety reason.
5404       (setq gnus-article-mime-handle-alist nil)
5405       (setq gnus-article-mime-handles nil)))
5406   (gnus-kill-save-kill-buffer)
5407   (gnus-async-halt-prefetch)
5408   (let* ((group gnus-newsgroup-name)
5409          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5410          (mode major-mode)
5411          (group-point nil)
5412          (buf (current-buffer)))
5413     (unless quit-config
5414       ;; Do adaptive scoring, and possibly save score files.
5415       (when gnus-newsgroup-adaptive
5416         (gnus-score-adaptive))
5417       (when gnus-use-scoring
5418         (gnus-score-save)))
5419     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5420     ;; If we have several article buffers, we kill them at exit.
5421     (unless gnus-single-article-buffer
5422       (gnus-kill-buffer gnus-original-article-buffer)
5423       (setq gnus-article-current nil))
5424     (when gnus-use-cache
5425       (gnus-cache-possibly-remove-articles)
5426       (gnus-cache-save-buffers))
5427     (gnus-async-prefetch-remove-group group)
5428     (when gnus-suppress-duplicates
5429       (gnus-dup-enter-articles))
5430     (when gnus-use-trees
5431       (gnus-tree-close group))
5432     (when gnus-use-cache
5433       (gnus-cache-write-active))
5434     ;; Remove entries for this group.
5435     (nnmail-purge-split-history (gnus-group-real-name group))
5436     ;; Make all changes in this group permanent.
5437     (unless quit-config
5438       (gnus-run-hooks 'gnus-exit-group-hook)
5439       (gnus-summary-update-info))
5440     (gnus-close-group group)
5441     ;; Make sure where we were, and go to next newsgroup.
5442     (set-buffer gnus-group-buffer)
5443     (unless quit-config
5444       (gnus-group-jump-to-group group))
5445     (gnus-run-hooks 'gnus-summary-exit-hook)
5446     (unless (or quit-config
5447                 ;; If this group has disappeared from the summary
5448                 ;; buffer, don't skip forwards.
5449                 (not (string= group (gnus-group-group-name))))
5450       (gnus-group-next-unread-group 1))
5451     (setq group-point (point))
5452     (if temporary
5453         nil                             ;Nothing to do.
5454       ;; If we have several article buffers, we kill them at exit.
5455       (unless gnus-single-article-buffer
5456         (gnus-kill-buffer gnus-article-buffer)
5457         (gnus-kill-buffer gnus-original-article-buffer)
5458         (setq gnus-article-current nil))
5459       (set-buffer buf)
5460       (if (not gnus-kill-summary-on-exit)
5461           (gnus-deaden-summary)
5462         ;; We set all buffer-local variables to nil.  It is unclear why
5463         ;; this is needed, but if we don't, buffer-local variables are
5464         ;; not garbage-collected, it seems.  This would the lead to en
5465         ;; ever-growing Emacs.
5466         (gnus-summary-clear-local-variables)
5467         (when (get-buffer gnus-article-buffer)
5468           (bury-buffer gnus-article-buffer))
5469         ;; We clear the global counterparts of the buffer-local
5470         ;; variables as well, just to be on the safe side.
5471         (set-buffer gnus-group-buffer)
5472         (gnus-summary-clear-local-variables)
5473         ;; Return to group mode buffer.
5474         (when (eq mode 'gnus-summary-mode)
5475           (gnus-kill-buffer buf)))
5476       (setq gnus-current-select-method gnus-select-method)
5477       (pop-to-buffer gnus-group-buffer)
5478       (if (not quit-config)
5479           (progn
5480             (goto-char group-point)
5481             (gnus-configure-windows 'group 'force))
5482         (gnus-handle-ephemeral-exit quit-config))
5483       ;; Clear the current group name.
5484       (unless quit-config
5485         (setq gnus-newsgroup-name nil)))))
5486
5487 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5488 (defun gnus-summary-exit-no-update (&optional no-questions)
5489   "Quit reading current newsgroup without updating read article info."
5490   (interactive)
5491   (let* ((group gnus-newsgroup-name)
5492          (quit-config (gnus-group-quit-config group)))
5493     (when (or no-questions
5494               gnus-expert-user
5495               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5496       (gnus-async-halt-prefetch)
5497       (mapcar 'funcall
5498               (delq 'gnus-summary-expire-articles
5499                     (copy-sequence gnus-summary-prepare-exit-hook)))
5500       (when (gnus-buffer-live-p gnus-article-buffer)
5501         (save-excursion
5502           (set-buffer gnus-article-buffer)
5503           (mm-destroy-parts gnus-article-mime-handles)
5504           ;; Set it to nil for safety reason.
5505           (setq gnus-article-mime-handle-alist nil)
5506           (setq gnus-article-mime-handles nil)))
5507       ;; If we have several article buffers, we kill them at exit.
5508       (unless gnus-single-article-buffer
5509         (gnus-kill-buffer gnus-article-buffer)
5510         (gnus-kill-buffer gnus-original-article-buffer)
5511         (setq gnus-article-current nil))
5512       (if (not gnus-kill-summary-on-exit)
5513           (gnus-deaden-summary)
5514         (gnus-close-group group)
5515         (gnus-summary-clear-local-variables)
5516         (set-buffer gnus-group-buffer)
5517         (gnus-summary-clear-local-variables)
5518         (when (get-buffer gnus-summary-buffer)
5519           (kill-buffer gnus-summary-buffer)))
5520       (unless gnus-single-article-buffer
5521         (setq gnus-article-current nil))
5522       (when gnus-use-trees
5523         (gnus-tree-close group))
5524       (gnus-async-prefetch-remove-group group)
5525       (when (get-buffer gnus-article-buffer)
5526         (bury-buffer gnus-article-buffer))
5527       ;; Return to the group buffer.
5528       (gnus-configure-windows 'group 'force)
5529       ;; Clear the current group name.
5530       (setq gnus-newsgroup-name nil)
5531       (when (equal (gnus-group-group-name) group)
5532         (gnus-group-next-unread-group 1))
5533       (when quit-config
5534         (gnus-handle-ephemeral-exit quit-config)))))
5535
5536 (defun gnus-handle-ephemeral-exit (quit-config)
5537   "Handle movement when leaving an ephemeral group.
5538 The state which existed when entering the ephemeral is reset."
5539   (if (not (buffer-name (car quit-config)))
5540       (gnus-configure-windows 'group 'force)
5541     (set-buffer (car quit-config))
5542     (cond ((eq major-mode 'gnus-summary-mode)
5543            (gnus-set-global-variables))
5544           ((eq major-mode 'gnus-article-mode)
5545            (save-excursion
5546              ;; The `gnus-summary-buffer' variable may point
5547              ;; to the old summary buffer when using a single
5548              ;; article buffer.
5549              (unless (gnus-buffer-live-p gnus-summary-buffer)
5550                (set-buffer gnus-group-buffer))
5551              (set-buffer gnus-summary-buffer)
5552              (gnus-set-global-variables))))
5553     (if (or (eq (cdr quit-config) 'article)
5554             (eq (cdr quit-config) 'pick))
5555         (progn
5556           ;; The current article may be from the ephemeral group
5557           ;; thus it is best that we reload this article
5558           (gnus-summary-show-article)
5559           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5560               (gnus-configure-windows 'pick 'force)
5561             (gnus-configure-windows (cdr quit-config) 'force)))
5562       (gnus-configure-windows (cdr quit-config) 'force))
5563     (when (eq major-mode 'gnus-summary-mode)
5564       (gnus-summary-next-subject 1 nil t)
5565       (gnus-summary-recenter)
5566       (gnus-summary-position-point))))
5567
5568 ;;; Dead summaries.
5569
5570 (defvar gnus-dead-summary-mode-map nil)
5571
5572 (unless gnus-dead-summary-mode-map
5573   (setq gnus-dead-summary-mode-map (make-keymap))
5574   (suppress-keymap gnus-dead-summary-mode-map)
5575   (substitute-key-definition
5576    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5577   (let ((keys '("\C-d" "\r" "\177" [delete])))
5578     (while keys
5579       (define-key gnus-dead-summary-mode-map
5580         (pop keys) 'gnus-summary-wake-up-the-dead))))
5581
5582 (defvar gnus-dead-summary-mode nil
5583   "Minor mode for Gnus summary buffers.")
5584
5585 (defun gnus-dead-summary-mode (&optional arg)
5586   "Minor mode for Gnus summary buffers."
5587   (interactive "P")
5588   (when (eq major-mode 'gnus-summary-mode)
5589     (make-local-variable 'gnus-dead-summary-mode)
5590     (setq gnus-dead-summary-mode
5591           (if (null arg) (not gnus-dead-summary-mode)
5592             (> (prefix-numeric-value arg) 0)))
5593     (when gnus-dead-summary-mode
5594       (gnus-add-minor-mode
5595        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5596
5597 (defun gnus-deaden-summary ()
5598   "Make the current summary buffer into a dead summary buffer."
5599   ;; Kill any previous dead summary buffer.
5600   (when (and gnus-dead-summary
5601              (buffer-name gnus-dead-summary))
5602     (save-excursion
5603       (set-buffer gnus-dead-summary)
5604       (when gnus-dead-summary-mode
5605         (kill-buffer (current-buffer)))))
5606   ;; Make this the current dead summary.
5607   (setq gnus-dead-summary (current-buffer))
5608   (gnus-dead-summary-mode 1)
5609   (let ((name (buffer-name)))
5610     (when (string-match "Summary" name)
5611       (rename-buffer
5612        (concat (substring name 0 (match-beginning 0)) "Dead "
5613                (substring name (match-beginning 0)))
5614        t)
5615       (bury-buffer))))
5616
5617 (defun gnus-kill-or-deaden-summary (buffer)
5618   "Kill or deaden the summary BUFFER."
5619   (save-excursion
5620     (when (and (buffer-name buffer)
5621                (not gnus-single-article-buffer))
5622       (save-excursion
5623         (set-buffer buffer)
5624         (gnus-kill-buffer gnus-article-buffer)
5625         (gnus-kill-buffer gnus-original-article-buffer)))
5626     (cond (gnus-kill-summary-on-exit
5627            (when (and gnus-use-trees
5628                       (gnus-buffer-exists-p buffer))
5629              (save-excursion
5630                (set-buffer buffer)
5631                (gnus-tree-close gnus-newsgroup-name)))
5632            (gnus-kill-buffer buffer))
5633           ((gnus-buffer-exists-p buffer)
5634            (save-excursion
5635              (set-buffer buffer)
5636              (gnus-deaden-summary))))))
5637
5638 (defun gnus-summary-wake-up-the-dead (&rest args)
5639   "Wake up the dead summary buffer."
5640   (interactive)
5641   (gnus-dead-summary-mode -1)
5642   (let ((name (buffer-name)))
5643     (when (string-match "Dead " name)
5644       (rename-buffer
5645        (concat (substring name 0 (match-beginning 0))
5646                (substring name (match-end 0)))
5647        t)))
5648   (gnus-message 3 "This dead summary is now alive again"))
5649
5650 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5651 (defun gnus-summary-fetch-faq (&optional faq-dir)
5652   "Fetch the FAQ for the current group.
5653 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5654 in."
5655   (interactive
5656    (list
5657     (when current-prefix-arg
5658       (completing-read
5659        "Faq dir: " (and (listp gnus-group-faq-directory)
5660                         (mapcar (lambda (file) (list file))
5661                                 gnus-group-faq-directory))))))
5662   (let (gnus-faq-buffer)
5663     (when (setq gnus-faq-buffer
5664                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5665       (gnus-configure-windows 'summary-faq))))
5666
5667 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5668 (defun gnus-summary-describe-group (&optional force)
5669   "Describe the current newsgroup."
5670   (interactive "P")
5671   (gnus-group-describe-group force gnus-newsgroup-name))
5672
5673 (defun gnus-summary-describe-briefly ()
5674   "Describe summary mode commands briefly."
5675   (interactive)
5676   (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")))
5677
5678 ;; Walking around group mode buffer from summary mode.
5679
5680 (defun gnus-summary-next-group (&optional no-article target-group backward)
5681   "Exit current newsgroup and then select next unread newsgroup.
5682 If prefix argument NO-ARTICLE is non-nil, no article is selected
5683 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5684 previous group instead."
5685   (interactive "P")
5686   ;; Stop pre-fetching.
5687   (gnus-async-halt-prefetch)
5688   (let ((current-group gnus-newsgroup-name)
5689         (current-buffer (current-buffer))
5690         entered)
5691     ;; First we semi-exit this group to update Xrefs and all variables.
5692     ;; We can't do a real exit, because the window conf must remain
5693     ;; the same in case the user is prompted for info, and we don't
5694     ;; want the window conf to change before that...
5695     (gnus-summary-exit t)
5696     (while (not entered)
5697       ;; Then we find what group we are supposed to enter.
5698       (set-buffer gnus-group-buffer)
5699       (gnus-group-jump-to-group current-group)
5700       (setq target-group
5701             (or target-group
5702                 (if (eq gnus-keep-same-level 'best)
5703                     (gnus-summary-best-group gnus-newsgroup-name)
5704                   (gnus-summary-search-group backward gnus-keep-same-level))))
5705       (if (not target-group)
5706           ;; There are no further groups, so we return to the group
5707           ;; buffer.
5708           (progn
5709             (gnus-message 5 "Returning to the group buffer")
5710             (setq entered t)
5711             (when (gnus-buffer-live-p current-buffer)
5712               (set-buffer current-buffer)
5713               (gnus-summary-exit))
5714             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5715         ;; We try to enter the target group.
5716         (gnus-group-jump-to-group target-group)
5717         (let ((unreads (gnus-group-group-unread)))
5718           (if (and (or (eq t unreads)
5719                        (and unreads (not (zerop unreads))))
5720                    (gnus-summary-read-group
5721                     target-group nil no-article
5722                     (and (buffer-name current-buffer) current-buffer)
5723                     nil backward))
5724               (setq entered t)
5725             (setq current-group target-group
5726                   target-group nil)))))))
5727
5728 (defun gnus-summary-prev-group (&optional no-article)
5729   "Exit current newsgroup and then select previous unread newsgroup.
5730 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5731   (interactive "P")
5732   (gnus-summary-next-group no-article nil t))
5733
5734 ;; Walking around summary lines.
5735
5736 (defun gnus-summary-first-subject (&optional unread undownloaded)
5737   "Go to the first unread subject.
5738 If UNREAD is non-nil, go to the first unread article.
5739 Returns the article selected or nil if there are no unread articles."
5740   (interactive "P")
5741   (prog1
5742       (cond
5743        ;; Empty summary.
5744        ((null gnus-newsgroup-data)
5745         (gnus-message 3 "No articles in the group")
5746         nil)
5747        ;; Pick the first article.
5748        ((not unread)
5749         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5750         (gnus-data-number (car gnus-newsgroup-data)))
5751        ;; No unread articles.
5752        ((null gnus-newsgroup-unreads)
5753         (gnus-message 3 "No more unread articles")
5754         nil)
5755        ;; Find the first unread article.
5756        (t
5757         (let ((data gnus-newsgroup-data))
5758           (while (and data
5759                       (and (not (and undownloaded
5760                                      (eq gnus-undownloaded-mark
5761                                          (gnus-data-mark (car data)))))
5762                            (not (gnus-data-unread-p (car data)))))
5763             (setq data (cdr data)))
5764           (when data
5765             (goto-char (gnus-data-pos (car data)))
5766             (gnus-data-number (car data))))))
5767     (gnus-summary-position-point)))
5768
5769 (defun gnus-summary-next-subject (n &optional unread dont-display)
5770   "Go to next N'th summary line.
5771 If N is negative, go to the previous N'th subject line.
5772 If UNREAD is non-nil, only unread articles are selected.
5773 The difference between N and the actual number of steps taken is
5774 returned."
5775   (interactive "p")
5776   (let ((backward (< n 0))
5777         (n (abs n)))
5778     (while (and (> n 0)
5779                 (if backward
5780                     (gnus-summary-find-prev unread)
5781                   (gnus-summary-find-next unread)))
5782       (unless (zerop (setq n (1- n)))
5783         (gnus-summary-show-thread)))
5784     (when (/= 0 n)
5785       (gnus-message 7 "No more%s articles"
5786                     (if unread " unread" "")))
5787     (unless dont-display
5788       (gnus-summary-recenter)
5789       (gnus-summary-position-point))
5790     n))
5791
5792 (defun gnus-summary-next-unread-subject (n)
5793   "Go to next N'th unread summary line."
5794   (interactive "p")
5795   (gnus-summary-next-subject n t))
5796
5797 (defun gnus-summary-prev-subject (n &optional unread)
5798   "Go to previous N'th summary line.
5799 If optional argument UNREAD is non-nil, only unread article is selected."
5800   (interactive "p")
5801   (gnus-summary-next-subject (- n) unread))
5802
5803 (defun gnus-summary-prev-unread-subject (n)
5804   "Go to previous N'th unread summary line."
5805   (interactive "p")
5806   (gnus-summary-next-subject (- n) t))
5807
5808 (defun gnus-summary-goto-subject (article &optional force silent)
5809   "Go the subject line of ARTICLE.
5810 If FORCE, also allow jumping to articles not currently shown."
5811   (interactive "nArticle number: ")
5812   (let ((b (point))
5813         (data (gnus-data-find article)))
5814     ;; We read in the article if we have to.
5815     (and (not data)
5816          force
5817          (gnus-summary-insert-subject
5818           article
5819           (if (or (numberp force) (vectorp force)) force)
5820           t)
5821          (setq data (gnus-data-find article)))
5822     (goto-char b)
5823     (if (not data)
5824         (progn
5825           (unless silent
5826             (gnus-message 3 "Can't find article %d" article))
5827           nil)
5828       (goto-char (gnus-data-pos data))
5829       (gnus-summary-position-point)
5830       article)))
5831
5832 ;; Walking around summary lines with displaying articles.
5833
5834 (defun gnus-summary-expand-window (&optional arg)
5835   "Make the summary buffer take up the entire Emacs frame.
5836 Given a prefix, will force an `article' buffer configuration."
5837   (interactive "P")
5838   (if arg
5839       (gnus-configure-windows 'article 'force)
5840     (gnus-configure-windows 'summary 'force)))
5841
5842 (defun gnus-summary-display-article (article &optional all-header)
5843   "Display ARTICLE in article buffer."
5844   (gnus-set-global-variables)
5845   (if (null article)
5846       nil
5847     (prog1
5848         (if gnus-summary-display-article-function
5849             (funcall gnus-summary-display-article-function article all-header)
5850           (gnus-article-prepare article all-header))
5851       (gnus-run-hooks 'gnus-select-article-hook)
5852       (when (and gnus-current-article
5853                  (not (zerop gnus-current-article)))
5854         (gnus-summary-goto-subject gnus-current-article))
5855       (gnus-summary-recenter)
5856       (when (and gnus-use-trees gnus-show-threads)
5857         (gnus-possibly-generate-tree article)
5858         (gnus-highlight-selected-tree article))
5859       ;; Successfully display article.
5860       (gnus-article-set-window-start
5861        (cdr (assq article gnus-newsgroup-bookmarks))))))
5862
5863 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5864   "Select the current article.
5865 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5866 non-nil, the article will be re-fetched even if it already present in
5867 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5868 be displayed."
5869   ;; Make sure we are in the summary buffer to work around bbdb bug.
5870   (unless (eq major-mode 'gnus-summary-mode)
5871     (set-buffer gnus-summary-buffer))
5872   (let ((article (or article (gnus-summary-article-number)))
5873         (all-headers (not (not all-headers))) ;Must be T or NIL.
5874         gnus-summary-display-article-function)
5875     (and (not pseudo)
5876          (gnus-summary-article-pseudo-p article)
5877          (error "This is a pseudo-article"))
5878     (save-excursion
5879       (set-buffer gnus-summary-buffer)
5880       (if (or (and gnus-single-article-buffer
5881                    (or (null gnus-current-article)
5882                        (null gnus-article-current)
5883                        (null (get-buffer gnus-article-buffer))
5884                        (not (eq article (cdr gnus-article-current)))
5885                        (not (equal (car gnus-article-current)
5886                                    gnus-newsgroup-name))))
5887               (and (not gnus-single-article-buffer)
5888                    (or (null gnus-current-article)
5889                        (not (eq gnus-current-article article))))
5890               force)
5891           ;; The requested article is different from the current article.
5892           (progn
5893             (when (gnus-buffer-live-p gnus-article-buffer)
5894               (with-current-buffer gnus-article-buffer
5895                 (mm-enable-multibyte)))
5896             (gnus-summary-display-article article all-headers)
5897             (when (gnus-buffer-live-p gnus-article-buffer)
5898               (with-current-buffer gnus-article-buffer
5899                 (if (not gnus-article-decoded-p) ;; a local variable
5900                     (mm-disable-multibyte))))
5901             (when (or all-headers gnus-show-all-headers)
5902               (gnus-article-show-all-headers))
5903             (gnus-article-set-window-start
5904              (cdr (assq article gnus-newsgroup-bookmarks)))
5905             article)
5906         (when (or all-headers gnus-show-all-headers)
5907           (gnus-article-show-all-headers))
5908         'old))))
5909
5910 (defun gnus-summary-set-current-mark (&optional current-mark)
5911   "Obsolete function."
5912   nil)
5913
5914 (defun gnus-summary-next-article (&optional unread subject backward push)
5915   "Select the next article.
5916 If UNREAD, only unread articles are selected.
5917 If SUBJECT, only articles with SUBJECT are selected.
5918 If BACKWARD, the previous article is selected instead of the next."
5919   (interactive "P")
5920   (cond
5921    ;; Is there such an article?
5922    ((and (gnus-summary-search-forward unread subject backward)
5923          (or (gnus-summary-display-article (gnus-summary-article-number))
5924              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5925     (gnus-summary-position-point))
5926    ;; If not, we try the first unread, if that is wanted.
5927    ((and subject
5928          gnus-auto-select-same
5929          (gnus-summary-first-unread-article))
5930     (gnus-summary-position-point)
5931     (gnus-message 6 "Wrapped"))
5932    ;; Try to get next/previous article not displayed in this group.
5933    ((and gnus-auto-extend-newsgroup
5934          (not unread) (not subject))
5935     (gnus-summary-goto-article
5936      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5937      nil (count-lines (point-min) (point))))
5938    ;; Go to next/previous group.
5939    (t
5940     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5941       (gnus-summary-jump-to-group gnus-newsgroup-name))
5942     (let ((cmd last-command-char)
5943           (point
5944            (save-excursion
5945              (set-buffer gnus-group-buffer)
5946              (point)))
5947           (group
5948            (if (eq gnus-keep-same-level 'best)
5949                (gnus-summary-best-group gnus-newsgroup-name)
5950              (gnus-summary-search-group backward gnus-keep-same-level))))
5951       ;; For some reason, the group window gets selected.  We change
5952       ;; it back.
5953       (select-window (get-buffer-window (current-buffer)))
5954       ;; Select next unread newsgroup automagically.
5955       (cond
5956        ((or (not gnus-auto-select-next)
5957             (not cmd))
5958         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5959        ((or (eq gnus-auto-select-next 'quietly)
5960             (and (eq gnus-auto-select-next 'slightly-quietly)
5961                  push)
5962             (and (eq gnus-auto-select-next 'almost-quietly)
5963                  (gnus-summary-last-article-p)))
5964         ;; Select quietly.
5965         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5966             (gnus-summary-exit)
5967           (gnus-message 7 "No more%s articles (%s)..."
5968                         (if unread " unread" "")
5969                         (if group (concat "selecting " group)
5970                           "exiting"))
5971           (gnus-summary-next-group nil group backward)))
5972        (t
5973         (when (gnus-key-press-event-p last-input-event)
5974           (gnus-summary-walk-group-buffer
5975            gnus-newsgroup-name cmd unread backward point))))))))
5976
5977 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5978   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5979                       (?\C-p (gnus-group-prev-unread-group 1))))
5980         (cursor-in-echo-area t)
5981         keve key group ended)
5982     (save-excursion
5983       (set-buffer gnus-group-buffer)
5984       (goto-char start)
5985       (setq group
5986             (if (eq gnus-keep-same-level 'best)
5987                 (gnus-summary-best-group gnus-newsgroup-name)
5988               (gnus-summary-search-group backward gnus-keep-same-level))))
5989     (while (not ended)
5990       (gnus-message
5991        5 "No more%s articles%s" (if unread " unread" "")
5992        (if (and group
5993                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5994            (format " (Type %s for %s [%s])"
5995                    (single-key-description cmd) group
5996                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5997          (format " (Type %s to exit %s)"
5998                  (single-key-description cmd)
5999                  gnus-newsgroup-name)))
6000       ;; Confirm auto selection.
6001       (setq key (car (setq keve (gnus-read-event-char))))
6002       (setq ended t)
6003       (cond
6004        ((assq key keystrokes)
6005         (let ((obuf (current-buffer)))
6006           (switch-to-buffer gnus-group-buffer)
6007           (when group
6008             (gnus-group-jump-to-group group))
6009           (eval (cadr (assq key keystrokes)))
6010           (setq group (gnus-group-group-name))
6011           (switch-to-buffer obuf))
6012         (setq ended nil))
6013        ((equal key cmd)
6014         (if (or (not group)
6015                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6016             (gnus-summary-exit)
6017           (gnus-summary-next-group nil group backward)))
6018        (t
6019         (push (cdr keve) unread-command-events))))))
6020
6021 (defun gnus-summary-next-unread-article ()
6022   "Select unread article after current one."
6023   (interactive)
6024   (gnus-summary-next-article
6025    (or (not (eq gnus-summary-goto-unread 'never))
6026        (gnus-summary-last-article-p (gnus-summary-article-number)))
6027    (and gnus-auto-select-same
6028         (gnus-summary-article-subject))))
6029
6030 (defun gnus-summary-prev-article (&optional unread subject)
6031   "Select the article after the current one.
6032 If UNREAD is non-nil, only unread articles are selected."
6033   (interactive "P")
6034   (gnus-summary-next-article unread subject t))
6035
6036 (defun gnus-summary-prev-unread-article ()
6037   "Select unread article before current one."
6038   (interactive)
6039   (gnus-summary-prev-article
6040    (or (not (eq gnus-summary-goto-unread 'never))
6041        (gnus-summary-first-article-p (gnus-summary-article-number)))
6042    (and gnus-auto-select-same
6043         (gnus-summary-article-subject))))
6044
6045 (defun gnus-summary-next-page (&optional lines circular)
6046   "Show next page of the selected article.
6047 If at the end of the current article, select the next article.
6048 LINES says how many lines should be scrolled up.
6049
6050 If CIRCULAR is non-nil, go to the start of the article instead of
6051 selecting the next article when reaching the end of the current
6052 article."
6053   (interactive "P")
6054   (setq gnus-summary-buffer (current-buffer))
6055   (gnus-set-global-variables)
6056   (let ((article (gnus-summary-article-number))
6057         (article-window (get-buffer-window gnus-article-buffer t))
6058         endp)
6059     ;; If the buffer is empty, we have no article.
6060     (unless article
6061       (error "No article to select"))
6062     (gnus-configure-windows 'article)
6063     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6064         (if (and (eq gnus-summary-goto-unread 'never)
6065                  (not (gnus-summary-last-article-p article)))
6066             (gnus-summary-next-article)
6067           (gnus-summary-next-unread-article))
6068       (if (or (null gnus-current-article)
6069               (null gnus-article-current)
6070               (/= article (cdr gnus-article-current))
6071               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6072           ;; Selected subject is different from current article's.
6073           (gnus-summary-display-article article)
6074         (when article-window
6075           (gnus-eval-in-buffer-window gnus-article-buffer
6076             (setq endp (gnus-article-next-page lines)))
6077           (when endp
6078             (cond (circular
6079                    (gnus-summary-beginning-of-article))
6080                   (lines
6081                    (gnus-message 3 "End of message"))
6082                   ((null lines)
6083                    (if (and (eq gnus-summary-goto-unread 'never)
6084                             (not (gnus-summary-last-article-p article)))
6085                        (gnus-summary-next-article)
6086                      (gnus-summary-next-unread-article))))))))
6087     (gnus-summary-recenter)
6088     (gnus-summary-position-point)))
6089
6090 (defun gnus-summary-prev-page (&optional lines move)
6091   "Show previous page of selected article.
6092 Argument LINES specifies lines to be scrolled down.
6093 If MOVE, move to the previous unread article if point is at
6094 the beginning of the buffer."
6095   (interactive "P")
6096   (let ((article (gnus-summary-article-number))
6097         (article-window (get-buffer-window gnus-article-buffer t))
6098         endp)
6099     (gnus-configure-windows 'article)
6100     (if (or (null gnus-current-article)
6101             (null gnus-article-current)
6102             (/= article (cdr gnus-article-current))
6103             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6104         ;; Selected subject is different from current article's.
6105         (gnus-summary-display-article article)
6106       (gnus-summary-recenter)
6107       (when article-window
6108         (gnus-eval-in-buffer-window gnus-article-buffer
6109           (setq endp (gnus-article-prev-page lines)))
6110         (when (and move endp)
6111           (cond (lines
6112                  (gnus-message 3 "Beginning of message"))
6113                 ((null lines)
6114                  (if (and (eq gnus-summary-goto-unread 'never)
6115                           (not (gnus-summary-first-article-p article)))
6116                      (gnus-summary-prev-article)
6117                    (gnus-summary-prev-unread-article))))))))
6118   (gnus-summary-position-point))
6119
6120 (defun gnus-summary-prev-page-or-article (&optional lines)
6121   "Show previous page of selected article.
6122 Argument LINES specifies lines to be scrolled down.
6123 If at the beginning of the article, go to the next article."
6124   (interactive "P")
6125   (gnus-summary-prev-page lines t))
6126
6127 (defun gnus-summary-scroll-up (lines)
6128   "Scroll up (or down) one line current article.
6129 Argument LINES specifies lines to be scrolled up (or down if negative)."
6130   (interactive "p")
6131   (gnus-configure-windows 'article)
6132   (gnus-summary-show-thread)
6133   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6134     (gnus-eval-in-buffer-window gnus-article-buffer
6135       (cond ((> lines 0)
6136              (when (gnus-article-next-page lines)
6137                (gnus-message 3 "End of message")))
6138             ((< lines 0)
6139              (gnus-article-prev-page (- lines))))))
6140   (gnus-summary-recenter)
6141   (gnus-summary-position-point))
6142
6143 (defun gnus-summary-scroll-down (lines)
6144   "Scroll down (or up) one line current article.
6145 Argument LINES specifies lines to be scrolled down (or up if negative)."
6146   (interactive "p")
6147   (gnus-summary-scroll-up (- lines)))
6148
6149 (defun gnus-summary-next-same-subject ()
6150   "Select next article which has the same subject as current one."
6151   (interactive)
6152   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6153
6154 (defun gnus-summary-prev-same-subject ()
6155   "Select previous article which has the same subject as current one."
6156   (interactive)
6157   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6158
6159 (defun gnus-summary-next-unread-same-subject ()
6160   "Select next unread article which has the same subject as current one."
6161   (interactive)
6162   (gnus-summary-next-article t (gnus-summary-article-subject)))
6163
6164 (defun gnus-summary-prev-unread-same-subject ()
6165   "Select previous unread article which has the same subject as current one."
6166   (interactive)
6167   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6168
6169 (defun gnus-summary-first-unread-article ()
6170   "Select the first unread article.
6171 Return nil if there are no unread articles."
6172   (interactive)
6173   (prog1
6174       (when (gnus-summary-first-subject t)
6175         (gnus-summary-show-thread)
6176         (gnus-summary-first-subject t)
6177         (gnus-summary-display-article (gnus-summary-article-number)))
6178     (gnus-summary-position-point)))
6179
6180 (defun gnus-summary-first-unread-subject ()
6181   "Place the point on the subject line of the first unread article.
6182 Return nil if there are no unread articles."
6183   (interactive)
6184   (prog1
6185       (when (gnus-summary-first-subject t)
6186         (gnus-summary-show-thread)
6187         (gnus-summary-first-subject t))
6188     (gnus-summary-position-point)))
6189
6190 (defun gnus-summary-first-article ()
6191   "Select the first article.
6192 Return nil if there are no articles."
6193   (interactive)
6194   (prog1
6195       (when (gnus-summary-first-subject)
6196         (gnus-summary-show-thread)
6197         (gnus-summary-first-subject)
6198         (gnus-summary-display-article (gnus-summary-article-number)))
6199     (gnus-summary-position-point)))
6200
6201 (defun gnus-summary-best-unread-article ()
6202   "Select the unread article with the highest score."
6203   (interactive)
6204   (let ((best -1000000)
6205         (data gnus-newsgroup-data)
6206         article score)
6207     (while data
6208       (and (gnus-data-unread-p (car data))
6209            (> (setq score
6210                     (gnus-summary-article-score (gnus-data-number (car data))))
6211               best)
6212            (setq best score
6213                  article (gnus-data-number (car data))))
6214       (setq data (cdr data)))
6215     (prog1
6216         (if article
6217             (gnus-summary-goto-article article)
6218           (error "No unread articles"))
6219       (gnus-summary-position-point))))
6220
6221 (defun gnus-summary-last-subject ()
6222   "Go to the last displayed subject line in the group."
6223   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6224     (when article
6225       (gnus-summary-goto-subject article))))
6226
6227 (defun gnus-summary-goto-article (article &optional all-headers force)
6228   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6229 If ALL-HEADERS is non-nil, no header lines are hidden.
6230 If FORCE, go to the article even if it isn't displayed.  If FORCE
6231 is a number, it is the line the article is to be displayed on."
6232   (interactive
6233    (list
6234     (completing-read
6235      "Article number or Message-ID: "
6236      (mapcar (lambda (number) (list (int-to-string number)))
6237              gnus-newsgroup-limit))
6238     current-prefix-arg
6239     t))
6240   (prog1
6241       (if (and (stringp article)
6242                (string-match "@" article))
6243           (gnus-summary-refer-article article)
6244         (when (stringp article)
6245           (setq article (string-to-number article)))
6246         (if (gnus-summary-goto-subject article force)
6247             (gnus-summary-display-article article all-headers)
6248           (gnus-message 4 "Couldn't go to article %s" article) nil))
6249     (gnus-summary-position-point)))
6250
6251 (defun gnus-summary-goto-last-article ()
6252   "Go to the previously read article."
6253   (interactive)
6254   (prog1
6255       (when gnus-last-article
6256         (gnus-summary-goto-article gnus-last-article nil t))
6257     (gnus-summary-position-point)))
6258
6259 (defun gnus-summary-pop-article (number)
6260   "Pop one article off the history and go to the previous.
6261 NUMBER articles will be popped off."
6262   (interactive "p")
6263   (let (to)
6264     (setq gnus-newsgroup-history
6265           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6266     (if to
6267         (gnus-summary-goto-article (car to) nil t)
6268       (error "Article history empty")))
6269   (gnus-summary-position-point))
6270
6271 ;; Summary commands and functions for limiting the summary buffer.
6272
6273 (defun gnus-summary-limit-to-articles (n)
6274   "Limit the summary buffer to the next N articles.
6275 If not given a prefix, use the process marked articles instead."
6276   (interactive "P")
6277   (prog1
6278       (let ((articles (gnus-summary-work-articles n)))
6279         (setq gnus-newsgroup-processable nil)
6280         (gnus-summary-limit articles))
6281     (gnus-summary-position-point)))
6282
6283 (defun gnus-summary-pop-limit (&optional total)
6284   "Restore the previous limit.
6285 If given a prefix, remove all limits."
6286   (interactive "P")
6287   (when total
6288     (setq gnus-newsgroup-limits
6289           (list (mapcar (lambda (h) (mail-header-number h))
6290                         gnus-newsgroup-headers))))
6291   (unless gnus-newsgroup-limits
6292     (error "No limit to pop"))
6293   (prog1
6294       (gnus-summary-limit nil 'pop)
6295     (gnus-summary-position-point)))
6296
6297 (defun gnus-summary-limit-to-subject (subject &optional header)
6298   "Limit the summary buffer to articles that have subjects that match a regexp."
6299   (interactive "sLimit to subject (regexp): ")
6300   (unless header
6301     (setq header "subject"))
6302   (when (not (equal "" subject))
6303     (prog1
6304         (let ((articles (gnus-summary-find-matching
6305                          (or header "subject") subject 'all)))
6306           (unless articles
6307             (error "Found no matches for \"%s\"" subject))
6308           (gnus-summary-limit articles))
6309       (gnus-summary-position-point))))
6310
6311 (defun gnus-summary-limit-to-author (from)
6312   "Limit the summary buffer to articles that have authors that match a regexp."
6313   (interactive "sLimit to author (regexp): ")
6314   (gnus-summary-limit-to-subject from "from"))
6315
6316 (defun gnus-summary-limit-to-age (age &optional younger-p)
6317   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6318 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6319 articles that are younger than AGE days."
6320   (interactive
6321    (let ((younger current-prefix-arg)
6322          (days-got nil)
6323          days)
6324      (while (not days-got)
6325        (setq days (if younger
6326                       (read-string "Limit to articles within (in days): ")
6327                     (read-string "Limit to articles old than (in days): ")))
6328        (when (> (length days) 0)
6329          (setq days (read days)))
6330        (if (numberp days)
6331            (setq days-got t)
6332          (message "Please enter a number.")
6333          (sleep-for 1)))
6334      (list days younger)))
6335   (prog1
6336       (let ((data gnus-newsgroup-data)
6337             (cutoff (days-to-time age))
6338             articles d date is-younger)
6339         (while (setq d (pop data))
6340           (when (and (vectorp (gnus-data-header d))
6341                      (setq date (mail-header-date (gnus-data-header d))))
6342             (setq is-younger (time-less-p
6343                               (time-since (condition-case ()
6344                                               (date-to-time date)
6345                                             (error '(0 0))))
6346                               cutoff))
6347             (when (if younger-p
6348                       is-younger
6349                     (not is-younger))
6350               (push (gnus-data-number d) articles))))
6351         (gnus-summary-limit (nreverse articles)))
6352     (gnus-summary-position-point)))
6353
6354 (defun gnus-summary-limit-to-extra (header regexp)
6355   "Limit the summary buffer to articles that match an 'extra' header."
6356   (interactive
6357    (let ((header
6358           (intern
6359            (gnus-completing-read
6360             (symbol-name (car gnus-extra-headers))
6361             "Limit extra header:"
6362             (mapcar (lambda (x)
6363                       (cons (symbol-name x) x))
6364                     gnus-extra-headers)
6365             nil
6366             t))))
6367      (list header
6368            (read-string (format "Limit to header %s (regexp): " header)))))
6369   (when (not (equal "" regexp))
6370     (prog1
6371         (let ((articles (gnus-summary-find-matching
6372                          (cons 'extra header) regexp 'all)))
6373           (unless articles
6374             (error "Found no matches for \"%s\"" regexp))
6375           (gnus-summary-limit articles))
6376       (gnus-summary-position-point))))
6377
6378 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6379 (make-obsolete
6380  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6381
6382 (defun gnus-summary-limit-to-unread (&optional all)
6383   "Limit the summary buffer to articles that are not marked as read.
6384 If ALL is non-nil, limit strictly to unread articles."
6385   (interactive "P")
6386   (if all
6387       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6388     (gnus-summary-limit-to-marks
6389      ;; Concat all the marks that say that an article is read and have
6390      ;; those removed.
6391      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6392            gnus-killed-mark gnus-kill-file-mark
6393            gnus-low-score-mark gnus-expirable-mark
6394            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6395            gnus-duplicate-mark gnus-souped-mark)
6396      'reverse)))
6397
6398 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6399 (make-obsolete 'gnus-summary-delete-marked-with
6400                'gnus-summary-limit-exlude-marks)
6401
6402 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6403   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6404 If REVERSE, limit the summary buffer to articles that are marked
6405 with MARKS.  MARKS can either be a string of marks or a list of marks.
6406 Returns how many articles were removed."
6407   (interactive "sMarks: ")
6408   (gnus-summary-limit-to-marks marks t))
6409
6410 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6411   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6412 If REVERSE (the prefix), limit the summary buffer to articles that are
6413 not marked with MARKS.  MARKS can either be a string of marks or a
6414 list of marks.
6415 Returns how many articles were removed."
6416   (interactive "sMarks: \nP")
6417   (prog1
6418       (let ((data gnus-newsgroup-data)
6419             (marks (if (listp marks) marks
6420                      (append marks nil))) ; Transform to list.
6421             articles)
6422         (while data
6423           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6424                   (memq (gnus-data-mark (car data)) marks))
6425             (push (gnus-data-number (car data)) articles))
6426           (setq data (cdr data)))
6427         (gnus-summary-limit articles))
6428     (gnus-summary-position-point)))
6429
6430 (defun gnus-summary-limit-to-score (&optional score)
6431   "Limit to articles with score at or above SCORE."
6432   (interactive "P")
6433   (setq score (if score
6434                   (prefix-numeric-value score)
6435                 (or gnus-summary-default-score 0)))
6436   (let ((data gnus-newsgroup-data)
6437         articles)
6438     (while data
6439       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6440                 score)
6441         (push (gnus-data-number (car data)) articles))
6442       (setq data (cdr data)))
6443     (prog1
6444         (gnus-summary-limit articles)
6445       (gnus-summary-position-point))))
6446
6447 (defun gnus-summary-limit-include-thread (id)
6448   "Display all the hidden articles that in the current thread."
6449   (interactive (list (mail-header-id (gnus-summary-article-header))))
6450   (let ((articles (gnus-articles-in-thread
6451                    (gnus-id-to-thread (gnus-root-id id)))))
6452     (prog1
6453         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6454       (gnus-summary-position-point))))
6455
6456 (defun gnus-summary-limit-include-dormant ()
6457   "Display all the hidden articles that are marked as dormant.
6458 Note that this command only works on a subset of the articles currently
6459 fetched for this group."
6460   (interactive)
6461   (unless gnus-newsgroup-dormant
6462     (error "There are no dormant articles in this group"))
6463   (prog1
6464       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6465     (gnus-summary-position-point)))
6466
6467 (defun gnus-summary-limit-exclude-dormant ()
6468   "Hide all dormant articles."
6469   (interactive)
6470   (prog1
6471       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6472     (gnus-summary-position-point)))
6473
6474 (defun gnus-summary-limit-exclude-childless-dormant ()
6475   "Hide all dormant articles that have no children."
6476   (interactive)
6477   (let ((data (gnus-data-list t))
6478         articles d children)
6479     ;; Find all articles that are either not dormant or have
6480     ;; children.
6481     (while (setq d (pop data))
6482       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6483                 (and (setq children
6484                            (gnus-article-children (gnus-data-number d)))
6485                      (let (found)
6486                        (while children
6487                          (when (memq (car children) articles)
6488                            (setq children nil
6489                                  found t))
6490                          (pop children))
6491                        found)))
6492         (push (gnus-data-number d) articles)))
6493     ;; Do the limiting.
6494     (prog1
6495         (gnus-summary-limit articles)
6496       (gnus-summary-position-point))))
6497
6498 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6499   "Mark all unread excluded articles as read.
6500 If ALL, mark even excluded ticked and dormants as read."
6501   (interactive "P")
6502   (let ((articles (gnus-sorted-complement
6503                    (sort
6504                     (mapcar (lambda (h) (mail-header-number h))
6505                             gnus-newsgroup-headers)
6506                     '<)
6507                    (sort gnus-newsgroup-limit '<)))
6508         article)
6509     (setq gnus-newsgroup-unreads
6510           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6511     (if all
6512         (setq gnus-newsgroup-dormant nil
6513               gnus-newsgroup-marked nil
6514               gnus-newsgroup-reads
6515               (nconc
6516                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6517                gnus-newsgroup-reads))
6518       (while (setq article (pop articles))
6519         (unless (or (memq article gnus-newsgroup-dormant)
6520                     (memq article gnus-newsgroup-marked))
6521           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6522
6523 (defun gnus-summary-limit (articles &optional pop)
6524   (if pop
6525       ;; We pop the previous limit off the stack and use that.
6526       (setq articles (car gnus-newsgroup-limits)
6527             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6528     ;; We use the new limit, so we push the old limit on the stack.
6529     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6530   ;; Set the limit.
6531   (setq gnus-newsgroup-limit articles)
6532   (let ((total (length gnus-newsgroup-data))
6533         (data (gnus-data-find-list (gnus-summary-article-number)))
6534         (gnus-summary-mark-below nil)   ; Inhibit this.
6535         found)
6536     ;; This will do all the work of generating the new summary buffer
6537     ;; according to the new limit.
6538     (gnus-summary-prepare)
6539     ;; Hide any threads, possibly.
6540     (and gnus-show-threads
6541          gnus-thread-hide-subtree
6542          (gnus-summary-hide-all-threads))
6543     ;; Try to return to the article you were at, or one in the
6544     ;; neighborhood.
6545     (when data
6546       ;; We try to find some article after the current one.
6547       (while data
6548         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6549           (setq data nil
6550                 found t))
6551         (setq data (cdr data))))
6552     (unless found
6553       ;; If there is no data, that means that we were after the last
6554       ;; article.  The same goes when we can't find any articles
6555       ;; after the current one.
6556       (goto-char (point-max))
6557       (gnus-summary-find-prev))
6558     (gnus-set-mode-line 'summary)
6559     ;; We return how many articles were removed from the summary
6560     ;; buffer as a result of the new limit.
6561     (- total (length gnus-newsgroup-data))))
6562
6563 (defsubst gnus-invisible-cut-children (threads)
6564   (let ((num 0))
6565     (while threads
6566       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6567         (incf num))
6568       (pop threads))
6569     (< num 2)))
6570
6571 (defsubst gnus-cut-thread (thread)
6572   "Go forwards in the thread until we find an article that we want to display."
6573   (when (or (eq gnus-fetch-old-headers 'some)
6574             (eq gnus-fetch-old-headers 'invisible)
6575             (numberp gnus-fetch-old-headers)
6576             (eq gnus-build-sparse-threads 'some)
6577             (eq gnus-build-sparse-threads 'more))
6578     ;; Deal with old-fetched headers and sparse threads.
6579     (while (and
6580             thread
6581             (or
6582              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6583              (gnus-summary-article-ancient-p
6584               (mail-header-number (car thread))))
6585             (if (or (<= (length (cdr thread)) 1)
6586                     (eq gnus-fetch-old-headers 'invisible))
6587                 (setq gnus-newsgroup-limit
6588                       (delq (mail-header-number (car thread))
6589                             gnus-newsgroup-limit)
6590                       thread (cadr thread))
6591               (when (gnus-invisible-cut-children (cdr thread))
6592                 (let ((th (cdr thread)))
6593                   (while th
6594                     (if (memq (mail-header-number (caar th))
6595                               gnus-newsgroup-limit)
6596                         (setq thread (car th)
6597                               th nil)
6598                       (setq th (cdr th))))))))))
6599   thread)
6600
6601 (defun gnus-cut-threads (threads)
6602   "Cut off all uninteresting articles from the beginning of threads."
6603   (when (or (eq gnus-fetch-old-headers 'some)
6604             (eq gnus-fetch-old-headers 'invisible)
6605             (numberp gnus-fetch-old-headers)
6606             (eq gnus-build-sparse-threads 'some)
6607             (eq gnus-build-sparse-threads 'more))
6608     (let ((th threads))
6609       (while th
6610         (setcar th (gnus-cut-thread (car th)))
6611         (setq th (cdr th)))))
6612   ;; Remove nixed out threads.
6613   (delq nil threads))
6614
6615 (defun gnus-summary-initial-limit (&optional show-if-empty)
6616   "Figure out what the initial limit is supposed to be on group entry.
6617 This entails weeding out unwanted dormants, low-scored articles,
6618 fetch-old-headers verbiage, and so on."
6619   ;; Most groups have nothing to remove.
6620   (if (or gnus-inhibit-limiting
6621           (and (null gnus-newsgroup-dormant)
6622                (not (eq gnus-fetch-old-headers 'some))
6623                (not (numberp gnus-fetch-old-headers))
6624                (not (eq gnus-fetch-old-headers 'invisible))
6625                (null gnus-summary-expunge-below)
6626                (not (eq gnus-build-sparse-threads 'some))
6627                (not (eq gnus-build-sparse-threads 'more))
6628                (null gnus-thread-expunge-below)
6629                (not gnus-use-nocem)))
6630       ()                                ; Do nothing.
6631     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6632     (setq gnus-newsgroup-limit nil)
6633     (mapatoms
6634      (lambda (node)
6635        (unless (car (symbol-value node))
6636          ;; These threads have no parents -- they are roots.
6637          (let ((nodes (cdr (symbol-value node)))
6638                thread)
6639            (while nodes
6640              (if (and gnus-thread-expunge-below
6641                       (< (gnus-thread-total-score (car nodes))
6642                          gnus-thread-expunge-below))
6643                  (gnus-expunge-thread (pop nodes))
6644                (setq thread (pop nodes))
6645                (gnus-summary-limit-children thread))))))
6646      gnus-newsgroup-dependencies)
6647     ;; If this limitation resulted in an empty group, we might
6648     ;; pop the previous limit and use it instead.
6649     (when (and (not gnus-newsgroup-limit)
6650                show-if-empty)
6651       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6652     gnus-newsgroup-limit))
6653
6654 (defun gnus-summary-limit-children (thread)
6655   "Return 1 if this subthread is visible and 0 if it is not."
6656   ;; First we get the number of visible children to this thread.  This
6657   ;; is done by recursing down the thread using this function, so this
6658   ;; will really go down to a leaf article first, before slowly
6659   ;; working its way up towards the root.
6660   (when thread
6661     (let ((children
6662            (if (cdr thread)
6663                (apply '+ (mapcar 'gnus-summary-limit-children
6664                                  (cdr thread)))
6665              0))
6666           (number (mail-header-number (car thread)))
6667           score)
6668       (if (and
6669            (not (memq number gnus-newsgroup-marked))
6670            (or
6671             ;; If this article is dormant and has absolutely no visible
6672             ;; children, then this article isn't visible.
6673             (and (memq number gnus-newsgroup-dormant)
6674                  (zerop children))
6675             ;; If this is "fetch-old-headered" and there is no
6676             ;; visible children, then we don't want this article.
6677             (and (or (eq gnus-fetch-old-headers 'some)
6678                      (numberp gnus-fetch-old-headers))
6679                  (gnus-summary-article-ancient-p number)
6680                  (zerop children))
6681             ;; If this is "fetch-old-headered" and `invisible', then
6682             ;; we don't want this article.
6683             (and (eq gnus-fetch-old-headers 'invisible)
6684                  (gnus-summary-article-ancient-p number))
6685             ;; If this is a sparsely inserted article with no children,
6686             ;; we don't want it.
6687             (and (eq gnus-build-sparse-threads 'some)
6688                  (gnus-summary-article-sparse-p number)
6689                  (zerop children))
6690             ;; If we use expunging, and this article is really
6691             ;; low-scored, then we don't want this article.
6692             (when (and gnus-summary-expunge-below
6693                        (< (setq score
6694                                 (or (cdr (assq number gnus-newsgroup-scored))
6695                                     gnus-summary-default-score))
6696                           gnus-summary-expunge-below))
6697               ;; We increase the expunge-tally here, but that has
6698               ;; nothing to do with the limits, really.
6699               (incf gnus-newsgroup-expunged-tally)
6700               ;; We also mark as read here, if that's wanted.
6701               (when (and gnus-summary-mark-below
6702                          (< score gnus-summary-mark-below))
6703                 (setq gnus-newsgroup-unreads
6704                       (delq number gnus-newsgroup-unreads))
6705                 (if gnus-newsgroup-auto-expire
6706                     (push number gnus-newsgroup-expirable)
6707                   (push (cons number gnus-low-score-mark)
6708                         gnus-newsgroup-reads)))
6709               t)
6710             ;; Check NoCeM things.
6711             (if (and gnus-use-nocem
6712                      (gnus-nocem-unwanted-article-p
6713                       (mail-header-id (car thread))))
6714                 (progn
6715                   (setq gnus-newsgroup-unreads
6716                         (delq number gnus-newsgroup-unreads))
6717                   t))))
6718           ;; Nope, invisible article.
6719           0
6720         ;; Ok, this article is to be visible, so we add it to the limit
6721         ;; and return 1.
6722         (push number gnus-newsgroup-limit)
6723         1))))
6724
6725 (defun gnus-expunge-thread (thread)
6726   "Mark all articles in THREAD as read."
6727   (let* ((number (mail-header-number (car thread))))
6728     (incf gnus-newsgroup-expunged-tally)
6729     ;; We also mark as read here, if that's wanted.
6730     (setq gnus-newsgroup-unreads
6731           (delq number gnus-newsgroup-unreads))
6732     (if gnus-newsgroup-auto-expire
6733         (push number gnus-newsgroup-expirable)
6734       (push (cons number gnus-low-score-mark)
6735             gnus-newsgroup-reads)))
6736   ;; Go recursively through all subthreads.
6737   (mapcar 'gnus-expunge-thread (cdr thread)))
6738
6739 ;; Summary article oriented commands
6740
6741 (defun gnus-summary-refer-parent-article (n)
6742   "Refer parent article N times.
6743 If N is negative, go to ancestor -N instead.
6744 The difference between N and the number of articles fetched is returned."
6745   (interactive "p")
6746   (let ((skip 1)
6747         error header ref)
6748     (when (not (natnump n))
6749       (setq skip (abs n)
6750             n 1))
6751     (while (and (> n 0)
6752                 (not error))
6753       (setq header (gnus-summary-article-header))
6754       (if (and (eq (mail-header-number header)
6755                    (cdr gnus-article-current))
6756                (equal gnus-newsgroup-name
6757                       (car gnus-article-current)))
6758           ;; If we try to find the parent of the currently
6759           ;; displayed article, then we take a look at the actual
6760           ;; References header, since this is slightly more
6761           ;; reliable than the References field we got from the
6762           ;; server.
6763           (save-excursion
6764             (set-buffer gnus-original-article-buffer)
6765             (nnheader-narrow-to-headers)
6766             (unless (setq ref (message-fetch-field "references"))
6767               (setq ref (message-fetch-field "in-reply-to")))
6768             (widen))
6769         (setq ref
6770               ;; It's not the current article, so we take a bet on
6771               ;; the value we got from the server.
6772               (mail-header-references header)))
6773       (if (and ref
6774                (not (equal ref "")))
6775           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6776             (gnus-message 1 "Couldn't find parent"))
6777         (gnus-message 1 "No references in article %d"
6778                       (gnus-summary-article-number))
6779         (setq error t))
6780       (decf n))
6781     (gnus-summary-position-point)
6782     n))
6783
6784 (defun gnus-summary-refer-references ()
6785   "Fetch all articles mentioned in the References header.
6786 Return the number of articles fetched."
6787   (interactive)
6788   (let ((ref (mail-header-references (gnus-summary-article-header)))
6789         (current (gnus-summary-article-number))
6790         (n 0))
6791     (if (or (not ref)
6792             (equal ref ""))
6793         (error "No References in the current article")
6794       ;; For each Message-ID in the References header...
6795       (while (string-match "<[^>]*>" ref)
6796         (incf n)
6797         ;; ... fetch that article.
6798         (gnus-summary-refer-article
6799          (prog1 (match-string 0 ref)
6800            (setq ref (substring ref (match-end 0))))))
6801       (gnus-summary-goto-subject current)
6802       (gnus-summary-position-point)
6803       n)))
6804
6805 (defun gnus-summary-refer-thread (&optional limit)
6806   "Fetch all articles in the current thread.
6807 If LIMIT (the numerical prefix), fetch that many old headers instead
6808 of what's specified by the `gnus-refer-thread-limit' variable."
6809   (interactive "P")
6810   (let ((id (mail-header-id (gnus-summary-article-header)))
6811         (limit (if limit (prefix-numeric-value limit)
6812                  gnus-refer-thread-limit)))
6813     ;; We want to fetch LIMIT *old* headers, but we also have to
6814     ;; re-fetch all the headers in the current buffer, because many of
6815     ;; them may be undisplayed.  So we adjust LIMIT.
6816     (when (numberp limit)
6817       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6818     (unless (eq gnus-fetch-old-headers 'invisible)
6819       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6820       ;; Retrieve the headers and read them in.
6821       (if (eq (gnus-retrieve-headers
6822                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6823               'nov)
6824           (gnus-build-all-threads)
6825         (error "Can't fetch thread from backends that don't support NOV"))
6826       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6827     (gnus-summary-limit-include-thread id)))
6828
6829 (defun gnus-summary-refer-article (message-id)
6830   "Fetch an article specified by MESSAGE-ID."
6831   (interactive "sMessage-ID: ")
6832   (when (and (stringp message-id)
6833              (not (zerop (length message-id))))
6834     ;; Construct the correct Message-ID if necessary.
6835     ;; Suggested by tale@pawl.rpi.edu.
6836     (unless (string-match "^<" message-id)
6837       (setq message-id (concat "<" message-id)))
6838     (unless (string-match ">$" message-id)
6839       (setq message-id (concat message-id ">")))
6840     (let* ((header (gnus-id-to-header message-id))
6841            (sparse (and header
6842                         (gnus-summary-article-sparse-p
6843                          (mail-header-number header))
6844                         (memq (mail-header-number header)
6845                               gnus-newsgroup-limit)))
6846            number)
6847       (cond
6848        ;; If the article is present in the buffer we just go to it.
6849        ((and header
6850              (or (not (gnus-summary-article-sparse-p
6851                        (mail-header-number header)))
6852                  sparse))
6853         (prog1
6854             (gnus-summary-goto-article
6855              (mail-header-number header) nil t)
6856           (when sparse
6857             (gnus-summary-update-article (mail-header-number header)))))
6858        (t
6859         ;; We fetch the article.
6860         (catch 'found
6861           (dolist (gnus-override-method (gnus-refer-article-methods))
6862             (gnus-check-server gnus-override-method)
6863             ;; Fetch the header, and display the article.
6864             (when (setq number (gnus-summary-insert-subject message-id))
6865               (gnus-summary-select-article nil nil nil number)
6866               (throw 'found t)))
6867           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6868
6869 (defun gnus-refer-article-methods ()
6870   "Return a list of referrable methods."
6871   (cond
6872    ;; No method, so we default to current and native.
6873    ((null gnus-refer-article-method)
6874     (list gnus-current-select-method gnus-select-method))
6875    ;; Current.
6876    ((eq 'current gnus-refer-article-method)
6877     (list gnus-current-select-method))
6878    ;; List of select methods.
6879    ((not (stringp (cadr gnus-refer-article-method)))
6880     (let (out)
6881       (dolist (method gnus-refer-article-method)
6882         (push (if (eq 'current method)
6883                   gnus-current-select-method
6884                 method)
6885               out))
6886       (nreverse out)))
6887    ;; One single select method.
6888    (t
6889     (list gnus-refer-article-method))))
6890
6891 (defun gnus-summary-edit-parameters ()
6892   "Edit the group parameters of the current group."
6893   (interactive)
6894   (gnus-group-edit-group gnus-newsgroup-name 'params))
6895
6896 (defun gnus-summary-customize-parameters ()
6897   "Customize the group parameters of the current group."
6898   (interactive)
6899   (gnus-group-customize gnus-newsgroup-name))
6900
6901 (defun gnus-summary-enter-digest-group (&optional force)
6902   "Enter an nndoc group based on the current article.
6903 If FORCE, force a digest interpretation.  If not, try
6904 to guess what the document format is."
6905   (interactive "P")
6906   (let ((conf gnus-current-window-configuration))
6907     (save-excursion
6908       (gnus-summary-select-article))
6909     (setq gnus-current-window-configuration conf)
6910     (let* ((name (format "%s-%d"
6911                          (gnus-group-prefixed-name
6912                           gnus-newsgroup-name (list 'nndoc ""))
6913                          (save-excursion
6914                            (set-buffer gnus-summary-buffer)
6915                            gnus-current-article)))
6916            (ogroup gnus-newsgroup-name)
6917            (params (append (gnus-info-params (gnus-get-info ogroup))
6918                            (list (cons 'to-group ogroup))
6919                            (list (cons 'save-article-group ogroup))))
6920            (case-fold-search t)
6921            (buf (current-buffer))
6922            dig to-address)
6923       (save-excursion
6924         (set-buffer gnus-original-article-buffer)
6925         ;; Have the digest group inherit the main mail address of
6926         ;; the parent article.
6927         (when (setq to-address (or (message-fetch-field "reply-to")
6928                                    (message-fetch-field "from")))
6929           (setq params (append (list (cons 'to-address to-address)))))
6930         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6931         (insert-buffer-substring gnus-original-article-buffer)
6932         ;; Remove lines that may lead nndoc to misinterpret the
6933         ;; document type.
6934         (narrow-to-region
6935          (goto-char (point-min))
6936          (or (search-forward "\n\n" nil t) (point)))
6937         (goto-char (point-min))
6938         (delete-matching-lines "^Path:\\|^From ")
6939         (widen))
6940       (unwind-protect
6941           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6942                     (gnus-newsgroup-ephemeral-ignored-charsets
6943                      gnus-newsgroup-ignored-charsets))
6944                 (gnus-group-read-ephemeral-group
6945                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6946                               (nndoc-article-type
6947                                ,(if force 'mbox 'guess))) t))
6948               ;; Make all postings to this group go to the parent group.
6949               (nconc (gnus-info-params (gnus-get-info name))
6950                      params)
6951             ;; Couldn't select this doc group.
6952             (switch-to-buffer buf)
6953             (gnus-set-global-variables)
6954             (gnus-configure-windows 'summary)
6955             (gnus-message 3 "Article couldn't be entered?"))
6956         (kill-buffer dig)))))
6957
6958 (defun gnus-summary-read-document (n)
6959   "Open a new group based on the current article(s).
6960 This will allow you to read digests and other similar
6961 documents as newsgroups.
6962 Obeys the standard process/prefix convention."
6963   (interactive "P")
6964   (let* ((articles (gnus-summary-work-articles n))
6965          (ogroup gnus-newsgroup-name)
6966          (params (append (gnus-info-params (gnus-get-info ogroup))
6967                          (list (cons 'to-group ogroup))))
6968          article group egroup groups vgroup)
6969     (while (setq article (pop articles))
6970       (setq group (format "%s-%d" gnus-newsgroup-name article))
6971       (gnus-summary-remove-process-mark article)
6972       (when (gnus-summary-display-article article)
6973         (save-excursion
6974           (with-temp-buffer
6975             (insert-buffer-substring gnus-original-article-buffer)
6976             ;; Remove some headers that may lead nndoc to make
6977             ;; the wrong guess.
6978             (message-narrow-to-head)
6979             (goto-char (point-min))
6980             (delete-matching-lines "^\\(Path\\):\\|^From ")
6981             (widen)
6982             (if (setq egroup
6983                       (gnus-group-read-ephemeral-group
6984                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6985                                      (nndoc-article-type guess))
6986                        t nil t))
6987                 (progn
6988                   ;; Make all postings to this group go to the parent group.
6989                   (nconc (gnus-info-params (gnus-get-info egroup))
6990                          params)
6991                   (push egroup groups))
6992               ;; Couldn't select this doc group.
6993               (gnus-error 3 "Article couldn't be entered"))))))
6994     ;; Now we have selected all the documents.
6995     (cond
6996      ((not groups)
6997       (error "None of the articles could be interpreted as documents"))
6998      ((gnus-group-read-ephemeral-group
6999        (setq vgroup (format
7000                      "nnvirtual:%s-%s" gnus-newsgroup-name
7001                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7002        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7003        t
7004        (cons (current-buffer) 'summary)))
7005      (t
7006       (error "Couldn't select virtual nndoc group")))))
7007
7008 (defun gnus-summary-isearch-article (&optional regexp-p)
7009   "Do incremental search forward on the current article.
7010 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7011   (interactive "P")
7012   (gnus-summary-select-article)
7013   (gnus-configure-windows 'article)
7014   (gnus-eval-in-buffer-window gnus-article-buffer
7015     (save-restriction
7016       (widen)
7017       (isearch-forward regexp-p))))
7018
7019 (defun gnus-summary-search-article-forward (regexp &optional backward)
7020   "Search for an article containing REGEXP forward.
7021 If BACKWARD, search backward instead."
7022   (interactive
7023    (list (read-string
7024           (format "Search article %s (regexp%s): "
7025                   (if current-prefix-arg "backward" "forward")
7026                   (if gnus-last-search-regexp
7027                       (concat ", default " gnus-last-search-regexp)
7028                     "")))
7029          current-prefix-arg))
7030   (if (string-equal regexp "")
7031       (setq regexp (or gnus-last-search-regexp ""))
7032     (setq gnus-last-search-regexp regexp))
7033   (if (gnus-summary-search-article regexp backward)
7034       (gnus-summary-show-thread)
7035     (error "Search failed: \"%s\"" regexp)))
7036
7037 (defun gnus-summary-search-article-backward (regexp)
7038   "Search for an article containing REGEXP backward."
7039   (interactive
7040    (list (read-string
7041           (format "Search article backward (regexp%s): "
7042                   (if gnus-last-search-regexp
7043                       (concat ", default " gnus-last-search-regexp)
7044                     "")))))
7045   (gnus-summary-search-article-forward regexp 'backward))
7046
7047 (defun gnus-summary-search-article (regexp &optional backward)
7048   "Search for an article containing REGEXP.
7049 Optional argument BACKWARD means do search for backward.
7050 `gnus-select-article-hook' is not called during the search."
7051   ;; We have to require this here to make sure that the following
7052   ;; dynamic binding isn't shadowed by autoloading.
7053   (require 'gnus-async)
7054   (require 'gnus-art)
7055   (let ((gnus-select-article-hook nil)  ;Disable hook.
7056         (gnus-article-prepare-hook nil)
7057         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7058         (gnus-use-article-prefetch nil)
7059         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7060         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7061         (sum (current-buffer))
7062         (gnus-display-mime-function nil)
7063         (found nil)
7064         point)
7065     (gnus-save-hidden-threads
7066       (gnus-summary-select-article)
7067       (set-buffer gnus-article-buffer)
7068       (goto-char (window-point (get-buffer-window (current-buffer))))
7069       (when backward
7070         (forward-line -1))
7071       (while (not found)
7072         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7073         (if (if backward
7074                 (re-search-backward regexp nil t)
7075               (re-search-forward regexp nil t))
7076             ;; We found the regexp.
7077             (progn
7078               (setq found 'found)
7079               (beginning-of-line)
7080               (set-window-start
7081                (get-buffer-window (current-buffer))
7082                (point))
7083               (forward-line 1)
7084               (set-window-point
7085                (get-buffer-window (current-buffer))
7086                (point))
7087               (set-buffer sum)
7088               (setq point (point)))
7089           ;; We didn't find it, so we go to the next article.
7090           (set-buffer sum)
7091           (setq found 'not)
7092           (while (eq found 'not)
7093             (if (not (if backward (gnus-summary-find-prev)
7094                        (gnus-summary-find-next)))
7095                 ;; No more articles.
7096                 (setq found t)
7097               ;; Select the next article and adjust point.
7098               (unless (gnus-summary-article-sparse-p
7099                        (gnus-summary-article-number))
7100                 (setq found nil)
7101                 (gnus-summary-select-article)
7102                 (set-buffer gnus-article-buffer)
7103                 (widen)
7104                 (goto-char (if backward (point-max) (point-min))))))))
7105       (gnus-message 7 ""))
7106     ;; Return whether we found the regexp.
7107     (when (eq found 'found)
7108       (goto-char point)
7109       (gnus-summary-show-thread)
7110       (gnus-summary-goto-subject gnus-current-article)
7111       (gnus-summary-position-point)
7112       t)))
7113
7114 (defun gnus-summary-find-matching (header regexp &optional backward unread
7115                                           not-case-fold)
7116   "Return a list of all articles that match REGEXP on HEADER.
7117 The search stars on the current article and goes forwards unless
7118 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7119 If UNREAD is non-nil, only unread articles will
7120 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7121 in the comparisons."
7122   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7123                 (gnus-data-find-list
7124                  (gnus-summary-article-number) (gnus-data-list backward))))
7125         (case-fold-search (not not-case-fold))
7126         articles d func)
7127     (if (consp header)
7128         (if (eq (car header) 'extra)
7129             (setq func
7130                   `(lambda (h)
7131                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7132                          "")))
7133           (error "%s is an invalid header" header))
7134       (unless (fboundp (intern (concat "mail-header-" header)))
7135         (error "%s is not a valid header" header))
7136       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7137     (while data
7138       (setq d (car data))
7139       (and (or (not unread)             ; We want all articles...
7140                (gnus-data-unread-p d))  ; Or just unreads.
7141            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7142            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7143            (push (gnus-data-number d) articles)) ; Success!
7144       (setq data (cdr data)))
7145     (nreverse articles)))
7146
7147 (defun gnus-summary-execute-command (header regexp command &optional backward)
7148   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7149 If HEADER is an empty string (or nil), the match is done on the entire
7150 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7151   (interactive
7152    (list (let ((completion-ignore-case t))
7153            (completing-read
7154             "Header name: "
7155             (mapcar (lambda (string) (list string))
7156                     '("Number" "Subject" "From" "Lines" "Date"
7157                       "Message-ID" "Xref" "References" "Body"))
7158             nil 'require-match))
7159          (read-string "Regexp: ")
7160          (read-key-sequence "Command: ")
7161          current-prefix-arg))
7162   (when (equal header "Body")
7163     (setq header ""))
7164   ;; Hidden thread subtrees must be searched as well.
7165   (gnus-summary-show-all-threads)
7166   ;; We don't want to change current point nor window configuration.
7167   (save-excursion
7168     (save-window-excursion
7169       (gnus-message 6 "Executing %s..." (key-description command))
7170       ;; We'd like to execute COMMAND interactively so as to give arguments.
7171       (gnus-execute header regexp
7172                     `(call-interactively ',(key-binding command))
7173                     backward)
7174       (gnus-message 6 "Executing %s...done" (key-description command)))))
7175
7176 (defun gnus-summary-beginning-of-article ()
7177   "Scroll the article back to the beginning."
7178   (interactive)
7179   (gnus-summary-select-article)
7180   (gnus-configure-windows 'article)
7181   (gnus-eval-in-buffer-window gnus-article-buffer
7182     (widen)
7183     (goto-char (point-min))
7184     (when gnus-page-broken
7185       (gnus-narrow-to-page))))
7186
7187 (defun gnus-summary-end-of-article ()
7188   "Scroll to the end of the article."
7189   (interactive)
7190   (gnus-summary-select-article)
7191   (gnus-configure-windows 'article)
7192   (gnus-eval-in-buffer-window gnus-article-buffer
7193     (widen)
7194     (goto-char (point-max))
7195     (recenter -3)
7196     (when gnus-page-broken
7197       (gnus-narrow-to-page))))
7198
7199 (defun gnus-summary-print-article (&optional filename n)
7200   "Generate and print a PostScript image of the N next (mail) articles.
7201
7202 If N is negative, print the N previous articles.  If N is nil and articles
7203 have been marked with the process mark, print these instead.
7204
7205 If the optional first argument FILENAME is nil, send the image to the
7206 printer.  If FILENAME is a string, save the PostScript image in a file with
7207 that name.  If FILENAME is a number, prompt the user for the name of the file
7208 to save in."
7209   (interactive (list (ps-print-preprint current-prefix-arg)
7210                      current-prefix-arg))
7211   (dolist (article (gnus-summary-work-articles n))
7212     (gnus-summary-select-article nil nil 'pseudo article)
7213     (gnus-eval-in-buffer-window gnus-article-buffer
7214       (let ((buffer (generate-new-buffer " *print*")))
7215         (unwind-protect
7216             (progn
7217               (copy-to-buffer buffer (point-min) (point-max))
7218               (set-buffer buffer)
7219               (gnus-article-delete-invisible-text)
7220               (let ((ps-left-header
7221                      (list
7222                       (concat "("
7223                               (mail-header-subject gnus-current-headers) ")")
7224                       (concat "("
7225                               (mail-header-from gnus-current-headers) ")")))
7226                     (ps-right-header
7227                      (list
7228                       "/pagenumberstring load"
7229                       (concat "("
7230                               (mail-header-date gnus-current-headers) ")"))))
7231                 (gnus-run-hooks 'gnus-ps-print-hook)
7232                 (save-excursion
7233                   (ps-print-buffer-with-faces filename))))
7234           (kill-buffer buffer))))))
7235
7236 (defun gnus-summary-show-article (&optional arg)
7237   "Force re-fetching of the current article.
7238 If ARG (the prefix) is a number, show the article with the charset
7239 defined in `gnus-summary-show-article-charset-alist', or the charset
7240 inputed.
7241 If ARG (the prefix) is non-nil and not a number, show the raw article
7242 without any article massaging functions being run."
7243   (interactive "P")
7244   (cond
7245    ((numberp arg)
7246     (let ((gnus-newsgroup-charset
7247            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7248                (read-coding-system "Charset: ")))
7249           (gnus-newsgroup-ignored-charsets 'gnus-all))
7250       (gnus-summary-select-article nil 'force)))
7251    ((not arg)
7252     ;; Select the article the normal way.
7253     (gnus-summary-select-article nil 'force))
7254    (t
7255     ;; We have to require this here to make sure that the following
7256     ;; dynamic binding isn't shadowed by autoloading.
7257     (require 'gnus-async)
7258     (require 'gnus-art)
7259     ;; Bind the article treatment functions to nil.
7260     (let ((gnus-have-all-headers t)
7261           gnus-article-prepare-hook
7262           gnus-article-decode-hook
7263           gnus-display-mime-function
7264           gnus-break-pages)
7265       ;; Destroy any MIME parts.
7266       (when (gnus-buffer-live-p gnus-article-buffer)
7267         (save-excursion
7268           (set-buffer gnus-article-buffer)
7269           (mm-destroy-parts gnus-article-mime-handles)
7270           ;; Set it to nil for safety reason.
7271           (setq gnus-article-mime-handle-alist nil)
7272           (setq gnus-article-mime-handles nil)))
7273       (gnus-summary-select-article nil 'force))))
7274   (gnus-summary-goto-subject gnus-current-article)
7275   (gnus-summary-position-point))
7276
7277 (defun gnus-summary-verbose-headers (&optional arg)
7278   "Toggle permanent full header display.
7279 If ARG is a positive number, turn header display on.
7280 If ARG is a negative number, turn header display off."
7281   (interactive "P")
7282   (setq gnus-show-all-headers
7283         (cond ((or (not (numberp arg))
7284                    (zerop arg))
7285                (not gnus-show-all-headers))
7286               ((natnump arg)
7287                t)))
7288   (gnus-summary-show-article))
7289
7290 (defun gnus-summary-toggle-header (&optional arg)
7291   "Show the headers if they are hidden, or hide them if they are shown.
7292 If ARG is a positive number, show the entire header.
7293 If ARG is a negative number, hide the unwanted header lines."
7294   (interactive "P")
7295   (save-excursion
7296     (set-buffer gnus-article-buffer)
7297     (save-restriction
7298       (let* ((buffer-read-only nil)
7299              (inhibit-point-motion-hooks t)
7300              hidden e)
7301         (setq hidden
7302               (if (numberp arg)
7303                   (>= arg 0)
7304                 (save-restriction
7305                   (article-narrow-to-head)
7306                   (gnus-article-hidden-text-p 'headers))))
7307         (goto-char (point-min))
7308         (when (search-forward "\n\n" nil t)
7309           (delete-region (point-min) (1- (point))))
7310         (goto-char (point-min))
7311         (save-excursion
7312           (set-buffer gnus-original-article-buffer)
7313           (goto-char (point-min))
7314           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7315         (insert-buffer-substring gnus-original-article-buffer 1 e)
7316         (save-restriction
7317           (narrow-to-region (point-min) (point))
7318           (article-decode-encoded-words)
7319           (if  hidden
7320               (let ((gnus-treat-hide-headers nil)
7321                     (gnus-treat-hide-boring-headers nil))
7322                 (setq gnus-article-wash-types
7323                       (delq 'headers gnus-article-wash-types))
7324                 (gnus-treat-article 'head))
7325             (gnus-treat-article 'head)))
7326         (gnus-set-mode-line 'article)))))
7327
7328 (defun gnus-summary-show-all-headers ()
7329   "Make all header lines visible."
7330   (interactive)
7331   (gnus-article-show-all-headers))
7332
7333 (defun gnus-summary-caesar-message (&optional arg)
7334   "Caesar rotate the current article by 13.
7335 The numerical prefix specifies how many places to rotate each letter
7336 forward."
7337   (interactive "P")
7338   (gnus-summary-select-article)
7339   (let ((mail-header-separator ""))
7340     (gnus-eval-in-buffer-window gnus-article-buffer
7341       (save-restriction
7342         (widen)
7343         (let ((start (window-start))
7344               buffer-read-only)
7345           (message-caesar-buffer-body arg)
7346           (set-window-start (get-buffer-window (current-buffer)) start))))))
7347
7348 (defun gnus-summary-stop-page-breaking ()
7349   "Stop page breaking in the current article."
7350   (interactive)
7351   (gnus-summary-select-article)
7352   (gnus-eval-in-buffer-window gnus-article-buffer
7353     (widen)
7354     (when (gnus-visual-p 'page-marker)
7355       (let ((buffer-read-only nil))
7356         (gnus-remove-text-with-property 'gnus-prev)
7357         (gnus-remove-text-with-property 'gnus-next))
7358       (setq gnus-page-broken nil))))
7359
7360 (defun gnus-summary-move-article (&optional n to-newsgroup
7361                                             select-method action)
7362   "Move the current article to a different newsgroup.
7363 If N is a positive number, move the N next articles.
7364 If N is a negative number, move the N previous articles.
7365 If N is nil and any articles have been marked with the process mark,
7366 move those articles instead.
7367 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7368 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7369 re-spool using this method.
7370
7371 For this function to work, both the current newsgroup and the
7372 newsgroup that you want to move to have to support the `request-move'
7373 and `request-accept' functions.
7374
7375 ACTION can be either `move' (the default), `crosspost' or `copy'."
7376   (interactive "P")
7377   (unless action
7378     (setq action 'move))
7379   ;; Disable marking as read.
7380   (let (gnus-mark-article-hook)
7381     (save-window-excursion
7382       (gnus-summary-select-article)))
7383   ;; Check whether the source group supports the required functions.
7384   (cond ((and (eq action 'move)
7385               (not (gnus-check-backend-function
7386                     'request-move-article gnus-newsgroup-name)))
7387          (error "The current group does not support article moving"))
7388         ((and (eq action 'crosspost)
7389               (not (gnus-check-backend-function
7390                     'request-replace-article gnus-newsgroup-name)))
7391          (error "The current group does not support article editing")))
7392   (let ((articles (gnus-summary-work-articles n))
7393         (prefix (if (gnus-check-backend-function
7394                     'request-move-article gnus-newsgroup-name)
7395                     (gnus-group-real-prefix gnus-newsgroup-name)
7396                   ""))
7397         (names '((move "Move" "Moving")
7398                  (copy "Copy" "Copying")
7399                  (crosspost "Crosspost" "Crossposting")))
7400         (copy-buf (save-excursion
7401                     (nnheader-set-temp-buffer " *copy article*")))
7402         art-group to-method new-xref article to-groups)
7403     (unless (assq action names)
7404       (error "Unknown action %s" action))
7405     ;; Read the newsgroup name.
7406     (when (and (not to-newsgroup)
7407                (not select-method))
7408       (setq to-newsgroup
7409             (gnus-read-move-group-name
7410              (cadr (assq action names))
7411              (symbol-value (intern (format "gnus-current-%s-group" action)))
7412              articles prefix))
7413       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7414     (setq to-method (or select-method
7415                         (gnus-server-to-method
7416                          (gnus-group-method to-newsgroup))))
7417     ;; Check the method we are to move this article to...
7418     (unless (gnus-check-backend-function
7419              'request-accept-article (car to-method))
7420       (error "%s does not support article copying" (car to-method)))
7421     (unless (gnus-check-server to-method)
7422       (error "Can't open server %s" (car to-method)))
7423     (gnus-message 6 "%s to %s: %s..."
7424                   (caddr (assq action names))
7425                   (or (car select-method) to-newsgroup) articles)
7426     (while articles
7427       (setq article (pop articles))
7428       (setq
7429        art-group
7430        (cond
7431         ;; Move the article.
7432         ((eq action 'move)
7433          ;; Remove this article from future suppression.
7434          (gnus-dup-unsuppress-article article)
7435          (gnus-request-move-article
7436           article                       ; Article to move
7437           gnus-newsgroup-name           ; From newsgroup
7438           (nth 1 (gnus-find-method-for-group
7439                   gnus-newsgroup-name)) ; Server
7440           (list 'gnus-request-accept-article
7441                 to-newsgroup (list 'quote select-method)
7442                 (not articles) t)       ; Accept form
7443           (not articles)))              ; Only save nov last time
7444         ;; Copy the article.
7445         ((eq action 'copy)
7446          (save-excursion
7447            (set-buffer copy-buf)
7448            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7449              (gnus-request-accept-article
7450               to-newsgroup select-method (not articles) t))))
7451         ;; Crosspost the article.
7452         ((eq action 'crosspost)
7453          (let ((xref (message-tokenize-header
7454                       (mail-header-xref (gnus-summary-article-header article))
7455                       " ")))
7456            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7457                                   ":" article))
7458            (unless xref
7459              (setq xref (list (system-name))))
7460            (setq new-xref
7461                  (concat
7462                   (mapconcat 'identity
7463                              (delete "Xref:" (delete new-xref xref))
7464                              " ")
7465                   " " new-xref))
7466            (save-excursion
7467              (set-buffer copy-buf)
7468              ;; First put the article in the destination group.
7469              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7470              (when (consp (setq art-group
7471                                 (gnus-request-accept-article
7472                                  to-newsgroup select-method (not articles))))
7473                (setq new-xref (concat new-xref " " (car art-group)
7474                                       ":" (cdr art-group)))
7475                ;; Now we have the new Xrefs header, so we insert
7476                ;; it and replace the new article.
7477                (nnheader-replace-header "Xref" new-xref)
7478                (gnus-request-replace-article
7479                 (cdr art-group) to-newsgroup (current-buffer))
7480                art-group))))))
7481       (cond
7482        ((not art-group)
7483         (gnus-message 1 "Couldn't %s article %s: %s"
7484                       (cadr (assq action names)) article
7485                       (nnheader-get-report (car to-method))))
7486        ((eq art-group 'junk)
7487         (when (eq action 'move)
7488           (gnus-summary-mark-article article gnus-canceled-mark)
7489           (gnus-message 4 "Deleted article %s" article)))
7490        (t
7491         (let* ((pto-group (gnus-group-prefixed-name
7492                            (car art-group) to-method))
7493                (entry
7494                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7495                (info (nth 2 entry))
7496                (to-group (gnus-info-group info))
7497                to-marks)
7498           ;; Update the group that has been moved to.
7499           (when (and info
7500                      (memq action '(move copy)))
7501             (unless (member to-group to-groups)
7502               (push to-group to-groups))
7503
7504             (unless (memq article gnus-newsgroup-unreads)
7505               (push 'read to-marks)
7506               (gnus-info-set-read
7507                info (gnus-add-to-range (gnus-info-read info)
7508                                        (list (cdr art-group)))))
7509
7510             ;; See whether the article is to be put in the cache.
7511             (let ((marks gnus-article-mark-lists)
7512                   (to-article (cdr art-group)))
7513
7514               ;; Enter the article into the cache in the new group,
7515               ;; if that is required.
7516               (when gnus-use-cache
7517                 (gnus-cache-possibly-enter-article
7518                  to-group to-article
7519                  (memq article gnus-newsgroup-marked)
7520                  (memq article gnus-newsgroup-dormant)
7521                  (memq article gnus-newsgroup-unreads)))
7522
7523               (when gnus-preserve-marks
7524                 ;; Copy any marks over to the new group.
7525                 (when (and (equal to-group gnus-newsgroup-name)
7526                            (not (memq article gnus-newsgroup-unreads)))
7527                   ;; Mark this article as read in this group.
7528                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7529                   (setcdr (gnus-active to-group) to-article)
7530                   (setcdr gnus-newsgroup-active to-article))
7531
7532                 (while marks
7533                   (when (memq article (symbol-value
7534                                        (intern (format "gnus-newsgroup-%s"
7535                                                        (caar marks)))))
7536                     (push (cdar marks) to-marks)
7537                     ;; If the other group is the same as this group,
7538                     ;; then we have to add the mark to the list.
7539                     (when (equal to-group gnus-newsgroup-name)
7540                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7541                            (cons to-article
7542                                  (symbol-value
7543                                   (intern (format "gnus-newsgroup-%s"
7544                                                   (caar marks)))))))
7545                     ;; Copy the marks to other group.
7546                     (gnus-add-marked-articles
7547                      to-group (cdar marks) (list to-article) info))
7548                   (setq marks (cdr marks)))
7549
7550                 (gnus-request-set-mark to-group (list (list (list to-article)
7551                                                             'set
7552                                                             to-marks))))
7553
7554               (gnus-dribble-enter
7555                (concat "(gnus-group-set-info '"
7556                        (gnus-prin1-to-string (gnus-get-info to-group))
7557                        ")"))))
7558
7559           ;; Update the Xref header in this article to point to
7560           ;; the new crossposted article we have just created.
7561           (when (eq action 'crosspost)
7562             (save-excursion
7563               (set-buffer copy-buf)
7564               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7565               (nnheader-replace-header "Xref" new-xref)
7566               (gnus-request-replace-article
7567                article gnus-newsgroup-name (current-buffer)))))
7568
7569         ;;;!!!Why is this necessary?
7570         (set-buffer gnus-summary-buffer)
7571
7572         (gnus-summary-goto-subject article)
7573         (when (eq action 'move)
7574           (gnus-summary-mark-article article gnus-canceled-mark))))
7575       (gnus-summary-remove-process-mark article))
7576     ;; Re-activate all groups that have been moved to.
7577     (while to-groups
7578       (save-excursion
7579         (set-buffer gnus-group-buffer)
7580         (when (gnus-group-goto-group (car to-groups) t)
7581           (gnus-group-get-new-news-this-group 1 t))
7582         (pop to-groups)))
7583
7584     (gnus-kill-buffer copy-buf)
7585     (gnus-summary-position-point)
7586     (gnus-set-mode-line 'summary)))
7587
7588 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7589   "Move the current article to a different newsgroup.
7590 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7591 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7592 re-spool using this method."
7593   (interactive "P")
7594   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7595
7596 (defun gnus-summary-crosspost-article (&optional n)
7597   "Crosspost the current article to some other group."
7598   (interactive "P")
7599   (gnus-summary-move-article n nil nil 'crosspost))
7600
7601 (defcustom gnus-summary-respool-default-method nil
7602   "Default method for respooling an article.
7603 If nil, use to the current newsgroup method."
7604   :type '(choice (gnus-select-method :value (nnml ""))
7605                  (const nil))
7606   :group 'gnus-summary-mail)
7607
7608 (defun gnus-summary-respool-article (&optional n method)
7609   "Respool the current article.
7610 The article will be squeezed through the mail spooling process again,
7611 which means that it will be put in some mail newsgroup or other
7612 depending on `nnmail-split-methods'.
7613 If N is a positive number, respool the N next articles.
7614 If N is a negative number, respool the N previous articles.
7615 If N is nil and any articles have been marked with the process mark,
7616 respool those articles instead.
7617
7618 Respooling can be done both from mail groups and \"real\" newsgroups.
7619 In the former case, the articles in question will be moved from the
7620 current group into whatever groups they are destined to.  In the
7621 latter case, they will be copied into the relevant groups."
7622   (interactive
7623    (list current-prefix-arg
7624          (let* ((methods (gnus-methods-using 'respool))
7625                 (methname
7626                  (symbol-name (or gnus-summary-respool-default-method
7627                                   (car (gnus-find-method-for-group
7628                                         gnus-newsgroup-name)))))
7629                 (method
7630                  (gnus-completing-read
7631                   methname "What backend do you want to use when respooling?"
7632                   methods nil t nil 'gnus-mail-method-history))
7633                 ms)
7634            (cond
7635             ((zerop (length (setq ms (gnus-servers-using-backend
7636                                       (intern method)))))
7637              (list (intern method) ""))
7638             ((= 1 (length ms))
7639              (car ms))
7640             (t
7641              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7642                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7643                            ms-alist))))))))
7644   (unless method
7645     (error "No method given for respooling"))
7646   (if (assoc (symbol-name
7647               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7648              (gnus-methods-using 'respool))
7649       (gnus-summary-move-article n nil method)
7650     (gnus-summary-copy-article n nil method)))
7651
7652 (defun gnus-summary-import-article (file)
7653   "Import an arbitrary file into a mail newsgroup."
7654   (interactive "fImport file: ")
7655   (let ((group gnus-newsgroup-name)
7656         (now (current-time))
7657         atts lines)
7658     (unless (gnus-check-backend-function 'request-accept-article group)
7659       (error "%s does not support article importing" group))
7660     (or (file-readable-p file)
7661         (not (file-regular-p file))
7662         (error "Can't read %s" file))
7663     (save-excursion
7664       (set-buffer (gnus-get-buffer-create " *import file*"))
7665       (erase-buffer)
7666       (nnheader-insert-file-contents file)
7667       (goto-char (point-min))
7668       (unless (nnheader-article-p)
7669         ;; This doesn't look like an article, so we fudge some headers.
7670         (setq atts (file-attributes file)
7671               lines (count-lines (point-min) (point-max)))
7672         (insert "From: " (read-string "From: ") "\n"
7673                 "Subject: " (read-string "Subject: ") "\n"
7674                 "Date: " (message-make-date (nth 5 atts))
7675                 "\n"
7676                 "Message-ID: " (message-make-message-id) "\n"
7677                 "Lines: " (int-to-string lines) "\n"
7678                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7679       (gnus-request-accept-article group nil t)
7680       (kill-buffer (current-buffer)))))
7681
7682 (defun gnus-summary-article-posted-p ()
7683   "Say whether the current (mail) article is available from news as well.
7684 This will be the case if the article has both been mailed and posted."
7685   (interactive)
7686   (let ((id (mail-header-references (gnus-summary-article-header)))
7687         (gnus-override-method (car (gnus-refer-article-methods))))
7688     (if (gnus-request-head id "")
7689         (gnus-message 2 "The current message was found on %s"
7690                       gnus-override-method)
7691       (gnus-message 2 "The current message couldn't be found on %s"
7692                     gnus-override-method)
7693       nil)))
7694
7695 (defun gnus-summary-expire-articles (&optional now)
7696   "Expire all articles that are marked as expirable in the current group."
7697   (interactive)
7698   (when (gnus-check-backend-function
7699          'request-expire-articles gnus-newsgroup-name)
7700     ;; This backend supports expiry.
7701     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7702            (expirable (if total
7703                           (progn
7704                             ;; We need to update the info for
7705                             ;; this group for `gnus-list-of-read-articles'
7706                             ;; to give us the right answer.
7707                             (gnus-run-hooks 'gnus-exit-group-hook)
7708                             (gnus-summary-update-info)
7709                             (gnus-list-of-read-articles gnus-newsgroup-name))
7710                         (setq gnus-newsgroup-expirable
7711                               (sort gnus-newsgroup-expirable '<))))
7712            (expiry-wait (if now 'immediate
7713                           (gnus-group-find-parameter
7714                            gnus-newsgroup-name 'expiry-wait)))
7715            (nnmail-expiry-target
7716             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7717                 nnmail-expiry-target))
7718            es)
7719       (when expirable
7720         ;; There are expirable articles in this group, so we run them
7721         ;; through the expiry process.
7722         (gnus-message 6 "Expiring articles...")
7723         (unless (gnus-check-group gnus-newsgroup-name)
7724           (error "Can't open server for %s" gnus-newsgroup-name))
7725         ;; The list of articles that weren't expired is returned.
7726         (save-excursion
7727           (if expiry-wait
7728               (let ((nnmail-expiry-wait-function nil)
7729                     (nnmail-expiry-wait expiry-wait))
7730                 (setq es (gnus-request-expire-articles
7731                           expirable gnus-newsgroup-name)))
7732             (setq es (gnus-request-expire-articles
7733                       expirable gnus-newsgroup-name))))
7734         (unless total
7735           (setq gnus-newsgroup-expirable es))
7736         ;; We go through the old list of expirable, and mark all
7737         ;; really expired articles as nonexistent.
7738         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7739           (let ((gnus-use-cache nil))
7740             (while expirable
7741               (unless (memq (car expirable) es)
7742                 (when (gnus-data-find (car expirable))
7743                   (gnus-summary-mark-article
7744                    (car expirable) gnus-canceled-mark)))
7745               (setq expirable (cdr expirable)))))
7746         (gnus-message 6 "Expiring articles...done")))))
7747
7748 (defun gnus-summary-expire-articles-now ()
7749   "Expunge all expirable articles in the current group.
7750 This means that *all* articles that are marked as expirable will be
7751 deleted forever, right now."
7752   (interactive)
7753   (or gnus-expert-user
7754       (gnus-yes-or-no-p
7755        "Are you really, really, really sure you want to delete all these messages? ")
7756       (error "Phew!"))
7757   (gnus-summary-expire-articles t))
7758
7759 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7760 (defun gnus-summary-delete-article (&optional n)
7761   "Delete the N next (mail) articles.
7762 This command actually deletes articles.  This is not a marking
7763 command.  The article will disappear forever from your life, never to
7764 return.
7765 If N is negative, delete backwards.
7766 If N is nil and articles have been marked with the process mark,
7767 delete these instead."
7768   (interactive "P")
7769   (unless (gnus-check-backend-function 'request-expire-articles
7770                                        gnus-newsgroup-name)
7771     (error "The current newsgroup does not support article deletion"))
7772   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7773     (error "Couldn't open server"))
7774   ;; Compute the list of articles to delete.
7775   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7776         not-deleted)
7777     (if (and gnus-novice-user
7778              (not (gnus-yes-or-no-p
7779                    (format "Do you really want to delete %s forever? "
7780                            (if (> (length articles) 1)
7781                                (format "these %s articles" (length articles))
7782                              "this article")))))
7783         ()
7784       ;; Delete the articles.
7785       (setq not-deleted (gnus-request-expire-articles
7786                          articles gnus-newsgroup-name 'force))
7787       (while articles
7788         (gnus-summary-remove-process-mark (car articles))
7789         ;; The backend might not have been able to delete the article
7790         ;; after all.
7791         (unless (memq (car articles) not-deleted)
7792           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7793         (setq articles (cdr articles)))
7794       (when not-deleted
7795         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7796     (gnus-summary-position-point)
7797     (gnus-set-mode-line 'summary)
7798     not-deleted))
7799
7800 (defun gnus-summary-edit-article (&optional arg)
7801   "Edit the current article.
7802 This will have permanent effect only in mail groups.
7803 If ARG is nil, edit the decoded articles.
7804 If ARG is 1, edit the raw articles. 
7805 If ARG is 2, edit the raw articles even in read-only groups.
7806 Otherwise, allow editing of articles even in read-only
7807 groups."
7808   (interactive "P")
7809   (let (force raw)
7810     (cond 
7811      ((null arg))
7812      ((eq arg 1) (setq raw t))
7813      ((eq arg 2) (setq raw t
7814                        force t))
7815      (t (setq force t)))
7816     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7817         (error "Can't edit the raw article in group nndraft:drafts."))
7818     (save-excursion
7819       (set-buffer gnus-summary-buffer)
7820       (let ((mail-parse-charset gnus-newsgroup-charset)
7821             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7822         (gnus-set-global-variables)
7823         (when (and (not force)
7824                    (gnus-group-read-only-p))
7825           (error "The current newsgroup does not support article editing"))
7826         (gnus-summary-show-article t)
7827         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7828           (with-current-buffer gnus-article-buffer
7829             (mm-enable-multibyte)))
7830         (if (equal gnus-newsgroup-name "nndraft:drafts")
7831             (setq raw t))
7832         (gnus-article-edit-article
7833          (if raw 'ignore 
7834            #'(lambda () 
7835                (let ((mbl mml-buffer-list))
7836                  (setq mml-buffer-list nil)
7837                  (mime-to-mml)
7838                  (make-local-hook 'kill-buffer-hook)
7839                  (let ((mml-buffer-list mml-buffer-list))
7840                    (setq mml-buffer-list mbl)
7841                    (make-local-variable 'mml-buffer-list))
7842                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7843          `(lambda (no-highlight)
7844             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7845                   (mail-parse-ignored-charsets 
7846                    ',gnus-newsgroup-ignored-charsets))
7847               ,(if (not raw) '(progn 
7848                                 (mml-to-mime)
7849                                 (mml-destroy-buffers)
7850                                 (remove-hook 'kill-buffer-hook 
7851                                              'mml-destroy-buffers t)
7852                                 (kill-local-variable 'mml-buffer-list)))
7853               (gnus-summary-edit-article-done
7854                ,(or (mail-header-references gnus-current-headers) "")
7855                ,(gnus-group-read-only-p) 
7856                ,gnus-summary-buffer no-highlight))))))))
7857
7858 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7859
7860 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7861                                                  no-highlight)
7862   "Make edits to the current article permanent."
7863   (interactive)
7864   ;; Replace the article.
7865   (let ((buf (current-buffer)))
7866     (with-temp-buffer
7867       (insert-buffer-substring buf)
7868       (if (and (not read-only)
7869                (not (gnus-request-replace-article
7870                      (cdr gnus-article-current) (car gnus-article-current)
7871                      (current-buffer) t)))
7872           (error "Couldn't replace article")
7873         ;; Update the summary buffer.
7874         (if (and references
7875                  (equal (message-tokenize-header references " ")
7876                         (message-tokenize-header
7877                          (or (message-fetch-field "references") "") " ")))
7878             ;; We only have to update this line.
7879             (save-excursion
7880               (save-restriction
7881                 (message-narrow-to-head)
7882                 (let ((head (buffer-string))
7883                       header)
7884                   (with-temp-buffer
7885                     (insert (format "211 %d Article retrieved.\n"
7886                                     (cdr gnus-article-current)))
7887                     (insert head)
7888                     (insert ".\n")
7889                     (let ((nntp-server-buffer (current-buffer)))
7890                       (setq header (car (gnus-get-newsgroup-headers
7891                                          (save-excursion
7892                                            (set-buffer gnus-summary-buffer)
7893                                            gnus-newsgroup-dependencies)
7894                                          t))))
7895                     (save-excursion
7896                       (set-buffer gnus-summary-buffer)
7897                       (gnus-data-set-header
7898                        (gnus-data-find (cdr gnus-article-current))
7899                        header)
7900                       (gnus-summary-update-article-line
7901                        (cdr gnus-article-current) header))))))
7902           ;; Update threads.
7903           (set-buffer (or buffer gnus-summary-buffer))
7904           (gnus-summary-update-article (cdr gnus-article-current)))
7905         ;; Prettify the article buffer again.
7906         (unless no-highlight
7907           (save-excursion
7908             (set-buffer gnus-article-buffer)
7909             ;;;!!! Fix this -- article should be rehighlighted.
7910             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7911             (set-buffer gnus-original-article-buffer)
7912             (gnus-request-article
7913              (cdr gnus-article-current)
7914              (car gnus-article-current) (current-buffer))))
7915         ;; Prettify the summary buffer line.
7916         (when (gnus-visual-p 'summary-highlight 'highlight)
7917           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7918
7919 (defun gnus-summary-edit-wash (key)
7920   "Perform editing command KEY in the article buffer."
7921   (interactive
7922    (list
7923     (progn
7924       (message "%s" (concat (this-command-keys) "- "))
7925       (read-char))))
7926   (message "")
7927   (gnus-summary-edit-article)
7928   (execute-kbd-macro (concat (this-command-keys) key))
7929   (gnus-article-edit-done))
7930
7931 ;;; Respooling
7932
7933 (defun gnus-summary-respool-query (&optional silent trace)
7934   "Query where the respool algorithm would put this article."
7935   (interactive)
7936   (let (gnus-mark-article-hook)
7937     (gnus-summary-select-article)
7938     (save-excursion
7939       (set-buffer gnus-original-article-buffer)
7940       (save-restriction
7941         (message-narrow-to-head)
7942         (let ((groups (nnmail-article-group 'identity trace)))
7943           (unless silent
7944             (if groups
7945                 (message "This message would go to %s"
7946                          (mapconcat 'car groups ", "))
7947               (message "This message would go to no groups"))
7948             groups))))))
7949
7950 (defun gnus-summary-respool-trace ()
7951   "Trace where the respool algorithm would put this article.
7952 Display a buffer showing all fancy splitting patterns which matched."
7953   (interactive)
7954   (gnus-summary-respool-query nil t))
7955
7956 ;; Summary marking commands.
7957
7958 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7959   "Mark articles which has the same subject as read, and then select the next.
7960 If UNMARK is positive, remove any kind of mark.
7961 If UNMARK is negative, tick articles."
7962   (interactive "P")
7963   (when unmark
7964     (setq unmark (prefix-numeric-value unmark)))
7965   (let ((count
7966          (gnus-summary-mark-same-subject
7967           (gnus-summary-article-subject) unmark)))
7968     ;; Select next unread article.  If auto-select-same mode, should
7969     ;; select the first unread article.
7970     (gnus-summary-next-article t (and gnus-auto-select-same
7971                                       (gnus-summary-article-subject)))
7972     (gnus-message 7 "%d article%s marked as %s"
7973                   count (if (= count 1) " is" "s are")
7974                   (if unmark "unread" "read"))))
7975
7976 (defun gnus-summary-kill-same-subject (&optional unmark)
7977   "Mark articles which has the same subject as read.
7978 If UNMARK is positive, remove any kind of mark.
7979 If UNMARK is negative, tick articles."
7980   (interactive "P")
7981   (when unmark
7982     (setq unmark (prefix-numeric-value unmark)))
7983   (let ((count
7984          (gnus-summary-mark-same-subject
7985           (gnus-summary-article-subject) unmark)))
7986     ;; If marked as read, go to next unread subject.
7987     (when (null unmark)
7988       ;; Go to next unread subject.
7989       (gnus-summary-next-subject 1 t))
7990     (gnus-message 7 "%d articles are marked as %s"
7991                   count (if unmark "unread" "read"))))
7992
7993 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7994   "Mark articles with same SUBJECT as read, and return marked number.
7995 If optional argument UNMARK is positive, remove any kinds of marks.
7996 If optional argument UNMARK is negative, mark articles as unread instead."
7997   (let ((count 1))
7998     (save-excursion
7999       (cond
8000        ((null unmark)                   ; Mark as read.
8001         (while (and
8002                 (progn
8003                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8004                   (gnus-summary-show-thread) t)
8005                 (gnus-summary-find-subject subject))
8006           (setq count (1+ count))))
8007        ((> unmark 0)                    ; Tick.
8008         (while (and
8009                 (progn
8010                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8011                   (gnus-summary-show-thread) t)
8012                 (gnus-summary-find-subject subject))
8013           (setq count (1+ count))))
8014        (t                               ; Mark as unread.
8015         (while (and
8016                 (progn
8017                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8018                   (gnus-summary-show-thread) t)
8019                 (gnus-summary-find-subject subject))
8020           (setq count (1+ count)))))
8021       (gnus-set-mode-line 'summary)
8022       ;; Return the number of marked articles.
8023       count)))
8024
8025 (defun gnus-summary-mark-as-processable (n &optional unmark)
8026   "Set the process mark on the next N articles.
8027 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8028 the process mark instead.  The difference between N and the actual
8029 number of articles marked is returned."
8030   (interactive "p")
8031   (let ((backward (< n 0))
8032         (n (abs n)))
8033     (while (and
8034             (> n 0)
8035             (if unmark
8036                 (gnus-summary-remove-process-mark
8037                  (gnus-summary-article-number))
8038               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8039             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8040       (setq n (1- n)))
8041     (when (/= 0 n)
8042       (gnus-message 7 "No more articles"))
8043     (gnus-summary-recenter)
8044     (gnus-summary-position-point)
8045     n))
8046
8047 (defun gnus-summary-unmark-as-processable (n)
8048   "Remove the process mark from the next N articles.
8049 If N is negative, unmark backward instead.  The difference between N and
8050 the actual number of articles unmarked is returned."
8051   (interactive "p")
8052   (gnus-summary-mark-as-processable n t))
8053
8054 (defun gnus-summary-unmark-all-processable ()
8055   "Remove the process mark from all articles."
8056   (interactive)
8057   (save-excursion
8058     (while gnus-newsgroup-processable
8059       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8060   (gnus-summary-position-point))
8061
8062 (defun gnus-summary-mark-as-expirable (n)
8063   "Mark N articles forward as expirable.
8064 If N is negative, mark backward instead.  The difference between N and
8065 the actual number of articles marked is returned."
8066   (interactive "p")
8067   (gnus-summary-mark-forward n gnus-expirable-mark))
8068
8069 (defun gnus-summary-mark-article-as-replied (article)
8070   "Mark ARTICLE replied and update the summary line."
8071   (push article gnus-newsgroup-replied)
8072   (let ((buffer-read-only nil))
8073     (when (gnus-summary-goto-subject article nil t)
8074       (gnus-summary-update-secondary-mark article))))
8075
8076 (defun gnus-summary-set-bookmark (article)
8077   "Set a bookmark in current article."
8078   (interactive (list (gnus-summary-article-number)))
8079   (when (or (not (get-buffer gnus-article-buffer))
8080             (not gnus-current-article)
8081             (not gnus-article-current)
8082             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8083     (error "No current article selected"))
8084   ;; Remove old bookmark, if one exists.
8085   (let ((old (assq article gnus-newsgroup-bookmarks)))
8086     (when old
8087       (setq gnus-newsgroup-bookmarks
8088             (delq old gnus-newsgroup-bookmarks))))
8089   ;; Set the new bookmark, which is on the form
8090   ;; (article-number . line-number-in-body).
8091   (push
8092    (cons article
8093          (save-excursion
8094            (set-buffer gnus-article-buffer)
8095            (count-lines
8096             (min (point)
8097                  (save-excursion
8098                    (goto-char (point-min))
8099                    (search-forward "\n\n" nil t)
8100                    (point)))
8101             (point))))
8102    gnus-newsgroup-bookmarks)
8103   (gnus-message 6 "A bookmark has been added to the current article."))
8104
8105 (defun gnus-summary-remove-bookmark (article)
8106   "Remove the bookmark from the current article."
8107   (interactive (list (gnus-summary-article-number)))
8108   ;; Remove old bookmark, if one exists.
8109   (let ((old (assq article gnus-newsgroup-bookmarks)))
8110     (if old
8111         (progn
8112           (setq gnus-newsgroup-bookmarks
8113                 (delq old gnus-newsgroup-bookmarks))
8114           (gnus-message 6 "Removed bookmark."))
8115       (gnus-message 6 "No bookmark in current article."))))
8116
8117 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8118 (defun gnus-summary-mark-as-dormant (n)
8119   "Mark N articles forward as dormant.
8120 If N is negative, mark backward instead.  The difference between N and
8121 the actual number of articles marked is returned."
8122   (interactive "p")
8123   (gnus-summary-mark-forward n gnus-dormant-mark))
8124
8125 (defun gnus-summary-set-process-mark (article)
8126   "Set the process mark on ARTICLE and update the summary line."
8127   (setq gnus-newsgroup-processable
8128         (cons article
8129               (delq article gnus-newsgroup-processable)))
8130   (when (gnus-summary-goto-subject article)
8131     (gnus-summary-show-thread)
8132     (gnus-summary-goto-subject article)
8133     (gnus-summary-update-secondary-mark article)))
8134
8135 (defun gnus-summary-remove-process-mark (article)
8136   "Remove the process mark from ARTICLE and update the summary line."
8137   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8138   (when (gnus-summary-goto-subject article)
8139     (gnus-summary-show-thread)
8140     (gnus-summary-goto-subject article)
8141     (gnus-summary-update-secondary-mark article)))
8142
8143 (defun gnus-summary-set-saved-mark (article)
8144   "Set the process mark on ARTICLE and update the summary line."
8145   (push article gnus-newsgroup-saved)
8146   (when (gnus-summary-goto-subject article)
8147     (gnus-summary-update-secondary-mark article)))
8148
8149 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8150   "Mark N articles as read forwards.
8151 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8152 The difference between N and the actual number of articles marked is
8153 returned.
8154 Iff NO-EXPIRE, auto-expiry will be inhibited."
8155   (interactive "p")
8156   (gnus-summary-show-thread)
8157   (let ((backward (< n 0))
8158         (gnus-summary-goto-unread
8159          (and gnus-summary-goto-unread
8160               (not (eq gnus-summary-goto-unread 'never))
8161               (not (memq mark (list gnus-unread-mark
8162                                     gnus-ticked-mark gnus-dormant-mark)))))
8163         (n (abs n))
8164         (mark (or mark gnus-del-mark)))
8165     (while (and (> n 0)
8166                 (gnus-summary-mark-article nil mark no-expire)
8167                 (zerop (gnus-summary-next-subject
8168                         (if backward -1 1)
8169                         (and gnus-summary-goto-unread
8170                              (not (eq gnus-summary-goto-unread 'never)))
8171                         t)))
8172       (setq n (1- n)))
8173     (when (/= 0 n)
8174       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8175     (gnus-summary-recenter)
8176     (gnus-summary-position-point)
8177     (gnus-set-mode-line 'summary)
8178     n))
8179
8180 (defun gnus-summary-mark-article-as-read (mark)
8181   "Mark the current article quickly as read with MARK."
8182   (let ((article (gnus-summary-article-number)))
8183     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8184     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8185     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8186     (push (cons article mark) gnus-newsgroup-reads)
8187     ;; Possibly remove from cache, if that is used.
8188     (when gnus-use-cache
8189       (gnus-cache-enter-remove-article article))
8190     ;; Allow the backend to change the mark.
8191     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8192     ;; Check for auto-expiry.
8193     (when (and gnus-newsgroup-auto-expire
8194                (memq mark gnus-auto-expirable-marks))
8195       (setq mark gnus-expirable-mark)
8196       ;; Let the backend know about the mark change.
8197       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8198       (push article gnus-newsgroup-expirable))
8199     ;; Set the mark in the buffer.
8200     (gnus-summary-update-mark mark 'unread)
8201     t))
8202
8203 (defun gnus-summary-mark-article-as-unread (mark)
8204   "Mark the current article quickly as unread with MARK."
8205   (let* ((article (gnus-summary-article-number))
8206          (old-mark (gnus-summary-article-mark article)))
8207     ;; Allow the backend to change the mark.
8208     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8209     (if (eq mark old-mark)
8210         t
8211       (if (<= article 0)
8212           (progn
8213             (gnus-error 1 "Can't mark negative article numbers")
8214             nil)
8215         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8216         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8217         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8218         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8219         (cond ((= mark gnus-ticked-mark)
8220                (push article gnus-newsgroup-marked))
8221               ((= mark gnus-dormant-mark)
8222                (push article gnus-newsgroup-dormant))
8223               (t
8224                (push article gnus-newsgroup-unreads)))
8225         (gnus-pull article gnus-newsgroup-reads)
8226
8227         ;; See whether the article is to be put in the cache.
8228         (and gnus-use-cache
8229              (vectorp (gnus-summary-article-header article))
8230              (save-excursion
8231                (gnus-cache-possibly-enter-article
8232                 gnus-newsgroup-name article
8233                 (= mark gnus-ticked-mark)
8234                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8235
8236         ;; Fix the mark.
8237         (gnus-summary-update-mark mark 'unread)
8238         t))))
8239
8240 (defun gnus-summary-mark-article (&optional article mark no-expire)
8241   "Mark ARTICLE with MARK.  MARK can be any character.
8242 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8243 `??' (dormant) and `?E' (expirable).
8244 If MARK is nil, then the default character `?r' is used.
8245 If ARTICLE is nil, then the article on the current line will be
8246 marked.
8247 Iff NO-EXPIRE, auto-expiry will be inhibited."
8248   ;; The mark might be a string.
8249   (when (stringp mark)
8250     (setq mark (aref mark 0)))
8251   ;; If no mark is given, then we check auto-expiring.
8252   (when (null mark)
8253     (setq mark gnus-del-mark))
8254   (when (and (not no-expire)
8255              gnus-newsgroup-auto-expire
8256              (memq mark gnus-auto-expirable-marks))
8257     (setq mark gnus-expirable-mark))
8258   (let ((article (or article (gnus-summary-article-number)))
8259         (old-mark (gnus-summary-article-mark article)))
8260     ;; Allow the backend to change the mark.
8261     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8262     (if (eq mark old-mark)
8263         t
8264       (unless article
8265         (error "No article on current line"))
8266       (if (not (if (or (= mark gnus-unread-mark)
8267                        (= mark gnus-ticked-mark)
8268                        (= mark gnus-dormant-mark))
8269                    (gnus-mark-article-as-unread article mark)
8270                  (gnus-mark-article-as-read article mark)))
8271           t
8272         ;; See whether the article is to be put in the cache.
8273         (and gnus-use-cache
8274              (not (= mark gnus-canceled-mark))
8275              (vectorp (gnus-summary-article-header article))
8276              (save-excursion
8277                (gnus-cache-possibly-enter-article
8278                 gnus-newsgroup-name article
8279                 (= mark gnus-ticked-mark)
8280                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8281
8282         (when (gnus-summary-goto-subject article nil t)
8283           (let ((buffer-read-only nil))
8284             (gnus-summary-show-thread)
8285             ;; Fix the mark.
8286             (gnus-summary-update-mark mark 'unread)
8287             t))))))
8288
8289 (defun gnus-summary-update-secondary-mark (article)
8290   "Update the secondary (read, process, cache) mark."
8291   (gnus-summary-update-mark
8292    (cond ((memq article gnus-newsgroup-processable)
8293           gnus-process-mark)
8294          ((memq article gnus-newsgroup-cached)
8295           gnus-cached-mark)
8296          ((memq article gnus-newsgroup-replied)
8297           gnus-replied-mark)
8298          ((memq article gnus-newsgroup-saved)
8299           gnus-saved-mark)
8300          (t gnus-unread-mark))
8301    'replied)
8302   (when (gnus-visual-p 'summary-highlight 'highlight)
8303     (gnus-run-hooks 'gnus-summary-update-hook))
8304   t)
8305
8306 (defun gnus-summary-update-mark (mark type)
8307   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8308         (buffer-read-only nil))
8309     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8310     (when forward
8311       (when (looking-at "\r")
8312         (incf forward))
8313       (when (<= (+ forward (point)) (point-max))
8314         ;; Go to the right position on the line.
8315         (goto-char (+ forward (point)))
8316         ;; Replace the old mark with the new mark.
8317         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8318         ;; Optionally update the marks by some user rule.
8319         (when (eq type 'unread)
8320           (gnus-data-set-mark
8321            (gnus-data-find (gnus-summary-article-number)) mark)
8322           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8323
8324 (defun gnus-mark-article-as-read (article &optional mark)
8325   "Enter ARTICLE in the pertinent lists and remove it from others."
8326   ;; Make the article expirable.
8327   (let ((mark (or mark gnus-del-mark)))
8328     (if (= mark gnus-expirable-mark)
8329         (push article gnus-newsgroup-expirable)
8330       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8331     ;; Remove from unread and marked lists.
8332     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8333     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8334     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8335     (push (cons article mark) gnus-newsgroup-reads)
8336     ;; Possibly remove from cache, if that is used.
8337     (when gnus-use-cache
8338       (gnus-cache-enter-remove-article article))
8339     t))
8340
8341 (defun gnus-mark-article-as-unread (article &optional mark)
8342   "Enter ARTICLE in the pertinent lists and remove it from others."
8343   (let ((mark (or mark gnus-ticked-mark)))
8344     (if (<= article 0)
8345         (progn
8346           (gnus-error 1 "Can't mark negative article numbers")
8347           nil)
8348       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8349             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8350             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8351             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8352
8353       ;; Unsuppress duplicates?
8354       (when gnus-suppress-duplicates
8355         (gnus-dup-unsuppress-article article))
8356
8357       (cond ((= mark gnus-ticked-mark)
8358              (push article gnus-newsgroup-marked))
8359             ((= mark gnus-dormant-mark)
8360              (push article gnus-newsgroup-dormant))
8361             (t
8362              (push article gnus-newsgroup-unreads)))
8363       (gnus-pull article gnus-newsgroup-reads)
8364       t)))
8365
8366 (defalias 'gnus-summary-mark-as-unread-forward
8367   'gnus-summary-tick-article-forward)
8368 (make-obsolete 'gnus-summary-mark-as-unread-forward
8369                'gnus-summary-tick-article-forward)
8370 (defun gnus-summary-tick-article-forward (n)
8371   "Tick N articles forwards.
8372 If N is negative, tick backwards instead.
8373 The difference between N and the number of articles ticked is returned."
8374   (interactive "p")
8375   (gnus-summary-mark-forward n gnus-ticked-mark))
8376
8377 (defalias 'gnus-summary-mark-as-unread-backward
8378   'gnus-summary-tick-article-backward)
8379 (make-obsolete 'gnus-summary-mark-as-unread-backward
8380                'gnus-summary-tick-article-backward)
8381 (defun gnus-summary-tick-article-backward (n)
8382   "Tick N articles backwards.
8383 The difference between N and the number of articles ticked is returned."
8384   (interactive "p")
8385   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8386
8387 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8388 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8389 (defun gnus-summary-tick-article (&optional article clear-mark)
8390   "Mark current article as unread.
8391 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8392 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8393   (interactive)
8394   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8395                                        gnus-ticked-mark)))
8396
8397 (defun gnus-summary-mark-as-read-forward (n)
8398   "Mark N articles as read forwards.
8399 If N is negative, mark backwards instead.
8400 The difference between N and the actual number of articles marked is
8401 returned."
8402   (interactive "p")
8403   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8404
8405 (defun gnus-summary-mark-as-read-backward (n)
8406   "Mark the N articles as read backwards.
8407 The difference between N and the actual number of articles marked is
8408 returned."
8409   (interactive "p")
8410   (gnus-summary-mark-forward
8411    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8412
8413 (defun gnus-summary-mark-as-read (&optional article mark)
8414   "Mark current article as read.
8415 ARTICLE specifies the article to be marked as read.
8416 MARK specifies a string to be inserted at the beginning of the line."
8417   (gnus-summary-mark-article article mark))
8418
8419 (defun gnus-summary-clear-mark-forward (n)
8420   "Clear marks from N articles forward.
8421 If N is negative, clear backward instead.
8422 The difference between N and the number of marks cleared is returned."
8423   (interactive "p")
8424   (gnus-summary-mark-forward n gnus-unread-mark))
8425
8426 (defun gnus-summary-clear-mark-backward (n)
8427   "Clear marks from N articles backward.
8428 The difference between N and the number of marks cleared is returned."
8429   (interactive "p")
8430   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8431
8432 (defun gnus-summary-mark-unread-as-read ()
8433   "Intended to be used by `gnus-summary-mark-article-hook'."
8434   (when (memq gnus-current-article gnus-newsgroup-unreads)
8435     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8436
8437 (defun gnus-summary-mark-read-and-unread-as-read ()
8438   "Intended to be used by `gnus-summary-mark-article-hook'."
8439   (let ((mark (gnus-summary-article-mark)))
8440     (when (or (gnus-unread-mark-p mark)
8441               (gnus-read-mark-p mark))
8442       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8443
8444 (defun gnus-summary-mark-region-as-read (point mark all)
8445   "Mark all unread articles between point and mark as read.
8446 If given a prefix, mark all articles between point and mark as read,
8447 even ticked and dormant ones."
8448   (interactive "r\nP")
8449   (save-excursion
8450     (let (article)
8451       (goto-char point)
8452       (beginning-of-line)
8453       (while (and
8454               (< (point) mark)
8455               (progn
8456                 (when (or all
8457                           (memq (setq article (gnus-summary-article-number))
8458                                 gnus-newsgroup-unreads))
8459                   (gnus-summary-mark-article article gnus-del-mark))
8460                 t)
8461               (gnus-summary-find-next))))))
8462
8463 (defun gnus-summary-mark-below (score mark)
8464   "Mark articles with score less than SCORE with MARK."
8465   (interactive "P\ncMark: ")
8466   (setq score (if score
8467                   (prefix-numeric-value score)
8468                 (or gnus-summary-default-score 0)))
8469   (save-excursion
8470     (set-buffer gnus-summary-buffer)
8471     (goto-char (point-min))
8472     (while
8473         (progn
8474           (and (< (gnus-summary-article-score) score)
8475                (gnus-summary-mark-article nil mark))
8476           (gnus-summary-find-next)))))
8477
8478 (defun gnus-summary-kill-below (&optional score)
8479   "Mark articles with score below SCORE as read."
8480   (interactive "P")
8481   (gnus-summary-mark-below score gnus-killed-mark))
8482
8483 (defun gnus-summary-clear-above (&optional score)
8484   "Clear all marks from articles with score above SCORE."
8485   (interactive "P")
8486   (gnus-summary-mark-above score gnus-unread-mark))
8487
8488 (defun gnus-summary-tick-above (&optional score)
8489   "Tick all articles with score above SCORE."
8490   (interactive "P")
8491   (gnus-summary-mark-above score gnus-ticked-mark))
8492
8493 (defun gnus-summary-mark-above (score mark)
8494   "Mark articles with score over SCORE with MARK."
8495   (interactive "P\ncMark: ")
8496   (setq score (if score
8497                   (prefix-numeric-value score)
8498                 (or gnus-summary-default-score 0)))
8499   (save-excursion
8500     (set-buffer gnus-summary-buffer)
8501     (goto-char (point-min))
8502     (while (and (progn
8503                   (when (> (gnus-summary-article-score) score)
8504                     (gnus-summary-mark-article nil mark))
8505                   t)
8506                 (gnus-summary-find-next)))))
8507
8508 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8509 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8510 (defun gnus-summary-limit-include-expunged (&optional no-error)
8511   "Display all the hidden articles that were expunged for low scores."
8512   (interactive)
8513   (let ((buffer-read-only nil))
8514     (let ((scored gnus-newsgroup-scored)
8515           headers h)
8516       (while scored
8517         (unless (gnus-summary-goto-subject (caar scored))
8518           (and (setq h (gnus-summary-article-header (caar scored)))
8519                (< (cdar scored) gnus-summary-expunge-below)
8520                (push h headers)))
8521         (setq scored (cdr scored)))
8522       (if (not headers)
8523           (when (not no-error)
8524             (error "No expunged articles hidden"))
8525         (goto-char (point-min))
8526         (gnus-summary-prepare-unthreaded (nreverse headers))
8527         (goto-char (point-min))
8528         (gnus-summary-position-point)
8529         t))))
8530
8531 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8532   "Mark all unread articles in this newsgroup as read.
8533 If prefix argument ALL is non-nil, ticked and dormant articles will
8534 also be marked as read.
8535 If QUIETLY is non-nil, no questions will be asked.
8536 If TO-HERE is non-nil, it should be a point in the buffer.  All
8537 articles before this point will be marked as read.
8538 Note that this function will only catch up the unread article
8539 in the current summary buffer limitation.
8540 The number of articles marked as read is returned."
8541   (interactive "P")
8542   (prog1
8543       (save-excursion
8544         (when (or quietly
8545                   (not gnus-interactive-catchup) ;Without confirmation?
8546                   gnus-expert-user
8547                   (gnus-y-or-n-p
8548                    (if all
8549                        "Mark absolutely all articles as read? "
8550                      "Mark all unread articles as read? ")))
8551           (if (and not-mark
8552                    (not gnus-newsgroup-adaptive)
8553                    (not gnus-newsgroup-auto-expire)
8554                    (not gnus-suppress-duplicates)
8555                    (or (not gnus-use-cache)
8556                        (eq gnus-use-cache 'passive)))
8557               (progn
8558                 (when all
8559                   (setq gnus-newsgroup-marked nil
8560                         gnus-newsgroup-dormant nil))
8561                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8562             ;; We actually mark all articles as canceled, which we
8563             ;; have to do when using auto-expiry or adaptive scoring.
8564             (gnus-summary-show-all-threads)
8565             (when (gnus-summary-first-subject (not all) t)
8566               (while (and
8567                       (if to-here (< (point) to-here) t)
8568                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8569                       (gnus-summary-find-next (not all) nil nil t))))
8570             (gnus-set-mode-line 'summary))
8571           t))
8572     (gnus-summary-position-point)))
8573
8574 (defun gnus-summary-catchup-to-here (&optional all)
8575   "Mark all unticked articles before the current one as read.
8576 If ALL is non-nil, also mark ticked and dormant articles as read."
8577   (interactive "P")
8578   (save-excursion
8579     (gnus-save-hidden-threads
8580       (let ((beg (point)))
8581         ;; We check that there are unread articles.
8582         (when (or all (gnus-summary-find-prev))
8583           (gnus-summary-catchup all t beg)))))
8584   (gnus-summary-position-point))
8585
8586 (defun gnus-summary-catchup-all (&optional quietly)
8587   "Mark all articles in this newsgroup as read."
8588   (interactive "P")
8589   (gnus-summary-catchup t quietly))
8590
8591 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8592   "Mark all unread articles in this group as read, then exit.
8593 If prefix argument ALL is non-nil, all articles are marked as read."
8594   (interactive "P")
8595   (when (gnus-summary-catchup all quietly nil 'fast)
8596     ;; Select next newsgroup or exit.
8597     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8598              (eq gnus-auto-select-next 'quietly))
8599         (gnus-summary-next-group nil)
8600       (gnus-summary-exit))))
8601
8602 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8603   "Mark all articles in this newsgroup as read, and then exit."
8604   (interactive "P")
8605   (gnus-summary-catchup-and-exit t quietly))
8606
8607 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8608   "Mark all articles in this group as read and select the next group.
8609 If given a prefix, mark all articles, unread as well as ticked, as
8610 read."
8611   (interactive "P")
8612   (save-excursion
8613     (gnus-summary-catchup all))
8614   (gnus-summary-next-group))
8615
8616 ;;;
8617 ;;; with article
8618 ;;;
8619
8620 (defmacro gnus-with-article (article &rest forms)
8621   "Select ARTICLE and perform FORMS in the original article buffer.
8622 Then replace the article with the result."
8623   `(progn
8624      ;; We don't want the article to be marked as read.
8625      (let (gnus-mark-article-hook)
8626        (gnus-summary-select-article t t nil ,article))
8627      (set-buffer gnus-original-article-buffer)
8628      ,@forms
8629      (if (not (gnus-check-backend-function
8630                'request-replace-article (car gnus-article-current)))
8631          (gnus-message 5 "Read-only group; not replacing")
8632        (unless (gnus-request-replace-article
8633                 ,article (car gnus-article-current)
8634                 (current-buffer) t)
8635          (error "Couldn't replace article")))
8636      ;; The cache and backlog have to be flushed somewhat.
8637      (when gnus-keep-backlog
8638        (gnus-backlog-remove-article
8639         (car gnus-article-current) (cdr gnus-article-current)))
8640      (when gnus-use-cache
8641        (gnus-cache-update-article
8642         (car gnus-article-current) (cdr gnus-article-current)))))
8643
8644 (put 'gnus-with-article 'lisp-indent-function 1)
8645 (put 'gnus-with-article 'edebug-form-spec '(form body))
8646
8647 ;; Thread-based commands.
8648
8649 (defun gnus-summary-articles-in-thread (&optional article)
8650   "Return a list of all articles in the current thread.
8651 If ARTICLE is non-nil, return all articles in the thread that starts
8652 with that article."
8653   (let* ((article (or article (gnus-summary-article-number)))
8654          (data (gnus-data-find-list article))
8655          (top-level (gnus-data-level (car data)))
8656          (top-subject
8657           (cond ((null gnus-thread-operation-ignore-subject)
8658                  (gnus-simplify-subject-re
8659                   (mail-header-subject (gnus-data-header (car data)))))
8660                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8661                  (gnus-simplify-subject-fuzzy
8662                   (mail-header-subject (gnus-data-header (car data)))))
8663                 (t nil)))
8664          (end-point (save-excursion
8665                       (if (gnus-summary-go-to-next-thread)
8666                           (point) (point-max))))
8667          articles)
8668     (while (and data
8669                 (< (gnus-data-pos (car data)) end-point))
8670       (when (or (not top-subject)
8671                 (string= top-subject
8672                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8673                              (gnus-simplify-subject-fuzzy
8674                               (mail-header-subject
8675                                (gnus-data-header (car data))))
8676                            (gnus-simplify-subject-re
8677                             (mail-header-subject
8678                              (gnus-data-header (car data)))))))
8679         (push (gnus-data-number (car data)) articles))
8680       (unless (and (setq data (cdr data))
8681                    (> (gnus-data-level (car data)) top-level))
8682         (setq data nil)))
8683     ;; Return the list of articles.
8684     (nreverse articles)))
8685
8686 (defun gnus-summary-rethread-current ()
8687   "Rethread the thread the current article is part of."
8688   (interactive)
8689   (let* ((gnus-show-threads t)
8690          (article (gnus-summary-article-number))
8691          (id (mail-header-id (gnus-summary-article-header)))
8692          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8693     (unless id
8694       (error "No article on the current line"))
8695     (gnus-rebuild-thread id)
8696     (gnus-summary-goto-subject article)))
8697
8698 (defun gnus-summary-reparent-thread ()
8699   "Make the current article child of the marked (or previous) article.
8700
8701 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8702 is non-nil or the Subject: of both articles are the same."
8703   (interactive)
8704   (unless (not (gnus-group-read-only-p))
8705     (error "The current newsgroup does not support article editing"))
8706   (unless (<= (length gnus-newsgroup-processable) 1)
8707     (error "No more than one article may be marked"))
8708   (save-window-excursion
8709     (let ((gnus-article-buffer " *reparent*")
8710           (current-article (gnus-summary-article-number))
8711           ;; First grab the marked article, otherwise one line up.
8712           (parent-article (if (not (null gnus-newsgroup-processable))
8713                               (car gnus-newsgroup-processable)
8714                             (save-excursion
8715                               (if (eq (forward-line -1) 0)
8716                                   (gnus-summary-article-number)
8717                                 (error "Beginning of summary buffer"))))))
8718       (unless (not (eq current-article parent-article))
8719         (error "An article may not be self-referential"))
8720       (let ((message-id (mail-header-id
8721                          (gnus-summary-article-header parent-article))))
8722         (unless (and message-id (not (equal message-id "")))
8723           (error "No message-id in desired parent"))
8724         (gnus-with-article current-article
8725           (save-restriction
8726             (goto-char (point-min))
8727             (message-narrow-to-head)
8728             (if (re-search-forward "^References: " nil t)
8729                 (progn
8730                   (re-search-forward "^[^ \t]" nil t)
8731                   (forward-line -1)
8732                   (end-of-line)
8733                   (insert " " message-id))
8734               (insert "References: " message-id "\n"))))
8735         (set-buffer gnus-summary-buffer)
8736         (gnus-summary-unmark-all-processable)
8737         (gnus-summary-update-article current-article)
8738         (gnus-summary-rethread-current)
8739         (gnus-message 3 "Article %d is now the child of article %d"
8740                       current-article parent-article)))))
8741
8742 (defun gnus-summary-toggle-threads (&optional arg)
8743   "Toggle showing conversation threads.
8744 If ARG is positive number, turn showing conversation threads on."
8745   (interactive "P")
8746   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8747     (setq gnus-show-threads
8748           (if (null arg) (not gnus-show-threads)
8749             (> (prefix-numeric-value arg) 0)))
8750     (gnus-summary-prepare)
8751     (gnus-summary-goto-subject current)
8752     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8753     (gnus-summary-position-point)))
8754
8755 (defun gnus-summary-show-all-threads ()
8756   "Show all threads."
8757   (interactive)
8758   (save-excursion
8759     (let ((buffer-read-only nil))
8760       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8761   (gnus-summary-position-point))
8762
8763 (defun gnus-summary-show-thread ()
8764   "Show thread subtrees.
8765 Returns nil if no thread was there to be shown."
8766   (interactive)
8767   (let ((buffer-read-only nil)
8768         (orig (point))
8769         ;; first goto end then to beg, to have point at beg after let
8770         (end (progn (end-of-line) (point)))
8771         (beg (progn (beginning-of-line) (point))))
8772     (prog1
8773         ;; Any hidden lines here?
8774         (search-forward "\r" end t)
8775       (subst-char-in-region beg end ?\^M ?\n t)
8776       (goto-char orig)
8777       (gnus-summary-position-point))))
8778
8779 (defun gnus-summary-hide-all-threads ()
8780   "Hide all thread subtrees."
8781   (interactive)
8782   (save-excursion
8783     (goto-char (point-min))
8784     (gnus-summary-hide-thread)
8785     (while (zerop (gnus-summary-next-thread 1 t))
8786       (gnus-summary-hide-thread)))
8787   (gnus-summary-position-point))
8788
8789 (defun gnus-summary-hide-thread ()
8790   "Hide thread subtrees.
8791 Returns nil if no threads were there to be hidden."
8792   (interactive)
8793   (let ((buffer-read-only nil)
8794         (start (point))
8795         (article (gnus-summary-article-number)))
8796     (goto-char start)
8797     ;; Go forward until either the buffer ends or the subthread
8798     ;; ends.
8799     (when (and (not (eobp))
8800                (or (zerop (gnus-summary-next-thread 1 t))
8801                    (goto-char (point-max))))
8802       (prog1
8803           (if (and (> (point) start)
8804                    (search-backward "\n" start t))
8805               (progn
8806                 (subst-char-in-region start (point) ?\n ?\^M)
8807                 (gnus-summary-goto-subject article))
8808             (goto-char start)
8809             nil)))))
8810
8811 (defun gnus-summary-go-to-next-thread (&optional previous)
8812   "Go to the same level (or less) next thread.
8813 If PREVIOUS is non-nil, go to previous thread instead.
8814 Return the article number moved to, or nil if moving was impossible."
8815   (let ((level (gnus-summary-thread-level))
8816         (way (if previous -1 1))
8817         (beg (point)))
8818     (forward-line way)
8819     (while (and (not (eobp))
8820                 (< level (gnus-summary-thread-level)))
8821       (forward-line way))
8822     (if (eobp)
8823         (progn
8824           (goto-char beg)
8825           nil)
8826       (setq beg (point))
8827       (prog1
8828           (gnus-summary-article-number)
8829         (goto-char beg)))))
8830
8831 (defun gnus-summary-next-thread (n &optional silent)
8832   "Go to the same level next N'th thread.
8833 If N is negative, search backward instead.
8834 Returns the difference between N and the number of skips actually
8835 done.
8836
8837 If SILENT, don't output messages."
8838   (interactive "p")
8839   (let ((backward (< n 0))
8840         (n (abs n)))
8841     (while (and (> n 0)
8842                 (gnus-summary-go-to-next-thread backward))
8843       (decf n))
8844     (unless silent
8845       (gnus-summary-position-point))
8846     (when (and (not silent) (/= 0 n))
8847       (gnus-message 7 "No more threads"))
8848     n))
8849
8850 (defun gnus-summary-prev-thread (n)
8851   "Go to the same level previous N'th thread.
8852 Returns the difference between N and the number of skips actually
8853 done."
8854   (interactive "p")
8855   (gnus-summary-next-thread (- n)))
8856
8857 (defun gnus-summary-go-down-thread ()
8858   "Go down one level in the current thread."
8859   (let ((children (gnus-summary-article-children)))
8860     (when children
8861       (gnus-summary-goto-subject (car children)))))
8862
8863 (defun gnus-summary-go-up-thread ()
8864   "Go up one level in the current thread."
8865   (let ((parent (gnus-summary-article-parent)))
8866     (when parent
8867       (gnus-summary-goto-subject parent))))
8868
8869 (defun gnus-summary-down-thread (n)
8870   "Go down thread N steps.
8871 If N is negative, go up instead.
8872 Returns the difference between N and how many steps down that were
8873 taken."
8874   (interactive "p")
8875   (let ((up (< n 0))
8876         (n (abs n)))
8877     (while (and (> n 0)
8878                 (if up (gnus-summary-go-up-thread)
8879                   (gnus-summary-go-down-thread)))
8880       (setq n (1- n)))
8881     (gnus-summary-position-point)
8882     (when (/= 0 n)
8883       (gnus-message 7 "Can't go further"))
8884     n))
8885
8886 (defun gnus-summary-up-thread (n)
8887   "Go up thread N steps.
8888 If N is negative, go up instead.
8889 Returns the difference between N and how many steps down that were
8890 taken."
8891   (interactive "p")
8892   (gnus-summary-down-thread (- n)))
8893
8894 (defun gnus-summary-top-thread ()
8895   "Go to the top of the thread."
8896   (interactive)
8897   (while (gnus-summary-go-up-thread))
8898   (gnus-summary-article-number))
8899
8900 (defun gnus-summary-kill-thread (&optional unmark)
8901   "Mark articles under current thread as read.
8902 If the prefix argument is positive, remove any kinds of marks.
8903 If the prefix argument is negative, tick articles instead."
8904   (interactive "P")
8905   (when unmark
8906     (setq unmark (prefix-numeric-value unmark)))
8907   (let ((articles (gnus-summary-articles-in-thread)))
8908     (save-excursion
8909       ;; Expand the thread.
8910       (gnus-summary-show-thread)
8911       ;; Mark all the articles.
8912       (while articles
8913         (gnus-summary-goto-subject (car articles))
8914         (cond ((null unmark)
8915                (gnus-summary-mark-article-as-read gnus-killed-mark))
8916               ((> unmark 0)
8917                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8918               (t
8919                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8920         (setq articles (cdr articles))))
8921     ;; Hide killed subtrees.
8922     (and (null unmark)
8923          gnus-thread-hide-killed
8924          (gnus-summary-hide-thread))
8925     ;; If marked as read, go to next unread subject.
8926     (when (null unmark)
8927       ;; Go to next unread subject.
8928       (gnus-summary-next-subject 1 t)))
8929   (gnus-set-mode-line 'summary))
8930
8931 ;; Summary sorting commands
8932
8933 (defun gnus-summary-sort-by-number (&optional reverse)
8934   "Sort the summary buffer by article number.
8935 Argument REVERSE means reverse order."
8936   (interactive "P")
8937   (gnus-summary-sort 'number reverse))
8938
8939 (defun gnus-summary-sort-by-author (&optional reverse)
8940   "Sort the summary buffer by author name alphabetically.
8941 If `case-fold-search' is non-nil, case of letters is ignored.
8942 Argument REVERSE means reverse order."
8943   (interactive "P")
8944   (gnus-summary-sort 'author reverse))
8945
8946 (defun gnus-summary-sort-by-subject (&optional reverse)
8947   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8948 If `case-fold-search' is non-nil, case of letters is ignored.
8949 Argument REVERSE means reverse order."
8950   (interactive "P")
8951   (gnus-summary-sort 'subject reverse))
8952
8953 (defun gnus-summary-sort-by-date (&optional reverse)
8954   "Sort the summary buffer by date.
8955 Argument REVERSE means reverse order."
8956   (interactive "P")
8957   (gnus-summary-sort 'date reverse))
8958
8959 (defun gnus-summary-sort-by-score (&optional reverse)
8960   "Sort the summary buffer by score.
8961 Argument REVERSE means reverse order."
8962   (interactive "P")
8963   (gnus-summary-sort 'score reverse))
8964
8965 (defun gnus-summary-sort-by-lines (&optional reverse)
8966   "Sort the summary buffer by the number of lines.
8967 Argument REVERSE means reverse order."
8968   (interactive "P")
8969   (gnus-summary-sort 'lines reverse))
8970
8971 (defun gnus-summary-sort-by-chars (&optional reverse)
8972   "Sort the summary buffer by article length.
8973 Argument REVERSE means reverse order."
8974   (interactive "P")
8975   (gnus-summary-sort 'chars reverse))
8976
8977 (defun gnus-summary-sort (predicate reverse)
8978   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8979   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8980          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8981          (gnus-thread-sort-functions
8982           (if (not reverse)
8983               thread
8984             `(lambda (t1 t2)
8985                (,thread t2 t1))))
8986          (gnus-sort-gathered-threads-function
8987           gnus-thread-sort-functions)
8988          (gnus-article-sort-functions
8989           (if (not reverse)
8990               article
8991             `(lambda (t1 t2)
8992                (,article t2 t1))))
8993          (buffer-read-only)
8994          (gnus-summary-prepare-hook nil))
8995     ;; We do the sorting by regenerating the threads.
8996     (gnus-summary-prepare)
8997     ;; Hide subthreads if needed.
8998     (when (and gnus-show-threads gnus-thread-hide-subtree)
8999       (gnus-summary-hide-all-threads))))
9000
9001 ;; Summary saving commands.
9002
9003 (defun gnus-summary-save-article (&optional n not-saved)
9004   "Save the current article using the default saver function.
9005 If N is a positive number, save the N next articles.
9006 If N is a negative number, save the N previous articles.
9007 If N is nil and any articles have been marked with the process mark,
9008 save those articles instead.
9009 The variable `gnus-default-article-saver' specifies the saver function."
9010   (interactive "P")
9011   (let* ((articles (gnus-summary-work-articles n))
9012          (save-buffer (save-excursion
9013                         (nnheader-set-temp-buffer " *Gnus Save*")))
9014          (num (length articles))
9015          header file)
9016     (dolist (article articles)
9017       (setq header (gnus-summary-article-header article))
9018       (if (not (vectorp header))
9019           ;; This is a pseudo-article.
9020           (if (assq 'name header)
9021               (gnus-copy-file (cdr (assq 'name header)))
9022             (gnus-message 1 "Article %d is unsaveable" article))
9023         ;; This is a real article.
9024         (save-window-excursion
9025           (gnus-summary-select-article t nil nil article))
9026         (save-excursion
9027           (set-buffer save-buffer)
9028           (erase-buffer)
9029           (insert-buffer-substring gnus-original-article-buffer))
9030         (setq file (gnus-article-save save-buffer file num))
9031         (gnus-summary-remove-process-mark article)
9032         (unless not-saved
9033           (gnus-summary-set-saved-mark article))))
9034     (gnus-kill-buffer save-buffer)
9035     (gnus-summary-position-point)
9036     (gnus-set-mode-line 'summary)
9037     n))
9038
9039 (defun gnus-summary-pipe-output (&optional arg)
9040   "Pipe the current article to a subprocess.
9041 If N is a positive number, pipe the N next articles.
9042 If N is a negative number, pipe the N previous articles.
9043 If N is nil and any articles have been marked with the process mark,
9044 pipe those articles instead."
9045   (interactive "P")
9046   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9047     (gnus-summary-save-article arg t))
9048   (gnus-configure-windows 'pipe))
9049
9050 (defun gnus-summary-save-article-mail (&optional arg)
9051   "Append the current article to an mail file.
9052 If N is a positive number, save the N next articles.
9053 If N is a negative number, save the N previous articles.
9054 If N is nil and any articles have been marked with the process mark,
9055 save those articles instead."
9056   (interactive "P")
9057   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9058     (gnus-summary-save-article arg)))
9059
9060 (defun gnus-summary-save-article-rmail (&optional arg)
9061   "Append the current article to an rmail file.
9062 If N is a positive number, save the N next articles.
9063 If N is a negative number, save the N previous articles.
9064 If N is nil and any articles have been marked with the process mark,
9065 save those articles instead."
9066   (interactive "P")
9067   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9068     (gnus-summary-save-article arg)))
9069
9070 (defun gnus-summary-save-article-file (&optional arg)
9071   "Append the current article to a file.
9072 If N is a positive number, save the N next articles.
9073 If N is a negative number, save the N previous articles.
9074 If N is nil and any articles have been marked with the process mark,
9075 save those articles instead."
9076   (interactive "P")
9077   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9078     (gnus-summary-save-article arg)))
9079
9080 (defun gnus-summary-write-article-file (&optional arg)
9081   "Write the current article to a file, deleting the previous file.
9082 If N is a positive number, save the N next articles.
9083 If N is a negative number, save the N previous articles.
9084 If N is nil and any articles have been marked with the process mark,
9085 save those articles instead."
9086   (interactive "P")
9087   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9088     (gnus-summary-save-article arg)))
9089
9090 (defun gnus-summary-save-article-body-file (&optional arg)
9091   "Append the current article body to a file.
9092 If N is a positive number, save the N next articles.
9093 If N is a negative number, save the N previous articles.
9094 If N is nil and any articles have been marked with the process mark,
9095 save those articles instead."
9096   (interactive "P")
9097   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9098     (gnus-summary-save-article arg)))
9099
9100 (defun gnus-summary-pipe-message (program)
9101   "Pipe the current article through PROGRAM."
9102   (interactive "sProgram: ")
9103   (gnus-summary-select-article)
9104   (let ((mail-header-separator ""))
9105     (gnus-eval-in-buffer-window gnus-article-buffer
9106       (save-restriction
9107         (widen)
9108         (let ((start (window-start))
9109               buffer-read-only)
9110           (message-pipe-buffer-body program)
9111           (set-window-start (get-buffer-window (current-buffer)) start))))))
9112
9113 (defun gnus-get-split-value (methods)
9114   "Return a value based on the split METHODS."
9115   (let (split-name method result match)
9116     (when methods
9117       (save-excursion
9118         (set-buffer gnus-original-article-buffer)
9119         (save-restriction
9120           (nnheader-narrow-to-headers)
9121           (while methods
9122             (goto-char (point-min))
9123             (setq method (pop methods))
9124             (setq match (car method))
9125             (when (cond
9126                    ((stringp match)
9127                     ;; Regular expression.
9128                     (ignore-errors
9129                       (re-search-forward match nil t)))
9130                    ((gnus-functionp match)
9131                     ;; Function.
9132                     (save-restriction
9133                       (widen)
9134                       (setq result (funcall match gnus-newsgroup-name))))
9135                    ((consp match)
9136                     ;; Form.
9137                     (save-restriction
9138                       (widen)
9139                       (setq result (eval match)))))
9140               (setq split-name (append (cdr method) split-name))
9141               (cond ((stringp result)
9142                      (push (expand-file-name
9143                             result gnus-article-save-directory)
9144                            split-name))
9145                     ((consp result)
9146                      (setq split-name (append result split-name)))))))))
9147     (nreverse split-name)))
9148
9149 (defun gnus-valid-move-group-p (group)
9150   (and (boundp group)
9151        (symbol-name group)
9152        (symbol-value group)
9153        (gnus-get-function (gnus-find-method-for-group
9154                            (symbol-name group)) 'request-accept-article t)))
9155
9156 (defun gnus-read-move-group-name (prompt default articles prefix)
9157   "Read a group name."
9158   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9159          (minibuffer-confirm-incomplete nil) ; XEmacs
9160          (prom
9161           (format "%s %s to:"
9162                   prompt
9163                   (if (> (length articles) 1)
9164                       (format "these %d articles" (length articles))
9165                     "this article")))
9166          (to-newsgroup
9167           (cond
9168            ((null split-name)
9169             (gnus-completing-read default prom
9170                                   gnus-active-hashtb
9171                                   'gnus-valid-move-group-p
9172                                   nil prefix
9173                                   'gnus-group-history))
9174            ((= 1 (length split-name))
9175             (gnus-completing-read (car split-name) prom
9176                                   gnus-active-hashtb
9177                                   'gnus-valid-move-group-p
9178                                   nil nil
9179                                   'gnus-group-history))
9180            (t
9181             (gnus-completing-read nil prom
9182                                   (mapcar (lambda (el) (list el))
9183                                           (nreverse split-name))
9184                                   nil nil nil
9185                                   'gnus-group-history))))
9186          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9187     (when to-newsgroup
9188       (if (or (string= to-newsgroup "")
9189               (string= to-newsgroup prefix))
9190           (setq to-newsgroup default))
9191       (unless to-newsgroup
9192         (error "No group name entered"))
9193       (or (gnus-active to-newsgroup)
9194           (gnus-activate-group to-newsgroup nil nil to-method)
9195           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9196                                      to-newsgroup))
9197               (or (and (gnus-request-create-group to-newsgroup to-method)
9198                        (gnus-activate-group
9199                         to-newsgroup nil nil to-method)
9200                        (gnus-subscribe-group to-newsgroup))
9201                   (error "Couldn't create group %s" to-newsgroup)))
9202           (error "No such group: %s" to-newsgroup)))
9203     to-newsgroup))
9204
9205 (defun gnus-summary-save-parts (type dir n &optional reverse)
9206   "Save parts matching TYPE to DIR.
9207 If REVERSE, save parts that do not match TYPE."
9208   (interactive
9209    (list (read-string "Save parts of type: " 
9210                       (or (car gnus-summary-save-parts-type-history)
9211                           gnus-summary-save-parts-default-mime)
9212                       'gnus-summary-save-parts-type-history)
9213          (setq gnus-summary-save-parts-last-directory
9214                (read-file-name "Save to directory: " 
9215                                gnus-summary-save-parts-last-directory
9216                                nil t))
9217          current-prefix-arg))
9218   (gnus-summary-iterate n
9219     (let ((gnus-display-mime-function nil)
9220           (gnus-inhibit-treatment t))
9221       (gnus-summary-select-article))
9222     (save-excursion
9223       (set-buffer gnus-article-buffer)
9224       (let ((handles (or gnus-article-mime-handles
9225                          (mm-dissect-buffer) (mm-uu-dissect))))
9226         (when handles
9227           (gnus-summary-save-parts-1 type dir handles reverse)
9228           (unless gnus-article-mime-handles ;; Don't destroy this case.
9229             (mm-destroy-parts handles)))))))
9230
9231 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9232   (if (stringp (car handle))
9233       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9234               (cdr handle))
9235     (when (if reverse
9236               (not (string-match type (mm-handle-media-type handle)))
9237             (string-match type (mm-handle-media-type handle)))
9238       (let ((file (expand-file-name
9239                    (file-name-nondirectory
9240                     (or
9241                      (mail-content-type-get
9242                       (mm-handle-disposition handle) 'filename)
9243                      (concat gnus-newsgroup-name
9244                              "." (number-to-string
9245                                   (cdr gnus-article-current)))))
9246                    dir)))
9247         (unless (file-exists-p file)
9248           (mm-save-part-to-file handle file))))))
9249
9250 ;; Summary extract commands
9251
9252 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9253   (let ((buffer-read-only nil)
9254         (article (gnus-summary-article-number))
9255         after-article b e)
9256     (unless (gnus-summary-goto-subject article)
9257       (error "No such article: %d" article))
9258     (gnus-summary-position-point)
9259     ;; If all commands are to be bunched up on one line, we collect
9260     ;; them here.
9261     (unless gnus-view-pseudos-separately
9262       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9263             files action)
9264         (while ps
9265           (setq action (cdr (assq 'action (car ps))))
9266           (setq files (list (cdr (assq 'name (car ps)))))
9267           (while (and ps (cdr ps)
9268                       (string= (or action "1")
9269                                (or (cdr (assq 'action (cadr ps))) "2")))
9270             (push (cdr (assq 'name (cadr ps))) files)
9271             (setcdr ps (cddr ps)))
9272           (when files
9273             (when (not (string-match "%s" action))
9274               (push " " files))
9275             (push " " files)
9276             (when (assq 'execute (car ps))
9277               (setcdr (assq 'execute (car ps))
9278                       (funcall (if (string-match "%s" action)
9279                                    'format 'concat)
9280                                action
9281                                (mapconcat
9282                                 (lambda (f)
9283                                   (if (equal f " ")
9284                                       f
9285                                     (mm-quote-arg f)))
9286                                 files " ")))))
9287           (setq ps (cdr ps)))))
9288     (if (and gnus-view-pseudos (not not-view))
9289         (while pslist
9290           (when (assq 'execute (car pslist))
9291             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9292                                   (eq gnus-view-pseudos 'not-confirm)))
9293           (setq pslist (cdr pslist)))
9294       (save-excursion
9295         (while pslist
9296           (setq after-article (or (cdr (assq 'article (car pslist)))
9297                                   (gnus-summary-article-number)))
9298           (gnus-summary-goto-subject after-article)
9299           (forward-line 1)
9300           (setq b (point))
9301           (insert "    " (file-name-nondirectory
9302                           (cdr (assq 'name (car pslist))))
9303                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9304           (setq e (point))
9305           (forward-line -1)             ; back to `b'
9306           (gnus-add-text-properties
9307            b (1- e) (list 'gnus-number gnus-reffed-article-number
9308                           gnus-mouse-face-prop gnus-mouse-face))
9309           (gnus-data-enter
9310            after-article gnus-reffed-article-number
9311            gnus-unread-mark b (car pslist) 0 (- e b))
9312           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9313           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9314           (setq pslist (cdr pslist)))))))
9315
9316 (defun gnus-pseudos< (p1 p2)
9317   (let ((c1 (cdr (assq 'action p1)))
9318         (c2 (cdr (assq 'action p2))))
9319     (and c1 c2 (string< c1 c2))))
9320
9321 (defun gnus-request-pseudo-article (props)
9322   (cond ((assq 'execute props)
9323          (gnus-execute-command (cdr (assq 'execute props)))))
9324   (let ((gnus-current-article (gnus-summary-article-number)))
9325     (gnus-run-hooks 'gnus-mark-article-hook)))
9326
9327 (defun gnus-execute-command (command &optional automatic)
9328   (save-excursion
9329     (gnus-article-setup-buffer)
9330     (set-buffer gnus-article-buffer)
9331     (setq buffer-read-only nil)
9332     (let ((command (if automatic command
9333                      (read-string "Command: " (cons command 0)))))
9334       (erase-buffer)
9335       (insert "$ " command "\n\n")
9336       (if gnus-view-pseudo-asynchronously
9337           (start-process "gnus-execute" (current-buffer) shell-file-name
9338                          shell-command-switch command)
9339         (call-process shell-file-name nil t nil
9340                       shell-command-switch command)))))
9341
9342 ;; Summary kill commands.
9343
9344 (defun gnus-summary-edit-global-kill (article)
9345   "Edit the \"global\" kill file."
9346   (interactive (list (gnus-summary-article-number)))
9347   (gnus-group-edit-global-kill article))
9348
9349 (defun gnus-summary-edit-local-kill ()
9350   "Edit a local kill file applied to the current newsgroup."
9351   (interactive)
9352   (setq gnus-current-headers (gnus-summary-article-header))
9353   (gnus-group-edit-local-kill
9354    (gnus-summary-article-number) gnus-newsgroup-name))
9355
9356 ;;; Header reading.
9357
9358 (defun gnus-read-header (id &optional header)
9359   "Read the headers of article ID and enter them into the Gnus system."
9360   (let ((group gnus-newsgroup-name)
9361         (gnus-override-method
9362          (or
9363           gnus-override-method
9364           (and (gnus-news-group-p gnus-newsgroup-name)
9365                (car (gnus-refer-article-methods)))))
9366         where)
9367     ;; First we check to see whether the header in question is already
9368     ;; fetched.
9369     (if (stringp id)
9370         ;; This is a Message-ID.
9371         (setq header (or header (gnus-id-to-header id)))
9372       ;; This is an article number.
9373       (setq header (or header (gnus-summary-article-header id))))
9374     (if (and header
9375              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9376         ;; We have found the header.
9377         header
9378       ;; If this is a sparse article, we have to nix out its
9379       ;; previous entry in the thread hashtb.
9380       (when (and header
9381                  (gnus-summary-article-sparse-p (mail-header-number header)))
9382         (let* ((parent (gnus-parent-id (mail-header-references header)))
9383                (thread (and parent (gnus-id-to-thread parent))))
9384           (when thread
9385             (delq (assq header thread) thread))))
9386       ;; We have to really fetch the header to this article.
9387       (save-excursion
9388         (set-buffer nntp-server-buffer)
9389         (when (setq where (gnus-request-head id group))
9390           (nnheader-fold-continuation-lines)
9391           (goto-char (point-max))
9392           (insert ".\n")
9393           (goto-char (point-min))
9394           (insert "211 ")
9395           (princ (cond
9396                   ((numberp id) id)
9397                   ((cdr where) (cdr where))
9398                   (header (mail-header-number header))
9399                   (t gnus-reffed-article-number))
9400                  (current-buffer))
9401           (insert " Article retrieved.\n"))
9402         (if (or (not where)
9403                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9404             ()                          ; Malformed head.
9405           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9406             (when (and (stringp id)
9407                        (not (string= (gnus-group-real-name group)
9408                                      (car where))))
9409               ;; If we fetched by Message-ID and the article came
9410               ;; from a different group, we fudge some bogus article
9411               ;; numbers for this article.
9412               (mail-header-set-number header gnus-reffed-article-number))
9413             (save-excursion
9414               (set-buffer gnus-summary-buffer)
9415               (decf gnus-reffed-article-number)
9416               (gnus-remove-header (mail-header-number header))
9417               (push header gnus-newsgroup-headers)
9418               (setq gnus-current-headers header)
9419               (push (mail-header-number header) gnus-newsgroup-limit)))
9420           header)))))
9421
9422 (defun gnus-remove-header (number)
9423   "Remove header NUMBER from `gnus-newsgroup-headers'."
9424   (if (and gnus-newsgroup-headers
9425            (= number (mail-header-number (car gnus-newsgroup-headers))))
9426       (pop gnus-newsgroup-headers)
9427     (let ((headers gnus-newsgroup-headers))
9428       (while (and (cdr headers)
9429                   (not (= number (mail-header-number (cadr headers)))))
9430         (pop headers))
9431       (when (cdr headers)
9432         (setcdr headers (cddr headers))))))
9433
9434 ;;;
9435 ;;; summary highlights
9436 ;;;
9437
9438 (defun gnus-highlight-selected-summary ()
9439   "Highlight selected article in summary buffer."
9440   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9441   (when gnus-summary-selected-face
9442     (save-excursion
9443       (let* ((beg (progn (beginning-of-line) (point)))
9444              (end (progn (end-of-line) (point)))
9445              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9446              (from (if (get-text-property beg gnus-mouse-face-prop)
9447                        beg
9448                      (or (next-single-property-change
9449                           beg gnus-mouse-face-prop nil end)
9450                          beg)))
9451              (to
9452               (if (= from end)
9453                   (- from 2)
9454                 (or (next-single-property-change
9455                      from gnus-mouse-face-prop nil end)
9456                     end))))
9457         ;; If no mouse-face prop on line we will have to = from = end,
9458         ;; so we highlight the entire line instead.
9459         (when (= (+ to 2) from)
9460           (setq from beg)
9461           (setq to end))
9462         (if gnus-newsgroup-selected-overlay
9463             ;; Move old overlay.
9464             (gnus-move-overlay
9465              gnus-newsgroup-selected-overlay from to (current-buffer))
9466           ;; Create new overlay.
9467           (gnus-overlay-put
9468            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9469            'face gnus-summary-selected-face))))))
9470
9471 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9472 (defun gnus-summary-highlight-line ()
9473   "Highlight current line according to `gnus-summary-highlight'."
9474   (let* ((list gnus-summary-highlight)
9475          (p (point))
9476          (end (progn (end-of-line) (point)))
9477          ;; now find out where the line starts and leave point there.
9478          (beg (progn (beginning-of-line) (point)))
9479          (article (gnus-summary-article-number))
9480          (score (or (cdr (assq (or article gnus-current-article)
9481                                gnus-newsgroup-scored))
9482                     gnus-summary-default-score 0))
9483          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9484          (inhibit-read-only t))
9485     ;; Eval the cars of the lists until we find a match.
9486     (let ((default gnus-summary-default-score))
9487       (while (and list
9488                   (not (eval (caar list))))
9489         (setq list (cdr list))))
9490     (let ((face (cdar list)))
9491       (unless (eq face (get-text-property beg 'face))
9492         (gnus-put-text-property-excluding-characters-with-faces
9493          beg end 'face
9494          (setq face (if (boundp face) (symbol-value face) face)))
9495         (when gnus-summary-highlight-line-function
9496           (funcall gnus-summary-highlight-line-function article face))))
9497     (goto-char p)))
9498
9499 (defun gnus-update-read-articles (group unread &optional compute)
9500   "Update the list of read articles in GROUP."
9501   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9502          (entry (gnus-gethash group gnus-newsrc-hashtb))
9503          (info (nth 2 entry))
9504          (prev 1)
9505          (unread (sort (copy-sequence unread) '<))
9506          read)
9507     (if (or (not info) (not active))
9508         ;; There is no info on this group if it was, in fact,
9509         ;; killed.  Gnus stores no information on killed groups, so
9510         ;; there's nothing to be done.
9511         ;; One could store the information somewhere temporarily,
9512         ;; perhaps...  Hmmm...
9513         ()
9514       ;; Remove any negative articles numbers.
9515       (while (and unread (< (car unread) 0))
9516         (setq unread (cdr unread)))
9517       ;; Remove any expired article numbers
9518       (while (and unread (< (car unread) (car active)))
9519         (setq unread (cdr unread)))
9520       ;; Compute the ranges of read articles by looking at the list of
9521       ;; unread articles.
9522       (while unread
9523         (when (/= (car unread) prev)
9524           (push (if (= prev (1- (car unread))) prev
9525                   (cons prev (1- (car unread))))
9526                 read))
9527         (setq prev (1+ (car unread)))
9528         (setq unread (cdr unread)))
9529       (when (<= prev (cdr active))
9530         (push (cons prev (cdr active)) read))
9531       (setq read (if (> (length read) 1) (nreverse read) read))
9532       (if compute
9533           read
9534         (save-excursion
9535           (let (setmarkundo)
9536             ;; Propagate the read marks to the backend.
9537             (when (gnus-check-backend-function 'request-set-mark group)
9538               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9539                     (add (gnus-remove-from-range read (gnus-info-read info))))
9540                 (when (or add del)
9541                   (unless (gnus-check-group group)
9542                     (error "Can't open server for %s" group))
9543                   (gnus-request-set-mark
9544                    group (delq nil (list (if add (list add 'add '(read)))
9545                                          (if del (list del 'del '(read))))))
9546                   (setq setmarkundo
9547                         `(gnus-request-set-mark
9548                           ,group
9549                           ',(delq nil (list
9550                                        (if del (list del 'add '(read)))
9551                                        (if add (list add 'del '(read))))))))))
9552             (set-buffer gnus-group-buffer)
9553             (gnus-undo-register
9554               `(progn
9555                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9556                  (gnus-info-set-read ',info ',(gnus-info-read info))
9557                  (gnus-get-unread-articles-in-group ',info 
9558                                                     (gnus-active ,group))
9559                  (gnus-group-update-group ,group t)
9560                  ,setmarkundo))))
9561         ;; Enter this list into the group info.
9562         (gnus-info-set-read info read)
9563         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9564         (gnus-get-unread-articles-in-group info (gnus-active group))
9565         t))))
9566
9567 (defun gnus-offer-save-summaries ()
9568   "Offer to save all active summary buffers."
9569   (save-excursion
9570     (let ((buflist (buffer-list))
9571           buffers bufname)
9572       ;; Go through all buffers and find all summaries.
9573       (while buflist
9574         (and (setq bufname (buffer-name (car buflist)))
9575              (string-match "Summary" bufname)
9576              (save-excursion
9577                (set-buffer bufname)
9578                ;; We check that this is, indeed, a summary buffer.
9579                (and (eq major-mode 'gnus-summary-mode)
9580                     ;; Also make sure this isn't bogus.
9581                     gnus-newsgroup-prepared
9582                     ;; Also make sure that this isn't a dead summary buffer.
9583                     (not gnus-dead-summary-mode)))
9584              (push bufname buffers))
9585         (setq buflist (cdr buflist)))
9586       ;; Go through all these summary buffers and offer to save them.
9587       (when buffers
9588         (map-y-or-n-p
9589          "Update summary buffer %s? "
9590          (lambda (buf)
9591            (switch-to-buffer buf)
9592            (gnus-summary-exit))
9593          buffers)))))
9594
9595 (defun gnus-summary-setup-default-charset ()
9596   "Setup newsgroup default charset."
9597   (if (equal gnus-newsgroup-name "nndraft:drafts")
9598       (setq gnus-newsgroup-charset nil)
9599     (let* ((name (and gnus-newsgroup-name
9600                       (gnus-group-real-name gnus-newsgroup-name)))
9601            (ignored-charsets
9602             (or gnus-newsgroup-ephemeral-ignored-charsets
9603                 (append
9604                  (and gnus-newsgroup-name
9605                       (or (gnus-group-find-parameter gnus-newsgroup-name
9606                                                      'ignored-charsets t)
9607                           (let ((alist gnus-group-ignored-charsets-alist)
9608                                 elem (charsets nil))
9609                             (while (setq elem (pop alist))
9610                               (when (and name
9611                                          (string-match (car elem) name))
9612                                 (setq alist nil
9613                                       charsets (cdr elem))))
9614                             charsets)))
9615                  gnus-newsgroup-ignored-charsets))))
9616       (setq gnus-newsgroup-charset
9617             (or gnus-newsgroup-ephemeral-charset
9618                 (and gnus-newsgroup-name
9619                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9620                          (let ((alist gnus-group-charset-alist)
9621                                elem charset)
9622                            (while (setq elem (pop alist))
9623                              (when (and name
9624                                         (string-match (car elem) name))
9625                                (setq alist nil
9626                                      charset (cadr elem))))
9627                            charset)))
9628                 gnus-default-charset))
9629       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9630            ignored-charsets))))
9631
9632 ;;;
9633 ;;; Mime Commands
9634 ;;;
9635
9636 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9637   "Display the current article buffer fully MIME-buttonized.
9638 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9639 treated as multipart/mixed."
9640   (interactive "P")
9641   (require 'gnus-art)
9642   (let ((gnus-unbuttonized-mime-types nil)
9643         (gnus-mime-display-multipart-as-mixed show-all-parts))
9644     (gnus-summary-show-article)))
9645
9646 (defun gnus-summary-repair-multipart (article)
9647   "Add a Content-Type header to a multipart article without one."
9648   (interactive (list (gnus-summary-article-number)))
9649   (gnus-with-article article
9650     (message-narrow-to-head)
9651     (goto-char (point-max))
9652     (widen)
9653     (when (search-forward "\n--" nil t)
9654       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9655         (message-narrow-to-head)
9656         (message-remove-header "Mime-Version")
9657         (message-remove-header "Content-Type")
9658         (goto-char (point-max))
9659         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9660                         separator))
9661         (insert "Mime-Version: 1.0\n")
9662         (widen))))
9663   (let (gnus-mark-article-hook)
9664     (gnus-summary-select-article t t nil article)))
9665
9666 (defun gnus-summary-toggle-display-buttonized ()
9667   "Toggle the buttonizing of the article buffer."
9668   (interactive)
9669   (require 'gnus-art)
9670   (if (setq gnus-inhibit-mime-unbuttonizing
9671             (not gnus-inhibit-mime-unbuttonizing))
9672       (let ((gnus-unbuttonized-mime-types nil))
9673         (gnus-summary-show-article))
9674     (gnus-summary-show-article)))
9675
9676 ;;;
9677 ;;; Generic summary marking commands
9678 ;;;
9679
9680 (defvar gnus-summary-marking-alist
9681   '((read gnus-del-mark "d")
9682     (unread gnus-unread-mark "u")
9683     (ticked gnus-ticked-mark "!")
9684     (dormant gnus-dormant-mark "?")
9685     (expirable gnus-expirable-mark "e"))
9686   "An alist of names/marks/keystrokes.")
9687
9688 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9689 (defvar gnus-summary-mark-map)
9690
9691 (defun gnus-summary-make-all-marking-commands ()
9692   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9693   (dolist (elem gnus-summary-marking-alist)
9694     (apply 'gnus-summary-make-marking-command elem)))
9695
9696 (defun gnus-summary-make-marking-command (name mark keystroke)
9697   (let ((map (make-sparse-keymap)))
9698     (define-key gnus-summary-generic-mark-map keystroke map)
9699     (dolist (lway `((next "next" next nil "n")
9700                     (next-unread "next unread" next t "N")
9701                     (prev "previous" prev nil "p")
9702                     (prev-unread "previous unread" prev t "P")
9703                     (nomove "" nil nil ,keystroke)))
9704       (let ((func (gnus-summary-make-marking-command-1
9705                    mark (car lway) lway name)))
9706         (setq func (eval func))
9707         (define-key map (nth 4 lway) func)))))
9708       
9709 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9710   `(defun ,(intern
9711             (format "gnus-summary-put-mark-as-%s%s"
9712                     name (if (eq way 'nomove)
9713                              ""
9714                            (concat "-" (symbol-name way)))))
9715      (n)
9716      ,(format
9717        "Mark the current article as %s%s.
9718 If N, the prefix, then repeat N times.
9719 If N is negative, move in reverse order.
9720 The difference between N and the actual number of articles marked is
9721 returned."
9722        name (cadr lway))
9723      (interactive "p")
9724      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9725     
9726 (defun gnus-summary-generic-mark (n mark move unread)
9727   "Mark N articles with MARK."
9728   (unless (eq major-mode 'gnus-summary-mode)
9729     (error "This command can only be used in the summary buffer"))
9730   (gnus-summary-show-thread)
9731   (let ((nummove
9732          (cond
9733           ((eq move 'next) 1)
9734           ((eq move 'prev) -1)
9735           (t 0))))
9736     (if (zerop nummove)
9737         (setq n 1)
9738       (when (< n 0)
9739         (setq n (abs n)
9740               nummove (* -1 nummove))))
9741     (while (and (> n 0)
9742                 (gnus-summary-mark-article nil mark)
9743                 (zerop (gnus-summary-next-subject nummove unread t)))
9744       (setq n (1- n)))
9745     (when (/= 0 n)
9746       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9747     (gnus-summary-recenter)
9748     (gnus-summary-position-point)
9749     (gnus-set-mode-line 'summary)
9750     n))
9751
9752 (gnus-summary-make-all-marking-commands)
9753
9754 (gnus-ems-redefine)
9755
9756 (provide 'gnus-sum)
9757
9758 (run-hooks 'gnus-sum-load-hook)
9759
9760 ;;; gnus-sum.el ends here