Sync up with Pterodactyl Gnus v0.95.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   ;; Avoid byte-compile warnings.
44   (defvar gnus-article-decoded-p)
45   (defvar gnus-decode-encoded-word-function)
46   )
47
48 (eval-and-compile
49   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, marking commands will go to the next unread article.
186 If `never', commands that usually go to the next unread article, will
187 go to the next article, whether it is read or not.
188 If nil, only the marking commands will go to the next (un)read article."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-auto-select-next t
289   "*If non-nil, offer to go to the next group from the end of the previous.
290 If the value is t and the next newsgroup is empty, Gnus will exit
291 summary mode and go back to group mode.  If the value is neither nil
292 nor t, Gnus will select the following unread newsgroup.  In
293 particular, if the value is the symbol `quietly', the next unread
294 newsgroup will be selected without any confirmation, and if it is
295 `almost-quietly', the next group will be selected without any
296 confirmation if you are located on the last article in the group.
297 Finally, if this variable is `slightly-quietly', the `Z n' command
298 will go to the next group without confirmation."
299   :group 'gnus-summary-maneuvering
300   :type '(choice (const :tag "off" nil)
301                  (const quietly)
302                  (const almost-quietly)
303                  (const slightly-quietly)
304                  (sexp :menu-tag "on" t)))
305
306 (defcustom gnus-auto-select-same nil
307   "*If non-nil, select the next article with the same subject.
308 If there are no more articles with the same subject, go to
309 the first unread article."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-summary-check-current nil
314   "*If non-nil, consider the current article when moving.
315 The \"unread\" movement commands will stay on the same line if the
316 current article is unread."
317   :group 'gnus-summary-maneuvering
318   :type 'boolean)
319
320 (defcustom gnus-auto-center-summary t
321   "*If non-nil, always center the current summary buffer.
322 In particular, if `vertical' do only vertical recentering.  If non-nil
323 and non-`vertical', do both horizontal and vertical recentering."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "none" nil)
326                  (const vertical)
327                  (integer :tag "height")
328                  (sexp :menu-tag "both" t)))
329
330 (defcustom gnus-show-all-headers nil
331   "*If non-nil, don't hide any headers."
332   :group 'gnus-article-hiding
333   :group 'gnus-article-headers
334   :type 'boolean)
335
336 (defcustom gnus-summary-ignore-duplicates nil
337   "*If non-nil, ignore articles with identical Message-ID headers."
338   :group 'gnus-summary
339   :type 'boolean)
340
341 (defcustom gnus-single-article-buffer t
342   "*If non-nil, display all articles in the same buffer.
343 If nil, each group will get its own article buffer."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-break-pages t
348   "*If non-nil, do page breaking on articles.
349 The page delimiter is specified by the `gnus-page-delimiter'
350 variable."
351   :group 'gnus-article-various
352   :type 'boolean)
353
354 (defcustom gnus-show-mime t
355   "*If non-nil, do mime processing of articles.
356 The articles will simply be fed to the function given by
357 `gnus-article-display-method-for-mime'."
358   :group 'gnus-article-mime
359   :type 'boolean)
360
361 (defcustom gnus-move-split-methods nil
362   "*Variable used to suggest where articles are to be moved to.
363 It uses the same syntax as the `gnus-split-methods' variable."
364   :group 'gnus-summary-mail
365   :type '(repeat (choice (list :value (fun) function)
366                          (cons :value ("" "") regexp (repeat string))
367                          (sexp :value nil))))
368
369 (defcustom gnus-unread-mark ? ;Whitespace
370   "*Mark used for unread articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-ticked-mark ?!
375   "*Mark used for ticked articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-dormant-mark ??
380   "*Mark used for dormant articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-del-mark ?r
385   "*Mark used for del'd articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-read-mark ?R
390   "*Mark used for read articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-expirable-mark ?E
395   "*Mark used for expirable articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-killed-mark ?K
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-souped-mark ?F
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-kill-file-mark ?X
410   "*Mark used for articles killed by kill files."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-low-score-mark ?Y
415   "*Mark used for articles with a low score."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-catchup-mark ?C
420   "*Mark used for articles that are caught up."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-replied-mark ?A
425   "*Mark used for articles that have been replied to."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-cached-mark ?*
430   "*Mark used for articles that are in the cache."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-saved-mark ?S
435   "*Mark used for articles that have been saved to."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-ancient-mark ?O
440   "*Mark used for ancient articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-sparse-mark ?Q
445   "*Mark used for sparsely reffed articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-canceled-mark ?G
450   "*Mark used for canceled articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-duplicate-mark ?M
455   "*Mark used for duplicate articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-undownloaded-mark ?@
460   "*Mark used for articles that weren't downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-downloadable-mark ?%
465   "*Mark used for articles that are to be downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-unsendable-mark ?=
470   "*Mark used for articles that won't be sent."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-over-mark ?+
475   "*Score mark used for articles with high scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-below-mark ?-
480   "*Score mark used for articles with low scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-empty-thread-mark ? ;Whitespace
485   "*There is no thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-not-empty-thread-mark ?=
490   "*There is a thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-view-pseudo-asynchronously nil
495   "*If non-nil, Gnus will view pseudo-articles asynchronously."
496   :group 'gnus-extract-view
497   :type 'boolean)
498
499 (defcustom gnus-auto-expirable-marks
500   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
501         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
502         gnus-souped-mark gnus-duplicate-mark)
503   "*The list of marks converted into expiration if a group is auto-expirable."
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :group 'gnus-summary
510   :type 'boolean)
511
512 (defcustom gnus-view-pseudos nil
513   "*If `automatic', pseudo-articles will be viewed automatically.
514 If `not-confirm', pseudos will be viewed automatically, and the user
515 will not be asked to confirm the command."
516   :group 'gnus-extract-view
517   :type '(choice (const :tag "off" nil)
518                  (const automatic)
519                  (const not-confirm)))
520
521 (defcustom gnus-view-pseudos-separately t
522   "*If non-nil, one pseudo-article will be created for each file to be viewed.
523 If nil, all files that use the same viewing command will be given as a
524 list of parameters to that command."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-insert-pseudo-articles t
529   "*If non-nil, insert pseudo-articles when decoding articles."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-summary-dummy-line-format
534   "  %(:                          :%) %S\n"
535   "*The format specification for the dummy roots in the summary buffer.
536 It works along the same lines as a normal formatting string,
537 with some simple extensions.
538
539 %S  The subject"
540   :group 'gnus-threading
541   :type 'string)
542
543 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
544   "*The format specification for the summary mode line.
545 It works along the same lines as a normal formatting string,
546 with some simple extensions:
547
548 %G  Group name
549 %p  Unprefixed group name
550 %A  Current article number
551 %z  Current article score
552 %V  Gnus version
553 %U  Number of unread articles in the group
554 %e  Number of unselected articles in the group
555 %Z  A string with unread/unselected article counts
556 %g  Shortish group name
557 %S  Subject of the current article
558 %u  User-defined spec
559 %s  Current score file name
560 %d  Number of dormant articles
561 %r  Number of articles that have been marked as read in this session
562 %E  Number of articles expunged by the score files"
563   :group 'gnus-summary-format
564   :type 'string)
565
566 (defcustom gnus-summary-mark-below 0
567   "*Mark all articles with a score below this variable as read.
568 This variable is local to each summary buffer and usually set by the
569 score file."
570   :group 'gnus-score-default
571   :type 'integer)
572
573 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
574   "*List of functions used for sorting articles in the summary buffer.
575 This variable is only used when not using a threaded display."
576   :group 'gnus-summary-sort
577   :type '(repeat (choice (function-item gnus-article-sort-by-number)
578                          (function-item gnus-article-sort-by-author)
579                          (function-item gnus-article-sort-by-subject)
580                          (function-item gnus-article-sort-by-date)
581                          (function-item gnus-article-sort-by-score)
582                          (function :tag "other"))))
583
584 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
585   "*List of functions used for sorting threads in the summary buffer.
586 By default, threads are sorted by article number.
587
588 Each function takes two threads and return non-nil if the first thread
589 should be sorted before the other.  If you use more than one function,
590 the primary sort function should be the last.  You should probably
591 always include `gnus-thread-sort-by-number' in the list of sorting
592 functions -- preferably first.
593
594 Ready-made functions include `gnus-thread-sort-by-number',
595 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
596 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
597 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
600                          (function-item gnus-thread-sort-by-author)
601                          (function-item gnus-thread-sort-by-subject)
602                          (function-item gnus-thread-sort-by-date)
603                          (function-item gnus-thread-sort-by-score)
604                          (function-item gnus-thread-sort-by-total-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-score-function '+
608   "*Function used for calculating the total score of a thread.
609
610 The function is called with the scores of the article and each
611 subthread and should then return the score of the thread.
612
613 Some functions you can use are `+', `max', or `min'."
614   :group 'gnus-summary-sort
615   :type 'function)
616
617 (defcustom gnus-summary-expunge-below nil
618   "All articles that have a score less than this variable will be expunged.
619 This variable is local to the summary buffers."
620   :group 'gnus-score-default
621   :type '(choice (const :tag "off" nil)
622                  integer))
623
624 (defcustom gnus-thread-expunge-below nil
625   "All threads that have a total score less than this variable will be expunged.
626 See `gnus-thread-score-function' for en explanation of what a
627 \"thread score\" is.
628
629 This variable is local to the summary buffers."
630   :group 'gnus-threading
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-summary-mode-hook nil
636   "*A hook for Gnus summary mode.
637 This hook is run before any variables are set in the summary buffer."
638   :group 'gnus-summary-various
639   :type 'hook)
640
641 (defcustom gnus-summary-menu-hook nil
642   "*Hook run after the creation of the summary mode menu."
643   :group 'gnus-summary-visual
644   :type 'hook)
645
646 (defcustom gnus-summary-exit-hook nil
647   "*A hook called on exit from the summary buffer.
648 It will be called with point in the group buffer."
649   :group 'gnus-summary-exit
650   :type 'hook)
651
652 (defcustom gnus-summary-prepare-hook nil
653   "*A hook called after the summary buffer has been generated.
654 If you want to modify the summary buffer, you can use this hook."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-prepared-hook nil
659   "*A hook called as the last thing after the summary buffer has been generated."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-generate-hook nil
664   "*A hook run just before generating the summary buffer.
665 This hook is commonly used to customize threading variables and the
666 like."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-select-group-hook nil
671   "*A hook called when a newsgroup is selected.
672
673 If you'd like to simplify subjects like the
674 `gnus-summary-next-same-subject' command does, you can use the
675 following hook:
676
677  (setq gnus-select-group-hook
678       (list
679         (lambda ()
680           (mapcar (lambda (header)
681                      (mail-header-set-subject
682                       header
683                       (gnus-simplify-subject
684                        (mail-header-subject header) 're-only)))
685                   gnus-newsgroup-headers))))"
686   :group 'gnus-group-select
687   :type 'hook)
688
689 (defcustom gnus-select-article-hook nil
690   "*A hook called when an article is selected."
691   :group 'gnus-summary-choose
692   :type 'hook)
693
694 (defcustom gnus-visual-mark-article-hook
695   (list 'gnus-highlight-selected-summary)
696   "*Hook run after selecting an article in the summary buffer.
697 It is meant to be used for highlighting the article in some way.  It
698 is not run if `gnus-visual' is nil."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
703   "*A hook called before parsing the headers."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-exit-group-hook nil
708   "*A hook called when exiting (not quitting) summary mode."
709   :group 'gnus-various
710   :type 'hook)
711
712 (defcustom gnus-summary-update-hook
713   (list 'gnus-summary-highlight-line)
714   "*A hook called when a summary line is changed.
715 The hook will not be called if `gnus-visual' is nil.
716
717 The default function `gnus-summary-highlight-line' will
718 highlight the line according to the `gnus-summary-highlight'
719 variable."
720   :group 'gnus-summary-visual
721   :type 'hook)
722
723 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
724   "*A hook called when an article is selected for the first time.
725 The hook is intended to mark an article as read (or unread)
726 automatically when it is selected."
727   :group 'gnus-summary-choose
728   :type 'hook)
729
730 (defcustom gnus-group-no-more-groups-hook nil
731   "*A hook run when returning to group mode having no more (unread) groups."
732   :group 'gnus-group-select
733   :type 'hook)
734
735 (defcustom gnus-ps-print-hook nil
736   "*A hook run before ps-printing something from Gnus."
737   :group 'gnus-summary
738   :type 'hook)
739
740 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
741   "Face used for highlighting the current article in the summary buffer."
742   :group 'gnus-summary-visual
743   :type 'face)
744
745 (defcustom gnus-summary-highlight
746   '(((= mark gnus-canceled-mark)
747      . gnus-summary-cancelled-face)
748     ((and (> score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-high-ticked-face)
752     ((and (< score default)
753           (or (= mark gnus-dormant-mark)
754               (= mark gnus-ticked-mark)))
755      . gnus-summary-low-ticked-face)
756     ((or (= mark gnus-dormant-mark)
757          (= mark gnus-ticked-mark))
758      . gnus-summary-normal-ticked-face)
759     ((and (> score default) (= mark gnus-ancient-mark))
760      . gnus-summary-high-ancient-face)
761     ((and (< score default) (= mark gnus-ancient-mark))
762      . gnus-summary-low-ancient-face)
763     ((= mark gnus-ancient-mark)
764      . gnus-summary-normal-ancient-face)
765     ((and (> score default) (= mark gnus-unread-mark))
766      . gnus-summary-high-unread-face)
767     ((and (< score default) (= mark gnus-unread-mark))
768      . gnus-summary-low-unread-face)
769     ((= mark gnus-unread-mark)
770      . gnus-summary-normal-unread-face)
771     ((and (> score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-high-unread-face)
774     ((and (< score default) (memq mark (list gnus-downloadable-mark
775                                              gnus-undownloaded-mark)))
776      . gnus-summary-low-unread-face)
777     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
778      . gnus-summary-normal-unread-face)
779     ((> score default)
780      . gnus-summary-high-read-face)
781     ((< score default)
782      . gnus-summary-low-read-face)
783     (t
784      . gnus-summary-normal-read-face))
785   "*Controls the highlighting of summary buffer lines.
786
787 A list of (FORM . FACE) pairs.  When deciding how a a particular
788 summary line should be displayed, each form is evaluated.  The content
789 of the face field after the first true form is used.  You can change
790 how those summary lines are displayed, by editing the face field.
791
792 You can use the following variables in the FORM field.
793
794 score:   The articles score
795 default: The default article score.
796 below:   The score below which articles are automatically marked as read.
797 mark:    The articles mark."
798   :group 'gnus-summary-visual
799   :type '(repeat (cons (sexp :tag "Form" nil)
800                        face)))
801
802 (defcustom gnus-alter-header-function nil
803   "Function called to allow alteration of article header structures.
804 The function is called with one parameter, the article header vector,
805 which it may alter in any way.")
806
807 (defcustom gnus-extra-headers nil
808   "*Extra headers to parse."
809   :group 'gnus-summary
810   :type '(repeat symbol))
811
812 (defcustom gnus-ignored-from-addresses
813   (and user-mail-address (regexp-quote user-mail-address))
814   "*Regexp of From headers that may be suppressed in favor of To headers."
815   :group 'gnus-summary
816   :type 'regexp)
817
818 (defcustom gnus-group-charset-alist
819   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
820     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
821     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
822     ("^relcom\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
827     (".*" iso-8859-1))
828   "Alist of regexps (to match group names) and default charsets to be used when reading."
829   :type '(repeat (list (regexp :tag "Group")
830                        (symbol :tag "Charset")))
831   :group 'gnus-charset)
832
833 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
834   "List of charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat symbol)
838   :group 'gnus-charset)
839
840 (defcustom gnus-group-ignored-charsets-alist 
841   '(("alt\\.chinese\\.text" iso-8859-1))
842   "Alist of regexps (to match group names) and charsets that should be ignored.
843 When these charsets are used in the \"charset\" parameter, the
844 default charset will be used instead."
845   :type '(repeat (cons (regexp :tag "Group")
846                        (repeat symbol)))
847   :group 'gnus-charset)
848
849 (defcustom gnus-group-highlight-words-alist nil
850   "Alist of group regexps and highlight regexps.
851 This variable uses the same syntax as `gnus-emphasis-alist'."
852   :type '(repeat (cons (regexp :tag "Group")
853                        (repeat (list (regexp :tag "Highlight regexp")
854                                      (number :tag "Group for entire word" 0)
855                                      (number :tag "Group for displayed part" 0)
856                                      (symbol :tag "Face" 
857                                              gnus-emphasis-highlight-words)))))
858   :group 'gnus-summary-visual)
859
860 ;;; Internal variables
861
862 (defvar gnus-scores-exclude-files nil)
863 (defvar gnus-page-broken nil)
864 (defvar gnus-inhibit-mime-unbuttonizing nil)
865
866 (defvar gnus-original-article nil)
867 (defvar gnus-article-internal-prepare-hook nil)
868 (defvar gnus-newsgroup-process-stack nil)
869
870 (defvar gnus-thread-indent-array nil)
871 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
872
873 ;; Avoid highlighting in kill files.
874 (defvar gnus-summary-inhibit-highlight nil)
875 (defvar gnus-newsgroup-selected-overlay nil)
876 (defvar gnus-inhibit-limiting nil)
877 (defvar gnus-newsgroup-adaptive-score-file nil)
878 (defvar gnus-current-score-file nil)
879 (defvar gnus-current-move-group nil)
880 (defvar gnus-current-copy-group nil)
881 (defvar gnus-current-crosspost-group nil)
882
883 (defvar gnus-newsgroup-dependencies nil)
884 (defvar gnus-newsgroup-adaptive nil)
885 (defvar gnus-summary-display-article-function nil)
886 (defvar gnus-summary-highlight-line-function nil
887   "Function called after highlighting a summary line.")
888
889 (defvar gnus-summary-line-format-alist
890   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
891     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
892     (?s gnus-tmp-subject-or-nil ?s)
893     (?n gnus-tmp-name ?s)
894     (?A (std11-address-string
895          (car (mime-read-field 'From gnus-tmp-header))) ?s)
896     (?a (or (std11-full-name-string
897              (car (mime-read-field 'From gnus-tmp-header)))
898             gnus-tmp-from) ?s)
899     (?F gnus-tmp-from ?s)
900     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
901     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
902     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
903     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
904     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
905     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
906     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
907     (?L gnus-tmp-lines ?d)
908     (?I gnus-tmp-indentation ?s)
909     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
910     (?R gnus-tmp-replied ?c)
911     (?\[ gnus-tmp-opening-bracket ?c)
912     (?\] gnus-tmp-closing-bracket ?c)
913     (?\> (make-string gnus-tmp-level ? ) ?s)
914     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
915     (?i gnus-tmp-score ?d)
916     (?z gnus-tmp-score-char ?c)
917     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
918     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
919     (?U gnus-tmp-unread ?c)
920     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
921     (?t (gnus-summary-number-of-articles-in-thread
922          (and (boundp 'thread) (car thread)) gnus-tmp-level)
923         ?d)
924     (?e (gnus-summary-number-of-articles-in-thread
925          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
926         ?c)
927     (?u gnus-tmp-user-defined ?s)
928     (?P (gnus-pick-line-number) ?d))
929   "An alist of format specifications that can appear in summary lines,
930 and what variables they correspond with, along with the type of the
931 variable (string, integer, character, etc).")
932
933 (defvar gnus-summary-dummy-line-format-alist
934   `((?S gnus-tmp-subject ?s)
935     (?N gnus-tmp-number ?d)
936     (?u gnus-tmp-user-defined ?s)))
937
938 (defvar gnus-summary-mode-line-format-alist
939   `((?G gnus-tmp-group-name ?s)
940     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
941     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
942     (?A gnus-tmp-article-number ?d)
943     (?Z gnus-tmp-unread-and-unselected ?s)
944     (?V gnus-version ?s)
945     (?U gnus-tmp-unread-and-unticked ?d)
946     (?S gnus-tmp-subject ?s)
947     (?e gnus-tmp-unselected ?d)
948     (?u gnus-tmp-user-defined ?s)
949     (?d (length gnus-newsgroup-dormant) ?d)
950     (?t (length gnus-newsgroup-marked) ?d)
951     (?r (length gnus-newsgroup-reads) ?d)
952     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
953     (?E gnus-newsgroup-expunged-tally ?d)
954     (?s (gnus-current-score-file-nondirectory) ?s)))
955
956 (defvar gnus-last-search-regexp nil
957   "Default regexp for article search command.")
958
959 (defvar gnus-last-shell-command nil
960   "Default shell command on article.")
961
962 (defvar gnus-newsgroup-begin nil)
963 (defvar gnus-newsgroup-end nil)
964 (defvar gnus-newsgroup-last-rmail nil)
965 (defvar gnus-newsgroup-last-mail nil)
966 (defvar gnus-newsgroup-last-folder nil)
967 (defvar gnus-newsgroup-last-file nil)
968 (defvar gnus-newsgroup-auto-expire nil)
969 (defvar gnus-newsgroup-active nil)
970
971 (defvar gnus-newsgroup-data nil)
972 (defvar gnus-newsgroup-data-reverse nil)
973 (defvar gnus-newsgroup-limit nil)
974 (defvar gnus-newsgroup-limits nil)
975
976 (defvar gnus-newsgroup-unreads nil
977   "List of unread articles in the current newsgroup.")
978
979 (defvar gnus-newsgroup-unselected nil
980   "List of unselected unread articles in the current newsgroup.")
981
982 (defvar gnus-newsgroup-reads nil
983   "Alist of read articles and article marks in the current newsgroup.")
984
985 (defvar gnus-newsgroup-expunged-tally nil)
986
987 (defvar gnus-newsgroup-marked nil
988   "List of ticked articles in the current newsgroup (a subset of unread art).")
989
990 (defvar gnus-newsgroup-killed nil
991   "List of ranges of articles that have been through the scoring process.")
992
993 (defvar gnus-newsgroup-cached nil
994   "List of articles that come from the article cache.")
995
996 (defvar gnus-newsgroup-saved nil
997   "List of articles that have been saved.")
998
999 (defvar gnus-newsgroup-kill-headers nil)
1000
1001 (defvar gnus-newsgroup-replied nil
1002   "List of articles that have been replied to in the current newsgroup.")
1003
1004 (defvar gnus-newsgroup-expirable nil
1005   "List of articles in the current newsgroup that can be expired.")
1006
1007 (defvar gnus-newsgroup-processable nil
1008   "List of articles in the current newsgroup that can be processed.")
1009
1010 (defvar gnus-newsgroup-downloadable nil
1011   "List of articles in the current newsgroup that can be processed.")
1012
1013 (defvar gnus-newsgroup-undownloaded nil
1014   "List of articles in the current newsgroup that haven't been downloaded..")
1015
1016 (defvar gnus-newsgroup-unsendable nil
1017   "List of articles in the current newsgroup that won't be sent.")
1018
1019 (defvar gnus-newsgroup-bookmarks nil
1020   "List of articles in the current newsgroup that have bookmarks.")
1021
1022 (defvar gnus-newsgroup-dormant nil
1023   "List of dormant articles in the current newsgroup.")
1024
1025 (defvar gnus-newsgroup-scored nil
1026   "List of scored articles in the current newsgroup.")
1027
1028 (defvar gnus-newsgroup-headers nil
1029   "List of article headers in the current newsgroup.")
1030
1031 (defvar gnus-newsgroup-threads nil)
1032
1033 (defvar gnus-newsgroup-prepared nil
1034   "Whether the current group has been prepared properly.")
1035
1036 (defvar gnus-newsgroup-ancient nil
1037   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1038
1039 (defvar gnus-newsgroup-sparse nil)
1040
1041 (defvar gnus-current-article nil)
1042 (defvar gnus-article-current nil)
1043 (defvar gnus-current-headers nil)
1044 (defvar gnus-have-all-headers nil)
1045 (defvar gnus-last-article nil)
1046 (defvar gnus-newsgroup-history nil)
1047 (defvar gnus-newsgroup-charset nil)
1048 (defvar gnus-newsgroup-ephemeral-charset nil)
1049 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1050
1051 (defconst gnus-summary-local-variables
1052   '(gnus-newsgroup-name
1053     gnus-newsgroup-begin gnus-newsgroup-end
1054     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1055     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1056     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1057     gnus-newsgroup-unselected gnus-newsgroup-marked
1058     gnus-newsgroup-reads gnus-newsgroup-saved
1059     gnus-newsgroup-replied gnus-newsgroup-expirable
1060     gnus-newsgroup-processable gnus-newsgroup-killed
1061     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1062     gnus-newsgroup-unsendable
1063     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1064     gnus-newsgroup-headers gnus-newsgroup-threads
1065     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1066     gnus-current-article gnus-current-headers gnus-have-all-headers
1067     gnus-last-article gnus-article-internal-prepare-hook
1068     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1069     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1070     gnus-thread-expunge-below
1071     gnus-score-alist gnus-current-score-file
1072     (gnus-summary-expunge-below . global)
1073     (gnus-summary-mark-below . global)
1074     gnus-newsgroup-active gnus-scores-exclude-files
1075     gnus-newsgroup-history gnus-newsgroup-ancient
1076     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1077     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1078     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1079     (gnus-newsgroup-expunged-tally . 0)
1080     gnus-cache-removable-articles gnus-newsgroup-cached
1081     gnus-newsgroup-data gnus-newsgroup-data-reverse
1082     gnus-newsgroup-limit gnus-newsgroup-limits
1083     gnus-newsgroup-charset)
1084   "Variables that are buffer-local to the summary buffers.")
1085
1086 ;; Byte-compiler warning.
1087 (defvar gnus-article-mode-map)
1088
1089 ;; Subject simplification.
1090
1091 (defun gnus-simplify-whitespace (str)
1092   "Remove excessive whitespace."
1093   (let ((mystr str))
1094     ;; Multiple spaces.
1095     (while (string-match "[ \t][ \t]+" mystr)
1096       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1097                           " "
1098                           (substring mystr (match-end 0)))))
1099     ;; Leading spaces.
1100     (when (string-match "^[ \t]+" mystr)
1101       (setq mystr (substring mystr (match-end 0))))
1102     ;; Trailing spaces.
1103     (when (string-match "[ \t]+$" mystr)
1104       (setq mystr (substring mystr 0 (match-beginning 0))))
1105     mystr))
1106
1107 (defsubst gnus-simplify-subject-re (subject)
1108   "Remove \"Re:\" from subject lines."
1109   (if (string-match "^[Rr][Ee]: *" subject)
1110       (substring subject (match-end 0))
1111     subject))
1112
1113 (defun gnus-simplify-subject (subject &optional re-only)
1114   "Remove `Re:' and words in parentheses.
1115 If RE-ONLY is non-nil, strip leading `Re:'s only."
1116   (let ((case-fold-search t))           ;Ignore case.
1117     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1118     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1119       (setq subject (substring subject (match-end 0))))
1120     ;; Remove uninteresting prefixes.
1121     (when (and (not re-only)
1122                gnus-simplify-ignored-prefixes
1123                (string-match gnus-simplify-ignored-prefixes subject))
1124       (setq subject (substring subject (match-end 0))))
1125     ;; Remove words in parentheses from end.
1126     (unless re-only
1127       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1128         (setq subject (substring subject 0 (match-beginning 0)))))
1129     ;; Return subject string.
1130     subject))
1131
1132 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1133 ;; all whitespace.
1134 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1135   (goto-char (point-min))
1136   (while (re-search-forward regexp nil t)
1137       (replace-match (or newtext ""))))
1138
1139 (defun gnus-simplify-buffer-fuzzy ()
1140   "Simplify string in the buffer fuzzily.
1141 The string in the accessible portion of the current buffer is simplified.
1142 It is assumed to be a single-line subject.
1143 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1144 matter is removed.  Additional things can be deleted by setting
1145 gnus-simplify-subject-fuzzy-regexp."
1146   (let ((case-fold-search t)
1147         (modified-tick))
1148     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1149
1150     (while (not (eq modified-tick (buffer-modified-tick)))
1151       (setq modified-tick (buffer-modified-tick))
1152       (cond
1153        ((listp gnus-simplify-subject-fuzzy-regexp)
1154         (mapcar 'gnus-simplify-buffer-fuzzy-step
1155                 gnus-simplify-subject-fuzzy-regexp))
1156        (gnus-simplify-subject-fuzzy-regexp
1157         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1158       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1159       (gnus-simplify-buffer-fuzzy-step
1160        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1161       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1162
1163     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1164     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1165     (gnus-simplify-buffer-fuzzy-step " $")
1166     (gnus-simplify-buffer-fuzzy-step "^ +")))
1167
1168 (defun gnus-simplify-subject-fuzzy (subject)
1169   "Simplify a subject string fuzzily.
1170 See `gnus-simplify-buffer-fuzzy' for details."
1171   (save-excursion
1172     (gnus-set-work-buffer)
1173     (let ((case-fold-search t))
1174       ;; Remove uninteresting prefixes.
1175       (when (and gnus-simplify-ignored-prefixes
1176                  (string-match gnus-simplify-ignored-prefixes subject))
1177         (setq subject (substring subject (match-end 0))))
1178       (insert subject)
1179       (inline (gnus-simplify-buffer-fuzzy))
1180       (buffer-string))))
1181
1182 (defsubst gnus-simplify-subject-fully (subject)
1183   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1184   (cond
1185    (gnus-simplify-subject-functions
1186     (gnus-map-function gnus-simplify-subject-functions subject))
1187    ((null gnus-summary-gather-subject-limit)
1188     (gnus-simplify-subject-re subject))
1189    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1190     (gnus-simplify-subject-fuzzy subject))
1191    ((numberp gnus-summary-gather-subject-limit)
1192     (gnus-limit-string (gnus-simplify-subject-re subject)
1193                        gnus-summary-gather-subject-limit))
1194    (t
1195     subject)))
1196
1197 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1198   "Check whether two subjects are equal.
1199 If optional argument simple-first is t, first argument is already
1200 simplified."
1201   (cond
1202    ((null simple-first)
1203     (equal (gnus-simplify-subject-fully s1)
1204            (gnus-simplify-subject-fully s2)))
1205    (t
1206     (equal s1
1207            (gnus-simplify-subject-fully s2)))))
1208
1209 (defun gnus-summary-bubble-group ()
1210   "Increase the score of the current group.
1211 This is a handy function to add to `gnus-summary-exit-hook' to
1212 increase the score of each group you read."
1213   (gnus-group-add-score gnus-newsgroup-name))
1214
1215 \f
1216 ;;;
1217 ;;; Gnus summary mode
1218 ;;;
1219
1220 (put 'gnus-summary-mode 'mode-class 'special)
1221
1222 (when t
1223   ;; Non-orthogonal keys
1224
1225   (gnus-define-keys gnus-summary-mode-map
1226     " " gnus-summary-next-page
1227     "\177" gnus-summary-prev-page
1228     [delete] gnus-summary-prev-page
1229     [backspace] gnus-summary-prev-page
1230     "\r" gnus-summary-scroll-up
1231     "\M-\r" gnus-summary-scroll-down
1232     "n" gnus-summary-next-unread-article
1233     "p" gnus-summary-prev-unread-article
1234     "N" gnus-summary-next-article
1235     "P" gnus-summary-prev-article
1236     "\M-\C-n" gnus-summary-next-same-subject
1237     "\M-\C-p" gnus-summary-prev-same-subject
1238     "\M-n" gnus-summary-next-unread-subject
1239     "\M-p" gnus-summary-prev-unread-subject
1240     "." gnus-summary-first-unread-article
1241     "," gnus-summary-best-unread-article
1242     "\M-s" gnus-summary-search-article-forward
1243     "\M-r" gnus-summary-search-article-backward
1244     "<" gnus-summary-beginning-of-article
1245     ">" gnus-summary-end-of-article
1246     "j" gnus-summary-goto-article
1247     "^" gnus-summary-refer-parent-article
1248     "\M-^" gnus-summary-refer-article
1249     "u" gnus-summary-tick-article-forward
1250     "!" gnus-summary-tick-article-forward
1251     "U" gnus-summary-tick-article-backward
1252     "d" gnus-summary-mark-as-read-forward
1253     "D" gnus-summary-mark-as-read-backward
1254     "E" gnus-summary-mark-as-expirable
1255     "\M-u" gnus-summary-clear-mark-forward
1256     "\M-U" gnus-summary-clear-mark-backward
1257     "k" gnus-summary-kill-same-subject-and-select
1258     "\C-k" gnus-summary-kill-same-subject
1259     "\M-\C-k" gnus-summary-kill-thread
1260     "\M-\C-l" gnus-summary-lower-thread
1261     "e" gnus-summary-edit-article
1262     "#" gnus-summary-mark-as-processable
1263     "\M-#" gnus-summary-unmark-as-processable
1264     "\M-\C-t" gnus-summary-toggle-threads
1265     "\M-\C-s" gnus-summary-show-thread
1266     "\M-\C-h" gnus-summary-hide-thread
1267     "\M-\C-f" gnus-summary-next-thread
1268     "\M-\C-b" gnus-summary-prev-thread
1269     "\M-\C-u" gnus-summary-up-thread
1270     "\M-\C-d" gnus-summary-down-thread
1271     "&" gnus-summary-execute-command
1272     "c" gnus-summary-catchup-and-exit
1273     "\C-w" gnus-summary-mark-region-as-read
1274     "\C-t" gnus-summary-toggle-truncation
1275     "?" gnus-summary-mark-as-dormant
1276     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1277     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1278     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1279     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1280     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1281     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1282     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1283     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1284     "=" gnus-summary-expand-window
1285     "\C-x\C-s" gnus-summary-reselect-current-group
1286     "\M-g" gnus-summary-rescan-group
1287     "w" gnus-summary-stop-page-breaking
1288     "\C-c\C-r" gnus-summary-caesar-message
1289     "\M-t" gnus-summary-toggle-mime
1290     "f" gnus-summary-followup
1291     "F" gnus-summary-followup-with-original
1292     "C" gnus-summary-cancel-article
1293     "r" gnus-summary-reply
1294     "R" gnus-summary-reply-with-original
1295     "\C-c\C-f" gnus-summary-mail-forward
1296     "o" gnus-summary-save-article
1297     "\C-o" gnus-summary-save-article-mail
1298     "|" gnus-summary-pipe-output
1299     "\M-k" gnus-summary-edit-local-kill
1300     "\M-K" gnus-summary-edit-global-kill
1301     ;; "V" gnus-version
1302     "\C-c\C-d" gnus-summary-describe-group
1303     "q" gnus-summary-exit
1304     "Q" gnus-summary-exit-no-update
1305     "\C-c\C-i" gnus-info-find-node
1306     gnus-mouse-2 gnus-mouse-pick-article
1307     "m" gnus-summary-mail-other-window
1308     "a" gnus-summary-post-news
1309     "x" gnus-summary-limit-to-unread
1310     "s" gnus-summary-isearch-article
1311     "t" gnus-article-toggle-headers
1312     "g" gnus-summary-show-article
1313     "l" gnus-summary-goto-last-article
1314     "v" gnus-summary-preview-mime-message
1315     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1316     "\C-d" gnus-summary-enter-digest-group
1317     "\M-\C-d" gnus-summary-read-document
1318     "\M-\C-e" gnus-summary-edit-parameters
1319     "\M-\C-g" gnus-summary-customize-parameters
1320     "\C-c\C-b" gnus-bug
1321     "*" gnus-cache-enter-article
1322     "\M-*" gnus-cache-remove-article
1323     "\M-&" gnus-summary-universal-argument
1324     "\C-l" gnus-recenter
1325     "I" gnus-summary-increase-score
1326     "L" gnus-summary-lower-score
1327     "\M-i" gnus-symbolic-argument
1328     "h" gnus-summary-select-article-buffer
1329
1330     "V" gnus-summary-score-map
1331     "X" gnus-uu-extract-map
1332     "S" gnus-summary-send-map)
1333
1334   ;; Sort of orthogonal keymap
1335   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1336     "t" gnus-summary-tick-article-forward
1337     "!" gnus-summary-tick-article-forward
1338     "d" gnus-summary-mark-as-read-forward
1339     "r" gnus-summary-mark-as-read-forward
1340     "c" gnus-summary-clear-mark-forward
1341     " " gnus-summary-clear-mark-forward
1342     "e" gnus-summary-mark-as-expirable
1343     "x" gnus-summary-mark-as-expirable
1344     "?" gnus-summary-mark-as-dormant
1345     "b" gnus-summary-set-bookmark
1346     "B" gnus-summary-remove-bookmark
1347     "#" gnus-summary-mark-as-processable
1348     "\M-#" gnus-summary-unmark-as-processable
1349     "S" gnus-summary-limit-include-expunged
1350     "C" gnus-summary-catchup
1351     "H" gnus-summary-catchup-to-here
1352     "\C-c" gnus-summary-catchup-all
1353     "k" gnus-summary-kill-same-subject-and-select
1354     "K" gnus-summary-kill-same-subject
1355     "P" gnus-uu-mark-map)
1356
1357   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1358     "c" gnus-summary-clear-above
1359     "u" gnus-summary-tick-above
1360     "m" gnus-summary-mark-above
1361     "k" gnus-summary-kill-below)
1362
1363   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1364     "/" gnus-summary-limit-to-subject
1365     "n" gnus-summary-limit-to-articles
1366     "w" gnus-summary-pop-limit
1367     "s" gnus-summary-limit-to-subject
1368     "a" gnus-summary-limit-to-author
1369     "u" gnus-summary-limit-to-unread
1370     "m" gnus-summary-limit-to-marks
1371     "M" gnus-summary-limit-exclude-marks
1372     "v" gnus-summary-limit-to-score
1373     "*" gnus-summary-limit-include-cached
1374     "D" gnus-summary-limit-include-dormant
1375     "T" gnus-summary-limit-include-thread
1376     "d" gnus-summary-limit-exclude-dormant
1377     "t" gnus-summary-limit-to-age
1378     "x" gnus-summary-limit-to-extra 
1379     "E" gnus-summary-limit-include-expunged
1380     "c" gnus-summary-limit-exclude-childless-dormant
1381     "C" gnus-summary-limit-mark-excluded-as-read)
1382
1383   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1384     "n" gnus-summary-next-unread-article
1385     "p" gnus-summary-prev-unread-article
1386     "N" gnus-summary-next-article
1387     "P" gnus-summary-prev-article
1388     "\C-n" gnus-summary-next-same-subject
1389     "\C-p" gnus-summary-prev-same-subject
1390     "\M-n" gnus-summary-next-unread-subject
1391     "\M-p" gnus-summary-prev-unread-subject
1392     "f" gnus-summary-first-unread-article
1393     "b" gnus-summary-best-unread-article
1394     "j" gnus-summary-goto-article
1395     "g" gnus-summary-goto-subject
1396     "l" gnus-summary-goto-last-article
1397     "o" gnus-summary-pop-article)
1398
1399   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1400     "k" gnus-summary-kill-thread
1401     "l" gnus-summary-lower-thread
1402     "i" gnus-summary-raise-thread
1403     "T" gnus-summary-toggle-threads
1404     "t" gnus-summary-rethread-current
1405     "^" gnus-summary-reparent-thread
1406     "s" gnus-summary-show-thread
1407     "S" gnus-summary-show-all-threads
1408     "h" gnus-summary-hide-thread
1409     "H" gnus-summary-hide-all-threads
1410     "n" gnus-summary-next-thread
1411     "p" gnus-summary-prev-thread
1412     "u" gnus-summary-up-thread
1413     "o" gnus-summary-top-thread
1414     "d" gnus-summary-down-thread
1415     "#" gnus-uu-mark-thread
1416     "\M-#" gnus-uu-unmark-thread)
1417
1418   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1419     "g" gnus-summary-prepare
1420     "c" gnus-summary-insert-cached-articles)
1421
1422   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1423     "c" gnus-summary-catchup-and-exit
1424     "C" gnus-summary-catchup-all-and-exit
1425     "E" gnus-summary-exit-no-update
1426     "Q" gnus-summary-exit
1427     "Z" gnus-summary-exit
1428     "n" gnus-summary-catchup-and-goto-next-group
1429     "R" gnus-summary-reselect-current-group
1430     "G" gnus-summary-rescan-group
1431     "N" gnus-summary-next-group
1432     "s" gnus-summary-save-newsrc
1433     "P" gnus-summary-prev-group)
1434
1435   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1436     " " gnus-summary-next-page
1437     "n" gnus-summary-next-page
1438     "\177" gnus-summary-prev-page
1439     [delete] gnus-summary-prev-page
1440     "p" gnus-summary-prev-page
1441     "\r" gnus-summary-scroll-up
1442     "\M-\r" gnus-summary-scroll-down
1443     "<" gnus-summary-beginning-of-article
1444     ">" gnus-summary-end-of-article
1445     "b" gnus-summary-beginning-of-article
1446     "e" gnus-summary-end-of-article
1447     "^" gnus-summary-refer-parent-article
1448     "r" gnus-summary-refer-parent-article
1449     "D" gnus-summary-enter-digest-group
1450     "R" gnus-summary-refer-references
1451     "T" gnus-summary-refer-thread
1452     "g" gnus-summary-show-article
1453     "s" gnus-summary-isearch-article
1454     "P" gnus-summary-print-article
1455     "t" gnus-article-babel)
1456
1457   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1458     "b" gnus-article-add-buttons
1459     "B" gnus-article-add-buttons-to-head
1460     "o" gnus-article-treat-overstrike
1461     "e" gnus-article-emphasize
1462     "w" gnus-article-fill-cited-article
1463     "Q" gnus-article-fill-long-lines
1464     "C" gnus-article-capitalize-sentences
1465     "c" gnus-article-remove-cr
1466     "f" gnus-article-display-x-face
1467     "l" gnus-summary-stop-page-breaking
1468     "r" gnus-summary-caesar-message
1469     "t" gnus-article-toggle-headers
1470     "v" gnus-summary-verbose-headers
1471     "m" gnus-summary-toggle-mime
1472     "H" gnus-article-strip-headers-in-body
1473     "d" gnus-article-treat-dumbquotes)
1474
1475   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1476     "a" gnus-article-hide
1477     "h" gnus-article-toggle-headers
1478     "b" gnus-article-hide-boring-headers
1479     "s" gnus-article-hide-signature
1480     "c" gnus-article-hide-citation
1481     "C" gnus-article-hide-citation-in-followups
1482     "p" gnus-article-hide-pgp
1483     "B" gnus-article-strip-banner
1484     "P" gnus-article-hide-pem
1485     "\C-c" gnus-article-hide-citation-maybe)
1486
1487   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1488     "a" gnus-article-highlight
1489     "h" gnus-article-highlight-headers
1490     "c" gnus-article-highlight-citation
1491     "s" gnus-article-highlight-signature)
1492
1493   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1494     "z" gnus-article-date-ut
1495     "u" gnus-article-date-ut
1496     "l" gnus-article-date-local
1497     "e" gnus-article-date-lapsed
1498     "o" gnus-article-date-original
1499     "i" gnus-article-date-iso8601
1500     "s" gnus-article-date-user)
1501
1502   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1503     "t" gnus-article-remove-trailing-blank-lines
1504     "l" gnus-article-strip-leading-blank-lines
1505     "m" gnus-article-strip-multiple-blank-lines
1506     "a" gnus-article-strip-blank-lines
1507     "A" gnus-article-strip-all-blank-lines
1508     "s" gnus-article-strip-leading-space
1509     "e" gnus-article-strip-trailing-space)
1510
1511   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1512     "v" gnus-version
1513     "f" gnus-summary-fetch-faq
1514     "d" gnus-summary-describe-group
1515     "h" gnus-summary-describe-briefly
1516     "i" gnus-info-find-node)
1517
1518   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1519     "e" gnus-summary-expire-articles
1520     "\M-\C-e" gnus-summary-expire-articles-now
1521     "\177" gnus-summary-delete-article
1522     [delete] gnus-summary-delete-article
1523     [backspace] gnus-summary-delete-article
1524     "m" gnus-summary-move-article
1525     "r" gnus-summary-respool-article
1526     "w" gnus-summary-edit-article
1527     "c" gnus-summary-copy-article
1528     "B" gnus-summary-crosspost-article
1529     "q" gnus-summary-respool-query
1530     "t" gnus-summary-respool-trace
1531     "i" gnus-summary-import-article
1532     "p" gnus-summary-article-posted-p)
1533
1534   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1535     "o" gnus-summary-save-article
1536     "m" gnus-summary-save-article-mail
1537     "F" gnus-summary-write-article-file
1538     "r" gnus-summary-save-article-rmail
1539     "f" gnus-summary-save-article-file
1540     "b" gnus-summary-save-article-body-file
1541     "h" gnus-summary-save-article-folder
1542     "v" gnus-summary-save-article-vm
1543     "p" gnus-summary-pipe-output
1544     "s" gnus-soup-add-article)
1545
1546   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1547     "b" gnus-summary-display-buttonized
1548     "m" gnus-summary-repair-multipart
1549     "v" gnus-article-view-part
1550     "o" gnus-article-save-part
1551     "c" gnus-article-copy-part
1552     "e" gnus-article-externalize-part
1553     "i" gnus-article-inline-part
1554     "|" gnus-article-pipe-part)
1555   )
1556
1557 (defun gnus-summary-make-menu-bar ()
1558   (gnus-turn-off-edit-menu 'summary)
1559
1560   (unless (boundp 'gnus-summary-misc-menu)
1561
1562     (easy-menu-define
1563      gnus-summary-kill-menu gnus-summary-mode-map ""
1564      (cons
1565       "Score"
1566       (nconc
1567        (list
1568         ["Enter score..." gnus-summary-score-entry t]
1569         ["Customize" gnus-score-customize t])
1570        (gnus-make-score-map 'increase)
1571        (gnus-make-score-map 'lower)
1572        '(("Mark"
1573           ["Kill below" gnus-summary-kill-below t]
1574           ["Mark above" gnus-summary-mark-above t]
1575           ["Tick above" gnus-summary-tick-above t]
1576           ["Clear above" gnus-summary-clear-above t])
1577          ["Current score" gnus-summary-current-score t]
1578          ["Set score" gnus-summary-set-score t]
1579          ["Switch current score file..." gnus-score-change-score-file t]
1580          ["Set mark below..." gnus-score-set-mark-below t]
1581          ["Set expunge below..." gnus-score-set-expunge-below t]
1582          ["Edit current score file" gnus-score-edit-current-scores t]
1583          ["Edit score file" gnus-score-edit-file t]
1584          ["Trace score" gnus-score-find-trace t]
1585          ["Find words" gnus-score-find-favourite-words t]
1586          ["Rescore buffer" gnus-summary-rescore t]
1587          ["Increase score..." gnus-summary-increase-score t]
1588          ["Lower score..." gnus-summary-lower-score t]))))
1589
1590     ;; Define both the Article menu in the summary buffer and the equivalent
1591     ;; Commands menu in the article buffer here for consistency.
1592     (let ((innards
1593            '(("Hide"
1594               ["All" gnus-article-hide t]
1595               ["Headers" gnus-article-toggle-headers t]
1596               ["Signature" gnus-article-hide-signature t]
1597               ["Citation" gnus-article-hide-citation t]
1598               ["PGP" gnus-article-hide-pgp t]
1599               ["Banner" gnus-article-strip-banner t]
1600               ["Boring headers" gnus-article-hide-boring-headers t])
1601              ("Highlight"
1602               ["All" gnus-article-highlight t]
1603               ["Headers" gnus-article-highlight-headers t]
1604               ["Signature" gnus-article-highlight-signature t]
1605               ["Citation" gnus-article-highlight-citation t])
1606              ("Date"
1607               ["Local" gnus-article-date-local t]
1608               ["ISO8601" gnus-article-date-iso8601 t]
1609               ["UT" gnus-article-date-ut t]
1610               ["Original" gnus-article-date-original t]
1611               ["Lapsed" gnus-article-date-lapsed t]
1612               ["User-defined" gnus-article-date-user t])
1613              ("Washing"
1614               ("Remove Blanks"
1615                ["Leading" gnus-article-strip-leading-blank-lines t]
1616                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1617                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1618                ["All of the above" gnus-article-strip-blank-lines t]
1619                ["All" gnus-article-strip-all-blank-lines t]
1620                ["Leading space" gnus-article-strip-leading-space t]
1621                ["Trailing space" gnus-article-strip-trailing-space t])
1622               ["Overstrike" gnus-article-treat-overstrike t]
1623               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1624               ["Emphasis" gnus-article-emphasize t]
1625               ["Word wrap" gnus-article-fill-cited-article t]
1626               ["Fill long lines" gnus-article-fill-long-lines t]
1627               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1628               ["CR" gnus-article-remove-cr t]
1629               ["Show X-Face" gnus-article-display-x-face t]
1630               ["Rot 13" gnus-summary-caesar-message t]
1631               ["Unix pipe" gnus-summary-pipe-message t]
1632               ["Add buttons" gnus-article-add-buttons t]
1633               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1634               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1635               ["Toggle MIME" gnus-summary-toggle-mime t]
1636               ["Verbose header" gnus-summary-verbose-headers t]
1637               ["Toggle header" gnus-summary-toggle-header t])
1638              ("Output"
1639               ["Save in default format" gnus-summary-save-article t]
1640               ["Save in file" gnus-summary-save-article-file t]
1641               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1642               ["Save in MH folder" gnus-summary-save-article-folder t]
1643               ["Save in VM folder" gnus-summary-save-article-vm t]
1644               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1645               ["Save body in file" gnus-summary-save-article-body-file t]
1646               ["Pipe through a filter" gnus-summary-pipe-output t]
1647               ["Add to SOUP packet" gnus-soup-add-article t]
1648               ["Print" gnus-summary-print-article t])
1649              ("Backend"
1650               ["Respool article..." gnus-summary-respool-article t]
1651               ["Move article..." gnus-summary-move-article
1652                (gnus-check-backend-function
1653                 'request-move-article gnus-newsgroup-name)]
1654               ["Copy article..." gnus-summary-copy-article t]
1655               ["Crosspost article..." gnus-summary-crosspost-article
1656                (gnus-check-backend-function
1657                 'request-replace-article gnus-newsgroup-name)]
1658               ["Import file..." gnus-summary-import-article t]
1659               ["Check if posted" gnus-summary-article-posted-p t]
1660               ["Edit article" gnus-summary-edit-article
1661                (not (gnus-group-read-only-p))]
1662               ["Delete article" gnus-summary-delete-article
1663                (gnus-check-backend-function
1664                 'request-expire-articles gnus-newsgroup-name)]
1665               ["Query respool" gnus-summary-respool-query t]
1666               ["Trace respool" gnus-summary-respool-trace t]
1667               ["Delete expirable articles" gnus-summary-expire-articles-now
1668                (gnus-check-backend-function
1669                 'request-expire-articles gnus-newsgroup-name)])
1670              ("Extract"
1671               ["Uudecode" gnus-uu-decode-uu t]
1672               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1673               ["Unshar" gnus-uu-decode-unshar t]
1674               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1675               ["Save" gnus-uu-decode-save t]
1676               ["Binhex" gnus-uu-decode-binhex t]
1677               ["Postscript" gnus-uu-decode-postscript t])
1678              ("Cache"
1679               ["Enter article" gnus-cache-enter-article t]
1680               ["Remove article" gnus-cache-remove-article t])
1681              ["Translate" gnus-article-babel t]
1682              ["Select article buffer" gnus-summary-select-article-buffer t]
1683              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1684              ["Isearch article..." gnus-summary-isearch-article t]
1685              ["Beginning of the article" gnus-summary-beginning-of-article t]
1686              ["End of the article" gnus-summary-end-of-article t]
1687              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1688              ["Fetch referenced articles" gnus-summary-refer-references t]
1689              ["Fetch current thread" gnus-summary-refer-thread t]
1690              ["Fetch article with id..." gnus-summary-refer-article t]
1691              ["Redisplay" gnus-summary-show-article t])))
1692       (easy-menu-define
1693        gnus-summary-article-menu gnus-summary-mode-map ""
1694        (cons "Article" innards))
1695
1696       (easy-menu-define
1697        gnus-article-commands-menu gnus-article-mode-map ""
1698        (cons "Commands" innards)))
1699
1700     (easy-menu-define
1701      gnus-summary-thread-menu gnus-summary-mode-map ""
1702      '("Threads"
1703        ["Toggle threading" gnus-summary-toggle-threads t]
1704        ["Hide threads" gnus-summary-hide-all-threads t]
1705        ["Show threads" gnus-summary-show-all-threads t]
1706        ["Hide thread" gnus-summary-hide-thread t]
1707        ["Show thread" gnus-summary-show-thread t]
1708        ["Go to next thread" gnus-summary-next-thread t]
1709        ["Go to previous thread" gnus-summary-prev-thread t]
1710        ["Go down thread" gnus-summary-down-thread t]
1711        ["Go up thread" gnus-summary-up-thread t]
1712        ["Top of thread" gnus-summary-top-thread t]
1713        ["Mark thread as read" gnus-summary-kill-thread t]
1714        ["Lower thread score" gnus-summary-lower-thread t]
1715        ["Raise thread score" gnus-summary-raise-thread t]
1716        ["Rethread current" gnus-summary-rethread-current t]
1717        ))
1718
1719     (easy-menu-define
1720      gnus-summary-post-menu gnus-summary-mode-map ""
1721      '("Post"
1722        ["Post an article" gnus-summary-post-news t]
1723        ["Followup" gnus-summary-followup t]
1724        ["Followup and yank" gnus-summary-followup-with-original t]
1725        ["Supersede article" gnus-summary-supersede-article t]
1726        ["Cancel article" gnus-summary-cancel-article t]
1727        ["Reply" gnus-summary-reply t]
1728        ["Reply and yank" gnus-summary-reply-with-original t]
1729        ["Wide reply" gnus-summary-wide-reply t]
1730        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1731        ["Mail forward" gnus-summary-mail-forward t]
1732        ["Post forward" gnus-summary-post-forward t]
1733        ["Digest and mail" gnus-summary-mail-digest t]
1734        ["Digest and post" gnus-summary-post-digest t]
1735        ["Resend message" gnus-summary-resend-message t]
1736        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1737        ["Send a mail" gnus-summary-mail-other-window t]
1738        ["Uuencode and post" gnus-uu-post-news t]
1739        ["Followup via news" gnus-summary-followup-to-mail t]
1740        ["Followup via news and yank"
1741         gnus-summary-followup-to-mail-with-original t]
1742        ;;("Draft"
1743        ;;["Send" gnus-summary-send-draft t]
1744        ;;["Send bounced" gnus-resend-bounced-mail t])
1745        ))
1746
1747     (easy-menu-define
1748      gnus-summary-misc-menu gnus-summary-mode-map ""
1749      '("Misc"
1750        ("Mark Read"
1751         ["Mark as read" gnus-summary-mark-as-read-forward t]
1752         ["Mark same subject and select"
1753          gnus-summary-kill-same-subject-and-select t]
1754         ["Mark same subject" gnus-summary-kill-same-subject t]
1755         ["Catchup" gnus-summary-catchup t]
1756         ["Catchup all" gnus-summary-catchup-all t]
1757         ["Catchup to here" gnus-summary-catchup-to-here t]
1758         ["Catchup region" gnus-summary-mark-region-as-read t]
1759         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1760        ("Mark Various"
1761         ["Tick" gnus-summary-tick-article-forward t]
1762         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1763         ["Remove marks" gnus-summary-clear-mark-forward t]
1764         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1765         ["Set bookmark" gnus-summary-set-bookmark t]
1766         ["Remove bookmark" gnus-summary-remove-bookmark t])
1767        ("Mark Limit"
1768         ["Marks..." gnus-summary-limit-to-marks t]
1769         ["Subject..." gnus-summary-limit-to-subject t]
1770         ["Author..." gnus-summary-limit-to-author t]
1771         ["Age..." gnus-summary-limit-to-age t]
1772         ["Extra..." gnus-summary-limit-to-extra t]
1773         ["Score" gnus-summary-limit-to-score t]
1774         ["Unread" gnus-summary-limit-to-unread t]
1775         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1776         ["Articles" gnus-summary-limit-to-articles t]
1777         ["Pop limit" gnus-summary-pop-limit t]
1778         ["Show dormant" gnus-summary-limit-include-dormant t]
1779         ["Hide childless dormant"
1780          gnus-summary-limit-exclude-childless-dormant t]
1781         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1782         ["Hide marked" gnus-summary-limit-exclude-marks t]
1783         ["Show expunged" gnus-summary-show-all-expunged t])
1784        ("Process Mark"
1785         ["Set mark" gnus-summary-mark-as-processable t]
1786         ["Remove mark" gnus-summary-unmark-as-processable t]
1787         ["Remove all marks" gnus-summary-unmark-all-processable t]
1788         ["Mark above" gnus-uu-mark-over t]
1789         ["Mark series" gnus-uu-mark-series t]
1790         ["Mark region" gnus-uu-mark-region t]
1791         ["Unmark region" gnus-uu-unmark-region t]
1792         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1793         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1794         ["Mark all" gnus-uu-mark-all t]
1795         ["Mark buffer" gnus-uu-mark-buffer t]
1796         ["Mark sparse" gnus-uu-mark-sparse t]
1797         ["Mark thread" gnus-uu-mark-thread t]
1798         ["Unmark thread" gnus-uu-unmark-thread t]
1799         ("Process Mark Sets"
1800          ["Kill" gnus-summary-kill-process-mark t]
1801          ["Yank" gnus-summary-yank-process-mark
1802           gnus-newsgroup-process-stack]
1803          ["Save" gnus-summary-save-process-mark t]))
1804        ("Scroll article"
1805         ["Page forward" gnus-summary-next-page t]
1806         ["Page backward" gnus-summary-prev-page t]
1807         ["Line forward" gnus-summary-scroll-up t])
1808        ("Move"
1809         ["Next unread article" gnus-summary-next-unread-article t]
1810         ["Previous unread article" gnus-summary-prev-unread-article t]
1811         ["Next article" gnus-summary-next-article t]
1812         ["Previous article" gnus-summary-prev-article t]
1813         ["Next unread subject" gnus-summary-next-unread-subject t]
1814         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1815         ["Next article same subject" gnus-summary-next-same-subject t]
1816         ["Previous article same subject" gnus-summary-prev-same-subject t]
1817         ["First unread article" gnus-summary-first-unread-article t]
1818         ["Best unread article" gnus-summary-best-unread-article t]
1819         ["Go to subject number..." gnus-summary-goto-subject t]
1820         ["Go to article number..." gnus-summary-goto-article t]
1821         ["Go to the last article" gnus-summary-goto-last-article t]
1822         ["Pop article off history" gnus-summary-pop-article t])
1823        ("Sort"
1824         ["Sort by number" gnus-summary-sort-by-number t]
1825         ["Sort by author" gnus-summary-sort-by-author t]
1826         ["Sort by subject" gnus-summary-sort-by-subject t]
1827         ["Sort by date" gnus-summary-sort-by-date t]
1828         ["Sort by score" gnus-summary-sort-by-score t]
1829         ["Sort by lines" gnus-summary-sort-by-lines t]
1830         ["Sort by characters" gnus-summary-sort-by-chars t])
1831        ("Help"
1832         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1833         ["Describe group" gnus-summary-describe-group t]
1834         ["Read manual" gnus-info-find-node t])
1835        ("Modes"
1836         ["Pick and read" gnus-pick-mode t]
1837         ["Binary" gnus-binary-mode t])
1838        ("Regeneration"
1839         ["Regenerate" gnus-summary-prepare t]
1840         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1841         ["Toggle threading" gnus-summary-toggle-threads t])
1842        ["Filter articles..." gnus-summary-execute-command t]
1843        ["Run command on subjects..." gnus-summary-universal-argument t]
1844        ["Search articles forward..." gnus-summary-search-article-forward t]
1845        ["Search articles backward..." gnus-summary-search-article-backward t]
1846        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1847        ["Expand window" gnus-summary-expand-window t]
1848        ["Expire expirable articles" gnus-summary-expire-articles
1849         (gnus-check-backend-function
1850          'request-expire-articles gnus-newsgroup-name)]
1851        ["Edit local kill file" gnus-summary-edit-local-kill t]
1852        ["Edit main kill file" gnus-summary-edit-global-kill t]
1853        ["Edit group parameters" gnus-summary-edit-parameters t]
1854        ["Customize group parameters" gnus-summary-customize-parameters t]
1855        ["Send a bug report" gnus-bug t]
1856        ("Exit"
1857         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1858         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1859         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1860         ["Exit group" gnus-summary-exit t]
1861         ["Exit group without updating" gnus-summary-exit-no-update t]
1862         ["Exit and goto next group" gnus-summary-next-group t]
1863         ["Exit and goto prev group" gnus-summary-prev-group t]
1864         ["Reselect group" gnus-summary-reselect-current-group t]
1865         ["Rescan group" gnus-summary-rescan-group t]
1866         ["Update dribble" gnus-summary-save-newsrc t])))
1867
1868     (gnus-run-hooks 'gnus-summary-menu-hook)))
1869
1870 (defun gnus-score-set-default (var value)
1871   "A version of set that updates the GNU Emacs menu-bar."
1872   (set var value)
1873   ;; It is the message that forces the active status to be updated.
1874   (message ""))
1875
1876 (defun gnus-make-score-map (type)
1877   "Make a summary score map of type TYPE."
1878   (if t
1879       nil
1880     (let ((headers '(("author" "from" string)
1881                      ("subject" "subject" string)
1882                      ("article body" "body" string)
1883                      ("article head" "head" string)
1884                      ("xref" "xref" string)
1885                      ("extra header" "extra" string)
1886                      ("lines" "lines" number)
1887                      ("followups to author" "followup" string)))
1888           (types '((number ("less than" <)
1889                            ("greater than" >)
1890                            ("equal" =))
1891                    (string ("substring" s)
1892                            ("exact string" e)
1893                            ("fuzzy string" f)
1894                            ("regexp" r))))
1895           (perms '(("temporary" (current-time-string))
1896                    ("permanent" nil)
1897                    ("immediate" now)))
1898           header)
1899       (list
1900        (apply
1901         'nconc
1902         (list
1903          (if (eq type 'lower)
1904              "Lower score"
1905            "Increase score"))
1906         (let (outh)
1907           (while headers
1908             (setq header (car headers))
1909             (setq outh
1910                   (cons
1911                    (apply
1912                     'nconc
1913                     (list (car header))
1914                     (let ((ts (cdr (assoc (nth 2 header) types)))
1915                           outt)
1916                       (while ts
1917                         (setq outt
1918                               (cons
1919                                (apply
1920                                 'nconc
1921                                 (list (caar ts))
1922                                 (let ((ps perms)
1923                                       outp)
1924                                   (while ps
1925                                     (setq outp
1926                                           (cons
1927                                            (vector
1928                                             (caar ps)
1929                                             (list
1930                                              'gnus-summary-score-entry
1931                                              (nth 1 header)
1932                                              (if (or (string= (nth 1 header)
1933                                                               "head")
1934                                                      (string= (nth 1 header)
1935                                                               "body"))
1936                                                  ""
1937                                                (list 'gnus-summary-header
1938                                                      (nth 1 header)))
1939                                              (list 'quote (nth 1 (car ts)))
1940                                              (list 'gnus-score-default nil)
1941                                              (nth 1 (car ps))
1942                                              t)
1943                                             t)
1944                                            outp))
1945                                     (setq ps (cdr ps)))
1946                                   (list (nreverse outp))))
1947                                outt))
1948                         (setq ts (cdr ts)))
1949                       (list (nreverse outt))))
1950                    outh))
1951             (setq headers (cdr headers)))
1952           (list (nreverse outh))))))))
1953
1954 \f
1955
1956 (defun gnus-summary-mode (&optional group)
1957   "Major mode for reading articles.
1958
1959 All normal editing commands are switched off.
1960 \\<gnus-summary-mode-map>
1961 Each line in this buffer represents one article.  To read an
1962 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1963 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1964 respectively.
1965
1966 You can also post articles and send mail from this buffer.  To
1967 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1968 of an article, type `\\[gnus-summary-reply]'.
1969
1970 There are approx. one gazillion commands you can execute in this
1971 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1972
1973 The following commands are available:
1974
1975 \\{gnus-summary-mode-map}"
1976   (interactive)
1977   (when (gnus-visual-p 'summary-menu 'menu)
1978     (gnus-summary-make-menu-bar))
1979   (kill-all-local-variables)
1980   (gnus-summary-make-local-variables)
1981   (gnus-make-thread-indent-array)
1982   (gnus-simplify-mode-line)
1983   (setq major-mode 'gnus-summary-mode)
1984   (setq mode-name "Summary")
1985   (make-local-variable 'minor-mode-alist)
1986   (use-local-map gnus-summary-mode-map)
1987   (buffer-disable-undo)
1988   (setq buffer-read-only t)             ;Disable modification
1989   (setq truncate-lines t)
1990   (setq selective-display t)
1991   (setq selective-display-ellipses t)   ;Display `...'
1992   (gnus-summary-set-display-table)
1993   (gnus-set-default-directory)
1994   (setq gnus-newsgroup-name group)
1995   (make-local-variable 'gnus-summary-line-format)
1996   (make-local-variable 'gnus-summary-line-format-spec)
1997   (make-local-variable 'gnus-summary-dummy-line-format)
1998   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1999   (make-local-variable 'gnus-summary-mark-positions)
2000   (make-local-hook 'pre-command-hook)
2001   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2002   (gnus-run-hooks 'gnus-summary-mode-hook)
2003   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2004   (gnus-update-summary-mark-positions))
2005
2006 (defun gnus-summary-make-local-variables ()
2007   "Make all the local summary buffer variables."
2008   (let (global)
2009     (dolist (local gnus-summary-local-variables)
2010       (if (consp local)
2011           (progn
2012             (if (eq (cdr local) 'global)
2013                 ;; Copy the global value of the variable.
2014                 (setq global (symbol-value (car local)))
2015               ;; Use the value from the list.
2016               (setq global (eval (cdr local))))
2017             (set (make-local-variable (car local)) global))
2018         ;; Simple nil-valued local variable.
2019         (set (make-local-variable local) nil)))))
2020
2021 (defun gnus-summary-clear-local-variables ()
2022   (let ((locals gnus-summary-local-variables))
2023     (while locals
2024       (if (consp (car locals))
2025           (and (vectorp (caar locals))
2026                (set (caar locals) nil))
2027         (and (vectorp (car locals))
2028              (set (car locals) nil)))
2029       (setq locals (cdr locals)))))
2030
2031 ;; Summary data functions.
2032
2033 (defmacro gnus-data-number (data)
2034   `(car ,data))
2035
2036 (defmacro gnus-data-set-number (data number)
2037   `(setcar ,data ,number))
2038
2039 (defmacro gnus-data-mark (data)
2040   `(nth 1 ,data))
2041
2042 (defmacro gnus-data-set-mark (data mark)
2043   `(setcar (nthcdr 1 ,data) ,mark))
2044
2045 (defmacro gnus-data-pos (data)
2046   `(nth 2 ,data))
2047
2048 (defmacro gnus-data-set-pos (data pos)
2049   `(setcar (nthcdr 2 ,data) ,pos))
2050
2051 (defmacro gnus-data-header (data)
2052   `(nth 3 ,data))
2053
2054 (defmacro gnus-data-set-header (data header)
2055   `(setf (nth 3 ,data) ,header))
2056
2057 (defmacro gnus-data-level (data)
2058   `(nth 4 ,data))
2059
2060 (defmacro gnus-data-unread-p (data)
2061   `(= (nth 1 ,data) gnus-unread-mark))
2062
2063 (defmacro gnus-data-read-p (data)
2064   `(/= (nth 1 ,data) gnus-unread-mark))
2065
2066 (defmacro gnus-data-pseudo-p (data)
2067   `(consp (nth 3 ,data)))
2068
2069 (defmacro gnus-data-find (number)
2070   `(assq ,number gnus-newsgroup-data))
2071
2072 (defmacro gnus-data-find-list (number &optional data)
2073   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2074      (memq (assq ,number bdata)
2075            bdata)))
2076
2077 (defmacro gnus-data-make (number mark pos header level)
2078   `(list ,number ,mark ,pos ,header ,level))
2079
2080 (defun gnus-data-enter (after-article number mark pos header level offset)
2081   (let ((data (gnus-data-find-list after-article)))
2082     (unless data
2083       (error "No such article: %d" after-article))
2084     (setcdr data (cons (gnus-data-make number mark pos header level)
2085                        (cdr data)))
2086     (setq gnus-newsgroup-data-reverse nil)
2087     (gnus-data-update-list (cddr data) offset)))
2088
2089 (defun gnus-data-enter-list (after-article list &optional offset)
2090   (when list
2091     (let ((data (and after-article (gnus-data-find-list after-article)))
2092           (ilist list))
2093       (if (not (or data
2094                    after-article))
2095           (let ((odata gnus-newsgroup-data))
2096             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2097             (when offset
2098               (gnus-data-update-list odata offset)))
2099         ;; Find the last element in the list to be spliced into the main
2100         ;; list.
2101         (while (cdr list)
2102           (setq list (cdr list)))
2103         (if (not data)
2104             (progn
2105               (setcdr list gnus-newsgroup-data)
2106               (setq gnus-newsgroup-data ilist)
2107               (when offset
2108                 (gnus-data-update-list (cdr list) offset)))
2109           (setcdr list (cdr data))
2110           (setcdr data ilist)
2111           (when offset
2112             (gnus-data-update-list (cdr list) offset))))
2113       (setq gnus-newsgroup-data-reverse nil))))
2114
2115 (defun gnus-data-remove (article &optional offset)
2116   (let ((data gnus-newsgroup-data))
2117     (if (= (gnus-data-number (car data)) article)
2118         (progn
2119           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2120                 gnus-newsgroup-data-reverse nil)
2121           (when offset
2122             (gnus-data-update-list gnus-newsgroup-data offset)))
2123       (while (cdr data)
2124         (when (= (gnus-data-number (cadr data)) article)
2125           (setcdr data (cddr data))
2126           (when offset
2127             (gnus-data-update-list (cdr data) offset))
2128           (setq data nil
2129                 gnus-newsgroup-data-reverse nil))
2130         (setq data (cdr data))))))
2131
2132 (defmacro gnus-data-list (backward)
2133   `(if ,backward
2134        (or gnus-newsgroup-data-reverse
2135            (setq gnus-newsgroup-data-reverse
2136                  (reverse gnus-newsgroup-data)))
2137      gnus-newsgroup-data))
2138
2139 (defun gnus-data-update-list (data offset)
2140   "Add OFFSET to the POS of all data entries in DATA."
2141   (setq gnus-newsgroup-data-reverse nil)
2142   (while data
2143     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2144     (setq data (cdr data))))
2145
2146 (defun gnus-summary-article-pseudo-p (article)
2147   "Say whether this article is a pseudo article or not."
2148   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2149
2150 (defmacro gnus-summary-article-sparse-p (article)
2151   "Say whether this article is a sparse article or not."
2152   `(memq ,article gnus-newsgroup-sparse))
2153
2154 (defmacro gnus-summary-article-ancient-p (article)
2155   "Say whether this article is a sparse article or not."
2156   `(memq ,article gnus-newsgroup-ancient))
2157
2158 (defun gnus-article-parent-p (number)
2159   "Say whether this article is a parent or not."
2160   (let ((data (gnus-data-find-list number)))
2161     (and (cdr data)                     ; There has to be an article after...
2162          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2163             (gnus-data-level (nth 1 data))))))
2164
2165 (defun gnus-article-children (number)
2166   "Return a list of all children to NUMBER."
2167   (let* ((data (gnus-data-find-list number))
2168          (level (gnus-data-level (car data)))
2169          children)
2170     (setq data (cdr data))
2171     (while (and data
2172                 (= (gnus-data-level (car data)) (1+ level)))
2173       (push (gnus-data-number (car data)) children)
2174       (setq data (cdr data)))
2175     children))
2176
2177 (defmacro gnus-summary-skip-intangible ()
2178   "If the current article is intangible, then jump to a different article."
2179   '(let ((to (get-text-property (point) 'gnus-intangible)))
2180      (and to (gnus-summary-goto-subject to))))
2181
2182 (defmacro gnus-summary-article-intangible-p ()
2183   "Say whether this article is intangible or not."
2184   '(get-text-property (point) 'gnus-intangible))
2185
2186 (defun gnus-article-read-p (article)
2187   "Say whether ARTICLE is read or not."
2188   (not (or (memq article gnus-newsgroup-marked)
2189            (memq article gnus-newsgroup-unreads)
2190            (memq article gnus-newsgroup-unselected)
2191            (memq article gnus-newsgroup-dormant))))
2192
2193 ;; Some summary mode macros.
2194
2195 (defmacro gnus-summary-article-number ()
2196   "The article number of the article on the current line.
2197 If there isn's an article number here, then we return the current
2198 article number."
2199   '(progn
2200      (gnus-summary-skip-intangible)
2201      (or (get-text-property (point) 'gnus-number)
2202          (gnus-summary-last-subject))))
2203
2204 (defmacro gnus-summary-article-header (&optional number)
2205   "Return the header of article NUMBER."
2206   `(gnus-data-header (gnus-data-find
2207                       ,(or number '(gnus-summary-article-number)))))
2208
2209 (defmacro gnus-summary-thread-level (&optional number)
2210   "Return the level of thread that starts with article NUMBER."
2211   `(if (and (eq gnus-summary-make-false-root 'dummy)
2212             (get-text-property (point) 'gnus-intangible))
2213        0
2214      (gnus-data-level (gnus-data-find
2215                        ,(or number '(gnus-summary-article-number))))))
2216
2217 (defmacro gnus-summary-article-mark (&optional number)
2218   "Return the mark of article NUMBER."
2219   `(gnus-data-mark (gnus-data-find
2220                     ,(or number '(gnus-summary-article-number)))))
2221
2222 (defmacro gnus-summary-article-pos (&optional number)
2223   "Return the position of the line of article NUMBER."
2224   `(gnus-data-pos (gnus-data-find
2225                    ,(or number '(gnus-summary-article-number)))))
2226
2227 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2228 (defmacro gnus-summary-article-subject (&optional number)
2229   "Return current subject string or nil if nothing."
2230   `(let ((headers
2231           ,(if number
2232                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2233              '(gnus-data-header (assq (gnus-summary-article-number)
2234                                       gnus-newsgroup-data)))))
2235      (and headers
2236           (vectorp headers)
2237           (mail-header-subject headers))))
2238
2239 (defmacro gnus-summary-article-score (&optional number)
2240   "Return current article score."
2241   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2242                   gnus-newsgroup-scored))
2243        gnus-summary-default-score 0))
2244
2245 (defun gnus-summary-article-children (&optional number)
2246   "Return a list of article numbers that are children of article NUMBER."
2247   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2248          (level (gnus-data-level (car data)))
2249          l children)
2250     (while (and (setq data (cdr data))
2251                 (> (setq l (gnus-data-level (car data))) level))
2252       (and (= (1+ level) l)
2253            (push (gnus-data-number (car data))
2254                  children)))
2255     (nreverse children)))
2256
2257 (defun gnus-summary-article-parent (&optional number)
2258   "Return the article number of the parent of article NUMBER."
2259   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2260                                     (gnus-data-list t)))
2261          (level (gnus-data-level (car data))))
2262     (if (zerop level)
2263         ()                              ; This is a root.
2264       ;; We search until we find an article with a level less than
2265       ;; this one.  That function has to be the parent.
2266       (while (and (setq data (cdr data))
2267                   (not (< (gnus-data-level (car data)) level))))
2268       (and data (gnus-data-number (car data))))))
2269
2270 (defun gnus-unread-mark-p (mark)
2271   "Say whether MARK is the unread mark."
2272   (= mark gnus-unread-mark))
2273
2274 (defun gnus-read-mark-p (mark)
2275   "Say whether MARK is one of the marks that mark as read.
2276 This is all marks except unread, ticked, dormant, and expirable."
2277   (not (or (= mark gnus-unread-mark)
2278            (= mark gnus-ticked-mark)
2279            (= mark gnus-dormant-mark)
2280            (= mark gnus-expirable-mark))))
2281
2282 (defmacro gnus-article-mark (number)
2283   "Return the MARK of article NUMBER.
2284 This macro should only be used when computing the mark the \"first\"
2285 time; i.e., when generating the summary lines.  After that,
2286 `gnus-summary-article-mark' should be used to examine the
2287 marks of articles."
2288   `(cond
2289     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2290     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2291     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2292     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2293     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2294     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2295     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2296     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2297            gnus-ancient-mark))))
2298
2299 ;; Saving hidden threads.
2300
2301 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2302 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2303
2304 (defmacro gnus-save-hidden-threads (&rest forms)
2305   "Save hidden threads, eval FORMS, and restore the hidden threads."
2306   (let ((config (make-symbol "config")))
2307     `(let ((,config (gnus-hidden-threads-configuration)))
2308        (unwind-protect
2309            (save-excursion
2310              ,@forms)
2311          (gnus-restore-hidden-threads-configuration ,config)))))
2312
2313 (defun gnus-data-compute-positions ()
2314   "Compute the positions of all articles."
2315   (setq gnus-newsgroup-data-reverse nil)
2316   (let ((data gnus-newsgroup-data))
2317     (save-excursion
2318       (gnus-save-hidden-threads
2319         (gnus-summary-show-all-threads)
2320         (goto-char (point-min))
2321         (while data
2322           (while (get-text-property (point) 'gnus-intangible)
2323             (forward-line 1))
2324           (gnus-data-set-pos (car data) (+ (point) 3))
2325           (setq data (cdr data))
2326           (forward-line 1))))))
2327
2328 (defun gnus-hidden-threads-configuration ()
2329   "Return the current hidden threads configuration."
2330   (save-excursion
2331     (let (config)
2332       (goto-char (point-min))
2333       (while (search-forward "\r" nil t)
2334         (push (1- (point)) config))
2335       config)))
2336
2337 (defun gnus-restore-hidden-threads-configuration (config)
2338   "Restore hidden threads configuration from CONFIG."
2339   (let (point buffer-read-only)
2340     (while (setq point (pop config))
2341       (when (and (< point (point-max))
2342                  (goto-char point)
2343                  (eq (char-after) ?\n))
2344         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2345
2346 ;; Various summary mode internalish functions.
2347
2348 (defun gnus-mouse-pick-article (e)
2349   (interactive "e")
2350   (mouse-set-point e)
2351   (gnus-summary-next-page nil t))
2352
2353 (defun gnus-summary-set-display-table ()
2354   ;; Change the display table.  Odd characters have a tendency to mess
2355   ;; up nicely formatted displays - we make all possible glyphs
2356   ;; display only a single character.
2357
2358   ;; We start from the standard display table, if any.
2359   (let ((table (or (copy-sequence standard-display-table)
2360                    (make-display-table)))
2361         (i 32))
2362     ;; Nix out all the control chars...
2363     (while (>= (setq i (1- i)) 0)
2364       (aset table i [??]))
2365     ;; ... but not newline and cr, of course.  (cr is necessary for the
2366     ;; selective display).
2367     (aset table ?\n nil)
2368     (aset table ?\r nil)
2369     ;; We keep TAB as well.
2370     (aset table ?\t nil)
2371     ;; We nix out any glyphs over 126 that are not set already.
2372     (let ((i 256))
2373       (while (>= (setq i (1- i)) 127)
2374         ;; Only modify if the entry is nil.
2375         (unless (aref table i)
2376           (aset table i [??]))))
2377     (setq buffer-display-table table)))
2378
2379 (defun gnus-summary-setup-buffer (group)
2380   "Initialize summary buffer."
2381   (let ((buffer (concat "*Summary " group "*")))
2382     (if (get-buffer buffer)
2383         (progn
2384           (set-buffer buffer)
2385           (setq gnus-summary-buffer (current-buffer))
2386           (not gnus-newsgroup-prepared))
2387       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2388       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2389       (gnus-summary-mode group)
2390       (when gnus-carpal
2391         (gnus-carpal-setup-buffer 'summary))
2392       (unless gnus-single-article-buffer
2393         (make-local-variable 'gnus-article-buffer)
2394         (make-local-variable 'gnus-article-current)
2395         (make-local-variable 'gnus-original-article-buffer))
2396       (setq gnus-newsgroup-name group)
2397       t)))
2398
2399 (defun gnus-set-global-variables ()
2400   ;; Set the global equivalents of the summary buffer-local variables
2401   ;; to the latest values they had.  These reflect the summary buffer
2402   ;; that was in action when the last article was fetched.
2403   (when (eq major-mode 'gnus-summary-mode)
2404     (setq gnus-summary-buffer (current-buffer))
2405     (let ((name gnus-newsgroup-name)
2406           (marked gnus-newsgroup-marked)
2407           (unread gnus-newsgroup-unreads)
2408           (headers gnus-current-headers)
2409           (data gnus-newsgroup-data)
2410           (summary gnus-summary-buffer)
2411           (article-buffer gnus-article-buffer)
2412           (original gnus-original-article-buffer)
2413           (gac gnus-article-current)
2414           (reffed gnus-reffed-article-number)
2415           (score-file gnus-current-score-file)
2416           (default-charset gnus-newsgroup-charset))
2417       (save-excursion
2418         (set-buffer gnus-group-buffer)
2419         (setq gnus-newsgroup-name name
2420               gnus-newsgroup-marked marked
2421               gnus-newsgroup-unreads unread
2422               gnus-current-headers headers
2423               gnus-newsgroup-data data
2424               gnus-article-current gac
2425               gnus-summary-buffer summary
2426               gnus-article-buffer article-buffer
2427               gnus-original-article-buffer original
2428               gnus-reffed-article-number reffed
2429               gnus-current-score-file score-file
2430               gnus-newsgroup-charset default-charset)
2431         ;; The article buffer also has local variables.
2432         (when (gnus-buffer-live-p gnus-article-buffer)
2433           (set-buffer gnus-article-buffer)
2434           (setq gnus-summary-buffer summary))))))
2435
2436 (defun gnus-summary-article-unread-p (article)
2437   "Say whether ARTICLE is unread or not."
2438   (memq article gnus-newsgroup-unreads))
2439
2440 (defun gnus-summary-first-article-p (&optional article)
2441   "Return whether ARTICLE is the first article in the buffer."
2442   (if (not (setq article (or article (gnus-summary-article-number))))
2443       nil
2444     (eq article (caar gnus-newsgroup-data))))
2445
2446 (defun gnus-summary-last-article-p (&optional article)
2447   "Return whether ARTICLE is the last article in the buffer."
2448   (if (not (setq article (or article (gnus-summary-article-number))))
2449       ;; All non-existent numbers are the last article.  :-)
2450       t
2451     (not (cdr (gnus-data-find-list article)))))
2452
2453 (defun gnus-make-thread-indent-array ()
2454   (let ((n 200))
2455     (unless (and gnus-thread-indent-array
2456                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2457       (setq gnus-thread-indent-array (make-vector 201 "")
2458             gnus-thread-indent-array-level gnus-thread-indent-level)
2459       (while (>= n 0)
2460         (aset gnus-thread-indent-array n
2461               (make-string (* n gnus-thread-indent-level) ? ))
2462         (setq n (1- n))))))
2463
2464 (defun gnus-update-summary-mark-positions ()
2465   "Compute where the summary marks are to go."
2466   (save-excursion
2467     (when (gnus-buffer-exists-p gnus-summary-buffer)
2468       (set-buffer gnus-summary-buffer))
2469     (let ((gnus-replied-mark 129)
2470           (gnus-score-below-mark 130)
2471           (gnus-score-over-mark 130)
2472           (gnus-download-mark 131)
2473           (spec gnus-summary-line-format-spec)
2474           gnus-visual pos)
2475       (save-excursion
2476         (gnus-set-work-buffer)
2477         (let ((gnus-summary-line-format-spec spec)
2478               (gnus-newsgroup-downloadable '((0 . t))))
2479           (gnus-summary-insert-line
2480            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2481            0 nil 128 t nil "" nil 1)
2482           (goto-char (point-min))
2483           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2484                                              (- (point) 2)))))
2485           (goto-char (point-min))
2486           (push (cons 'replied (and (search-forward "\201" nil t)
2487                                     (- (point) 2)))
2488                 pos)
2489           (goto-char (point-min))
2490           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2491                 pos)
2492           (goto-char (point-min))
2493           (push (cons 'download
2494                       (and (search-forward "\203" nil t) (- (point) 2)))
2495                 pos)))
2496       (setq gnus-summary-mark-positions pos))))
2497
2498 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2499   "Insert a dummy root in the summary buffer."
2500   (beginning-of-line)
2501   (gnus-add-text-properties
2502    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2503    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2504
2505 (defun gnus-summary-from-or-to-or-newsgroups (header)
2506   (let ((to (cdr (assq 'To (mail-header-extra header))))
2507         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2508         (mail-parse-charset gnus-newsgroup-charset)
2509         (mail-parse-ignored-charsets 
2510          (save-excursion (set-buffer gnus-summary-buffer)
2511                          gnus-newsgroup-ignored-charsets)))
2512     (cond
2513      ((and to
2514            gnus-ignored-from-addresses
2515            (string-match gnus-ignored-from-addresses
2516                          (mail-header-from header)))
2517       (concat "-> "
2518               (or (car (funcall gnus-extract-address-components
2519                                 (funcall
2520                                  gnus-decode-encoded-word-function to)))
2521                   (funcall gnus-decode-encoded-word-function to))))
2522      ((and newsgroups
2523            gnus-ignored-from-addresses
2524            (string-match gnus-ignored-from-addresses
2525                          (mail-header-from header)))
2526       (concat "=> " newsgroups))
2527      (t
2528       (or (car (funcall gnus-extract-address-components
2529                         (mail-header-from header)))
2530           (mail-header-from header))))))
2531
2532 (defun gnus-summary-insert-line (gnus-tmp-header
2533                                  gnus-tmp-level gnus-tmp-current
2534                                  gnus-tmp-unread gnus-tmp-replied
2535                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2536                                  &optional gnus-tmp-dummy gnus-tmp-score
2537                                  gnus-tmp-process)
2538   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2539          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2540          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2541          (gnus-tmp-score-char
2542           (if (or (null gnus-summary-default-score)
2543                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2544                       gnus-summary-zcore-fuzz))
2545               ? ;Whitespace
2546             (if (< gnus-tmp-score gnus-summary-default-score)
2547                 gnus-score-below-mark gnus-score-over-mark)))
2548          (gnus-tmp-replied
2549           (cond (gnus-tmp-process gnus-process-mark)
2550                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2551                  gnus-cached-mark)
2552                 (gnus-tmp-replied gnus-replied-mark)
2553                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2554                  gnus-saved-mark)
2555                 (t gnus-unread-mark)))
2556          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2557          (gnus-tmp-name
2558           (cond
2559            ((string-match "<[^>]+> *$" gnus-tmp-from)
2560             (let ((beg (match-beginning 0)))
2561               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2562                        (substring gnus-tmp-from (1+ (match-beginning 0))
2563                                   (1- (match-end 0))))
2564                   (substring gnus-tmp-from 0 beg))))
2565            ((string-match "(.+)" gnus-tmp-from)
2566             (substring gnus-tmp-from
2567                        (1+ (match-beginning 0)) (1- (match-end 0))))
2568            (t gnus-tmp-from)))
2569          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2570          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2571          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2572          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2573          (buffer-read-only nil))
2574     (when (string= gnus-tmp-name "")
2575       (setq gnus-tmp-name gnus-tmp-from))
2576     (unless (numberp gnus-tmp-lines)
2577       (setq gnus-tmp-lines 0))
2578     (gnus-put-text-property-excluding-characters-with-faces
2579      (point)
2580      (progn (eval gnus-summary-line-format-spec) (point))
2581      'gnus-number gnus-tmp-number)
2582     (when (gnus-visual-p 'summary-highlight 'highlight)
2583       (forward-line -1)
2584       (gnus-run-hooks 'gnus-summary-update-hook)
2585       (forward-line 1))))
2586
2587 (defun gnus-summary-update-line (&optional dont-update)
2588   ;; Update summary line after change.
2589   (when (and gnus-summary-default-score
2590              (not gnus-summary-inhibit-highlight))
2591     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2592            (article (gnus-summary-article-number))
2593            (score (gnus-summary-article-score article)))
2594       (unless dont-update
2595         (if (and gnus-summary-mark-below
2596                  (< (gnus-summary-article-score)
2597                     gnus-summary-mark-below))
2598             ;; This article has a low score, so we mark it as read.
2599             (when (memq article gnus-newsgroup-unreads)
2600               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2601           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2602             ;; This article was previously marked as read on account
2603             ;; of a low score, but now it has risen, so we mark it as
2604             ;; unread.
2605             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2606         (gnus-summary-update-mark
2607          (if (or (null gnus-summary-default-score)
2608                  (<= (abs (- score gnus-summary-default-score))
2609                      gnus-summary-zcore-fuzz))
2610              ? ;Whitespace
2611            (if (< score gnus-summary-default-score)
2612                gnus-score-below-mark gnus-score-over-mark))
2613          'score))
2614       ;; Do visual highlighting.
2615       (when (gnus-visual-p 'summary-highlight 'highlight)
2616         (gnus-run-hooks 'gnus-summary-update-hook)))))
2617
2618 (defvar gnus-tmp-new-adopts nil)
2619
2620 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2621   "Return the number of articles in THREAD.
2622 This may be 0 in some cases -- if none of the articles in
2623 the thread are to be displayed."
2624   (let* ((number
2625           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2626           (cond
2627            ((not (listp thread))
2628             1)
2629            ((and (consp thread) (cdr thread))
2630             (apply
2631              '+ 1 (mapcar
2632                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2633            ((null thread)
2634             1)
2635            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2636             1)
2637            (t 0))))
2638     (when (and level (zerop level) gnus-tmp-new-adopts)
2639       (incf number
2640             (apply '+ (mapcar
2641                        'gnus-summary-number-of-articles-in-thread
2642                        gnus-tmp-new-adopts))))
2643     (if char
2644         (if (> number 1) gnus-not-empty-thread-mark
2645           gnus-empty-thread-mark)
2646       number)))
2647
2648 (defun gnus-summary-set-local-parameters (group)
2649   "Go through the local params of GROUP and set all variable specs in that list."
2650   (let ((params (gnus-group-find-parameter group))
2651         elem)
2652     (while params
2653       (setq elem (car params)
2654             params (cdr params))
2655       (and (consp elem)                 ; Has to be a cons.
2656            (consp (cdr elem))           ; The cdr has to be a list.
2657            (symbolp (car elem))         ; Has to be a symbol in there.
2658            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2659            (ignore-errors               ; So we set it.
2660              (make-local-variable (car elem))
2661              (set (car elem) (eval (nth 1 elem))))))))
2662
2663 (defun gnus-summary-read-group (group &optional show-all no-article
2664                                       kill-buffer no-display backward
2665                                       select-articles)
2666   "Start reading news in newsgroup GROUP.
2667 If SHOW-ALL is non-nil, already read articles are also listed.
2668 If NO-ARTICLE is non-nil, no article is selected initially.
2669 If NO-DISPLAY, don't generate a summary buffer."
2670   (let (result)
2671     (while (and group
2672                 (null (setq result
2673                             (let ((gnus-auto-select-next nil))
2674                               (or (gnus-summary-read-group-1
2675                                    group show-all no-article
2676                                    kill-buffer no-display
2677                                    select-articles)
2678                                   (setq show-all nil
2679                                         select-articles nil)))))
2680                 (eq gnus-auto-select-next 'quietly))
2681       (set-buffer gnus-group-buffer)
2682       ;; The entry function called above goes to the next
2683       ;; group automatically, so we go two groups back
2684       ;; if we are searching for the previous group.
2685       (when backward
2686         (gnus-group-prev-unread-group 2))
2687       (if (not (equal group (gnus-group-group-name)))
2688           (setq group (gnus-group-group-name))
2689         (setq group nil)))
2690     result))
2691
2692 (defun gnus-summary-read-group-1 (group show-all no-article
2693                                         kill-buffer no-display
2694                                         &optional select-articles)
2695   ;; Killed foreign groups can't be entered.
2696   (when (and (not (gnus-group-native-p group))
2697              (not (gnus-gethash group gnus-newsrc-hashtb)))
2698     (error "Dead non-native groups can't be entered"))
2699   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2700   (let* ((new-group (gnus-summary-setup-buffer group))
2701          (quit-config (gnus-group-quit-config group))
2702          (did-select (and new-group (gnus-select-newsgroup
2703                                      group show-all select-articles))))
2704     (cond
2705      ;; This summary buffer exists already, so we just select it.
2706      ((not new-group)
2707       (gnus-set-global-variables)
2708       (when kill-buffer
2709         (gnus-kill-or-deaden-summary kill-buffer))
2710       (gnus-configure-windows 'summary 'force)
2711       (gnus-set-mode-line 'summary)
2712       (gnus-summary-position-point)
2713       (message "")
2714       t)
2715      ;; We couldn't select this group.
2716      ((null did-select)
2717       (when (and (eq major-mode 'gnus-summary-mode)
2718                  (not (equal (current-buffer) kill-buffer)))
2719         (kill-buffer (current-buffer))
2720         (if (not quit-config)
2721             (progn
2722               ;; Update the info -- marks might need to be removed,
2723               ;; for instance.
2724               (gnus-summary-update-info)
2725               (set-buffer gnus-group-buffer)
2726               (gnus-group-jump-to-group group)
2727               (gnus-group-next-unread-group 1))
2728           (gnus-handle-ephemeral-exit quit-config)))
2729       (gnus-message 3 "Can't select group")
2730       nil)
2731      ;; The user did a `C-g' while prompting for number of articles,
2732      ;; so we exit this group.
2733      ((eq did-select 'quit)
2734       (and (eq major-mode 'gnus-summary-mode)
2735            (not (equal (current-buffer) kill-buffer))
2736            (kill-buffer (current-buffer)))
2737       (when kill-buffer
2738         (gnus-kill-or-deaden-summary kill-buffer))
2739       (if (not quit-config)
2740           (progn
2741             (set-buffer gnus-group-buffer)
2742             (gnus-group-jump-to-group group)
2743             (gnus-group-next-unread-group 1)
2744             (gnus-configure-windows 'group 'force))
2745         (gnus-handle-ephemeral-exit quit-config))
2746       ;; Finally signal the quit.
2747       (signal 'quit nil))
2748      ;; The group was successfully selected.
2749      (t
2750       (gnus-set-global-variables)
2751       ;; Save the active value in effect when the group was entered.
2752       (setq gnus-newsgroup-active
2753             (gnus-copy-sequence
2754              (gnus-active gnus-newsgroup-name)))
2755       ;; You can change the summary buffer in some way with this hook.
2756       (gnus-run-hooks 'gnus-select-group-hook)
2757       ;; Set any local variables in the group parameters.
2758       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2759       (gnus-update-format-specifications
2760        nil 'summary 'summary-mode 'summary-dummy)
2761       (gnus-update-summary-mark-positions)
2762       ;; Do score processing.
2763       (when gnus-use-scoring
2764         (gnus-possibly-score-headers))
2765       ;; Check whether to fill in the gaps in the threads.
2766       (when gnus-build-sparse-threads
2767         (gnus-build-sparse-threads))
2768       ;; Find the initial limit.
2769       (if gnus-show-threads
2770           (if show-all
2771               (let ((gnus-newsgroup-dormant nil))
2772                 (gnus-summary-initial-limit show-all))
2773             (gnus-summary-initial-limit show-all))
2774         (setq gnus-newsgroup-limit
2775               (mapcar
2776                (lambda (header) (mail-header-number header))
2777                gnus-newsgroup-headers)))
2778       ;; Generate the summary buffer.
2779       (unless no-display
2780         (gnus-summary-prepare))
2781       (when gnus-use-trees
2782         (gnus-tree-open group)
2783         (setq gnus-summary-highlight-line-function
2784               'gnus-tree-highlight-article))
2785       ;; If the summary buffer is empty, but there are some low-scored
2786       ;; articles or some excluded dormants, we include these in the
2787       ;; buffer.
2788       (when (and (zerop (buffer-size))
2789                  (not no-display))
2790         (cond (gnus-newsgroup-dormant
2791                (gnus-summary-limit-include-dormant))
2792               ((and gnus-newsgroup-scored show-all)
2793                (gnus-summary-limit-include-expunged t))))
2794       ;; Function `gnus-apply-kill-file' must be called in this hook.
2795       (gnus-run-hooks 'gnus-apply-kill-hook)
2796       (if (and (zerop (buffer-size))
2797                (not no-display))
2798           (progn
2799             ;; This newsgroup is empty.
2800             (gnus-summary-catchup-and-exit nil t)
2801             (gnus-message 6 "No unread news")
2802             (when kill-buffer
2803               (gnus-kill-or-deaden-summary kill-buffer))
2804             ;; Return nil from this function.
2805             nil)
2806         ;; Hide conversation thread subtrees.  We cannot do this in
2807         ;; gnus-summary-prepare-hook since kill processing may not
2808         ;; work with hidden articles.
2809         (and gnus-show-threads
2810              gnus-thread-hide-subtree
2811              (gnus-summary-hide-all-threads))
2812         (when kill-buffer
2813           (gnus-kill-or-deaden-summary kill-buffer))
2814         ;; Show first unread article if requested.
2815         (if (and (not no-article)
2816                  (not no-display)
2817                  gnus-newsgroup-unreads
2818                  gnus-auto-select-first)
2819             (progn
2820               (gnus-configure-windows 'summary)
2821               (cond
2822                ((eq gnus-auto-select-first 'best)
2823                 (gnus-summary-best-unread-article))
2824                ((eq gnus-auto-select-first t)
2825                 (gnus-summary-first-unread-article))
2826                ((gnus-functionp gnus-auto-select-first)
2827                 (funcall gnus-auto-select-first))))
2828           ;; Don't select any articles, just move point to the first
2829           ;; article in the group.
2830           (goto-char (point-min))
2831           (gnus-summary-position-point)
2832           (gnus-configure-windows 'summary 'force)
2833           (gnus-set-mode-line 'summary))
2834         (when (get-buffer-window gnus-group-buffer t)
2835           ;; Gotta use windows, because recenter does weird stuff if
2836           ;; the current buffer ain't the displayed window.
2837           (let ((owin (selected-window)))
2838             (select-window (get-buffer-window gnus-group-buffer t))
2839             (when (gnus-group-goto-group group)
2840               (recenter))
2841             (select-window owin)))
2842         ;; Mark this buffer as "prepared".
2843         (setq gnus-newsgroup-prepared t)
2844         (gnus-run-hooks 'gnus-summary-prepared-hook)
2845         t)))))
2846
2847 (defun gnus-summary-prepare ()
2848   "Generate the summary buffer."
2849   (interactive)
2850   (let ((buffer-read-only nil))
2851     (erase-buffer)
2852     (setq gnus-newsgroup-data nil
2853           gnus-newsgroup-data-reverse nil)
2854     (gnus-run-hooks 'gnus-summary-generate-hook)
2855     ;; Generate the buffer, either with threads or without.
2856     (when gnus-newsgroup-headers
2857       (gnus-summary-prepare-threads
2858        (if gnus-show-threads
2859            (gnus-sort-gathered-threads
2860             (funcall gnus-summary-thread-gathering-function
2861                      (gnus-sort-threads
2862                       (gnus-cut-threads (gnus-make-threads)))))
2863          ;; Unthreaded display.
2864          (gnus-sort-articles gnus-newsgroup-headers))))
2865     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2866     ;; Call hooks for modifying summary buffer.
2867     (goto-char (point-min))
2868     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2869
2870 (defsubst gnus-general-simplify-subject (subject)
2871   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2872   (setq subject
2873         (cond
2874          ;; Truncate the subject.
2875          (gnus-simplify-subject-functions
2876           (gnus-map-function gnus-simplify-subject-functions subject))
2877          ((numberp gnus-summary-gather-subject-limit)
2878           (setq subject (gnus-simplify-subject-re subject))
2879           (if (> (length subject) gnus-summary-gather-subject-limit)
2880               (substring subject 0 gnus-summary-gather-subject-limit)
2881             subject))
2882          ;; Fuzzily simplify it.
2883          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2884           (gnus-simplify-subject-fuzzy subject))
2885          ;; Just remove the leading "Re:".
2886          (t
2887           (gnus-simplify-subject-re subject))))
2888
2889   (if (and gnus-summary-gather-exclude-subject
2890            (string-match gnus-summary-gather-exclude-subject subject))
2891       nil                               ; This article shouldn't be gathered
2892     subject))
2893
2894 (defun gnus-summary-simplify-subject-query ()
2895   "Query where the respool algorithm would put this article."
2896   (interactive)
2897   (gnus-summary-select-article)
2898   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2899
2900 (defun gnus-gather-threads-by-subject (threads)
2901   "Gather threads by looking at Subject headers."
2902   (if (not gnus-summary-make-false-root)
2903       threads
2904     (let ((hashtb (gnus-make-hashtable 1024))
2905           (prev threads)
2906           (result threads)
2907           subject hthread whole-subject)
2908       (while threads
2909         (setq subject (gnus-general-simplify-subject
2910                        (setq whole-subject (mail-header-subject
2911                                             (caar threads)))))
2912         (when subject
2913           (if (setq hthread (gnus-gethash subject hashtb))
2914               (progn
2915                 ;; We enter a dummy root into the thread, if we
2916                 ;; haven't done that already.
2917                 (unless (stringp (caar hthread))
2918                   (setcar hthread (list whole-subject (car hthread))))
2919                 ;; We add this new gathered thread to this gathered
2920                 ;; thread.
2921                 (setcdr (car hthread)
2922                         (nconc (cdar hthread) (list (car threads))))
2923                 ;; Remove it from the list of threads.
2924                 (setcdr prev (cdr threads))
2925                 (setq threads prev))
2926             ;; Enter this thread into the hash table.
2927             (gnus-sethash subject threads hashtb)))
2928         (setq prev threads)
2929         (setq threads (cdr threads)))
2930       result)))
2931
2932 (defun gnus-gather-threads-by-references (threads)
2933   "Gather threads by looking at References headers."
2934   (let ((idhashtb (gnus-make-hashtable 1024))
2935         (thhashtb (gnus-make-hashtable 1024))
2936         (prev threads)
2937         (result threads)
2938         ids references id gthread gid entered ref)
2939     (while threads
2940       (when (setq references (mail-header-references (caar threads)))
2941         (setq id (mail-header-id (caar threads))
2942               ids (gnus-split-references references)
2943               entered nil)
2944         (while (setq ref (pop ids))
2945           (setq ids (delete ref ids))
2946           (if (not (setq gid (gnus-gethash ref idhashtb)))
2947               (progn
2948                 (gnus-sethash ref id idhashtb)
2949                 (gnus-sethash id threads thhashtb))
2950             (setq gthread (gnus-gethash gid thhashtb))
2951             (unless entered
2952               ;; We enter a dummy root into the thread, if we
2953               ;; haven't done that already.
2954               (unless (stringp (caar gthread))
2955                 (setcar gthread (list (mail-header-subject (caar gthread))
2956                                       (car gthread))))
2957               ;; We add this new gathered thread to this gathered
2958               ;; thread.
2959               (setcdr (car gthread)
2960                       (nconc (cdar gthread) (list (car threads)))))
2961             ;; Add it into the thread hash table.
2962             (gnus-sethash id gthread thhashtb)
2963             (setq entered t)
2964             ;; Remove it from the list of threads.
2965             (setcdr prev (cdr threads))
2966             (setq threads prev))))
2967       (setq prev threads)
2968       (setq threads (cdr threads)))
2969     result))
2970
2971 (defun gnus-sort-gathered-threads (threads)
2972   "Sort subtreads inside each gathered thread by article number."
2973   (let ((result threads))
2974     (while threads
2975       (when (stringp (caar threads))
2976         (setcdr (car threads)
2977                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2978       (setq threads (cdr threads)))
2979     result))
2980
2981 (defun gnus-thread-loop-p (root thread)
2982   "Say whether ROOT is in THREAD."
2983   (let ((stack (list thread))
2984         (infloop 0)
2985         th)
2986     (while (setq thread (pop stack))
2987       (setq th (cdr thread))
2988       (while (and th
2989                   (not (eq (caar th) root)))
2990         (pop th))
2991       (if th
2992           ;; We have found a loop.
2993           (let (ref-dep)
2994             (setcdr thread (delq (car th) (cdr thread)))
2995             (if (boundp (setq ref-dep (intern "none"
2996                                               gnus-newsgroup-dependencies)))
2997                 (setcdr (symbol-value ref-dep)
2998                         (nconc (cdr (symbol-value ref-dep))
2999                                (list (car th))))
3000               (set ref-dep (list nil (car th))))
3001             (setq infloop 1
3002                   stack nil))
3003         ;; Push all the subthreads onto the stack.
3004         (push (cdr thread) stack)))
3005     infloop))
3006
3007 (defun gnus-make-threads ()
3008   "Go through the dependency hashtb and find the roots.  Return all threads."
3009   (let (threads)
3010     (while (catch 'infloop
3011              (mapatoms
3012               (lambda (refs)
3013                 ;; Deal with self-referencing References loops.
3014                 (when (and (car (symbol-value refs))
3015                            (not (zerop
3016                                  (apply
3017                                   '+
3018                                   (mapcar
3019                                    (lambda (thread)
3020                                      (gnus-thread-loop-p
3021                                       (car (symbol-value refs)) thread))
3022                                    (cdr (symbol-value refs)))))))
3023                   (setq threads nil)
3024                   (throw 'infloop t))
3025                 (unless (car (symbol-value refs))
3026                   ;; These threads do not refer back to any other articles,
3027                   ;; so they're roots.
3028                   (setq threads (append (cdr (symbol-value refs)) threads))))
3029               gnus-newsgroup-dependencies)))
3030     threads))
3031
3032 ;; Build the thread tree.
3033 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3034   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3035
3036 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3037 if it was already present.
3038
3039 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3040 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3041 Message-IDs will be renamed be renamed to a unique Message-ID before
3042 being entered.
3043
3044 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3045   (let* ((id (mail-header-id header))
3046          (id-dep (and id (intern id dependencies)))
3047          ref ref-dep ref-header)
3048     ;; Enter this `header' in the `dependencies' table.
3049     (cond
3050      ((not id-dep)
3051       (setq header nil))
3052      ;; The first two cases do the normal part: enter a new `header'
3053      ;; in the `dependencies' table.
3054      ((not (boundp id-dep))
3055       (set id-dep (list header)))
3056      ((null (car (symbol-value id-dep)))
3057       (setcar (symbol-value id-dep) header))
3058
3059      ;; From here the `header' was already present in the
3060      ;; `dependencies' table.
3061      (force-new
3062       ;; Overrides an existing entry;
3063       ;; just set the header part of the entry.
3064       (setcar (symbol-value id-dep) header))
3065
3066      ;; Renames the existing `header' to a unique Message-ID.
3067      ((not gnus-summary-ignore-duplicates)
3068       ;; An article with this Message-ID has already been seen.
3069       ;; We rename the Message-ID.
3070       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3071            (list header))
3072       (mail-header-set-id header id))
3073
3074      ;; The last case ignores an existing entry, except it adds any
3075      ;; additional Xrefs (in case the two articles came from different
3076      ;; servers.
3077      ;; Also sets `header' to `nil' meaning that the `dependencies'
3078      ;; table was *not* modified.
3079      (t
3080       (mail-header-set-xref
3081        (car (symbol-value id-dep))
3082        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3083                    "")
3084                (or (mail-header-xref header) "")))
3085       (setq header nil)))
3086
3087     (when header
3088       ;; First check if that we are not creating a References loop.
3089       (setq ref (gnus-parent-id (mail-header-references header)))
3090       (while (and ref
3091                   (setq ref-dep (intern-soft ref dependencies))
3092                   (boundp ref-dep)
3093                   (setq ref-header (car (symbol-value ref-dep))))
3094         (if (string= id ref)
3095             ;; Yuk!  This is a reference loop.  Make the article be a
3096             ;; root article.
3097             (progn
3098               (mail-header-set-references (car (symbol-value id-dep)) "none")
3099               (setq ref nil))
3100           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3101       (setq ref (gnus-parent-id (mail-header-references header)))
3102       (setq ref-dep (intern (or ref "none") dependencies))
3103       (if (boundp ref-dep)
3104           (setcdr (symbol-value ref-dep)
3105                   (nconc (cdr (symbol-value ref-dep))
3106                          (list (symbol-value id-dep))))
3107         (set ref-dep (list nil (symbol-value id-dep)))))
3108     header))
3109
3110 (defun gnus-build-sparse-threads ()
3111   (let ((headers gnus-newsgroup-headers)
3112         (gnus-summary-ignore-duplicates t)
3113         header references generation relations
3114         subject child end new-child date)
3115     ;; First we create an alist of generations/relations, where
3116     ;; generations is how much we trust the relation, and the relation
3117     ;; is parent/child.
3118     (gnus-message 7 "Making sparse threads...")
3119     (save-excursion
3120       (nnheader-set-temp-buffer " *gnus sparse threads*")
3121       (while (setq header (pop headers))
3122         (when (and (setq references (mail-header-references header))
3123                    (not (string= references "")))
3124           (insert references)
3125           (setq child (mail-header-id header)
3126                 subject (mail-header-subject header)
3127                 date (mail-header-date header)
3128                 generation 0)
3129           (while (search-backward ">" nil t)
3130             (setq end (1+ (point)))
3131             (when (search-backward "<" nil t)
3132               (setq new-child (buffer-substring (point) end))
3133               (push (list (incf generation)
3134                           child (setq child new-child)
3135                           subject date)
3136                     relations)))
3137           (when child
3138             (push (list (1+ generation) child nil subject) relations))
3139           (erase-buffer)))
3140       (kill-buffer (current-buffer)))
3141     ;; Sort over trustworthiness.
3142     (mapcar
3143      (lambda (relation)
3144        (when (gnus-dependencies-add-header
3145               (make-full-mail-header
3146                gnus-reffed-article-number
3147                (nth 3 relation) "" (or (nth 4 relation) "")
3148                (nth 1 relation)
3149                (or (nth 2 relation) "") 0 0 "")
3150               gnus-newsgroup-dependencies nil)
3151          (push gnus-reffed-article-number gnus-newsgroup-limit)
3152          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3153          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3154                gnus-newsgroup-reads)
3155          (decf gnus-reffed-article-number)))
3156      (sort relations 'car-less-than-car))
3157     (gnus-message 7 "Making sparse threads...done")))
3158
3159 (defun gnus-build-old-threads ()
3160   ;; Look at all the articles that refer back to old articles, and
3161   ;; fetch the headers for the articles that aren't there.  This will
3162   ;; build complete threads - if the roots haven't been expired by the
3163   ;; server, that is.
3164   (let (id heads)
3165     (mapatoms
3166      (lambda (refs)
3167        (when (not (car (symbol-value refs)))
3168          (setq heads (cdr (symbol-value refs)))
3169          (while heads
3170            (if (memq (mail-header-number (caar heads))
3171                      gnus-newsgroup-dormant)
3172                (setq heads (cdr heads))
3173              (setq id (symbol-name refs))
3174              (while (and (setq id (gnus-build-get-header id))
3175                          (not (car (gnus-id-to-thread id)))))
3176              (setq heads nil)))))
3177      gnus-newsgroup-dependencies)))
3178
3179 ;; This function has to be called with point after the article number
3180 ;; on the beginning of the line.
3181 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3182   (let ((eol (gnus-point-at-eol))
3183         (buffer (current-buffer))
3184         header)
3185
3186     ;; overview: [num subject from date id refs chars lines misc]
3187     (unless (eobp)
3188       (forward-char))
3189
3190     (setq header
3191           (make-full-mail-header
3192            number                               ; number
3193            (nnheader-nov-field)                 ; subject
3194            (nnheader-nov-field)                 ; from
3195            (nnheader-nov-field)                 ; date
3196            (nnheader-nov-read-message-id)       ; id
3197            (nnheader-nov-field)                 ; refs
3198            (nnheader-nov-read-integer)          ; chars
3199            (nnheader-nov-read-integer)          ; lines
3200            (unless (eobp)
3201              (nnheader-nov-field))              ; misc
3202            (nnheader-nov-parse-extra)))         ; extra
3203
3204     (when gnus-alter-header-function
3205       (funcall gnus-alter-header-function header))
3206     (gnus-dependencies-add-header header dependencies force-new)))
3207
3208 (defun gnus-build-get-header (id)
3209   ;; Look through the buffer of NOV lines and find the header to
3210   ;; ID.  Enter this line into the dependencies hash table, and return
3211   ;; the id of the parent article (if any).
3212   (let ((deps gnus-newsgroup-dependencies)
3213         found header)
3214     (prog1
3215         (save-excursion
3216           (set-buffer nntp-server-buffer)
3217           (let ((case-fold-search nil))
3218             (goto-char (point-min))
3219             (while (and (not found)
3220                         (search-forward id nil t))
3221               (beginning-of-line)
3222               (setq found (looking-at
3223                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3224                                    (regexp-quote id))))
3225               (or found (beginning-of-line 2)))
3226             (when found
3227               (beginning-of-line)
3228               (and
3229                (setq header (gnus-nov-parse-line
3230                              (read (current-buffer)) deps))
3231                (gnus-parent-id (mail-header-references header))))))
3232       (when header
3233         (let ((number (mail-header-number header)))
3234           (push number gnus-newsgroup-limit)
3235           (push header gnus-newsgroup-headers)
3236           (if (memq number gnus-newsgroup-unselected)
3237               (progn
3238                 (push number gnus-newsgroup-unreads)
3239                 (setq gnus-newsgroup-unselected
3240                       (delq number gnus-newsgroup-unselected)))
3241             (push number gnus-newsgroup-ancient)))))))
3242
3243 (defun gnus-build-all-threads ()
3244   "Read all the headers."
3245   (let ((gnus-summary-ignore-duplicates t)
3246         (dependencies gnus-newsgroup-dependencies)
3247         header article)
3248     (save-excursion
3249       (set-buffer nntp-server-buffer)
3250       (let ((case-fold-search nil))
3251         (goto-char (point-min))
3252         (while (not (eobp))
3253           (ignore-errors
3254             (setq article (read (current-buffer))
3255                   header (gnus-nov-parse-line
3256                           article dependencies)))
3257           (when header
3258             (save-excursion
3259               (set-buffer gnus-summary-buffer)
3260               (push header gnus-newsgroup-headers)
3261               (if (memq (setq article (mail-header-number header))
3262                         gnus-newsgroup-unselected)
3263                   (progn
3264                     (push article gnus-newsgroup-unreads)
3265                     (setq gnus-newsgroup-unselected
3266                           (delq article gnus-newsgroup-unselected)))
3267                 (push article gnus-newsgroup-ancient)))
3268             (forward-line 1)))))))
3269
3270 (defun gnus-summary-update-article-line (article header)
3271   "Update the line for ARTICLE using HEADERS."
3272   (let* ((id (mail-header-id header))
3273          (thread (gnus-id-to-thread id)))
3274     (unless thread
3275       (error "Article in no thread"))
3276     ;; Update the thread.
3277     (setcar thread header)
3278     (gnus-summary-goto-subject article)
3279     (let* ((datal (gnus-data-find-list article))
3280            (data (car datal))
3281            (length (when (cdr datal)
3282                      (- (gnus-data-pos data)
3283                         (gnus-data-pos (cadr datal)))))
3284            (buffer-read-only nil)
3285            (level (gnus-summary-thread-level)))
3286       (gnus-delete-line)
3287       (gnus-summary-insert-line
3288        header level nil (gnus-article-mark article)
3289        (memq article gnus-newsgroup-replied)
3290        (memq article gnus-newsgroup-expirable)
3291        ;; Only insert the Subject string when it's different
3292        ;; from the previous Subject string.
3293        (if (gnus-subject-equal
3294             (condition-case ()
3295                 (mail-header-subject
3296                  (gnus-data-header
3297                   (cadr
3298                    (gnus-data-find-list
3299                     article
3300                     (gnus-data-list t)))))
3301               ;; Error on the side of excessive subjects.
3302               (error ""))
3303             (mail-header-subject header))
3304            ""
3305          (mail-header-subject header))
3306        nil (cdr (assq article gnus-newsgroup-scored))
3307        (memq article gnus-newsgroup-processable))
3308       (when length
3309         (gnus-data-update-list
3310          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3311
3312 (defun gnus-summary-update-article (article &optional iheader)
3313   "Update ARTICLE in the summary buffer."
3314   (set-buffer gnus-summary-buffer)
3315   (let* ((header (gnus-summary-article-header article))
3316          (id (mail-header-id header))
3317          (data (gnus-data-find article))
3318          (thread (gnus-id-to-thread id))
3319          (references (mail-header-references header))
3320          (parent
3321           (gnus-id-to-thread
3322            (or (gnus-parent-id
3323                 (when (and references
3324                            (not (equal "" references)))
3325                   references))
3326                "none")))
3327          (buffer-read-only nil)
3328          (old (car thread)))
3329     (when thread
3330       (unless iheader
3331         (setcar thread nil)
3332         (when parent
3333           (delq thread parent)))
3334       (if (gnus-summary-insert-subject id header)
3335           ;; Set the (possibly) new article number in the data structure.
3336           (gnus-data-set-number data (gnus-id-to-article id))
3337         (setcar thread old)
3338         nil))))
3339
3340 (defun gnus-rebuild-thread (id &optional line)
3341   "Rebuild the thread containing ID.
3342 If LINE, insert the rebuilt thread starting on line LINE."
3343   (let ((buffer-read-only nil)
3344         old-pos current thread data)
3345     (if (not gnus-show-threads)
3346         (setq thread (list (car (gnus-id-to-thread id))))
3347       ;; Get the thread this article is part of.
3348       (setq thread (gnus-remove-thread id)))
3349     (setq old-pos (gnus-point-at-bol))
3350     (setq current (save-excursion
3351                     (and (zerop (forward-line -1))
3352                          (gnus-summary-article-number))))
3353     ;; If this is a gathered thread, we have to go some re-gathering.
3354     (when (stringp (car thread))
3355       (let ((subject (car thread))
3356             roots thr)
3357         (setq thread (cdr thread))
3358         (while thread
3359           (unless (memq (setq thr (gnus-id-to-thread
3360                                    (gnus-root-id
3361                                     (mail-header-id (caar thread)))))
3362                         roots)
3363             (push thr roots))
3364           (setq thread (cdr thread)))
3365         ;; We now have all (unique) roots.
3366         (if (= (length roots) 1)
3367             ;; All the loose roots are now one solid root.
3368             (setq thread (car roots))
3369           (setq thread (cons subject (gnus-sort-threads roots))))))
3370     (let (threads)
3371       ;; We then insert this thread into the summary buffer.
3372       (when line
3373         (goto-char (point-min))
3374         (forward-line (1- line)))
3375       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3376         (if gnus-show-threads
3377             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3378           (gnus-summary-prepare-unthreaded thread))
3379         (setq data (nreverse gnus-newsgroup-data))
3380         (setq threads gnus-newsgroup-threads))
3381       ;; We splice the new data into the data structure.
3382       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3383       ;;!!! then we want to insert at the beginning of the buffer.
3384       ;;!!! That happens to be true with Gnus now, but that may
3385       ;;!!! change in the future.  Perhaps.
3386       (gnus-data-enter-list
3387        (if line nil current) data (- (point) old-pos))
3388       (setq gnus-newsgroup-threads
3389             (nconc threads gnus-newsgroup-threads))
3390       (gnus-data-compute-positions))))
3391
3392 (defun gnus-number-to-header (number)
3393   "Return the header for article NUMBER."
3394   (let ((headers gnus-newsgroup-headers))
3395     (while (and headers
3396                 (not (= number (mail-header-number (car headers)))))
3397       (pop headers))
3398     (when headers
3399       (car headers))))
3400
3401 (defun gnus-parent-headers (in-headers &optional generation)
3402   "Return the headers of the GENERATIONeth parent of HEADERS."
3403   (unless generation
3404     (setq generation 1))
3405   (let ((parent t)
3406         (headers in-headers)
3407         references)
3408     (while (and parent
3409                 (not (zerop generation))
3410                 (setq references (mail-header-references headers)))
3411       (setq headers (if (and references
3412                              (setq parent (gnus-parent-id references)))
3413                         (car (gnus-id-to-thread parent))
3414                       nil))
3415       (decf generation))
3416     (and (not (eq headers in-headers))
3417          headers)))
3418
3419 (defun gnus-id-to-thread (id)
3420   "Return the (sub-)thread where ID appears."
3421   (gnus-gethash id gnus-newsgroup-dependencies))
3422
3423 (defun gnus-id-to-article (id)
3424   "Return the article number of ID."
3425   (let ((thread (gnus-id-to-thread id)))
3426     (when (and thread
3427                (car thread))
3428       (mail-header-number (car thread)))))
3429
3430 (defun gnus-id-to-header (id)
3431   "Return the article headers of ID."
3432   (car (gnus-id-to-thread id)))
3433
3434 (defun gnus-article-displayed-root-p (article)
3435   "Say whether ARTICLE is a root(ish) article."
3436   (let ((level (gnus-summary-thread-level article))
3437         (refs (mail-header-references  (gnus-summary-article-header article)))
3438         particle)
3439     (cond
3440      ((null level) nil)
3441      ((zerop level) t)
3442      ((null refs) t)
3443      ((null (gnus-parent-id refs)) t)
3444      ((and (= 1 level)
3445            (null (setq particle (gnus-id-to-article
3446                                  (gnus-parent-id refs))))
3447            (null (gnus-summary-thread-level particle)))))))
3448
3449 (defun gnus-root-id (id)
3450   "Return the id of the root of the thread where ID appears."
3451   (let (last-id prev)
3452     (while (and id (setq prev (car (gnus-id-to-thread id))))
3453       (setq last-id id
3454             id (gnus-parent-id (mail-header-references prev))))
3455     last-id))
3456
3457 (defun gnus-articles-in-thread (thread)
3458   "Return the list of articles in THREAD."
3459   (cons (mail-header-number (car thread))
3460         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3461
3462 (defun gnus-remove-thread (id &optional dont-remove)
3463   "Remove the thread that has ID in it."
3464   (let (headers thread last-id)
3465     ;; First go up in this thread until we find the root.
3466     (setq last-id (gnus-root-id id)
3467           headers (message-flatten-list (gnus-id-to-thread last-id)))
3468     ;; We have now found the real root of this thread.  It might have
3469     ;; been gathered into some loose thread, so we have to search
3470     ;; through the threads to find the thread we wanted.
3471     (let ((threads gnus-newsgroup-threads)
3472           sub)
3473       (while threads
3474         (setq sub (car threads))
3475         (if (stringp (car sub))
3476             ;; This is a gathered thread, so we look at the roots
3477             ;; below it to find whether this article is in this
3478             ;; gathered root.
3479             (progn
3480               (setq sub (cdr sub))
3481               (while sub
3482                 (when (member (caar sub) headers)
3483                   (setq thread (car threads)
3484                         threads nil
3485                         sub nil))
3486                 (setq sub (cdr sub))))
3487           ;; It's an ordinary thread, so we check it.
3488           (when (eq (car sub) (car headers))
3489             (setq thread sub
3490                   threads nil)))
3491         (setq threads (cdr threads)))
3492       ;; If this article is in no thread, then it's a root.
3493       (if thread
3494           (unless dont-remove
3495             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3496         (setq thread (gnus-id-to-thread last-id)))
3497       (when thread
3498         (prog1
3499             thread                      ; We return this thread.
3500           (unless dont-remove
3501             (if (stringp (car thread))
3502                 (progn
3503                   ;; If we use dummy roots, then we have to remove the
3504                   ;; dummy root as well.
3505                   (when (eq gnus-summary-make-false-root 'dummy)
3506                     ;; We go to the dummy root by going to
3507                     ;; the first sub-"thread", and then one line up.
3508                     (gnus-summary-goto-article
3509                      (mail-header-number (caadr thread)))
3510                     (forward-line -1)
3511                     (gnus-delete-line)
3512                     (gnus-data-compute-positions))
3513                   (setq thread (cdr thread))
3514                   (while thread
3515                     (gnus-remove-thread-1 (car thread))
3516                     (setq thread (cdr thread))))
3517               (gnus-summary-show-all-threads)
3518               (gnus-remove-thread-1 thread))))))))
3519
3520 (defun gnus-remove-thread-1 (thread)
3521   "Remove the thread THREAD recursively."
3522   (let ((number (mail-header-number (pop thread)))
3523         d)
3524     (setq thread (reverse thread))
3525     (while thread
3526       (gnus-remove-thread-1 (pop thread)))
3527     (when (setq d (gnus-data-find number))
3528       (goto-char (gnus-data-pos d))
3529       (gnus-data-remove
3530        number
3531        (- (gnus-point-at-bol)
3532           (prog1
3533               (1+ (gnus-point-at-eol))
3534             (gnus-delete-line)))))))
3535
3536 (defun gnus-sort-threads (threads)
3537   "Sort THREADS."
3538   (if (not gnus-thread-sort-functions)
3539       threads
3540     (gnus-message 8 "Sorting threads...")
3541     (prog1
3542         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3543       (gnus-message 8 "Sorting threads...done"))))
3544
3545 (defun gnus-sort-articles (articles)
3546   "Sort ARTICLES."
3547   (when gnus-article-sort-functions
3548     (gnus-message 7 "Sorting articles...")
3549     (prog1
3550         (setq gnus-newsgroup-headers
3551               (sort articles (gnus-make-sort-function
3552                               gnus-article-sort-functions)))
3553       (gnus-message 7 "Sorting articles...done"))))
3554
3555 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3556 (defmacro gnus-thread-header (thread)
3557   ;; Return header of first article in THREAD.
3558   ;; Note that THREAD must never, ever be anything else than a variable -
3559   ;; using some other form will lead to serious barfage.
3560   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3561   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3562   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3563         (vector thread) 2))
3564
3565 (defsubst gnus-article-sort-by-number (h1 h2)
3566   "Sort articles by article number."
3567   (< (mail-header-number h1)
3568      (mail-header-number h2)))
3569
3570 (defun gnus-thread-sort-by-number (h1 h2)
3571   "Sort threads by root article number."
3572   (gnus-article-sort-by-number
3573    (gnus-thread-header h1) (gnus-thread-header h2)))
3574
3575 (defsubst gnus-article-sort-by-lines (h1 h2)
3576   "Sort articles by article Lines header."
3577   (< (mail-header-lines h1)
3578      (mail-header-lines h2)))
3579
3580 (defun gnus-thread-sort-by-lines (h1 h2)
3581   "Sort threads by root article Lines header."
3582   (gnus-article-sort-by-lines
3583    (gnus-thread-header h1) (gnus-thread-header h2)))
3584
3585 (defsubst gnus-article-sort-by-chars (h1 h2)
3586   "Sort articles by octet length."
3587   (< (mail-header-chars h1)
3588      (mail-header-chars h2)))
3589
3590 (defun gnus-thread-sort-by-chars (h1 h2)
3591   "Sort threads by root article octet length."
3592   (gnus-article-sort-by-chars
3593    (gnus-thread-header h1) (gnus-thread-header h2)))
3594
3595 (defsubst gnus-article-sort-by-author (h1 h2)
3596   "Sort articles by root author."
3597   (string-lessp
3598    (let ((addr (car (mime-read-field 'From h1))))
3599      (or (std11-full-name-string addr)
3600          (std11-address-string addr)
3601          ""))
3602    (let ((addr (car (mime-read-field 'From h2))))
3603      (or (std11-full-name-string addr)
3604          (std11-address-string addr)
3605          ""))
3606    ))
3607
3608 (defun gnus-thread-sort-by-author (h1 h2)
3609   "Sort threads by root author."
3610   (gnus-article-sort-by-author
3611    (gnus-thread-header h1)  (gnus-thread-header h2)))
3612
3613 (defsubst gnus-article-sort-by-subject (h1 h2)
3614   "Sort articles by root subject."
3615   (string-lessp
3616    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3617    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3618
3619 (defun gnus-thread-sort-by-subject (h1 h2)
3620   "Sort threads by root subject."
3621   (gnus-article-sort-by-subject
3622    (gnus-thread-header h1) (gnus-thread-header h2)))
3623
3624 (defsubst gnus-article-sort-by-date (h1 h2)
3625   "Sort articles by root article date."
3626   (time-less-p
3627    (gnus-date-get-time (mail-header-date h1))
3628    (gnus-date-get-time (mail-header-date h2))))
3629
3630 (defun gnus-thread-sort-by-date (h1 h2)
3631   "Sort threads by root article date."
3632   (gnus-article-sort-by-date
3633    (gnus-thread-header h1) (gnus-thread-header h2)))
3634
3635 (defsubst gnus-article-sort-by-score (h1 h2)
3636   "Sort articles by root article score.
3637 Unscored articles will be counted as having a score of zero."
3638   (> (or (cdr (assq (mail-header-number h1)
3639                     gnus-newsgroup-scored))
3640          gnus-summary-default-score 0)
3641      (or (cdr (assq (mail-header-number h2)
3642                     gnus-newsgroup-scored))
3643          gnus-summary-default-score 0)))
3644
3645 (defun gnus-thread-sort-by-score (h1 h2)
3646   "Sort threads by root article score."
3647   (gnus-article-sort-by-score
3648    (gnus-thread-header h1) (gnus-thread-header h2)))
3649
3650 (defun gnus-thread-sort-by-total-score (h1 h2)
3651   "Sort threads by the sum of all scores in the thread.
3652 Unscored articles will be counted as having a score of zero."
3653   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3654
3655 (defun gnus-thread-total-score (thread)
3656   ;; This function find the total score of THREAD.
3657   (cond ((null thread)
3658          0)
3659         ((consp thread)
3660          (if (stringp (car thread))
3661              (apply gnus-thread-score-function 0
3662                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3663            (gnus-thread-total-score-1 thread)))
3664         (t
3665          (gnus-thread-total-score-1 (list thread)))))
3666
3667 (defun gnus-thread-total-score-1 (root)
3668   ;; This function find the total score of the thread below ROOT.
3669   (setq root (car root))
3670   (apply gnus-thread-score-function
3671          (or (append
3672               (mapcar 'gnus-thread-total-score
3673                       (cdr (gnus-id-to-thread (mail-header-id root))))
3674               (when (> (mail-header-number root) 0)
3675                 (list (or (cdr (assq (mail-header-number root)
3676                                      gnus-newsgroup-scored))
3677                           gnus-summary-default-score 0))))
3678              (list gnus-summary-default-score)
3679              '(0))))
3680
3681 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3682 (defvar gnus-tmp-prev-subject nil)
3683 (defvar gnus-tmp-false-parent nil)
3684 (defvar gnus-tmp-root-expunged nil)
3685 (defvar gnus-tmp-dummy-line nil)
3686
3687 (defvar gnus-tmp-header)
3688 (defun gnus-extra-header (type &optional header)
3689   "Return the extra header of TYPE."
3690   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3691       ""))
3692
3693 (defun gnus-summary-prepare-threads (threads)
3694   "Prepare summary buffer from THREADS and indentation LEVEL.
3695 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3696 or a straight list of headers."
3697   (gnus-message 7 "Generating summary...")
3698
3699   (setq gnus-newsgroup-threads threads)
3700   (beginning-of-line)
3701
3702   (let ((gnus-tmp-level 0)
3703         (default-score (or gnus-summary-default-score 0))
3704         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3705         thread number subject stack state gnus-tmp-gathered beg-match
3706         new-roots gnus-tmp-new-adopts thread-end
3707         gnus-tmp-header gnus-tmp-unread
3708         gnus-tmp-replied gnus-tmp-subject-or-nil
3709         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3710         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3711         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3712
3713     (setq gnus-tmp-prev-subject nil)
3714
3715     (if (vectorp (car threads))
3716         ;; If this is a straight (sic) list of headers, then a
3717         ;; threaded summary display isn't required, so we just create
3718         ;; an unthreaded one.
3719         (gnus-summary-prepare-unthreaded threads)
3720
3721       ;; Do the threaded display.
3722
3723       (while (or threads stack gnus-tmp-new-adopts new-roots)
3724
3725         (if (and (= gnus-tmp-level 0)
3726                  (or (not stack)
3727                      (= (caar stack) 0))
3728                  (not gnus-tmp-false-parent)
3729                  (or gnus-tmp-new-adopts new-roots))
3730             (if gnus-tmp-new-adopts
3731                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3732                       thread (list (car gnus-tmp-new-adopts))
3733                       gnus-tmp-header (caar thread)
3734                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3735               (when new-roots
3736                 (setq thread (list (car new-roots))
3737                       gnus-tmp-header (caar thread)
3738                       new-roots (cdr new-roots))))
3739
3740           (if threads
3741               ;; If there are some threads, we do them before the
3742               ;; threads on the stack.
3743               (setq thread threads
3744                     gnus-tmp-header (caar thread))
3745             ;; There were no current threads, so we pop something off
3746             ;; the stack.
3747             (setq state (car stack)
3748                   gnus-tmp-level (car state)
3749                   thread (cdr state)
3750                   stack (cdr stack)
3751                   gnus-tmp-header (caar thread))))
3752
3753         (setq gnus-tmp-false-parent nil)
3754         (setq gnus-tmp-root-expunged nil)
3755         (setq thread-end nil)
3756
3757         (if (stringp gnus-tmp-header)
3758             ;; The header is a dummy root.
3759             (cond
3760              ((eq gnus-summary-make-false-root 'adopt)
3761               ;; We let the first article adopt the rest.
3762               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3763                                                (cddar thread)))
3764               (setq gnus-tmp-gathered
3765                     (nconc (mapcar
3766                             (lambda (h) (mail-header-number (car h)))
3767                             (cddar thread))
3768                            gnus-tmp-gathered))
3769               (setq thread (cons (list (caar thread)
3770                                        (cadar thread))
3771                                  (cdr thread)))
3772               (setq gnus-tmp-level -1
3773                     gnus-tmp-false-parent t))
3774              ((eq gnus-summary-make-false-root 'empty)
3775               ;; We print adopted articles with empty subject fields.
3776               (setq gnus-tmp-gathered
3777                     (nconc (mapcar
3778                             (lambda (h) (mail-header-number (car h)))
3779                             (cddar thread))
3780                            gnus-tmp-gathered))
3781               (setq gnus-tmp-level -1))
3782              ((eq gnus-summary-make-false-root 'dummy)
3783               ;; We remember that we probably want to output a dummy
3784               ;; root.
3785               (setq gnus-tmp-dummy-line gnus-tmp-header)
3786               (setq gnus-tmp-prev-subject gnus-tmp-header))
3787              (t
3788               ;; We do not make a root for the gathered
3789               ;; sub-threads at all.
3790               (setq gnus-tmp-level -1)))
3791
3792           (setq number (mail-header-number gnus-tmp-header)
3793                 subject (mail-header-subject gnus-tmp-header))
3794
3795           (cond
3796            ;; If the thread has changed subject, we might want to make
3797            ;; this subthread into a root.
3798            ((and (null gnus-thread-ignore-subject)
3799                  (not (zerop gnus-tmp-level))
3800                  gnus-tmp-prev-subject
3801                  (not (inline
3802                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3803             (setq new-roots (nconc new-roots (list (car thread)))
3804                   thread-end t
3805                   gnus-tmp-header nil))
3806            ;; If the article lies outside the current limit,
3807            ;; then we do not display it.
3808            ((not (memq number gnus-newsgroup-limit))
3809             (setq gnus-tmp-gathered
3810                   (nconc (mapcar
3811                           (lambda (h) (mail-header-number (car h)))
3812                           (cdar thread))
3813                          gnus-tmp-gathered))
3814             (setq gnus-tmp-new-adopts (if (cdar thread)
3815                                           (append gnus-tmp-new-adopts
3816                                                   (cdar thread))
3817                                         gnus-tmp-new-adopts)
3818                   thread-end t
3819                   gnus-tmp-header nil)
3820             (when (zerop gnus-tmp-level)
3821               (setq gnus-tmp-root-expunged t)))
3822            ;; Perhaps this article is to be marked as read?
3823            ((and gnus-summary-mark-below
3824                  (< (or (cdr (assq number gnus-newsgroup-scored))
3825                         default-score)
3826                     gnus-summary-mark-below)
3827                  ;; Don't touch sparse articles.
3828                  (not (gnus-summary-article-sparse-p number))
3829                  (not (gnus-summary-article-ancient-p number)))
3830             (setq gnus-newsgroup-unreads
3831                   (delq number gnus-newsgroup-unreads))
3832             (if gnus-newsgroup-auto-expire
3833                 (push number gnus-newsgroup-expirable)
3834               (push (cons number gnus-low-score-mark)
3835                     gnus-newsgroup-reads))))
3836
3837           (when gnus-tmp-header
3838             ;; We may have an old dummy line to output before this
3839             ;; article.
3840             (when (and gnus-tmp-dummy-line
3841                        (gnus-subject-equal
3842                         gnus-tmp-dummy-line
3843                         (mail-header-subject gnus-tmp-header)))
3844               (gnus-summary-insert-dummy-line
3845                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3846               (setq gnus-tmp-dummy-line nil))
3847
3848             ;; Compute the mark.
3849             (setq gnus-tmp-unread (gnus-article-mark number))
3850
3851             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3852                                   gnus-tmp-header gnus-tmp-level)
3853                   gnus-newsgroup-data)
3854
3855             ;; Actually insert the line.
3856             (setq
3857              gnus-tmp-subject-or-nil
3858              (cond
3859               ((and gnus-thread-ignore-subject
3860                     gnus-tmp-prev-subject
3861                     (not (inline (gnus-subject-equal
3862                                   gnus-tmp-prev-subject subject))))
3863                subject)
3864               ((zerop gnus-tmp-level)
3865                (if (and (eq gnus-summary-make-false-root 'empty)
3866                         (memq number gnus-tmp-gathered)
3867                         gnus-tmp-prev-subject
3868                         (inline (gnus-subject-equal
3869                                  gnus-tmp-prev-subject subject)))
3870                    gnus-summary-same-subject
3871                  subject))
3872               (t gnus-summary-same-subject)))
3873             (if (and (eq gnus-summary-make-false-root 'adopt)
3874                      (= gnus-tmp-level 1)
3875                      (memq number gnus-tmp-gathered))
3876                 (setq gnus-tmp-opening-bracket ?\<
3877                       gnus-tmp-closing-bracket ?\>)
3878               (setq gnus-tmp-opening-bracket ?\[
3879                     gnus-tmp-closing-bracket ?\]))
3880             (setq
3881              gnus-tmp-indentation
3882              (aref gnus-thread-indent-array gnus-tmp-level)
3883              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3884              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3885                                 gnus-summary-default-score 0)
3886              gnus-tmp-score-char
3887              (if (or (null gnus-summary-default-score)
3888                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3889                          gnus-summary-zcore-fuzz))
3890                  ? ;Whitespace
3891                (if (< gnus-tmp-score gnus-summary-default-score)
3892                    gnus-score-below-mark gnus-score-over-mark))
3893              gnus-tmp-replied
3894              (cond ((memq number gnus-newsgroup-processable)
3895                     gnus-process-mark)
3896                    ((memq number gnus-newsgroup-cached)
3897                     gnus-cached-mark)
3898                    ((memq number gnus-newsgroup-replied)
3899                     gnus-replied-mark)
3900                    ((memq number gnus-newsgroup-saved)
3901                     gnus-saved-mark)
3902                    (t gnus-unread-mark))
3903              gnus-tmp-from (mail-header-from gnus-tmp-header)
3904              gnus-tmp-name
3905              (cond
3906               ((string-match "<[^>]+> *$" gnus-tmp-from)
3907                (setq beg-match (match-beginning 0))
3908                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3909                         (substring gnus-tmp-from (1+ (match-beginning 0))
3910                                    (1- (match-end 0))))
3911                    (substring gnus-tmp-from 0 beg-match)))
3912               ((string-match "(.+)" gnus-tmp-from)
3913                (substring gnus-tmp-from
3914                           (1+ (match-beginning 0)) (1- (match-end 0))))
3915               (t gnus-tmp-from)))
3916             (when (string= gnus-tmp-name "")
3917               (setq gnus-tmp-name gnus-tmp-from))
3918             (unless (numberp gnus-tmp-lines)
3919               (setq gnus-tmp-lines 0))
3920             (gnus-put-text-property
3921              (point)
3922              (progn (eval gnus-summary-line-format-spec) (point))
3923              'gnus-number number)
3924             (when gnus-visual-p
3925               (forward-line -1)
3926               (gnus-run-hooks 'gnus-summary-update-hook)
3927               (forward-line 1))
3928
3929             (setq gnus-tmp-prev-subject subject)))
3930
3931         (when (nth 1 thread)
3932           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3933         (incf gnus-tmp-level)
3934         (setq threads (if thread-end nil (cdar thread)))
3935         (unless threads
3936           (setq gnus-tmp-level 0)))))
3937   (gnus-message 7 "Generating summary...done"))
3938
3939 (defun gnus-summary-prepare-unthreaded (headers)
3940   "Generate an unthreaded summary buffer based on HEADERS."
3941   (let (header number mark)
3942
3943     (beginning-of-line)
3944
3945     (while headers
3946       ;; We may have to root out some bad articles...
3947       (when (memq (setq number (mail-header-number
3948                                 (setq header (pop headers))))
3949                   gnus-newsgroup-limit)
3950         ;; Mark article as read when it has a low score.
3951         (when (and gnus-summary-mark-below
3952                    (< (or (cdr (assq number gnus-newsgroup-scored))
3953                           gnus-summary-default-score 0)
3954                       gnus-summary-mark-below)
3955                    (not (gnus-summary-article-ancient-p number)))
3956           (setq gnus-newsgroup-unreads
3957                 (delq number gnus-newsgroup-unreads))
3958           (if gnus-newsgroup-auto-expire
3959               (push number gnus-newsgroup-expirable)
3960             (push (cons number gnus-low-score-mark)
3961                   gnus-newsgroup-reads)))
3962
3963         (setq mark (gnus-article-mark number))
3964         (push (gnus-data-make number mark (1+ (point)) header 0)
3965               gnus-newsgroup-data)
3966         (gnus-summary-insert-line
3967          header 0 number
3968          mark (memq number gnus-newsgroup-replied)
3969          (memq number gnus-newsgroup-expirable)
3970          (mail-header-subject header) nil
3971          (cdr (assq number gnus-newsgroup-scored))
3972          (memq number gnus-newsgroup-processable))))))
3973
3974 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3975   "Select newsgroup GROUP.
3976 If READ-ALL is non-nil, all articles in the group are selected.
3977 If SELECT-ARTICLES, only select those articles from GROUP."
3978   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3979          ;;!!! Dirty hack; should be removed.
3980          (gnus-summary-ignore-duplicates
3981           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3982               t
3983             gnus-summary-ignore-duplicates))
3984          (info (nth 2 entry))
3985          articles fetched-articles cached)
3986
3987     (unless (gnus-check-server
3988              (setq gnus-current-select-method
3989                    (gnus-find-method-for-group group)))
3990       (error "Couldn't open server"))
3991
3992     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3993         (gnus-activate-group group)     ; Or we can activate it...
3994         (progn                          ; Or we bug out.
3995           (when (equal major-mode 'gnus-summary-mode)
3996             (kill-buffer (current-buffer)))
3997           (error "Couldn't request group %s: %s"
3998                  group (gnus-status-message group))))
3999
4000     (unless (gnus-request-group group t)
4001       (when (equal major-mode 'gnus-summary-mode)
4002         (kill-buffer (current-buffer)))
4003       (error "Couldn't request group %s: %s"
4004              group (gnus-status-message group)))
4005
4006     (setq gnus-newsgroup-name group)
4007     (setq gnus-newsgroup-unselected nil)
4008     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4009     (gnus-summary-setup-default-charset)
4010
4011     ;; Adjust and set lists of article marks.
4012     (when info
4013       (gnus-adjust-marked-articles info))
4014
4015     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4016     (setq cached
4017           (if (gnus-virtual-group-p group)
4018               gnus-newsgroup-cached
4019             (gnus-cache-articles-in-group group)))
4020
4021     (setq gnus-newsgroup-unreads
4022           (gnus-set-difference
4023            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4024            gnus-newsgroup-dormant))
4025
4026     (setq gnus-newsgroup-processable nil)
4027
4028     (gnus-update-read-articles group gnus-newsgroup-unreads)
4029
4030     (if (setq articles select-articles)
4031         (setq gnus-newsgroup-unselected
4032               (gnus-sorted-intersection
4033                gnus-newsgroup-unreads
4034                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4035       (setq articles (gnus-articles-to-read group read-all)))
4036
4037     (cond
4038      ((null articles)
4039       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4040       'quit)
4041      ((eq articles 0) nil)
4042      (t
4043       ;; Init the dependencies hash table.
4044       (setq gnus-newsgroup-dependencies
4045             (gnus-make-hashtable (length articles)))
4046       (gnus-set-global-variables)
4047       ;; Retrieve the headers and read them in.
4048       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4049       (setq gnus-newsgroup-headers
4050             (gnus-retrieve-parsed-headers
4051              articles gnus-newsgroup-name
4052              ;; We might want to fetch old headers, but
4053              ;; not if there is only 1 article.
4054              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4055                            (not (numberp gnus-fetch-old-headers)))
4056                       (> (length articles) 1))
4057                   gnus-fetch-old-headers)))
4058       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4059
4060       ;; Suppress duplicates?
4061       (when gnus-suppress-duplicates
4062         (gnus-dup-suppress-articles))
4063
4064       ;; Set the initial limit.
4065       (setq gnus-newsgroup-limit (copy-sequence articles))
4066       ;; Remove canceled articles from the list of unread articles.
4067       (setq gnus-newsgroup-unreads
4068             (gnus-set-sorted-intersection
4069              gnus-newsgroup-unreads
4070              (setq fetched-articles
4071                    (mapcar (lambda (headers) (mail-header-number headers))
4072                            gnus-newsgroup-headers))))
4073       ;; Removed marked articles that do not exist.
4074       (gnus-update-missing-marks
4075        (gnus-sorted-complement fetched-articles articles))
4076
4077       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4078       (when cached
4079         (setq gnus-newsgroup-cached cached))
4080
4081       ;; We might want to build some more threads first.
4082       (when (and gnus-fetch-old-headers
4083                  (eq gnus-headers-retrieved-by 'nov))
4084         (if (eq gnus-fetch-old-headers 'invisible)
4085             (gnus-build-all-threads)
4086           (gnus-build-old-threads)))
4087       ;; Let the Gnus agent mark articles as read.
4088       (when gnus-agent
4089         (gnus-agent-get-undownloaded-list))
4090       ;; Check whether auto-expire is to be done in this group.
4091       (setq gnus-newsgroup-auto-expire
4092             (gnus-group-auto-expirable-p group))
4093       ;; Set up the article buffer now, if necessary.
4094       (unless gnus-single-article-buffer
4095         (gnus-article-setup-buffer))
4096       ;; First and last article in this newsgroup.
4097       (when gnus-newsgroup-headers
4098         (setq gnus-newsgroup-begin
4099               (mail-header-number (car gnus-newsgroup-headers))
4100               gnus-newsgroup-end
4101               (mail-header-number
4102                (gnus-last-element gnus-newsgroup-headers))))
4103       ;; GROUP is successfully selected.
4104       (or gnus-newsgroup-headers t)))))
4105
4106 (defun gnus-articles-to-read (group &optional read-all)
4107   ;; Find out what articles the user wants to read.
4108   (let* ((articles
4109           ;; Select all articles if `read-all' is non-nil, or if there
4110           ;; are no unread articles.
4111           (if (or read-all
4112                   (and (zerop (length gnus-newsgroup-marked))
4113                        (zerop (length gnus-newsgroup-unreads)))
4114                   (eq (gnus-group-find-parameter group 'display)
4115                       'all))
4116               (gnus-uncompress-range (gnus-active group))
4117             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4118                           (copy-sequence gnus-newsgroup-unreads))
4119                   '<)))
4120          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4121          (scored (length scored-list))
4122          (number (length articles))
4123          (marked (+ (length gnus-newsgroup-marked)
4124                     (length gnus-newsgroup-dormant)))
4125          (select
4126           (cond
4127            ((numberp read-all)
4128             read-all)
4129            (t
4130             (condition-case ()
4131                 (cond
4132                  ((and (or (<= scored marked) (= scored number))
4133                        (natnump gnus-large-newsgroup)
4134                        (> number gnus-large-newsgroup))
4135                   (let* ((minibuffer-setup-hook (append
4136                                                  minibuffer-setup-hook
4137                                                  '(beginning-of-line)))
4138                          (input (read-string
4139                                  (format
4140                                   "How many articles from %s (max %d): "
4141                                   (gnus-limit-string gnus-newsgroup-name 35)
4142                                   number)
4143                                  (number-to-string gnus-large-newsgroup))))
4144                     (if (string-match "^[ \t]*$" input)
4145                         number
4146                       input)))
4147                  ((and (> scored marked) (< scored number)
4148                        (> (- scored number) 20))
4149                   (let ((input
4150                          (read-string
4151                           (format "%s %s (%d scored, %d total): "
4152                                   "How many articles from"
4153                                   group scored number))))
4154                     (if (string-match "^[ \t]*$" input)
4155                         number input)))
4156                  (t number))
4157               (quit nil))))))
4158     (setq select (if (stringp select) (string-to-number select) select))
4159     (if (or (null select) (zerop select))
4160         select
4161       (if (and (not (zerop scored)) (<= (abs select) scored))
4162           (progn
4163             (setq articles (sort scored-list '<))
4164             (setq number (length articles)))
4165         (setq articles (copy-sequence articles)))
4166
4167       (when (< (abs select) number)
4168         (if (< select 0)
4169             ;; Select the N oldest articles.
4170             (setcdr (nthcdr (1- (abs select)) articles) nil)
4171           ;; Select the N most recent articles.
4172           (setq articles (nthcdr (- number select) articles))))
4173       (setq gnus-newsgroup-unselected
4174             (gnus-sorted-intersection
4175              gnus-newsgroup-unreads
4176              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4177       articles)))
4178
4179 (defun gnus-killed-articles (killed articles)
4180   (let (out)
4181     (while articles
4182       (when (inline (gnus-member-of-range (car articles) killed))
4183         (push (car articles) out))
4184       (setq articles (cdr articles)))
4185     out))
4186
4187 (defun gnus-uncompress-marks (marks)
4188   "Uncompress the mark ranges in MARKS."
4189   (let ((uncompressed '(score bookmark))
4190         out)
4191     (while marks
4192       (if (memq (caar marks) uncompressed)
4193           (push (car marks) out)
4194         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4195       (setq marks (cdr marks)))
4196     out))
4197
4198 (defun gnus-adjust-marked-articles (info)
4199   "Set all article lists and remove all marks that are no longer valid."
4200   (let* ((marked-lists (gnus-info-marks info))
4201          (active (gnus-active (gnus-info-group info)))
4202          (min (car active))
4203          (max (cdr active))
4204          (types gnus-article-mark-lists)
4205          (uncompressed '(score bookmark killed))
4206          marks var articles article mark)
4207
4208     (while marked-lists
4209       (setq marks (pop marked-lists))
4210       (set (setq var (intern (format "gnus-newsgroup-%s"
4211                                      (car (rassq (setq mark (car marks))
4212                                                  types)))))
4213            (if (memq (car marks) uncompressed) (cdr marks)
4214              (gnus-uncompress-range (cdr marks))))
4215
4216       (setq articles (symbol-value var))
4217
4218       ;; All articles have to be subsets of the active articles.
4219       (cond
4220        ;; Adjust "simple" lists.
4221        ((memq mark '(tick dormant expire reply save))
4222         (while articles
4223           (when (or (< (setq article (pop articles)) min) (> article max))
4224             (set var (delq article (symbol-value var))))))
4225        ;; Adjust assocs.
4226        ((memq mark uncompressed)
4227         (when (not (listp (cdr (symbol-value var))))
4228           (set var (list (symbol-value var))))
4229         (when (not (listp (cdr articles)))
4230           (setq articles (list articles)))
4231         (while articles
4232           (when (or (not (consp (setq article (pop articles))))
4233                     (< (car article) min)
4234                     (> (car article) max))
4235             (set var (delq article (symbol-value var))))))))))
4236
4237 (defun gnus-update-missing-marks (missing)
4238   "Go through the list of MISSING articles and remove them from the mark lists."
4239   (when missing
4240     (let ((types gnus-article-mark-lists)
4241           var m)
4242       ;; Go through all types.
4243       (while types
4244         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4245         (when (symbol-value var)
4246           ;; This list has articles.  So we delete all missing articles
4247           ;; from it.
4248           (setq m missing)
4249           (while m
4250             (set var (delq (pop m) (symbol-value var)))))))))
4251
4252 (defun gnus-update-marks ()
4253   "Enter the various lists of marked articles into the newsgroup info list."
4254   (let ((types gnus-article-mark-lists)
4255         (info (gnus-get-info gnus-newsgroup-name))
4256         (uncompressed '(score bookmark killed))
4257         type list newmarked symbol delta-marks)
4258     (when info
4259       ;; Add all marks lists to the list of marks lists.
4260       (while (setq type (pop types))
4261         (setq list (symbol-value
4262                           (setq symbol
4263                                 (intern (format "gnus-newsgroup-%s"
4264                                                 (car type))))))
4265
4266         (when list
4267           ;; Get rid of the entries of the articles that have the
4268           ;; default score.
4269           (when (and (eq (cdr type) 'score)
4270                      gnus-save-score
4271                      list)
4272             (let* ((arts list)
4273                    (prev (cons nil list))
4274                    (all prev))
4275               (while arts
4276                 (if (or (not (consp (car arts)))
4277                         (= (cdar arts) gnus-summary-default-score))
4278                     (setcdr prev (cdr arts))
4279                   (setq prev arts))
4280                 (setq arts (cdr arts)))
4281               (setq list (cdr all)))))
4282
4283        (or (memq (cdr type) uncompressed)
4284            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4285        
4286        (when (gnus-check-backend-function 'request-set-mark
4287                                           gnus-newsgroup-name)
4288          ;; uncompressed:s are not proper flags (they are cons cells)
4289          ;; cache is a internal gnus flag
4290          (unless (memq (cdr type) (cons 'cache uncompressed))
4291            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4292                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4293                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4294              (if add
4295                  (push (list add 'add (list (cdr type))) delta-marks))
4296              (if del
4297                  (push (list del 'del (list (cdr type))) delta-marks)))))
4298           
4299         (when list
4300          (push (cons (cdr type) list) newmarked)))
4301
4302       (when delta-marks
4303         (unless (gnus-check-group gnus-newsgroup-name)
4304           (error "Can't open server for %s" gnus-newsgroup-name))
4305         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4306           
4307       ;; Enter these new marks into the info of the group.
4308       (if (nthcdr 3 info)
4309           (setcar (nthcdr 3 info) newmarked)
4310         ;; Add the marks lists to the end of the info.
4311         (when newmarked
4312           (setcdr (nthcdr 2 info) (list newmarked))))
4313
4314       ;; Cut off the end of the info if there's nothing else there.
4315       (let ((i 5))
4316         (while (and (> i 2)
4317                     (not (nth i info)))
4318           (when (nthcdr (decf i) info)
4319             (setcdr (nthcdr i info) nil)))))))
4320
4321 (defun gnus-set-mode-line (where)
4322   "This function sets the mode line of the article or summary buffers.
4323 If WHERE is `summary', the summary mode line format will be used."
4324   ;; Is this mode line one we keep updated?
4325   (when (and (memq where gnus-updated-mode-lines)
4326              (symbol-value
4327               (intern (format "gnus-%s-mode-line-format-spec" where))))
4328     (let (mode-string)
4329       (save-excursion
4330         ;; We evaluate this in the summary buffer since these
4331         ;; variables are buffer-local to that buffer.
4332         (set-buffer gnus-summary-buffer)
4333         ;; We bind all these variables that are used in the `eval' form
4334         ;; below.
4335         (let* ((mformat (symbol-value
4336                          (intern
4337                           (format "gnus-%s-mode-line-format-spec" where))))
4338                (gnus-tmp-group-name gnus-newsgroup-name)
4339                (gnus-tmp-article-number (or gnus-current-article 0))
4340                (gnus-tmp-unread gnus-newsgroup-unreads)
4341                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4342                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4343                (gnus-tmp-unread-and-unselected
4344                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4345                             (zerop gnus-tmp-unselected))
4346                        "")
4347                       ((zerop gnus-tmp-unselected)
4348                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4349                       (t (format "{%d(+%d) more}"
4350                                  gnus-tmp-unread-and-unticked
4351                                  gnus-tmp-unselected))))
4352                (gnus-tmp-subject
4353                 (if (and gnus-current-headers
4354                          (vectorp gnus-current-headers))
4355                     (gnus-mode-string-quote
4356                      (mail-header-subject gnus-current-headers))
4357                   ""))
4358                bufname-length max-len
4359                gnus-tmp-header);; passed as argument to any user-format-funcs
4360           (setq mode-string (eval mformat))
4361           (setq bufname-length (if (string-match "%b" mode-string)
4362                                    (- (length
4363                                        (buffer-name
4364                                         (if (eq where 'summary)
4365                                             nil
4366                                           (get-buffer gnus-article-buffer))))
4367                                       2)
4368                                  0))
4369           (setq max-len (max 4 (if gnus-mode-non-string-length
4370                                    (- (window-width)
4371                                       gnus-mode-non-string-length
4372                                       bufname-length)
4373                                  (length mode-string))))
4374           ;; We might have to chop a bit of the string off...
4375           (when (> (length mode-string) max-len)
4376             (setq mode-string
4377                   (concat (gnus-truncate-string mode-string (- max-len 3))
4378                           "...")))
4379           ;; Pad the mode string a bit.
4380           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4381       ;; Update the mode line.
4382       (setq mode-line-buffer-identification
4383             (gnus-mode-line-buffer-identification (list mode-string)))
4384       (set-buffer-modified-p t))))
4385
4386 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4387   "Go through the HEADERS list and add all Xrefs to a hash table.
4388 The resulting hash table is returned, or nil if no Xrefs were found."
4389   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4390          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4391          (xref-hashtb (gnus-make-hashtable))
4392          start group entry number xrefs header)
4393     (while headers
4394       (setq header (pop headers))
4395       (when (and (setq xrefs (mail-header-xref header))
4396                  (not (memq (setq number (mail-header-number header))
4397                             unreads)))
4398         (setq start 0)
4399         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4400           (setq start (match-end 0))
4401           (setq group (if prefix
4402                           (concat prefix (substring xrefs (match-beginning 1)
4403                                                     (match-end 1)))
4404                         (substring xrefs (match-beginning 1) (match-end 1))))
4405           (setq number
4406                 (string-to-int (substring xrefs (match-beginning 2)
4407                                           (match-end 2))))
4408           (if (setq entry (gnus-gethash group xref-hashtb))
4409               (setcdr entry (cons number (cdr entry)))
4410             (gnus-sethash group (cons number nil) xref-hashtb)))))
4411     (and start xref-hashtb)))
4412
4413 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4414   "Look through all the headers and mark the Xrefs as read."
4415   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4416         name entry info xref-hashtb idlist method nth4)
4417     (save-excursion
4418       (set-buffer gnus-group-buffer)
4419       (when (setq xref-hashtb
4420                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4421         (mapatoms
4422          (lambda (group)
4423            (unless (string= from-newsgroup (setq name (symbol-name group)))
4424              (setq idlist (symbol-value group))
4425              ;; Dead groups are not updated.
4426              (and (prog1
4427                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4428                             info (nth 2 entry))
4429                     (when (stringp (setq nth4 (gnus-info-method info)))
4430                       (setq nth4 (gnus-server-to-method nth4))))
4431                   ;; Only do the xrefs if the group has the same
4432                   ;; select method as the group we have just read.
4433                   (or (gnus-methods-equal-p
4434                        nth4 (gnus-find-method-for-group from-newsgroup))
4435                       virtual
4436                       (equal nth4 (setq method (gnus-find-method-for-group
4437                                                 from-newsgroup)))
4438                       (and (equal (car nth4) (car method))
4439                            (equal (nth 1 nth4) (nth 1 method))))
4440                   gnus-use-cross-reference
4441                   (or (not (eq gnus-use-cross-reference t))
4442                       virtual
4443                       ;; Only do cross-references on subscribed
4444                       ;; groups, if that is what is wanted.
4445                       (<= (gnus-info-level info) gnus-level-subscribed))
4446                   (gnus-group-make-articles-read name idlist))))
4447          xref-hashtb)))))
4448
4449 (defun gnus-compute-read-articles (group articles)
4450   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4451          (info (nth 2 entry))
4452          (active (gnus-active group))
4453          ninfo)
4454     (when entry
4455       ;; First peel off all invalid article numbers.
4456       (when active
4457         (let ((ids articles)
4458               id first)
4459           (while (setq id (pop ids))
4460             (when (and first (> id (cdr active)))
4461               ;; We'll end up in this situation in one particular
4462               ;; obscure situation.  If you re-scan a group and get
4463               ;; a new article that is cross-posted to a different
4464               ;; group that has not been re-scanned, you might get
4465               ;; crossposted article that has a higher number than
4466               ;; Gnus believes possible.  So we re-activate this
4467               ;; group as well.  This might mean doing the
4468               ;; crossposting thingy will *increase* the number
4469               ;; of articles in some groups.  Tsk, tsk.
4470               (setq active (or (gnus-activate-group group) active)))
4471             (when (or (> id (cdr active))
4472                       (< id (car active)))
4473               (setq articles (delq id articles))))))
4474       ;; If the read list is nil, we init it.
4475       (if (and active
4476                (null (gnus-info-read info))
4477                (> (car active) 1))
4478           (setq ninfo (cons 1 (1- (car active))))
4479         (setq ninfo (gnus-info-read info)))
4480       ;; Then we add the read articles to the range.
4481       (gnus-add-to-range
4482        ninfo (setq articles (sort articles '<))))))
4483
4484 (defun gnus-group-make-articles-read (group articles)
4485   "Update the info of GROUP to say that ARTICLES are read."
4486   (let* ((num 0)
4487          (entry (gnus-gethash group gnus-newsrc-hashtb))
4488          (info (nth 2 entry))
4489          (active (gnus-active group))
4490          range)
4491     (when entry
4492       (setq range (gnus-compute-read-articles group articles))
4493       (save-excursion
4494         (set-buffer gnus-group-buffer)
4495         (gnus-undo-register
4496           `(progn
4497              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4498              (gnus-info-set-read ',info ',(gnus-info-read info))
4499              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4500              (gnus-group-update-group ,group t))))
4501       ;; Add the read articles to the range.
4502       (gnus-info-set-read info range)
4503       ;; Then we have to re-compute how many unread
4504       ;; articles there are in this group.
4505       (when active
4506         (cond
4507          ((not range)
4508           (setq num (- (1+ (cdr active)) (car active))))
4509          ((not (listp (cdr range)))
4510           (setq num (- (cdr active) (- (1+ (cdr range))
4511                                        (car range)))))
4512          (t
4513           (while range
4514             (if (numberp (car range))
4515                 (setq num (1+ num))
4516               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4517             (setq range (cdr range)))
4518           (setq num (- (cdr active) num))))
4519         ;; Update the number of unread articles.
4520         (setcar entry num)
4521         ;; Update the group buffer.
4522         (gnus-group-update-group group t)))))
4523
4524 (defvar gnus-newsgroup-none-id 0)
4525
4526 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4527   (let ((cur nntp-server-buffer)
4528         (dependencies
4529          (or dependencies
4530              (save-excursion (set-buffer gnus-summary-buffer)
4531                              gnus-newsgroup-dependencies)))
4532         headers id end ref
4533         (mail-parse-charset gnus-newsgroup-charset)
4534         (mail-parse-ignored-charsets
4535          (save-excursion (condition-case nil
4536                              (set-buffer gnus-summary-buffer)
4537                            (error))
4538                          gnus-newsgroup-ignored-charsets)))
4539     (save-excursion
4540       (set-buffer nntp-server-buffer)
4541       ;; Translate all TAB characters into SPACE characters.
4542       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4543       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4544       (gnus-run-hooks 'gnus-parse-headers-hook)
4545       (let ((case-fold-search t)
4546             in-reply-to header p lines chars ctype)
4547         (goto-char (point-min))
4548         ;; Search to the beginning of the next header.  Error messages
4549         ;; do not begin with 2 or 3.
4550         (while (re-search-forward "^[23][0-9]+ " nil t)
4551           (setq id nil
4552                 ref nil)
4553           ;; This implementation of this function, with nine
4554           ;; search-forwards instead of the one re-search-forward and
4555           ;; a case (which basically was the old function) is actually
4556           ;; about twice as fast, even though it looks messier.  You
4557           ;; can't have everything, I guess.  Speed and elegance
4558           ;; doesn't always go hand in hand.
4559           (setq
4560            header
4561            (make-full-mail-header
4562             ;; Number.
4563             (prog1
4564                 (read cur)
4565               (end-of-line)
4566               (setq p (point))
4567               (narrow-to-region (point)
4568                                 (or (and (search-forward "\n.\n" nil t)
4569                                          (- (point) 2))
4570                                     (point))))
4571             ;; Subject.
4572             (progn
4573               (goto-char p)
4574               (if (search-forward "\nsubject: " nil t)
4575                   (buffer-substring (match-end 0) (std11-field-end))
4576                 "(none)"))
4577             ;; From.
4578             (progn
4579               (goto-char p)
4580               (if (search-forward "\nfrom: " nil t)
4581                   (buffer-substring (match-end 0) (std11-field-end))
4582                 "(nobody)"))
4583             ;; Date.
4584             (progn
4585               (goto-char p)
4586               (if (search-forward "\ndate: " nil t)
4587                   (buffer-substring (match-end 0) (std11-field-end))
4588                 ""))
4589             ;; Message-ID.
4590             (progn
4591               (goto-char p)
4592               (setq id (if (re-search-forward
4593                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4594                            ;; We do it this way to make sure the Message-ID
4595                            ;; is (somewhat) syntactically valid.
4596                            (buffer-substring (match-beginning 1)
4597                                              (match-end 1))
4598                          ;; If there was no message-id, we just fake one
4599                          ;; to make subsequent routines simpler.
4600                          (nnheader-generate-fake-message-id))))
4601             ;; References.
4602             (progn
4603               (goto-char p)
4604               (if (search-forward "\nreferences: " nil t)
4605                   (progn
4606                     (setq end (point))
4607                     (prog1
4608                         (buffer-substring (match-end 0) (std11-field-end))
4609                       (setq ref
4610                             (buffer-substring
4611                              (progn
4612                                ;; (end-of-line)
4613                                (search-backward ">" end t)
4614                                (1+ (point)))
4615                              (progn
4616                                (search-backward "<" end t)
4617                                (point))))))
4618                 ;; Get the references from the in-reply-to header if there
4619                 ;; were no references and the in-reply-to header looks
4620                 ;; promising.
4621                 (if (and (search-forward "\nin-reply-to: " nil t)
4622                          (setq in-reply-to
4623                                (buffer-substring (match-end 0)
4624                                                  (std11-field-end)))
4625                          (string-match "<[^>]+>" in-reply-to))
4626                     (let (ref2)
4627                       (setq ref (substring in-reply-to (match-beginning 0)
4628                                            (match-end 0)))
4629                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4630                         (setq ref2 (substring in-reply-to (match-beginning 0)
4631                                               (match-end 0)))
4632                         (when (> (length ref2) (length ref))
4633                           (setq ref ref2)))
4634                       ref)
4635                   (setq ref nil))))
4636             ;; Chars.
4637             (progn
4638               (goto-char p)
4639               (if (search-forward "\nchars: " nil t)
4640                   (if (numberp (setq chars (ignore-errors (read cur))))
4641                       chars 0)
4642                 0))
4643             ;; Lines.
4644             (progn
4645               (goto-char p)
4646               (if (search-forward "\nlines: " nil t)
4647                   (if (numberp (setq lines (ignore-errors (read cur))))
4648                       lines 0)
4649                 0))
4650             ;; Xref.
4651             (progn
4652               (goto-char p)
4653               (and (search-forward "\nxref: " nil t)
4654                    (buffer-substring (match-end 0) (std11-field-end))))
4655             ;; Extra.
4656             (when gnus-extra-headers
4657               (let ((extra gnus-extra-headers)
4658                     out)
4659                 (while extra
4660                   (goto-char p)
4661                   (when (search-forward
4662                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4663                     (push (cons (car extra)
4664                                 (buffer-substring (match-end 0)
4665                                                   (std11-field-end)))
4666                           out))
4667                   (pop extra))
4668                 out))))
4669           (goto-char p)
4670           (if (and (search-forward "\ncontent-type: " nil t)
4671                    (setq ctype
4672                          (buffer-substring (match-end 0) (std11-field-end))))
4673               (mime-entity-set-content-type-internal
4674                header (mime-parse-Content-Type ctype)))
4675           (when (equal id ref)
4676             (setq ref nil))
4677
4678           (when gnus-alter-header-function
4679             (funcall gnus-alter-header-function header)
4680             (setq id (mail-header-id header)
4681                   ref (gnus-parent-id (mail-header-references header))))
4682
4683           (when (setq header
4684                       (gnus-dependencies-add-header
4685                        header dependencies force-new))
4686             (push header headers))
4687           (goto-char (point-max))
4688           (widen))
4689         (nreverse headers)))))
4690
4691 ;; Goes through the xover lines and returns a list of vectors
4692 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4693                                                   force-new dependencies
4694                                                   group also-fetch-heads)
4695   "Parse the news overview data in the server buffer, and return a
4696 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4697   ;; Get the Xref when the users reads the articles since most/some
4698   ;; NNTP servers do not include Xrefs when using XOVER.
4699   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4700   (let ((mail-parse-charset gnus-newsgroup-charset)
4701         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4702         (cur nntp-server-buffer)
4703         (dependencies (or dependencies gnus-newsgroup-dependencies))
4704         number headers header)
4705     (save-excursion
4706       (set-buffer nntp-server-buffer)
4707       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4708       ;; Allow the user to mangle the headers before parsing them.
4709       (gnus-run-hooks 'gnus-parse-headers-hook)
4710       (goto-char (point-min))
4711       (while (not (eobp))
4712         (condition-case ()
4713             (while (and sequence (not (eobp)))
4714               (setq number (read cur))
4715               (while (and sequence
4716                           (< (car sequence) number))
4717                 (setq sequence (cdr sequence)))
4718               (and sequence
4719                    (eq number (car sequence))
4720                    (progn
4721                      (setq sequence (cdr sequence))
4722                      (setq header (inline
4723                                     (gnus-nov-parse-line
4724                                      number dependencies force-new))))
4725                    (push header headers))
4726               (forward-line 1))
4727           (error
4728            (gnus-error 4 "Strange nov line (%d)"
4729                        (count-lines (point-min) (point)))))
4730         (forward-line 1))
4731       ;; A common bug in inn is that if you have posted an article and
4732       ;; then retrieves the active file, it will answer correctly --
4733       ;; the new article is included.  However, a NOV entry for the
4734       ;; article may not have been generated yet, so this may fail.
4735       ;; We work around this problem by retrieving the last few
4736       ;; headers using HEAD.
4737       (if (or (not also-fetch-heads)
4738               (not sequence))
4739           ;; We (probably) got all the headers.
4740           (nreverse headers)
4741         (let ((gnus-nov-is-evil t))
4742           (nconc
4743            (nreverse headers)
4744            (gnus-retrieve-parsed-headers sequence group)
4745            ))))))
4746
4747 (defun gnus-article-get-xrefs ()
4748   "Fill in the Xref value in `gnus-current-headers', if necessary.
4749 This is meant to be called in `gnus-article-internal-prepare-hook'."
4750   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4751                                  gnus-current-headers)))
4752     (or (not gnus-use-cross-reference)
4753         (not headers)
4754         (and (mail-header-xref headers)
4755              (not (string= (mail-header-xref headers) "")))
4756         (let ((case-fold-search t)
4757               xref)
4758           (save-restriction
4759             (nnheader-narrow-to-headers)
4760             (goto-char (point-min))
4761             (when (or (and (eq (downcase (char-after)) ?x)
4762                            (looking-at "Xref:"))
4763                       (search-forward "\nXref:" nil t))
4764               (goto-char (1+ (match-end 0)))
4765               (setq xref (buffer-substring (point)
4766                                            (progn (end-of-line) (point))))
4767               (mail-header-set-xref headers xref)))))))
4768
4769 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4770   "Find article ID and insert the summary line for that article.
4771 OLD-HEADER can either be a header or a line number to insert
4772 the subject line on."
4773   (let* ((line (and (numberp old-header) old-header))
4774          (old-header (and (vectorp old-header) old-header))
4775          (header (cond ((and old-header use-old-header)
4776                         old-header)
4777                        ((and (numberp id)
4778                              (gnus-number-to-header id))
4779                         (gnus-number-to-header id))
4780                        (t
4781                         (gnus-read-header id))))
4782          (number (and (numberp id) id))
4783          d)
4784     (when header
4785       ;; Rebuild the thread that this article is part of and go to the
4786       ;; article we have fetched.
4787       (when (and (not gnus-show-threads)
4788                  old-header)
4789         (when (and number
4790                    (setq d (gnus-data-find (mail-header-number old-header))))
4791           (goto-char (gnus-data-pos d))
4792           (gnus-data-remove
4793            number
4794            (- (gnus-point-at-bol)
4795               (prog1
4796                   (1+ (gnus-point-at-eol))
4797                 (gnus-delete-line))))))
4798       (when old-header
4799         (mail-header-set-number header (mail-header-number old-header)))
4800       (setq gnus-newsgroup-sparse
4801             (delq (setq number (mail-header-number header))
4802                   gnus-newsgroup-sparse))
4803       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4804       (push number gnus-newsgroup-limit)
4805       (gnus-rebuild-thread (mail-header-id header) line)
4806       (gnus-summary-goto-subject number nil t))
4807     (when (and (numberp number)
4808                (> number 0))
4809       ;; We have to update the boundaries even if we can't fetch the
4810       ;; article if ID is a number -- so that the next `P' or `N'
4811       ;; command will fetch the previous (or next) article even
4812       ;; if the one we tried to fetch this time has been canceled.
4813       (when (> number gnus-newsgroup-end)
4814         (setq gnus-newsgroup-end number))
4815       (when (< number gnus-newsgroup-begin)
4816         (setq gnus-newsgroup-begin number))
4817       (setq gnus-newsgroup-unselected
4818             (delq number gnus-newsgroup-unselected)))
4819     ;; Report back a success?
4820     (and header (mail-header-number header))))
4821
4822 ;;; Process/prefix in the summary buffer
4823
4824 (defun gnus-summary-work-articles (n)
4825   "Return a list of articles to be worked upon.
4826 The prefix argument, the list of process marked articles, and the
4827 current article will be taken into consideration."
4828   (save-excursion
4829     (set-buffer gnus-summary-buffer)
4830     (cond
4831      (n
4832       ;; A numerical prefix has been given.
4833       (setq n (prefix-numeric-value n))
4834       (let ((backward (< n 0))
4835             (n (abs (prefix-numeric-value n)))
4836             articles article)
4837         (save-excursion
4838           (while
4839               (and (> n 0)
4840                    (push (setq article (gnus-summary-article-number))
4841                          articles)
4842                    (if backward
4843                        (gnus-summary-find-prev nil article)
4844                      (gnus-summary-find-next nil article)))
4845             (decf n)))
4846         (nreverse articles)))
4847      ((and (gnus-region-active-p) (mark))
4848       (message "region active")
4849       ;; Work on the region between point and mark.
4850       (let ((max (max (point) (mark)))
4851             articles article)
4852         (save-excursion
4853           (goto-char (min (point) (mark)))
4854           (while
4855               (and
4856                (push (setq article (gnus-summary-article-number)) articles)
4857                (gnus-summary-find-next nil article)
4858                (< (point) max)))
4859           (nreverse articles))))
4860      (gnus-newsgroup-processable
4861       ;; There are process-marked articles present.
4862       ;; Save current state.
4863       (gnus-summary-save-process-mark)
4864       ;; Return the list.
4865       (reverse gnus-newsgroup-processable))
4866      (t
4867       ;; Just return the current article.
4868       (list (gnus-summary-article-number))))))
4869
4870 (defmacro gnus-summary-iterate (arg &rest forms)
4871   "Iterate over the process/prefixed articles and do FORMS.
4872 ARG is the interactive prefix given to the command.  FORMS will be
4873 executed with point over the summary line of the articles."
4874   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4875     `(let ((,articles (gnus-summary-work-articles ,arg)))
4876        (while ,articles
4877          (gnus-summary-goto-subject (car ,articles))
4878          ,@forms
4879          (pop ,articles)))))
4880
4881 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4882 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4883
4884 (defun gnus-summary-save-process-mark ()
4885   "Push the current set of process marked articles on the stack."
4886   (interactive)
4887   (push (copy-sequence gnus-newsgroup-processable)
4888         gnus-newsgroup-process-stack))
4889
4890 (defun gnus-summary-kill-process-mark ()
4891   "Push the current set of process marked articles on the stack and unmark."
4892   (interactive)
4893   (gnus-summary-save-process-mark)
4894   (gnus-summary-unmark-all-processable))
4895
4896 (defun gnus-summary-yank-process-mark ()
4897   "Pop the last process mark state off the stack and restore it."
4898   (interactive)
4899   (unless gnus-newsgroup-process-stack
4900     (error "Empty mark stack"))
4901   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4902
4903 (defun gnus-summary-process-mark-set (set)
4904   "Make SET into the current process marked articles."
4905   (gnus-summary-unmark-all-processable)
4906   (while set
4907     (gnus-summary-set-process-mark (pop set))))
4908
4909 ;;; Searching and stuff
4910
4911 (defun gnus-summary-search-group (&optional backward use-level)
4912   "Search for next unread newsgroup.
4913 If optional argument BACKWARD is non-nil, search backward instead."
4914   (save-excursion
4915     (set-buffer gnus-group-buffer)
4916     (when (gnus-group-search-forward
4917            backward nil (if use-level (gnus-group-group-level) nil))
4918       (gnus-group-group-name))))
4919
4920 (defun gnus-summary-best-group (&optional exclude-group)
4921   "Find the name of the best unread group.
4922 If EXCLUDE-GROUP, do not go to this group."
4923   (save-excursion
4924     (set-buffer gnus-group-buffer)
4925     (save-excursion
4926       (gnus-group-best-unread-group exclude-group))))
4927
4928 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4929   (if backward (gnus-summary-find-prev)
4930     (let* ((dummy (gnus-summary-article-intangible-p))
4931            (article (or article (gnus-summary-article-number)))
4932            (arts (gnus-data-find-list article))
4933            result)
4934       (when (and (not dummy)
4935                  (or (not gnus-summary-check-current)
4936                      (not unread)
4937                      (not (gnus-data-unread-p (car arts)))))
4938         (setq arts (cdr arts)))
4939       (when (setq result
4940                   (if unread
4941                       (progn
4942                         (while arts
4943                           (when (or (and undownloaded
4944                                          (eq gnus-undownloaded-mark
4945                                              (gnus-data-mark (car arts))))
4946                                     (gnus-data-unread-p (car arts)))
4947                             (setq result (car arts)
4948                                   arts nil))
4949                           (setq arts (cdr arts)))
4950                         result)
4951                     (car arts)))
4952         (goto-char (gnus-data-pos result))
4953         (gnus-data-number result)))))
4954
4955 (defun gnus-summary-find-prev (&optional unread article)
4956   (let* ((eobp (eobp))
4957          (article (or article (gnus-summary-article-number)))
4958          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4959          result)
4960     (when (and (not eobp)
4961                (or (not gnus-summary-check-current)
4962                    (not unread)
4963                    (not (gnus-data-unread-p (car arts)))))
4964       (setq arts (cdr arts)))
4965     (when (setq result
4966                 (if unread
4967                     (progn
4968                       (while arts
4969                         (when (gnus-data-unread-p (car arts))
4970                           (setq result (car arts)
4971                                 arts nil))
4972                         (setq arts (cdr arts)))
4973                       result)
4974                   (car arts)))
4975       (goto-char (gnus-data-pos result))
4976       (gnus-data-number result))))
4977
4978 (defun gnus-summary-find-subject (subject &optional unread backward article)
4979   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4980          (article (or article (gnus-summary-article-number)))
4981          (articles (gnus-data-list backward))
4982          (arts (gnus-data-find-list article articles))
4983          result)
4984     (when (or (not gnus-summary-check-current)
4985               (not unread)
4986               (not (gnus-data-unread-p (car arts))))
4987       (setq arts (cdr arts)))
4988     (while arts
4989       (and (or (not unread)
4990                (gnus-data-unread-p (car arts)))
4991            (vectorp (gnus-data-header (car arts)))
4992            (gnus-subject-equal
4993             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4994            (setq result (car arts)
4995                  arts nil))
4996       (setq arts (cdr arts)))
4997     (and result
4998          (goto-char (gnus-data-pos result))
4999          (gnus-data-number result))))
5000
5001 (defun gnus-summary-search-forward (&optional unread subject backward)
5002   "Search forward for an article.
5003 If UNREAD, look for unread articles.  If SUBJECT, look for
5004 articles with that subject.  If BACKWARD, search backward instead."
5005   (cond (subject (gnus-summary-find-subject subject unread backward))
5006         (backward (gnus-summary-find-prev unread))
5007         (t (gnus-summary-find-next unread))))
5008
5009 (defun gnus-recenter (&optional n)
5010   "Center point in window and redisplay frame.
5011 Also do horizontal recentering."
5012   (interactive "P")
5013   (when (and gnus-auto-center-summary
5014              (not (eq gnus-auto-center-summary 'vertical)))
5015     (gnus-horizontal-recenter))
5016   (recenter n))
5017
5018 (defun gnus-summary-recenter ()
5019   "Center point in the summary window.
5020 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5021 displayed, no centering will be performed."
5022   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5023   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5024   (let* ((top (cond ((< (window-height) 4) 0)
5025                     ((< (window-height) 7) 1)
5026                     (t (if (numberp gnus-auto-center-summary)
5027                            gnus-auto-center-summary
5028                          2))))
5029          (height (1- (window-height)))
5030          (bottom (save-excursion (goto-char (point-max))
5031                                  (forward-line (- height))
5032                                  (point)))
5033          (window (get-buffer-window (current-buffer))))
5034     ;; The user has to want it.
5035     (when gnus-auto-center-summary
5036       (when (get-buffer-window gnus-article-buffer)
5037         ;; Only do recentering when the article buffer is displayed,
5038         ;; Set the window start to either `bottom', which is the biggest
5039         ;; possible valid number, or the second line from the top,
5040         ;; whichever is the least.
5041         (set-window-start
5042          window (min bottom (save-excursion
5043                               (forward-line (- top)) (point)))))
5044       ;; Do horizontal recentering while we're at it.
5045       (when (and (get-buffer-window (current-buffer) t)
5046                  (not (eq gnus-auto-center-summary 'vertical)))
5047         (let ((selected (selected-window)))
5048           (select-window (get-buffer-window (current-buffer) t))
5049           (gnus-summary-position-point)
5050           (gnus-horizontal-recenter)
5051           (select-window selected))))))
5052
5053 (defun gnus-summary-jump-to-group (newsgroup)
5054   "Move point to NEWSGROUP in group mode buffer."
5055   ;; Keep update point of group mode buffer if visible.
5056   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5057       (save-window-excursion
5058         ;; Take care of tree window mode.
5059         (when (get-buffer-window gnus-group-buffer)
5060           (pop-to-buffer gnus-group-buffer))
5061         (gnus-group-jump-to-group newsgroup))
5062     (save-excursion
5063       ;; Take care of tree window mode.
5064       (if (get-buffer-window gnus-group-buffer)
5065           (pop-to-buffer gnus-group-buffer)
5066         (set-buffer gnus-group-buffer))
5067       (gnus-group-jump-to-group newsgroup))))
5068
5069 ;; This function returns a list of article numbers based on the
5070 ;; difference between the ranges of read articles in this group and
5071 ;; the range of active articles.
5072 (defun gnus-list-of-unread-articles (group)
5073   (let* ((read (gnus-info-read (gnus-get-info group)))
5074          (active (or (gnus-active group) (gnus-activate-group group)))
5075          (last (cdr active))
5076          first nlast unread)
5077     ;; If none are read, then all are unread.
5078     (if (not read)
5079         (setq first (car active))
5080       ;; If the range of read articles is a single range, then the
5081       ;; first unread article is the article after the last read
5082       ;; article.  Sounds logical, doesn't it?
5083       (if (not (listp (cdr read)))
5084           (setq first (max (car active) (1+ (cdr read))))
5085         ;; `read' is a list of ranges.
5086         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5087                                   (caar read)))
5088                   1)
5089           (setq first (car active)))
5090         (while read
5091           (when first
5092             (while (< first nlast)
5093               (push first unread)
5094               (setq first (1+ first))))
5095           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5096           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5097           (setq read (cdr read)))))
5098     ;; And add the last unread articles.
5099     (while (<= first last)
5100       (push first unread)
5101       (setq first (1+ first)))
5102     ;; Return the list of unread articles.
5103     (delq 0 (nreverse unread))))
5104
5105 (defun gnus-list-of-read-articles (group)
5106   "Return a list of unread, unticked and non-dormant articles."
5107   (let* ((info (gnus-get-info group))
5108          (marked (gnus-info-marks info))
5109          (active (gnus-active group)))
5110     (and info active
5111          (gnus-set-difference
5112           (gnus-sorted-complement
5113            (gnus-uncompress-range active)
5114            (gnus-list-of-unread-articles group))
5115           (append
5116            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5117            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5118
5119 ;; Various summary commands
5120
5121 (defun gnus-summary-select-article-buffer ()
5122   "Reconfigure windows to show article buffer."
5123   (interactive)
5124   (if (not (gnus-buffer-live-p gnus-article-buffer))
5125       (error "There is no article buffer for this summary buffer")
5126     (gnus-configure-windows 'article)
5127     (select-window (get-buffer-window gnus-article-buffer))))
5128
5129 (defun gnus-summary-universal-argument (arg)
5130   "Perform any operation on all articles that are process/prefixed."
5131   (interactive "P")
5132   (let ((articles (gnus-summary-work-articles arg))
5133         func article)
5134     (if (eq
5135          (setq
5136           func
5137           (key-binding
5138            (read-key-sequence
5139             (substitute-command-keys
5140              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5141              ))))
5142          'undefined)
5143         (gnus-error 1 "Undefined key")
5144       (save-excursion
5145         (while articles
5146           (gnus-summary-goto-subject (setq article (pop articles)))
5147           (let (gnus-newsgroup-processable)
5148             (command-execute func))
5149           (gnus-summary-remove-process-mark article)))))
5150   (gnus-summary-position-point))
5151
5152 (defun gnus-summary-toggle-truncation (&optional arg)
5153   "Toggle truncation of summary lines.
5154 With arg, turn line truncation on iff arg is positive."
5155   (interactive "P")
5156   (setq truncate-lines
5157         (if (null arg) (not truncate-lines)
5158           (> (prefix-numeric-value arg) 0)))
5159   (redraw-display))
5160
5161 (defun gnus-summary-reselect-current-group (&optional all rescan)
5162   "Exit and then reselect the current newsgroup.
5163 The prefix argument ALL means to select all articles."
5164   (interactive "P")
5165   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5166     (error "Ephemeral groups can't be reselected"))
5167   (let ((current-subject (gnus-summary-article-number))
5168         (group gnus-newsgroup-name))
5169     (setq gnus-newsgroup-begin nil)
5170     (gnus-summary-exit)
5171     ;; We have to adjust the point of group mode buffer because
5172     ;; point was moved to the next unread newsgroup by exiting.
5173     (gnus-summary-jump-to-group group)
5174     (when rescan
5175       (save-excursion
5176         (gnus-group-get-new-news-this-group 1)))
5177     (gnus-group-read-group all t)
5178     (gnus-summary-goto-subject current-subject nil t)))
5179
5180 (defun gnus-summary-rescan-group (&optional all)
5181   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5182   (interactive "P")
5183   (gnus-summary-reselect-current-group all t))
5184
5185 (defun gnus-summary-update-info (&optional non-destructive)
5186   (save-excursion
5187     (let ((group gnus-newsgroup-name))
5188       (when group
5189         (when gnus-newsgroup-kill-headers
5190           (setq gnus-newsgroup-killed
5191                 (gnus-compress-sequence
5192                  (nconc
5193                   (gnus-set-sorted-intersection
5194                    (gnus-uncompress-range gnus-newsgroup-killed)
5195                    (setq gnus-newsgroup-unselected
5196                          (sort gnus-newsgroup-unselected '<)))
5197                   (setq gnus-newsgroup-unreads
5198                         (sort gnus-newsgroup-unreads '<)))
5199                  t)))
5200         (unless (listp (cdr gnus-newsgroup-killed))
5201           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5202         (let ((headers gnus-newsgroup-headers))
5203           ;; Set the new ranges of read articles.
5204           (save-excursion
5205             (set-buffer gnus-group-buffer)
5206             (gnus-undo-force-boundary))
5207           (gnus-update-read-articles
5208            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5209           ;; Set the current article marks.
5210           (let ((gnus-newsgroup-scored
5211                  (if (and (not gnus-save-score)
5212                           (not non-destructive))
5213                      nil
5214                    gnus-newsgroup-scored)))
5215             (save-excursion
5216               (gnus-update-marks)))
5217           ;; Do the cross-ref thing.
5218           (when gnus-use-cross-reference
5219             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5220           ;; Do not switch windows but change the buffer to work.
5221           (set-buffer gnus-group-buffer)
5222           (unless (gnus-ephemeral-group-p group)
5223             (gnus-group-update-group group)))))))
5224
5225 (defun gnus-summary-save-newsrc (&optional force)
5226   "Save the current number of read/marked articles in the dribble buffer.
5227 The dribble buffer will then be saved.
5228 If FORCE (the prefix), also save the .newsrc file(s)."
5229   (interactive "P")
5230   (gnus-summary-update-info t)
5231   (if force
5232       (gnus-save-newsrc-file)
5233     (gnus-dribble-save)))
5234
5235 (defun gnus-summary-exit (&optional temporary)
5236   "Exit reading current newsgroup, and then return to group selection mode.
5237 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5238   (interactive)
5239   (gnus-set-global-variables)
5240   (gnus-kill-save-kill-buffer)
5241   (gnus-async-halt-prefetch)
5242   (let* ((group gnus-newsgroup-name)
5243          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5244          (mode major-mode)
5245          (group-point nil)
5246          (buf (current-buffer)))
5247     (unless quit-config
5248       ;; Do adaptive scoring, and possibly save score files.
5249       (when gnus-newsgroup-adaptive
5250         (gnus-score-adaptive))
5251       (when gnus-use-scoring
5252         (gnus-score-save)))
5253     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5254     ;; If we have several article buffers, we kill them at exit.
5255     (unless gnus-single-article-buffer
5256       (gnus-kill-buffer gnus-original-article-buffer)
5257       (setq gnus-article-current nil))
5258     (when gnus-use-cache
5259       (gnus-cache-possibly-remove-articles)
5260       (gnus-cache-save-buffers))
5261     (gnus-async-prefetch-remove-group group)
5262     (when gnus-suppress-duplicates
5263       (gnus-dup-enter-articles))
5264     (when gnus-use-trees
5265       (gnus-tree-close group))
5266     ;; Remove entries for this group.
5267     (nnmail-purge-split-history (gnus-group-real-name group))
5268     ;; Make all changes in this group permanent.
5269     (unless quit-config
5270       (gnus-run-hooks 'gnus-exit-group-hook)
5271       (gnus-summary-update-info))
5272     (gnus-close-group group)
5273     ;; Make sure where we were, and go to next newsgroup.
5274     (set-buffer gnus-group-buffer)
5275     (unless quit-config
5276       (gnus-group-jump-to-group group))
5277     (gnus-run-hooks 'gnus-summary-exit-hook)
5278     (unless (or quit-config
5279                 ;; If this group has disappeared from the summary
5280                 ;; buffer, don't skip forwards.
5281                 (not (string= group (gnus-group-group-name))))
5282       (gnus-group-next-unread-group 1))
5283     (setq group-point (point))
5284     (if temporary
5285         nil                             ;Nothing to do.
5286       ;; If we have several article buffers, we kill them at exit.
5287       (unless gnus-single-article-buffer
5288         (gnus-kill-buffer gnus-article-buffer)
5289         (gnus-kill-buffer gnus-original-article-buffer)
5290         (setq gnus-article-current nil))
5291       (set-buffer buf)
5292       (if (not gnus-kill-summary-on-exit)
5293           (gnus-deaden-summary)
5294         ;; We set all buffer-local variables to nil.  It is unclear why
5295         ;; this is needed, but if we don't, buffer-local variables are
5296         ;; not garbage-collected, it seems.  This would the lead to en
5297         ;; ever-growing Emacs.
5298         (gnus-summary-clear-local-variables)
5299         (when (get-buffer gnus-article-buffer)
5300           (bury-buffer gnus-article-buffer))
5301         ;; We clear the global counterparts of the buffer-local
5302         ;; variables as well, just to be on the safe side.
5303         (set-buffer gnus-group-buffer)
5304         (gnus-summary-clear-local-variables)
5305         ;; Return to group mode buffer.
5306         (when (eq mode 'gnus-summary-mode)
5307           (gnus-kill-buffer buf)))
5308       (setq gnus-current-select-method gnus-select-method)
5309       (pop-to-buffer gnus-group-buffer)
5310       (if (not quit-config)
5311           (progn
5312             (goto-char group-point)
5313             (gnus-configure-windows 'group 'force))
5314         (gnus-handle-ephemeral-exit quit-config))
5315       ;; Clear the current group name.
5316       (unless quit-config
5317         (setq gnus-newsgroup-name nil)))))
5318
5319 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5320 (defun gnus-summary-exit-no-update (&optional no-questions)
5321   "Quit reading current newsgroup without updating read article info."
5322   (interactive)
5323   (let* ((group gnus-newsgroup-name)
5324          (quit-config (gnus-group-quit-config group)))
5325     (when (or no-questions
5326               gnus-expert-user
5327               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5328       (gnus-async-halt-prefetch)
5329       (mapcar 'funcall
5330               (delq 'gnus-summary-expire-articles
5331                     (copy-list gnus-summary-prepare-exit-hook)))
5332       ;; If we have several article buffers, we kill them at exit.
5333       (unless gnus-single-article-buffer
5334         (gnus-kill-buffer gnus-article-buffer)
5335         (gnus-kill-buffer gnus-original-article-buffer)
5336         (setq gnus-article-current nil))
5337       (if (not gnus-kill-summary-on-exit)
5338           (gnus-deaden-summary)
5339         (gnus-close-group group)
5340         (gnus-summary-clear-local-variables)
5341         (set-buffer gnus-group-buffer)
5342         (gnus-summary-clear-local-variables)
5343         (when (get-buffer gnus-summary-buffer)
5344           (kill-buffer gnus-summary-buffer)))
5345       (unless gnus-single-article-buffer
5346         (setq gnus-article-current nil))
5347       (when gnus-use-trees
5348         (gnus-tree-close group))
5349       (gnus-async-prefetch-remove-group group)
5350       (when (get-buffer gnus-article-buffer)
5351         (bury-buffer gnus-article-buffer))
5352       ;; Return to the group buffer.
5353       (gnus-configure-windows 'group 'force)
5354       ;; Clear the current group name.
5355       (setq gnus-newsgroup-name nil)
5356       (when (equal (gnus-group-group-name) group)
5357         (gnus-group-next-unread-group 1))
5358       (when quit-config
5359         (gnus-handle-ephemeral-exit quit-config)))))
5360
5361 (defun gnus-handle-ephemeral-exit (quit-config)
5362   "Handle movement when leaving an ephemeral group.
5363 The state which existed when entering the ephemeral is reset."
5364   (if (not (buffer-name (car quit-config)))
5365       (gnus-configure-windows 'group 'force)
5366     (set-buffer (car quit-config))
5367     (cond ((eq major-mode 'gnus-summary-mode)
5368            (gnus-set-global-variables))
5369           ((eq major-mode 'gnus-article-mode)
5370            (save-excursion
5371              ;; The `gnus-summary-buffer' variable may point
5372              ;; to the old summary buffer when using a single
5373              ;; article buffer.
5374              (unless (gnus-buffer-live-p gnus-summary-buffer)
5375                (set-buffer gnus-group-buffer))
5376              (set-buffer gnus-summary-buffer)
5377              (gnus-set-global-variables))))
5378     (if (or (eq (cdr quit-config) 'article)
5379             (eq (cdr quit-config) 'pick))
5380         (progn
5381           ;; The current article may be from the ephemeral group
5382           ;; thus it is best that we reload this article
5383           (gnus-summary-show-article)
5384           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5385               (gnus-configure-windows 'pick 'force)
5386             (gnus-configure-windows (cdr quit-config) 'force)))
5387       (gnus-configure-windows (cdr quit-config) 'force))
5388     (when (eq major-mode 'gnus-summary-mode)
5389       (gnus-summary-next-subject 1 nil t)
5390       (gnus-summary-recenter)
5391       (gnus-summary-position-point))))
5392
5393 (defun gnus-summary-preview-mime-message ()
5394   "MIME decode and play this message."
5395   (interactive)
5396   (let ((gnus-break-pages nil)
5397         (gnus-show-mime t))
5398     (gnus-summary-select-article gnus-show-all-headers t))
5399   (select-window (get-buffer-window gnus-article-buffer)))
5400
5401 ;;; Dead summaries.
5402
5403 (defvar gnus-dead-summary-mode-map nil)
5404
5405 (unless gnus-dead-summary-mode-map
5406   (setq gnus-dead-summary-mode-map (make-keymap))
5407   (suppress-keymap gnus-dead-summary-mode-map)
5408   (substitute-key-definition
5409    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5410   (let ((keys '("\C-d" "\r" "\177" [delete])))
5411     (while keys
5412       (define-key gnus-dead-summary-mode-map
5413         (pop keys) 'gnus-summary-wake-up-the-dead))))
5414
5415 (defvar gnus-dead-summary-mode nil
5416   "Minor mode for Gnus summary buffers.")
5417
5418 (defun gnus-dead-summary-mode (&optional arg)
5419   "Minor mode for Gnus summary buffers."
5420   (interactive "P")
5421   (when (eq major-mode 'gnus-summary-mode)
5422     (make-local-variable 'gnus-dead-summary-mode)
5423     (setq gnus-dead-summary-mode
5424           (if (null arg) (not gnus-dead-summary-mode)
5425             (> (prefix-numeric-value arg) 0)))
5426     (when gnus-dead-summary-mode
5427       (gnus-add-minor-mode
5428        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5429
5430 (defun gnus-deaden-summary ()
5431   "Make the current summary buffer into a dead summary buffer."
5432   ;; Kill any previous dead summary buffer.
5433   (when (and gnus-dead-summary
5434              (buffer-name gnus-dead-summary))
5435     (save-excursion
5436       (set-buffer gnus-dead-summary)
5437       (when gnus-dead-summary-mode
5438         (kill-buffer (current-buffer)))))
5439   ;; Make this the current dead summary.
5440   (setq gnus-dead-summary (current-buffer))
5441   (gnus-dead-summary-mode 1)
5442   (let ((name (buffer-name)))
5443     (when (string-match "Summary" name)
5444       (rename-buffer
5445        (concat (substring name 0 (match-beginning 0)) "Dead "
5446                (substring name (match-beginning 0)))
5447        t))))
5448
5449 (defun gnus-kill-or-deaden-summary (buffer)
5450   "Kill or deaden the summary BUFFER."
5451   (save-excursion
5452     (when (and (buffer-name buffer)
5453                (not gnus-single-article-buffer))
5454       (save-excursion
5455         (set-buffer buffer)
5456         (gnus-kill-buffer gnus-article-buffer)
5457         (gnus-kill-buffer gnus-original-article-buffer)))
5458     (cond (gnus-kill-summary-on-exit
5459            (when (and gnus-use-trees
5460                       (gnus-buffer-exists-p buffer))
5461              (save-excursion
5462                (set-buffer buffer)
5463                (gnus-tree-close gnus-newsgroup-name)))
5464            (gnus-kill-buffer buffer))
5465           ((gnus-buffer-exists-p buffer)
5466            (save-excursion
5467              (set-buffer buffer)
5468              (gnus-deaden-summary))))))
5469
5470 (defun gnus-summary-wake-up-the-dead (&rest args)
5471   "Wake up the dead summary buffer."
5472   (interactive)
5473   (gnus-dead-summary-mode -1)
5474   (let ((name (buffer-name)))
5475     (when (string-match "Dead " name)
5476       (rename-buffer
5477        (concat (substring name 0 (match-beginning 0))
5478                (substring name (match-end 0)))
5479        t)))
5480   (gnus-message 3 "This dead summary is now alive again"))
5481
5482 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5483 (defun gnus-summary-fetch-faq (&optional faq-dir)
5484   "Fetch the FAQ for the current group.
5485 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5486 in."
5487   (interactive
5488    (list
5489     (when current-prefix-arg
5490       (completing-read
5491        "Faq dir: " (and (listp gnus-group-faq-directory)
5492                         (mapcar (lambda (file) (list file))
5493                                 gnus-group-faq-directory))))))
5494   (let (gnus-faq-buffer)
5495     (when (setq gnus-faq-buffer
5496                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5497       (gnus-configure-windows 'summary-faq))))
5498
5499 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5500 (defun gnus-summary-describe-group (&optional force)
5501   "Describe the current newsgroup."
5502   (interactive "P")
5503   (gnus-group-describe-group force gnus-newsgroup-name))
5504
5505 (defun gnus-summary-describe-briefly ()
5506   "Describe summary mode commands briefly."
5507   (interactive)
5508   (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")))
5509
5510 ;; Walking around group mode buffer from summary mode.
5511
5512 (defun gnus-summary-next-group (&optional no-article target-group backward)
5513   "Exit current newsgroup and then select next unread newsgroup.
5514 If prefix argument NO-ARTICLE is non-nil, no article is selected
5515 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5516 previous group instead."
5517   (interactive "P")
5518   ;; Stop pre-fetching.
5519   (gnus-async-halt-prefetch)
5520   (let ((current-group gnus-newsgroup-name)
5521         (current-buffer (current-buffer))
5522         entered)
5523     ;; First we semi-exit this group to update Xrefs and all variables.
5524     ;; We can't do a real exit, because the window conf must remain
5525     ;; the same in case the user is prompted for info, and we don't
5526     ;; want the window conf to change before that...
5527     (gnus-summary-exit t)
5528     (while (not entered)
5529       ;; Then we find what group we are supposed to enter.
5530       (set-buffer gnus-group-buffer)
5531       (gnus-group-jump-to-group current-group)
5532       (setq target-group
5533             (or target-group
5534                 (if (eq gnus-keep-same-level 'best)
5535                     (gnus-summary-best-group gnus-newsgroup-name)
5536                   (gnus-summary-search-group backward gnus-keep-same-level))))
5537       (if (not target-group)
5538           ;; There are no further groups, so we return to the group
5539           ;; buffer.
5540           (progn
5541             (gnus-message 5 "Returning to the group buffer")
5542             (setq entered t)
5543             (when (gnus-buffer-live-p current-buffer)
5544               (set-buffer current-buffer)
5545               (gnus-summary-exit))
5546             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5547         ;; We try to enter the target group.
5548         (gnus-group-jump-to-group target-group)
5549         (let ((unreads (gnus-group-group-unread)))
5550           (if (and (or (eq t unreads)
5551                        (and unreads (not (zerop unreads))))
5552                    (gnus-summary-read-group
5553                     target-group nil no-article
5554                     (and (buffer-name current-buffer) current-buffer)
5555                     nil backward))
5556               (setq entered t)
5557             (setq current-group target-group
5558                   target-group nil)))))))
5559
5560 (defun gnus-summary-prev-group (&optional no-article)
5561   "Exit current newsgroup and then select previous unread newsgroup.
5562 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5563   (interactive "P")
5564   (gnus-summary-next-group no-article nil t))
5565
5566 ;; Walking around summary lines.
5567
5568 (defun gnus-summary-first-subject (&optional unread undownloaded)
5569   "Go to the first unread subject.
5570 If UNREAD is non-nil, go to the first unread article.
5571 Returns the article selected or nil if there are no unread articles."
5572   (interactive "P")
5573   (prog1
5574       (cond
5575        ;; Empty summary.
5576        ((null gnus-newsgroup-data)
5577         (gnus-message 3 "No articles in the group")
5578         nil)
5579        ;; Pick the first article.
5580        ((not unread)
5581         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5582         (gnus-data-number (car gnus-newsgroup-data)))
5583        ;; No unread articles.
5584        ((null gnus-newsgroup-unreads)
5585         (gnus-message 3 "No more unread articles")
5586         nil)
5587        ;; Find the first unread article.
5588        (t
5589         (let ((data gnus-newsgroup-data))
5590           (while (and data
5591                       (and (not (and undownloaded
5592                                      (eq gnus-undownloaded-mark
5593                                          (gnus-data-mark (car data)))))
5594                            (not (gnus-data-unread-p (car data)))))
5595             (setq data (cdr data)))
5596           (when data
5597             (goto-char (gnus-data-pos (car data)))
5598             (gnus-data-number (car data))))))
5599     (gnus-summary-position-point)))
5600
5601 (defun gnus-summary-next-subject (n &optional unread dont-display)
5602   "Go to next N'th summary line.
5603 If N is negative, go to the previous N'th subject line.
5604 If UNREAD is non-nil, only unread articles are selected.
5605 The difference between N and the actual number of steps taken is
5606 returned."
5607   (interactive "p")
5608   (let ((backward (< n 0))
5609         (n (abs n)))
5610     (while (and (> n 0)
5611                 (if backward
5612                     (gnus-summary-find-prev unread)
5613                   (gnus-summary-find-next unread)))
5614       (gnus-summary-show-thread)
5615       (setq n (1- n)))
5616     (when (/= 0 n)
5617       (gnus-message 7 "No more%s articles"
5618                     (if unread " unread" "")))
5619     (unless dont-display
5620       (gnus-summary-recenter)
5621       (gnus-summary-position-point))
5622     n))
5623
5624 (defun gnus-summary-next-unread-subject (n)
5625   "Go to next N'th unread summary line."
5626   (interactive "p")
5627   (gnus-summary-next-subject n t))
5628
5629 (defun gnus-summary-prev-subject (n &optional unread)
5630   "Go to previous N'th summary line.
5631 If optional argument UNREAD is non-nil, only unread article is selected."
5632   (interactive "p")
5633   (gnus-summary-next-subject (- n) unread))
5634
5635 (defun gnus-summary-prev-unread-subject (n)
5636   "Go to previous N'th unread summary line."
5637   (interactive "p")
5638   (gnus-summary-next-subject (- n) t))
5639
5640 (defun gnus-summary-goto-subject (article &optional force silent)
5641   "Go the subject line of ARTICLE.
5642 If FORCE, also allow jumping to articles not currently shown."
5643   (interactive "nArticle number: ")
5644   (let ((b (point))
5645         (data (gnus-data-find article)))
5646     ;; We read in the article if we have to.
5647     (and (not data)
5648          force
5649          (gnus-summary-insert-subject
5650           article
5651           (if (or (numberp force) (vectorp force)) force)
5652           t)
5653          (setq data (gnus-data-find article)))
5654     (goto-char b)
5655     (if (not data)
5656         (progn
5657           (unless silent
5658             (gnus-message 3 "Can't find article %d" article))
5659           nil)
5660       (goto-char (gnus-data-pos data))
5661       (gnus-summary-position-point)
5662       article)))
5663
5664 ;; Walking around summary lines with displaying articles.
5665
5666 (defun gnus-summary-expand-window (&optional arg)
5667   "Make the summary buffer take up the entire Emacs frame.
5668 Given a prefix, will force an `article' buffer configuration."
5669   (interactive "P")
5670   (if arg
5671       (gnus-configure-windows 'article 'force)
5672     (gnus-configure-windows 'summary 'force)))
5673
5674 (defun gnus-summary-display-article (article &optional all-header)
5675   "Display ARTICLE in article buffer."
5676   (gnus-set-global-variables)
5677   (if (null article)
5678       nil
5679     (prog1
5680         (if gnus-summary-display-article-function
5681             (funcall gnus-summary-display-article-function article all-header)
5682           (gnus-article-prepare article all-header))
5683       (gnus-run-hooks 'gnus-select-article-hook)
5684       (when (and gnus-current-article
5685                  (not (zerop gnus-current-article)))
5686         (gnus-summary-goto-subject gnus-current-article))
5687       (gnus-summary-recenter)
5688       (when (and gnus-use-trees gnus-show-threads)
5689         (gnus-possibly-generate-tree article)
5690         (gnus-highlight-selected-tree article))
5691       ;; Successfully display article.
5692       (gnus-article-set-window-start
5693        (cdr (assq article gnus-newsgroup-bookmarks))))))
5694
5695 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5696   "Select the current article.
5697 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5698 non-nil, the article will be re-fetched even if it already present in
5699 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5700 be displayed."
5701   ;; Make sure we are in the summary buffer to work around bbdb bug.
5702   (unless (eq major-mode 'gnus-summary-mode)
5703     (set-buffer gnus-summary-buffer))
5704   (let ((article (or article (gnus-summary-article-number)))
5705         (all-headers (not (not all-headers))) ;Must be T or NIL.
5706         gnus-summary-display-article-function
5707         did)
5708     (and (not pseudo)
5709          (gnus-summary-article-pseudo-p article)
5710          (error "This is a pseudo-article"))
5711     (prog1
5712         (save-excursion
5713           (set-buffer gnus-summary-buffer)
5714           (if (or (and gnus-single-article-buffer
5715                        (or (null gnus-current-article)
5716                            (null gnus-article-current)
5717                            (null (get-buffer gnus-article-buffer))
5718                            (not (eq article (cdr gnus-article-current)))
5719                            (not (equal (car gnus-article-current)
5720                                        gnus-newsgroup-name))))
5721                   (and (not gnus-single-article-buffer)
5722                        (or (null gnus-current-article)
5723                            (not (eq gnus-current-article article))))
5724                   force)
5725               ;; The requested article is different from the current article.
5726               (prog1
5727                   (gnus-summary-display-article article all-headers)
5728                 (setq did article)
5729                 (when (or all-headers gnus-show-all-headers)
5730                   (gnus-article-show-all-headers)))
5731             (when (or all-headers gnus-show-all-headers)
5732               (gnus-article-show-all-headers))
5733             'old))
5734       (when did
5735         (gnus-article-set-window-start
5736          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5737
5738 (defun gnus-summary-set-current-mark (&optional current-mark)
5739   "Obsolete function."
5740   nil)
5741
5742 (defun gnus-summary-next-article (&optional unread subject backward push)
5743   "Select the next article.
5744 If UNREAD, only unread articles are selected.
5745 If SUBJECT, only articles with SUBJECT are selected.
5746 If BACKWARD, the previous article is selected instead of the next."
5747   (interactive "P")
5748   (cond
5749    ;; Is there such an article?
5750    ((and (gnus-summary-search-forward unread subject backward)
5751          (or (gnus-summary-display-article (gnus-summary-article-number))
5752              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5753     (gnus-summary-position-point))
5754    ;; If not, we try the first unread, if that is wanted.
5755    ((and subject
5756          gnus-auto-select-same
5757          (gnus-summary-first-unread-article))
5758     (gnus-summary-position-point)
5759     (gnus-message 6 "Wrapped"))
5760    ;; Try to get next/previous article not displayed in this group.
5761    ((and gnus-auto-extend-newsgroup
5762          (not unread) (not subject))
5763     (gnus-summary-goto-article
5764      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5765      nil (count-lines (point-min) (point))))
5766    ;; Go to next/previous group.
5767    (t
5768     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5769       (gnus-summary-jump-to-group gnus-newsgroup-name))
5770     (let ((cmd last-command-char)
5771           (point
5772            (save-excursion
5773              (set-buffer gnus-group-buffer)
5774              (point)))
5775           (group
5776            (if (eq gnus-keep-same-level 'best)
5777                (gnus-summary-best-group gnus-newsgroup-name)
5778              (gnus-summary-search-group backward gnus-keep-same-level))))
5779       ;; For some reason, the group window gets selected.  We change
5780       ;; it back.
5781       (select-window (get-buffer-window (current-buffer)))
5782       ;; Select next unread newsgroup automagically.
5783       (cond
5784        ((or (not gnus-auto-select-next)
5785             (not cmd))
5786         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5787        ((or (eq gnus-auto-select-next 'quietly)
5788             (and (eq gnus-auto-select-next 'slightly-quietly)
5789                  push)
5790             (and (eq gnus-auto-select-next 'almost-quietly)
5791                  (gnus-summary-last-article-p)))
5792         ;; Select quietly.
5793         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5794             (gnus-summary-exit)
5795           (gnus-message 7 "No more%s articles (%s)..."
5796                         (if unread " unread" "")
5797                         (if group (concat "selecting " group)
5798                           "exiting"))
5799           (gnus-summary-next-group nil group backward)))
5800        (t
5801         (when (gnus-key-press-event-p last-input-event)
5802           (gnus-summary-walk-group-buffer
5803            gnus-newsgroup-name cmd unread backward point))))))))
5804
5805 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5806   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5807                       (?\C-p (gnus-group-prev-unread-group 1))))
5808         (cursor-in-echo-area t)
5809         keve key group ended)
5810     (save-excursion
5811       (set-buffer gnus-group-buffer)
5812       (goto-char start)
5813       (setq group
5814             (if (eq gnus-keep-same-level 'best)
5815                 (gnus-summary-best-group gnus-newsgroup-name)
5816               (gnus-summary-search-group backward gnus-keep-same-level))))
5817     (while (not ended)
5818       (gnus-message
5819        5 "No more%s articles%s" (if unread " unread" "")
5820        (if (and group
5821                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5822            (format " (Type %s for %s [%s])"
5823                    (single-key-description cmd) group
5824                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5825          (format " (Type %s to exit %s)"
5826                  (single-key-description cmd)
5827                  gnus-newsgroup-name)))
5828       ;; Confirm auto selection.
5829       (setq key (car (setq keve (gnus-read-event-char))))
5830       (setq ended t)
5831       (cond
5832        ((assq key keystrokes)
5833         (let ((obuf (current-buffer)))
5834           (switch-to-buffer gnus-group-buffer)
5835           (when group
5836             (gnus-group-jump-to-group group))
5837           (eval (cadr (assq key keystrokes)))
5838           (setq group (gnus-group-group-name))
5839           (switch-to-buffer obuf))
5840         (setq ended nil))
5841        ((equal key cmd)
5842         (if (or (not group)
5843                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5844             (gnus-summary-exit)
5845           (gnus-summary-next-group nil group backward)))
5846        (t
5847         (push (cdr keve) unread-command-events))))))
5848
5849 (defun gnus-summary-next-unread-article ()
5850   "Select unread article after current one."
5851   (interactive)
5852   (gnus-summary-next-article
5853    (or (not (eq gnus-summary-goto-unread 'never))
5854        (gnus-summary-last-article-p (gnus-summary-article-number)))
5855    (and gnus-auto-select-same
5856         (gnus-summary-article-subject))))
5857
5858 (defun gnus-summary-prev-article (&optional unread subject)
5859   "Select the article after the current one.
5860 If UNREAD is non-nil, only unread articles are selected."
5861   (interactive "P")
5862   (gnus-summary-next-article unread subject t))
5863
5864 (defun gnus-summary-prev-unread-article ()
5865   "Select unread article before current one."
5866   (interactive)
5867   (gnus-summary-prev-article
5868    (or (not (eq gnus-summary-goto-unread 'never))
5869        (gnus-summary-first-article-p (gnus-summary-article-number)))
5870    (and gnus-auto-select-same
5871         (gnus-summary-article-subject))))
5872
5873 (defun gnus-summary-next-page (&optional lines circular)
5874   "Show next page of the selected article.
5875 If at the end of the current article, select the next article.
5876 LINES says how many lines should be scrolled up.
5877
5878 If CIRCULAR is non-nil, go to the start of the article instead of
5879 selecting the next article when reaching the end of the current
5880 article."
5881   (interactive "P")
5882   (setq gnus-summary-buffer (current-buffer))
5883   (gnus-set-global-variables)
5884   (let ((article (gnus-summary-article-number))
5885         (article-window (get-buffer-window gnus-article-buffer t))
5886         endp)
5887     ;; If the buffer is empty, we have no article.
5888     (unless article
5889       (error "No article to select"))
5890     (gnus-configure-windows 'article)
5891     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5892         (if (and (eq gnus-summary-goto-unread 'never)
5893                  (not (gnus-summary-last-article-p article)))
5894             (gnus-summary-next-article)
5895           (gnus-summary-next-unread-article))
5896       (if (or (null gnus-current-article)
5897               (null gnus-article-current)
5898               (/= article (cdr gnus-article-current))
5899               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5900           ;; Selected subject is different from current article's.
5901           (gnus-summary-display-article article)
5902         (when article-window
5903           (gnus-eval-in-buffer-window gnus-article-buffer
5904             (setq endp (gnus-article-next-page lines)))
5905           (when endp
5906             (cond (circular
5907                    (gnus-summary-beginning-of-article))
5908                   (lines
5909                    (gnus-message 3 "End of message"))
5910                   ((null lines)
5911                    (if (and (eq gnus-summary-goto-unread 'never)
5912                             (not (gnus-summary-last-article-p article)))
5913                        (gnus-summary-next-article)
5914                      (gnus-summary-next-unread-article))))))))
5915     (gnus-summary-recenter)
5916     (gnus-summary-position-point)))
5917
5918 (defun gnus-summary-prev-page (&optional lines move)
5919   "Show previous page of selected article.
5920 Argument LINES specifies lines to be scrolled down.
5921 If MOVE, move to the previous unread article if point is at
5922 the beginning of the buffer."
5923   (interactive "P")
5924   (let ((article (gnus-summary-article-number))
5925         (article-window (get-buffer-window gnus-article-buffer t))
5926         endp)
5927     (gnus-configure-windows 'article)
5928     (if (or (null gnus-current-article)
5929             (null gnus-article-current)
5930             (/= article (cdr gnus-article-current))
5931             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5932         ;; Selected subject is different from current article's.
5933         (gnus-summary-display-article article)
5934       (gnus-summary-recenter)
5935       (when article-window
5936         (gnus-eval-in-buffer-window gnus-article-buffer
5937           (setq endp (gnus-article-prev-page lines)))
5938         (when (and move endp)
5939           (cond (lines
5940                  (gnus-message 3 "Beginning of message"))
5941                 ((null lines)
5942                  (if (and (eq gnus-summary-goto-unread 'never)
5943                           (not (gnus-summary-first-article-p article)))
5944                      (gnus-summary-prev-article)
5945                    (gnus-summary-prev-unread-article))))))))
5946   (gnus-summary-position-point))
5947
5948 (defun gnus-summary-prev-page-or-article (&optional lines)
5949   "Show previous page of selected article.
5950 Argument LINES specifies lines to be scrolled down.
5951 If at the beginning of the article, go to the next article."
5952   (interactive "P")
5953   (gnus-summary-prev-page lines t))
5954
5955 (defun gnus-summary-scroll-up (lines)
5956   "Scroll up (or down) one line current article.
5957 Argument LINES specifies lines to be scrolled up (or down if negative)."
5958   (interactive "p")
5959   (gnus-configure-windows 'article)
5960   (gnus-summary-show-thread)
5961   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5962     (gnus-eval-in-buffer-window gnus-article-buffer
5963       (cond ((> lines 0)
5964              (when (gnus-article-next-page lines)
5965                (gnus-message 3 "End of message")))
5966             ((< lines 0)
5967              (gnus-article-prev-page (- lines))))))
5968   (gnus-summary-recenter)
5969   (gnus-summary-position-point))
5970
5971 (defun gnus-summary-scroll-down (lines)
5972   "Scroll down (or up) one line current article.
5973 Argument LINES specifies lines to be scrolled down (or up if negative)."
5974   (interactive "p")
5975   (gnus-summary-scroll-up (- lines)))
5976
5977 (defun gnus-summary-next-same-subject ()
5978   "Select next article which has the same subject as current one."
5979   (interactive)
5980   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5981
5982 (defun gnus-summary-prev-same-subject ()
5983   "Select previous article which has the same subject as current one."
5984   (interactive)
5985   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5986
5987 (defun gnus-summary-next-unread-same-subject ()
5988   "Select next unread article which has the same subject as current one."
5989   (interactive)
5990   (gnus-summary-next-article t (gnus-summary-article-subject)))
5991
5992 (defun gnus-summary-prev-unread-same-subject ()
5993   "Select previous unread article which has the same subject as current one."
5994   (interactive)
5995   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5996
5997 (defun gnus-summary-first-unread-article ()
5998   "Select the first unread article.
5999 Return nil if there are no unread articles."
6000   (interactive)
6001   (prog1
6002       (when (gnus-summary-first-subject t)
6003         (gnus-summary-show-thread)
6004         (gnus-summary-first-subject t)
6005         (gnus-summary-display-article (gnus-summary-article-number)))
6006     (gnus-summary-position-point)))
6007
6008 (defun gnus-summary-first-unread-subject ()
6009   "Place the point on the subject line of the first unread article.
6010 Return nil if there are no unread articles."
6011   (interactive)
6012   (prog1
6013       (when (gnus-summary-first-subject t)
6014         (gnus-summary-show-thread)
6015         (gnus-summary-first-subject t))
6016     (gnus-summary-position-point)))
6017
6018 (defun gnus-summary-first-article ()
6019   "Select the first article.
6020 Return nil if there are no articles."
6021   (interactive)
6022   (prog1
6023       (when (gnus-summary-first-subject)
6024         (gnus-summary-show-thread)
6025         (gnus-summary-first-subject)
6026         (gnus-summary-display-article (gnus-summary-article-number)))
6027     (gnus-summary-position-point)))
6028
6029 (defun gnus-summary-best-unread-article ()
6030   "Select the unread article with the highest score."
6031   (interactive)
6032   (let ((best -1000000)
6033         (data gnus-newsgroup-data)
6034         article score)
6035     (while data
6036       (and (gnus-data-unread-p (car data))
6037            (> (setq score
6038                     (gnus-summary-article-score (gnus-data-number (car data))))
6039               best)
6040            (setq best score
6041                  article (gnus-data-number (car data))))
6042       (setq data (cdr data)))
6043     (prog1
6044         (if article
6045             (gnus-summary-goto-article article)
6046           (error "No unread articles"))
6047       (gnus-summary-position-point))))
6048
6049 (defun gnus-summary-last-subject ()
6050   "Go to the last displayed subject line in the group."
6051   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6052     (when article
6053       (gnus-summary-goto-subject article))))
6054
6055 (defun gnus-summary-goto-article (article &optional all-headers force)
6056   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6057 If ALL-HEADERS is non-nil, no header lines are hidden.
6058 If FORCE, go to the article even if it isn't displayed.  If FORCE
6059 is a number, it is the line the article is to be displayed on."
6060   (interactive
6061    (list
6062     (completing-read
6063      "Article number or Message-ID: "
6064      (mapcar (lambda (number) (list (int-to-string number)))
6065              gnus-newsgroup-limit))
6066     current-prefix-arg
6067     t))
6068   (prog1
6069       (if (and (stringp article)
6070                (string-match "@" article))
6071           (gnus-summary-refer-article article)
6072         (when (stringp article)
6073           (setq article (string-to-number article)))
6074         (if (gnus-summary-goto-subject article force)
6075             (gnus-summary-display-article article all-headers)
6076           (gnus-message 4 "Couldn't go to article %s" article) nil))
6077     (gnus-summary-position-point)))
6078
6079 (defun gnus-summary-goto-last-article ()
6080   "Go to the previously read article."
6081   (interactive)
6082   (prog1
6083       (when gnus-last-article
6084         (gnus-summary-goto-article gnus-last-article nil t))
6085     (gnus-summary-position-point)))
6086
6087 (defun gnus-summary-pop-article (number)
6088   "Pop one article off the history and go to the previous.
6089 NUMBER articles will be popped off."
6090   (interactive "p")
6091   (let (to)
6092     (setq gnus-newsgroup-history
6093           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6094     (if to
6095         (gnus-summary-goto-article (car to) nil t)
6096       (error "Article history empty")))
6097   (gnus-summary-position-point))
6098
6099 ;; Summary commands and functions for limiting the summary buffer.
6100
6101 (defun gnus-summary-limit-to-articles (n)
6102   "Limit the summary buffer to the next N articles.
6103 If not given a prefix, use the process marked articles instead."
6104   (interactive "P")
6105   (prog1
6106       (let ((articles (gnus-summary-work-articles n)))
6107         (setq gnus-newsgroup-processable nil)
6108         (gnus-summary-limit articles))
6109     (gnus-summary-position-point)))
6110
6111 (defun gnus-summary-pop-limit (&optional total)
6112   "Restore the previous limit.
6113 If given a prefix, remove all limits."
6114   (interactive "P")
6115   (when total
6116     (setq gnus-newsgroup-limits
6117           (list (mapcar (lambda (h) (mail-header-number h))
6118                         gnus-newsgroup-headers))))
6119   (unless gnus-newsgroup-limits
6120     (error "No limit to pop"))
6121   (prog1
6122       (gnus-summary-limit nil 'pop)
6123     (gnus-summary-position-point)))
6124
6125 (defun gnus-summary-limit-to-subject (subject &optional header)
6126   "Limit the summary buffer to articles that have subjects that match a regexp."
6127   (interactive "sLimit to subject (regexp): ")
6128   (unless header
6129     (setq header "subject"))
6130   (when (not (equal "" subject))
6131     (prog1
6132         (let ((articles (gnus-summary-find-matching
6133                          (or header "subject") subject 'all)))
6134           (unless articles
6135             (error "Found no matches for \"%s\"" subject))
6136           (gnus-summary-limit articles))
6137       (gnus-summary-position-point))))
6138
6139 (defun gnus-summary-limit-to-author (from)
6140   "Limit the summary buffer to articles that have authors that match a regexp."
6141   (interactive "sLimit to author (regexp): ")
6142   (gnus-summary-limit-to-subject from "from"))
6143
6144 (defun gnus-summary-limit-to-age (age &optional younger-p)
6145   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6146 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6147 articles that are younger than AGE days."
6148   (interactive "nLimit to articles older than (in days): \nP")
6149   (prog1
6150       (let ((data gnus-newsgroup-data)
6151             (cutoff (days-to-time age))
6152             articles d date is-younger)
6153         (while (setq d (pop data))
6154           (when (and (vectorp (gnus-data-header d))
6155                      (setq date (mail-header-date (gnus-data-header d))))
6156             (setq is-younger (time-less-p
6157                               (time-since (date-to-time date))
6158                               cutoff))
6159             (when (if younger-p
6160                       is-younger
6161                     (not is-younger))
6162               (push (gnus-data-number d) articles))))
6163         (gnus-summary-limit (nreverse articles)))
6164     (gnus-summary-position-point)))
6165
6166 (defun gnus-summary-limit-to-extra (header regexp)
6167   "Limit the summary buffer to articles that match an 'extra' header."
6168   (interactive
6169    (let ((header
6170           (intern
6171            (gnus-completing-read
6172             (symbol-name (car gnus-extra-headers))      
6173             "Limit extra header:"       
6174             (mapcar (lambda (x) 
6175                       (cons (symbol-name x) x))
6176                     gnus-extra-headers)
6177             nil                 
6178             t))))
6179      (list header
6180            (read-string (format "Limit to header %s (regexp): " header)))))
6181   (when (not (equal "" regexp))
6182     (prog1
6183         (let ((articles (gnus-summary-find-matching
6184                          (cons 'extra header) regexp 'all)))
6185           (unless articles
6186             (error "Found no matches for \"%s\"" regexp))
6187           (gnus-summary-limit articles))
6188       (gnus-summary-position-point))))
6189
6190 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6191 (make-obsolete
6192  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6193
6194 (defun gnus-summary-limit-to-unread (&optional all)
6195   "Limit the summary buffer to articles that are not marked as read.
6196 If ALL is non-nil, limit strictly to unread articles."
6197   (interactive "P")
6198   (if all
6199       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6200     (gnus-summary-limit-to-marks
6201      ;; Concat all the marks that say that an article is read and have
6202      ;; those removed.
6203      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6204            gnus-killed-mark gnus-kill-file-mark
6205            gnus-low-score-mark gnus-expirable-mark
6206            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6207            gnus-duplicate-mark gnus-souped-mark)
6208      'reverse)))
6209
6210 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6211 (make-obsolete 'gnus-summary-delete-marked-with
6212                'gnus-summary-limit-exlude-marks)
6213
6214 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6215   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6216 If REVERSE, limit the summary buffer to articles that are marked
6217 with MARKS.  MARKS can either be a string of marks or a list of marks.
6218 Returns how many articles were removed."
6219   (interactive "sMarks: ")
6220   (gnus-summary-limit-to-marks marks t))
6221
6222 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6223   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6224 If REVERSE (the prefix), limit the summary buffer to articles that are
6225 not marked with MARKS.  MARKS can either be a string of marks or a
6226 list of marks.
6227 Returns how many articles were removed."
6228   (interactive "sMarks: \nP")
6229   (prog1
6230       (let ((data gnus-newsgroup-data)
6231             (marks (if (listp marks) marks
6232                      (append marks nil))) ; Transform to list.
6233             articles)
6234         (while data
6235           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6236                   (memq (gnus-data-mark (car data)) marks))
6237             (push (gnus-data-number (car data)) articles))
6238           (setq data (cdr data)))
6239         (gnus-summary-limit articles))
6240     (gnus-summary-position-point)))
6241
6242 (defun gnus-summary-limit-to-score (&optional score)
6243   "Limit to articles with score at or above SCORE."
6244   (interactive "P")
6245   (setq score (if score
6246                   (prefix-numeric-value score)
6247                 (or gnus-summary-default-score 0)))
6248   (let ((data gnus-newsgroup-data)
6249         articles)
6250     (while data
6251       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6252                 score)
6253         (push (gnus-data-number (car data)) articles))
6254       (setq data (cdr data)))
6255     (prog1
6256         (gnus-summary-limit articles)
6257       (gnus-summary-position-point))))
6258
6259 (defun gnus-summary-limit-include-thread (id)
6260   "Display all the hidden articles that in the current thread."
6261   (interactive (list (mail-header-id (gnus-summary-article-header))))
6262   (let ((articles (gnus-articles-in-thread
6263                    (gnus-id-to-thread (gnus-root-id id)))))
6264     (prog1
6265         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6266       (gnus-summary-position-point))))
6267
6268 (defun gnus-summary-limit-include-dormant ()
6269   "Display all the hidden articles that are marked as dormant.
6270 Note that this command only works on a subset of the articles currently
6271 fetched for this group."
6272   (interactive)
6273   (unless gnus-newsgroup-dormant
6274     (error "There are no dormant articles in this group"))
6275   (prog1
6276       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6277     (gnus-summary-position-point)))
6278
6279 (defun gnus-summary-limit-exclude-dormant ()
6280   "Hide all dormant articles."
6281   (interactive)
6282   (prog1
6283       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6284     (gnus-summary-position-point)))
6285
6286 (defun gnus-summary-limit-exclude-childless-dormant ()
6287   "Hide all dormant articles that have no children."
6288   (interactive)
6289   (let ((data (gnus-data-list t))
6290         articles d children)
6291     ;; Find all articles that are either not dormant or have
6292     ;; children.
6293     (while (setq d (pop data))
6294       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6295                 (and (setq children
6296                            (gnus-article-children (gnus-data-number d)))
6297                      (let (found)
6298                        (while children
6299                          (when (memq (car children) articles)
6300                            (setq children nil
6301                                  found t))
6302                          (pop children))
6303                        found)))
6304         (push (gnus-data-number d) articles)))
6305     ;; Do the limiting.
6306     (prog1
6307         (gnus-summary-limit articles)
6308       (gnus-summary-position-point))))
6309
6310 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6311   "Mark all unread excluded articles as read.
6312 If ALL, mark even excluded ticked and dormants as read."
6313   (interactive "P")
6314   (let ((articles (gnus-sorted-complement
6315                    (sort
6316                     (mapcar (lambda (h) (mail-header-number h))
6317                             gnus-newsgroup-headers)
6318                     '<)
6319                    (sort gnus-newsgroup-limit '<)))
6320         article)
6321     (setq gnus-newsgroup-unreads
6322           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6323     (if all
6324         (setq gnus-newsgroup-dormant nil
6325               gnus-newsgroup-marked nil
6326               gnus-newsgroup-reads
6327               (nconc
6328                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6329                gnus-newsgroup-reads))
6330       (while (setq article (pop articles))
6331         (unless (or (memq article gnus-newsgroup-dormant)
6332                     (memq article gnus-newsgroup-marked))
6333           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6334
6335 (defun gnus-summary-limit (articles &optional pop)
6336   (if pop
6337       ;; We pop the previous limit off the stack and use that.
6338       (setq articles (car gnus-newsgroup-limits)
6339             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6340     ;; We use the new limit, so we push the old limit on the stack.
6341     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6342   ;; Set the limit.
6343   (setq gnus-newsgroup-limit articles)
6344   (let ((total (length gnus-newsgroup-data))
6345         (data (gnus-data-find-list (gnus-summary-article-number)))
6346         (gnus-summary-mark-below nil)   ; Inhibit this.
6347         found)
6348     ;; This will do all the work of generating the new summary buffer
6349     ;; according to the new limit.
6350     (gnus-summary-prepare)
6351     ;; Hide any threads, possibly.
6352     (and gnus-show-threads
6353          gnus-thread-hide-subtree
6354          (gnus-summary-hide-all-threads))
6355     ;; Try to return to the article you were at, or one in the
6356     ;; neighborhood.
6357     (when data
6358       ;; We try to find some article after the current one.
6359       (while data
6360         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6361           (setq data nil
6362                 found t))
6363         (setq data (cdr data))))
6364     (unless found
6365       ;; If there is no data, that means that we were after the last
6366       ;; article.  The same goes when we can't find any articles
6367       ;; after the current one.
6368       (goto-char (point-max))
6369       (gnus-summary-find-prev))
6370     (gnus-set-mode-line 'summary)
6371     ;; We return how many articles were removed from the summary
6372     ;; buffer as a result of the new limit.
6373     (- total (length gnus-newsgroup-data))))
6374
6375 (defsubst gnus-invisible-cut-children (threads)
6376   (let ((num 0))
6377     (while threads
6378       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6379         (incf num))
6380       (pop threads))
6381     (< num 2)))
6382
6383 (defsubst gnus-cut-thread (thread)
6384   "Go forwards in the thread until we find an article that we want to display."
6385   (when (or (eq gnus-fetch-old-headers 'some)
6386             (eq gnus-fetch-old-headers 'invisible)
6387             (eq gnus-build-sparse-threads 'some)
6388             (eq gnus-build-sparse-threads 'more))
6389     ;; Deal with old-fetched headers and sparse threads.
6390     (while (and
6391             thread
6392             (or
6393              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6394              (gnus-summary-article-ancient-p
6395               (mail-header-number (car thread))))
6396             (if (or (<= (length (cdr thread)) 1)
6397                     (eq gnus-fetch-old-headers 'invisible))
6398                 (setq gnus-newsgroup-limit
6399                       (delq (mail-header-number (car thread))
6400                             gnus-newsgroup-limit)
6401                       thread (cadr thread))
6402               (when (gnus-invisible-cut-children (cdr thread))
6403                 (let ((th (cdr thread)))
6404                   (while th
6405                     (if (memq (mail-header-number (caar th))
6406                               gnus-newsgroup-limit)
6407                         (setq thread (car th)
6408                               th nil)
6409                       (setq th (cdr th))))))))))
6410   thread)
6411
6412 (defun gnus-cut-threads (threads)
6413   "Cut off all uninteresting articles from the beginning of threads."
6414   (when (or (eq gnus-fetch-old-headers 'some)
6415             (eq gnus-fetch-old-headers 'invisible)
6416             (eq gnus-build-sparse-threads 'some)
6417             (eq gnus-build-sparse-threads 'more))
6418     (let ((th threads))
6419       (while th
6420         (setcar th (gnus-cut-thread (car th)))
6421         (setq th (cdr th)))))
6422   ;; Remove nixed out threads.
6423   (delq nil threads))
6424
6425 (defun gnus-summary-initial-limit (&optional show-if-empty)
6426   "Figure out what the initial limit is supposed to be on group entry.
6427 This entails weeding out unwanted dormants, low-scored articles,
6428 fetch-old-headers verbiage, and so on."
6429   ;; Most groups have nothing to remove.
6430   (if (or gnus-inhibit-limiting
6431           (and (null gnus-newsgroup-dormant)
6432                (not (eq gnus-fetch-old-headers 'some))
6433                (not (eq gnus-fetch-old-headers 'invisible))
6434                (null gnus-summary-expunge-below)
6435                (not (eq gnus-build-sparse-threads 'some))
6436                (not (eq gnus-build-sparse-threads 'more))
6437                (null gnus-thread-expunge-below)
6438                (not gnus-use-nocem)))
6439       ()                                ; Do nothing.
6440     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6441     (setq gnus-newsgroup-limit nil)
6442     (mapatoms
6443      (lambda (node)
6444        (unless (car (symbol-value node))
6445          ;; These threads have no parents -- they are roots.
6446          (let ((nodes (cdr (symbol-value node)))
6447                thread)
6448            (while nodes
6449              (if (and gnus-thread-expunge-below
6450                       (< (gnus-thread-total-score (car nodes))
6451                          gnus-thread-expunge-below))
6452                  (gnus-expunge-thread (pop nodes))
6453                (setq thread (pop nodes))
6454                (gnus-summary-limit-children thread))))))
6455      gnus-newsgroup-dependencies)
6456     ;; If this limitation resulted in an empty group, we might
6457     ;; pop the previous limit and use it instead.
6458     (when (and (not gnus-newsgroup-limit)
6459                show-if-empty)
6460       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6461     gnus-newsgroup-limit))
6462
6463 (defun gnus-summary-limit-children (thread)
6464   "Return 1 if this subthread is visible and 0 if it is not."
6465   ;; First we get the number of visible children to this thread.  This
6466   ;; is done by recursing down the thread using this function, so this
6467   ;; will really go down to a leaf article first, before slowly
6468   ;; working its way up towards the root.
6469   (when thread
6470     (let ((children
6471            (if (cdr thread)
6472                (apply '+ (mapcar 'gnus-summary-limit-children
6473                                  (cdr thread)))
6474              0))
6475           (number (mail-header-number (car thread)))
6476           score)
6477       (if (and
6478            (not (memq number gnus-newsgroup-marked))
6479            (or
6480             ;; If this article is dormant and has absolutely no visible
6481             ;; children, then this article isn't visible.
6482             (and (memq number gnus-newsgroup-dormant)
6483                  (zerop children))
6484             ;; If this is "fetch-old-headered" and there is no
6485             ;; visible children, then we don't want this article.
6486             (and (eq gnus-fetch-old-headers 'some)
6487                  (gnus-summary-article-ancient-p number)
6488                  (zerop children))
6489             ;; If this is "fetch-old-headered" and `invisible', then
6490             ;; we don't want this article.
6491             (and (eq gnus-fetch-old-headers 'invisible)
6492                  (gnus-summary-article-ancient-p number))
6493             ;; If this is a sparsely inserted article with no children,
6494             ;; we don't want it.
6495             (and (eq gnus-build-sparse-threads 'some)
6496                  (gnus-summary-article-sparse-p number)
6497                  (zerop children))
6498             ;; If we use expunging, and this article is really
6499             ;; low-scored, then we don't want this article.
6500             (when (and gnus-summary-expunge-below
6501                        (< (setq score
6502                                 (or (cdr (assq number gnus-newsgroup-scored))
6503                                     gnus-summary-default-score))
6504                           gnus-summary-expunge-below))
6505               ;; We increase the expunge-tally here, but that has
6506               ;; nothing to do with the limits, really.
6507               (incf gnus-newsgroup-expunged-tally)
6508               ;; We also mark as read here, if that's wanted.
6509               (when (and gnus-summary-mark-below
6510                          (< score gnus-summary-mark-below))
6511                 (setq gnus-newsgroup-unreads
6512                       (delq number gnus-newsgroup-unreads))
6513                 (if gnus-newsgroup-auto-expire
6514                     (push number gnus-newsgroup-expirable)
6515                   (push (cons number gnus-low-score-mark)
6516                         gnus-newsgroup-reads)))
6517               t)
6518             ;; Check NoCeM things.
6519             (if (and gnus-use-nocem
6520                      (gnus-nocem-unwanted-article-p
6521                       (mail-header-id (car thread))))
6522                 (progn
6523                   (setq gnus-newsgroup-unreads
6524                         (delq number gnus-newsgroup-unreads))
6525                   t))))
6526           ;; Nope, invisible article.
6527           0
6528         ;; Ok, this article is to be visible, so we add it to the limit
6529         ;; and return 1.
6530         (push number gnus-newsgroup-limit)
6531         1))))
6532
6533 (defun gnus-expunge-thread (thread)
6534   "Mark all articles in THREAD as read."
6535   (let* ((number (mail-header-number (car thread))))
6536     (incf gnus-newsgroup-expunged-tally)
6537     ;; We also mark as read here, if that's wanted.
6538     (setq gnus-newsgroup-unreads
6539           (delq number gnus-newsgroup-unreads))
6540     (if gnus-newsgroup-auto-expire
6541         (push number gnus-newsgroup-expirable)
6542       (push (cons number gnus-low-score-mark)
6543             gnus-newsgroup-reads)))
6544   ;; Go recursively through all subthreads.
6545   (mapcar 'gnus-expunge-thread (cdr thread)))
6546
6547 ;; Summary article oriented commands
6548
6549 (defun gnus-summary-refer-parent-article (n)
6550   "Refer parent article N times.
6551 If N is negative, go to ancestor -N instead.
6552 The difference between N and the number of articles fetched is returned."
6553   (interactive "p")
6554   (let ((skip 1)
6555         error header ref)
6556     (when (not (natnump n))
6557       (setq skip (abs n)
6558             n 1))
6559     (while (and (> n 0)
6560                 (not error))
6561       (setq header (gnus-summary-article-header))
6562       (if (and (eq (mail-header-number header)
6563                    (cdr gnus-article-current))
6564                (equal gnus-newsgroup-name
6565                       (car gnus-article-current)))
6566           ;; If we try to find the parent of the currently
6567           ;; displayed article, then we take a look at the actual
6568           ;; References header, since this is slightly more
6569           ;; reliable than the References field we got from the
6570           ;; server.
6571           (save-excursion
6572             (set-buffer gnus-original-article-buffer)
6573             (nnheader-narrow-to-headers)
6574             (unless (setq ref (message-fetch-field "references"))
6575               (setq ref (message-fetch-field "in-reply-to")))
6576             (widen))
6577         (setq ref
6578               ;; It's not the current article, so we take a bet on
6579               ;; the value we got from the server.
6580               (mail-header-references header)))
6581       (if (and ref
6582                (not (equal ref "")))
6583           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6584             (gnus-message 1 "Couldn't find parent"))
6585         (gnus-message 1 "No references in article %d"
6586                       (gnus-summary-article-number))
6587         (setq error t))
6588       (decf n))
6589     (gnus-summary-position-point)
6590     n))
6591
6592 (defun gnus-summary-refer-references ()
6593   "Fetch all articles mentioned in the References header.
6594 Return the number of articles fetched."
6595   (interactive)
6596   (let ((ref (mail-header-references (gnus-summary-article-header)))
6597         (current (gnus-summary-article-number))
6598         (n 0))
6599     (if (or (not ref)
6600             (equal ref ""))
6601         (error "No References in the current article")
6602       ;; For each Message-ID in the References header...
6603       (while (string-match "<[^>]*>" ref)
6604         (incf n)
6605         ;; ... fetch that article.
6606         (gnus-summary-refer-article
6607          (prog1 (match-string 0 ref)
6608            (setq ref (substring ref (match-end 0))))))
6609       (gnus-summary-goto-subject current)
6610       (gnus-summary-position-point)
6611       n)))
6612
6613 (defun gnus-summary-refer-thread (&optional limit)
6614   "Fetch all articles in the current thread.
6615 If LIMIT (the numerical prefix), fetch that many old headers instead
6616 of what's specified by the `gnus-refer-thread-limit' variable."
6617   (interactive "P")
6618   (let ((id (mail-header-id (gnus-summary-article-header)))
6619         (limit (if limit (prefix-numeric-value limit)
6620                  gnus-refer-thread-limit)))
6621     ;; We want to fetch LIMIT *old* headers, but we also have to
6622     ;; re-fetch all the headers in the current buffer, because many of
6623     ;; them may be undisplayed.  So we adjust LIMIT.
6624     (when (numberp limit)
6625       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6626     (unless (eq gnus-fetch-old-headers 'invisible)
6627       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6628       ;; Retrieve the headers and read them in.
6629       (if (eq (gnus-retrieve-headers
6630                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6631               'nov)
6632           (gnus-build-all-threads)
6633         (error "Can't fetch thread from backends that don't support NOV"))
6634       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6635     (gnus-summary-limit-include-thread id)))
6636
6637 (defun gnus-summary-refer-article (message-id &optional arg)
6638   "Fetch an article specified by MESSAGE-ID.
6639 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6640 or `gnus-select-method', no matter what backend the article comes from."
6641   (interactive "sMessage-ID: \nP")
6642   (when (and (stringp message-id)
6643              (not (zerop (length message-id))))
6644     ;; Construct the correct Message-ID if necessary.
6645     ;; Suggested by tale@pawl.rpi.edu.
6646     (unless (string-match "^<" message-id)
6647       (setq message-id (concat "<" message-id)))
6648     (unless (string-match ">$" message-id)
6649       (setq message-id (concat message-id ">")))
6650     (let* ((header (gnus-id-to-header message-id))
6651            (sparse (and header
6652                         (gnus-summary-article-sparse-p
6653                          (mail-header-number header))
6654                         (memq (mail-header-number header)
6655                               gnus-newsgroup-limit))))
6656       (cond
6657        ;; If the article is present in the buffer we just go to it.
6658        ((and header
6659              (or (not (gnus-summary-article-sparse-p
6660                        (mail-header-number header)))
6661                  sparse))
6662         (prog1
6663             (gnus-summary-goto-article
6664              (mail-header-number header) nil t)
6665           (when sparse
6666             (gnus-summary-update-article (mail-header-number header)))))
6667        (t
6668         ;; We fetch the article
6669         (let ((gnus-override-method
6670                (cond ((gnus-news-group-p gnus-newsgroup-name)
6671                       gnus-refer-article-method)
6672                      (arg
6673                       (or gnus-refer-article-method gnus-select-method))
6674                      (t nil)))
6675               number)
6676           ;; Start the special refer-article method, if necessary.
6677           (when (and gnus-refer-article-method
6678                      (gnus-news-group-p gnus-newsgroup-name))
6679             (gnus-check-server gnus-refer-article-method))
6680           ;; Fetch the header, and display the article.
6681           (if (setq number (gnus-summary-insert-subject message-id))
6682               (gnus-summary-select-article nil nil nil number)
6683             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6684
6685 (defun gnus-summary-edit-parameters ()
6686   "Edit the group parameters of the current group."
6687   (interactive)
6688   (gnus-group-edit-group gnus-newsgroup-name 'params))
6689
6690 (defun gnus-summary-customize-parameters ()
6691   "Customize the group parameters of the current group."
6692   (interactive)
6693   (gnus-group-customize gnus-newsgroup-name))
6694
6695 (defun gnus-summary-enter-digest-group (&optional force)
6696   "Enter an nndoc group based on the current article.
6697 If FORCE, force a digest interpretation.  If not, try
6698 to guess what the document format is."
6699   (interactive "P")
6700   (let ((conf gnus-current-window-configuration))
6701     (save-excursion
6702       (gnus-summary-select-article))
6703     (setq gnus-current-window-configuration conf)
6704     (let* ((name (format "%s-%d"
6705                          (gnus-group-prefixed-name
6706                           gnus-newsgroup-name (list 'nndoc ""))
6707                          (save-excursion
6708                            (set-buffer gnus-summary-buffer)
6709                            gnus-current-article)))
6710            (ogroup gnus-newsgroup-name)
6711            (params (append (gnus-info-params (gnus-get-info ogroup))
6712                            (list (cons 'to-group ogroup))
6713                            (list (cons 'save-article-group ogroup))))
6714            (case-fold-search t)
6715            (buf (current-buffer))
6716            dig)
6717       (save-excursion
6718         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6719         (insert-buffer-substring gnus-original-article-buffer)
6720         ;; Remove lines that may lead nndoc to misinterpret the
6721         ;; document type.
6722         (narrow-to-region
6723          (goto-char (point-min))
6724          (or (search-forward "\n\n" nil t) (point)))
6725         (goto-char (point-min))
6726         (delete-matching-lines "^Path:\\|^From ")
6727         (widen))
6728       (unwind-protect
6729           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6730                     (gnus-newsgroup-ephemeral-ignored-charsets
6731                      gnus-newsgroup-ignored-charsets))
6732                 (gnus-group-read-ephemeral-group
6733                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6734                               (nndoc-article-type
6735                                ,(if force 'digest 'guess))) t))
6736               ;; Make all postings to this group go to the parent group.
6737               (nconc (gnus-info-params (gnus-get-info name))
6738                      params)
6739             ;; Couldn't select this doc group.
6740             (switch-to-buffer buf)
6741             (gnus-set-global-variables)
6742             (gnus-configure-windows 'summary)
6743             (gnus-message 3 "Article couldn't be entered?"))
6744         (kill-buffer dig)))))
6745
6746 (defun gnus-summary-read-document (n)
6747   "Open a new group based on the current article(s).
6748 This will allow you to read digests and other similar
6749 documents as newsgroups.
6750 Obeys the standard process/prefix convention."
6751   (interactive "P")
6752   (let* ((articles (gnus-summary-work-articles n))
6753          (ogroup gnus-newsgroup-name)
6754          (params (append (gnus-info-params (gnus-get-info ogroup))
6755                          (list (cons 'to-group ogroup))))
6756          article group egroup groups vgroup)
6757     (while (setq article (pop articles))
6758       (setq group (format "%s-%d" gnus-newsgroup-name article))
6759       (gnus-summary-remove-process-mark article)
6760       (when (gnus-summary-display-article article)
6761         (save-excursion
6762           (with-temp-buffer
6763             (insert-buffer-substring gnus-original-article-buffer)
6764             ;; Remove some headers that may lead nndoc to make
6765             ;; the wrong guess.
6766             (message-narrow-to-head)
6767             (goto-char (point-min))
6768             (delete-matching-lines "^\\(Path\\):\\|^From ")
6769             (widen)
6770             (if (setq egroup
6771                       (gnus-group-read-ephemeral-group
6772                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6773                                      (nndoc-article-type guess))
6774                        t nil t))
6775                 (progn
6776                   ;; Make all postings to this group go to the parent group.
6777                   (nconc (gnus-info-params (gnus-get-info egroup))
6778                          params)
6779                   (push egroup groups))
6780               ;; Couldn't select this doc group.
6781               (gnus-error 3 "Article couldn't be entered"))))))
6782     ;; Now we have selected all the documents.
6783     (cond
6784      ((not groups)
6785       (error "None of the articles could be interpreted as documents"))
6786      ((gnus-group-read-ephemeral-group
6787        (setq vgroup (format
6788                      "nnvirtual:%s-%s" gnus-newsgroup-name
6789                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6790        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6791        t
6792        (cons (current-buffer) 'summary)))
6793      (t
6794       (error "Couldn't select virtual nndoc group")))))
6795
6796 (defun gnus-summary-isearch-article (&optional regexp-p)
6797   "Do incremental search forward on the current article.
6798 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6799   (interactive "P")
6800   (gnus-summary-select-article)
6801   (gnus-configure-windows 'article)
6802   (gnus-eval-in-buffer-window gnus-article-buffer
6803     (save-restriction
6804       (widen)
6805       (isearch-forward regexp-p))))
6806
6807 (defun gnus-summary-search-article-forward (regexp &optional backward)
6808   "Search for an article containing REGEXP forward.
6809 If BACKWARD, search backward instead."
6810   (interactive
6811    (list (read-string
6812           (format "Search article %s (regexp%s): "
6813                   (if current-prefix-arg "backward" "forward")
6814                   (if gnus-last-search-regexp
6815                       (concat ", default " gnus-last-search-regexp)
6816                     "")))
6817          current-prefix-arg))
6818   (if (string-equal regexp "")
6819       (setq regexp (or gnus-last-search-regexp ""))
6820     (setq gnus-last-search-regexp regexp))
6821   (if (gnus-summary-search-article regexp backward)
6822       (gnus-summary-show-thread)
6823     (error "Search failed: \"%s\"" regexp)))
6824
6825 (defun gnus-summary-search-article-backward (regexp)
6826   "Search for an article containing REGEXP backward."
6827   (interactive
6828    (list (read-string
6829           (format "Search article backward (regexp%s): "
6830                   (if gnus-last-search-regexp
6831                       (concat ", default " gnus-last-search-regexp)
6832                     "")))))
6833   (gnus-summary-search-article-forward regexp 'backward))
6834
6835 (defun gnus-summary-search-article (regexp &optional backward)
6836   "Search for an article containing REGEXP.
6837 Optional argument BACKWARD means do search for backward.
6838 `gnus-select-article-hook' is not called during the search."
6839   ;; We have to require this here to make sure that the following
6840   ;; dynamic binding isn't shadowed by autoloading.
6841   (require 'gnus-async)
6842   (require 'gnus-art)
6843   (let ((gnus-select-article-hook nil)  ;Disable hook.
6844         (gnus-article-display-hook nil)
6845         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6846         (gnus-use-article-prefetch nil)
6847         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6848         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6849         (sum (current-buffer))
6850         (found nil)
6851         point)
6852     (gnus-save-hidden-threads
6853       (gnus-summary-select-article)
6854       (set-buffer gnus-article-buffer)
6855       (when backward
6856         (forward-line -1))
6857       (while (not found)
6858         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6859         (if (if backward
6860                 (re-search-backward regexp nil t)
6861               (re-search-forward regexp nil t))
6862             ;; We found the regexp.
6863             (progn
6864               (setq found 'found)
6865               (beginning-of-line)
6866               (set-window-start
6867                (get-buffer-window (current-buffer))
6868                (point))
6869               (forward-line 1)
6870               (set-buffer sum)
6871               (setq point (point)))
6872           ;; We didn't find it, so we go to the next article.
6873           (set-buffer sum)
6874           (setq found 'not)
6875           (while (eq found 'not)
6876             (if (not (if backward (gnus-summary-find-prev)
6877                        (gnus-summary-find-next)))
6878                 ;; No more articles.
6879                 (setq found t)
6880               ;; Select the next article and adjust point.
6881               (unless (gnus-summary-article-sparse-p
6882                        (gnus-summary-article-number))
6883                 (setq found nil)
6884                 (gnus-summary-select-article)
6885                 (set-buffer gnus-article-buffer)
6886                 (widen)
6887                 (goto-char (if backward (point-max) (point-min))))))))
6888       (gnus-message 7 ""))
6889     ;; Return whether we found the regexp.
6890     (when (eq found 'found)
6891       (goto-char point)
6892       (gnus-summary-show-thread)
6893       (gnus-summary-goto-subject gnus-current-article)
6894       (gnus-summary-position-point)
6895       t)))
6896
6897 (defun gnus-summary-find-matching (header regexp &optional backward unread
6898                                           not-case-fold)
6899   "Return a list of all articles that match REGEXP on HEADER.
6900 The search stars on the current article and goes forwards unless
6901 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6902 If UNREAD is non-nil, only unread articles will
6903 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6904 in the comparisons."
6905   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6906                 (gnus-data-find-list
6907                  (gnus-summary-article-number) (gnus-data-list backward))))
6908         (case-fold-search (not not-case-fold))
6909         articles d func)
6910     (if (consp header)
6911         (if (eq (car header) 'extra)
6912             (setq func
6913                   `(lambda (h)
6914                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6915                          "")))
6916           (error "%s is an invalid header" header))
6917       (unless (fboundp (intern (concat "mail-header-" header)))
6918         (error "%s is not a valid header" header))
6919       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6920     (while data
6921       (setq d (car data))
6922       (and (or (not unread)             ; We want all articles...
6923                (gnus-data-unread-p d))  ; Or just unreads.
6924            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6925            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6926            (push (gnus-data-number d) articles)) ; Success!
6927       (setq data (cdr data)))
6928     (nreverse articles)))
6929
6930 (defun gnus-summary-execute-command (header regexp command &optional backward)
6931   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6932 If HEADER is an empty string (or nil), the match is done on the entire
6933 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6934   (interactive
6935    (list (let ((completion-ignore-case t))
6936            (completing-read
6937             "Header name: "
6938             (mapcar (lambda (string) (list string))
6939                     '("Number" "Subject" "From" "Lines" "Date"
6940                       "Message-ID" "Xref" "References" "Body"))
6941             nil 'require-match))
6942          (read-string "Regexp: ")
6943          (read-key-sequence "Command: ")
6944          current-prefix-arg))
6945   (when (equal header "Body")
6946     (setq header ""))
6947   ;; Hidden thread subtrees must be searched as well.
6948   (gnus-summary-show-all-threads)
6949   ;; We don't want to change current point nor window configuration.
6950   (save-excursion
6951     (save-window-excursion
6952       (gnus-message 6 "Executing %s..." (key-description command))
6953       ;; We'd like to execute COMMAND interactively so as to give arguments.
6954       (gnus-execute header regexp
6955                     `(call-interactively ',(key-binding command))
6956                     backward)
6957       (gnus-message 6 "Executing %s...done" (key-description command)))))
6958
6959 (defun gnus-summary-beginning-of-article ()
6960   "Scroll the article back to the beginning."
6961   (interactive)
6962   (gnus-summary-select-article)
6963   (gnus-configure-windows 'article)
6964   (gnus-eval-in-buffer-window gnus-article-buffer
6965     (widen)
6966     (goto-char (point-min))
6967     (when gnus-page-broken
6968       (gnus-narrow-to-page))))
6969
6970 (defun gnus-summary-end-of-article ()
6971   "Scroll to the end of the article."
6972   (interactive)
6973   (gnus-summary-select-article)
6974   (gnus-configure-windows 'article)
6975   (gnus-eval-in-buffer-window gnus-article-buffer
6976     (widen)
6977     (goto-char (point-max))
6978     (recenter -3)
6979     (when gnus-page-broken
6980       (gnus-narrow-to-page))))
6981
6982 (defun gnus-summary-print-article (&optional filename n)
6983   "Generate and print a PostScript image of the N next (mail) articles.
6984
6985 If N is negative, print the N previous articles.  If N is nil and articles
6986 have been marked with the process mark, print these instead.
6987
6988 If the optional first argument FILENAME is nil, send the image to the
6989 printer.  If FILENAME is a string, save the PostScript image in a file with
6990 that name.  If FILENAME is a number, prompt the user for the name of the file
6991 to save in."
6992   (interactive (list (ps-print-preprint current-prefix-arg)
6993                      current-prefix-arg))
6994   (dolist (article (gnus-summary-work-articles n))
6995     (gnus-summary-select-article nil nil 'pseudo article)
6996     (gnus-eval-in-buffer-window gnus-article-buffer
6997       (let ((buffer (generate-new-buffer " *print*")))
6998         (unwind-protect
6999             (progn
7000               (copy-to-buffer buffer (point-min) (point-max))
7001               (set-buffer buffer)
7002               (gnus-article-delete-invisible-text)
7003               (let ((ps-left-header
7004                      (list
7005                       (concat "("
7006                               (mail-header-subject gnus-current-headers) ")")
7007                       (concat "("
7008                               (mail-header-from gnus-current-headers) ")")))
7009                     (ps-right-header
7010                      (list
7011                       "/pagenumberstring load"
7012                       (concat "("
7013                               (mail-header-date gnus-current-headers) ")"))))
7014                 (gnus-run-hooks 'gnus-ps-print-hook)
7015                 (save-excursion
7016                   (ps-print-buffer-with-faces filename))))
7017           (kill-buffer buffer))))))
7018
7019 (defun gnus-summary-show-article (&optional arg)
7020   "Force re-fetching of the current article.
7021 If ARG (the prefix) is non-nil, show the raw article without any
7022 article massaging functions being run."
7023   (interactive "P")
7024   (if (not arg)
7025       ;; Select the article the normal way.
7026       (gnus-summary-select-article nil 'force)
7027     ;; We have to require this here to make sure that the following
7028     ;; dynamic binding isn't shadowed by autoloading.
7029     (require 'gnus-async)
7030     (require 'gnus-art)
7031     ;; Bind the article treatment functions to nil.
7032     (let ((gnus-have-all-headers t)
7033           gnus-article-display-hook
7034           gnus-article-prepare-hook
7035           gnus-article-decode-hook
7036           gnus-break-pages
7037           gnus-show-mime
7038           gnus-visual)
7039       (gnus-summary-select-article nil 'force)))
7040   (gnus-summary-goto-subject gnus-current-article)
7041   (gnus-summary-position-point))
7042
7043 (defun gnus-summary-verbose-headers (&optional arg)
7044   "Toggle permanent full header display.
7045 If ARG is a positive number, turn header display on.
7046 If ARG is a negative number, turn header display off."
7047   (interactive "P")
7048   (setq gnus-show-all-headers
7049         (cond ((or (not (numberp arg))
7050                    (zerop arg))
7051                (not gnus-show-all-headers))
7052               ((natnump arg)
7053                t)))
7054   (gnus-summary-show-article))
7055
7056 (defun gnus-summary-toggle-header (&optional arg)
7057   "Show the headers if they are hidden, or hide them if they are shown.
7058 If ARG is a positive number, show the entire header.
7059 If ARG is a negative number, hide the unwanted header lines."
7060   (interactive "P")
7061   (save-excursion
7062     (set-buffer gnus-article-buffer)
7063     (save-restriction
7064       (let* ((buffer-read-only nil)
7065              (inhibit-point-motion-hooks t)
7066              hidden e)
7067         (save-restriction 
7068           (article-narrow-to-head)
7069           (setq hidden (gnus-article-hidden-text-p 'headers)))
7070         (goto-char (point-min))
7071         (when (search-forward "\n\n" nil t)
7072           (delete-region (point-min) (1- (point))))
7073         (goto-char (point-min))
7074         (save-excursion
7075           (set-buffer gnus-original-article-buffer)
7076           (goto-char (point-min))
7077           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7078         (insert-buffer-substring gnus-original-article-buffer 1 e)
7079         (save-restriction
7080           (narrow-to-region (point-min) (point))
7081           (article-decode-encoded-words)
7082           (if (or hidden
7083                   (and (numberp arg) (< arg 0)))
7084               (let ((gnus-treat-hide-headers nil)
7085                     (gnus-treat-hide-boring-headers nil))
7086                 (gnus-treat-article 'head))
7087             (gnus-treat-article 'head)))))))
7088
7089 (defun gnus-summary-show-all-headers ()
7090   "Make all header lines visible."
7091   (interactive)
7092   (gnus-article-show-all-headers))
7093
7094 (defun gnus-summary-toggle-mime (&optional arg)
7095   "Toggle MIME processing.
7096 If ARG is a positive number, turn MIME processing on."
7097   (interactive "P")
7098   (setq gnus-show-mime
7099         (if (null arg)
7100             (not gnus-show-mime)
7101           (> (prefix-numeric-value arg) 0)))
7102   (gnus-summary-select-article t 'force))
7103
7104 (defun gnus-summary-caesar-message (&optional arg)
7105   "Caesar rotate the current article by 13.
7106 The numerical prefix specifies how many places to rotate each letter
7107 forward."
7108   (interactive "P")
7109   (gnus-summary-select-article)
7110   (let ((mail-header-separator ""))
7111     (gnus-eval-in-buffer-window gnus-article-buffer
7112       (save-restriction
7113         (widen)
7114         (let ((start (window-start))
7115               buffer-read-only)
7116           (message-caesar-buffer-body arg)
7117           (set-window-start (get-buffer-window (current-buffer)) start))))))
7118
7119 (defun gnus-summary-stop-page-breaking ()
7120   "Stop page breaking in the current article."
7121   (interactive)
7122   (gnus-summary-select-article)
7123   (gnus-eval-in-buffer-window gnus-article-buffer
7124     (widen)
7125     (when (gnus-visual-p 'page-marker)
7126       (let ((buffer-read-only nil))
7127         (gnus-remove-text-with-property 'gnus-prev)
7128         (gnus-remove-text-with-property 'gnus-next))
7129       (setq gnus-page-broken nil))))
7130
7131 (defun gnus-summary-move-article (&optional n to-newsgroup
7132                                             select-method action)
7133   "Move the current article to a different newsgroup.
7134 If N is a positive number, move the N next articles.
7135 If N is a negative number, move the N previous articles.
7136 If N is nil and any articles have been marked with the process mark,
7137 move those articles instead.
7138 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7139 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7140 re-spool using this method.
7141
7142 For this function to work, both the current newsgroup and the
7143 newsgroup that you want to move to have to support the `request-move'
7144 and `request-accept' functions."
7145   (interactive "P")
7146   (unless action
7147     (setq action 'move))
7148   ;; Disable marking as read.
7149   (let (gnus-mark-article-hook)
7150     (save-window-excursion
7151       (gnus-summary-select-article)))
7152   ;; Check whether the source group supports the required functions.
7153   (cond ((and (eq action 'move)
7154               (not (gnus-check-backend-function
7155                     'request-move-article gnus-newsgroup-name)))
7156          (error "The current group does not support article moving"))
7157         ((and (eq action 'crosspost)
7158               (not (gnus-check-backend-function
7159                     'request-replace-article gnus-newsgroup-name)))
7160          (error "The current group does not support article editing")))
7161   (let ((articles (gnus-summary-work-articles n))
7162         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7163         (names '((move "Move" "Moving")
7164                  (copy "Copy" "Copying")
7165                  (crosspost "Crosspost" "Crossposting")))
7166         (copy-buf (save-excursion
7167                     (nnheader-set-temp-buffer " *copy article*")))
7168         (default-marks gnus-article-mark-lists)
7169         (no-expire-marks (delete '(expirable . expire)
7170                                  (copy-sequence gnus-article-mark-lists)))
7171         art-group to-method new-xref article to-groups)
7172     (unless (assq action names)
7173       (error "Unknown action %s" action))
7174     ;; Read the newsgroup name.
7175     (when (and (not to-newsgroup)
7176                (not select-method))
7177       (setq to-newsgroup
7178             (gnus-read-move-group-name
7179              (cadr (assq action names))
7180              (symbol-value (intern (format "gnus-current-%s-group" action)))
7181              articles prefix))
7182       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7183     (setq to-method (or select-method
7184                         (gnus-group-name-to-method to-newsgroup)))
7185     ;; Check the method we are to move this article to...
7186     (unless (gnus-check-backend-function
7187              'request-accept-article (car to-method))
7188       (error "%s does not support article copying" (car to-method)))
7189     (unless (gnus-check-server to-method)
7190       (error "Can't open server %s" (car to-method)))
7191     (gnus-message 6 "%s to %s: %s..."
7192                   (caddr (assq action names))
7193                   (or (car select-method) to-newsgroup) articles)
7194     (while articles
7195       (setq article (pop articles))
7196       (setq
7197        art-group
7198        (cond
7199         ;; Move the article.
7200         ((eq action 'move)
7201          ;; Remove this article from future suppression.
7202          (gnus-dup-unsuppress-article article)
7203          (gnus-request-move-article
7204           article                       ; Article to move
7205           gnus-newsgroup-name           ; From newsgroup
7206           (nth 1 (gnus-find-method-for-group
7207                   gnus-newsgroup-name)) ; Server
7208           (list 'gnus-request-accept-article
7209                 to-newsgroup (list 'quote select-method)
7210                 (not articles) t)               ; Accept form
7211           (not articles)))              ; Only save nov last time
7212         ;; Copy the article.
7213         ((eq action 'copy)
7214          (save-excursion
7215            (set-buffer copy-buf)
7216            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7217              (gnus-request-accept-article
7218               to-newsgroup select-method (not articles) t))))
7219         ;; Crosspost the article.
7220         ((eq action 'crosspost)
7221          (let ((xref (message-tokenize-header
7222                       (mail-header-xref (gnus-summary-article-header article))
7223                       " ")))
7224            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7225                                   ":" article))
7226            (unless xref
7227              (setq xref (list (system-name))))
7228            (setq new-xref
7229                  (concat
7230                   (mapconcat 'identity
7231                              (delete "Xref:" (delete new-xref xref))
7232                              " ")
7233                   " " new-xref))
7234            (save-excursion
7235              (set-buffer copy-buf)
7236              ;; First put the article in the destination group.
7237              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7238              (when (consp (setq art-group
7239                                 (gnus-request-accept-article
7240                                  to-newsgroup select-method (not articles))))
7241                (setq new-xref (concat new-xref " " (car art-group)
7242                                       ":" (cdr art-group)))
7243                ;; Now we have the new Xrefs header, so we insert
7244                ;; it and replace the new article.
7245                (nnheader-replace-header "Xref" new-xref)
7246                (gnus-request-replace-article
7247                 (cdr art-group) to-newsgroup (current-buffer))
7248                art-group))))))
7249       (cond
7250        ((not art-group)
7251         (gnus-message 1 "Couldn't %s article %s"
7252                       (cadr (assq action names)) article))
7253        ((and (eq art-group 'junk)
7254              (eq action 'move))
7255         (gnus-summary-mark-article article gnus-canceled-mark)
7256         (gnus-message 4 "Deleted article %s" article))
7257        (t
7258         (let* ((pto-group (gnus-group-prefixed-name
7259                            (car art-group) to-method))
7260                (entry
7261                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7262                (info (nth 2 entry))
7263                (to-group (gnus-info-group info))
7264                to-marks)
7265           ;; Update the group that has been moved to.
7266           (when (and info
7267                      (memq action '(move copy)))
7268             (unless (member to-group to-groups)
7269               (push to-group to-groups))
7270
7271             (unless (memq article gnus-newsgroup-unreads)
7272               (push 'read to-marks)
7273               (gnus-info-set-read
7274                info (gnus-add-to-range (gnus-info-read info)
7275                                        (list (cdr art-group)))))
7276
7277             ;; Copy any marks over to the new group.
7278             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7279                              default-marks
7280                            no-expire-marks))
7281                   (to-article (cdr art-group)))
7282
7283               ;; See whether the article is to be put in the cache.
7284               (when gnus-use-cache
7285                 (gnus-cache-possibly-enter-article
7286                  to-group to-article
7287                  (let ((header (copy-sequence
7288                                 (gnus-summary-article-header article))))
7289                    (mail-header-set-number header to-article)
7290                    header)
7291                  (memq article gnus-newsgroup-marked)
7292                  (memq article gnus-newsgroup-dormant)
7293                  (memq article gnus-newsgroup-unreads)))
7294
7295               (when (and (equal to-group gnus-newsgroup-name)
7296                          (not (memq article gnus-newsgroup-unreads)))
7297                 ;; Mark this article as read in this group.
7298                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7299                 (setcdr (gnus-active to-group) to-article)
7300                 (setcdr gnus-newsgroup-active to-article))
7301
7302               (while marks
7303                 (when (memq article (symbol-value
7304                                      (intern (format "gnus-newsgroup-%s"
7305                                                      (caar marks)))))
7306                   (push (cdar marks) to-marks)
7307                   ;; If the other group is the same as this group,
7308                   ;; then we have to add the mark to the list.
7309                   (when (equal to-group gnus-newsgroup-name)
7310                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7311                          (cons to-article
7312                                (symbol-value
7313                                 (intern (format "gnus-newsgroup-%s"
7314                                                 (caar marks)))))))
7315                   ;; Copy the marks to other group.
7316                   (gnus-add-marked-articles
7317                    to-group (cdar marks) (list to-article) info))
7318                 (setq marks (cdr marks)))
7319
7320               (gnus-request-set-mark to-group (list (list (list to-article)
7321                                                           'set
7322                                                           to-marks)))
7323
7324               (gnus-dribble-enter
7325                (concat "(gnus-group-set-info '"
7326                        (gnus-prin1-to-string (gnus-get-info to-group))
7327                        ")"))))
7328
7329           ;; Update the Xref header in this article to point to
7330           ;; the new crossposted article we have just created.
7331           (when (eq action 'crosspost)
7332             (save-excursion
7333               (set-buffer copy-buf)
7334               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7335               (nnheader-replace-header "Xref" new-xref)
7336               (gnus-request-replace-article
7337                article gnus-newsgroup-name (current-buffer)))))
7338
7339         ;;;!!!Why is this necessary?
7340         (set-buffer gnus-summary-buffer)
7341
7342         (gnus-summary-goto-subject article)
7343         (when (eq action 'move)
7344           (gnus-summary-mark-article article gnus-canceled-mark))))
7345       (gnus-summary-remove-process-mark article))
7346     ;; Re-activate all groups that have been moved to.
7347     (while to-groups
7348       (save-excursion
7349         (set-buffer gnus-group-buffer)
7350         (when (gnus-group-goto-group (car to-groups) t)
7351           (gnus-group-get-new-news-this-group 1 t))
7352         (pop to-groups)))
7353
7354     (gnus-kill-buffer copy-buf)
7355     (gnus-summary-position-point)
7356     (gnus-set-mode-line 'summary)))
7357
7358 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7359   "Move the current article to a different newsgroup.
7360 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7361 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7362 re-spool using this method."
7363   (interactive "P")
7364   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7365
7366 (defun gnus-summary-crosspost-article (&optional n)
7367   "Crosspost the current article to some other group."
7368   (interactive "P")
7369   (gnus-summary-move-article n nil nil 'crosspost))
7370
7371 (defcustom gnus-summary-respool-default-method nil
7372   "Default method for respooling an article.
7373 If nil, use to the current newsgroup method."
7374   :type '(choice (gnus-select-method :value (nnml ""))
7375                  (const nil))
7376   :group 'gnus-summary-mail)
7377
7378 (defun gnus-summary-respool-article (&optional n method)
7379   "Respool the current article.
7380 The article will be squeezed through the mail spooling process again,
7381 which means that it will be put in some mail newsgroup or other
7382 depending on `nnmail-split-methods'.
7383 If N is a positive number, respool the N next articles.
7384 If N is a negative number, respool the N previous articles.
7385 If N is nil and any articles have been marked with the process mark,
7386 respool those articles instead.
7387
7388 Respooling can be done both from mail groups and \"real\" newsgroups.
7389 In the former case, the articles in question will be moved from the
7390 current group into whatever groups they are destined to.  In the
7391 latter case, they will be copied into the relevant groups."
7392   (interactive
7393    (list current-prefix-arg
7394          (let* ((methods (gnus-methods-using 'respool))
7395                 (methname
7396                  (symbol-name (or gnus-summary-respool-default-method
7397                                   (car (gnus-find-method-for-group
7398                                         gnus-newsgroup-name)))))
7399                 (method
7400                  (gnus-completing-read
7401                   methname "What backend do you want to use when respooling?"
7402                   methods nil t nil 'gnus-mail-method-history))
7403                 ms)
7404            (cond
7405             ((zerop (length (setq ms (gnus-servers-using-backend
7406                                       (intern method)))))
7407              (list (intern method) ""))
7408             ((= 1 (length ms))
7409              (car ms))
7410             (t
7411              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7412                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7413                            ms-alist))))))))
7414   (unless method
7415     (error "No method given for respooling"))
7416   (if (assoc (symbol-name
7417               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7418              (gnus-methods-using 'respool))
7419       (gnus-summary-move-article n nil method)
7420     (gnus-summary-copy-article n nil method)))
7421
7422 (defun gnus-summary-import-article (file)
7423   "Import an arbitrary file into a mail newsgroup."
7424   (interactive "fImport file: ")
7425   (let ((group gnus-newsgroup-name)
7426         (now (current-time))
7427         atts lines)
7428     (unless (gnus-check-backend-function 'request-accept-article group)
7429       (error "%s does not support article importing" group))
7430     (or (file-readable-p file)
7431         (not (file-regular-p file))
7432         (error "Can't read %s" file))
7433     (save-excursion
7434       (set-buffer (gnus-get-buffer-create " *import file*"))
7435       (erase-buffer)
7436       (nnheader-insert-file-contents file)
7437       (goto-char (point-min))
7438       (unless (nnheader-article-p)
7439         ;; This doesn't look like an article, so we fudge some headers.
7440         (setq atts (file-attributes file)
7441               lines (count-lines (point-min) (point-max)))
7442         (insert "From: " (read-string "From: ") "\n"
7443                 "Subject: " (read-string "Subject: ") "\n"
7444                 "Date: " (message-make-date (nth 5 atts))
7445                 "\n"
7446                 "Message-ID: " (message-make-message-id) "\n"
7447                 "Lines: " (int-to-string lines) "\n"
7448                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7449       (gnus-request-accept-article group nil t)
7450       (kill-buffer (current-buffer)))))
7451
7452 (defun gnus-summary-article-posted-p ()
7453   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7454 This will be the case if the article has both been mailed and posted."
7455   (interactive)
7456   (let ((id (mail-header-references (gnus-summary-article-header)))
7457         (gnus-override-method
7458          (or gnus-refer-article-method gnus-select-method)))
7459     (if (gnus-request-head id "")
7460         (gnus-message 2 "The current message was found on %s"
7461                       gnus-override-method)
7462       (gnus-message 2 "The current message couldn't be found on %s"
7463                     gnus-override-method)
7464       nil)))
7465
7466 (defun gnus-summary-expire-articles (&optional now)
7467   "Expire all articles that are marked as expirable in the current group."
7468   (interactive)
7469   (when (gnus-check-backend-function
7470          'request-expire-articles gnus-newsgroup-name)
7471     ;; This backend supports expiry.
7472     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7473            (expirable (if total
7474                           (progn
7475                             ;; We need to update the info for
7476                             ;; this group for `gnus-list-of-read-articles'
7477                             ;; to give us the right answer.
7478                             (gnus-run-hooks 'gnus-exit-group-hook)
7479                             (gnus-summary-update-info)
7480                             (gnus-list-of-read-articles gnus-newsgroup-name))
7481                         (setq gnus-newsgroup-expirable
7482                               (sort gnus-newsgroup-expirable '<))))
7483            (expiry-wait (if now 'immediate
7484                           (gnus-group-find-parameter
7485                            gnus-newsgroup-name 'expiry-wait)))
7486            es)
7487       (when expirable
7488         ;; There are expirable articles in this group, so we run them
7489         ;; through the expiry process.
7490         (gnus-message 6 "Expiring articles...")
7491         (unless (gnus-check-group gnus-newsgroup-name)
7492           (error "Can't open server for %s" gnus-newsgroup-name))
7493         ;; The list of articles that weren't expired is returned.
7494         (save-excursion
7495           (if expiry-wait
7496               (let ((nnmail-expiry-wait-function nil)
7497                     (nnmail-expiry-wait expiry-wait))
7498                 (setq es (gnus-request-expire-articles
7499                           expirable gnus-newsgroup-name)))
7500             (setq es (gnus-request-expire-articles
7501                       expirable gnus-newsgroup-name))))
7502         (unless total
7503           (setq gnus-newsgroup-expirable es))
7504         ;; We go through the old list of expirable, and mark all
7505         ;; really expired articles as nonexistent.
7506         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7507           (let ((gnus-use-cache nil))
7508             (while expirable
7509               (unless (memq (car expirable) es)
7510                 (when (gnus-data-find (car expirable))
7511                   (gnus-summary-mark-article
7512                    (car expirable) gnus-canceled-mark)))
7513               (setq expirable (cdr expirable)))))
7514         (gnus-message 6 "Expiring articles...done")))))
7515
7516 (defun gnus-summary-expire-articles-now ()
7517   "Expunge all expirable articles in the current group.
7518 This means that *all* articles that are marked as expirable will be
7519 deleted forever, right now."
7520   (interactive)
7521   (or gnus-expert-user
7522       (gnus-yes-or-no-p
7523        "Are you really, really, really sure you want to delete all these messages? ")
7524       (error "Phew!"))
7525   (gnus-summary-expire-articles t))
7526
7527 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7528 (defun gnus-summary-delete-article (&optional n)
7529   "Delete the N next (mail) articles.
7530 This command actually deletes articles.  This is not a marking
7531 command.  The article will disappear forever from your life, never to
7532 return.
7533 If N is negative, delete backwards.
7534 If N is nil and articles have been marked with the process mark,
7535 delete these instead."
7536   (interactive "P")
7537   (unless (gnus-check-backend-function 'request-expire-articles
7538                                        gnus-newsgroup-name)
7539     (error "The current newsgroup does not support article deletion"))
7540   ;; Compute the list of articles to delete.
7541   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7542         not-deleted)
7543     (if (and gnus-novice-user
7544              (not (gnus-yes-or-no-p
7545                    (format "Do you really want to delete %s forever? "
7546                            (if (> (length articles) 1)
7547                                (format "these %s articles" (length articles))
7548                              "this article")))))
7549         ()
7550       ;; Delete the articles.
7551       (setq not-deleted (gnus-request-expire-articles
7552                          articles gnus-newsgroup-name 'force))
7553       (while articles
7554         (gnus-summary-remove-process-mark (car articles))
7555         ;; The backend might not have been able to delete the article
7556         ;; after all.
7557         (unless (memq (car articles) not-deleted)
7558           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7559         (setq articles (cdr articles)))
7560       (when not-deleted
7561         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7562     (gnus-summary-position-point)
7563     (gnus-set-mode-line 'summary)
7564     not-deleted))
7565
7566 (defun gnus-summary-edit-article (&optional force)
7567   "Edit the current article.
7568 This will have permanent effect only in mail groups.
7569 If FORCE is non-nil, allow editing of articles even in read-only
7570 groups."
7571   (interactive "P")
7572   (save-excursion
7573     (set-buffer gnus-summary-buffer)
7574     (let ((mail-parse-charset gnus-newsgroup-charset)
7575           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7576       (gnus-set-global-variables)
7577       (when (and (not force)
7578                  (gnus-group-read-only-p))
7579         (error "The current newsgroup does not support article editing"))
7580       (gnus-summary-show-article t)
7581       (gnus-article-edit-article
7582        'ignore
7583        `(lambda (no-highlight)
7584           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7585                 (mail-parse-ignored-charsets
7586                  ',gnus-newsgroup-ignored-charsets))
7587             (gnus-summary-edit-article-done
7588              ,(or (mail-header-references gnus-current-headers) "")
7589              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7590
7591 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7592
7593 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7594                                                  no-highlight)
7595   "Make edits to the current article permanent."
7596   (interactive)
7597   ;; Replace the article.
7598   (let ((buf (current-buffer)))
7599     (with-temp-buffer
7600       (insert-buffer-substring buf)
7601       (if (and (not read-only)
7602                (not (gnus-request-replace-article
7603                      (cdr gnus-article-current) (car gnus-article-current)
7604                      (current-buffer) t)))
7605           (error "Couldn't replace article")
7606         ;; Update the summary buffer.
7607         (if (and references
7608                  (equal (message-tokenize-header references " ")
7609                         (message-tokenize-header
7610                          (or (message-fetch-field "references") "") " ")))
7611             ;; We only have to update this line.
7612             (save-excursion
7613               (save-restriction
7614                 (message-narrow-to-head)
7615                 (let ((head (buffer-string))
7616                       header)
7617                   (with-temp-buffer
7618                     (insert (format "211 %d Article retrieved.\n"
7619                                     (cdr gnus-article-current)))
7620                     (insert head)
7621                     (insert ".\n")
7622                     (let ((nntp-server-buffer (current-buffer)))
7623                       (setq header (car (gnus-get-newsgroup-headers
7624                                          (save-excursion
7625                                            (set-buffer gnus-summary-buffer)
7626                                            gnus-newsgroup-dependencies)
7627                                          t))))
7628                     (save-excursion
7629                       (set-buffer gnus-summary-buffer)
7630                       (gnus-data-set-header
7631                        (gnus-data-find (cdr gnus-article-current))
7632                        header)
7633                       (gnus-summary-update-article-line
7634                        (cdr gnus-article-current) header))))))
7635           ;; Update threads.
7636           (set-buffer (or buffer gnus-summary-buffer))
7637           (gnus-summary-update-article (cdr gnus-article-current)))
7638         ;; Prettify the article buffer again.
7639         (unless no-highlight
7640           (save-excursion
7641             (set-buffer gnus-article-buffer)
7642             ;;;!!! Fix this -- article should be rehighlighted.
7643             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7644             (set-buffer gnus-original-article-buffer)
7645             (gnus-request-article
7646              (cdr gnus-article-current)
7647              (car gnus-article-current) (current-buffer))))
7648         ;; Prettify the summary buffer line.
7649         (when (gnus-visual-p 'summary-highlight 'highlight)
7650           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7651
7652 (defun gnus-summary-edit-wash (key)
7653   "Perform editing command KEY in the article buffer."
7654   (interactive
7655    (list
7656     (progn
7657       (message "%s" (concat (this-command-keys) "- "))
7658       (read-char))))
7659   (message "")
7660   (gnus-summary-edit-article)
7661   (execute-kbd-macro (concat (this-command-keys) key))
7662   (gnus-article-edit-done))
7663
7664 ;;; Respooling
7665
7666 (defun gnus-summary-respool-query (&optional silent trace)
7667   "Query where the respool algorithm would put this article."
7668   (interactive)
7669   (let (gnus-mark-article-hook)
7670     (gnus-summary-select-article)
7671     (save-excursion
7672       (set-buffer gnus-original-article-buffer)
7673       (save-restriction
7674         (message-narrow-to-head)
7675         (let ((groups (nnmail-article-group 'identity trace)))
7676           (unless silent
7677             (if groups
7678                 (message "This message would go to %s"
7679                          (mapconcat 'car groups ", "))
7680               (message "This message would go to no groups"))
7681             groups))))))
7682
7683 (defun gnus-summary-respool-trace ()
7684   "Trace where the respool algorithm would put this article.
7685 Display a buffer showing all fancy splitting patterns which matched."
7686   (interactive)
7687   (gnus-summary-respool-query nil t))
7688
7689 ;; Summary marking commands.
7690
7691 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7692   "Mark articles which has the same subject as read, and then select the next.
7693 If UNMARK is positive, remove any kind of mark.
7694 If UNMARK is negative, tick articles."
7695   (interactive "P")
7696   (when unmark
7697     (setq unmark (prefix-numeric-value unmark)))
7698   (let ((count
7699          (gnus-summary-mark-same-subject
7700           (gnus-summary-article-subject) unmark)))
7701     ;; Select next unread article.  If auto-select-same mode, should
7702     ;; select the first unread article.
7703     (gnus-summary-next-article t (and gnus-auto-select-same
7704                                       (gnus-summary-article-subject)))
7705     (gnus-message 7 "%d article%s marked as %s"
7706                   count (if (= count 1) " is" "s are")
7707                   (if unmark "unread" "read"))))
7708
7709 (defun gnus-summary-kill-same-subject (&optional unmark)
7710   "Mark articles which has the same subject as read.
7711 If UNMARK is positive, remove any kind of mark.
7712 If UNMARK is negative, tick articles."
7713   (interactive "P")
7714   (when unmark
7715     (setq unmark (prefix-numeric-value unmark)))
7716   (let ((count
7717          (gnus-summary-mark-same-subject
7718           (gnus-summary-article-subject) unmark)))
7719     ;; If marked as read, go to next unread subject.
7720     (when (null unmark)
7721       ;; Go to next unread subject.
7722       (gnus-summary-next-subject 1 t))
7723     (gnus-message 7 "%d articles are marked as %s"
7724                   count (if unmark "unread" "read"))))
7725
7726 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7727   "Mark articles with same SUBJECT as read, and return marked number.
7728 If optional argument UNMARK is positive, remove any kinds of marks.
7729 If optional argument UNMARK is negative, mark articles as unread instead."
7730   (let ((count 1))
7731     (save-excursion
7732       (cond
7733        ((null unmark)                   ; Mark as read.
7734         (while (and
7735                 (progn
7736                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7737                   (gnus-summary-show-thread) t)
7738                 (gnus-summary-find-subject subject))
7739           (setq count (1+ count))))
7740        ((> unmark 0)                    ; Tick.
7741         (while (and
7742                 (progn
7743                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7744                   (gnus-summary-show-thread) t)
7745                 (gnus-summary-find-subject subject))
7746           (setq count (1+ count))))
7747        (t                               ; Mark as unread.
7748         (while (and
7749                 (progn
7750                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7751                   (gnus-summary-show-thread) t)
7752                 (gnus-summary-find-subject subject))
7753           (setq count (1+ count)))))
7754       (gnus-set-mode-line 'summary)
7755       ;; Return the number of marked articles.
7756       count)))
7757
7758 (defun gnus-summary-mark-as-processable (n &optional unmark)
7759   "Set the process mark on the next N articles.
7760 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7761 the process mark instead.  The difference between N and the actual
7762 number of articles marked is returned."
7763   (interactive "p")
7764   (let ((backward (< n 0))
7765         (n (abs n)))
7766     (while (and
7767             (> n 0)
7768             (if unmark
7769                 (gnus-summary-remove-process-mark
7770                  (gnus-summary-article-number))
7771               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7772             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7773       (setq n (1- n)))
7774     (when (/= 0 n)
7775       (gnus-message 7 "No more articles"))
7776     (gnus-summary-recenter)
7777     (gnus-summary-position-point)
7778     n))
7779
7780 (defun gnus-summary-unmark-as-processable (n)
7781   "Remove the process mark from the next N articles.
7782 If N is negative, unmark backward instead.  The difference between N and
7783 the actual number of articles unmarked is returned."
7784   (interactive "p")
7785   (gnus-summary-mark-as-processable n t))
7786
7787 (defun gnus-summary-unmark-all-processable ()
7788   "Remove the process mark from all articles."
7789   (interactive)
7790   (save-excursion
7791     (while gnus-newsgroup-processable
7792       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7793   (gnus-summary-position-point))
7794
7795 (defun gnus-summary-mark-as-expirable (n)
7796   "Mark N articles forward as expirable.
7797 If N is negative, mark backward instead.  The difference between N and
7798 the actual number of articles marked is returned."
7799   (interactive "p")
7800   (gnus-summary-mark-forward n gnus-expirable-mark))
7801
7802 (defun gnus-summary-mark-article-as-replied (article)
7803   "Mark ARTICLE replied and update the summary line."
7804   (push article gnus-newsgroup-replied)
7805   (let ((buffer-read-only nil))
7806     (when (gnus-summary-goto-subject article nil t)
7807       (gnus-summary-update-secondary-mark article))))
7808
7809 (defun gnus-summary-set-bookmark (article)
7810   "Set a bookmark in current article."
7811   (interactive (list (gnus-summary-article-number)))
7812   (when (or (not (get-buffer gnus-article-buffer))
7813             (not gnus-current-article)
7814             (not gnus-article-current)
7815             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7816     (error "No current article selected"))
7817   ;; Remove old bookmark, if one exists.
7818   (let ((old (assq article gnus-newsgroup-bookmarks)))
7819     (when old
7820       (setq gnus-newsgroup-bookmarks
7821             (delq old gnus-newsgroup-bookmarks))))
7822   ;; Set the new bookmark, which is on the form
7823   ;; (article-number . line-number-in-body).
7824   (push
7825    (cons article
7826          (save-excursion
7827            (set-buffer gnus-article-buffer)
7828            (count-lines
7829             (min (point)
7830                  (save-excursion
7831                    (goto-char (point-min))
7832                    (search-forward "\n\n" nil t)
7833                    (point)))
7834             (point))))
7835    gnus-newsgroup-bookmarks)
7836   (gnus-message 6 "A bookmark has been added to the current article."))
7837
7838 (defun gnus-summary-remove-bookmark (article)
7839   "Remove the bookmark from the current article."
7840   (interactive (list (gnus-summary-article-number)))
7841   ;; Remove old bookmark, if one exists.
7842   (let ((old (assq article gnus-newsgroup-bookmarks)))
7843     (if old
7844         (progn
7845           (setq gnus-newsgroup-bookmarks
7846                 (delq old gnus-newsgroup-bookmarks))
7847           (gnus-message 6 "Removed bookmark."))
7848       (gnus-message 6 "No bookmark in current article."))))
7849
7850 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7851 (defun gnus-summary-mark-as-dormant (n)
7852   "Mark N articles forward as dormant.
7853 If N is negative, mark backward instead.  The difference between N and
7854 the actual number of articles marked is returned."
7855   (interactive "p")
7856   (gnus-summary-mark-forward n gnus-dormant-mark))
7857
7858 (defun gnus-summary-set-process-mark (article)
7859   "Set the process mark on ARTICLE and update the summary line."
7860   (setq gnus-newsgroup-processable
7861         (cons article
7862               (delq article gnus-newsgroup-processable)))
7863   (when (gnus-summary-goto-subject article)
7864     (gnus-summary-show-thread)
7865     (gnus-summary-goto-subject article)
7866     (gnus-summary-update-secondary-mark article)))
7867
7868 (defun gnus-summary-remove-process-mark (article)
7869   "Remove the process mark from ARTICLE and update the summary line."
7870   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7871   (when (gnus-summary-goto-subject article)
7872     (gnus-summary-show-thread)
7873     (gnus-summary-goto-subject article)
7874     (gnus-summary-update-secondary-mark article)))
7875
7876 (defun gnus-summary-set-saved-mark (article)
7877   "Set the process mark on ARTICLE and update the summary line."
7878   (push article gnus-newsgroup-saved)
7879   (when (gnus-summary-goto-subject article)
7880     (gnus-summary-update-secondary-mark article)))
7881
7882 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7883   "Mark N articles as read forwards.
7884 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7885 The difference between N and the actual number of articles marked is
7886 returned."
7887   (interactive "p")
7888   (gnus-summary-show-thread)
7889   (let ((backward (< n 0))
7890         (gnus-summary-goto-unread
7891          (and gnus-summary-goto-unread
7892               (not (eq gnus-summary-goto-unread 'never))
7893               (not (memq mark (list gnus-unread-mark
7894                                     gnus-ticked-mark gnus-dormant-mark)))))
7895         (n (abs n))
7896         (mark (or mark gnus-del-mark)))
7897     (while (and (> n 0)
7898                 (gnus-summary-mark-article nil mark no-expire)
7899                 (zerop (gnus-summary-next-subject
7900                         (if backward -1 1)
7901                         (and gnus-summary-goto-unread
7902                              (not (eq gnus-summary-goto-unread 'never)))
7903                         t)))
7904       (setq n (1- n)))
7905     (when (/= 0 n)
7906       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7907     (gnus-summary-recenter)
7908     (gnus-summary-position-point)
7909     (gnus-set-mode-line 'summary)
7910     n))
7911
7912 (defun gnus-summary-mark-article-as-read (mark)
7913   "Mark the current article quickly as read with MARK."
7914   (let ((article (gnus-summary-article-number)))
7915     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7916     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7917     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7918     (push (cons article mark) gnus-newsgroup-reads)
7919     ;; Possibly remove from cache, if that is used.
7920     (when gnus-use-cache
7921       (gnus-cache-enter-remove-article article))
7922     ;; Allow the backend to change the mark.
7923     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7924     ;; Check for auto-expiry.
7925     (when (and gnus-newsgroup-auto-expire
7926                (memq mark gnus-auto-expirable-marks))
7927       (setq mark gnus-expirable-mark)
7928       ;; Let the backend know about the mark change.
7929       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7930       (push article gnus-newsgroup-expirable))
7931     ;; Set the mark in the buffer.
7932     (gnus-summary-update-mark mark 'unread)
7933     t))
7934
7935 (defun gnus-summary-mark-article-as-unread (mark)
7936   "Mark the current article quickly as unread with MARK."
7937   (let* ((article (gnus-summary-article-number))
7938          (old-mark (gnus-summary-article-mark article)))
7939     ;; Allow the backend to change the mark.
7940     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7941     (if (eq mark old-mark)
7942         t
7943       (if (<= article 0)
7944           (progn
7945             (gnus-error 1 "Can't mark negative article numbers")
7946             nil)
7947         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7948         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7949         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7950         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7951         (cond ((= mark gnus-ticked-mark)
7952                (push article gnus-newsgroup-marked))
7953               ((= mark gnus-dormant-mark)
7954                (push article gnus-newsgroup-dormant))
7955               (t
7956                (push article gnus-newsgroup-unreads)))
7957         (gnus-pull article gnus-newsgroup-reads)
7958
7959         ;; See whether the article is to be put in the cache.
7960         (and gnus-use-cache
7961              (vectorp (gnus-summary-article-header article))
7962              (save-excursion
7963                (gnus-cache-possibly-enter-article
7964                 gnus-newsgroup-name article
7965                 (gnus-summary-article-header article)
7966                 (= mark gnus-ticked-mark)
7967                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7968
7969         ;; Fix the mark.
7970         (gnus-summary-update-mark mark 'unread)
7971         t))))
7972
7973 (defun gnus-summary-mark-article (&optional article mark no-expire)
7974   "Mark ARTICLE with MARK.  MARK can be any character.
7975 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7976 `??' (dormant) and `?E' (expirable).
7977 If MARK is nil, then the default character `?r' is used.
7978 If ARTICLE is nil, then the article on the current line will be
7979 marked."
7980   ;; The mark might be a string.
7981   (when (stringp mark)
7982     (setq mark (aref mark 0)))
7983   ;; If no mark is given, then we check auto-expiring.
7984   (when (null mark)
7985     (setq mark gnus-del-mark))
7986   (when (and (not no-expire)
7987              gnus-newsgroup-auto-expire
7988              (memq mark gnus-auto-expirable-marks))
7989     (setq mark gnus-expirable-mark))
7990   (let ((article (or article (gnus-summary-article-number)))
7991         (old-mark (gnus-summary-article-mark article)))
7992     ;; Allow the backend to change the mark.
7993     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7994     (if (eq mark old-mark)
7995         t
7996       (unless article
7997         (error "No article on current line"))
7998       (if (not (if (or (= mark gnus-unread-mark)
7999                        (= mark gnus-ticked-mark)
8000                        (= mark gnus-dormant-mark))
8001                    (gnus-mark-article-as-unread article mark)
8002                  (gnus-mark-article-as-read article mark)))
8003           t
8004         ;; See whether the article is to be put in the cache.
8005         (and gnus-use-cache
8006              (not (= mark gnus-canceled-mark))
8007              (vectorp (gnus-summary-article-header article))
8008              (save-excursion
8009                (gnus-cache-possibly-enter-article
8010                 gnus-newsgroup-name article
8011                 (gnus-summary-article-header article)
8012                 (= mark gnus-ticked-mark)
8013                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8014
8015         (when (gnus-summary-goto-subject article nil t)
8016           (let ((buffer-read-only nil))
8017             (gnus-summary-show-thread)
8018             ;; Fix the mark.
8019             (gnus-summary-update-mark mark 'unread)
8020             t))))))
8021
8022 (defun gnus-summary-update-secondary-mark (article)
8023   "Update the secondary (read, process, cache) mark."
8024   (gnus-summary-update-mark
8025    (cond ((memq article gnus-newsgroup-processable)
8026           gnus-process-mark)
8027          ((memq article gnus-newsgroup-cached)
8028           gnus-cached-mark)
8029          ((memq article gnus-newsgroup-replied)
8030           gnus-replied-mark)
8031          ((memq article gnus-newsgroup-saved)
8032           gnus-saved-mark)
8033          (t gnus-unread-mark))
8034    'replied)
8035   (when (gnus-visual-p 'summary-highlight 'highlight)
8036     (gnus-run-hooks 'gnus-summary-update-hook))
8037   t)
8038
8039 (defun gnus-summary-update-mark (mark type)
8040   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8041         (buffer-read-only nil))
8042     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8043     (when forward
8044       (when (looking-at "\r")
8045         (incf forward))
8046       (when (<= (+ forward (point)) (point-max))
8047         ;; Go to the right position on the line.
8048         (goto-char (+ forward (point)))
8049         ;; Replace the old mark with the new mark.
8050         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8051         ;; Optionally update the marks by some user rule.
8052         (when (eq type 'unread)
8053           (gnus-data-set-mark
8054            (gnus-data-find (gnus-summary-article-number)) mark)
8055           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8056
8057 (defun gnus-mark-article-as-read (article &optional mark)
8058   "Enter ARTICLE in the pertinent lists and remove it from others."
8059   ;; Make the article expirable.
8060   (let ((mark (or mark gnus-del-mark)))
8061     (if (= mark gnus-expirable-mark)
8062         (push article gnus-newsgroup-expirable)
8063       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8064     ;; Remove from unread and marked lists.
8065     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8066     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8067     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8068     (push (cons article mark) gnus-newsgroup-reads)
8069     ;; Possibly remove from cache, if that is used.
8070     (when gnus-use-cache
8071       (gnus-cache-enter-remove-article article))
8072     t))
8073
8074 (defun gnus-mark-article-as-unread (article &optional mark)
8075   "Enter ARTICLE in the pertinent lists and remove it from others."
8076   (let ((mark (or mark gnus-ticked-mark)))
8077     (if (<= article 0)
8078         (progn
8079           (gnus-error 1 "Can't mark negative article numbers")
8080           nil)
8081       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8082             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8083             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8084             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8085
8086       ;; Unsuppress duplicates?
8087       (when gnus-suppress-duplicates
8088         (gnus-dup-unsuppress-article article))
8089
8090       (cond ((= mark gnus-ticked-mark)
8091              (push article gnus-newsgroup-marked))
8092             ((= mark gnus-dormant-mark)
8093              (push article gnus-newsgroup-dormant))
8094             (t
8095              (push article gnus-newsgroup-unreads)))
8096       (gnus-pull article gnus-newsgroup-reads)
8097       t)))
8098
8099 (defalias 'gnus-summary-mark-as-unread-forward
8100   'gnus-summary-tick-article-forward)
8101 (make-obsolete 'gnus-summary-mark-as-unread-forward
8102                'gnus-summary-tick-article-forward)
8103 (defun gnus-summary-tick-article-forward (n)
8104   "Tick N articles forwards.
8105 If N is negative, tick backwards instead.
8106 The difference between N and the number of articles ticked is returned."
8107   (interactive "p")
8108   (gnus-summary-mark-forward n gnus-ticked-mark))
8109
8110 (defalias 'gnus-summary-mark-as-unread-backward
8111   'gnus-summary-tick-article-backward)
8112 (make-obsolete 'gnus-summary-mark-as-unread-backward
8113                'gnus-summary-tick-article-backward)
8114 (defun gnus-summary-tick-article-backward (n)
8115   "Tick N articles backwards.
8116 The difference between N and the number of articles ticked is returned."
8117   (interactive "p")
8118   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8119
8120 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8121 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8122 (defun gnus-summary-tick-article (&optional article clear-mark)
8123   "Mark current article as unread.
8124 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8125 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8126   (interactive)
8127   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8128                                        gnus-ticked-mark)))
8129
8130 (defun gnus-summary-mark-as-read-forward (n)
8131   "Mark N articles as read forwards.
8132 If N is negative, mark backwards instead.
8133 The difference between N and the actual number of articles marked is
8134 returned."
8135   (interactive "p")
8136   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8137
8138 (defun gnus-summary-mark-as-read-backward (n)
8139   "Mark the N articles as read backwards.
8140 The difference between N and the actual number of articles marked is
8141 returned."
8142   (interactive "p")
8143   (gnus-summary-mark-forward
8144    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8145
8146 (defun gnus-summary-mark-as-read (&optional article mark)
8147   "Mark current article as read.
8148 ARTICLE specifies the article to be marked as read.
8149 MARK specifies a string to be inserted at the beginning of the line."
8150   (gnus-summary-mark-article article mark))
8151
8152 (defun gnus-summary-clear-mark-forward (n)
8153   "Clear marks from N articles forward.
8154 If N is negative, clear backward instead.
8155 The difference between N and the number of marks cleared is returned."
8156   (interactive "p")
8157   (gnus-summary-mark-forward n gnus-unread-mark))
8158
8159 (defun gnus-summary-clear-mark-backward (n)
8160   "Clear marks from N articles backward.
8161 The difference between N and the number of marks cleared is returned."
8162   (interactive "p")
8163   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8164
8165 (defun gnus-summary-mark-unread-as-read ()
8166   "Intended to be used by `gnus-summary-mark-article-hook'."
8167   (when (memq gnus-current-article gnus-newsgroup-unreads)
8168     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8169
8170 (defun gnus-summary-mark-read-and-unread-as-read ()
8171   "Intended to be used by `gnus-summary-mark-article-hook'."
8172   (let ((mark (gnus-summary-article-mark)))
8173     (when (or (gnus-unread-mark-p mark)
8174               (gnus-read-mark-p mark))
8175       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8176
8177 (defun gnus-summary-mark-region-as-read (point mark all)
8178   "Mark all unread articles between point and mark as read.
8179 If given a prefix, mark all articles between point and mark as read,
8180 even ticked and dormant ones."
8181   (interactive "r\nP")
8182   (save-excursion
8183     (let (article)
8184       (goto-char point)
8185       (beginning-of-line)
8186       (while (and
8187               (< (point) mark)
8188               (progn
8189                 (when (or all
8190                           (memq (setq article (gnus-summary-article-number))
8191                                 gnus-newsgroup-unreads))
8192                   (gnus-summary-mark-article article gnus-del-mark))
8193                 t)
8194               (gnus-summary-find-next))))))
8195
8196 (defun gnus-summary-mark-below (score mark)
8197   "Mark articles with score less than SCORE with MARK."
8198   (interactive "P\ncMark: ")
8199   (setq score (if score
8200                   (prefix-numeric-value score)
8201                 (or gnus-summary-default-score 0)))
8202   (save-excursion
8203     (set-buffer gnus-summary-buffer)
8204     (goto-char (point-min))
8205     (while
8206         (progn
8207           (and (< (gnus-summary-article-score) score)
8208                (gnus-summary-mark-article nil mark))
8209           (gnus-summary-find-next)))))
8210
8211 (defun gnus-summary-kill-below (&optional score)
8212   "Mark articles with score below SCORE as read."
8213   (interactive "P")
8214   (gnus-summary-mark-below score gnus-killed-mark))
8215
8216 (defun gnus-summary-clear-above (&optional score)
8217   "Clear all marks from articles with score above SCORE."
8218   (interactive "P")
8219   (gnus-summary-mark-above score gnus-unread-mark))
8220
8221 (defun gnus-summary-tick-above (&optional score)
8222   "Tick all articles with score above SCORE."
8223   (interactive "P")
8224   (gnus-summary-mark-above score gnus-ticked-mark))
8225
8226 (defun gnus-summary-mark-above (score mark)
8227   "Mark articles with score over SCORE with MARK."
8228   (interactive "P\ncMark: ")
8229   (setq score (if score
8230                   (prefix-numeric-value score)
8231                 (or gnus-summary-default-score 0)))
8232   (save-excursion
8233     (set-buffer gnus-summary-buffer)
8234     (goto-char (point-min))
8235     (while (and (progn
8236                   (when (> (gnus-summary-article-score) score)
8237                     (gnus-summary-mark-article nil mark))
8238                   t)
8239                 (gnus-summary-find-next)))))
8240
8241 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8242 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8243 (defun gnus-summary-limit-include-expunged (&optional no-error)
8244   "Display all the hidden articles that were expunged for low scores."
8245   (interactive)
8246   (let ((buffer-read-only nil))
8247     (let ((scored gnus-newsgroup-scored)
8248           headers h)
8249       (while scored
8250         (unless (gnus-summary-goto-subject (caar scored))
8251           (and (setq h (gnus-summary-article-header (caar scored)))
8252                (< (cdar scored) gnus-summary-expunge-below)
8253                (push h headers)))
8254         (setq scored (cdr scored)))
8255       (if (not headers)
8256           (when (not no-error)
8257             (error "No expunged articles hidden"))
8258         (goto-char (point-min))
8259         (gnus-summary-prepare-unthreaded (nreverse headers))
8260         (goto-char (point-min))
8261         (gnus-summary-position-point)
8262         t))))
8263
8264 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8265   "Mark all unread articles in this newsgroup as read.
8266 If prefix argument ALL is non-nil, ticked and dormant articles will
8267 also be marked as read.
8268 If QUIETLY is non-nil, no questions will be asked.
8269 If TO-HERE is non-nil, it should be a point in the buffer.  All
8270 articles before this point will be marked as read.
8271 Note that this function will only catch up the unread article
8272 in the current summary buffer limitation.
8273 The number of articles marked as read is returned."
8274   (interactive "P")
8275   (prog1
8276       (save-excursion
8277         (when (or quietly
8278                   (not gnus-interactive-catchup) ;Without confirmation?
8279                   gnus-expert-user
8280                   (gnus-y-or-n-p
8281                    (if all
8282                        "Mark absolutely all articles as read? "
8283                      "Mark all unread articles as read? ")))
8284           (if (and not-mark
8285                    (not gnus-newsgroup-adaptive)
8286                    (not gnus-newsgroup-auto-expire)
8287                    (not gnus-suppress-duplicates)
8288                    (or (not gnus-use-cache)
8289                        (eq gnus-use-cache 'passive)))
8290               (progn
8291                 (when all
8292                   (setq gnus-newsgroup-marked nil
8293                         gnus-newsgroup-dormant nil))
8294                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8295             ;; We actually mark all articles as canceled, which we
8296             ;; have to do when using auto-expiry or adaptive scoring.
8297             (gnus-summary-show-all-threads)
8298             (when (gnus-summary-first-subject (not all) t)
8299               (while (and
8300                       (if to-here (< (point) to-here) t)
8301                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8302                       (gnus-summary-find-next (not all) nil nil t))))
8303             (gnus-set-mode-line 'summary))
8304           t))
8305     (gnus-summary-position-point)))
8306
8307 (defun gnus-summary-catchup-to-here (&optional all)
8308   "Mark all unticked articles before the current one as read.
8309 If ALL is non-nil, also mark ticked and dormant articles as read."
8310   (interactive "P")
8311   (save-excursion
8312     (gnus-save-hidden-threads
8313       (let ((beg (point)))
8314         ;; We check that there are unread articles.
8315         (when (or all (gnus-summary-find-prev))
8316           (gnus-summary-catchup all t beg)))))
8317   (gnus-summary-position-point))
8318
8319 (defun gnus-summary-catchup-all (&optional quietly)
8320   "Mark all articles in this newsgroup as read."
8321   (interactive "P")
8322   (gnus-summary-catchup t quietly))
8323
8324 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8325   "Mark all unread articles in this group as read, then exit.
8326 If prefix argument ALL is non-nil, all articles are marked as read."
8327   (interactive "P")
8328   (when (gnus-summary-catchup all quietly nil 'fast)
8329     ;; Select next newsgroup or exit.
8330     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8331              (eq gnus-auto-select-next 'quietly))
8332         (gnus-summary-next-group nil)
8333       (gnus-summary-exit))))
8334
8335 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8336   "Mark all articles in this newsgroup as read, and then exit."
8337   (interactive "P")
8338   (gnus-summary-catchup-and-exit t quietly))
8339
8340 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8341 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8342   "Mark all articles in this group as read and select the next group.
8343 If given a prefix, mark all articles, unread as well as ticked, as
8344 read."
8345   (interactive "P")
8346   (save-excursion
8347     (gnus-summary-catchup all))
8348   (gnus-summary-next-article t nil nil t))
8349
8350 ;; Thread-based commands.
8351
8352 (defun gnus-summary-articles-in-thread (&optional article)
8353   "Return a list of all articles in the current thread.
8354 If ARTICLE is non-nil, return all articles in the thread that starts
8355 with that article."
8356   (let* ((article (or article (gnus-summary-article-number)))
8357          (data (gnus-data-find-list article))
8358          (top-level (gnus-data-level (car data)))
8359          (top-subject
8360           (cond ((null gnus-thread-operation-ignore-subject)
8361                  (gnus-simplify-subject-re
8362                   (mail-header-subject (gnus-data-header (car data)))))
8363                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8364                  (gnus-simplify-subject-fuzzy
8365                   (mail-header-subject (gnus-data-header (car data)))))
8366                 (t nil)))
8367          (end-point (save-excursion
8368                       (if (gnus-summary-go-to-next-thread)
8369                           (point) (point-max))))
8370          articles)
8371     (while (and data
8372                 (< (gnus-data-pos (car data)) end-point))
8373       (when (or (not top-subject)
8374                 (string= top-subject
8375                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8376                              (gnus-simplify-subject-fuzzy
8377                               (mail-header-subject
8378                                (gnus-data-header (car data))))
8379                            (gnus-simplify-subject-re
8380                             (mail-header-subject
8381                              (gnus-data-header (car data)))))))
8382         (push (gnus-data-number (car data)) articles))
8383       (unless (and (setq data (cdr data))
8384                    (> (gnus-data-level (car data)) top-level))
8385         (setq data nil)))
8386     ;; Return the list of articles.
8387     (nreverse articles)))
8388
8389 (defun gnus-summary-rethread-current ()
8390   "Rethread the thread the current article is part of."
8391   (interactive)
8392   (let* ((gnus-show-threads t)
8393          (article (gnus-summary-article-number))
8394          (id (mail-header-id (gnus-summary-article-header)))
8395          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8396     (unless id
8397       (error "No article on the current line"))
8398     (gnus-rebuild-thread id)
8399     (gnus-summary-goto-subject article)))
8400
8401 (defun gnus-summary-reparent-thread ()
8402   "Make the current article child of the marked (or previous) article.
8403
8404 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8405 is non-nil or the Subject: of both articles are the same."
8406   (interactive)
8407   (unless (not (gnus-group-read-only-p))
8408     (error "The current newsgroup does not support article editing"))
8409   (unless (<= (length gnus-newsgroup-processable) 1)
8410     (error "No more than one article may be marked"))
8411   (save-window-excursion
8412     (let ((gnus-article-buffer " *reparent*")
8413           (current-article (gnus-summary-article-number))
8414           ;; First grab the marked article, otherwise one line up.
8415           (parent-article (if (not (null gnus-newsgroup-processable))
8416                               (car gnus-newsgroup-processable)
8417                             (save-excursion
8418                               (if (eq (forward-line -1) 0)
8419                                   (gnus-summary-article-number)
8420                                 (error "Beginning of summary buffer"))))))
8421       (unless (not (eq current-article parent-article))
8422         (error "An article may not be self-referential"))
8423       (let ((message-id (mail-header-id
8424                          (gnus-summary-article-header parent-article))))
8425         (unless (and message-id (not (equal message-id "")))
8426           (error "No message-id in desired parent"))
8427         (gnus-with-article current-article
8428           (goto-char (point-min))
8429           (if (re-search-forward "^References: " nil t)
8430               (progn
8431                 (re-search-forward "^[^ \t]" nil t)
8432                 (forward-line -1)
8433                 (end-of-line)
8434                 (insert " " message-id))
8435             (insert "References: " message-id "\n")))
8436         (set-buffer gnus-summary-buffer)
8437         (gnus-summary-unmark-all-processable)
8438         (gnus-summary-update-article current-article)
8439         (gnus-summary-rethread-current)
8440         (gnus-message 3 "Article %d is now the child of article %d"
8441                       current-article parent-article)))))
8442
8443 (defun gnus-summary-toggle-threads (&optional arg)
8444   "Toggle showing conversation threads.
8445 If ARG is positive number, turn showing conversation threads on."
8446   (interactive "P")
8447   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8448     (setq gnus-show-threads
8449           (if (null arg) (not gnus-show-threads)
8450             (> (prefix-numeric-value arg) 0)))
8451     (gnus-summary-prepare)
8452     (gnus-summary-goto-subject current)
8453     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8454     (gnus-summary-position-point)))
8455
8456 (defun gnus-summary-show-all-threads ()
8457   "Show all threads."
8458   (interactive)
8459   (save-excursion
8460     (let ((buffer-read-only nil))
8461       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8462   (gnus-summary-position-point))
8463
8464 (defun gnus-summary-show-thread ()
8465   "Show thread subtrees.
8466 Returns nil if no thread was there to be shown."
8467   (interactive)
8468   (let ((buffer-read-only nil)
8469         (orig (point))
8470         ;; first goto end then to beg, to have point at beg after let
8471         (end (progn (end-of-line) (point)))
8472         (beg (progn (beginning-of-line) (point))))
8473     (prog1
8474         ;; Any hidden lines here?
8475         (search-forward "\r" end t)
8476       (subst-char-in-region beg end ?\^M ?\n t)
8477       (goto-char orig)
8478       (gnus-summary-position-point))))
8479
8480 (defun gnus-summary-hide-all-threads ()
8481   "Hide all thread subtrees."
8482   (interactive)
8483   (save-excursion
8484     (goto-char (point-min))
8485     (gnus-summary-hide-thread)
8486     (while (zerop (gnus-summary-next-thread 1 t))
8487       (gnus-summary-hide-thread)))
8488   (gnus-summary-position-point))
8489
8490 (defun gnus-summary-hide-thread ()
8491   "Hide thread subtrees.
8492 Returns nil if no threads were there to be hidden."
8493   (interactive)
8494   (let ((buffer-read-only nil)
8495         (start (point))
8496         (article (gnus-summary-article-number)))
8497     (goto-char start)
8498     ;; Go forward until either the buffer ends or the subthread
8499     ;; ends.
8500     (when (and (not (eobp))
8501                (or (zerop (gnus-summary-next-thread 1 t))
8502                    (goto-char (point-max))))
8503       (prog1
8504           (if (and (> (point) start)
8505                    (search-backward "\n" start t))
8506               (progn
8507                 (subst-char-in-region start (point) ?\n ?\^M)
8508                 (gnus-summary-goto-subject article))
8509             (goto-char start)
8510             nil)
8511         ;;(gnus-summary-position-point)
8512         ))))
8513
8514 (defun gnus-summary-go-to-next-thread (&optional previous)
8515   "Go to the same level (or less) next thread.
8516 If PREVIOUS is non-nil, go to previous thread instead.
8517 Return the article number moved to, or nil if moving was impossible."
8518   (let ((level (gnus-summary-thread-level))
8519         (way (if previous -1 1))
8520         (beg (point)))
8521     (forward-line way)
8522     (while (and (not (eobp))
8523                 (< level (gnus-summary-thread-level)))
8524       (forward-line way))
8525     (if (eobp)
8526         (progn
8527           (goto-char beg)
8528           nil)
8529       (setq beg (point))
8530       (prog1
8531           (gnus-summary-article-number)
8532         (goto-char beg)))))
8533
8534 (defun gnus-summary-next-thread (n &optional silent)
8535   "Go to the same level next N'th thread.
8536 If N is negative, search backward instead.
8537 Returns the difference between N and the number of skips actually
8538 done.
8539
8540 If SILENT, don't output messages."
8541   (interactive "p")
8542   (let ((backward (< n 0))
8543         (n (abs n)))
8544     (while (and (> n 0)
8545                 (gnus-summary-go-to-next-thread backward))
8546       (decf n))
8547     (unless silent
8548       (gnus-summary-position-point))
8549     (when (and (not silent) (/= 0 n))
8550       (gnus-message 7 "No more threads"))
8551     n))
8552
8553 (defun gnus-summary-prev-thread (n)
8554   "Go to the same level previous N'th thread.
8555 Returns the difference between N and the number of skips actually
8556 done."
8557   (interactive "p")
8558   (gnus-summary-next-thread (- n)))
8559
8560 (defun gnus-summary-go-down-thread ()
8561   "Go down one level in the current thread."
8562   (let ((children (gnus-summary-article-children)))
8563     (when children
8564       (gnus-summary-goto-subject (car children)))))
8565
8566 (defun gnus-summary-go-up-thread ()
8567   "Go up one level in the current thread."
8568   (let ((parent (gnus-summary-article-parent)))
8569     (when parent
8570       (gnus-summary-goto-subject parent))))
8571
8572 (defun gnus-summary-down-thread (n)
8573   "Go down thread N steps.
8574 If N is negative, go up instead.
8575 Returns the difference between N and how many steps down that were
8576 taken."
8577   (interactive "p")
8578   (let ((up (< n 0))
8579         (n (abs n)))
8580     (while (and (> n 0)
8581                 (if up (gnus-summary-go-up-thread)
8582                   (gnus-summary-go-down-thread)))
8583       (setq n (1- n)))
8584     (gnus-summary-position-point)
8585     (when (/= 0 n)
8586       (gnus-message 7 "Can't go further"))
8587     n))
8588
8589 (defun gnus-summary-up-thread (n)
8590   "Go up thread N steps.
8591 If N is negative, go up instead.
8592 Returns the difference between N and how many steps down that were
8593 taken."
8594   (interactive "p")
8595   (gnus-summary-down-thread (- n)))
8596
8597 (defun gnus-summary-top-thread ()
8598   "Go to the top of the thread."
8599   (interactive)
8600   (while (gnus-summary-go-up-thread))
8601   (gnus-summary-article-number))
8602
8603 (defun gnus-summary-kill-thread (&optional unmark)
8604   "Mark articles under current thread as read.
8605 If the prefix argument is positive, remove any kinds of marks.
8606 If the prefix argument is negative, tick articles instead."
8607   (interactive "P")
8608   (when unmark
8609     (setq unmark (prefix-numeric-value unmark)))
8610   (let ((articles (gnus-summary-articles-in-thread)))
8611     (save-excursion
8612       ;; Expand the thread.
8613       (gnus-summary-show-thread)
8614       ;; Mark all the articles.
8615       (while articles
8616         (gnus-summary-goto-subject (car articles))
8617         (cond ((null unmark)
8618                (gnus-summary-mark-article-as-read gnus-killed-mark))
8619               ((> unmark 0)
8620                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8621               (t
8622                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8623         (setq articles (cdr articles))))
8624     ;; Hide killed subtrees.
8625     (and (null unmark)
8626          gnus-thread-hide-killed
8627          (gnus-summary-hide-thread))
8628     ;; If marked as read, go to next unread subject.
8629     (when (null unmark)
8630       ;; Go to next unread subject.
8631       (gnus-summary-next-subject 1 t)))
8632   (gnus-set-mode-line 'summary))
8633
8634 ;; Summary sorting commands
8635
8636 (defun gnus-summary-sort-by-number (&optional reverse)
8637   "Sort the summary buffer by article number.
8638 Argument REVERSE means reverse order."
8639   (interactive "P")
8640   (gnus-summary-sort 'number reverse))
8641
8642 (defun gnus-summary-sort-by-author (&optional reverse)
8643   "Sort the summary buffer by author name alphabetically.
8644 If case-fold-search is non-nil, case of letters is ignored.
8645 Argument REVERSE means reverse order."
8646   (interactive "P")
8647   (gnus-summary-sort 'author reverse))
8648
8649 (defun gnus-summary-sort-by-subject (&optional reverse)
8650   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8651 If case-fold-search is non-nil, case of letters is ignored.
8652 Argument REVERSE means reverse order."
8653   (interactive "P")
8654   (gnus-summary-sort 'subject reverse))
8655
8656 (defun gnus-summary-sort-by-date (&optional reverse)
8657   "Sort the summary buffer by date.
8658 Argument REVERSE means reverse order."
8659   (interactive "P")
8660   (gnus-summary-sort 'date reverse))
8661
8662 (defun gnus-summary-sort-by-score (&optional reverse)
8663   "Sort the summary buffer by score.
8664 Argument REVERSE means reverse order."
8665   (interactive "P")
8666   (gnus-summary-sort 'score reverse))
8667
8668 (defun gnus-summary-sort-by-lines (&optional reverse)
8669   "Sort the summary buffer by the number of lines.
8670 Argument REVERSE means reverse order."
8671   (interactive "P")
8672   (gnus-summary-sort 'lines reverse))
8673
8674 (defun gnus-summary-sort-by-chars (&optional reverse)
8675   "Sort the summary buffer by article length.
8676 Argument REVERSE means reverse order."
8677   (interactive "P")
8678   (gnus-summary-sort 'chars reverse))   
8679
8680 (defun gnus-summary-sort (predicate reverse)
8681   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8682   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8683          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8684          (gnus-thread-sort-functions
8685           (if (not reverse)
8686               thread
8687             `(lambda (t1 t2)
8688                (,thread t2 t1))))
8689          (gnus-article-sort-functions
8690           (if (not reverse)
8691               article
8692             `(lambda (t1 t2)
8693                (,article t2 t1))))
8694          (buffer-read-only)
8695          (gnus-summary-prepare-hook nil))
8696     ;; We do the sorting by regenerating the threads.
8697     (gnus-summary-prepare)
8698     ;; Hide subthreads if needed.
8699     (when (and gnus-show-threads gnus-thread-hide-subtree)
8700       (gnus-summary-hide-all-threads))))
8701
8702 ;; Summary saving commands.
8703
8704 (defun gnus-summary-save-article (&optional n not-saved)
8705   "Save the current article using the default saver function.
8706 If N is a positive number, save the N next articles.
8707 If N is a negative number, save the N previous articles.
8708 If N is nil and any articles have been marked with the process mark,
8709 save those articles instead.
8710 The variable `gnus-default-article-saver' specifies the saver function."
8711   (interactive "P")
8712   (let* ((articles (gnus-summary-work-articles n))
8713          (save-buffer (save-excursion
8714                         (nnheader-set-temp-buffer " *Gnus Save*")))
8715          (num (length articles))
8716          header file)
8717     (dolist (article articles)
8718       (setq header (gnus-summary-article-header article))
8719       (if (not (vectorp header))
8720           ;; This is a pseudo-article.
8721           (if (assq 'name header)
8722               (gnus-copy-file (cdr (assq 'name header)))
8723             (gnus-message 1 "Article %d is unsaveable" article))
8724         ;; This is a real article.
8725         (save-window-excursion
8726           (gnus-summary-select-article t nil nil article))
8727         (save-excursion
8728           (set-buffer save-buffer)
8729           (erase-buffer)
8730           (insert-buffer-substring gnus-original-article-buffer))
8731         (setq file (gnus-article-save save-buffer file num))
8732         (gnus-summary-remove-process-mark article)
8733         (unless not-saved
8734           (gnus-summary-set-saved-mark article))))
8735     (gnus-kill-buffer save-buffer)
8736     (gnus-summary-position-point)
8737     (gnus-set-mode-line 'summary)
8738     n))
8739
8740 (defun gnus-summary-pipe-output (&optional arg)
8741   "Pipe the current article to a subprocess.
8742 If N is a positive number, pipe the N next articles.
8743 If N is a negative number, pipe the N previous articles.
8744 If N is nil and any articles have been marked with the process mark,
8745 pipe those articles instead."
8746   (interactive "P")
8747   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8748     (gnus-summary-save-article arg t))
8749   (gnus-configure-windows 'pipe))
8750
8751 (defun gnus-summary-save-article-mail (&optional arg)
8752   "Append the current article to an mail file.
8753 If N is a positive number, save the N next articles.
8754 If N is a negative number, save the N previous articles.
8755 If N is nil and any articles have been marked with the process mark,
8756 save those articles instead."
8757   (interactive "P")
8758   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8759     (gnus-summary-save-article arg)))
8760
8761 (defun gnus-summary-save-article-rmail (&optional arg)
8762   "Append the current article to an rmail file.
8763 If N is a positive number, save the N next articles.
8764 If N is a negative number, save the N previous articles.
8765 If N is nil and any articles have been marked with the process mark,
8766 save those articles instead."
8767   (interactive "P")
8768   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8769     (gnus-summary-save-article arg)))
8770
8771 (defun gnus-summary-save-article-file (&optional arg)
8772   "Append the current article to a file.
8773 If N is a positive number, save the N next articles.
8774 If N is a negative number, save the N previous articles.
8775 If N is nil and any articles have been marked with the process mark,
8776 save those articles instead."
8777   (interactive "P")
8778   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8779     (gnus-summary-save-article arg)))
8780
8781 (defun gnus-summary-write-article-file (&optional arg)
8782   "Write the current article to a file, deleting the previous file.
8783 If N is a positive number, save the N next articles.
8784 If N is a negative number, save the N previous articles.
8785 If N is nil and any articles have been marked with the process mark,
8786 save those articles instead."
8787   (interactive "P")
8788   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8789     (gnus-summary-save-article arg)))
8790
8791 (defun gnus-summary-save-article-body-file (&optional arg)
8792   "Append the current article body to a file.
8793 If N is a positive number, save the N next articles.
8794 If N is a negative number, save the N previous articles.
8795 If N is nil and any articles have been marked with the process mark,
8796 save those articles instead."
8797   (interactive "P")
8798   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8799     (gnus-summary-save-article arg)))
8800
8801 (defun gnus-summary-pipe-message (program)
8802   "Pipe the current article through PROGRAM."
8803   (interactive "sProgram: ")
8804   (gnus-summary-select-article)
8805   (let ((mail-header-separator ""))
8806     (gnus-eval-in-buffer-window gnus-article-buffer
8807       (save-restriction
8808         (widen)
8809         (let ((start (window-start))
8810               buffer-read-only)
8811           (message-pipe-buffer-body program)
8812           (set-window-start (get-buffer-window (current-buffer)) start))))))
8813
8814 (defun gnus-get-split-value (methods)
8815   "Return a value based on the split METHODS."
8816   (let (split-name method result match)
8817     (when methods
8818       (save-excursion
8819         (set-buffer gnus-original-article-buffer)
8820         (save-restriction
8821           (nnheader-narrow-to-headers)
8822           (while methods
8823             (goto-char (point-min))
8824             (setq method (pop methods))
8825             (setq match (car method))
8826             (when (cond
8827                    ((stringp match)
8828                     ;; Regular expression.
8829                     (ignore-errors
8830                       (re-search-forward match nil t)))
8831                    ((gnus-functionp match)
8832                     ;; Function.
8833                     (save-restriction
8834                       (widen)
8835                       (setq result (funcall match gnus-newsgroup-name))))
8836                    ((consp match)
8837                     ;; Form.
8838                     (save-restriction
8839                       (widen)
8840                       (setq result (eval match)))))
8841               (setq split-name (append (cdr method) split-name))
8842               (cond ((stringp result)
8843                      (push (expand-file-name
8844                             result gnus-article-save-directory)
8845                            split-name))
8846                     ((consp result)
8847                      (setq split-name (append result split-name)))))))))
8848     (nreverse split-name)))
8849
8850 (defun gnus-valid-move-group-p (group)
8851   (and (boundp group)
8852        (symbol-name group)
8853        (symbol-value group)
8854        (gnus-get-function (gnus-find-method-for-group
8855                            (symbol-name group)) 'request-accept-article t)))
8856
8857 (defun gnus-read-move-group-name (prompt default articles prefix)
8858   "Read a group name."
8859   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8860          (minibuffer-confirm-incomplete nil) ; XEmacs
8861          (prom
8862           (format "%s %s to:"
8863                   prompt
8864                   (if (> (length articles) 1)
8865                       (format "these %d articles" (length articles))
8866                     "this article")))
8867          (to-newsgroup
8868           (cond
8869            ((null split-name)
8870             (gnus-completing-read default prom
8871                                   gnus-active-hashtb
8872                                   'gnus-valid-move-group-p
8873                                   nil prefix
8874                                   'gnus-group-history))
8875            ((= 1 (length split-name))
8876             (gnus-completing-read (car split-name) prom
8877                                   gnus-active-hashtb
8878                                   'gnus-valid-move-group-p
8879                                   nil nil
8880                                   'gnus-group-history))
8881            (t
8882             (gnus-completing-read nil prom
8883                                   (mapcar (lambda (el) (list el))
8884                                           (nreverse split-name))
8885                                   nil nil nil
8886                                   'gnus-group-history)))))
8887     (when to-newsgroup
8888       (if (or (string= to-newsgroup "")
8889               (string= to-newsgroup prefix))
8890           (setq to-newsgroup default))
8891       (unless to-newsgroup
8892         (error "No group name entered"))
8893       (or (gnus-active to-newsgroup)
8894           (gnus-activate-group to-newsgroup)
8895           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8896                                      to-newsgroup))
8897               (or (and (gnus-request-create-group
8898                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8899                        (gnus-activate-group to-newsgroup nil nil
8900                                             (gnus-group-name-to-method
8901                                              to-newsgroup)))
8902                   (error "Couldn't create group %s" to-newsgroup)))
8903           (error "No such group: %s" to-newsgroup)))
8904     to-newsgroup))
8905
8906 (defun gnus-summary-save-parts (type dir n reverse)
8907   "Save parts matching TYPE to DIR.
8908 If REVERSE, save parts that do not match TYPE."
8909   (interactive
8910    (list (read-string "Save parts of type: " "image/.*")
8911          (read-file-name "Save to directory: " t nil t)
8912          current-prefix-arg))
8913   (gnus-summary-iterate n
8914     (let ((gnus-display-mime-function nil)
8915           (gnus-inhibit-treatment t))
8916       (gnus-summary-select-article))
8917     (save-excursion
8918       (set-buffer gnus-article-buffer)
8919       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8920         (when handles
8921           (gnus-summary-save-parts-1 type dir handles reverse)
8922           (mm-destroy-parts handles))))))
8923
8924 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8925   (if (stringp (car handle))
8926       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8927               (cdr handle))
8928     (when (if reverse
8929               (not (string-match type (mm-handle-media-type handle)))
8930             (string-match type (mm-handle-media-type handle)))
8931       (let ((file (expand-file-name
8932                    (file-name-nondirectory
8933                     (or
8934                      (mail-content-type-get
8935                       (mm-handle-disposition handle) 'filename)
8936                      (concat gnus-newsgroup-name "." gnus-current-article)))
8937                    dir)))
8938         (unless (file-exists-p file)
8939           (mm-save-part-to-file handle file))))))
8940
8941 ;; Summary extract commands
8942
8943 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8944   (let ((buffer-read-only nil)
8945         (article (gnus-summary-article-number))
8946         after-article b e)
8947     (unless (gnus-summary-goto-subject article)
8948       (error "No such article: %d" article))
8949     (gnus-summary-position-point)
8950     ;; If all commands are to be bunched up on one line, we collect
8951     ;; them here.
8952     (unless gnus-view-pseudos-separately
8953       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8954             files action)
8955         (while ps
8956           (setq action (cdr (assq 'action (car ps))))
8957           (setq files (list (cdr (assq 'name (car ps)))))
8958           (while (and ps (cdr ps)
8959                       (string= (or action "1")
8960                                (or (cdr (assq 'action (cadr ps))) "2")))
8961             (push (cdr (assq 'name (cadr ps))) files)
8962             (setcdr ps (cddr ps)))
8963           (when files
8964             (when (not (string-match "%s" action))
8965               (push " " files))
8966             (push " " files)
8967             (when (assq 'execute (car ps))
8968               (setcdr (assq 'execute (car ps))
8969                       (funcall (if (string-match "%s" action)
8970                                    'format 'concat)
8971                                action
8972                                (mapconcat
8973                                 (lambda (f)
8974                                   (if (equal f " ")
8975                                       f
8976                                     (gnus-quote-arg-for-sh-or-csh f)))
8977                                 files " ")))))
8978           (setq ps (cdr ps)))))
8979     (if (and gnus-view-pseudos (not not-view))
8980         (while pslist
8981           (when (assq 'execute (car pslist))
8982             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8983                                   (eq gnus-view-pseudos 'not-confirm)))
8984           (setq pslist (cdr pslist)))
8985       (save-excursion
8986         (while pslist
8987           (setq after-article (or (cdr (assq 'article (car pslist)))
8988                                   (gnus-summary-article-number)))
8989           (gnus-summary-goto-subject after-article)
8990           (forward-line 1)
8991           (setq b (point))
8992           (insert "    " (file-name-nondirectory
8993                           (cdr (assq 'name (car pslist))))
8994                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8995           (setq e (point))
8996           (forward-line -1)             ; back to `b'
8997           (gnus-add-text-properties
8998            b (1- e) (list 'gnus-number gnus-reffed-article-number
8999                           gnus-mouse-face-prop gnus-mouse-face))
9000           (gnus-data-enter
9001            after-article gnus-reffed-article-number
9002            gnus-unread-mark b (car pslist) 0 (- e b))
9003           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9004           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9005           (setq pslist (cdr pslist)))))))
9006
9007 (defun gnus-pseudos< (p1 p2)
9008   (let ((c1 (cdr (assq 'action p1)))
9009         (c2 (cdr (assq 'action p2))))
9010     (and c1 c2 (string< c1 c2))))
9011
9012 (defun gnus-request-pseudo-article (props)
9013   (cond ((assq 'execute props)
9014          (gnus-execute-command (cdr (assq 'execute props)))))
9015   (let ((gnus-current-article (gnus-summary-article-number)))
9016     (gnus-run-hooks 'gnus-mark-article-hook)))
9017
9018 (defun gnus-execute-command (command &optional automatic)
9019   (save-excursion
9020     (gnus-article-setup-buffer)
9021     (set-buffer gnus-article-buffer)
9022     (setq buffer-read-only nil)
9023     (let ((command (if automatic command
9024                      (read-string "Command: " (cons command 0)))))
9025       (erase-buffer)
9026       (insert "$ " command "\n\n")
9027       (if gnus-view-pseudo-asynchronously
9028           (start-process "gnus-execute" (current-buffer) shell-file-name
9029                          shell-command-switch command)
9030         (call-process shell-file-name nil t nil
9031                       shell-command-switch command)))))
9032
9033 ;; Summary kill commands.
9034
9035 (defun gnus-summary-edit-global-kill (article)
9036   "Edit the \"global\" kill file."
9037   (interactive (list (gnus-summary-article-number)))
9038   (gnus-group-edit-global-kill article))
9039
9040 (defun gnus-summary-edit-local-kill ()
9041   "Edit a local kill file applied to the current newsgroup."
9042   (interactive)
9043   (setq gnus-current-headers (gnus-summary-article-header))
9044   (gnus-group-edit-local-kill
9045    (gnus-summary-article-number) gnus-newsgroup-name))
9046
9047 ;;; Header reading.
9048
9049 (defun gnus-read-header (id &optional header)
9050   "Read the headers of article ID and enter them into the Gnus system."
9051   (let ((group gnus-newsgroup-name)
9052         (gnus-override-method
9053          (and (gnus-news-group-p gnus-newsgroup-name)
9054               gnus-refer-article-method))
9055         where)
9056     ;; First we check to see whether the header in question is already
9057     ;; fetched.
9058     (if (stringp id)
9059         ;; This is a Message-ID.
9060         (setq header (or header (gnus-id-to-header id)))
9061       ;; This is an article number.
9062       (setq header (or header (gnus-summary-article-header id))))
9063     (if (and header
9064              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9065         ;; We have found the header.
9066         header
9067       ;; If this is a sparse article, we have to nix out its
9068       ;; previous entry in the thread hashtb.
9069       (when (and header
9070                  (gnus-summary-article-sparse-p (mail-header-number header)))
9071         (let* ((parent (gnus-parent-id (mail-header-references header)))
9072                (thread (and parent (gnus-id-to-thread parent))))
9073           (when thread
9074             (delq (assq header thread) thread))))
9075       ;; We have to really fetch the header to this article.
9076       (save-excursion
9077         (set-buffer nntp-server-buffer)
9078         (when (setq where (gnus-request-head id group))
9079           (nnheader-fold-continuation-lines)
9080           (goto-char (point-max))
9081           (insert ".\n")
9082           (goto-char (point-min))
9083           (insert "211 ")
9084           (princ (cond
9085                   ((numberp id) id)
9086                   ((cdr where) (cdr where))
9087                   (header (mail-header-number header))
9088                   (t gnus-reffed-article-number))
9089                  (current-buffer))
9090           (insert " Article retrieved.\n"))
9091         (if (or (not where)
9092                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9093             ()                          ; Malformed head.
9094           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9095             (when (and (stringp id)
9096                        (not (string= (gnus-group-real-name group)
9097                                      (car where))))
9098               ;; If we fetched by Message-ID and the article came
9099               ;; from a different group, we fudge some bogus article
9100               ;; numbers for this article.
9101               (mail-header-set-number header gnus-reffed-article-number))
9102             (save-excursion
9103               (set-buffer gnus-summary-buffer)
9104               (decf gnus-reffed-article-number)
9105               (gnus-remove-header (mail-header-number header))
9106               (push header gnus-newsgroup-headers)
9107               (setq gnus-current-headers header)
9108               (push (mail-header-number header) gnus-newsgroup-limit)))
9109           header)))))
9110
9111 (defun gnus-remove-header (number)
9112   "Remove header NUMBER from `gnus-newsgroup-headers'."
9113   (if (and gnus-newsgroup-headers
9114            (= number (mail-header-number (car gnus-newsgroup-headers))))
9115       (pop gnus-newsgroup-headers)
9116     (let ((headers gnus-newsgroup-headers))
9117       (while (and (cdr headers)
9118                   (not (= number (mail-header-number (cadr headers)))))
9119         (pop headers))
9120       (when (cdr headers)
9121         (setcdr headers (cddr headers))))))
9122
9123 ;;;
9124 ;;; summary highlights
9125 ;;;
9126
9127 (defun gnus-highlight-selected-summary ()
9128   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9129   ;; Highlight selected article in summary buffer
9130   (when gnus-summary-selected-face
9131     (save-excursion
9132       (let* ((beg (progn (beginning-of-line) (point)))
9133              (end (progn (end-of-line) (point)))
9134              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9135              (from (if (get-text-property beg gnus-mouse-face-prop)
9136                        beg
9137                      (or (next-single-property-change
9138                           beg gnus-mouse-face-prop nil end)
9139                          beg)))
9140              (to
9141               (if (= from end)
9142                   (- from 2)
9143                 (or (next-single-property-change
9144                      from gnus-mouse-face-prop nil end)
9145                     end))))
9146         ;; If no mouse-face prop on line we will have to = from = end,
9147         ;; so we highlight the entire line instead.
9148         (when (= (+ to 2) from)
9149           (setq from beg)
9150           (setq to end))
9151         (if gnus-newsgroup-selected-overlay
9152             ;; Move old overlay.
9153             (gnus-move-overlay
9154              gnus-newsgroup-selected-overlay from to (current-buffer))
9155           ;; Create new overlay.
9156           (gnus-overlay-put
9157            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9158            'face gnus-summary-selected-face))))))
9159
9160 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9161 (defun gnus-summary-highlight-line ()
9162   "Highlight current line according to `gnus-summary-highlight'."
9163   (let* ((list gnus-summary-highlight)
9164          (p (point))
9165          (end (progn (end-of-line) (point)))
9166          ;; now find out where the line starts and leave point there.
9167          (beg (progn (beginning-of-line) (point)))
9168          (article (gnus-summary-article-number))
9169          (score (or (cdr (assq (or article gnus-current-article)
9170                                gnus-newsgroup-scored))
9171                     gnus-summary-default-score 0))
9172          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9173          (inhibit-read-only t))
9174     ;; Eval the cars of the lists until we find a match.
9175     (let ((default gnus-summary-default-score))
9176       (while (and list
9177                   (not (eval (caar list))))
9178         (setq list (cdr list))))
9179     (let ((face (cdar list)))
9180       (unless (eq face (get-text-property beg 'face))
9181         (gnus-put-text-property-excluding-characters-with-faces
9182          beg end 'face
9183          (setq face (if (boundp face) (symbol-value face) face)))
9184         (when gnus-summary-highlight-line-function
9185           (funcall gnus-summary-highlight-line-function article face))))
9186     (goto-char p)))
9187
9188 (defun gnus-update-read-articles (group unread &optional compute)
9189   "Update the list of read articles in GROUP."
9190   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9191          (entry (gnus-gethash group gnus-newsrc-hashtb))
9192          (info (nth 2 entry))
9193          (prev 1)
9194          (unread (sort (copy-sequence unread) '<))
9195          read)
9196     (if (or (not info) (not active))
9197         ;; There is no info on this group if it was, in fact,
9198         ;; killed.  Gnus stores no information on killed groups, so
9199         ;; there's nothing to be done.
9200         ;; One could store the information somewhere temporarily,
9201         ;; perhaps...  Hmmm...
9202         ()
9203       ;; Remove any negative articles numbers.
9204       (while (and unread (< (car unread) 0))
9205         (setq unread (cdr unread)))
9206       ;; Remove any expired article numbers
9207       (while (and unread (< (car unread) (car active)))
9208         (setq unread (cdr unread)))
9209       ;; Compute the ranges of read articles by looking at the list of
9210       ;; unread articles.
9211       (while unread
9212         (when (/= (car unread) prev)
9213           (push (if (= prev (1- (car unread))) prev
9214                   (cons prev (1- (car unread))))
9215                 read))
9216         (setq prev (1+ (car unread)))
9217         (setq unread (cdr unread)))
9218       (when (<= prev (cdr active))
9219         (push (cons prev (cdr active)) read))
9220       (setq read (if (> (length read) 1) (nreverse read) read))
9221       (if compute
9222           read
9223         (save-excursion
9224           (set-buffer gnus-group-buffer)
9225           (gnus-undo-register
9226             `(progn
9227                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9228                (gnus-info-set-read ',info ',(gnus-info-read info))
9229                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9230                (gnus-group-update-group ,group t))))
9231        ;; Propagate the read marks to the backend.
9232        (if (gnus-check-backend-function 'request-set-mark group)
9233            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9234                  (add (gnus-remove-from-range read (gnus-info-read info))))
9235              (when (or add del)
9236                (unless (gnus-check-group group)
9237                  (error "Can't open server for %s" group))
9238                (gnus-request-set-mark
9239                 group (delq nil (list (if add (list add 'add '(read)))
9240                                       (if del (list del 'del '(read)))))))))
9241         ;; Enter this list into the group info.
9242         (gnus-info-set-read info read)
9243         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9244         (gnus-get-unread-articles-in-group info (gnus-active group))
9245         t))))
9246
9247 (defun gnus-offer-save-summaries ()
9248   "Offer to save all active summary buffers."
9249   (save-excursion
9250     (let ((buflist (buffer-list))
9251           buffers bufname)
9252       ;; Go through all buffers and find all summaries.
9253       (while buflist
9254         (and (setq bufname (buffer-name (car buflist)))
9255              (string-match "Summary" bufname)
9256              (save-excursion
9257                (set-buffer bufname)
9258                ;; We check that this is, indeed, a summary buffer.
9259                (and (eq major-mode 'gnus-summary-mode)
9260                     ;; Also make sure this isn't bogus.
9261                     gnus-newsgroup-prepared
9262                     ;; Also make sure that this isn't a dead summary buffer.
9263                     (not gnus-dead-summary-mode)))
9264              (push bufname buffers))
9265         (setq buflist (cdr buflist)))
9266       ;; Go through all these summary buffers and offer to save them.
9267       (when buffers
9268         (map-y-or-n-p
9269          "Update summary buffer %s? "
9270          (lambda (buf)
9271            (switch-to-buffer buf)
9272            (gnus-summary-exit))
9273          buffers)))))
9274
9275
9276 ;;; @ for mime-partial
9277 ;;;
9278
9279 (defun gnus-request-partial-message ()
9280   (save-excursion
9281     (let ((number (gnus-summary-article-number))
9282           (group gnus-newsgroup-name)
9283           (mother gnus-article-buffer))
9284       (set-buffer (get-buffer-create " *Partial Article*"))
9285       (erase-buffer)
9286       (setq mime-preview-buffer mother)
9287       (gnus-request-article-this-buffer number group)
9288       (mime-parse-buffer)
9289       )))
9290
9291 (autoload 'mime-combine-message/partial-pieces-automatically
9292   "mime-partial"
9293   "Internal method to combine message/partial messages automatically.")
9294
9295 (mime-add-condition
9296  'action '((type . message)(subtype . partial)
9297            (major-mode . gnus-original-article-mode)
9298            (method . mime-combine-message/partial-pieces-automatically)
9299            (summary-buffer-exp . gnus-summary-buffer)
9300            (request-partial-message-method . gnus-request-partial-message)
9301            ))
9302
9303
9304 ;;; @ for message/rfc822
9305 ;;;
9306
9307 (defun gnus-mime-extract-message/rfc822 (entity situation)
9308   (let (group article num cwin swin cur)
9309     (with-current-buffer (mime-entity-buffer entity)
9310       (save-restriction
9311         (narrow-to-region (mime-entity-body-start entity)
9312                           (mime-entity-body-end entity))
9313         (setq group (or (cdr (assq 'group situation))
9314                         (completing-read "Group: "
9315                                          gnus-active-hashtb
9316                                          nil
9317                                          (gnus-read-active-file-p)
9318                                          gnus-newsgroup-name))
9319               article (gnus-request-accept-article group)
9320               )
9321         ))
9322     (when (and (consp article)
9323                (numberp (setq article (cdr article))))
9324       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9325             cwin (get-buffer-window (current-buffer) t)
9326             )
9327       (save-window-excursion
9328         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9329             (select-window swin)
9330           (set-buffer gnus-summary-buffer)
9331           )
9332         (setq cur gnus-current-article)
9333         (forward-line num)
9334         (let (gnus-show-threads)
9335           (gnus-summary-goto-subject article t)
9336           )
9337         (gnus-summary-clear-mark-forward 1)
9338         (gnus-summary-goto-subject cur)
9339         )
9340       (when (and cwin (window-frame cwin))
9341         (select-frame (window-frame cwin))
9342         )
9343       (when (boundp 'mime-acting-situation-to-override)
9344         (set-alist 'mime-acting-situation-to-override
9345                    'group
9346                    group)
9347         (set-alist 'mime-acting-situation-to-override
9348                    'after-method
9349                    `(progn
9350                       (save-current-buffer
9351                         (set-buffer gnus-group-buffer)
9352                         (gnus-activate-group ,group)
9353                         )
9354                       (gnus-summary-goto-article ,cur
9355                                                  gnus-show-all-headers)
9356                       ))
9357         (set-alist 'mime-acting-situation-to-override
9358                    'number num)
9359         )
9360       )))
9361
9362 (mime-add-condition
9363  'action '((type . message)(subtype . rfc822)
9364            (major-mode . gnus-original-article-mode)
9365            (method . gnus-mime-extract-message/rfc822)
9366            (mode . "extract")
9367            ))
9368
9369 (mime-add-condition
9370  'action '((type . message)(subtype . news)
9371            (major-mode . gnus-original-article-mode)
9372            (method . gnus-mime-extract-message/rfc822)
9373            (mode . "extract")
9374            ))
9375
9376 (defun gnus-mime-extract-multipart (entity situation)
9377   (let ((children (mime-entity-children entity))
9378         mime-acting-situation-to-override
9379         f)
9380     (while children
9381       (mime-play-entity (car children)
9382                         (cons (assq 'mode situation)
9383                               mime-acting-situation-to-override))
9384       (setq children (cdr children)))
9385     (if (setq f (cdr (assq 'after-method
9386                            mime-acting-situation-to-override)))
9387         (eval f)
9388       )))  
9389
9390 (mime-add-condition
9391  'action '((type . multipart)
9392            (method . gnus-mime-extract-multipart)
9393            (mode . "extract")
9394            )
9395  'with-default)
9396
9397
9398 ;;; @ end
9399 ;;;
9400
9401 (defun gnus-summary-setup-default-charset ()
9402   "Setup newsgroup default charset."
9403   (if (equal gnus-newsgroup-name "nndraft:drafts")
9404       (setq gnus-newsgroup-charset nil)
9405   (let* ((name (and gnus-newsgroup-name
9406                    (gnus-group-real-name gnus-newsgroup-name)))
9407          (ignored-charsets 
9408           (or gnus-newsgroup-ephemeral-ignored-charsets
9409               (append
9410                (and gnus-newsgroup-name
9411                     (or (gnus-group-find-parameter gnus-newsgroup-name
9412                                                    'ignored-charsets t)
9413                         (let ((alist gnus-group-ignored-charsets-alist)
9414                            elem (charsets nil))
9415                           (while (setq elem (pop alist))
9416                             (when (and name
9417                                        (string-match (car elem) name))
9418                               (setq alist nil
9419                                     charsets (cdr elem))))
9420                           charsets))))
9421               gnus-newsgroup-ignored-charsets)))
9422     (setq gnus-newsgroup-charset
9423           (or gnus-newsgroup-ephemeral-charset
9424               (and gnus-newsgroup-name
9425                    (or (gnus-group-find-parameter gnus-newsgroup-name
9426                                                   'charset)
9427                        (let ((alist gnus-group-charset-alist)
9428                              elem (charset nil))
9429                          (while (setq elem (pop alist))
9430                            (when (and name
9431                                       (string-match (car elem) name))
9432                              (setq alist nil
9433                                    charset (cadr elem))))
9434                          charset)))
9435               gnus-default-charset))
9436     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9437          ignored-charsets))))
9438
9439 ;;;
9440 ;;; Mime Commands
9441 ;;;
9442
9443 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9444   "Display the current article buffer fully MIME-buttonized.
9445 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9446 treated as multipart/mixed."
9447   (interactive "P")
9448   (require 'gnus-art)
9449   (let ((gnus-unbuttonized-mime-types nil)
9450         (gnus-mime-display-multipart-as-mixed show-all-parts))
9451     (gnus-summary-show-article)))
9452
9453 (defun gnus-summary-repair-multipart (article)
9454   "Add a Content-Type header to a multipart article without one."
9455   (interactive (list (gnus-summary-article-number)))
9456   (gnus-with-article article
9457     (message-narrow-to-head)
9458     (goto-char (point-max))
9459     (widen)
9460     (when (search-forward "\n--" nil t)
9461       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9462         (message-narrow-to-head)
9463         (message-remove-header "Mime-Version")
9464         (message-remove-header "Content-Type")
9465         (goto-char (point-max))
9466         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9467                         separator))
9468         (insert "Mime-Version: 1.0\n")
9469         (widen))))
9470   (let (gnus-mark-article-hook)
9471     (gnus-summary-select-article t t nil article)))
9472
9473 (defun gnus-summary-toggle-display-buttonized ()
9474   "Toggle the buttonizing of the article buffer."
9475   (interactive)
9476   (require 'gnus-art)
9477   (if (setq gnus-inhibit-mime-unbuttonizing
9478             (not gnus-inhibit-mime-unbuttonizing))
9479       (let ((gnus-unbuttonized-mime-types nil))
9480         (gnus-summary-show-article))
9481     (gnus-summary-show-article)))
9482
9483 ;;;
9484 ;;; with article
9485 ;;;
9486
9487 (defmacro gnus-with-article (article &rest forms)
9488   "Select ARTICLE and perform FORMS in the original article buffer.
9489 Then replace the article with the result."
9490   `(progn
9491      ;; We don't want the article to be marked as read.
9492      (let (gnus-mark-article-hook)
9493        (gnus-summary-select-article t t nil ,article))
9494      (set-buffer gnus-original-article-buffer)
9495      ,@forms
9496      (if (not (gnus-check-backend-function
9497                'request-replace-article (car gnus-article-current)))
9498          (gnus-message 5 "Read-only group; not replacing")
9499        (unless (gnus-request-replace-article
9500                 ,article (car gnus-article-current)
9501                 (current-buffer) t)
9502          (error "Couldn't replace article")))
9503      ;; The cache and backlog have to be flushed somewhat.
9504      (when gnus-keep-backlog
9505        (gnus-backlog-remove-article
9506         (car gnus-article-current) (cdr gnus-article-current)))
9507      (when gnus-use-cache
9508        (gnus-cache-update-article
9509         (car gnus-article-current) (cdr gnus-article-current)))))
9510
9511 (put 'gnus-with-article 'lisp-indent-function 1)
9512 (put 'gnus-with-article 'edebug-form-spec '(form body))
9513
9514 ;;;
9515 ;;; Generic summary marking commands
9516 ;;;
9517
9518 (defvar gnus-summary-marking-alist
9519   '((read gnus-del-mark "d")
9520     (unread gnus-unread-mark "u")
9521     (ticked gnus-ticked-mark "!")
9522     (dormant gnus-dormant-mark "?")
9523     (expirable gnus-expirable-mark "e"))
9524   "An alist of names/marks/keystrokes.")
9525
9526 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9527 (defvar gnus-summary-mark-map)
9528
9529 (defun gnus-summary-make-all-marking-commands ()
9530   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9531   (dolist (elem gnus-summary-marking-alist)
9532     (apply 'gnus-summary-make-marking-command elem)))
9533
9534 (defun gnus-summary-make-marking-command (name mark keystroke)
9535   (let ((map (make-sparse-keymap)))
9536     (define-key gnus-summary-generic-mark-map keystroke map)
9537     (dolist (lway `((next "next" next nil "n")
9538                     (next-unread "next unread" next t "N")
9539                     (prev "previous" prev nil "p")
9540                     (prev-unread "previous unread" prev t "P")
9541                     (nomove "" nil nil ,keystroke)))
9542       (let ((func (gnus-summary-make-marking-command-1
9543                    mark (car lway) lway name)))
9544         (setq func (eval func))
9545         (define-key map (nth 4 lway) func)))))
9546       
9547 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9548   `(defun ,(intern
9549             (format "gnus-summary-put-mark-as-%s%s"
9550                     name (if (eq way 'nomove)
9551                              ""
9552                            (concat "-" (symbol-name way)))))
9553      (n)
9554      ,(format
9555        "Mark the current article as %s%s.
9556 If N, the prefix, then repeat N times.
9557 If N is negative, move in reverse order.
9558 The difference between N and the actual number of articles marked is
9559 returned."
9560        name (cadr lway))
9561      (interactive "p")
9562      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9563     
9564 (defun gnus-summary-generic-mark (n mark move unread)
9565   "Mark N articles with MARK."
9566   (unless (eq major-mode 'gnus-summary-mode)
9567     (error "This command can only be used in the summary buffer"))
9568   (gnus-summary-show-thread)
9569   (let ((nummove
9570          (cond
9571           ((eq move 'next) 1)
9572           ((eq move 'prev) -1)
9573           (t 0))))
9574     (if (zerop nummove)
9575         (setq n 1)
9576       (when (< n 0)
9577         (setq n (abs n)
9578               nummove (* -1 nummove))))
9579     (while (and (> n 0)
9580                 (gnus-summary-mark-article nil mark)
9581                 (zerop (gnus-summary-next-subject nummove unread t)))
9582       (setq n (1- n)))
9583     (when (/= 0 n)
9584       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9585     (gnus-summary-recenter)
9586     (gnus-summary-position-point)
9587     (gnus-set-mode-line 'summary)
9588     n))
9589
9590 (gnus-summary-make-all-marking-commands)
9591
9592 (gnus-ems-redefine)
9593
9594 (provide 'gnus-sum)
9595
9596 (run-hooks 'gnus-sum-load-hook)
9597
9598 ;;; gnus-sum.el ends here