Sync up with Pterodactyl Gnus v0.93.
[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       ;; Do score processing.
2762       (when gnus-use-scoring
2763         (gnus-possibly-score-headers))
2764       ;; Check whether to fill in the gaps in the threads.
2765       (when gnus-build-sparse-threads
2766         (gnus-build-sparse-threads))
2767       ;; Find the initial limit.
2768       (if gnus-show-threads
2769           (if show-all
2770               (let ((gnus-newsgroup-dormant nil))
2771                 (gnus-summary-initial-limit show-all))
2772             (gnus-summary-initial-limit show-all))
2773         (setq gnus-newsgroup-limit
2774               (mapcar
2775                (lambda (header) (mail-header-number header))
2776                gnus-newsgroup-headers)))
2777       ;; Generate the summary buffer.
2778       (unless no-display
2779         (gnus-summary-prepare))
2780       (when gnus-use-trees
2781         (gnus-tree-open group)
2782         (setq gnus-summary-highlight-line-function
2783               'gnus-tree-highlight-article))
2784       ;; If the summary buffer is empty, but there are some low-scored
2785       ;; articles or some excluded dormants, we include these in the
2786       ;; buffer.
2787       (when (and (zerop (buffer-size))
2788                  (not no-display))
2789         (cond (gnus-newsgroup-dormant
2790                (gnus-summary-limit-include-dormant))
2791               ((and gnus-newsgroup-scored show-all)
2792                (gnus-summary-limit-include-expunged t))))
2793       ;; Function `gnus-apply-kill-file' must be called in this hook.
2794       (gnus-run-hooks 'gnus-apply-kill-hook)
2795       (if (and (zerop (buffer-size))
2796                (not no-display))
2797           (progn
2798             ;; This newsgroup is empty.
2799             (gnus-summary-catchup-and-exit nil t)
2800             (gnus-message 6 "No unread news")
2801             (when kill-buffer
2802               (gnus-kill-or-deaden-summary kill-buffer))
2803             ;; Return nil from this function.
2804             nil)
2805         ;; Hide conversation thread subtrees.  We cannot do this in
2806         ;; gnus-summary-prepare-hook since kill processing may not
2807         ;; work with hidden articles.
2808         (and gnus-show-threads
2809              gnus-thread-hide-subtree
2810              (gnus-summary-hide-all-threads))
2811         (when kill-buffer
2812           (gnus-kill-or-deaden-summary kill-buffer))
2813         ;; Show first unread article if requested.
2814         (if (and (not no-article)
2815                  (not no-display)
2816                  gnus-newsgroup-unreads
2817                  gnus-auto-select-first)
2818             (progn
2819               (gnus-configure-windows 'summary)
2820               (cond
2821                ((eq gnus-auto-select-first 'best)
2822                 (gnus-summary-best-unread-article))
2823                ((eq gnus-auto-select-first t)
2824                 (gnus-summary-first-unread-article))
2825                ((gnus-functionp gnus-auto-select-first)
2826                 (funcall gnus-auto-select-first))))
2827           ;; Don't select any articles, just move point to the first
2828           ;; article in the group.
2829           (goto-char (point-min))
2830           (gnus-summary-position-point)
2831           (gnus-configure-windows 'summary 'force)
2832           (gnus-set-mode-line 'summary))
2833         (when (get-buffer-window gnus-group-buffer t)
2834           ;; Gotta use windows, because recenter does weird stuff if
2835           ;; the current buffer ain't the displayed window.
2836           (let ((owin (selected-window)))
2837             (select-window (get-buffer-window gnus-group-buffer t))
2838             (when (gnus-group-goto-group group)
2839               (recenter))
2840             (select-window owin)))
2841         ;; Mark this buffer as "prepared".
2842         (setq gnus-newsgroup-prepared t)
2843         (gnus-run-hooks 'gnus-summary-prepared-hook)
2844         t)))))
2845
2846 (defun gnus-summary-prepare ()
2847   "Generate the summary buffer."
2848   (interactive)
2849   (let ((buffer-read-only nil))
2850     (erase-buffer)
2851     (setq gnus-newsgroup-data nil
2852           gnus-newsgroup-data-reverse nil)
2853     (gnus-run-hooks 'gnus-summary-generate-hook)
2854     ;; Generate the buffer, either with threads or without.
2855     (when gnus-newsgroup-headers
2856       (gnus-summary-prepare-threads
2857        (if gnus-show-threads
2858            (gnus-sort-gathered-threads
2859             (funcall gnus-summary-thread-gathering-function
2860                      (gnus-sort-threads
2861                       (gnus-cut-threads (gnus-make-threads)))))
2862          ;; Unthreaded display.
2863          (gnus-sort-articles gnus-newsgroup-headers))))
2864     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2865     ;; Call hooks for modifying summary buffer.
2866     (goto-char (point-min))
2867     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2868
2869 (defsubst gnus-general-simplify-subject (subject)
2870   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2871   (setq subject
2872         (cond
2873          ;; Truncate the subject.
2874          (gnus-simplify-subject-functions
2875           (gnus-map-function gnus-simplify-subject-functions subject))
2876          ((numberp gnus-summary-gather-subject-limit)
2877           (setq subject (gnus-simplify-subject-re subject))
2878           (if (> (length subject) gnus-summary-gather-subject-limit)
2879               (substring subject 0 gnus-summary-gather-subject-limit)
2880             subject))
2881          ;; Fuzzily simplify it.
2882          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2883           (gnus-simplify-subject-fuzzy subject))
2884          ;; Just remove the leading "Re:".
2885          (t
2886           (gnus-simplify-subject-re subject))))
2887
2888   (if (and gnus-summary-gather-exclude-subject
2889            (string-match gnus-summary-gather-exclude-subject subject))
2890       nil                               ; This article shouldn't be gathered
2891     subject))
2892
2893 (defun gnus-summary-simplify-subject-query ()
2894   "Query where the respool algorithm would put this article."
2895   (interactive)
2896   (gnus-summary-select-article)
2897   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2898
2899 (defun gnus-gather-threads-by-subject (threads)
2900   "Gather threads by looking at Subject headers."
2901   (if (not gnus-summary-make-false-root)
2902       threads
2903     (let ((hashtb (gnus-make-hashtable 1024))
2904           (prev threads)
2905           (result threads)
2906           subject hthread whole-subject)
2907       (while threads
2908         (setq subject (gnus-general-simplify-subject
2909                        (setq whole-subject (mail-header-subject
2910                                             (caar threads)))))
2911         (when subject
2912           (if (setq hthread (gnus-gethash subject hashtb))
2913               (progn
2914                 ;; We enter a dummy root into the thread, if we
2915                 ;; haven't done that already.
2916                 (unless (stringp (caar hthread))
2917                   (setcar hthread (list whole-subject (car hthread))))
2918                 ;; We add this new gathered thread to this gathered
2919                 ;; thread.
2920                 (setcdr (car hthread)
2921                         (nconc (cdar hthread) (list (car threads))))
2922                 ;; Remove it from the list of threads.
2923                 (setcdr prev (cdr threads))
2924                 (setq threads prev))
2925             ;; Enter this thread into the hash table.
2926             (gnus-sethash subject threads hashtb)))
2927         (setq prev threads)
2928         (setq threads (cdr threads)))
2929       result)))
2930
2931 (defun gnus-gather-threads-by-references (threads)
2932   "Gather threads by looking at References headers."
2933   (let ((idhashtb (gnus-make-hashtable 1024))
2934         (thhashtb (gnus-make-hashtable 1024))
2935         (prev threads)
2936         (result threads)
2937         ids references id gthread gid entered ref)
2938     (while threads
2939       (when (setq references (mail-header-references (caar threads)))
2940         (setq id (mail-header-id (caar threads))
2941               ids (gnus-split-references references)
2942               entered nil)
2943         (while (setq ref (pop ids))
2944           (setq ids (delete ref ids))
2945           (if (not (setq gid (gnus-gethash ref idhashtb)))
2946               (progn
2947                 (gnus-sethash ref id idhashtb)
2948                 (gnus-sethash id threads thhashtb))
2949             (setq gthread (gnus-gethash gid thhashtb))
2950             (unless entered
2951               ;; We enter a dummy root into the thread, if we
2952               ;; haven't done that already.
2953               (unless (stringp (caar gthread))
2954                 (setcar gthread (list (mail-header-subject (caar gthread))
2955                                       (car gthread))))
2956               ;; We add this new gathered thread to this gathered
2957               ;; thread.
2958               (setcdr (car gthread)
2959                       (nconc (cdar gthread) (list (car threads)))))
2960             ;; Add it into the thread hash table.
2961             (gnus-sethash id gthread thhashtb)
2962             (setq entered t)
2963             ;; Remove it from the list of threads.
2964             (setcdr prev (cdr threads))
2965             (setq threads prev))))
2966       (setq prev threads)
2967       (setq threads (cdr threads)))
2968     result))
2969
2970 (defun gnus-sort-gathered-threads (threads)
2971   "Sort subtreads inside each gathered thread by article number."
2972   (let ((result threads))
2973     (while threads
2974       (when (stringp (caar threads))
2975         (setcdr (car threads)
2976                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2977       (setq threads (cdr threads)))
2978     result))
2979
2980 (defun gnus-thread-loop-p (root thread)
2981   "Say whether ROOT is in THREAD."
2982   (let ((stack (list thread))
2983         (infloop 0)
2984         th)
2985     (while (setq thread (pop stack))
2986       (setq th (cdr thread))
2987       (while (and th
2988                   (not (eq (caar th) root)))
2989         (pop th))
2990       (if th
2991           ;; We have found a loop.
2992           (let (ref-dep)
2993             (setcdr thread (delq (car th) (cdr thread)))
2994             (if (boundp (setq ref-dep (intern "none"
2995                                               gnus-newsgroup-dependencies)))
2996                 (setcdr (symbol-value ref-dep)
2997                         (nconc (cdr (symbol-value ref-dep))
2998                                (list (car th))))
2999               (set ref-dep (list nil (car th))))
3000             (setq infloop 1
3001                   stack nil))
3002         ;; Push all the subthreads onto the stack.
3003         (push (cdr thread) stack)))
3004     infloop))
3005
3006 (defun gnus-make-threads ()
3007   "Go through the dependency hashtb and find the roots.  Return all threads."
3008   (let (threads)
3009     (while (catch 'infloop
3010              (mapatoms
3011               (lambda (refs)
3012                 ;; Deal with self-referencing References loops.
3013                 (when (and (car (symbol-value refs))
3014                            (not (zerop
3015                                  (apply
3016                                   '+
3017                                   (mapcar
3018                                    (lambda (thread)
3019                                      (gnus-thread-loop-p
3020                                       (car (symbol-value refs)) thread))
3021                                    (cdr (symbol-value refs)))))))
3022                   (setq threads nil)
3023                   (throw 'infloop t))
3024                 (unless (car (symbol-value refs))
3025                   ;; These threads do not refer back to any other articles,
3026                   ;; so they're roots.
3027                   (setq threads (append (cdr (symbol-value refs)) threads))))
3028               gnus-newsgroup-dependencies)))
3029     threads))
3030
3031 ;; Build the thread tree.
3032 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3033   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3034
3035 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3036 if it was already present.
3037
3038 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3039 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3040 Message-IDs will be renamed be renamed to a unique Message-ID before
3041 being entered.
3042
3043 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3044   (let* ((id (mail-header-id header))
3045          (id-dep (and id (intern id dependencies)))
3046          ref ref-dep ref-header)
3047     ;; Enter this `header' in the `dependencies' table.
3048     (cond
3049      ((not id-dep)
3050       (setq header nil))
3051      ;; The first two cases do the normal part: enter a new `header'
3052      ;; in the `dependencies' table.
3053      ((not (boundp id-dep))
3054       (set id-dep (list header)))
3055      ((null (car (symbol-value id-dep)))
3056       (setcar (symbol-value id-dep) header))
3057
3058      ;; From here the `header' was already present in the
3059      ;; `dependencies' table.
3060      (force-new
3061       ;; Overrides an existing entry;
3062       ;; just set the header part of the entry.
3063       (setcar (symbol-value id-dep) header))
3064
3065      ;; Renames the existing `header' to a unique Message-ID.
3066      ((not gnus-summary-ignore-duplicates)
3067       ;; An article with this Message-ID has already been seen.
3068       ;; We rename the Message-ID.
3069       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3070            (list header))
3071       (mail-header-set-id header id))
3072
3073      ;; The last case ignores an existing entry, except it adds any
3074      ;; additional Xrefs (in case the two articles came from different
3075      ;; servers.
3076      ;; Also sets `header' to `nil' meaning that the `dependencies'
3077      ;; table was *not* modified.
3078      (t
3079       (mail-header-set-xref
3080        (car (symbol-value id-dep))
3081        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3082                    "")
3083                (or (mail-header-xref header) "")))
3084       (setq header nil)))
3085
3086     (when header
3087       ;; First check if that we are not creating a References loop.
3088       (setq ref (gnus-parent-id (mail-header-references header)))
3089       (while (and ref
3090                   (setq ref-dep (intern-soft ref dependencies))
3091                   (boundp ref-dep)
3092                   (setq ref-header (car (symbol-value ref-dep))))
3093         (if (string= id ref)
3094             ;; Yuk!  This is a reference loop.  Make the article be a
3095             ;; root article.
3096             (progn
3097               (mail-header-set-references (car (symbol-value id-dep)) "none")
3098               (setq ref nil))
3099           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3100       (setq ref (gnus-parent-id (mail-header-references header)))
3101       (setq ref-dep (intern (or ref "none") dependencies))
3102       (if (boundp ref-dep)
3103           (setcdr (symbol-value ref-dep)
3104                   (nconc (cdr (symbol-value ref-dep))
3105                          (list (symbol-value id-dep))))
3106         (set ref-dep (list nil (symbol-value id-dep)))))
3107     header))
3108
3109 (defun gnus-build-sparse-threads ()
3110   (let ((headers gnus-newsgroup-headers)
3111         (gnus-summary-ignore-duplicates t)
3112         header references generation relations
3113         subject child end new-child date)
3114     ;; First we create an alist of generations/relations, where
3115     ;; generations is how much we trust the relation, and the relation
3116     ;; is parent/child.
3117     (gnus-message 7 "Making sparse threads...")
3118     (save-excursion
3119       (nnheader-set-temp-buffer " *gnus sparse threads*")
3120       (while (setq header (pop headers))
3121         (when (and (setq references (mail-header-references header))
3122                    (not (string= references "")))
3123           (insert references)
3124           (setq child (mail-header-id header)
3125                 subject (mail-header-subject header)
3126                 date (mail-header-date header)
3127                 generation 0)
3128           (while (search-backward ">" nil t)
3129             (setq end (1+ (point)))
3130             (when (search-backward "<" nil t)
3131               (setq new-child (buffer-substring (point) end))
3132               (push (list (incf generation)
3133                           child (setq child new-child)
3134                           subject date)
3135                     relations)))
3136           (when child
3137             (push (list (1+ generation) child nil subject) relations))
3138           (erase-buffer)))
3139       (kill-buffer (current-buffer)))
3140     ;; Sort over trustworthiness.
3141     (mapcar
3142      (lambda (relation)
3143        (when (gnus-dependencies-add-header
3144               (make-full-mail-header
3145                gnus-reffed-article-number
3146                (nth 3 relation) "" (or (nth 4 relation) "")
3147                (nth 1 relation)
3148                (or (nth 2 relation) "") 0 0 "")
3149               gnus-newsgroup-dependencies nil)
3150          (push gnus-reffed-article-number gnus-newsgroup-limit)
3151          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3152          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3153                gnus-newsgroup-reads)
3154          (decf gnus-reffed-article-number)))
3155      (sort relations 'car-less-than-car))
3156     (gnus-message 7 "Making sparse threads...done")))
3157
3158 (defun gnus-build-old-threads ()
3159   ;; Look at all the articles that refer back to old articles, and
3160   ;; fetch the headers for the articles that aren't there.  This will
3161   ;; build complete threads - if the roots haven't been expired by the
3162   ;; server, that is.
3163   (let (id heads)
3164     (mapatoms
3165      (lambda (refs)
3166        (when (not (car (symbol-value refs)))
3167          (setq heads (cdr (symbol-value refs)))
3168          (while heads
3169            (if (memq (mail-header-number (caar heads))
3170                      gnus-newsgroup-dormant)
3171                (setq heads (cdr heads))
3172              (setq id (symbol-name refs))
3173              (while (and (setq id (gnus-build-get-header id))
3174                          (not (car (gnus-id-to-thread id)))))
3175              (setq heads nil)))))
3176      gnus-newsgroup-dependencies)))
3177
3178 ;; This function has to be called with point after the article number
3179 ;; on the beginning of the line.
3180 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3181   (let ((eol (gnus-point-at-eol))
3182         (buffer (current-buffer))
3183         header)
3184
3185     ;; overview: [num subject from date id refs chars lines misc]
3186     (unless (eobp)
3187       (forward-char))
3188
3189     (setq header
3190           (make-full-mail-header
3191            number                               ; number
3192            (nnheader-nov-field)                 ; subject
3193            (nnheader-nov-field)                 ; from
3194            (nnheader-nov-field)                 ; date
3195            (nnheader-nov-read-message-id)       ; id
3196            (nnheader-nov-field)                 ; refs
3197            (nnheader-nov-read-integer)          ; chars
3198            (nnheader-nov-read-integer)          ; lines
3199            (unless (eobp)
3200              (nnheader-nov-field))              ; misc
3201            (nnheader-nov-parse-extra)))         ; extra
3202
3203     (when gnus-alter-header-function
3204       (funcall gnus-alter-header-function header))
3205     (gnus-dependencies-add-header header dependencies force-new)))
3206
3207 (defun gnus-build-get-header (id)
3208   ;; Look through the buffer of NOV lines and find the header to
3209   ;; ID.  Enter this line into the dependencies hash table, and return
3210   ;; the id of the parent article (if any).
3211   (let ((deps gnus-newsgroup-dependencies)
3212         found header)
3213     (prog1
3214         (save-excursion
3215           (set-buffer nntp-server-buffer)
3216           (let ((case-fold-search nil))
3217             (goto-char (point-min))
3218             (while (and (not found)
3219                         (search-forward id nil t))
3220               (beginning-of-line)
3221               (setq found (looking-at
3222                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3223                                    (regexp-quote id))))
3224               (or found (beginning-of-line 2)))
3225             (when found
3226               (beginning-of-line)
3227               (and
3228                (setq header (gnus-nov-parse-line
3229                              (read (current-buffer)) deps))
3230                (gnus-parent-id (mail-header-references header))))))
3231       (when header
3232         (let ((number (mail-header-number header)))
3233           (push number gnus-newsgroup-limit)
3234           (push header gnus-newsgroup-headers)
3235           (if (memq number gnus-newsgroup-unselected)
3236               (progn
3237                 (push number gnus-newsgroup-unreads)
3238                 (setq gnus-newsgroup-unselected
3239                       (delq number gnus-newsgroup-unselected)))
3240             (push number gnus-newsgroup-ancient)))))))
3241
3242 (defun gnus-build-all-threads ()
3243   "Read all the headers."
3244   (let ((gnus-summary-ignore-duplicates t)
3245         (dependencies gnus-newsgroup-dependencies)
3246         header article)
3247     (save-excursion
3248       (set-buffer nntp-server-buffer)
3249       (let ((case-fold-search nil))
3250         (goto-char (point-min))
3251         (while (not (eobp))
3252           (ignore-errors
3253             (setq article (read (current-buffer))
3254                   header (gnus-nov-parse-line
3255                           article dependencies)))
3256           (when header
3257             (save-excursion
3258               (set-buffer gnus-summary-buffer)
3259               (push header gnus-newsgroup-headers)
3260               (if (memq (setq article (mail-header-number header))
3261                         gnus-newsgroup-unselected)
3262                   (progn
3263                     (push article gnus-newsgroup-unreads)
3264                     (setq gnus-newsgroup-unselected
3265                           (delq article gnus-newsgroup-unselected)))
3266                 (push article gnus-newsgroup-ancient)))
3267             (forward-line 1)))))))
3268
3269 (defun gnus-summary-update-article-line (article header)
3270   "Update the line for ARTICLE using HEADERS."
3271   (let* ((id (mail-header-id header))
3272          (thread (gnus-id-to-thread id)))
3273     (unless thread
3274       (error "Article in no thread"))
3275     ;; Update the thread.
3276     (setcar thread header)
3277     (gnus-summary-goto-subject article)
3278     (let* ((datal (gnus-data-find-list article))
3279            (data (car datal))
3280            (length (when (cdr datal)
3281                      (- (gnus-data-pos data)
3282                         (gnus-data-pos (cadr datal)))))
3283            (buffer-read-only nil)
3284            (level (gnus-summary-thread-level)))
3285       (gnus-delete-line)
3286       (gnus-summary-insert-line
3287        header level nil (gnus-article-mark article)
3288        (memq article gnus-newsgroup-replied)
3289        (memq article gnus-newsgroup-expirable)
3290        ;; Only insert the Subject string when it's different
3291        ;; from the previous Subject string.
3292        (if (gnus-subject-equal
3293             (condition-case ()
3294                 (mail-header-subject
3295                  (gnus-data-header
3296                   (cadr
3297                    (gnus-data-find-list
3298                     article
3299                     (gnus-data-list t)))))
3300               ;; Error on the side of excessive subjects.
3301               (error ""))
3302             (mail-header-subject header))
3303            ""
3304          (mail-header-subject header))
3305        nil (cdr (assq article gnus-newsgroup-scored))
3306        (memq article gnus-newsgroup-processable))
3307       (when length
3308         (gnus-data-update-list
3309          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3310
3311 (defun gnus-summary-update-article (article &optional iheader)
3312   "Update ARTICLE in the summary buffer."
3313   (set-buffer gnus-summary-buffer)
3314   (let* ((header (gnus-summary-article-header article))
3315          (id (mail-header-id header))
3316          (data (gnus-data-find article))
3317          (thread (gnus-id-to-thread id))
3318          (references (mail-header-references header))
3319          (parent
3320           (gnus-id-to-thread
3321            (or (gnus-parent-id
3322                 (when (and references
3323                            (not (equal "" references)))
3324                   references))
3325                "none")))
3326          (buffer-read-only nil)
3327          (old (car thread)))
3328     (when thread
3329       (unless iheader
3330         (setcar thread nil)
3331         (when parent
3332           (delq thread parent)))
3333       (if (gnus-summary-insert-subject id header)
3334           ;; Set the (possibly) new article number in the data structure.
3335           (gnus-data-set-number data (gnus-id-to-article id))
3336         (setcar thread old)
3337         nil))))
3338
3339 (defun gnus-rebuild-thread (id &optional line)
3340   "Rebuild the thread containing ID.
3341 If LINE, insert the rebuilt thread starting on line LINE."
3342   (let ((buffer-read-only nil)
3343         old-pos current thread data)
3344     (if (not gnus-show-threads)
3345         (setq thread (list (car (gnus-id-to-thread id))))
3346       ;; Get the thread this article is part of.
3347       (setq thread (gnus-remove-thread id)))
3348     (setq old-pos (gnus-point-at-bol))
3349     (setq current (save-excursion
3350                     (and (zerop (forward-line -1))
3351                          (gnus-summary-article-number))))
3352     ;; If this is a gathered thread, we have to go some re-gathering.
3353     (when (stringp (car thread))
3354       (let ((subject (car thread))
3355             roots thr)
3356         (setq thread (cdr thread))
3357         (while thread
3358           (unless (memq (setq thr (gnus-id-to-thread
3359                                    (gnus-root-id
3360                                     (mail-header-id (caar thread)))))
3361                         roots)
3362             (push thr roots))
3363           (setq thread (cdr thread)))
3364         ;; We now have all (unique) roots.
3365         (if (= (length roots) 1)
3366             ;; All the loose roots are now one solid root.
3367             (setq thread (car roots))
3368           (setq thread (cons subject (gnus-sort-threads roots))))))
3369     (let (threads)
3370       ;; We then insert this thread into the summary buffer.
3371       (when line
3372         (goto-char (point-min))
3373         (forward-line (1- line)))
3374       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3375         (if gnus-show-threads
3376             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3377           (gnus-summary-prepare-unthreaded thread))
3378         (setq data (nreverse gnus-newsgroup-data))
3379         (setq threads gnus-newsgroup-threads))
3380       ;; We splice the new data into the data structure.
3381       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3382       ;;!!! then we want to insert at the beginning of the buffer.
3383       ;;!!! That happens to be true with Gnus now, but that may
3384       ;;!!! change in the future.  Perhaps.
3385       (gnus-data-enter-list
3386        (if line nil current) data (- (point) old-pos))
3387       (setq gnus-newsgroup-threads
3388             (nconc threads gnus-newsgroup-threads))
3389       (gnus-data-compute-positions))))
3390
3391 (defun gnus-number-to-header (number)
3392   "Return the header for article NUMBER."
3393   (let ((headers gnus-newsgroup-headers))
3394     (while (and headers
3395                 (not (= number (mail-header-number (car headers)))))
3396       (pop headers))
3397     (when headers
3398       (car headers))))
3399
3400 (defun gnus-parent-headers (in-headers &optional generation)
3401   "Return the headers of the GENERATIONeth parent of HEADERS."
3402   (unless generation
3403     (setq generation 1))
3404   (let ((parent t)
3405         (headers in-headers)
3406         references)
3407     (while (and parent
3408                 (not (zerop generation))
3409                 (setq references (mail-header-references headers)))
3410       (setq headers (if (and references
3411                              (setq parent (gnus-parent-id references)))
3412                         (car (gnus-id-to-thread parent))
3413                       nil))
3414       (decf generation))
3415     (and (not (eq headers in-headers))
3416          headers)))
3417
3418 (defun gnus-id-to-thread (id)
3419   "Return the (sub-)thread where ID appears."
3420   (gnus-gethash id gnus-newsgroup-dependencies))
3421
3422 (defun gnus-id-to-article (id)
3423   "Return the article number of ID."
3424   (let ((thread (gnus-id-to-thread id)))
3425     (when (and thread
3426                (car thread))
3427       (mail-header-number (car thread)))))
3428
3429 (defun gnus-id-to-header (id)
3430   "Return the article headers of ID."
3431   (car (gnus-id-to-thread id)))
3432
3433 (defun gnus-article-displayed-root-p (article)
3434   "Say whether ARTICLE is a root(ish) article."
3435   (let ((level (gnus-summary-thread-level article))
3436         (refs (mail-header-references  (gnus-summary-article-header article)))
3437         particle)
3438     (cond
3439      ((null level) nil)
3440      ((zerop level) t)
3441      ((null refs) t)
3442      ((null (gnus-parent-id refs)) t)
3443      ((and (= 1 level)
3444            (null (setq particle (gnus-id-to-article
3445                                  (gnus-parent-id refs))))
3446            (null (gnus-summary-thread-level particle)))))))
3447
3448 (defun gnus-root-id (id)
3449   "Return the id of the root of the thread where ID appears."
3450   (let (last-id prev)
3451     (while (and id (setq prev (car (gnus-id-to-thread id))))
3452       (setq last-id id
3453             id (gnus-parent-id (mail-header-references prev))))
3454     last-id))
3455
3456 (defun gnus-articles-in-thread (thread)
3457   "Return the list of articles in THREAD."
3458   (cons (mail-header-number (car thread))
3459         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3460
3461 (defun gnus-remove-thread (id &optional dont-remove)
3462   "Remove the thread that has ID in it."
3463   (let (headers thread last-id)
3464     ;; First go up in this thread until we find the root.
3465     (setq last-id (gnus-root-id id)
3466           headers (message-flatten-list (gnus-id-to-thread last-id)))
3467     ;; We have now found the real root of this thread.  It might have
3468     ;; been gathered into some loose thread, so we have to search
3469     ;; through the threads to find the thread we wanted.
3470     (let ((threads gnus-newsgroup-threads)
3471           sub)
3472       (while threads
3473         (setq sub (car threads))
3474         (if (stringp (car sub))
3475             ;; This is a gathered thread, so we look at the roots
3476             ;; below it to find whether this article is in this
3477             ;; gathered root.
3478             (progn
3479               (setq sub (cdr sub))
3480               (while sub
3481                 (when (member (caar sub) headers)
3482                   (setq thread (car threads)
3483                         threads nil
3484                         sub nil))
3485                 (setq sub (cdr sub))))
3486           ;; It's an ordinary thread, so we check it.
3487           (when (eq (car sub) (car headers))
3488             (setq thread sub
3489                   threads nil)))
3490         (setq threads (cdr threads)))
3491       ;; If this article is in no thread, then it's a root.
3492       (if thread
3493           (unless dont-remove
3494             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3495         (setq thread (gnus-id-to-thread last-id)))
3496       (when thread
3497         (prog1
3498             thread                      ; We return this thread.
3499           (unless dont-remove
3500             (if (stringp (car thread))
3501                 (progn
3502                   ;; If we use dummy roots, then we have to remove the
3503                   ;; dummy root as well.
3504                   (when (eq gnus-summary-make-false-root 'dummy)
3505                     ;; We go to the dummy root by going to
3506                     ;; the first sub-"thread", and then one line up.
3507                     (gnus-summary-goto-article
3508                      (mail-header-number (caadr thread)))
3509                     (forward-line -1)
3510                     (gnus-delete-line)
3511                     (gnus-data-compute-positions))
3512                   (setq thread (cdr thread))
3513                   (while thread
3514                     (gnus-remove-thread-1 (car thread))
3515                     (setq thread (cdr thread))))
3516               (gnus-summary-show-all-threads)
3517               (gnus-remove-thread-1 thread))))))))
3518
3519 (defun gnus-remove-thread-1 (thread)
3520   "Remove the thread THREAD recursively."
3521   (let ((number (mail-header-number (pop thread)))
3522         d)
3523     (setq thread (reverse thread))
3524     (while thread
3525       (gnus-remove-thread-1 (pop thread)))
3526     (when (setq d (gnus-data-find number))
3527       (goto-char (gnus-data-pos d))
3528       (gnus-data-remove
3529        number
3530        (- (gnus-point-at-bol)
3531           (prog1
3532               (1+ (gnus-point-at-eol))
3533             (gnus-delete-line)))))))
3534
3535 (defun gnus-sort-threads (threads)
3536   "Sort THREADS."
3537   (if (not gnus-thread-sort-functions)
3538       threads
3539     (gnus-message 8 "Sorting threads...")
3540     (prog1
3541         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3542       (gnus-message 8 "Sorting threads...done"))))
3543
3544 (defun gnus-sort-articles (articles)
3545   "Sort ARTICLES."
3546   (when gnus-article-sort-functions
3547     (gnus-message 7 "Sorting articles...")
3548     (prog1
3549         (setq gnus-newsgroup-headers
3550               (sort articles (gnus-make-sort-function
3551                               gnus-article-sort-functions)))
3552       (gnus-message 7 "Sorting articles...done"))))
3553
3554 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3555 (defmacro gnus-thread-header (thread)
3556   ;; Return header of first article in THREAD.
3557   ;; Note that THREAD must never, ever be anything else than a variable -
3558   ;; using some other form will lead to serious barfage.
3559   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3560   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3561   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3562         (vector thread) 2))
3563
3564 (defsubst gnus-article-sort-by-number (h1 h2)
3565   "Sort articles by article number."
3566   (< (mail-header-number h1)
3567      (mail-header-number h2)))
3568
3569 (defun gnus-thread-sort-by-number (h1 h2)
3570   "Sort threads by root article number."
3571   (gnus-article-sort-by-number
3572    (gnus-thread-header h1) (gnus-thread-header h2)))
3573
3574 (defsubst gnus-article-sort-by-lines (h1 h2)
3575   "Sort articles by article Lines header."
3576   (< (mail-header-lines h1)
3577      (mail-header-lines h2)))
3578
3579 (defun gnus-thread-sort-by-lines (h1 h2)
3580   "Sort threads by root article Lines header."
3581   (gnus-article-sort-by-lines
3582    (gnus-thread-header h1) (gnus-thread-header h2)))
3583
3584 (defsubst gnus-article-sort-by-chars (h1 h2)
3585   "Sort articles by octet length."
3586   (< (mail-header-chars h1)
3587      (mail-header-chars h2)))
3588
3589 (defun gnus-thread-sort-by-chars (h1 h2)
3590   "Sort threads by root article octet length."
3591   (gnus-article-sort-by-chars
3592    (gnus-thread-header h1) (gnus-thread-header h2)))
3593
3594 (defsubst gnus-article-sort-by-author (h1 h2)
3595   "Sort articles by root author."
3596   (string-lessp
3597    (let ((addr (car (mime-read-field 'From h1))))
3598      (or (std11-full-name-string addr)
3599          (std11-address-string addr)
3600          ""))
3601    (let ((addr (car (mime-read-field 'From h2))))
3602      (or (std11-full-name-string addr)
3603          (std11-address-string addr)
3604          ""))
3605    ))
3606
3607 (defun gnus-thread-sort-by-author (h1 h2)
3608   "Sort threads by root author."
3609   (gnus-article-sort-by-author
3610    (gnus-thread-header h1)  (gnus-thread-header h2)))
3611
3612 (defsubst gnus-article-sort-by-subject (h1 h2)
3613   "Sort articles by root subject."
3614   (string-lessp
3615    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3616    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3617
3618 (defun gnus-thread-sort-by-subject (h1 h2)
3619   "Sort threads by root subject."
3620   (gnus-article-sort-by-subject
3621    (gnus-thread-header h1) (gnus-thread-header h2)))
3622
3623 (defsubst gnus-article-sort-by-date (h1 h2)
3624   "Sort articles by root article date."
3625   (time-less-p
3626    (gnus-date-get-time (mail-header-date h1))
3627    (gnus-date-get-time (mail-header-date h2))))
3628
3629 (defun gnus-thread-sort-by-date (h1 h2)
3630   "Sort threads by root article date."
3631   (gnus-article-sort-by-date
3632    (gnus-thread-header h1) (gnus-thread-header h2)))
3633
3634 (defsubst gnus-article-sort-by-score (h1 h2)
3635   "Sort articles by root article score.
3636 Unscored articles will be counted as having a score of zero."
3637   (> (or (cdr (assq (mail-header-number h1)
3638                     gnus-newsgroup-scored))
3639          gnus-summary-default-score 0)
3640      (or (cdr (assq (mail-header-number h2)
3641                     gnus-newsgroup-scored))
3642          gnus-summary-default-score 0)))
3643
3644 (defun gnus-thread-sort-by-score (h1 h2)
3645   "Sort threads by root article score."
3646   (gnus-article-sort-by-score
3647    (gnus-thread-header h1) (gnus-thread-header h2)))
3648
3649 (defun gnus-thread-sort-by-total-score (h1 h2)
3650   "Sort threads by the sum of all scores in the thread.
3651 Unscored articles will be counted as having a score of zero."
3652   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3653
3654 (defun gnus-thread-total-score (thread)
3655   ;; This function find the total score of THREAD.
3656   (cond ((null thread)
3657          0)
3658         ((consp thread)
3659          (if (stringp (car thread))
3660              (apply gnus-thread-score-function 0
3661                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3662            (gnus-thread-total-score-1 thread)))
3663         (t
3664          (gnus-thread-total-score-1 (list thread)))))
3665
3666 (defun gnus-thread-total-score-1 (root)
3667   ;; This function find the total score of the thread below ROOT.
3668   (setq root (car root))
3669   (apply gnus-thread-score-function
3670          (or (append
3671               (mapcar 'gnus-thread-total-score
3672                       (cdr (gnus-id-to-thread (mail-header-id root))))
3673               (when (> (mail-header-number root) 0)
3674                 (list (or (cdr (assq (mail-header-number root)
3675                                      gnus-newsgroup-scored))
3676                           gnus-summary-default-score 0))))
3677              (list gnus-summary-default-score)
3678              '(0))))
3679
3680 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3681 (defvar gnus-tmp-prev-subject nil)
3682 (defvar gnus-tmp-false-parent nil)
3683 (defvar gnus-tmp-root-expunged nil)
3684 (defvar gnus-tmp-dummy-line nil)
3685
3686 (defvar gnus-tmp-header)
3687 (defun gnus-extra-header (type &optional header)
3688   "Return the extra header of TYPE."
3689   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3690       ""))
3691
3692 (defun gnus-summary-prepare-threads (threads)
3693   "Prepare summary buffer from THREADS and indentation LEVEL.
3694 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3695 or a straight list of headers."
3696   (gnus-message 7 "Generating summary...")
3697
3698   (setq gnus-newsgroup-threads threads)
3699   (beginning-of-line)
3700
3701   (let ((gnus-tmp-level 0)
3702         (default-score (or gnus-summary-default-score 0))
3703         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3704         thread number subject stack state gnus-tmp-gathered beg-match
3705         new-roots gnus-tmp-new-adopts thread-end
3706         gnus-tmp-header gnus-tmp-unread
3707         gnus-tmp-replied gnus-tmp-subject-or-nil
3708         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3709         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3710         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3711
3712     (setq gnus-tmp-prev-subject nil)
3713
3714     (if (vectorp (car threads))
3715         ;; If this is a straight (sic) list of headers, then a
3716         ;; threaded summary display isn't required, so we just create
3717         ;; an unthreaded one.
3718         (gnus-summary-prepare-unthreaded threads)
3719
3720       ;; Do the threaded display.
3721
3722       (while (or threads stack gnus-tmp-new-adopts new-roots)
3723
3724         (if (and (= gnus-tmp-level 0)
3725                  (or (not stack)
3726                      (= (caar stack) 0))
3727                  (not gnus-tmp-false-parent)
3728                  (or gnus-tmp-new-adopts new-roots))
3729             (if gnus-tmp-new-adopts
3730                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3731                       thread (list (car gnus-tmp-new-adopts))
3732                       gnus-tmp-header (caar thread)
3733                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3734               (when new-roots
3735                 (setq thread (list (car new-roots))
3736                       gnus-tmp-header (caar thread)
3737                       new-roots (cdr new-roots))))
3738
3739           (if threads
3740               ;; If there are some threads, we do them before the
3741               ;; threads on the stack.
3742               (setq thread threads
3743                     gnus-tmp-header (caar thread))
3744             ;; There were no current threads, so we pop something off
3745             ;; the stack.
3746             (setq state (car stack)
3747                   gnus-tmp-level (car state)
3748                   thread (cdr state)
3749                   stack (cdr stack)
3750                   gnus-tmp-header (caar thread))))
3751
3752         (setq gnus-tmp-false-parent nil)
3753         (setq gnus-tmp-root-expunged nil)
3754         (setq thread-end nil)
3755
3756         (if (stringp gnus-tmp-header)
3757             ;; The header is a dummy root.
3758             (cond
3759              ((eq gnus-summary-make-false-root 'adopt)
3760               ;; We let the first article adopt the rest.
3761               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3762                                                (cddar thread)))
3763               (setq gnus-tmp-gathered
3764                     (nconc (mapcar
3765                             (lambda (h) (mail-header-number (car h)))
3766                             (cddar thread))
3767                            gnus-tmp-gathered))
3768               (setq thread (cons (list (caar thread)
3769                                        (cadar thread))
3770                                  (cdr thread)))
3771               (setq gnus-tmp-level -1
3772                     gnus-tmp-false-parent t))
3773              ((eq gnus-summary-make-false-root 'empty)
3774               ;; We print adopted articles with empty subject fields.
3775               (setq gnus-tmp-gathered
3776                     (nconc (mapcar
3777                             (lambda (h) (mail-header-number (car h)))
3778                             (cddar thread))
3779                            gnus-tmp-gathered))
3780               (setq gnus-tmp-level -1))
3781              ((eq gnus-summary-make-false-root 'dummy)
3782               ;; We remember that we probably want to output a dummy
3783               ;; root.
3784               (setq gnus-tmp-dummy-line gnus-tmp-header)
3785               (setq gnus-tmp-prev-subject gnus-tmp-header))
3786              (t
3787               ;; We do not make a root for the gathered
3788               ;; sub-threads at all.
3789               (setq gnus-tmp-level -1)))
3790
3791           (setq number (mail-header-number gnus-tmp-header)
3792                 subject (mail-header-subject gnus-tmp-header))
3793
3794           (cond
3795            ;; If the thread has changed subject, we might want to make
3796            ;; this subthread into a root.
3797            ((and (null gnus-thread-ignore-subject)
3798                  (not (zerop gnus-tmp-level))
3799                  gnus-tmp-prev-subject
3800                  (not (inline
3801                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3802             (setq new-roots (nconc new-roots (list (car thread)))
3803                   thread-end t
3804                   gnus-tmp-header nil))
3805            ;; If the article lies outside the current limit,
3806            ;; then we do not display it.
3807            ((not (memq number gnus-newsgroup-limit))
3808             (setq gnus-tmp-gathered
3809                   (nconc (mapcar
3810                           (lambda (h) (mail-header-number (car h)))
3811                           (cdar thread))
3812                          gnus-tmp-gathered))
3813             (setq gnus-tmp-new-adopts (if (cdar thread)
3814                                           (append gnus-tmp-new-adopts
3815                                                   (cdar thread))
3816                                         gnus-tmp-new-adopts)
3817                   thread-end t
3818                   gnus-tmp-header nil)
3819             (when (zerop gnus-tmp-level)
3820               (setq gnus-tmp-root-expunged t)))
3821            ;; Perhaps this article is to be marked as read?
3822            ((and gnus-summary-mark-below
3823                  (< (or (cdr (assq number gnus-newsgroup-scored))
3824                         default-score)
3825                     gnus-summary-mark-below)
3826                  ;; Don't touch sparse articles.
3827                  (not (gnus-summary-article-sparse-p number))
3828                  (not (gnus-summary-article-ancient-p number)))
3829             (setq gnus-newsgroup-unreads
3830                   (delq number gnus-newsgroup-unreads))
3831             (if gnus-newsgroup-auto-expire
3832                 (push number gnus-newsgroup-expirable)
3833               (push (cons number gnus-low-score-mark)
3834                     gnus-newsgroup-reads))))
3835
3836           (when gnus-tmp-header
3837             ;; We may have an old dummy line to output before this
3838             ;; article.
3839             (when (and gnus-tmp-dummy-line
3840                        (gnus-subject-equal
3841                         gnus-tmp-dummy-line
3842                         (mail-header-subject gnus-tmp-header)))
3843               (gnus-summary-insert-dummy-line
3844                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3845               (setq gnus-tmp-dummy-line nil))
3846
3847             ;; Compute the mark.
3848             (setq gnus-tmp-unread (gnus-article-mark number))
3849
3850             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3851                                   gnus-tmp-header gnus-tmp-level)
3852                   gnus-newsgroup-data)
3853
3854             ;; Actually insert the line.
3855             (setq
3856              gnus-tmp-subject-or-nil
3857              (cond
3858               ((and gnus-thread-ignore-subject
3859                     gnus-tmp-prev-subject
3860                     (not (inline (gnus-subject-equal
3861                                   gnus-tmp-prev-subject subject))))
3862                subject)
3863               ((zerop gnus-tmp-level)
3864                (if (and (eq gnus-summary-make-false-root 'empty)
3865                         (memq number gnus-tmp-gathered)
3866                         gnus-tmp-prev-subject
3867                         (inline (gnus-subject-equal
3868                                  gnus-tmp-prev-subject subject)))
3869                    gnus-summary-same-subject
3870                  subject))
3871               (t gnus-summary-same-subject)))
3872             (if (and (eq gnus-summary-make-false-root 'adopt)
3873                      (= gnus-tmp-level 1)
3874                      (memq number gnus-tmp-gathered))
3875                 (setq gnus-tmp-opening-bracket ?\<
3876                       gnus-tmp-closing-bracket ?\>)
3877               (setq gnus-tmp-opening-bracket ?\[
3878                     gnus-tmp-closing-bracket ?\]))
3879             (setq
3880              gnus-tmp-indentation
3881              (aref gnus-thread-indent-array gnus-tmp-level)
3882              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3883              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3884                                 gnus-summary-default-score 0)
3885              gnus-tmp-score-char
3886              (if (or (null gnus-summary-default-score)
3887                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3888                          gnus-summary-zcore-fuzz))
3889                  ? ;Whitespace
3890                (if (< gnus-tmp-score gnus-summary-default-score)
3891                    gnus-score-below-mark gnus-score-over-mark))
3892              gnus-tmp-replied
3893              (cond ((memq number gnus-newsgroup-processable)
3894                     gnus-process-mark)
3895                    ((memq number gnus-newsgroup-cached)
3896                     gnus-cached-mark)
3897                    ((memq number gnus-newsgroup-replied)
3898                     gnus-replied-mark)
3899                    ((memq number gnus-newsgroup-saved)
3900                     gnus-saved-mark)
3901                    (t gnus-unread-mark))
3902              gnus-tmp-from (mail-header-from gnus-tmp-header)
3903              gnus-tmp-name
3904              (cond
3905               ((string-match "<[^>]+> *$" gnus-tmp-from)
3906                (setq beg-match (match-beginning 0))
3907                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3908                         (substring gnus-tmp-from (1+ (match-beginning 0))
3909                                    (1- (match-end 0))))
3910                    (substring gnus-tmp-from 0 beg-match)))
3911               ((string-match "(.+)" gnus-tmp-from)
3912                (substring gnus-tmp-from
3913                           (1+ (match-beginning 0)) (1- (match-end 0))))
3914               (t gnus-tmp-from)))
3915             (when (string= gnus-tmp-name "")
3916               (setq gnus-tmp-name gnus-tmp-from))
3917             (unless (numberp gnus-tmp-lines)
3918               (setq gnus-tmp-lines 0))
3919             (gnus-put-text-property
3920              (point)
3921              (progn (eval gnus-summary-line-format-spec) (point))
3922              'gnus-number number)
3923             (when gnus-visual-p
3924               (forward-line -1)
3925               (gnus-run-hooks 'gnus-summary-update-hook)
3926               (forward-line 1))
3927
3928             (setq gnus-tmp-prev-subject subject)))
3929
3930         (when (nth 1 thread)
3931           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3932         (incf gnus-tmp-level)
3933         (setq threads (if thread-end nil (cdar thread)))
3934         (unless threads
3935           (setq gnus-tmp-level 0)))))
3936   (gnus-message 7 "Generating summary...done"))
3937
3938 (defun gnus-summary-prepare-unthreaded (headers)
3939   "Generate an unthreaded summary buffer based on HEADERS."
3940   (let (header number mark)
3941
3942     (beginning-of-line)
3943
3944     (while headers
3945       ;; We may have to root out some bad articles...
3946       (when (memq (setq number (mail-header-number
3947                                 (setq header (pop headers))))
3948                   gnus-newsgroup-limit)
3949         ;; Mark article as read when it has a low score.
3950         (when (and gnus-summary-mark-below
3951                    (< (or (cdr (assq number gnus-newsgroup-scored))
3952                           gnus-summary-default-score 0)
3953                       gnus-summary-mark-below)
3954                    (not (gnus-summary-article-ancient-p number)))
3955           (setq gnus-newsgroup-unreads
3956                 (delq number gnus-newsgroup-unreads))
3957           (if gnus-newsgroup-auto-expire
3958               (push number gnus-newsgroup-expirable)
3959             (push (cons number gnus-low-score-mark)
3960                   gnus-newsgroup-reads)))
3961
3962         (setq mark (gnus-article-mark number))
3963         (push (gnus-data-make number mark (1+ (point)) header 0)
3964               gnus-newsgroup-data)
3965         (gnus-summary-insert-line
3966          header 0 number
3967          mark (memq number gnus-newsgroup-replied)
3968          (memq number gnus-newsgroup-expirable)
3969          (mail-header-subject header) nil
3970          (cdr (assq number gnus-newsgroup-scored))
3971          (memq number gnus-newsgroup-processable))))))
3972
3973 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3974   "Select newsgroup GROUP.
3975 If READ-ALL is non-nil, all articles in the group are selected.
3976 If SELECT-ARTICLES, only select those articles from GROUP."
3977   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3978          ;;!!! Dirty hack; should be removed.
3979          (gnus-summary-ignore-duplicates
3980           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3981               t
3982             gnus-summary-ignore-duplicates))
3983          (info (nth 2 entry))
3984          articles fetched-articles cached)
3985
3986     (unless (gnus-check-server
3987              (setq gnus-current-select-method
3988                    (gnus-find-method-for-group group)))
3989       (error "Couldn't open server"))
3990
3991     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3992         (gnus-activate-group group)     ; Or we can activate it...
3993         (progn                          ; Or we bug out.
3994           (when (equal major-mode 'gnus-summary-mode)
3995             (kill-buffer (current-buffer)))
3996           (error "Couldn't request group %s: %s"
3997                  group (gnus-status-message group))))
3998
3999     (unless (gnus-request-group group t)
4000       (when (equal major-mode 'gnus-summary-mode)
4001         (kill-buffer (current-buffer)))
4002       (error "Couldn't request group %s: %s"
4003              group (gnus-status-message group)))
4004
4005     (setq gnus-newsgroup-name group)
4006     (setq gnus-newsgroup-unselected nil)
4007     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4008     (gnus-summary-setup-default-charset)
4009
4010     ;; Adjust and set lists of article marks.
4011     (when info
4012       (gnus-adjust-marked-articles info))
4013
4014     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4015     (setq cached
4016           (if (gnus-virtual-group-p group)
4017               gnus-newsgroup-cached
4018             (gnus-cache-articles-in-group group)))
4019
4020     (setq gnus-newsgroup-unreads
4021           (gnus-set-difference
4022            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4023            gnus-newsgroup-dormant))
4024
4025     (setq gnus-newsgroup-processable nil)
4026
4027     (gnus-update-read-articles group gnus-newsgroup-unreads)
4028
4029     (if (setq articles select-articles)
4030         (setq gnus-newsgroup-unselected
4031               (gnus-sorted-intersection
4032                gnus-newsgroup-unreads
4033                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4034       (setq articles (gnus-articles-to-read group read-all)))
4035
4036     (cond
4037      ((null articles)
4038       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4039       'quit)
4040      ((eq articles 0) nil)
4041      (t
4042       ;; Init the dependencies hash table.
4043       (setq gnus-newsgroup-dependencies
4044             (gnus-make-hashtable (length articles)))
4045       (gnus-set-global-variables)
4046       ;; Retrieve the headers and read them in.
4047       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4048       (setq gnus-newsgroup-headers
4049             (gnus-retrieve-parsed-headers
4050              articles gnus-newsgroup-name
4051              ;; We might want to fetch old headers, but
4052              ;; not if there is only 1 article.
4053              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4054                            (not (numberp gnus-fetch-old-headers)))
4055                       (> (length articles) 1))
4056                   gnus-fetch-old-headers)))
4057       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4058
4059       ;; Suppress duplicates?
4060       (when gnus-suppress-duplicates
4061         (gnus-dup-suppress-articles))
4062
4063       ;; Set the initial limit.
4064       (setq gnus-newsgroup-limit (copy-sequence articles))
4065       ;; Remove canceled articles from the list of unread articles.
4066       (setq gnus-newsgroup-unreads
4067             (gnus-set-sorted-intersection
4068              gnus-newsgroup-unreads
4069              (setq fetched-articles
4070                    (mapcar (lambda (headers) (mail-header-number headers))
4071                            gnus-newsgroup-headers))))
4072       ;; Removed marked articles that do not exist.
4073       (gnus-update-missing-marks
4074        (gnus-sorted-complement fetched-articles articles))
4075
4076       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4077       (when cached
4078         (setq gnus-newsgroup-cached cached))
4079
4080       ;; We might want to build some more threads first.
4081       (when (and gnus-fetch-old-headers
4082                  (eq gnus-headers-retrieved-by 'nov))
4083         (if (eq gnus-fetch-old-headers 'invisible)
4084             (gnus-build-all-threads)
4085           (gnus-build-old-threads)))
4086       ;; Let the Gnus agent mark articles as read.
4087       (when gnus-agent
4088         (gnus-agent-get-undownloaded-list))
4089       ;; Check whether auto-expire is to be done in this group.
4090       (setq gnus-newsgroup-auto-expire
4091             (gnus-group-auto-expirable-p group))
4092       ;; Set up the article buffer now, if necessary.
4093       (unless gnus-single-article-buffer
4094         (gnus-article-setup-buffer))
4095       ;; First and last article in this newsgroup.
4096       (when gnus-newsgroup-headers
4097         (setq gnus-newsgroup-begin
4098               (mail-header-number (car gnus-newsgroup-headers))
4099               gnus-newsgroup-end
4100               (mail-header-number
4101                (gnus-last-element gnus-newsgroup-headers))))
4102       ;; GROUP is successfully selected.
4103       (or gnus-newsgroup-headers t)))))
4104
4105 (defun gnus-articles-to-read (group &optional read-all)
4106   ;; Find out what articles the user wants to read.
4107   (let* ((articles
4108           ;; Select all articles if `read-all' is non-nil, or if there
4109           ;; are no unread articles.
4110           (if (or read-all
4111                   (and (zerop (length gnus-newsgroup-marked))
4112                        (zerop (length gnus-newsgroup-unreads)))
4113                   (eq (gnus-group-find-parameter group 'display)
4114                       'all))
4115               (gnus-uncompress-range (gnus-active group))
4116             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4117                           (copy-sequence gnus-newsgroup-unreads))
4118                   '<)))
4119          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4120          (scored (length scored-list))
4121          (number (length articles))
4122          (marked (+ (length gnus-newsgroup-marked)
4123                     (length gnus-newsgroup-dormant)))
4124          (select
4125           (cond
4126            ((numberp read-all)
4127             read-all)
4128            (t
4129             (condition-case ()
4130                 (cond
4131                  ((and (or (<= scored marked) (= scored number))
4132                        (natnump gnus-large-newsgroup)
4133                        (> number gnus-large-newsgroup))
4134                   (let* ((minibuffer-setup-hook (append
4135                                                  minibuffer-setup-hook
4136                                                  '(beginning-of-line)))
4137                          (input (read-string
4138                                  (format
4139                                   "How many articles from %s (max %d): "
4140                                   (gnus-limit-string gnus-newsgroup-name 35)
4141                                   number)
4142                                  (number-to-string gnus-large-newsgroup))))
4143                     (if (string-match "^[ \t]*$" input)
4144                         number
4145                       input)))
4146                  ((and (> scored marked) (< scored number)
4147                        (> (- scored number) 20))
4148                   (let ((input
4149                          (read-string
4150                           (format "%s %s (%d scored, %d total): "
4151                                   "How many articles from"
4152                                   group scored number))))
4153                     (if (string-match "^[ \t]*$" input)
4154                         number input)))
4155                  (t number))
4156               (quit nil))))))
4157     (setq select (if (stringp select) (string-to-number select) select))
4158     (if (or (null select) (zerop select))
4159         select
4160       (if (and (not (zerop scored)) (<= (abs select) scored))
4161           (progn
4162             (setq articles (sort scored-list '<))
4163             (setq number (length articles)))
4164         (setq articles (copy-sequence articles)))
4165
4166       (when (< (abs select) number)
4167         (if (< select 0)
4168             ;; Select the N oldest articles.
4169             (setcdr (nthcdr (1- (abs select)) articles) nil)
4170           ;; Select the N most recent articles.
4171           (setq articles (nthcdr (- number select) articles))))
4172       (setq gnus-newsgroup-unselected
4173             (gnus-sorted-intersection
4174              gnus-newsgroup-unreads
4175              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4176       articles)))
4177
4178 (defun gnus-killed-articles (killed articles)
4179   (let (out)
4180     (while articles
4181       (when (inline (gnus-member-of-range (car articles) killed))
4182         (push (car articles) out))
4183       (setq articles (cdr articles)))
4184     out))
4185
4186 (defun gnus-uncompress-marks (marks)
4187   "Uncompress the mark ranges in MARKS."
4188   (let ((uncompressed '(score bookmark))
4189         out)
4190     (while marks
4191       (if (memq (caar marks) uncompressed)
4192           (push (car marks) out)
4193         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4194       (setq marks (cdr marks)))
4195     out))
4196
4197 (defun gnus-adjust-marked-articles (info)
4198   "Set all article lists and remove all marks that are no longer valid."
4199   (let* ((marked-lists (gnus-info-marks info))
4200          (active (gnus-active (gnus-info-group info)))
4201          (min (car active))
4202          (max (cdr active))
4203          (types gnus-article-mark-lists)
4204          (uncompressed '(score bookmark killed))
4205          marks var articles article mark)
4206
4207     (while marked-lists
4208       (setq marks (pop marked-lists))
4209       (set (setq var (intern (format "gnus-newsgroup-%s"
4210                                      (car (rassq (setq mark (car marks))
4211                                                  types)))))
4212            (if (memq (car marks) uncompressed) (cdr marks)
4213              (gnus-uncompress-range (cdr marks))))
4214
4215       (setq articles (symbol-value var))
4216
4217       ;; All articles have to be subsets of the active articles.
4218       (cond
4219        ;; Adjust "simple" lists.
4220        ((memq mark '(tick dormant expire reply save))
4221         (while articles
4222           (when (or (< (setq article (pop articles)) min) (> article max))
4223             (set var (delq article (symbol-value var))))))
4224        ;; Adjust assocs.
4225        ((memq mark uncompressed)
4226         (when (not (listp (cdr (symbol-value var))))
4227           (set var (list (symbol-value var))))
4228         (when (not (listp (cdr articles)))
4229           (setq articles (list articles)))
4230         (while articles
4231           (when (or (not (consp (setq article (pop articles))))
4232                     (< (car article) min)
4233                     (> (car article) max))
4234             (set var (delq article (symbol-value var))))))))))
4235
4236 (defun gnus-update-missing-marks (missing)
4237   "Go through the list of MISSING articles and remove them from the mark lists."
4238   (when missing
4239     (let ((types gnus-article-mark-lists)
4240           var m)
4241       ;; Go through all types.
4242       (while types
4243         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4244         (when (symbol-value var)
4245           ;; This list has articles.  So we delete all missing articles
4246           ;; from it.
4247           (setq m missing)
4248           (while m
4249             (set var (delq (pop m) (symbol-value var)))))))))
4250
4251 (defun gnus-update-marks ()
4252   "Enter the various lists of marked articles into the newsgroup info list."
4253   (let ((types gnus-article-mark-lists)
4254         (info (gnus-get-info gnus-newsgroup-name))
4255         (uncompressed '(score bookmark killed))
4256         type list newmarked symbol delta-marks)
4257     (when info
4258       ;; Add all marks lists to the list of marks lists.
4259       (while (setq type (pop types))
4260         (setq list (symbol-value
4261                           (setq symbol
4262                                 (intern (format "gnus-newsgroup-%s"
4263                                                 (car type))))))
4264
4265         (when list
4266           ;; Get rid of the entries of the articles that have the
4267           ;; default score.
4268           (when (and (eq (cdr type) 'score)
4269                      gnus-save-score
4270                      list)
4271             (let* ((arts list)
4272                    (prev (cons nil list))
4273                    (all prev))
4274               (while arts
4275                 (if (or (not (consp (car arts)))
4276                         (= (cdar arts) gnus-summary-default-score))
4277                     (setcdr prev (cdr arts))
4278                   (setq prev arts))
4279                 (setq arts (cdr arts)))
4280               (setq list (cdr all)))))
4281
4282        (or (memq (cdr type) uncompressed)
4283            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4284        
4285        (when (gnus-check-backend-function 'request-set-mark
4286                                           gnus-newsgroup-name)
4287          ;; uncompressed:s are not proper flags (they are cons cells)
4288          ;; cache is a internal gnus flag
4289          (unless (memq (cdr type) (cons 'cache uncompressed))
4290            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4291                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4292                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4293              (if add
4294                  (push (list add 'add (list (cdr type))) delta-marks))
4295              (if del
4296                  (push (list del 'del (list (cdr type))) delta-marks)))))
4297           
4298         (when list
4299          (push (cons (cdr type) list) newmarked)))
4300
4301       (when delta-marks
4302         (unless (gnus-check-group gnus-newsgroup-name)
4303           (error "Can't open server for %s" gnus-newsgroup-name))
4304         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4305           
4306       ;; Enter these new marks into the info of the group.
4307       (if (nthcdr 3 info)
4308           (setcar (nthcdr 3 info) newmarked)
4309         ;; Add the marks lists to the end of the info.
4310         (when newmarked
4311           (setcdr (nthcdr 2 info) (list newmarked))))
4312
4313       ;; Cut off the end of the info if there's nothing else there.
4314       (let ((i 5))
4315         (while (and (> i 2)
4316                     (not (nth i info)))
4317           (when (nthcdr (decf i) info)
4318             (setcdr (nthcdr i info) nil)))))))
4319
4320 (defun gnus-set-mode-line (where)
4321   "This function sets the mode line of the article or summary buffers.
4322 If WHERE is `summary', the summary mode line format will be used."
4323   ;; Is this mode line one we keep updated?
4324   (when (and (memq where gnus-updated-mode-lines)
4325              (symbol-value
4326               (intern (format "gnus-%s-mode-line-format-spec" where))))
4327     (let (mode-string)
4328       (save-excursion
4329         ;; We evaluate this in the summary buffer since these
4330         ;; variables are buffer-local to that buffer.
4331         (set-buffer gnus-summary-buffer)
4332         ;; We bind all these variables that are used in the `eval' form
4333         ;; below.
4334         (let* ((mformat (symbol-value
4335                          (intern
4336                           (format "gnus-%s-mode-line-format-spec" where))))
4337                (gnus-tmp-group-name gnus-newsgroup-name)
4338                (gnus-tmp-article-number (or gnus-current-article 0))
4339                (gnus-tmp-unread gnus-newsgroup-unreads)
4340                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4341                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4342                (gnus-tmp-unread-and-unselected
4343                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4344                             (zerop gnus-tmp-unselected))
4345                        "")
4346                       ((zerop gnus-tmp-unselected)
4347                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4348                       (t (format "{%d(+%d) more}"
4349                                  gnus-tmp-unread-and-unticked
4350                                  gnus-tmp-unselected))))
4351                (gnus-tmp-subject
4352                 (if (and gnus-current-headers
4353                          (vectorp gnus-current-headers))
4354                     (gnus-mode-string-quote
4355                      (mail-header-subject gnus-current-headers))
4356                   ""))
4357                bufname-length max-len
4358                gnus-tmp-header);; passed as argument to any user-format-funcs
4359           (setq mode-string (eval mformat))
4360           (setq bufname-length (if (string-match "%b" mode-string)
4361                                    (- (length
4362                                        (buffer-name
4363                                         (if (eq where 'summary)
4364                                             nil
4365                                           (get-buffer gnus-article-buffer))))
4366                                       2)
4367                                  0))
4368           (setq max-len (max 4 (if gnus-mode-non-string-length
4369                                    (- (window-width)
4370                                       gnus-mode-non-string-length
4371                                       bufname-length)
4372                                  (length mode-string))))
4373           ;; We might have to chop a bit of the string off...
4374           (when (> (length mode-string) max-len)
4375             (setq mode-string
4376                   (concat (gnus-truncate-string mode-string (- max-len 3))
4377                           "...")))
4378           ;; Pad the mode string a bit.
4379           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4380       ;; Update the mode line.
4381       (setq mode-line-buffer-identification
4382             (gnus-mode-line-buffer-identification (list mode-string)))
4383       (set-buffer-modified-p t))))
4384
4385 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4386   "Go through the HEADERS list and add all Xrefs to a hash table.
4387 The resulting hash table is returned, or nil if no Xrefs were found."
4388   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4389          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4390          (xref-hashtb (gnus-make-hashtable))
4391          start group entry number xrefs header)
4392     (while headers
4393       (setq header (pop headers))
4394       (when (and (setq xrefs (mail-header-xref header))
4395                  (not (memq (setq number (mail-header-number header))
4396                             unreads)))
4397         (setq start 0)
4398         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4399           (setq start (match-end 0))
4400           (setq group (if prefix
4401                           (concat prefix (substring xrefs (match-beginning 1)
4402                                                     (match-end 1)))
4403                         (substring xrefs (match-beginning 1) (match-end 1))))
4404           (setq number
4405                 (string-to-int (substring xrefs (match-beginning 2)
4406                                           (match-end 2))))
4407           (if (setq entry (gnus-gethash group xref-hashtb))
4408               (setcdr entry (cons number (cdr entry)))
4409             (gnus-sethash group (cons number nil) xref-hashtb)))))
4410     (and start xref-hashtb)))
4411
4412 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4413   "Look through all the headers and mark the Xrefs as read."
4414   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4415         name entry info xref-hashtb idlist method nth4)
4416     (save-excursion
4417       (set-buffer gnus-group-buffer)
4418       (when (setq xref-hashtb
4419                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4420         (mapatoms
4421          (lambda (group)
4422            (unless (string= from-newsgroup (setq name (symbol-name group)))
4423              (setq idlist (symbol-value group))
4424              ;; Dead groups are not updated.
4425              (and (prog1
4426                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4427                             info (nth 2 entry))
4428                     (when (stringp (setq nth4 (gnus-info-method info)))
4429                       (setq nth4 (gnus-server-to-method nth4))))
4430                   ;; Only do the xrefs if the group has the same
4431                   ;; select method as the group we have just read.
4432                   (or (gnus-methods-equal-p
4433                        nth4 (gnus-find-method-for-group from-newsgroup))
4434                       virtual
4435                       (equal nth4 (setq method (gnus-find-method-for-group
4436                                                 from-newsgroup)))
4437                       (and (equal (car nth4) (car method))
4438                            (equal (nth 1 nth4) (nth 1 method))))
4439                   gnus-use-cross-reference
4440                   (or (not (eq gnus-use-cross-reference t))
4441                       virtual
4442                       ;; Only do cross-references on subscribed
4443                       ;; groups, if that is what is wanted.
4444                       (<= (gnus-info-level info) gnus-level-subscribed))
4445                   (gnus-group-make-articles-read name idlist))))
4446          xref-hashtb)))))
4447
4448 (defun gnus-compute-read-articles (group articles)
4449   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4450          (info (nth 2 entry))
4451          (active (gnus-active group))
4452          ninfo)
4453     (when entry
4454       ;; First peel off all invalid article numbers.
4455       (when active
4456         (let ((ids articles)
4457               id first)
4458           (while (setq id (pop ids))
4459             (when (and first (> id (cdr active)))
4460               ;; We'll end up in this situation in one particular
4461               ;; obscure situation.  If you re-scan a group and get
4462               ;; a new article that is cross-posted to a different
4463               ;; group that has not been re-scanned, you might get
4464               ;; crossposted article that has a higher number than
4465               ;; Gnus believes possible.  So we re-activate this
4466               ;; group as well.  This might mean doing the
4467               ;; crossposting thingy will *increase* the number
4468               ;; of articles in some groups.  Tsk, tsk.
4469               (setq active (or (gnus-activate-group group) active)))
4470             (when (or (> id (cdr active))
4471                       (< id (car active)))
4472               (setq articles (delq id articles))))))
4473       ;; If the read list is nil, we init it.
4474       (if (and active
4475                (null (gnus-info-read info))
4476                (> (car active) 1))
4477           (setq ninfo (cons 1 (1- (car active))))
4478         (setq ninfo (gnus-info-read info)))
4479       ;; Then we add the read articles to the range.
4480       (gnus-add-to-range
4481        ninfo (setq articles (sort articles '<))))))
4482
4483 (defun gnus-group-make-articles-read (group articles)
4484   "Update the info of GROUP to say that ARTICLES are read."
4485   (let* ((num 0)
4486          (entry (gnus-gethash group gnus-newsrc-hashtb))
4487          (info (nth 2 entry))
4488          (active (gnus-active group))
4489          range)
4490     (when entry
4491       (setq range (gnus-compute-read-articles group articles))
4492       (save-excursion
4493         (set-buffer gnus-group-buffer)
4494         (gnus-undo-register
4495           `(progn
4496              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4497              (gnus-info-set-read ',info ',(gnus-info-read info))
4498              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4499              (gnus-group-update-group ,group t))))
4500       ;; Add the read articles to the range.
4501       (gnus-info-set-read info range)
4502       ;; Then we have to re-compute how many unread
4503       ;; articles there are in this group.
4504       (when active
4505         (cond
4506          ((not range)
4507           (setq num (- (1+ (cdr active)) (car active))))
4508          ((not (listp (cdr range)))
4509           (setq num (- (cdr active) (- (1+ (cdr range))
4510                                        (car range)))))
4511          (t
4512           (while range
4513             (if (numberp (car range))
4514                 (setq num (1+ num))
4515               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4516             (setq range (cdr range)))
4517           (setq num (- (cdr active) num))))
4518         ;; Update the number of unread articles.
4519         (setcar entry num)
4520         ;; Update the group buffer.
4521         (gnus-group-update-group group t)))))
4522
4523 (defvar gnus-newsgroup-none-id 0)
4524
4525 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4526   (let ((cur nntp-server-buffer)
4527         (dependencies
4528          (or dependencies
4529              (save-excursion (set-buffer gnus-summary-buffer)
4530                              gnus-newsgroup-dependencies)))
4531         headers id end ref
4532         (mail-parse-charset gnus-newsgroup-charset)
4533         (mail-parse-ignored-charsets
4534          (or (and (gnus-buffer-live-p gnus-summary-buffer)
4535                   (save-excursion (set-buffer gnus-summary-buffer)
4536                                   gnus-newsgroup-ignored-charsets))
4537              gnus-newsgroup-ignored-charsets)))
4538     (save-excursion
4539       (set-buffer nntp-server-buffer)
4540       ;; Translate all TAB characters into SPACE characters.
4541       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4542       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4543       (gnus-run-hooks 'gnus-parse-headers-hook)
4544       (let ((case-fold-search t)
4545             in-reply-to header p lines chars ctype)
4546         (goto-char (point-min))
4547         ;; Search to the beginning of the next header.  Error messages
4548         ;; do not begin with 2 or 3.
4549         (while (re-search-forward "^[23][0-9]+ " nil t)
4550           (setq id nil
4551                 ref nil)
4552           ;; This implementation of this function, with nine
4553           ;; search-forwards instead of the one re-search-forward and
4554           ;; a case (which basically was the old function) is actually
4555           ;; about twice as fast, even though it looks messier.  You
4556           ;; can't have everything, I guess.  Speed and elegance
4557           ;; doesn't always go hand in hand.
4558           (setq
4559            header
4560            (make-full-mail-header
4561             ;; Number.
4562             (prog1
4563                 (read cur)
4564               (end-of-line)
4565               (setq p (point))
4566               (narrow-to-region (point)
4567                                 (or (and (search-forward "\n.\n" nil t)
4568                                          (- (point) 2))
4569                                     (point))))
4570             ;; Subject.
4571             (progn
4572               (goto-char p)
4573               (if (search-forward "\nsubject: " nil t)
4574                   (buffer-substring (match-end 0) (std11-field-end))
4575                 "(none)"))
4576             ;; From.
4577             (progn
4578               (goto-char p)
4579               (if (search-forward "\nfrom: " nil t)
4580                   (buffer-substring (match-end 0) (std11-field-end))
4581                 "(nobody)"))
4582             ;; Date.
4583             (progn
4584               (goto-char p)
4585               (if (search-forward "\ndate: " nil t)
4586                   (buffer-substring (match-end 0) (std11-field-end))
4587                 ""))
4588             ;; Message-ID.
4589             (progn
4590               (goto-char p)
4591               (setq id (if (re-search-forward
4592                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4593                            ;; We do it this way to make sure the Message-ID
4594                            ;; is (somewhat) syntactically valid.
4595                            (buffer-substring (match-beginning 1)
4596                                              (match-end 1))
4597                          ;; If there was no message-id, we just fake one
4598                          ;; to make subsequent routines simpler.
4599                          (nnheader-generate-fake-message-id))))
4600             ;; References.
4601             (progn
4602               (goto-char p)
4603               (if (search-forward "\nreferences: " nil t)
4604                   (progn
4605                     (setq end (point))
4606                     (prog1
4607                         (buffer-substring (match-end 0) (std11-field-end))
4608                       (setq ref
4609                             (buffer-substring
4610                              (progn
4611                                ;; (end-of-line)
4612                                (search-backward ">" end t)
4613                                (1+ (point)))
4614                              (progn
4615                                (search-backward "<" end t)
4616                                (point))))))
4617                 ;; Get the references from the in-reply-to header if there
4618                 ;; were no references and the in-reply-to header looks
4619                 ;; promising.
4620                 (if (and (search-forward "\nin-reply-to: " nil t)
4621                          (setq in-reply-to
4622                                (buffer-substring (match-end 0)
4623                                                  (std11-field-end)))
4624                          (string-match "<[^>]+>" in-reply-to))
4625                     (let (ref2)
4626                       (setq ref (substring in-reply-to (match-beginning 0)
4627                                            (match-end 0)))
4628                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4629                         (setq ref2 (substring in-reply-to (match-beginning 0)
4630                                               (match-end 0)))
4631                         (when (> (length ref2) (length ref))
4632                           (setq ref ref2)))
4633                       ref)
4634                   (setq ref nil))))
4635             ;; Chars.
4636             (progn
4637               (goto-char p)
4638               (if (search-forward "\nchars: " nil t)
4639                   (if (numberp (setq chars (ignore-errors (read cur))))
4640                       chars 0)
4641                 0))
4642             ;; Lines.
4643             (progn
4644               (goto-char p)
4645               (if (search-forward "\nlines: " nil t)
4646                   (if (numberp (setq lines (ignore-errors (read cur))))
4647                       lines 0)
4648                 0))
4649             ;; Xref.
4650             (progn
4651               (goto-char p)
4652               (and (search-forward "\nxref: " nil t)
4653                    (buffer-substring (match-end 0) (std11-field-end))))
4654             ;; Extra.
4655             (when gnus-extra-headers
4656               (let ((extra gnus-extra-headers)
4657                     out)
4658                 (while extra
4659                   (goto-char p)
4660                   (when (search-forward
4661                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4662                     (push (cons (car extra)
4663                                 (buffer-substring (match-end 0)
4664                                                   (std11-field-end)))
4665                           out))
4666                   (pop extra))
4667                 out))))
4668           (goto-char p)
4669           (if (and (search-forward "\ncontent-type: " nil t)
4670                    (setq ctype
4671                          (buffer-substring (match-end 0) (std11-field-end))))
4672               (mime-entity-set-content-type-internal
4673                header (mime-parse-Content-Type ctype)))
4674           (when (equal id ref)
4675             (setq ref nil))
4676
4677           (when gnus-alter-header-function
4678             (funcall gnus-alter-header-function header)
4679             (setq id (mail-header-id header)
4680                   ref (gnus-parent-id (mail-header-references header))))
4681
4682           (when (setq header
4683                       (gnus-dependencies-add-header
4684                        header dependencies force-new))
4685             (push header headers))
4686           (goto-char (point-max))
4687           (widen))
4688         (nreverse headers)))))
4689
4690 ;; Goes through the xover lines and returns a list of vectors
4691 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4692                                                   force-new dependencies
4693                                                   group also-fetch-heads)
4694   "Parse the news overview data in the server buffer, and return a
4695 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4696   ;; Get the Xref when the users reads the articles since most/some
4697   ;; NNTP servers do not include Xrefs when using XOVER.
4698   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4699   (let ((mail-parse-charset gnus-newsgroup-charset)
4700         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4701         (cur nntp-server-buffer)
4702         (dependencies (or dependencies gnus-newsgroup-dependencies))
4703         number headers header)
4704     (save-excursion
4705       (set-buffer nntp-server-buffer)
4706       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4707       ;; Allow the user to mangle the headers before parsing them.
4708       (gnus-run-hooks 'gnus-parse-headers-hook)
4709       (goto-char (point-min))
4710       (while (not (eobp))
4711         (condition-case ()
4712             (while (and sequence (not (eobp)))
4713               (setq number (read cur))
4714               (while (and sequence
4715                           (< (car sequence) number))
4716                 (setq sequence (cdr sequence)))
4717               (and sequence
4718                    (eq number (car sequence))
4719                    (progn
4720                      (setq sequence (cdr sequence))
4721                      (setq header (inline
4722                                     (gnus-nov-parse-line
4723                                      number dependencies force-new))))
4724                    (push header headers))
4725               (forward-line 1))
4726           (error
4727            (gnus-error 4 "Strange nov line (%d)"
4728                        (count-lines (point-min) (point)))))
4729         (forward-line 1))
4730       ;; A common bug in inn is that if you have posted an article and
4731       ;; then retrieves the active file, it will answer correctly --
4732       ;; the new article is included.  However, a NOV entry for the
4733       ;; article may not have been generated yet, so this may fail.
4734       ;; We work around this problem by retrieving the last few
4735       ;; headers using HEAD.
4736       (if (or (not also-fetch-heads)
4737               (not sequence))
4738           ;; We (probably) got all the headers.
4739           (nreverse headers)
4740         (let ((gnus-nov-is-evil t))
4741           (nconc
4742            (nreverse headers)
4743            (gnus-retrieve-parsed-headers sequence group)
4744            ))))))
4745
4746 (defun gnus-article-get-xrefs ()
4747   "Fill in the Xref value in `gnus-current-headers', if necessary.
4748 This is meant to be called in `gnus-article-internal-prepare-hook'."
4749   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4750                                  gnus-current-headers)))
4751     (or (not gnus-use-cross-reference)
4752         (not headers)
4753         (and (mail-header-xref headers)
4754              (not (string= (mail-header-xref headers) "")))
4755         (let ((case-fold-search t)
4756               xref)
4757           (save-restriction
4758             (nnheader-narrow-to-headers)
4759             (goto-char (point-min))
4760             (when (or (and (eq (downcase (char-after)) ?x)
4761                            (looking-at "Xref:"))
4762                       (search-forward "\nXref:" nil t))
4763               (goto-char (1+ (match-end 0)))
4764               (setq xref (buffer-substring (point)
4765                                            (progn (end-of-line) (point))))
4766               (mail-header-set-xref headers xref)))))))
4767
4768 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4769   "Find article ID and insert the summary line for that article.
4770 OLD-HEADER can either be a header or a line number to insert
4771 the subject line on."
4772   (let* ((line (and (numberp old-header) old-header))
4773          (old-header (and (vectorp old-header) old-header))
4774          (header (cond ((and old-header use-old-header)
4775                         old-header)
4776                        ((and (numberp id)
4777                              (gnus-number-to-header id))
4778                         (gnus-number-to-header id))
4779                        (t
4780                         (gnus-read-header id))))
4781          (number (and (numberp id) id))
4782          d)
4783     (when header
4784       ;; Rebuild the thread that this article is part of and go to the
4785       ;; article we have fetched.
4786       (when (and (not gnus-show-threads)
4787                  old-header)
4788         (when (and number
4789                    (setq d (gnus-data-find (mail-header-number old-header))))
4790           (goto-char (gnus-data-pos d))
4791           (gnus-data-remove
4792            number
4793            (- (gnus-point-at-bol)
4794               (prog1
4795                   (1+ (gnus-point-at-eol))
4796                 (gnus-delete-line))))))
4797       (when old-header
4798         (mail-header-set-number header (mail-header-number old-header)))
4799       (setq gnus-newsgroup-sparse
4800             (delq (setq number (mail-header-number header))
4801                   gnus-newsgroup-sparse))
4802       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4803       (push number gnus-newsgroup-limit)
4804       (gnus-rebuild-thread (mail-header-id header) line)
4805       (gnus-summary-goto-subject number nil t))
4806     (when (and (numberp number)
4807                (> number 0))
4808       ;; We have to update the boundaries even if we can't fetch the
4809       ;; article if ID is a number -- so that the next `P' or `N'
4810       ;; command will fetch the previous (or next) article even
4811       ;; if the one we tried to fetch this time has been canceled.
4812       (when (> number gnus-newsgroup-end)
4813         (setq gnus-newsgroup-end number))
4814       (when (< number gnus-newsgroup-begin)
4815         (setq gnus-newsgroup-begin number))
4816       (setq gnus-newsgroup-unselected
4817             (delq number gnus-newsgroup-unselected)))
4818     ;; Report back a success?
4819     (and header (mail-header-number header))))
4820
4821 ;;; Process/prefix in the summary buffer
4822
4823 (defun gnus-summary-work-articles (n)
4824   "Return a list of articles to be worked upon.
4825 The prefix argument, the list of process marked articles, and the
4826 current article will be taken into consideration."
4827   (save-excursion
4828     (set-buffer gnus-summary-buffer)
4829     (cond
4830      (n
4831       ;; A numerical prefix has been given.
4832       (setq n (prefix-numeric-value n))
4833       (let ((backward (< n 0))
4834             (n (abs (prefix-numeric-value n)))
4835             articles article)
4836         (save-excursion
4837           (while
4838               (and (> n 0)
4839                    (push (setq article (gnus-summary-article-number))
4840                          articles)
4841                    (if backward
4842                        (gnus-summary-find-prev nil article)
4843                      (gnus-summary-find-next nil article)))
4844             (decf n)))
4845         (nreverse articles)))
4846      ((and (gnus-region-active-p) (mark))
4847       (message "region active")
4848       ;; Work on the region between point and mark.
4849       (let ((max (max (point) (mark)))
4850             articles article)
4851         (save-excursion
4852           (goto-char (min (point) (mark)))
4853           (while
4854               (and
4855                (push (setq article (gnus-summary-article-number)) articles)
4856                (gnus-summary-find-next nil article)
4857                (< (point) max)))
4858           (nreverse articles))))
4859      (gnus-newsgroup-processable
4860       ;; There are process-marked articles present.
4861       ;; Save current state.
4862       (gnus-summary-save-process-mark)
4863       ;; Return the list.
4864       (reverse gnus-newsgroup-processable))
4865      (t
4866       ;; Just return the current article.
4867       (list (gnus-summary-article-number))))))
4868
4869 (defmacro gnus-summary-iterate (arg &rest forms)
4870   "Iterate over the process/prefixed articles and do FORMS.
4871 ARG is the interactive prefix given to the command.  FORMS will be
4872 executed with point over the summary line of the articles."
4873   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4874     `(let ((,articles (gnus-summary-work-articles ,arg)))
4875        (while ,articles
4876          (gnus-summary-goto-subject (car ,articles))
4877          ,@forms
4878          (pop ,articles)))))
4879
4880 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4881 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4882
4883 (defun gnus-summary-save-process-mark ()
4884   "Push the current set of process marked articles on the stack."
4885   (interactive)
4886   (push (copy-sequence gnus-newsgroup-processable)
4887         gnus-newsgroup-process-stack))
4888
4889 (defun gnus-summary-kill-process-mark ()
4890   "Push the current set of process marked articles on the stack and unmark."
4891   (interactive)
4892   (gnus-summary-save-process-mark)
4893   (gnus-summary-unmark-all-processable))
4894
4895 (defun gnus-summary-yank-process-mark ()
4896   "Pop the last process mark state off the stack and restore it."
4897   (interactive)
4898   (unless gnus-newsgroup-process-stack
4899     (error "Empty mark stack"))
4900   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4901
4902 (defun gnus-summary-process-mark-set (set)
4903   "Make SET into the current process marked articles."
4904   (gnus-summary-unmark-all-processable)
4905   (while set
4906     (gnus-summary-set-process-mark (pop set))))
4907
4908 ;;; Searching and stuff
4909
4910 (defun gnus-summary-search-group (&optional backward use-level)
4911   "Search for next unread newsgroup.
4912 If optional argument BACKWARD is non-nil, search backward instead."
4913   (save-excursion
4914     (set-buffer gnus-group-buffer)
4915     (when (gnus-group-search-forward
4916            backward nil (if use-level (gnus-group-group-level) nil))
4917       (gnus-group-group-name))))
4918
4919 (defun gnus-summary-best-group (&optional exclude-group)
4920   "Find the name of the best unread group.
4921 If EXCLUDE-GROUP, do not go to this group."
4922   (save-excursion
4923     (set-buffer gnus-group-buffer)
4924     (save-excursion
4925       (gnus-group-best-unread-group exclude-group))))
4926
4927 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4928   (if backward (gnus-summary-find-prev)
4929     (let* ((dummy (gnus-summary-article-intangible-p))
4930            (article (or article (gnus-summary-article-number)))
4931            (arts (gnus-data-find-list article))
4932            result)
4933       (when (and (not dummy)
4934                  (or (not gnus-summary-check-current)
4935                      (not unread)
4936                      (not (gnus-data-unread-p (car arts)))))
4937         (setq arts (cdr arts)))
4938       (when (setq result
4939                   (if unread
4940                       (progn
4941                         (while arts
4942                           (when (or (and undownloaded
4943                                          (eq gnus-undownloaded-mark
4944                                              (gnus-data-mark (car arts))))
4945                                     (gnus-data-unread-p (car arts)))
4946                             (setq result (car arts)
4947                                   arts nil))
4948                           (setq arts (cdr arts)))
4949                         result)
4950                     (car arts)))
4951         (goto-char (gnus-data-pos result))
4952         (gnus-data-number result)))))
4953
4954 (defun gnus-summary-find-prev (&optional unread article)
4955   (let* ((eobp (eobp))
4956          (article (or article (gnus-summary-article-number)))
4957          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4958          result)
4959     (when (and (not eobp)
4960                (or (not gnus-summary-check-current)
4961                    (not unread)
4962                    (not (gnus-data-unread-p (car arts)))))
4963       (setq arts (cdr arts)))
4964     (when (setq result
4965                 (if unread
4966                     (progn
4967                       (while arts
4968                         (when (gnus-data-unread-p (car arts))
4969                           (setq result (car arts)
4970                                 arts nil))
4971                         (setq arts (cdr arts)))
4972                       result)
4973                   (car arts)))
4974       (goto-char (gnus-data-pos result))
4975       (gnus-data-number result))))
4976
4977 (defun gnus-summary-find-subject (subject &optional unread backward article)
4978   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4979          (article (or article (gnus-summary-article-number)))
4980          (articles (gnus-data-list backward))
4981          (arts (gnus-data-find-list article articles))
4982          result)
4983     (when (or (not gnus-summary-check-current)
4984               (not unread)
4985               (not (gnus-data-unread-p (car arts))))
4986       (setq arts (cdr arts)))
4987     (while arts
4988       (and (or (not unread)
4989                (gnus-data-unread-p (car arts)))
4990            (vectorp (gnus-data-header (car arts)))
4991            (gnus-subject-equal
4992             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4993            (setq result (car arts)
4994                  arts nil))
4995       (setq arts (cdr arts)))
4996     (and result
4997          (goto-char (gnus-data-pos result))
4998          (gnus-data-number result))))
4999
5000 (defun gnus-summary-search-forward (&optional unread subject backward)
5001   "Search forward for an article.
5002 If UNREAD, look for unread articles.  If SUBJECT, look for
5003 articles with that subject.  If BACKWARD, search backward instead."
5004   (cond (subject (gnus-summary-find-subject subject unread backward))
5005         (backward (gnus-summary-find-prev unread))
5006         (t (gnus-summary-find-next unread))))
5007
5008 (defun gnus-recenter (&optional n)
5009   "Center point in window and redisplay frame.
5010 Also do horizontal recentering."
5011   (interactive "P")
5012   (when (and gnus-auto-center-summary
5013              (not (eq gnus-auto-center-summary 'vertical)))
5014     (gnus-horizontal-recenter))
5015   (recenter n))
5016
5017 (defun gnus-summary-recenter ()
5018   "Center point in the summary window.
5019 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5020 displayed, no centering will be performed."
5021   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5022   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5023   (let* ((top (cond ((< (window-height) 4) 0)
5024                     ((< (window-height) 7) 1)
5025                     (t (if (numberp gnus-auto-center-summary)
5026                            gnus-auto-center-summary
5027                          2))))
5028          (height (1- (window-height)))
5029          (bottom (save-excursion (goto-char (point-max))
5030                                  (forward-line (- height))
5031                                  (point)))
5032          (window (get-buffer-window (current-buffer))))
5033     ;; The user has to want it.
5034     (when gnus-auto-center-summary
5035       (when (get-buffer-window gnus-article-buffer)
5036         ;; Only do recentering when the article buffer is displayed,
5037         ;; Set the window start to either `bottom', which is the biggest
5038         ;; possible valid number, or the second line from the top,
5039         ;; whichever is the least.
5040         (set-window-start
5041          window (min bottom (save-excursion
5042                               (forward-line (- top)) (point)))))
5043       ;; Do horizontal recentering while we're at it.
5044       (when (and (get-buffer-window (current-buffer) t)
5045                  (not (eq gnus-auto-center-summary 'vertical)))
5046         (let ((selected (selected-window)))
5047           (select-window (get-buffer-window (current-buffer) t))
5048           (gnus-summary-position-point)
5049           (gnus-horizontal-recenter)
5050           (select-window selected))))))
5051
5052 (defun gnus-summary-jump-to-group (newsgroup)
5053   "Move point to NEWSGROUP in group mode buffer."
5054   ;; Keep update point of group mode buffer if visible.
5055   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5056       (save-window-excursion
5057         ;; Take care of tree window mode.
5058         (when (get-buffer-window gnus-group-buffer)
5059           (pop-to-buffer gnus-group-buffer))
5060         (gnus-group-jump-to-group newsgroup))
5061     (save-excursion
5062       ;; Take care of tree window mode.
5063       (if (get-buffer-window gnus-group-buffer)
5064           (pop-to-buffer gnus-group-buffer)
5065         (set-buffer gnus-group-buffer))
5066       (gnus-group-jump-to-group newsgroup))))
5067
5068 ;; This function returns a list of article numbers based on the
5069 ;; difference between the ranges of read articles in this group and
5070 ;; the range of active articles.
5071 (defun gnus-list-of-unread-articles (group)
5072   (let* ((read (gnus-info-read (gnus-get-info group)))
5073          (active (or (gnus-active group) (gnus-activate-group group)))
5074          (last (cdr active))
5075          first nlast unread)
5076     ;; If none are read, then all are unread.
5077     (if (not read)
5078         (setq first (car active))
5079       ;; If the range of read articles is a single range, then the
5080       ;; first unread article is the article after the last read
5081       ;; article.  Sounds logical, doesn't it?
5082       (if (not (listp (cdr read)))
5083           (setq first (max (car active) (1+ (cdr read))))
5084         ;; `read' is a list of ranges.
5085         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5086                                   (caar read)))
5087                   1)
5088           (setq first (car active)))
5089         (while read
5090           (when first
5091             (while (< first nlast)
5092               (push first unread)
5093               (setq first (1+ first))))
5094           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5095           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5096           (setq read (cdr read)))))
5097     ;; And add the last unread articles.
5098     (while (<= first last)
5099       (push first unread)
5100       (setq first (1+ first)))
5101     ;; Return the list of unread articles.
5102     (delq 0 (nreverse unread))))
5103
5104 (defun gnus-list-of-read-articles (group)
5105   "Return a list of unread, unticked and non-dormant articles."
5106   (let* ((info (gnus-get-info group))
5107          (marked (gnus-info-marks info))
5108          (active (gnus-active group)))
5109     (and info active
5110          (gnus-set-difference
5111           (gnus-sorted-complement
5112            (gnus-uncompress-range active)
5113            (gnus-list-of-unread-articles group))
5114           (append
5115            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5116            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5117
5118 ;; Various summary commands
5119
5120 (defun gnus-summary-select-article-buffer ()
5121   "Reconfigure windows to show article buffer."
5122   (interactive)
5123   (if (not (gnus-buffer-live-p gnus-article-buffer))
5124       (error "There is no article buffer for this summary buffer")
5125     (gnus-configure-windows 'article)
5126     (select-window (get-buffer-window gnus-article-buffer))))
5127
5128 (defun gnus-summary-universal-argument (arg)
5129   "Perform any operation on all articles that are process/prefixed."
5130   (interactive "P")
5131   (let ((articles (gnus-summary-work-articles arg))
5132         func article)
5133     (if (eq
5134          (setq
5135           func
5136           (key-binding
5137            (read-key-sequence
5138             (substitute-command-keys
5139              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5140              ))))
5141          'undefined)
5142         (gnus-error 1 "Undefined key")
5143       (save-excursion
5144         (while articles
5145           (gnus-summary-goto-subject (setq article (pop articles)))
5146           (let (gnus-newsgroup-processable)
5147             (command-execute func))
5148           (gnus-summary-remove-process-mark article)))))
5149   (gnus-summary-position-point))
5150
5151 (defun gnus-summary-toggle-truncation (&optional arg)
5152   "Toggle truncation of summary lines.
5153 With arg, turn line truncation on iff arg is positive."
5154   (interactive "P")
5155   (setq truncate-lines
5156         (if (null arg) (not truncate-lines)
5157           (> (prefix-numeric-value arg) 0)))
5158   (redraw-display))
5159
5160 (defun gnus-summary-reselect-current-group (&optional all rescan)
5161   "Exit and then reselect the current newsgroup.
5162 The prefix argument ALL means to select all articles."
5163   (interactive "P")
5164   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5165     (error "Ephemeral groups can't be reselected"))
5166   (let ((current-subject (gnus-summary-article-number))
5167         (group gnus-newsgroup-name))
5168     (setq gnus-newsgroup-begin nil)
5169     (gnus-summary-exit)
5170     ;; We have to adjust the point of group mode buffer because
5171     ;; point was moved to the next unread newsgroup by exiting.
5172     (gnus-summary-jump-to-group group)
5173     (when rescan
5174       (save-excursion
5175         (gnus-group-get-new-news-this-group 1)))
5176     (gnus-group-read-group all t)
5177     (gnus-summary-goto-subject current-subject nil t)))
5178
5179 (defun gnus-summary-rescan-group (&optional all)
5180   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5181   (interactive "P")
5182   (gnus-summary-reselect-current-group all t))
5183
5184 (defun gnus-summary-update-info (&optional non-destructive)
5185   (save-excursion
5186     (let ((group gnus-newsgroup-name))
5187       (when group
5188         (when gnus-newsgroup-kill-headers
5189           (setq gnus-newsgroup-killed
5190                 (gnus-compress-sequence
5191                  (nconc
5192                   (gnus-set-sorted-intersection
5193                    (gnus-uncompress-range gnus-newsgroup-killed)
5194                    (setq gnus-newsgroup-unselected
5195                          (sort gnus-newsgroup-unselected '<)))
5196                   (setq gnus-newsgroup-unreads
5197                         (sort gnus-newsgroup-unreads '<)))
5198                  t)))
5199         (unless (listp (cdr gnus-newsgroup-killed))
5200           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5201         (let ((headers gnus-newsgroup-headers))
5202           ;; Set the new ranges of read articles.
5203           (save-excursion
5204             (set-buffer gnus-group-buffer)
5205             (gnus-undo-force-boundary))
5206           (gnus-update-read-articles
5207            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5208           ;; Set the current article marks.
5209           (let ((gnus-newsgroup-scored
5210                  (if (and (not gnus-save-score)
5211                           (not non-destructive))
5212                      nil
5213                    gnus-newsgroup-scored)))
5214             (save-excursion
5215               (gnus-update-marks)))
5216           ;; Do the cross-ref thing.
5217           (when gnus-use-cross-reference
5218             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5219           ;; Do not switch windows but change the buffer to work.
5220           (set-buffer gnus-group-buffer)
5221           (unless (gnus-ephemeral-group-p group)
5222             (gnus-group-update-group group)))))))
5223
5224 (defun gnus-summary-save-newsrc (&optional force)
5225   "Save the current number of read/marked articles in the dribble buffer.
5226 The dribble buffer will then be saved.
5227 If FORCE (the prefix), also save the .newsrc file(s)."
5228   (interactive "P")
5229   (gnus-summary-update-info t)
5230   (if force
5231       (gnus-save-newsrc-file)
5232     (gnus-dribble-save)))
5233
5234 (defun gnus-summary-exit (&optional temporary)
5235   "Exit reading current newsgroup, and then return to group selection mode.
5236 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5237   (interactive)
5238   (gnus-set-global-variables)
5239   (gnus-kill-save-kill-buffer)
5240   (gnus-async-halt-prefetch)
5241   (let* ((group gnus-newsgroup-name)
5242          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5243          (mode major-mode)
5244          (group-point nil)
5245          (buf (current-buffer)))
5246     (unless quit-config
5247       ;; Do adaptive scoring, and possibly save score files.
5248       (when gnus-newsgroup-adaptive
5249         (gnus-score-adaptive))
5250       (when gnus-use-scoring
5251         (gnus-score-save)))
5252     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5253     ;; If we have several article buffers, we kill them at exit.
5254     (unless gnus-single-article-buffer
5255       (gnus-kill-buffer gnus-original-article-buffer)
5256       (setq gnus-article-current nil))
5257     (when gnus-use-cache
5258       (gnus-cache-possibly-remove-articles)
5259       (gnus-cache-save-buffers))
5260     (gnus-async-prefetch-remove-group group)
5261     (when gnus-suppress-duplicates
5262       (gnus-dup-enter-articles))
5263     (when gnus-use-trees
5264       (gnus-tree-close group))
5265     ;; Remove entries for this group.
5266     (nnmail-purge-split-history (gnus-group-real-name group))
5267     ;; Make all changes in this group permanent.
5268     (unless quit-config
5269       (gnus-run-hooks 'gnus-exit-group-hook)
5270       (gnus-summary-update-info))
5271     (gnus-close-group group)
5272     ;; Make sure where we were, and go to next newsgroup.
5273     (set-buffer gnus-group-buffer)
5274     (unless quit-config
5275       (gnus-group-jump-to-group group))
5276     (gnus-run-hooks 'gnus-summary-exit-hook)
5277     (unless (or quit-config
5278                 ;; If this group has disappeared from the summary
5279                 ;; buffer, don't skip forwards.
5280                 (not (string= group (gnus-group-group-name))))
5281       (gnus-group-next-unread-group 1))
5282     (setq group-point (point))
5283     (if temporary
5284         nil                             ;Nothing to do.
5285       ;; If we have several article buffers, we kill them at exit.
5286       (unless gnus-single-article-buffer
5287         (gnus-kill-buffer gnus-article-buffer)
5288         (gnus-kill-buffer gnus-original-article-buffer)
5289         (setq gnus-article-current nil))
5290       (set-buffer buf)
5291       (if (not gnus-kill-summary-on-exit)
5292           (gnus-deaden-summary)
5293         ;; We set all buffer-local variables to nil.  It is unclear why
5294         ;; this is needed, but if we don't, buffer-local variables are
5295         ;; not garbage-collected, it seems.  This would the lead to en
5296         ;; ever-growing Emacs.
5297         (gnus-summary-clear-local-variables)
5298         (when (get-buffer gnus-article-buffer)
5299           (bury-buffer gnus-article-buffer))
5300         ;; We clear the global counterparts of the buffer-local
5301         ;; variables as well, just to be on the safe side.
5302         (set-buffer gnus-group-buffer)
5303         (gnus-summary-clear-local-variables)
5304         ;; Return to group mode buffer.
5305         (when (eq mode 'gnus-summary-mode)
5306           (gnus-kill-buffer buf)))
5307       (setq gnus-current-select-method gnus-select-method)
5308       (pop-to-buffer gnus-group-buffer)
5309       (if (not quit-config)
5310           (progn
5311             (goto-char group-point)
5312             (gnus-configure-windows 'group 'force))
5313         (gnus-handle-ephemeral-exit quit-config))
5314       ;; Clear the current group name.
5315       (unless quit-config
5316         (setq gnus-newsgroup-name nil)))))
5317
5318 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5319 (defun gnus-summary-exit-no-update (&optional no-questions)
5320   "Quit reading current newsgroup without updating read article info."
5321   (interactive)
5322   (let* ((group gnus-newsgroup-name)
5323          (quit-config (gnus-group-quit-config group)))
5324     (when (or no-questions
5325               gnus-expert-user
5326               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5327       (gnus-async-halt-prefetch)
5328       (mapcar 'funcall
5329               (delq 'gnus-summary-expire-articles
5330                     (copy-list gnus-summary-prepare-exit-hook)))
5331       ;; If we have several article buffers, we kill them at exit.
5332       (unless gnus-single-article-buffer
5333         (gnus-kill-buffer gnus-article-buffer)
5334         (gnus-kill-buffer gnus-original-article-buffer)
5335         (setq gnus-article-current nil))
5336       (if (not gnus-kill-summary-on-exit)
5337           (gnus-deaden-summary)
5338         (gnus-close-group group)
5339         (gnus-summary-clear-local-variables)
5340         (set-buffer gnus-group-buffer)
5341         (gnus-summary-clear-local-variables)
5342         (when (get-buffer gnus-summary-buffer)
5343           (kill-buffer gnus-summary-buffer)))
5344       (unless gnus-single-article-buffer
5345         (setq gnus-article-current nil))
5346       (when gnus-use-trees
5347         (gnus-tree-close group))
5348       (gnus-async-prefetch-remove-group group)
5349       (when (get-buffer gnus-article-buffer)
5350         (bury-buffer gnus-article-buffer))
5351       ;; Return to the group buffer.
5352       (gnus-configure-windows 'group 'force)
5353       ;; Clear the current group name.
5354       (setq gnus-newsgroup-name nil)
5355       (when (equal (gnus-group-group-name) group)
5356         (gnus-group-next-unread-group 1))
5357       (when quit-config
5358         (gnus-handle-ephemeral-exit quit-config)))))
5359
5360 (defun gnus-handle-ephemeral-exit (quit-config)
5361   "Handle movement when leaving an ephemeral group.
5362 The state which existed when entering the ephemeral is reset."
5363   (if (not (buffer-name (car quit-config)))
5364       (gnus-configure-windows 'group 'force)
5365     (set-buffer (car quit-config))
5366     (cond ((eq major-mode 'gnus-summary-mode)
5367            (gnus-set-global-variables))
5368           ((eq major-mode 'gnus-article-mode)
5369            (save-excursion
5370              ;; The `gnus-summary-buffer' variable may point
5371              ;; to the old summary buffer when using a single
5372              ;; article buffer.
5373              (unless (gnus-buffer-live-p gnus-summary-buffer)
5374                (set-buffer gnus-group-buffer))
5375              (set-buffer gnus-summary-buffer)
5376              (gnus-set-global-variables))))
5377     (if (or (eq (cdr quit-config) 'article)
5378             (eq (cdr quit-config) 'pick))
5379         (progn
5380           ;; The current article may be from the ephemeral group
5381           ;; thus it is best that we reload this article
5382           (gnus-summary-show-article)
5383           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5384               (gnus-configure-windows 'pick 'force)
5385             (gnus-configure-windows (cdr quit-config) 'force)))
5386       (gnus-configure-windows (cdr quit-config) 'force))
5387     (when (eq major-mode 'gnus-summary-mode)
5388       (gnus-summary-next-subject 1 nil t)
5389       (gnus-summary-recenter)
5390       (gnus-summary-position-point))))
5391
5392 (defun gnus-summary-preview-mime-message ()
5393   "MIME decode and play this message."
5394   (interactive)
5395   (let ((gnus-break-pages nil)
5396         (gnus-show-mime t))
5397     (gnus-summary-select-article gnus-show-all-headers t))
5398   (select-window (get-buffer-window gnus-article-buffer)))
5399
5400 ;;; Dead summaries.
5401
5402 (defvar gnus-dead-summary-mode-map nil)
5403
5404 (unless gnus-dead-summary-mode-map
5405   (setq gnus-dead-summary-mode-map (make-keymap))
5406   (suppress-keymap gnus-dead-summary-mode-map)
5407   (substitute-key-definition
5408    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5409   (let ((keys '("\C-d" "\r" "\177" [delete])))
5410     (while keys
5411       (define-key gnus-dead-summary-mode-map
5412         (pop keys) 'gnus-summary-wake-up-the-dead))))
5413
5414 (defvar gnus-dead-summary-mode nil
5415   "Minor mode for Gnus summary buffers.")
5416
5417 (defun gnus-dead-summary-mode (&optional arg)
5418   "Minor mode for Gnus summary buffers."
5419   (interactive "P")
5420   (when (eq major-mode 'gnus-summary-mode)
5421     (make-local-variable 'gnus-dead-summary-mode)
5422     (setq gnus-dead-summary-mode
5423           (if (null arg) (not gnus-dead-summary-mode)
5424             (> (prefix-numeric-value arg) 0)))
5425     (when gnus-dead-summary-mode
5426       (gnus-add-minor-mode
5427        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5428
5429 (defun gnus-deaden-summary ()
5430   "Make the current summary buffer into a dead summary buffer."
5431   ;; Kill any previous dead summary buffer.
5432   (when (and gnus-dead-summary
5433              (buffer-name gnus-dead-summary))
5434     (save-excursion
5435       (set-buffer gnus-dead-summary)
5436       (when gnus-dead-summary-mode
5437         (kill-buffer (current-buffer)))))
5438   ;; Make this the current dead summary.
5439   (setq gnus-dead-summary (current-buffer))
5440   (gnus-dead-summary-mode 1)
5441   (let ((name (buffer-name)))
5442     (when (string-match "Summary" name)
5443       (rename-buffer
5444        (concat (substring name 0 (match-beginning 0)) "Dead "
5445                (substring name (match-beginning 0)))
5446        t))))
5447
5448 (defun gnus-kill-or-deaden-summary (buffer)
5449   "Kill or deaden the summary BUFFER."
5450   (save-excursion
5451     (when (and (buffer-name buffer)
5452                (not gnus-single-article-buffer))
5453       (save-excursion
5454         (set-buffer buffer)
5455         (gnus-kill-buffer gnus-article-buffer)
5456         (gnus-kill-buffer gnus-original-article-buffer)))
5457     (cond (gnus-kill-summary-on-exit
5458            (when (and gnus-use-trees
5459                       (gnus-buffer-exists-p buffer))
5460              (save-excursion
5461                (set-buffer buffer)
5462                (gnus-tree-close gnus-newsgroup-name)))
5463            (gnus-kill-buffer buffer))
5464           ((gnus-buffer-exists-p buffer)
5465            (save-excursion
5466              (set-buffer buffer)
5467              (gnus-deaden-summary))))))
5468
5469 (defun gnus-summary-wake-up-the-dead (&rest args)
5470   "Wake up the dead summary buffer."
5471   (interactive)
5472   (gnus-dead-summary-mode -1)
5473   (let ((name (buffer-name)))
5474     (when (string-match "Dead " name)
5475       (rename-buffer
5476        (concat (substring name 0 (match-beginning 0))
5477                (substring name (match-end 0)))
5478        t)))
5479   (gnus-message 3 "This dead summary is now alive again"))
5480
5481 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5482 (defun gnus-summary-fetch-faq (&optional faq-dir)
5483   "Fetch the FAQ for the current group.
5484 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5485 in."
5486   (interactive
5487    (list
5488     (when current-prefix-arg
5489       (completing-read
5490        "Faq dir: " (and (listp gnus-group-faq-directory)
5491                         (mapcar (lambda (file) (list file))
5492                                 gnus-group-faq-directory))))))
5493   (let (gnus-faq-buffer)
5494     (when (setq gnus-faq-buffer
5495                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5496       (gnus-configure-windows 'summary-faq))))
5497
5498 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5499 (defun gnus-summary-describe-group (&optional force)
5500   "Describe the current newsgroup."
5501   (interactive "P")
5502   (gnus-group-describe-group force gnus-newsgroup-name))
5503
5504 (defun gnus-summary-describe-briefly ()
5505   "Describe summary mode commands briefly."
5506   (interactive)
5507   (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")))
5508
5509 ;; Walking around group mode buffer from summary mode.
5510
5511 (defun gnus-summary-next-group (&optional no-article target-group backward)
5512   "Exit current newsgroup and then select next unread newsgroup.
5513 If prefix argument NO-ARTICLE is non-nil, no article is selected
5514 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5515 previous group instead."
5516   (interactive "P")
5517   ;; Stop pre-fetching.
5518   (gnus-async-halt-prefetch)
5519   (let ((current-group gnus-newsgroup-name)
5520         (current-buffer (current-buffer))
5521         entered)
5522     ;; First we semi-exit this group to update Xrefs and all variables.
5523     ;; We can't do a real exit, because the window conf must remain
5524     ;; the same in case the user is prompted for info, and we don't
5525     ;; want the window conf to change before that...
5526     (gnus-summary-exit t)
5527     (while (not entered)
5528       ;; Then we find what group we are supposed to enter.
5529       (set-buffer gnus-group-buffer)
5530       (gnus-group-jump-to-group current-group)
5531       (setq target-group
5532             (or target-group
5533                 (if (eq gnus-keep-same-level 'best)
5534                     (gnus-summary-best-group gnus-newsgroup-name)
5535                   (gnus-summary-search-group backward gnus-keep-same-level))))
5536       (if (not target-group)
5537           ;; There are no further groups, so we return to the group
5538           ;; buffer.
5539           (progn
5540             (gnus-message 5 "Returning to the group buffer")
5541             (setq entered t)
5542             (when (gnus-buffer-live-p current-buffer)
5543               (set-buffer current-buffer)
5544               (gnus-summary-exit))
5545             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5546         ;; We try to enter the target group.
5547         (gnus-group-jump-to-group target-group)
5548         (let ((unreads (gnus-group-group-unread)))
5549           (if (and (or (eq t unreads)
5550                        (and unreads (not (zerop unreads))))
5551                    (gnus-summary-read-group
5552                     target-group nil no-article
5553                     (and (buffer-name current-buffer) current-buffer)
5554                     nil backward))
5555               (setq entered t)
5556             (setq current-group target-group
5557                   target-group nil)))))))
5558
5559 (defun gnus-summary-prev-group (&optional no-article)
5560   "Exit current newsgroup and then select previous unread newsgroup.
5561 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5562   (interactive "P")
5563   (gnus-summary-next-group no-article nil t))
5564
5565 ;; Walking around summary lines.
5566
5567 (defun gnus-summary-first-subject (&optional unread undownloaded)
5568   "Go to the first unread subject.
5569 If UNREAD is non-nil, go to the first unread article.
5570 Returns the article selected or nil if there are no unread articles."
5571   (interactive "P")
5572   (prog1
5573       (cond
5574        ;; Empty summary.
5575        ((null gnus-newsgroup-data)
5576         (gnus-message 3 "No articles in the group")
5577         nil)
5578        ;; Pick the first article.
5579        ((not unread)
5580         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5581         (gnus-data-number (car gnus-newsgroup-data)))
5582        ;; No unread articles.
5583        ((null gnus-newsgroup-unreads)
5584         (gnus-message 3 "No more unread articles")
5585         nil)
5586        ;; Find the first unread article.
5587        (t
5588         (let ((data gnus-newsgroup-data))
5589           (while (and data
5590                       (and (not (and undownloaded
5591                                      (eq gnus-undownloaded-mark
5592                                          (gnus-data-mark (car data)))))
5593                            (not (gnus-data-unread-p (car data)))))
5594             (setq data (cdr data)))
5595           (when data
5596             (goto-char (gnus-data-pos (car data)))
5597             (gnus-data-number (car data))))))
5598     (gnus-summary-position-point)))
5599
5600 (defun gnus-summary-next-subject (n &optional unread dont-display)
5601   "Go to next N'th summary line.
5602 If N is negative, go to the previous N'th subject line.
5603 If UNREAD is non-nil, only unread articles are selected.
5604 The difference between N and the actual number of steps taken is
5605 returned."
5606   (interactive "p")
5607   (let ((backward (< n 0))
5608         (n (abs n)))
5609     (while (and (> n 0)
5610                 (if backward
5611                     (gnus-summary-find-prev unread)
5612                   (gnus-summary-find-next unread)))
5613       (gnus-summary-show-thread)
5614       (setq n (1- n)))
5615     (when (/= 0 n)
5616       (gnus-message 7 "No more%s articles"
5617                     (if unread " unread" "")))
5618     (unless dont-display
5619       (gnus-summary-recenter)
5620       (gnus-summary-position-point))
5621     n))
5622
5623 (defun gnus-summary-next-unread-subject (n)
5624   "Go to next N'th unread summary line."
5625   (interactive "p")
5626   (gnus-summary-next-subject n t))
5627
5628 (defun gnus-summary-prev-subject (n &optional unread)
5629   "Go to previous N'th summary line.
5630 If optional argument UNREAD is non-nil, only unread article is selected."
5631   (interactive "p")
5632   (gnus-summary-next-subject (- n) unread))
5633
5634 (defun gnus-summary-prev-unread-subject (n)
5635   "Go to previous N'th unread summary line."
5636   (interactive "p")
5637   (gnus-summary-next-subject (- n) t))
5638
5639 (defun gnus-summary-goto-subject (article &optional force silent)
5640   "Go the subject line of ARTICLE.
5641 If FORCE, also allow jumping to articles not currently shown."
5642   (interactive "nArticle number: ")
5643   (let ((b (point))
5644         (data (gnus-data-find article)))
5645     ;; We read in the article if we have to.
5646     (and (not data)
5647          force
5648          (gnus-summary-insert-subject
5649           article
5650           (if (or (numberp force) (vectorp force)) force)
5651           t)
5652          (setq data (gnus-data-find article)))
5653     (goto-char b)
5654     (if (not data)
5655         (progn
5656           (unless silent
5657             (gnus-message 3 "Can't find article %d" article))
5658           nil)
5659       (goto-char (gnus-data-pos data))
5660       (gnus-summary-position-point)
5661       article)))
5662
5663 ;; Walking around summary lines with displaying articles.
5664
5665 (defun gnus-summary-expand-window (&optional arg)
5666   "Make the summary buffer take up the entire Emacs frame.
5667 Given a prefix, will force an `article' buffer configuration."
5668   (interactive "P")
5669   (if arg
5670       (gnus-configure-windows 'article 'force)
5671     (gnus-configure-windows 'summary 'force)))
5672
5673 (defun gnus-summary-display-article (article &optional all-header)
5674   "Display ARTICLE in article buffer."
5675   (gnus-set-global-variables)
5676   (if (null article)
5677       nil
5678     (prog1
5679         (if gnus-summary-display-article-function
5680             (funcall gnus-summary-display-article-function article all-header)
5681           (gnus-article-prepare article all-header))
5682       (gnus-run-hooks 'gnus-select-article-hook)
5683       (when (and gnus-current-article
5684                  (not (zerop gnus-current-article)))
5685         (gnus-summary-goto-subject gnus-current-article))
5686       (gnus-summary-recenter)
5687       (when (and gnus-use-trees gnus-show-threads)
5688         (gnus-possibly-generate-tree article)
5689         (gnus-highlight-selected-tree article))
5690       ;; Successfully display article.
5691       (gnus-article-set-window-start
5692        (cdr (assq article gnus-newsgroup-bookmarks))))))
5693
5694 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5695   "Select the current article.
5696 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5697 non-nil, the article will be re-fetched even if it already present in
5698 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5699 be displayed."
5700   ;; Make sure we are in the summary buffer to work around bbdb bug.
5701   (unless (eq major-mode 'gnus-summary-mode)
5702     (set-buffer gnus-summary-buffer))
5703   (let ((article (or article (gnus-summary-article-number)))
5704         (all-headers (not (not all-headers))) ;Must be T or NIL.
5705         gnus-summary-display-article-function
5706         did)
5707     (and (not pseudo)
5708          (gnus-summary-article-pseudo-p article)
5709          (error "This is a pseudo-article"))
5710     (prog1
5711         (save-excursion
5712           (set-buffer gnus-summary-buffer)
5713           (if (or (and gnus-single-article-buffer
5714                        (or (null gnus-current-article)
5715                            (null gnus-article-current)
5716                            (null (get-buffer gnus-article-buffer))
5717                            (not (eq article (cdr gnus-article-current)))
5718                            (not (equal (car gnus-article-current)
5719                                        gnus-newsgroup-name))))
5720                   (and (not gnus-single-article-buffer)
5721                        (or (null gnus-current-article)
5722                            (not (eq gnus-current-article article))))
5723                   force)
5724               ;; The requested article is different from the current article.
5725               (prog1
5726                   (gnus-summary-display-article article all-headers)
5727                 (setq did article)
5728                 (when (or all-headers gnus-show-all-headers)
5729                   (gnus-article-show-all-headers)))
5730             (when (or all-headers gnus-show-all-headers)
5731               (gnus-article-show-all-headers))
5732             'old))
5733       (when did
5734         (gnus-article-set-window-start
5735          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5736
5737 (defun gnus-summary-set-current-mark (&optional current-mark)
5738   "Obsolete function."
5739   nil)
5740
5741 (defun gnus-summary-next-article (&optional unread subject backward push)
5742   "Select the next article.
5743 If UNREAD, only unread articles are selected.
5744 If SUBJECT, only articles with SUBJECT are selected.
5745 If BACKWARD, the previous article is selected instead of the next."
5746   (interactive "P")
5747   (cond
5748    ;; Is there such an article?
5749    ((and (gnus-summary-search-forward unread subject backward)
5750          (or (gnus-summary-display-article (gnus-summary-article-number))
5751              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5752     (gnus-summary-position-point))
5753    ;; If not, we try the first unread, if that is wanted.
5754    ((and subject
5755          gnus-auto-select-same
5756          (gnus-summary-first-unread-article))
5757     (gnus-summary-position-point)
5758     (gnus-message 6 "Wrapped"))
5759    ;; Try to get next/previous article not displayed in this group.
5760    ((and gnus-auto-extend-newsgroup
5761          (not unread) (not subject))
5762     (gnus-summary-goto-article
5763      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5764      nil (count-lines (point-min) (point))))
5765    ;; Go to next/previous group.
5766    (t
5767     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5768       (gnus-summary-jump-to-group gnus-newsgroup-name))
5769     (let ((cmd last-command-char)
5770           (point
5771            (save-excursion
5772              (set-buffer gnus-group-buffer)
5773              (point)))
5774           (group
5775            (if (eq gnus-keep-same-level 'best)
5776                (gnus-summary-best-group gnus-newsgroup-name)
5777              (gnus-summary-search-group backward gnus-keep-same-level))))
5778       ;; For some reason, the group window gets selected.  We change
5779       ;; it back.
5780       (select-window (get-buffer-window (current-buffer)))
5781       ;; Select next unread newsgroup automagically.
5782       (cond
5783        ((or (not gnus-auto-select-next)
5784             (not cmd))
5785         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5786        ((or (eq gnus-auto-select-next 'quietly)
5787             (and (eq gnus-auto-select-next 'slightly-quietly)
5788                  push)
5789             (and (eq gnus-auto-select-next 'almost-quietly)
5790                  (gnus-summary-last-article-p)))
5791         ;; Select quietly.
5792         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5793             (gnus-summary-exit)
5794           (gnus-message 7 "No more%s articles (%s)..."
5795                         (if unread " unread" "")
5796                         (if group (concat "selecting " group)
5797                           "exiting"))
5798           (gnus-summary-next-group nil group backward)))
5799        (t
5800         (when (gnus-key-press-event-p last-input-event)
5801           (gnus-summary-walk-group-buffer
5802            gnus-newsgroup-name cmd unread backward point))))))))
5803
5804 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5805   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5806                       (?\C-p (gnus-group-prev-unread-group 1))))
5807         (cursor-in-echo-area t)
5808         keve key group ended)
5809     (save-excursion
5810       (set-buffer gnus-group-buffer)
5811       (goto-char start)
5812       (setq group
5813             (if (eq gnus-keep-same-level 'best)
5814                 (gnus-summary-best-group gnus-newsgroup-name)
5815               (gnus-summary-search-group backward gnus-keep-same-level))))
5816     (while (not ended)
5817       (gnus-message
5818        5 "No more%s articles%s" (if unread " unread" "")
5819        (if (and group
5820                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5821            (format " (Type %s for %s [%s])"
5822                    (single-key-description cmd) group
5823                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5824          (format " (Type %s to exit %s)"
5825                  (single-key-description cmd)
5826                  gnus-newsgroup-name)))
5827       ;; Confirm auto selection.
5828       (setq key (car (setq keve (gnus-read-event-char))))
5829       (setq ended t)
5830       (cond
5831        ((assq key keystrokes)
5832         (let ((obuf (current-buffer)))
5833           (switch-to-buffer gnus-group-buffer)
5834           (when group
5835             (gnus-group-jump-to-group group))
5836           (eval (cadr (assq key keystrokes)))
5837           (setq group (gnus-group-group-name))
5838           (switch-to-buffer obuf))
5839         (setq ended nil))
5840        ((equal key cmd)
5841         (if (or (not group)
5842                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5843             (gnus-summary-exit)
5844           (gnus-summary-next-group nil group backward)))
5845        (t
5846         (push (cdr keve) unread-command-events))))))
5847
5848 (defun gnus-summary-next-unread-article ()
5849   "Select unread article after current one."
5850   (interactive)
5851   (gnus-summary-next-article
5852    (or (not (eq gnus-summary-goto-unread 'never))
5853        (gnus-summary-last-article-p (gnus-summary-article-number)))
5854    (and gnus-auto-select-same
5855         (gnus-summary-article-subject))))
5856
5857 (defun gnus-summary-prev-article (&optional unread subject)
5858   "Select the article after the current one.
5859 If UNREAD is non-nil, only unread articles are selected."
5860   (interactive "P")
5861   (gnus-summary-next-article unread subject t))
5862
5863 (defun gnus-summary-prev-unread-article ()
5864   "Select unread article before current one."
5865   (interactive)
5866   (gnus-summary-prev-article
5867    (or (not (eq gnus-summary-goto-unread 'never))
5868        (gnus-summary-first-article-p (gnus-summary-article-number)))
5869    (and gnus-auto-select-same
5870         (gnus-summary-article-subject))))
5871
5872 (defun gnus-summary-next-page (&optional lines circular)
5873   "Show next page of the selected article.
5874 If at the end of the current article, select the next article.
5875 LINES says how many lines should be scrolled up.
5876
5877 If CIRCULAR is non-nil, go to the start of the article instead of
5878 selecting the next article when reaching the end of the current
5879 article."
5880   (interactive "P")
5881   (setq gnus-summary-buffer (current-buffer))
5882   (gnus-set-global-variables)
5883   (let ((article (gnus-summary-article-number))
5884         (article-window (get-buffer-window gnus-article-buffer t))
5885         endp)
5886     ;; If the buffer is empty, we have no article.
5887     (unless article
5888       (error "No article to select"))
5889     (gnus-configure-windows 'article)
5890     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5891         (if (and (eq gnus-summary-goto-unread 'never)
5892                  (not (gnus-summary-last-article-p article)))
5893             (gnus-summary-next-article)
5894           (gnus-summary-next-unread-article))
5895       (if (or (null gnus-current-article)
5896               (null gnus-article-current)
5897               (/= article (cdr gnus-article-current))
5898               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5899           ;; Selected subject is different from current article's.
5900           (gnus-summary-display-article article)
5901         (when article-window
5902           (gnus-eval-in-buffer-window gnus-article-buffer
5903             (setq endp (gnus-article-next-page lines)))
5904           (when endp
5905             (cond (circular
5906                    (gnus-summary-beginning-of-article))
5907                   (lines
5908                    (gnus-message 3 "End of message"))
5909                   ((null lines)
5910                    (if (and (eq gnus-summary-goto-unread 'never)
5911                             (not (gnus-summary-last-article-p article)))
5912                        (gnus-summary-next-article)
5913                      (gnus-summary-next-unread-article))))))))
5914     (gnus-summary-recenter)
5915     (gnus-summary-position-point)))
5916
5917 (defun gnus-summary-prev-page (&optional lines move)
5918   "Show previous page of selected article.
5919 Argument LINES specifies lines to be scrolled down.
5920 If MOVE, move to the previous unread article if point is at
5921 the beginning of the buffer."
5922   (interactive "P")
5923   (let ((article (gnus-summary-article-number))
5924         (article-window (get-buffer-window gnus-article-buffer t))
5925         endp)
5926     (gnus-configure-windows 'article)
5927     (if (or (null gnus-current-article)
5928             (null gnus-article-current)
5929             (/= article (cdr gnus-article-current))
5930             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5931         ;; Selected subject is different from current article's.
5932         (gnus-summary-display-article article)
5933       (gnus-summary-recenter)
5934       (when article-window
5935         (gnus-eval-in-buffer-window gnus-article-buffer
5936           (setq endp (gnus-article-prev-page lines)))
5937         (when (and move endp)
5938           (cond (lines
5939                  (gnus-message 3 "Beginning of message"))
5940                 ((null lines)
5941                  (if (and (eq gnus-summary-goto-unread 'never)
5942                           (not (gnus-summary-first-article-p article)))
5943                      (gnus-summary-prev-article)
5944                    (gnus-summary-prev-unread-article))))))))
5945   (gnus-summary-position-point))
5946
5947 (defun gnus-summary-prev-page-or-article (&optional lines)
5948   "Show previous page of selected article.
5949 Argument LINES specifies lines to be scrolled down.
5950 If at the beginning of the article, go to the next article."
5951   (interactive "P")
5952   (gnus-summary-prev-page lines t))
5953
5954 (defun gnus-summary-scroll-up (lines)
5955   "Scroll up (or down) one line current article.
5956 Argument LINES specifies lines to be scrolled up (or down if negative)."
5957   (interactive "p")
5958   (gnus-configure-windows 'article)
5959   (gnus-summary-show-thread)
5960   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5961     (gnus-eval-in-buffer-window gnus-article-buffer
5962       (cond ((> lines 0)
5963              (when (gnus-article-next-page lines)
5964                (gnus-message 3 "End of message")))
5965             ((< lines 0)
5966              (gnus-article-prev-page (- lines))))))
5967   (gnus-summary-recenter)
5968   (gnus-summary-position-point))
5969
5970 (defun gnus-summary-scroll-down (lines)
5971   "Scroll down (or up) one line current article.
5972 Argument LINES specifies lines to be scrolled down (or up if negative)."
5973   (interactive "p")
5974   (gnus-summary-scroll-up (- lines)))
5975
5976 (defun gnus-summary-next-same-subject ()
5977   "Select next article which has the same subject as current one."
5978   (interactive)
5979   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5980
5981 (defun gnus-summary-prev-same-subject ()
5982   "Select previous article which has the same subject as current one."
5983   (interactive)
5984   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5985
5986 (defun gnus-summary-next-unread-same-subject ()
5987   "Select next unread article which has the same subject as current one."
5988   (interactive)
5989   (gnus-summary-next-article t (gnus-summary-article-subject)))
5990
5991 (defun gnus-summary-prev-unread-same-subject ()
5992   "Select previous unread article which has the same subject as current one."
5993   (interactive)
5994   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5995
5996 (defun gnus-summary-first-unread-article ()
5997   "Select the first unread article.
5998 Return nil if there are no unread articles."
5999   (interactive)
6000   (prog1
6001       (when (gnus-summary-first-subject t)
6002         (gnus-summary-show-thread)
6003         (gnus-summary-first-subject t)
6004         (gnus-summary-display-article (gnus-summary-article-number)))
6005     (gnus-summary-position-point)))
6006
6007 (defun gnus-summary-first-unread-subject ()
6008   "Place the point on the subject line of the first unread article.
6009 Return nil if there are no unread articles."
6010   (interactive)
6011   (prog1
6012       (when (gnus-summary-first-subject t)
6013         (gnus-summary-show-thread)
6014         (gnus-summary-first-subject t))
6015     (gnus-summary-position-point)))
6016
6017 (defun gnus-summary-first-article ()
6018   "Select the first article.
6019 Return nil if there are no articles."
6020   (interactive)
6021   (prog1
6022       (when (gnus-summary-first-subject)
6023         (gnus-summary-show-thread)
6024         (gnus-summary-first-subject)
6025         (gnus-summary-display-article (gnus-summary-article-number)))
6026     (gnus-summary-position-point)))
6027
6028 (defun gnus-summary-best-unread-article ()
6029   "Select the unread article with the highest score."
6030   (interactive)
6031   (let ((best -1000000)
6032         (data gnus-newsgroup-data)
6033         article score)
6034     (while data
6035       (and (gnus-data-unread-p (car data))
6036            (> (setq score
6037                     (gnus-summary-article-score (gnus-data-number (car data))))
6038               best)
6039            (setq best score
6040                  article (gnus-data-number (car data))))
6041       (setq data (cdr data)))
6042     (prog1
6043         (if article
6044             (gnus-summary-goto-article article)
6045           (error "No unread articles"))
6046       (gnus-summary-position-point))))
6047
6048 (defun gnus-summary-last-subject ()
6049   "Go to the last displayed subject line in the group."
6050   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6051     (when article
6052       (gnus-summary-goto-subject article))))
6053
6054 (defun gnus-summary-goto-article (article &optional all-headers force)
6055   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6056 If ALL-HEADERS is non-nil, no header lines are hidden.
6057 If FORCE, go to the article even if it isn't displayed.  If FORCE
6058 is a number, it is the line the article is to be displayed on."
6059   (interactive
6060    (list
6061     (completing-read
6062      "Article number or Message-ID: "
6063      (mapcar (lambda (number) (list (int-to-string number)))
6064              gnus-newsgroup-limit))
6065     current-prefix-arg
6066     t))
6067   (prog1
6068       (if (and (stringp article)
6069                (string-match "@" article))
6070           (gnus-summary-refer-article article)
6071         (when (stringp article)
6072           (setq article (string-to-number article)))
6073         (if (gnus-summary-goto-subject article force)
6074             (gnus-summary-display-article article all-headers)
6075           (gnus-message 4 "Couldn't go to article %s" article) nil))
6076     (gnus-summary-position-point)))
6077
6078 (defun gnus-summary-goto-last-article ()
6079   "Go to the previously read article."
6080   (interactive)
6081   (prog1
6082       (when gnus-last-article
6083         (gnus-summary-goto-article gnus-last-article nil t))
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-pop-article (number)
6087   "Pop one article off the history and go to the previous.
6088 NUMBER articles will be popped off."
6089   (interactive "p")
6090   (let (to)
6091     (setq gnus-newsgroup-history
6092           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6093     (if to
6094         (gnus-summary-goto-article (car to) nil t)
6095       (error "Article history empty")))
6096   (gnus-summary-position-point))
6097
6098 ;; Summary commands and functions for limiting the summary buffer.
6099
6100 (defun gnus-summary-limit-to-articles (n)
6101   "Limit the summary buffer to the next N articles.
6102 If not given a prefix, use the process marked articles instead."
6103   (interactive "P")
6104   (prog1
6105       (let ((articles (gnus-summary-work-articles n)))
6106         (setq gnus-newsgroup-processable nil)
6107         (gnus-summary-limit articles))
6108     (gnus-summary-position-point)))
6109
6110 (defun gnus-summary-pop-limit (&optional total)
6111   "Restore the previous limit.
6112 If given a prefix, remove all limits."
6113   (interactive "P")
6114   (when total
6115     (setq gnus-newsgroup-limits
6116           (list (mapcar (lambda (h) (mail-header-number h))
6117                         gnus-newsgroup-headers))))
6118   (unless gnus-newsgroup-limits
6119     (error "No limit to pop"))
6120   (prog1
6121       (gnus-summary-limit nil 'pop)
6122     (gnus-summary-position-point)))
6123
6124 (defun gnus-summary-limit-to-subject (subject &optional header)
6125   "Limit the summary buffer to articles that have subjects that match a regexp."
6126   (interactive "sLimit to subject (regexp): ")
6127   (unless header
6128     (setq header "subject"))
6129   (when (not (equal "" subject))
6130     (prog1
6131         (let ((articles (gnus-summary-find-matching
6132                          (or header "subject") subject 'all)))
6133           (unless articles
6134             (error "Found no matches for \"%s\"" subject))
6135           (gnus-summary-limit articles))
6136       (gnus-summary-position-point))))
6137
6138 (defun gnus-summary-limit-to-author (from)
6139   "Limit the summary buffer to articles that have authors that match a regexp."
6140   (interactive "sLimit to author (regexp): ")
6141   (gnus-summary-limit-to-subject from "from"))
6142
6143 (defun gnus-summary-limit-to-age (age &optional younger-p)
6144   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6145 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6146 articles that are younger than AGE days."
6147   (interactive "nLimit to articles older than (in days): \nP")
6148   (prog1
6149       (let ((data gnus-newsgroup-data)
6150             (cutoff (days-to-time age))
6151             articles d date is-younger)
6152         (while (setq d (pop data))
6153           (when (and (vectorp (gnus-data-header d))
6154                      (setq date (mail-header-date (gnus-data-header d))))
6155             (setq is-younger (time-less-p
6156                               (time-since (date-to-time date))
6157                               cutoff))
6158             (when (if younger-p
6159                       is-younger
6160                     (not is-younger))
6161               (push (gnus-data-number d) articles))))
6162         (gnus-summary-limit (nreverse articles)))
6163     (gnus-summary-position-point)))
6164
6165 (defun gnus-summary-limit-to-extra (header regexp)
6166   "Limit the summary buffer to articles that match an 'extra' header."
6167   (interactive
6168    (let ((header
6169           (intern
6170            (gnus-completing-read
6171             (symbol-name (car gnus-extra-headers))      
6172             "Limit extra header:"       
6173             (mapcar (lambda (x) 
6174                       (cons (symbol-name x) x))
6175                     gnus-extra-headers)
6176             nil                 
6177             t))))
6178      (list header
6179            (read-string (format "Limit to header %s (regexp): " header)))))
6180   (when (not (equal "" regexp))
6181     (prog1
6182         (let ((articles (gnus-summary-find-matching
6183                          (cons 'extra header) regexp 'all)))
6184           (unless articles
6185             (error "Found no matches for \"%s\"" regexp))
6186           (gnus-summary-limit articles))
6187       (gnus-summary-position-point))))
6188
6189 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6190 (make-obsolete
6191  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6192
6193 (defun gnus-summary-limit-to-unread (&optional all)
6194   "Limit the summary buffer to articles that are not marked as read.
6195 If ALL is non-nil, limit strictly to unread articles."
6196   (interactive "P")
6197   (if all
6198       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6199     (gnus-summary-limit-to-marks
6200      ;; Concat all the marks that say that an article is read and have
6201      ;; those removed.
6202      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6203            gnus-killed-mark gnus-kill-file-mark
6204            gnus-low-score-mark gnus-expirable-mark
6205            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6206            gnus-duplicate-mark gnus-souped-mark)
6207      'reverse)))
6208
6209 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6210 (make-obsolete 'gnus-summary-delete-marked-with
6211                'gnus-summary-limit-exlude-marks)
6212
6213 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6214   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6215 If REVERSE, limit the summary buffer to articles that are marked
6216 with MARKS.  MARKS can either be a string of marks or a list of marks.
6217 Returns how many articles were removed."
6218   (interactive "sMarks: ")
6219   (gnus-summary-limit-to-marks marks t))
6220
6221 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6222   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6223 If REVERSE (the prefix), limit the summary buffer to articles that are
6224 not marked with MARKS.  MARKS can either be a string of marks or a
6225 list of marks.
6226 Returns how many articles were removed."
6227   (interactive "sMarks: \nP")
6228   (prog1
6229       (let ((data gnus-newsgroup-data)
6230             (marks (if (listp marks) marks
6231                      (append marks nil))) ; Transform to list.
6232             articles)
6233         (while data
6234           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6235                   (memq (gnus-data-mark (car data)) marks))
6236             (push (gnus-data-number (car data)) articles))
6237           (setq data (cdr data)))
6238         (gnus-summary-limit articles))
6239     (gnus-summary-position-point)))
6240
6241 (defun gnus-summary-limit-to-score (&optional score)
6242   "Limit to articles with score at or above SCORE."
6243   (interactive "P")
6244   (setq score (if score
6245                   (prefix-numeric-value score)
6246                 (or gnus-summary-default-score 0)))
6247   (let ((data gnus-newsgroup-data)
6248         articles)
6249     (while data
6250       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6251                 score)
6252         (push (gnus-data-number (car data)) articles))
6253       (setq data (cdr data)))
6254     (prog1
6255         (gnus-summary-limit articles)
6256       (gnus-summary-position-point))))
6257
6258 (defun gnus-summary-limit-include-thread (id)
6259   "Display all the hidden articles that in the current thread."
6260   (interactive (list (mail-header-id (gnus-summary-article-header))))
6261   (let ((articles (gnus-articles-in-thread
6262                    (gnus-id-to-thread (gnus-root-id id)))))
6263     (prog1
6264         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6265       (gnus-summary-position-point))))
6266
6267 (defun gnus-summary-limit-include-dormant ()
6268   "Display all the hidden articles that are marked as dormant.
6269 Note that this command only works on a subset of the articles currently
6270 fetched for this group."
6271   (interactive)
6272   (unless gnus-newsgroup-dormant
6273     (error "There are no dormant articles in this group"))
6274   (prog1
6275       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6276     (gnus-summary-position-point)))
6277
6278 (defun gnus-summary-limit-exclude-dormant ()
6279   "Hide all dormant articles."
6280   (interactive)
6281   (prog1
6282       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6283     (gnus-summary-position-point)))
6284
6285 (defun gnus-summary-limit-exclude-childless-dormant ()
6286   "Hide all dormant articles that have no children."
6287   (interactive)
6288   (let ((data (gnus-data-list t))
6289         articles d children)
6290     ;; Find all articles that are either not dormant or have
6291     ;; children.
6292     (while (setq d (pop data))
6293       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6294                 (and (setq children
6295                            (gnus-article-children (gnus-data-number d)))
6296                      (let (found)
6297                        (while children
6298                          (when (memq (car children) articles)
6299                            (setq children nil
6300                                  found t))
6301                          (pop children))
6302                        found)))
6303         (push (gnus-data-number d) articles)))
6304     ;; Do the limiting.
6305     (prog1
6306         (gnus-summary-limit articles)
6307       (gnus-summary-position-point))))
6308
6309 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6310   "Mark all unread excluded articles as read.
6311 If ALL, mark even excluded ticked and dormants as read."
6312   (interactive "P")
6313   (let ((articles (gnus-sorted-complement
6314                    (sort
6315                     (mapcar (lambda (h) (mail-header-number h))
6316                             gnus-newsgroup-headers)
6317                     '<)
6318                    (sort gnus-newsgroup-limit '<)))
6319         article)
6320     (setq gnus-newsgroup-unreads
6321           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6322     (if all
6323         (setq gnus-newsgroup-dormant nil
6324               gnus-newsgroup-marked nil
6325               gnus-newsgroup-reads
6326               (nconc
6327                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6328                gnus-newsgroup-reads))
6329       (while (setq article (pop articles))
6330         (unless (or (memq article gnus-newsgroup-dormant)
6331                     (memq article gnus-newsgroup-marked))
6332           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6333
6334 (defun gnus-summary-limit (articles &optional pop)
6335   (if pop
6336       ;; We pop the previous limit off the stack and use that.
6337       (setq articles (car gnus-newsgroup-limits)
6338             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6339     ;; We use the new limit, so we push the old limit on the stack.
6340     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6341   ;; Set the limit.
6342   (setq gnus-newsgroup-limit articles)
6343   (let ((total (length gnus-newsgroup-data))
6344         (data (gnus-data-find-list (gnus-summary-article-number)))
6345         (gnus-summary-mark-below nil)   ; Inhibit this.
6346         found)
6347     ;; This will do all the work of generating the new summary buffer
6348     ;; according to the new limit.
6349     (gnus-summary-prepare)
6350     ;; Hide any threads, possibly.
6351     (and gnus-show-threads
6352          gnus-thread-hide-subtree
6353          (gnus-summary-hide-all-threads))
6354     ;; Try to return to the article you were at, or one in the
6355     ;; neighborhood.
6356     (when data
6357       ;; We try to find some article after the current one.
6358       (while data
6359         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6360           (setq data nil
6361                 found t))
6362         (setq data (cdr data))))
6363     (unless found
6364       ;; If there is no data, that means that we were after the last
6365       ;; article.  The same goes when we can't find any articles
6366       ;; after the current one.
6367       (goto-char (point-max))
6368       (gnus-summary-find-prev))
6369     (gnus-set-mode-line 'summary)
6370     ;; We return how many articles were removed from the summary
6371     ;; buffer as a result of the new limit.
6372     (- total (length gnus-newsgroup-data))))
6373
6374 (defsubst gnus-invisible-cut-children (threads)
6375   (let ((num 0))
6376     (while threads
6377       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6378         (incf num))
6379       (pop threads))
6380     (< num 2)))
6381
6382 (defsubst gnus-cut-thread (thread)
6383   "Go forwards in the thread until we find an article that we want to display."
6384   (when (or (eq gnus-fetch-old-headers 'some)
6385             (eq gnus-fetch-old-headers 'invisible)
6386             (eq gnus-build-sparse-threads 'some)
6387             (eq gnus-build-sparse-threads 'more))
6388     ;; Deal with old-fetched headers and sparse threads.
6389     (while (and
6390             thread
6391             (or
6392              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6393              (gnus-summary-article-ancient-p
6394               (mail-header-number (car thread))))
6395             (if (or (<= (length (cdr thread)) 1)
6396                     (eq gnus-fetch-old-headers 'invisible))
6397                 (setq gnus-newsgroup-limit
6398                       (delq (mail-header-number (car thread))
6399                             gnus-newsgroup-limit)
6400                       thread (cadr thread))
6401               (when (gnus-invisible-cut-children (cdr thread))
6402                 (let ((th (cdr thread)))
6403                   (while th
6404                     (if (memq (mail-header-number (caar th))
6405                               gnus-newsgroup-limit)
6406                         (setq thread (car th)
6407                               th nil)
6408                       (setq th (cdr th))))))))))
6409   thread)
6410
6411 (defun gnus-cut-threads (threads)
6412   "Cut off all uninteresting articles from the beginning of threads."
6413   (when (or (eq gnus-fetch-old-headers 'some)
6414             (eq gnus-fetch-old-headers 'invisible)
6415             (eq gnus-build-sparse-threads 'some)
6416             (eq gnus-build-sparse-threads 'more))
6417     (let ((th threads))
6418       (while th
6419         (setcar th (gnus-cut-thread (car th)))
6420         (setq th (cdr th)))))
6421   ;; Remove nixed out threads.
6422   (delq nil threads))
6423
6424 (defun gnus-summary-initial-limit (&optional show-if-empty)
6425   "Figure out what the initial limit is supposed to be on group entry.
6426 This entails weeding out unwanted dormants, low-scored articles,
6427 fetch-old-headers verbiage, and so on."
6428   ;; Most groups have nothing to remove.
6429   (if (or gnus-inhibit-limiting
6430           (and (null gnus-newsgroup-dormant)
6431                (not (eq gnus-fetch-old-headers 'some))
6432                (not (eq gnus-fetch-old-headers 'invisible))
6433                (null gnus-summary-expunge-below)
6434                (not (eq gnus-build-sparse-threads 'some))
6435                (not (eq gnus-build-sparse-threads 'more))
6436                (null gnus-thread-expunge-below)
6437                (not gnus-use-nocem)))
6438       ()                                ; Do nothing.
6439     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6440     (setq gnus-newsgroup-limit nil)
6441     (mapatoms
6442      (lambda (node)
6443        (unless (car (symbol-value node))
6444          ;; These threads have no parents -- they are roots.
6445          (let ((nodes (cdr (symbol-value node)))
6446                thread)
6447            (while nodes
6448              (if (and gnus-thread-expunge-below
6449                       (< (gnus-thread-total-score (car nodes))
6450                          gnus-thread-expunge-below))
6451                  (gnus-expunge-thread (pop nodes))
6452                (setq thread (pop nodes))
6453                (gnus-summary-limit-children thread))))))
6454      gnus-newsgroup-dependencies)
6455     ;; If this limitation resulted in an empty group, we might
6456     ;; pop the previous limit and use it instead.
6457     (when (and (not gnus-newsgroup-limit)
6458                show-if-empty)
6459       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6460     gnus-newsgroup-limit))
6461
6462 (defun gnus-summary-limit-children (thread)
6463   "Return 1 if this subthread is visible and 0 if it is not."
6464   ;; First we get the number of visible children to this thread.  This
6465   ;; is done by recursing down the thread using this function, so this
6466   ;; will really go down to a leaf article first, before slowly
6467   ;; working its way up towards the root.
6468   (when thread
6469     (let ((children
6470            (if (cdr thread)
6471                (apply '+ (mapcar 'gnus-summary-limit-children
6472                                  (cdr thread)))
6473              0))
6474           (number (mail-header-number (car thread)))
6475           score)
6476       (if (and
6477            (not (memq number gnus-newsgroup-marked))
6478            (or
6479             ;; If this article is dormant and has absolutely no visible
6480             ;; children, then this article isn't visible.
6481             (and (memq number gnus-newsgroup-dormant)
6482                  (zerop children))
6483             ;; If this is "fetch-old-headered" and there is no
6484             ;; visible children, then we don't want this article.
6485             (and (eq gnus-fetch-old-headers 'some)
6486                  (gnus-summary-article-ancient-p number)
6487                  (zerop children))
6488             ;; If this is "fetch-old-headered" and `invisible', then
6489             ;; we don't want this article.
6490             (and (eq gnus-fetch-old-headers 'invisible)
6491                  (gnus-summary-article-ancient-p number))
6492             ;; If this is a sparsely inserted article with no children,
6493             ;; we don't want it.
6494             (and (eq gnus-build-sparse-threads 'some)
6495                  (gnus-summary-article-sparse-p number)
6496                  (zerop children))
6497             ;; If we use expunging, and this article is really
6498             ;; low-scored, then we don't want this article.
6499             (when (and gnus-summary-expunge-below
6500                        (< (setq score
6501                                 (or (cdr (assq number gnus-newsgroup-scored))
6502                                     gnus-summary-default-score))
6503                           gnus-summary-expunge-below))
6504               ;; We increase the expunge-tally here, but that has
6505               ;; nothing to do with the limits, really.
6506               (incf gnus-newsgroup-expunged-tally)
6507               ;; We also mark as read here, if that's wanted.
6508               (when (and gnus-summary-mark-below
6509                          (< score gnus-summary-mark-below))
6510                 (setq gnus-newsgroup-unreads
6511                       (delq number gnus-newsgroup-unreads))
6512                 (if gnus-newsgroup-auto-expire
6513                     (push number gnus-newsgroup-expirable)
6514                   (push (cons number gnus-low-score-mark)
6515                         gnus-newsgroup-reads)))
6516               t)
6517             ;; Check NoCeM things.
6518             (if (and gnus-use-nocem
6519                      (gnus-nocem-unwanted-article-p
6520                       (mail-header-id (car thread))))
6521                 (progn
6522                   (setq gnus-newsgroup-unreads
6523                         (delq number gnus-newsgroup-unreads))
6524                   t))))
6525           ;; Nope, invisible article.
6526           0
6527         ;; Ok, this article is to be visible, so we add it to the limit
6528         ;; and return 1.
6529         (push number gnus-newsgroup-limit)
6530         1))))
6531
6532 (defun gnus-expunge-thread (thread)
6533   "Mark all articles in THREAD as read."
6534   (let* ((number (mail-header-number (car thread))))
6535     (incf gnus-newsgroup-expunged-tally)
6536     ;; We also mark as read here, if that's wanted.
6537     (setq gnus-newsgroup-unreads
6538           (delq number gnus-newsgroup-unreads))
6539     (if gnus-newsgroup-auto-expire
6540         (push number gnus-newsgroup-expirable)
6541       (push (cons number gnus-low-score-mark)
6542             gnus-newsgroup-reads)))
6543   ;; Go recursively through all subthreads.
6544   (mapcar 'gnus-expunge-thread (cdr thread)))
6545
6546 ;; Summary article oriented commands
6547
6548 (defun gnus-summary-refer-parent-article (n)
6549   "Refer parent article N times.
6550 If N is negative, go to ancestor -N instead.
6551 The difference between N and the number of articles fetched is returned."
6552   (interactive "p")
6553   (let ((skip 1)
6554         error header ref)
6555     (when (not (natnump n))
6556       (setq skip (abs n)
6557             n 1))
6558     (while (and (> n 0)
6559                 (not error))
6560       (setq header (gnus-summary-article-header))
6561       (if (and (eq (mail-header-number header)
6562                    (cdr gnus-article-current))
6563                (equal gnus-newsgroup-name
6564                       (car gnus-article-current)))
6565           ;; If we try to find the parent of the currently
6566           ;; displayed article, then we take a look at the actual
6567           ;; References header, since this is slightly more
6568           ;; reliable than the References field we got from the
6569           ;; server.
6570           (save-excursion
6571             (set-buffer gnus-original-article-buffer)
6572             (nnheader-narrow-to-headers)
6573             (unless (setq ref (message-fetch-field "references"))
6574               (setq ref (message-fetch-field "in-reply-to")))
6575             (widen))
6576         (setq ref
6577               ;; It's not the current article, so we take a bet on
6578               ;; the value we got from the server.
6579               (mail-header-references header)))
6580       (if (and ref
6581                (not (equal ref "")))
6582           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6583             (gnus-message 1 "Couldn't find parent"))
6584         (gnus-message 1 "No references in article %d"
6585                       (gnus-summary-article-number))
6586         (setq error t))
6587       (decf n))
6588     (gnus-summary-position-point)
6589     n))
6590
6591 (defun gnus-summary-refer-references ()
6592   "Fetch all articles mentioned in the References header.
6593 Return the number of articles fetched."
6594   (interactive)
6595   (let ((ref (mail-header-references (gnus-summary-article-header)))
6596         (current (gnus-summary-article-number))
6597         (n 0))
6598     (if (or (not ref)
6599             (equal ref ""))
6600         (error "No References in the current article")
6601       ;; For each Message-ID in the References header...
6602       (while (string-match "<[^>]*>" ref)
6603         (incf n)
6604         ;; ... fetch that article.
6605         (gnus-summary-refer-article
6606          (prog1 (match-string 0 ref)
6607            (setq ref (substring ref (match-end 0))))))
6608       (gnus-summary-goto-subject current)
6609       (gnus-summary-position-point)
6610       n)))
6611
6612 (defun gnus-summary-refer-thread (&optional limit)
6613   "Fetch all articles in the current thread.
6614 If LIMIT (the numerical prefix), fetch that many old headers instead
6615 of what's specified by the `gnus-refer-thread-limit' variable."
6616   (interactive "P")
6617   (let ((id (mail-header-id (gnus-summary-article-header)))
6618         (limit (if limit (prefix-numeric-value limit)
6619                  gnus-refer-thread-limit)))
6620     ;; We want to fetch LIMIT *old* headers, but we also have to
6621     ;; re-fetch all the headers in the current buffer, because many of
6622     ;; them may be undisplayed.  So we adjust LIMIT.
6623     (when (numberp limit)
6624       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6625     (unless (eq gnus-fetch-old-headers 'invisible)
6626       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6627       ;; Retrieve the headers and read them in.
6628       (if (eq (gnus-retrieve-headers
6629                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6630               'nov)
6631           (gnus-build-all-threads)
6632         (error "Can't fetch thread from backends that don't support NOV"))
6633       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6634     (gnus-summary-limit-include-thread id)))
6635
6636 (defun gnus-summary-refer-article (message-id &optional arg)
6637   "Fetch an article specified by MESSAGE-ID.
6638 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6639 or `gnus-select-method', no matter what backend the article comes from."
6640   (interactive "sMessage-ID: \nP")
6641   (when (and (stringp message-id)
6642              (not (zerop (length message-id))))
6643     ;; Construct the correct Message-ID if necessary.
6644     ;; Suggested by tale@pawl.rpi.edu.
6645     (unless (string-match "^<" message-id)
6646       (setq message-id (concat "<" message-id)))
6647     (unless (string-match ">$" message-id)
6648       (setq message-id (concat message-id ">")))
6649     (let* ((header (gnus-id-to-header message-id))
6650            (sparse (and header
6651                         (gnus-summary-article-sparse-p
6652                          (mail-header-number header))
6653                         (memq (mail-header-number header)
6654                               gnus-newsgroup-limit))))
6655       (cond
6656        ;; If the article is present in the buffer we just go to it.
6657        ((and header
6658              (or (not (gnus-summary-article-sparse-p
6659                        (mail-header-number header)))
6660                  sparse))
6661         (prog1
6662             (gnus-summary-goto-article
6663              (mail-header-number header) nil t)
6664           (when sparse
6665             (gnus-summary-update-article (mail-header-number header)))))
6666        (t
6667         ;; We fetch the article
6668         (let ((gnus-override-method
6669                (cond ((gnus-news-group-p gnus-newsgroup-name)
6670                       gnus-refer-article-method)
6671                      (arg
6672                       (or gnus-refer-article-method gnus-select-method))
6673                      (t nil)))
6674               number)
6675           ;; Start the special refer-article method, if necessary.
6676           (when (and gnus-refer-article-method
6677                      (gnus-news-group-p gnus-newsgroup-name))
6678             (gnus-check-server gnus-refer-article-method))
6679           ;; Fetch the header, and display the article.
6680           (if (setq number (gnus-summary-insert-subject message-id))
6681               (gnus-summary-select-article nil nil nil number)
6682             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6683
6684 (defun gnus-summary-edit-parameters ()
6685   "Edit the group parameters of the current group."
6686   (interactive)
6687   (gnus-group-edit-group gnus-newsgroup-name 'params))
6688
6689 (defun gnus-summary-customize-parameters ()
6690   "Customize the group parameters of the current group."
6691   (interactive)
6692   (gnus-group-customize gnus-newsgroup-name))
6693
6694 (defun gnus-summary-enter-digest-group (&optional force)
6695   "Enter an nndoc group based on the current article.
6696 If FORCE, force a digest interpretation.  If not, try
6697 to guess what the document format is."
6698   (interactive "P")
6699   (let ((conf gnus-current-window-configuration))
6700     (save-excursion
6701       (gnus-summary-select-article))
6702     (setq gnus-current-window-configuration conf)
6703     (let* ((name (format "%s-%d"
6704                          (gnus-group-prefixed-name
6705                           gnus-newsgroup-name (list 'nndoc ""))
6706                          (save-excursion
6707                            (set-buffer gnus-summary-buffer)
6708                            gnus-current-article)))
6709            (ogroup gnus-newsgroup-name)
6710            (params (append (gnus-info-params (gnus-get-info ogroup))
6711                            (list (cons 'to-group ogroup))
6712                            (list (cons 'save-article-group ogroup))))
6713            (case-fold-search t)
6714            (buf (current-buffer))
6715            dig)
6716       (save-excursion
6717         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6718         (insert-buffer-substring gnus-original-article-buffer)
6719         ;; Remove lines that may lead nndoc to misinterpret the
6720         ;; document type.
6721         (narrow-to-region
6722          (goto-char (point-min))
6723          (or (search-forward "\n\n" nil t) (point)))
6724         (goto-char (point-min))
6725         (delete-matching-lines "^Path:\\|^From ")
6726         (widen))
6727       (unwind-protect
6728           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6729                     (gnus-newsgroup-ephemeral-ignored-charsets
6730                      gnus-newsgroup-ignored-charsets))
6731                 (gnus-group-read-ephemeral-group
6732                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6733                               (nndoc-article-type
6734                                ,(if force 'digest 'guess))) t))
6735               ;; Make all postings to this group go to the parent group.
6736               (nconc (gnus-info-params (gnus-get-info name))
6737                      params)
6738             ;; Couldn't select this doc group.
6739             (switch-to-buffer buf)
6740             (gnus-set-global-variables)
6741             (gnus-configure-windows 'summary)
6742             (gnus-message 3 "Article couldn't be entered?"))
6743         (kill-buffer dig)))))
6744
6745 (defun gnus-summary-read-document (n)
6746   "Open a new group based on the current article(s).
6747 This will allow you to read digests and other similar
6748 documents as newsgroups.
6749 Obeys the standard process/prefix convention."
6750   (interactive "P")
6751   (let* ((articles (gnus-summary-work-articles n))
6752          (ogroup gnus-newsgroup-name)
6753          (params (append (gnus-info-params (gnus-get-info ogroup))
6754                          (list (cons 'to-group ogroup))))
6755          article group egroup groups vgroup)
6756     (while (setq article (pop articles))
6757       (setq group (format "%s-%d" gnus-newsgroup-name article))
6758       (gnus-summary-remove-process-mark article)
6759       (when (gnus-summary-display-article article)
6760         (save-excursion
6761           (with-temp-buffer
6762             (insert-buffer-substring gnus-original-article-buffer)
6763             ;; Remove some headers that may lead nndoc to make
6764             ;; the wrong guess.
6765             (message-narrow-to-head)
6766             (goto-char (point-min))
6767             (delete-matching-lines "^\\(Path\\):\\|^From ")
6768             (widen)
6769             (if (setq egroup
6770                       (gnus-group-read-ephemeral-group
6771                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6772                                      (nndoc-article-type guess))
6773                        t nil t))
6774                 (progn
6775                   ;; Make all postings to this group go to the parent group.
6776                   (nconc (gnus-info-params (gnus-get-info egroup))
6777                          params)
6778                   (push egroup groups))
6779               ;; Couldn't select this doc group.
6780               (gnus-error 3 "Article couldn't be entered"))))))
6781     ;; Now we have selected all the documents.
6782     (cond
6783      ((not groups)
6784       (error "None of the articles could be interpreted as documents"))
6785      ((gnus-group-read-ephemeral-group
6786        (setq vgroup (format
6787                      "nnvirtual:%s-%s" gnus-newsgroup-name
6788                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6789        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6790        t
6791        (cons (current-buffer) 'summary)))
6792      (t
6793       (error "Couldn't select virtual nndoc group")))))
6794
6795 (defun gnus-summary-isearch-article (&optional regexp-p)
6796   "Do incremental search forward on the current article.
6797 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6798   (interactive "P")
6799   (gnus-summary-select-article)
6800   (gnus-configure-windows 'article)
6801   (gnus-eval-in-buffer-window gnus-article-buffer
6802     (save-restriction
6803       (widen)
6804       (isearch-forward regexp-p))))
6805
6806 (defun gnus-summary-search-article-forward (regexp &optional backward)
6807   "Search for an article containing REGEXP forward.
6808 If BACKWARD, search backward instead."
6809   (interactive
6810    (list (read-string
6811           (format "Search article %s (regexp%s): "
6812                   (if current-prefix-arg "backward" "forward")
6813                   (if gnus-last-search-regexp
6814                       (concat ", default " gnus-last-search-regexp)
6815                     "")))
6816          current-prefix-arg))
6817   (if (string-equal regexp "")
6818       (setq regexp (or gnus-last-search-regexp ""))
6819     (setq gnus-last-search-regexp regexp))
6820   (if (gnus-summary-search-article regexp backward)
6821       (gnus-summary-show-thread)
6822     (error "Search failed: \"%s\"" regexp)))
6823
6824 (defun gnus-summary-search-article-backward (regexp)
6825   "Search for an article containing REGEXP backward."
6826   (interactive
6827    (list (read-string
6828           (format "Search article backward (regexp%s): "
6829                   (if gnus-last-search-regexp
6830                       (concat ", default " gnus-last-search-regexp)
6831                     "")))))
6832   (gnus-summary-search-article-forward regexp 'backward))
6833
6834 (defun gnus-summary-search-article (regexp &optional backward)
6835   "Search for an article containing REGEXP.
6836 Optional argument BACKWARD means do search for backward.
6837 `gnus-select-article-hook' is not called during the search."
6838   ;; We have to require this here to make sure that the following
6839   ;; dynamic binding isn't shadowed by autoloading.
6840   (require 'gnus-async)
6841   (require 'gnus-art)
6842   (let ((gnus-select-article-hook nil)  ;Disable hook.
6843         (gnus-article-display-hook nil)
6844         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6845         (gnus-use-article-prefetch nil)
6846         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6847         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6848         (sum (current-buffer))
6849         (found nil)
6850         point)
6851     (gnus-save-hidden-threads
6852       (gnus-summary-select-article)
6853       (set-buffer gnus-article-buffer)
6854       (when backward
6855         (forward-line -1))
6856       (while (not found)
6857         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6858         (if (if backward
6859                 (re-search-backward regexp nil t)
6860               (re-search-forward regexp nil t))
6861             ;; We found the regexp.
6862             (progn
6863               (setq found 'found)
6864               (beginning-of-line)
6865               (set-window-start
6866                (get-buffer-window (current-buffer))
6867                (point))
6868               (forward-line 1)
6869               (set-buffer sum)
6870               (setq point (point)))
6871           ;; We didn't find it, so we go to the next article.
6872           (set-buffer sum)
6873           (setq found 'not)
6874           (while (eq found 'not)
6875             (if (not (if backward (gnus-summary-find-prev)
6876                        (gnus-summary-find-next)))
6877                 ;; No more articles.
6878                 (setq found t)
6879               ;; Select the next article and adjust point.
6880               (unless (gnus-summary-article-sparse-p
6881                        (gnus-summary-article-number))
6882                 (setq found nil)
6883                 (gnus-summary-select-article)
6884                 (set-buffer gnus-article-buffer)
6885                 (widen)
6886                 (goto-char (if backward (point-max) (point-min))))))))
6887       (gnus-message 7 ""))
6888     ;; Return whether we found the regexp.
6889     (when (eq found 'found)
6890       (goto-char point)
6891       (gnus-summary-show-thread)
6892       (gnus-summary-goto-subject gnus-current-article)
6893       (gnus-summary-position-point)
6894       t)))
6895
6896 (defun gnus-summary-find-matching (header regexp &optional backward unread
6897                                           not-case-fold)
6898   "Return a list of all articles that match REGEXP on HEADER.
6899 The search stars on the current article and goes forwards unless
6900 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6901 If UNREAD is non-nil, only unread articles will
6902 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6903 in the comparisons."
6904   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6905                 (gnus-data-find-list
6906                  (gnus-summary-article-number) (gnus-data-list backward))))
6907         (case-fold-search (not not-case-fold))
6908         articles d func)
6909     (if (consp header)
6910         (if (eq (car header) 'extra)
6911             (setq func
6912                   `(lambda (h)
6913                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6914                          "")))
6915           (error "%s is an invalid header" header))
6916       (unless (fboundp (intern (concat "mail-header-" header)))
6917         (error "%s is not a valid header" header))
6918       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6919     (while data
6920       (setq d (car data))
6921       (and (or (not unread)             ; We want all articles...
6922                (gnus-data-unread-p d))  ; Or just unreads.
6923            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6924            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6925            (push (gnus-data-number d) articles)) ; Success!
6926       (setq data (cdr data)))
6927     (nreverse articles)))
6928
6929 (defun gnus-summary-execute-command (header regexp command &optional backward)
6930   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6931 If HEADER is an empty string (or nil), the match is done on the entire
6932 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6933   (interactive
6934    (list (let ((completion-ignore-case t))
6935            (completing-read
6936             "Header name: "
6937             (mapcar (lambda (string) (list string))
6938                     '("Number" "Subject" "From" "Lines" "Date"
6939                       "Message-ID" "Xref" "References" "Body"))
6940             nil 'require-match))
6941          (read-string "Regexp: ")
6942          (read-key-sequence "Command: ")
6943          current-prefix-arg))
6944   (when (equal header "Body")
6945     (setq header ""))
6946   ;; Hidden thread subtrees must be searched as well.
6947   (gnus-summary-show-all-threads)
6948   ;; We don't want to change current point nor window configuration.
6949   (save-excursion
6950     (save-window-excursion
6951       (gnus-message 6 "Executing %s..." (key-description command))
6952       ;; We'd like to execute COMMAND interactively so as to give arguments.
6953       (gnus-execute header regexp
6954                     `(call-interactively ',(key-binding command))
6955                     backward)
6956       (gnus-message 6 "Executing %s...done" (key-description command)))))
6957
6958 (defun gnus-summary-beginning-of-article ()
6959   "Scroll the article back to the beginning."
6960   (interactive)
6961   (gnus-summary-select-article)
6962   (gnus-configure-windows 'article)
6963   (gnus-eval-in-buffer-window gnus-article-buffer
6964     (widen)
6965     (goto-char (point-min))
6966     (when gnus-page-broken
6967       (gnus-narrow-to-page))))
6968
6969 (defun gnus-summary-end-of-article ()
6970   "Scroll to the end of the article."
6971   (interactive)
6972   (gnus-summary-select-article)
6973   (gnus-configure-windows 'article)
6974   (gnus-eval-in-buffer-window gnus-article-buffer
6975     (widen)
6976     (goto-char (point-max))
6977     (recenter -3)
6978     (when gnus-page-broken
6979       (gnus-narrow-to-page))))
6980
6981 (defun gnus-summary-print-article (&optional filename n)
6982   "Generate and print a PostScript image of the N next (mail) articles.
6983
6984 If N is negative, print the N previous articles.  If N is nil and articles
6985 have been marked with the process mark, print these instead.
6986
6987 If the optional first argument FILENAME is nil, send the image to the
6988 printer.  If FILENAME is a string, save the PostScript image in a file with
6989 that name.  If FILENAME is a number, prompt the user for the name of the file
6990 to save in."
6991   (interactive (list (ps-print-preprint current-prefix-arg)
6992                      current-prefix-arg))
6993   (dolist (article (gnus-summary-work-articles n))
6994     (gnus-summary-select-article nil nil 'pseudo article)
6995     (gnus-eval-in-buffer-window gnus-article-buffer
6996       (let ((buffer (generate-new-buffer " *print*")))
6997         (unwind-protect
6998             (progn
6999               (copy-to-buffer buffer (point-min) (point-max))
7000               (set-buffer buffer)
7001               (gnus-article-delete-invisible-text)
7002               (let ((ps-left-header
7003                      (list
7004                       (concat "("
7005                               (mail-header-subject gnus-current-headers) ")")
7006                       (concat "("
7007                               (mail-header-from gnus-current-headers) ")")))
7008                     (ps-right-header
7009                      (list
7010                       "/pagenumberstring load"
7011                       (concat "("
7012                               (mail-header-date gnus-current-headers) ")"))))
7013                 (gnus-run-hooks 'gnus-ps-print-hook)
7014                 (save-excursion
7015                   (ps-print-buffer-with-faces filename))))
7016           (kill-buffer buffer))))))
7017
7018 (defun gnus-summary-show-article (&optional arg)
7019   "Force re-fetching of the current article.
7020 If ARG (the prefix) is non-nil, show the raw article without any
7021 article massaging functions being run."
7022   (interactive "P")
7023   (if (not arg)
7024       ;; Select the article the normal way.
7025       (gnus-summary-select-article nil 'force)
7026     ;; We have to require this here to make sure that the following
7027     ;; dynamic binding isn't shadowed by autoloading.
7028     (require 'gnus-async)
7029     (require 'gnus-art)
7030     ;; Bind the article treatment functions to nil.
7031     (let ((gnus-have-all-headers t)
7032           gnus-article-display-hook
7033           gnus-article-prepare-hook
7034           gnus-article-decode-hook
7035           gnus-break-pages
7036           gnus-show-mime
7037           gnus-visual)
7038       (gnus-summary-select-article nil 'force)))
7039   (gnus-summary-goto-subject gnus-current-article)
7040   (gnus-summary-position-point))
7041
7042 (defun gnus-summary-verbose-headers (&optional arg)
7043   "Toggle permanent full header display.
7044 If ARG is a positive number, turn header display on.
7045 If ARG is a negative number, turn header display off."
7046   (interactive "P")
7047   (setq gnus-show-all-headers
7048         (cond ((or (not (numberp arg))
7049                    (zerop arg))
7050                (not gnus-show-all-headers))
7051               ((natnump arg)
7052                t)))
7053   (gnus-summary-show-article))
7054
7055 (defun gnus-summary-toggle-header (&optional arg)
7056   "Show the headers if they are hidden, or hide them if they are shown.
7057 If ARG is a positive number, show the entire header.
7058 If ARG is a negative number, hide the unwanted header lines."
7059   (interactive "P")
7060   (save-excursion
7061     (set-buffer gnus-article-buffer)
7062     (save-restriction
7063       (let* ((buffer-read-only nil)
7064              (inhibit-point-motion-hooks t)
7065              hidden e)
7066         (save-restriction 
7067           (article-narrow-to-head)
7068           (setq hidden (gnus-article-hidden-text-p 'headers)))
7069         (goto-char (point-min))
7070         (when (search-forward "\n\n" nil t)
7071           (delete-region (point-min) (1- (point))))
7072         (goto-char (point-min))
7073         (save-excursion
7074           (set-buffer gnus-original-article-buffer)
7075           (goto-char (point-min))
7076           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7077         (insert-buffer-substring gnus-original-article-buffer 1 e)
7078         (save-restriction
7079           (narrow-to-region (point-min) (point))
7080           (article-decode-encoded-words)
7081           (if (or hidden
7082                   (and (numberp arg) (< arg 0)))
7083               (let ((gnus-treat-hide-headers nil)
7084                     (gnus-treat-hide-boring-headers nil))
7085                 (gnus-treat-article 'head))
7086             (gnus-treat-article 'head)))))))
7087
7088 (defun gnus-summary-show-all-headers ()
7089   "Make all header lines visible."
7090   (interactive)
7091   (gnus-article-show-all-headers))
7092
7093 (defun gnus-summary-toggle-mime (&optional arg)
7094   "Toggle MIME processing.
7095 If ARG is a positive number, turn MIME processing on."
7096   (interactive "P")
7097   (setq gnus-show-mime
7098         (if (null arg)
7099             (not gnus-show-mime)
7100           (> (prefix-numeric-value arg) 0)))
7101   (gnus-summary-select-article t 'force))
7102
7103 (defun gnus-summary-caesar-message (&optional arg)
7104   "Caesar rotate the current article by 13.
7105 The numerical prefix specifies how many places to rotate each letter
7106 forward."
7107   (interactive "P")
7108   (gnus-summary-select-article)
7109   (let ((mail-header-separator ""))
7110     (gnus-eval-in-buffer-window gnus-article-buffer
7111       (save-restriction
7112         (widen)
7113         (let ((start (window-start))
7114               buffer-read-only)
7115           (message-caesar-buffer-body arg)
7116           (set-window-start (get-buffer-window (current-buffer)) start))))))
7117
7118 (defun gnus-summary-stop-page-breaking ()
7119   "Stop page breaking in the current article."
7120   (interactive)
7121   (gnus-summary-select-article)
7122   (gnus-eval-in-buffer-window gnus-article-buffer
7123     (widen)
7124     (when (gnus-visual-p 'page-marker)
7125       (let ((buffer-read-only nil))
7126         (gnus-remove-text-with-property 'gnus-prev)
7127         (gnus-remove-text-with-property 'gnus-next))
7128       (setq gnus-page-broken nil))))
7129
7130 (defun gnus-summary-move-article (&optional n to-newsgroup
7131                                             select-method action)
7132   "Move the current article to a different newsgroup.
7133 If N is a positive number, move the N next articles.
7134 If N is a negative number, move the N previous articles.
7135 If N is nil and any articles have been marked with the process mark,
7136 move those articles instead.
7137 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7138 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7139 re-spool using this method.
7140
7141 For this function to work, both the current newsgroup and the
7142 newsgroup that you want to move to have to support the `request-move'
7143 and `request-accept' functions."
7144   (interactive "P")
7145   (unless action
7146     (setq action 'move))
7147   ;; Disable marking as read.
7148   (let (gnus-mark-article-hook)
7149     (save-window-excursion
7150       (gnus-summary-select-article)))
7151   ;; Check whether the source group supports the required functions.
7152   (cond ((and (eq action 'move)
7153               (not (gnus-check-backend-function
7154                     'request-move-article gnus-newsgroup-name)))
7155          (error "The current group does not support article moving"))
7156         ((and (eq action 'crosspost)
7157               (not (gnus-check-backend-function
7158                     'request-replace-article gnus-newsgroup-name)))
7159          (error "The current group does not support article editing")))
7160   (let ((articles (gnus-summary-work-articles n))
7161         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7162         (names '((move "Move" "Moving")
7163                  (copy "Copy" "Copying")
7164                  (crosspost "Crosspost" "Crossposting")))
7165         (copy-buf (save-excursion
7166                     (nnheader-set-temp-buffer " *copy article*")))
7167         (default-marks gnus-article-mark-lists)
7168         (no-expire-marks (delete '(expirable . expire)
7169                                  (copy-sequence gnus-article-mark-lists)))
7170         art-group to-method new-xref article to-groups)
7171     (unless (assq action names)
7172       (error "Unknown action %s" action))
7173     ;; Read the newsgroup name.
7174     (when (and (not to-newsgroup)
7175                (not select-method))
7176       (setq to-newsgroup
7177             (gnus-read-move-group-name
7178              (cadr (assq action names))
7179              (symbol-value (intern (format "gnus-current-%s-group" action)))
7180              articles prefix))
7181       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7182     (setq to-method (or select-method
7183                         (gnus-group-name-to-method to-newsgroup)))
7184     ;; Check the method we are to move this article to...
7185     (unless (gnus-check-backend-function
7186              'request-accept-article (car to-method))
7187       (error "%s does not support article copying" (car to-method)))
7188     (unless (gnus-check-server to-method)
7189       (error "Can't open server %s" (car to-method)))
7190     (gnus-message 6 "%s to %s: %s..."
7191                   (caddr (assq action names))
7192                   (or (car select-method) to-newsgroup) articles)
7193     (while articles
7194       (setq article (pop articles))
7195       (setq
7196        art-group
7197        (cond
7198         ;; Move the article.
7199         ((eq action 'move)
7200          ;; Remove this article from future suppression.
7201          (gnus-dup-unsuppress-article article)
7202          (gnus-request-move-article
7203           article                       ; Article to move
7204           gnus-newsgroup-name           ; From newsgroup
7205           (nth 1 (gnus-find-method-for-group
7206                   gnus-newsgroup-name)) ; Server
7207           (list 'gnus-request-accept-article
7208                 to-newsgroup (list 'quote select-method)
7209                 (not articles) t)               ; Accept form
7210           (not articles)))              ; Only save nov last time
7211         ;; Copy the article.
7212         ((eq action 'copy)
7213          (save-excursion
7214            (set-buffer copy-buf)
7215            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7216              (gnus-request-accept-article
7217               to-newsgroup select-method (not articles) t))))
7218         ;; Crosspost the article.
7219         ((eq action 'crosspost)
7220          (let ((xref (message-tokenize-header
7221                       (mail-header-xref (gnus-summary-article-header article))
7222                       " ")))
7223            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7224                                   ":" article))
7225            (unless xref
7226              (setq xref (list (system-name))))
7227            (setq new-xref
7228                  (concat
7229                   (mapconcat 'identity
7230                              (delete "Xref:" (delete new-xref xref))
7231                              " ")
7232                   " " new-xref))
7233            (save-excursion
7234              (set-buffer copy-buf)
7235              ;; First put the article in the destination group.
7236              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7237              (when (consp (setq art-group
7238                                 (gnus-request-accept-article
7239                                  to-newsgroup select-method (not articles))))
7240                (setq new-xref (concat new-xref " " (car art-group)
7241                                       ":" (cdr art-group)))
7242                ;; Now we have the new Xrefs header, so we insert
7243                ;; it and replace the new article.
7244                (nnheader-replace-header "Xref" new-xref)
7245                (gnus-request-replace-article
7246                 (cdr art-group) to-newsgroup (current-buffer))
7247                art-group))))))
7248       (cond
7249        ((not art-group)
7250         (gnus-message 1 "Couldn't %s article %s"
7251                       (cadr (assq action names)) article))
7252        ((and (eq art-group 'junk)
7253              (eq action 'move))
7254         (gnus-summary-mark-article article gnus-canceled-mark)
7255         (gnus-message 4 "Deleted article %s" article))
7256        (t
7257         (let* ((pto-group (gnus-group-prefixed-name
7258                            (car art-group) to-method))
7259                (entry
7260                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7261                (info (nth 2 entry))
7262                (to-group (gnus-info-group info))
7263                to-marks)
7264           ;; Update the group that has been moved to.
7265           (when (and info
7266                      (memq action '(move copy)))
7267             (unless (member to-group to-groups)
7268               (push to-group to-groups))
7269
7270             (unless (memq article gnus-newsgroup-unreads)
7271               (push 'read to-marks)
7272               (gnus-info-set-read
7273                info (gnus-add-to-range (gnus-info-read info)
7274                                        (list (cdr art-group)))))
7275
7276             ;; Copy any marks over to the new group.
7277             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7278                              default-marks
7279                            no-expire-marks))
7280                   (to-article (cdr art-group)))
7281
7282               ;; See whether the article is to be put in the cache.
7283               (when gnus-use-cache
7284                 (gnus-cache-possibly-enter-article
7285                  to-group to-article
7286                  (let ((header (copy-sequence
7287                                 (gnus-summary-article-header article))))
7288                    (mail-header-set-number header to-article)
7289                    header)
7290                  (memq article gnus-newsgroup-marked)
7291                  (memq article gnus-newsgroup-dormant)
7292                  (memq article gnus-newsgroup-unreads)))
7293
7294               (when (and (equal to-group gnus-newsgroup-name)
7295                          (not (memq article gnus-newsgroup-unreads)))
7296                 ;; Mark this article as read in this group.
7297                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7298                 (setcdr (gnus-active to-group) to-article)
7299                 (setcdr gnus-newsgroup-active to-article))
7300
7301               (while marks
7302                 (when (memq article (symbol-value
7303                                      (intern (format "gnus-newsgroup-%s"
7304                                                      (caar marks)))))
7305                   (push (cdar marks) to-marks)
7306                   ;; If the other group is the same as this group,
7307                   ;; then we have to add the mark to the list.
7308                   (when (equal to-group gnus-newsgroup-name)
7309                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7310                          (cons to-article
7311                                (symbol-value
7312                                 (intern (format "gnus-newsgroup-%s"
7313                                                 (caar marks)))))))
7314                   ;; Copy the marks to other group.
7315                   (gnus-add-marked-articles
7316                    to-group (cdar marks) (list to-article) info))
7317                 (setq marks (cdr marks)))
7318
7319               (gnus-request-set-mark to-group (list (list (list to-article)
7320                                                           'set
7321                                                           to-marks)))
7322
7323               (gnus-dribble-enter
7324                (concat "(gnus-group-set-info '"
7325                        (gnus-prin1-to-string (gnus-get-info to-group))
7326                        ")"))))
7327
7328           ;; Update the Xref header in this article to point to
7329           ;; the new crossposted article we have just created.
7330           (when (eq action 'crosspost)
7331             (save-excursion
7332               (set-buffer copy-buf)
7333               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7334               (nnheader-replace-header "Xref" new-xref)
7335               (gnus-request-replace-article
7336                article gnus-newsgroup-name (current-buffer)))))
7337
7338         ;;;!!!Why is this necessary?
7339         (set-buffer gnus-summary-buffer)
7340
7341         (gnus-summary-goto-subject article)
7342         (when (eq action 'move)
7343           (gnus-summary-mark-article article gnus-canceled-mark))))
7344       (gnus-summary-remove-process-mark article))
7345     ;; Re-activate all groups that have been moved to.
7346     (while to-groups
7347       (save-excursion
7348         (set-buffer gnus-group-buffer)
7349         (when (gnus-group-goto-group (car to-groups) t)
7350           (gnus-group-get-new-news-this-group 1 t))
7351         (pop to-groups)))
7352
7353     (gnus-kill-buffer copy-buf)
7354     (gnus-summary-position-point)
7355     (gnus-set-mode-line 'summary)))
7356
7357 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7358   "Move the current article to a different newsgroup.
7359 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7360 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7361 re-spool using this method."
7362   (interactive "P")
7363   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7364
7365 (defun gnus-summary-crosspost-article (&optional n)
7366   "Crosspost the current article to some other group."
7367   (interactive "P")
7368   (gnus-summary-move-article n nil nil 'crosspost))
7369
7370 (defcustom gnus-summary-respool-default-method nil
7371   "Default method for respooling an article.
7372 If nil, use to the current newsgroup method."
7373   :type '(choice (gnus-select-method :value (nnml ""))
7374                  (const nil))
7375   :group 'gnus-summary-mail)
7376
7377 (defun gnus-summary-respool-article (&optional n method)
7378   "Respool the current article.
7379 The article will be squeezed through the mail spooling process again,
7380 which means that it will be put in some mail newsgroup or other
7381 depending on `nnmail-split-methods'.
7382 If N is a positive number, respool the N next articles.
7383 If N is a negative number, respool the N previous articles.
7384 If N is nil and any articles have been marked with the process mark,
7385 respool those articles instead.
7386
7387 Respooling can be done both from mail groups and \"real\" newsgroups.
7388 In the former case, the articles in question will be moved from the
7389 current group into whatever groups they are destined to.  In the
7390 latter case, they will be copied into the relevant groups."
7391   (interactive
7392    (list current-prefix-arg
7393          (let* ((methods (gnus-methods-using 'respool))
7394                 (methname
7395                  (symbol-name (or gnus-summary-respool-default-method
7396                                   (car (gnus-find-method-for-group
7397                                         gnus-newsgroup-name)))))
7398                 (method
7399                  (gnus-completing-read
7400                   methname "What backend do you want to use when respooling?"
7401                   methods nil t nil 'gnus-mail-method-history))
7402                 ms)
7403            (cond
7404             ((zerop (length (setq ms (gnus-servers-using-backend
7405                                       (intern method)))))
7406              (list (intern method) ""))
7407             ((= 1 (length ms))
7408              (car ms))
7409             (t
7410              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7411                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7412                            ms-alist))))))))
7413   (unless method
7414     (error "No method given for respooling"))
7415   (if (assoc (symbol-name
7416               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7417              (gnus-methods-using 'respool))
7418       (gnus-summary-move-article n nil method)
7419     (gnus-summary-copy-article n nil method)))
7420
7421 (defun gnus-summary-import-article (file)
7422   "Import an arbitrary file into a mail newsgroup."
7423   (interactive "fImport file: ")
7424   (let ((group gnus-newsgroup-name)
7425         (now (current-time))
7426         atts lines)
7427     (unless (gnus-check-backend-function 'request-accept-article group)
7428       (error "%s does not support article importing" group))
7429     (or (file-readable-p file)
7430         (not (file-regular-p file))
7431         (error "Can't read %s" file))
7432     (save-excursion
7433       (set-buffer (gnus-get-buffer-create " *import file*"))
7434       (erase-buffer)
7435       (nnheader-insert-file-contents file)
7436       (goto-char (point-min))
7437       (unless (nnheader-article-p)
7438         ;; This doesn't look like an article, so we fudge some headers.
7439         (setq atts (file-attributes file)
7440               lines (count-lines (point-min) (point-max)))
7441         (insert "From: " (read-string "From: ") "\n"
7442                 "Subject: " (read-string "Subject: ") "\n"
7443                 "Date: " (message-make-date (nth 5 atts))
7444                 "\n"
7445                 "Message-ID: " (message-make-message-id) "\n"
7446                 "Lines: " (int-to-string lines) "\n"
7447                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7448       (gnus-request-accept-article group nil t)
7449       (kill-buffer (current-buffer)))))
7450
7451 (defun gnus-summary-article-posted-p ()
7452   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7453 This will be the case if the article has both been mailed and posted."
7454   (interactive)
7455   (let ((id (mail-header-references (gnus-summary-article-header)))
7456         (gnus-override-method
7457          (or gnus-refer-article-method gnus-select-method)))
7458     (if (gnus-request-head id "")
7459         (gnus-message 2 "The current message was found on %s"
7460                       gnus-override-method)
7461       (gnus-message 2 "The current message couldn't be found on %s"
7462                     gnus-override-method)
7463       nil)))
7464
7465 (defun gnus-summary-expire-articles (&optional now)
7466   "Expire all articles that are marked as expirable in the current group."
7467   (interactive)
7468   (when (gnus-check-backend-function
7469          'request-expire-articles gnus-newsgroup-name)
7470     ;; This backend supports expiry.
7471     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7472            (expirable (if total
7473                           (progn
7474                             ;; We need to update the info for
7475                             ;; this group for `gnus-list-of-read-articles'
7476                             ;; to give us the right answer.
7477                             (gnus-run-hooks 'gnus-exit-group-hook)
7478                             (gnus-summary-update-info)
7479                             (gnus-list-of-read-articles gnus-newsgroup-name))
7480                         (setq gnus-newsgroup-expirable
7481                               (sort gnus-newsgroup-expirable '<))))
7482            (expiry-wait (if now 'immediate
7483                           (gnus-group-find-parameter
7484                            gnus-newsgroup-name 'expiry-wait)))
7485            es)
7486       (when expirable
7487         ;; There are expirable articles in this group, so we run them
7488         ;; through the expiry process.
7489         (gnus-message 6 "Expiring articles...")
7490         (unless (gnus-check-group gnus-newsgroup-name)
7491           (error "Can't open server for %s" gnus-newsgroup-name))
7492         ;; The list of articles that weren't expired is returned.
7493         (save-excursion
7494           (if expiry-wait
7495               (let ((nnmail-expiry-wait-function nil)
7496                     (nnmail-expiry-wait expiry-wait))
7497                 (setq es (gnus-request-expire-articles
7498                           expirable gnus-newsgroup-name)))
7499             (setq es (gnus-request-expire-articles
7500                       expirable gnus-newsgroup-name))))
7501         (unless total
7502           (setq gnus-newsgroup-expirable es))
7503         ;; We go through the old list of expirable, and mark all
7504         ;; really expired articles as nonexistent.
7505         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7506           (let ((gnus-use-cache nil))
7507             (while expirable
7508               (unless (memq (car expirable) es)
7509                 (when (gnus-data-find (car expirable))
7510                   (gnus-summary-mark-article
7511                    (car expirable) gnus-canceled-mark)))
7512               (setq expirable (cdr expirable)))))
7513         (gnus-message 6 "Expiring articles...done")))))
7514
7515 (defun gnus-summary-expire-articles-now ()
7516   "Expunge all expirable articles in the current group.
7517 This means that *all* articles that are marked as expirable will be
7518 deleted forever, right now."
7519   (interactive)
7520   (or gnus-expert-user
7521       (gnus-yes-or-no-p
7522        "Are you really, really, really sure you want to delete all these messages? ")
7523       (error "Phew!"))
7524   (gnus-summary-expire-articles t))
7525
7526 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7527 (defun gnus-summary-delete-article (&optional n)
7528   "Delete the N next (mail) articles.
7529 This command actually deletes articles.  This is not a marking
7530 command.  The article will disappear forever from your life, never to
7531 return.
7532 If N is negative, delete backwards.
7533 If N is nil and articles have been marked with the process mark,
7534 delete these instead."
7535   (interactive "P")
7536   (unless (gnus-check-backend-function 'request-expire-articles
7537                                        gnus-newsgroup-name)
7538     (error "The current newsgroup does not support article deletion"))
7539   ;; Compute the list of articles to delete.
7540   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7541         not-deleted)
7542     (if (and gnus-novice-user
7543              (not (gnus-yes-or-no-p
7544                    (format "Do you really want to delete %s forever? "
7545                            (if (> (length articles) 1)
7546                                (format "these %s articles" (length articles))
7547                              "this article")))))
7548         ()
7549       ;; Delete the articles.
7550       (setq not-deleted (gnus-request-expire-articles
7551                          articles gnus-newsgroup-name 'force))
7552       (while articles
7553         (gnus-summary-remove-process-mark (car articles))
7554         ;; The backend might not have been able to delete the article
7555         ;; after all.
7556         (unless (memq (car articles) not-deleted)
7557           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7558         (setq articles (cdr articles)))
7559       (when not-deleted
7560         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7561     (gnus-summary-position-point)
7562     (gnus-set-mode-line 'summary)
7563     not-deleted))
7564
7565 (defun gnus-summary-edit-article (&optional force)
7566   "Edit the current article.
7567 This will have permanent effect only in mail groups.
7568 If FORCE is non-nil, allow editing of articles even in read-only
7569 groups."
7570   (interactive "P")
7571   (save-excursion
7572     (set-buffer gnus-summary-buffer)
7573     (let ((mail-parse-charset gnus-newsgroup-charset)
7574           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7575       (gnus-set-global-variables)
7576       (when (and (not force)
7577                  (gnus-group-read-only-p))
7578         (error "The current newsgroup does not support article editing"))
7579       (gnus-summary-show-article t)
7580       (gnus-article-edit-article
7581        'ignore
7582        `(lambda (no-highlight)
7583           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7584                 (mail-parse-ignored-charsets
7585                  ',gnus-newsgroup-ignored-charsets))
7586             (gnus-summary-edit-article-done
7587              ,(or (mail-header-references gnus-current-headers) "")
7588              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7589
7590 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7591
7592 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7593                                                  no-highlight)
7594   "Make edits to the current article permanent."
7595   (interactive)
7596   ;; Replace the article.
7597   (let ((buf (current-buffer)))
7598     (with-temp-buffer
7599       (insert-buffer-substring buf)
7600       (if (and (not read-only)
7601                (not (gnus-request-replace-article
7602                      (cdr gnus-article-current) (car gnus-article-current)
7603                      (current-buffer) t)))
7604           (error "Couldn't replace article")
7605         ;; Update the summary buffer.
7606         (if (and references
7607                  (equal (message-tokenize-header references " ")
7608                         (message-tokenize-header
7609                          (or (message-fetch-field "references") "") " ")))
7610             ;; We only have to update this line.
7611             (save-excursion
7612               (save-restriction
7613                 (message-narrow-to-head)
7614                 (let ((head (buffer-string))
7615                       header)
7616                   (with-temp-buffer
7617                     (insert (format "211 %d Article retrieved.\n"
7618                                     (cdr gnus-article-current)))
7619                     (insert head)
7620                     (insert ".\n")
7621                     (let ((nntp-server-buffer (current-buffer)))
7622                       (setq header (car (gnus-get-newsgroup-headers
7623                                          (save-excursion
7624                                            (set-buffer gnus-summary-buffer)
7625                                            gnus-newsgroup-dependencies)
7626                                          t))))
7627                     (save-excursion
7628                       (set-buffer gnus-summary-buffer)
7629                       (gnus-data-set-header
7630                        (gnus-data-find (cdr gnus-article-current))
7631                        header)
7632                       (gnus-summary-update-article-line
7633                        (cdr gnus-article-current) header))))))
7634           ;; Update threads.
7635           (set-buffer (or buffer gnus-summary-buffer))
7636           (gnus-summary-update-article (cdr gnus-article-current)))
7637         ;; Prettify the article buffer again.
7638         (unless no-highlight
7639           (save-excursion
7640             (set-buffer gnus-article-buffer)
7641             ;;;!!! Fix this -- article should be rehighlighted.
7642             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7643             (set-buffer gnus-original-article-buffer)
7644             (gnus-request-article
7645              (cdr gnus-article-current)
7646              (car gnus-article-current) (current-buffer))))
7647         ;; Prettify the summary buffer line.
7648         (when (gnus-visual-p 'summary-highlight 'highlight)
7649           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7650
7651 (defun gnus-summary-edit-wash (key)
7652   "Perform editing command KEY in the article buffer."
7653   (interactive
7654    (list
7655     (progn
7656       (message "%s" (concat (this-command-keys) "- "))
7657       (read-char))))
7658   (message "")
7659   (gnus-summary-edit-article)
7660   (execute-kbd-macro (concat (this-command-keys) key))
7661   (gnus-article-edit-done))
7662
7663 ;;; Respooling
7664
7665 (defun gnus-summary-respool-query (&optional silent trace)
7666   "Query where the respool algorithm would put this article."
7667   (interactive)
7668   (let (gnus-mark-article-hook)
7669     (gnus-summary-select-article)
7670     (save-excursion
7671       (set-buffer gnus-original-article-buffer)
7672       (save-restriction
7673         (message-narrow-to-head)
7674         (let ((groups (nnmail-article-group 'identity trace)))
7675           (unless silent
7676             (if groups
7677                 (message "This message would go to %s"
7678                          (mapconcat 'car groups ", "))
7679               (message "This message would go to no groups"))
7680             groups))))))
7681
7682 (defun gnus-summary-respool-trace ()
7683   "Trace where the respool algorithm would put this article.
7684 Display a buffer showing all fancy splitting patterns which matched."
7685   (interactive)
7686   (gnus-summary-respool-query nil t))
7687
7688 ;; Summary marking commands.
7689
7690 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7691   "Mark articles which has the same subject as read, and then select the next.
7692 If UNMARK is positive, remove any kind of mark.
7693 If UNMARK is negative, tick articles."
7694   (interactive "P")
7695   (when unmark
7696     (setq unmark (prefix-numeric-value unmark)))
7697   (let ((count
7698          (gnus-summary-mark-same-subject
7699           (gnus-summary-article-subject) unmark)))
7700     ;; Select next unread article.  If auto-select-same mode, should
7701     ;; select the first unread article.
7702     (gnus-summary-next-article t (and gnus-auto-select-same
7703                                       (gnus-summary-article-subject)))
7704     (gnus-message 7 "%d article%s marked as %s"
7705                   count (if (= count 1) " is" "s are")
7706                   (if unmark "unread" "read"))))
7707
7708 (defun gnus-summary-kill-same-subject (&optional unmark)
7709   "Mark articles which has the same subject as read.
7710 If UNMARK is positive, remove any kind of mark.
7711 If UNMARK is negative, tick articles."
7712   (interactive "P")
7713   (when unmark
7714     (setq unmark (prefix-numeric-value unmark)))
7715   (let ((count
7716          (gnus-summary-mark-same-subject
7717           (gnus-summary-article-subject) unmark)))
7718     ;; If marked as read, go to next unread subject.
7719     (when (null unmark)
7720       ;; Go to next unread subject.
7721       (gnus-summary-next-subject 1 t))
7722     (gnus-message 7 "%d articles are marked as %s"
7723                   count (if unmark "unread" "read"))))
7724
7725 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7726   "Mark articles with same SUBJECT as read, and return marked number.
7727 If optional argument UNMARK is positive, remove any kinds of marks.
7728 If optional argument UNMARK is negative, mark articles as unread instead."
7729   (let ((count 1))
7730     (save-excursion
7731       (cond
7732        ((null unmark)                   ; Mark as read.
7733         (while (and
7734                 (progn
7735                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7736                   (gnus-summary-show-thread) t)
7737                 (gnus-summary-find-subject subject))
7738           (setq count (1+ count))))
7739        ((> unmark 0)                    ; Tick.
7740         (while (and
7741                 (progn
7742                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7743                   (gnus-summary-show-thread) t)
7744                 (gnus-summary-find-subject subject))
7745           (setq count (1+ count))))
7746        (t                               ; Mark as unread.
7747         (while (and
7748                 (progn
7749                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7750                   (gnus-summary-show-thread) t)
7751                 (gnus-summary-find-subject subject))
7752           (setq count (1+ count)))))
7753       (gnus-set-mode-line 'summary)
7754       ;; Return the number of marked articles.
7755       count)))
7756
7757 (defun gnus-summary-mark-as-processable (n &optional unmark)
7758   "Set the process mark on the next N articles.
7759 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7760 the process mark instead.  The difference between N and the actual
7761 number of articles marked is returned."
7762   (interactive "p")
7763   (let ((backward (< n 0))
7764         (n (abs n)))
7765     (while (and
7766             (> n 0)
7767             (if unmark
7768                 (gnus-summary-remove-process-mark
7769                  (gnus-summary-article-number))
7770               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7771             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7772       (setq n (1- n)))
7773     (when (/= 0 n)
7774       (gnus-message 7 "No more articles"))
7775     (gnus-summary-recenter)
7776     (gnus-summary-position-point)
7777     n))
7778
7779 (defun gnus-summary-unmark-as-processable (n)
7780   "Remove the process mark from the next N articles.
7781 If N is negative, unmark backward instead.  The difference between N and
7782 the actual number of articles unmarked is returned."
7783   (interactive "p")
7784   (gnus-summary-mark-as-processable n t))
7785
7786 (defun gnus-summary-unmark-all-processable ()
7787   "Remove the process mark from all articles."
7788   (interactive)
7789   (save-excursion
7790     (while gnus-newsgroup-processable
7791       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7792   (gnus-summary-position-point))
7793
7794 (defun gnus-summary-mark-as-expirable (n)
7795   "Mark N articles forward as expirable.
7796 If N is negative, mark backward instead.  The difference between N and
7797 the actual number of articles marked is returned."
7798   (interactive "p")
7799   (gnus-summary-mark-forward n gnus-expirable-mark))
7800
7801 (defun gnus-summary-mark-article-as-replied (article)
7802   "Mark ARTICLE replied and update the summary line."
7803   (push article gnus-newsgroup-replied)
7804   (let ((buffer-read-only nil))
7805     (when (gnus-summary-goto-subject article nil t)
7806       (gnus-summary-update-secondary-mark article))))
7807
7808 (defun gnus-summary-set-bookmark (article)
7809   "Set a bookmark in current article."
7810   (interactive (list (gnus-summary-article-number)))
7811   (when (or (not (get-buffer gnus-article-buffer))
7812             (not gnus-current-article)
7813             (not gnus-article-current)
7814             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7815     (error "No current article selected"))
7816   ;; Remove old bookmark, if one exists.
7817   (let ((old (assq article gnus-newsgroup-bookmarks)))
7818     (when old
7819       (setq gnus-newsgroup-bookmarks
7820             (delq old gnus-newsgroup-bookmarks))))
7821   ;; Set the new bookmark, which is on the form
7822   ;; (article-number . line-number-in-body).
7823   (push
7824    (cons article
7825          (save-excursion
7826            (set-buffer gnus-article-buffer)
7827            (count-lines
7828             (min (point)
7829                  (save-excursion
7830                    (goto-char (point-min))
7831                    (search-forward "\n\n" nil t)
7832                    (point)))
7833             (point))))
7834    gnus-newsgroup-bookmarks)
7835   (gnus-message 6 "A bookmark has been added to the current article."))
7836
7837 (defun gnus-summary-remove-bookmark (article)
7838   "Remove the bookmark from the current article."
7839   (interactive (list (gnus-summary-article-number)))
7840   ;; Remove old bookmark, if one exists.
7841   (let ((old (assq article gnus-newsgroup-bookmarks)))
7842     (if old
7843         (progn
7844           (setq gnus-newsgroup-bookmarks
7845                 (delq old gnus-newsgroup-bookmarks))
7846           (gnus-message 6 "Removed bookmark."))
7847       (gnus-message 6 "No bookmark in current article."))))
7848
7849 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7850 (defun gnus-summary-mark-as-dormant (n)
7851   "Mark N articles forward as dormant.
7852 If N is negative, mark backward instead.  The difference between N and
7853 the actual number of articles marked is returned."
7854   (interactive "p")
7855   (gnus-summary-mark-forward n gnus-dormant-mark))
7856
7857 (defun gnus-summary-set-process-mark (article)
7858   "Set the process mark on ARTICLE and update the summary line."
7859   (setq gnus-newsgroup-processable
7860         (cons article
7861               (delq article gnus-newsgroup-processable)))
7862   (when (gnus-summary-goto-subject article)
7863     (gnus-summary-show-thread)
7864     (gnus-summary-goto-subject article)
7865     (gnus-summary-update-secondary-mark article)))
7866
7867 (defun gnus-summary-remove-process-mark (article)
7868   "Remove the process mark from ARTICLE and update the summary line."
7869   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7870   (when (gnus-summary-goto-subject article)
7871     (gnus-summary-show-thread)
7872     (gnus-summary-goto-subject article)
7873     (gnus-summary-update-secondary-mark article)))
7874
7875 (defun gnus-summary-set-saved-mark (article)
7876   "Set the process mark on ARTICLE and update the summary line."
7877   (push article gnus-newsgroup-saved)
7878   (when (gnus-summary-goto-subject article)
7879     (gnus-summary-update-secondary-mark article)))
7880
7881 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7882   "Mark N articles as read forwards.
7883 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7884 The difference between N and the actual number of articles marked is
7885 returned."
7886   (interactive "p")
7887   (gnus-summary-show-thread)
7888   (let ((backward (< n 0))
7889         (gnus-summary-goto-unread
7890          (and gnus-summary-goto-unread
7891               (not (eq gnus-summary-goto-unread 'never))
7892               (not (memq mark (list gnus-unread-mark
7893                                     gnus-ticked-mark gnus-dormant-mark)))))
7894         (n (abs n))
7895         (mark (or mark gnus-del-mark)))
7896     (while (and (> n 0)
7897                 (gnus-summary-mark-article nil mark no-expire)
7898                 (zerop (gnus-summary-next-subject
7899                         (if backward -1 1)
7900                         (and gnus-summary-goto-unread
7901                              (not (eq gnus-summary-goto-unread 'never)))
7902                         t)))
7903       (setq n (1- n)))
7904     (when (/= 0 n)
7905       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7906     (gnus-summary-recenter)
7907     (gnus-summary-position-point)
7908     (gnus-set-mode-line 'summary)
7909     n))
7910
7911 (defun gnus-summary-mark-article-as-read (mark)
7912   "Mark the current article quickly as read with MARK."
7913   (let ((article (gnus-summary-article-number)))
7914     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7915     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7916     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7917     (push (cons article mark) gnus-newsgroup-reads)
7918     ;; Possibly remove from cache, if that is used.
7919     (when gnus-use-cache
7920       (gnus-cache-enter-remove-article article))
7921     ;; Allow the backend to change the mark.
7922     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7923     ;; Check for auto-expiry.
7924     (when (and gnus-newsgroup-auto-expire
7925                (memq mark gnus-auto-expirable-marks))
7926       (setq mark gnus-expirable-mark)
7927       ;; Let the backend know about the mark change.
7928       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7929       (push article gnus-newsgroup-expirable))
7930     ;; Set the mark in the buffer.
7931     (gnus-summary-update-mark mark 'unread)
7932     t))
7933
7934 (defun gnus-summary-mark-article-as-unread (mark)
7935   "Mark the current article quickly as unread with MARK."
7936   (let* ((article (gnus-summary-article-number))
7937          (old-mark (gnus-summary-article-mark article)))
7938     ;; Allow the backend to change the mark.
7939     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7940     (if (eq mark old-mark)
7941         t
7942       (if (<= article 0)
7943           (progn
7944             (gnus-error 1 "Can't mark negative article numbers")
7945             nil)
7946         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7947         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7948         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7949         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7950         (cond ((= mark gnus-ticked-mark)
7951                (push article gnus-newsgroup-marked))
7952               ((= mark gnus-dormant-mark)
7953                (push article gnus-newsgroup-dormant))
7954               (t
7955                (push article gnus-newsgroup-unreads)))
7956         (gnus-pull article gnus-newsgroup-reads)
7957
7958         ;; See whether the article is to be put in the cache.
7959         (and gnus-use-cache
7960              (vectorp (gnus-summary-article-header article))
7961              (save-excursion
7962                (gnus-cache-possibly-enter-article
7963                 gnus-newsgroup-name article
7964                 (gnus-summary-article-header article)
7965                 (= mark gnus-ticked-mark)
7966                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7967
7968         ;; Fix the mark.
7969         (gnus-summary-update-mark mark 'unread)
7970         t))))
7971
7972 (defun gnus-summary-mark-article (&optional article mark no-expire)
7973   "Mark ARTICLE with MARK.  MARK can be any character.
7974 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7975 `??' (dormant) and `?E' (expirable).
7976 If MARK is nil, then the default character `?r' is used.
7977 If ARTICLE is nil, then the article on the current line will be
7978 marked."
7979   ;; The mark might be a string.
7980   (when (stringp mark)
7981     (setq mark (aref mark 0)))
7982   ;; If no mark is given, then we check auto-expiring.
7983   (when (null mark)
7984     (setq mark gnus-del-mark))
7985   (when (and (not no-expire)
7986              gnus-newsgroup-auto-expire
7987              (memq mark gnus-auto-expirable-marks))
7988     (setq mark gnus-expirable-mark))
7989   (let ((article (or article (gnus-summary-article-number)))
7990         (old-mark (gnus-summary-article-mark article)))
7991     ;; Allow the backend to change the mark.
7992     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7993     (if (eq mark old-mark)
7994         t
7995       (unless article
7996         (error "No article on current line"))
7997       (if (not (if (or (= mark gnus-unread-mark)
7998                        (= mark gnus-ticked-mark)
7999                        (= mark gnus-dormant-mark))
8000                    (gnus-mark-article-as-unread article mark)
8001                  (gnus-mark-article-as-read article mark)))
8002           t
8003         ;; See whether the article is to be put in the cache.
8004         (and gnus-use-cache
8005              (not (= mark gnus-canceled-mark))
8006              (vectorp (gnus-summary-article-header article))
8007              (save-excursion
8008                (gnus-cache-possibly-enter-article
8009                 gnus-newsgroup-name article
8010                 (gnus-summary-article-header article)
8011                 (= mark gnus-ticked-mark)
8012                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8013
8014         (when (gnus-summary-goto-subject article nil t)
8015           (let ((buffer-read-only nil))
8016             (gnus-summary-show-thread)
8017             ;; Fix the mark.
8018             (gnus-summary-update-mark mark 'unread)
8019             t))))))
8020
8021 (defun gnus-summary-update-secondary-mark (article)
8022   "Update the secondary (read, process, cache) mark."
8023   (gnus-summary-update-mark
8024    (cond ((memq article gnus-newsgroup-processable)
8025           gnus-process-mark)
8026          ((memq article gnus-newsgroup-cached)
8027           gnus-cached-mark)
8028          ((memq article gnus-newsgroup-replied)
8029           gnus-replied-mark)
8030          ((memq article gnus-newsgroup-saved)
8031           gnus-saved-mark)
8032          (t gnus-unread-mark))
8033    'replied)
8034   (when (gnus-visual-p 'summary-highlight 'highlight)
8035     (gnus-run-hooks 'gnus-summary-update-hook))
8036   t)
8037
8038 (defun gnus-summary-update-mark (mark type)
8039   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8040         (buffer-read-only nil))
8041     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8042     (when forward
8043       (when (looking-at "\r")
8044         (incf forward))
8045       (when (<= (+ forward (point)) (point-max))
8046         ;; Go to the right position on the line.
8047         (goto-char (+ forward (point)))
8048         ;; Replace the old mark with the new mark.
8049         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8050         ;; Optionally update the marks by some user rule.
8051         (when (eq type 'unread)
8052           (gnus-data-set-mark
8053            (gnus-data-find (gnus-summary-article-number)) mark)
8054           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8055
8056 (defun gnus-mark-article-as-read (article &optional mark)
8057   "Enter ARTICLE in the pertinent lists and remove it from others."
8058   ;; Make the article expirable.
8059   (let ((mark (or mark gnus-del-mark)))
8060     (if (= mark gnus-expirable-mark)
8061         (push article gnus-newsgroup-expirable)
8062       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8063     ;; Remove from unread and marked lists.
8064     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8065     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8066     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8067     (push (cons article mark) gnus-newsgroup-reads)
8068     ;; Possibly remove from cache, if that is used.
8069     (when gnus-use-cache
8070       (gnus-cache-enter-remove-article article))
8071     t))
8072
8073 (defun gnus-mark-article-as-unread (article &optional mark)
8074   "Enter ARTICLE in the pertinent lists and remove it from others."
8075   (let ((mark (or mark gnus-ticked-mark)))
8076     (if (<= article 0)
8077         (progn
8078           (gnus-error 1 "Can't mark negative article numbers")
8079           nil)
8080       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8081             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8082             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8083             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8084
8085       ;; Unsuppress duplicates?
8086       (when gnus-suppress-duplicates
8087         (gnus-dup-unsuppress-article article))
8088
8089       (cond ((= mark gnus-ticked-mark)
8090              (push article gnus-newsgroup-marked))
8091             ((= mark gnus-dormant-mark)
8092              (push article gnus-newsgroup-dormant))
8093             (t
8094              (push article gnus-newsgroup-unreads)))
8095       (gnus-pull article gnus-newsgroup-reads)
8096       t)))
8097
8098 (defalias 'gnus-summary-mark-as-unread-forward
8099   'gnus-summary-tick-article-forward)
8100 (make-obsolete 'gnus-summary-mark-as-unread-forward
8101                'gnus-summary-tick-article-forward)
8102 (defun gnus-summary-tick-article-forward (n)
8103   "Tick N articles forwards.
8104 If N is negative, tick backwards instead.
8105 The difference between N and the number of articles ticked is returned."
8106   (interactive "p")
8107   (gnus-summary-mark-forward n gnus-ticked-mark))
8108
8109 (defalias 'gnus-summary-mark-as-unread-backward
8110   'gnus-summary-tick-article-backward)
8111 (make-obsolete 'gnus-summary-mark-as-unread-backward
8112                'gnus-summary-tick-article-backward)
8113 (defun gnus-summary-tick-article-backward (n)
8114   "Tick N articles backwards.
8115 The difference between N and the number of articles ticked is returned."
8116   (interactive "p")
8117   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8118
8119 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8120 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8121 (defun gnus-summary-tick-article (&optional article clear-mark)
8122   "Mark current article as unread.
8123 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8124 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8125   (interactive)
8126   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8127                                        gnus-ticked-mark)))
8128
8129 (defun gnus-summary-mark-as-read-forward (n)
8130   "Mark N articles as read forwards.
8131 If N is negative, mark backwards instead.
8132 The difference between N and the actual number of articles marked is
8133 returned."
8134   (interactive "p")
8135   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8136
8137 (defun gnus-summary-mark-as-read-backward (n)
8138   "Mark the N articles as read backwards.
8139 The difference between N and the actual number of articles marked is
8140 returned."
8141   (interactive "p")
8142   (gnus-summary-mark-forward
8143    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8144
8145 (defun gnus-summary-mark-as-read (&optional article mark)
8146   "Mark current article as read.
8147 ARTICLE specifies the article to be marked as read.
8148 MARK specifies a string to be inserted at the beginning of the line."
8149   (gnus-summary-mark-article article mark))
8150
8151 (defun gnus-summary-clear-mark-forward (n)
8152   "Clear marks from N articles forward.
8153 If N is negative, clear backward instead.
8154 The difference between N and the number of marks cleared is returned."
8155   (interactive "p")
8156   (gnus-summary-mark-forward n gnus-unread-mark))
8157
8158 (defun gnus-summary-clear-mark-backward (n)
8159   "Clear marks from N articles backward.
8160 The difference between N and the number of marks cleared is returned."
8161   (interactive "p")
8162   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8163
8164 (defun gnus-summary-mark-unread-as-read ()
8165   "Intended to be used by `gnus-summary-mark-article-hook'."
8166   (when (memq gnus-current-article gnus-newsgroup-unreads)
8167     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8168
8169 (defun gnus-summary-mark-read-and-unread-as-read ()
8170   "Intended to be used by `gnus-summary-mark-article-hook'."
8171   (let ((mark (gnus-summary-article-mark)))
8172     (when (or (gnus-unread-mark-p mark)
8173               (gnus-read-mark-p mark))
8174       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8175
8176 (defun gnus-summary-mark-region-as-read (point mark all)
8177   "Mark all unread articles between point and mark as read.
8178 If given a prefix, mark all articles between point and mark as read,
8179 even ticked and dormant ones."
8180   (interactive "r\nP")
8181   (save-excursion
8182     (let (article)
8183       (goto-char point)
8184       (beginning-of-line)
8185       (while (and
8186               (< (point) mark)
8187               (progn
8188                 (when (or all
8189                           (memq (setq article (gnus-summary-article-number))
8190                                 gnus-newsgroup-unreads))
8191                   (gnus-summary-mark-article article gnus-del-mark))
8192                 t)
8193               (gnus-summary-find-next))))))
8194
8195 (defun gnus-summary-mark-below (score mark)
8196   "Mark articles with score less than SCORE with MARK."
8197   (interactive "P\ncMark: ")
8198   (setq score (if score
8199                   (prefix-numeric-value score)
8200                 (or gnus-summary-default-score 0)))
8201   (save-excursion
8202     (set-buffer gnus-summary-buffer)
8203     (goto-char (point-min))
8204     (while
8205         (progn
8206           (and (< (gnus-summary-article-score) score)
8207                (gnus-summary-mark-article nil mark))
8208           (gnus-summary-find-next)))))
8209
8210 (defun gnus-summary-kill-below (&optional score)
8211   "Mark articles with score below SCORE as read."
8212   (interactive "P")
8213   (gnus-summary-mark-below score gnus-killed-mark))
8214
8215 (defun gnus-summary-clear-above (&optional score)
8216   "Clear all marks from articles with score above SCORE."
8217   (interactive "P")
8218   (gnus-summary-mark-above score gnus-unread-mark))
8219
8220 (defun gnus-summary-tick-above (&optional score)
8221   "Tick all articles with score above SCORE."
8222   (interactive "P")
8223   (gnus-summary-mark-above score gnus-ticked-mark))
8224
8225 (defun gnus-summary-mark-above (score mark)
8226   "Mark articles with score over SCORE with MARK."
8227   (interactive "P\ncMark: ")
8228   (setq score (if score
8229                   (prefix-numeric-value score)
8230                 (or gnus-summary-default-score 0)))
8231   (save-excursion
8232     (set-buffer gnus-summary-buffer)
8233     (goto-char (point-min))
8234     (while (and (progn
8235                   (when (> (gnus-summary-article-score) score)
8236                     (gnus-summary-mark-article nil mark))
8237                   t)
8238                 (gnus-summary-find-next)))))
8239
8240 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8241 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8242 (defun gnus-summary-limit-include-expunged (&optional no-error)
8243   "Display all the hidden articles that were expunged for low scores."
8244   (interactive)
8245   (let ((buffer-read-only nil))
8246     (let ((scored gnus-newsgroup-scored)
8247           headers h)
8248       (while scored
8249         (unless (gnus-summary-goto-subject (caar scored))
8250           (and (setq h (gnus-summary-article-header (caar scored)))
8251                (< (cdar scored) gnus-summary-expunge-below)
8252                (push h headers)))
8253         (setq scored (cdr scored)))
8254       (if (not headers)
8255           (when (not no-error)
8256             (error "No expunged articles hidden"))
8257         (goto-char (point-min))
8258         (gnus-summary-prepare-unthreaded (nreverse headers))
8259         (goto-char (point-min))
8260         (gnus-summary-position-point)
8261         t))))
8262
8263 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8264   "Mark all unread articles in this newsgroup as read.
8265 If prefix argument ALL is non-nil, ticked and dormant articles will
8266 also be marked as read.
8267 If QUIETLY is non-nil, no questions will be asked.
8268 If TO-HERE is non-nil, it should be a point in the buffer.  All
8269 articles before this point will be marked as read.
8270 Note that this function will only catch up the unread article
8271 in the current summary buffer limitation.
8272 The number of articles marked as read is returned."
8273   (interactive "P")
8274   (prog1
8275       (save-excursion
8276         (when (or quietly
8277                   (not gnus-interactive-catchup) ;Without confirmation?
8278                   gnus-expert-user
8279                   (gnus-y-or-n-p
8280                    (if all
8281                        "Mark absolutely all articles as read? "
8282                      "Mark all unread articles as read? ")))
8283           (if (and not-mark
8284                    (not gnus-newsgroup-adaptive)
8285                    (not gnus-newsgroup-auto-expire)
8286                    (not gnus-suppress-duplicates)
8287                    (or (not gnus-use-cache)
8288                        (eq gnus-use-cache 'passive)))
8289               (progn
8290                 (when all
8291                   (setq gnus-newsgroup-marked nil
8292                         gnus-newsgroup-dormant nil))
8293                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8294             ;; We actually mark all articles as canceled, which we
8295             ;; have to do when using auto-expiry or adaptive scoring.
8296             (gnus-summary-show-all-threads)
8297             (when (gnus-summary-first-subject (not all) t)
8298               (while (and
8299                       (if to-here (< (point) to-here) t)
8300                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8301                       (gnus-summary-find-next (not all) nil nil t))))
8302             (gnus-set-mode-line 'summary))
8303           t))
8304     (gnus-summary-position-point)))
8305
8306 (defun gnus-summary-catchup-to-here (&optional all)
8307   "Mark all unticked articles before the current one as read.
8308 If ALL is non-nil, also mark ticked and dormant articles as read."
8309   (interactive "P")
8310   (save-excursion
8311     (gnus-save-hidden-threads
8312       (let ((beg (point)))
8313         ;; We check that there are unread articles.
8314         (when (or all (gnus-summary-find-prev))
8315           (gnus-summary-catchup all t beg)))))
8316   (gnus-summary-position-point))
8317
8318 (defun gnus-summary-catchup-all (&optional quietly)
8319   "Mark all articles in this newsgroup as read."
8320   (interactive "P")
8321   (gnus-summary-catchup t quietly))
8322
8323 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8324   "Mark all unread articles in this group as read, then exit.
8325 If prefix argument ALL is non-nil, all articles are marked as read."
8326   (interactive "P")
8327   (when (gnus-summary-catchup all quietly nil 'fast)
8328     ;; Select next newsgroup or exit.
8329     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8330              (eq gnus-auto-select-next 'quietly))
8331         (gnus-summary-next-group nil)
8332       (gnus-summary-exit))))
8333
8334 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8335   "Mark all articles in this newsgroup as read, and then exit."
8336   (interactive "P")
8337   (gnus-summary-catchup-and-exit t quietly))
8338
8339 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8340 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8341   "Mark all articles in this group as read and select the next group.
8342 If given a prefix, mark all articles, unread as well as ticked, as
8343 read."
8344   (interactive "P")
8345   (save-excursion
8346     (gnus-summary-catchup all))
8347   (gnus-summary-next-article t nil nil t))
8348
8349 ;; Thread-based commands.
8350
8351 (defun gnus-summary-articles-in-thread (&optional article)
8352   "Return a list of all articles in the current thread.
8353 If ARTICLE is non-nil, return all articles in the thread that starts
8354 with that article."
8355   (let* ((article (or article (gnus-summary-article-number)))
8356          (data (gnus-data-find-list article))
8357          (top-level (gnus-data-level (car data)))
8358          (top-subject
8359           (cond ((null gnus-thread-operation-ignore-subject)
8360                  (gnus-simplify-subject-re
8361                   (mail-header-subject (gnus-data-header (car data)))))
8362                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8363                  (gnus-simplify-subject-fuzzy
8364                   (mail-header-subject (gnus-data-header (car data)))))
8365                 (t nil)))
8366          (end-point (save-excursion
8367                       (if (gnus-summary-go-to-next-thread)
8368                           (point) (point-max))))
8369          articles)
8370     (while (and data
8371                 (< (gnus-data-pos (car data)) end-point))
8372       (when (or (not top-subject)
8373                 (string= top-subject
8374                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8375                              (gnus-simplify-subject-fuzzy
8376                               (mail-header-subject
8377                                (gnus-data-header (car data))))
8378                            (gnus-simplify-subject-re
8379                             (mail-header-subject
8380                              (gnus-data-header (car data)))))))
8381         (push (gnus-data-number (car data)) articles))
8382       (unless (and (setq data (cdr data))
8383                    (> (gnus-data-level (car data)) top-level))
8384         (setq data nil)))
8385     ;; Return the list of articles.
8386     (nreverse articles)))
8387
8388 (defun gnus-summary-rethread-current ()
8389   "Rethread the thread the current article is part of."
8390   (interactive)
8391   (let* ((gnus-show-threads t)
8392          (article (gnus-summary-article-number))
8393          (id (mail-header-id (gnus-summary-article-header)))
8394          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8395     (unless id
8396       (error "No article on the current line"))
8397     (gnus-rebuild-thread id)
8398     (gnus-summary-goto-subject article)))
8399
8400 (defun gnus-summary-reparent-thread ()
8401   "Make the current article child of the marked (or previous) article.
8402
8403 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8404 is non-nil or the Subject: of both articles are the same."
8405   (interactive)
8406   (unless (not (gnus-group-read-only-p))
8407     (error "The current newsgroup does not support article editing"))
8408   (unless (<= (length gnus-newsgroup-processable) 1)
8409     (error "No more than one article may be marked"))
8410   (save-window-excursion
8411     (let ((gnus-article-buffer " *reparent*")
8412           (current-article (gnus-summary-article-number))
8413           ;; First grab the marked article, otherwise one line up.
8414           (parent-article (if (not (null gnus-newsgroup-processable))
8415                               (car gnus-newsgroup-processable)
8416                             (save-excursion
8417                               (if (eq (forward-line -1) 0)
8418                                   (gnus-summary-article-number)
8419                                 (error "Beginning of summary buffer"))))))
8420       (unless (not (eq current-article parent-article))
8421         (error "An article may not be self-referential"))
8422       (let ((message-id (mail-header-id
8423                          (gnus-summary-article-header parent-article))))
8424         (unless (and message-id (not (equal message-id "")))
8425           (error "No message-id in desired parent"))
8426         (gnus-with-article current-article
8427           (goto-char (point-min))
8428           (if (re-search-forward "^References: " nil t)
8429               (progn
8430                 (re-search-forward "^[^ \t]" nil t)
8431                 (forward-line -1)
8432                 (end-of-line)
8433                 (insert " " message-id))
8434             (insert "References: " message-id "\n")))
8435         (set-buffer gnus-summary-buffer)
8436         (gnus-summary-unmark-all-processable)
8437         (gnus-summary-update-article current-article)
8438         (gnus-summary-rethread-current)
8439         (gnus-message 3 "Article %d is now the child of article %d"
8440                       current-article parent-article)))))
8441
8442 (defun gnus-summary-toggle-threads (&optional arg)
8443   "Toggle showing conversation threads.
8444 If ARG is positive number, turn showing conversation threads on."
8445   (interactive "P")
8446   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8447     (setq gnus-show-threads
8448           (if (null arg) (not gnus-show-threads)
8449             (> (prefix-numeric-value arg) 0)))
8450     (gnus-summary-prepare)
8451     (gnus-summary-goto-subject current)
8452     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8453     (gnus-summary-position-point)))
8454
8455 (defun gnus-summary-show-all-threads ()
8456   "Show all threads."
8457   (interactive)
8458   (save-excursion
8459     (let ((buffer-read-only nil))
8460       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8461   (gnus-summary-position-point))
8462
8463 (defun gnus-summary-show-thread ()
8464   "Show thread subtrees.
8465 Returns nil if no thread was there to be shown."
8466   (interactive)
8467   (let ((buffer-read-only nil)
8468         (orig (point))
8469         ;; first goto end then to beg, to have point at beg after let
8470         (end (progn (end-of-line) (point)))
8471         (beg (progn (beginning-of-line) (point))))
8472     (prog1
8473         ;; Any hidden lines here?
8474         (search-forward "\r" end t)
8475       (subst-char-in-region beg end ?\^M ?\n t)
8476       (goto-char orig)
8477       (gnus-summary-position-point))))
8478
8479 (defun gnus-summary-hide-all-threads ()
8480   "Hide all thread subtrees."
8481   (interactive)
8482   (save-excursion
8483     (goto-char (point-min))
8484     (gnus-summary-hide-thread)
8485     (while (zerop (gnus-summary-next-thread 1 t))
8486       (gnus-summary-hide-thread)))
8487   (gnus-summary-position-point))
8488
8489 (defun gnus-summary-hide-thread ()
8490   "Hide thread subtrees.
8491 Returns nil if no threads were there to be hidden."
8492   (interactive)
8493   (let ((buffer-read-only nil)
8494         (start (point))
8495         (article (gnus-summary-article-number)))
8496     (goto-char start)
8497     ;; Go forward until either the buffer ends or the subthread
8498     ;; ends.
8499     (when (and (not (eobp))
8500                (or (zerop (gnus-summary-next-thread 1 t))
8501                    (goto-char (point-max))))
8502       (prog1
8503           (if (and (> (point) start)
8504                    (search-backward "\n" start t))
8505               (progn
8506                 (subst-char-in-region start (point) ?\n ?\^M)
8507                 (gnus-summary-goto-subject article))
8508             (goto-char start)
8509             nil)
8510         ;;(gnus-summary-position-point)
8511         ))))
8512
8513 (defun gnus-summary-go-to-next-thread (&optional previous)
8514   "Go to the same level (or less) next thread.
8515 If PREVIOUS is non-nil, go to previous thread instead.
8516 Return the article number moved to, or nil if moving was impossible."
8517   (let ((level (gnus-summary-thread-level))
8518         (way (if previous -1 1))
8519         (beg (point)))
8520     (forward-line way)
8521     (while (and (not (eobp))
8522                 (< level (gnus-summary-thread-level)))
8523       (forward-line way))
8524     (if (eobp)
8525         (progn
8526           (goto-char beg)
8527           nil)
8528       (setq beg (point))
8529       (prog1
8530           (gnus-summary-article-number)
8531         (goto-char beg)))))
8532
8533 (defun gnus-summary-next-thread (n &optional silent)
8534   "Go to the same level next N'th thread.
8535 If N is negative, search backward instead.
8536 Returns the difference between N and the number of skips actually
8537 done.
8538
8539 If SILENT, don't output messages."
8540   (interactive "p")
8541   (let ((backward (< n 0))
8542         (n (abs n)))
8543     (while (and (> n 0)
8544                 (gnus-summary-go-to-next-thread backward))
8545       (decf n))
8546     (unless silent
8547       (gnus-summary-position-point))
8548     (when (and (not silent) (/= 0 n))
8549       (gnus-message 7 "No more threads"))
8550     n))
8551
8552 (defun gnus-summary-prev-thread (n)
8553   "Go to the same level previous N'th thread.
8554 Returns the difference between N and the number of skips actually
8555 done."
8556   (interactive "p")
8557   (gnus-summary-next-thread (- n)))
8558
8559 (defun gnus-summary-go-down-thread ()
8560   "Go down one level in the current thread."
8561   (let ((children (gnus-summary-article-children)))
8562     (when children
8563       (gnus-summary-goto-subject (car children)))))
8564
8565 (defun gnus-summary-go-up-thread ()
8566   "Go up one level in the current thread."
8567   (let ((parent (gnus-summary-article-parent)))
8568     (when parent
8569       (gnus-summary-goto-subject parent))))
8570
8571 (defun gnus-summary-down-thread (n)
8572   "Go down thread N steps.
8573 If N is negative, go up instead.
8574 Returns the difference between N and how many steps down that were
8575 taken."
8576   (interactive "p")
8577   (let ((up (< n 0))
8578         (n (abs n)))
8579     (while (and (> n 0)
8580                 (if up (gnus-summary-go-up-thread)
8581                   (gnus-summary-go-down-thread)))
8582       (setq n (1- n)))
8583     (gnus-summary-position-point)
8584     (when (/= 0 n)
8585       (gnus-message 7 "Can't go further"))
8586     n))
8587
8588 (defun gnus-summary-up-thread (n)
8589   "Go up thread N steps.
8590 If N is negative, go up instead.
8591 Returns the difference between N and how many steps down that were
8592 taken."
8593   (interactive "p")
8594   (gnus-summary-down-thread (- n)))
8595
8596 (defun gnus-summary-top-thread ()
8597   "Go to the top of the thread."
8598   (interactive)
8599   (while (gnus-summary-go-up-thread))
8600   (gnus-summary-article-number))
8601
8602 (defun gnus-summary-kill-thread (&optional unmark)
8603   "Mark articles under current thread as read.
8604 If the prefix argument is positive, remove any kinds of marks.
8605 If the prefix argument is negative, tick articles instead."
8606   (interactive "P")
8607   (when unmark
8608     (setq unmark (prefix-numeric-value unmark)))
8609   (let ((articles (gnus-summary-articles-in-thread)))
8610     (save-excursion
8611       ;; Expand the thread.
8612       (gnus-summary-show-thread)
8613       ;; Mark all the articles.
8614       (while articles
8615         (gnus-summary-goto-subject (car articles))
8616         (cond ((null unmark)
8617                (gnus-summary-mark-article-as-read gnus-killed-mark))
8618               ((> unmark 0)
8619                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8620               (t
8621                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8622         (setq articles (cdr articles))))
8623     ;; Hide killed subtrees.
8624     (and (null unmark)
8625          gnus-thread-hide-killed
8626          (gnus-summary-hide-thread))
8627     ;; If marked as read, go to next unread subject.
8628     (when (null unmark)
8629       ;; Go to next unread subject.
8630       (gnus-summary-next-subject 1 t)))
8631   (gnus-set-mode-line 'summary))
8632
8633 ;; Summary sorting commands
8634
8635 (defun gnus-summary-sort-by-number (&optional reverse)
8636   "Sort the summary buffer by article number.
8637 Argument REVERSE means reverse order."
8638   (interactive "P")
8639   (gnus-summary-sort 'number reverse))
8640
8641 (defun gnus-summary-sort-by-author (&optional reverse)
8642   "Sort the summary buffer by author name alphabetically.
8643 If case-fold-search is non-nil, case of letters is ignored.
8644 Argument REVERSE means reverse order."
8645   (interactive "P")
8646   (gnus-summary-sort 'author reverse))
8647
8648 (defun gnus-summary-sort-by-subject (&optional reverse)
8649   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8650 If case-fold-search is non-nil, case of letters is ignored.
8651 Argument REVERSE means reverse order."
8652   (interactive "P")
8653   (gnus-summary-sort 'subject reverse))
8654
8655 (defun gnus-summary-sort-by-date (&optional reverse)
8656   "Sort the summary buffer by date.
8657 Argument REVERSE means reverse order."
8658   (interactive "P")
8659   (gnus-summary-sort 'date reverse))
8660
8661 (defun gnus-summary-sort-by-score (&optional reverse)
8662   "Sort the summary buffer by score.
8663 Argument REVERSE means reverse order."
8664   (interactive "P")
8665   (gnus-summary-sort 'score reverse))
8666
8667 (defun gnus-summary-sort-by-lines (&optional reverse)
8668   "Sort the summary buffer by the number of lines.
8669 Argument REVERSE means reverse order."
8670   (interactive "P")
8671   (gnus-summary-sort 'lines reverse))
8672
8673 (defun gnus-summary-sort-by-chars (&optional reverse)
8674   "Sort the summary buffer by article length.
8675 Argument REVERSE means reverse order."
8676   (interactive "P")
8677   (gnus-summary-sort 'chars reverse))   
8678
8679 (defun gnus-summary-sort (predicate reverse)
8680   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8681   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8682          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8683          (gnus-thread-sort-functions
8684           (if (not reverse)
8685               thread
8686             `(lambda (t1 t2)
8687                (,thread t2 t1))))
8688          (gnus-article-sort-functions
8689           (if (not reverse)
8690               article
8691             `(lambda (t1 t2)
8692                (,article t2 t1))))
8693          (buffer-read-only)
8694          (gnus-summary-prepare-hook nil))
8695     ;; We do the sorting by regenerating the threads.
8696     (gnus-summary-prepare)
8697     ;; Hide subthreads if needed.
8698     (when (and gnus-show-threads gnus-thread-hide-subtree)
8699       (gnus-summary-hide-all-threads))))
8700
8701 ;; Summary saving commands.
8702
8703 (defun gnus-summary-save-article (&optional n not-saved)
8704   "Save the current article using the default saver function.
8705 If N is a positive number, save the N next articles.
8706 If N is a negative number, save the N previous articles.
8707 If N is nil and any articles have been marked with the process mark,
8708 save those articles instead.
8709 The variable `gnus-default-article-saver' specifies the saver function."
8710   (interactive "P")
8711   (let* ((articles (gnus-summary-work-articles n))
8712          (save-buffer (save-excursion
8713                         (nnheader-set-temp-buffer " *Gnus Save*")))
8714          (num (length articles))
8715          header file)
8716     (dolist (article articles)
8717       (setq header (gnus-summary-article-header article))
8718       (if (not (vectorp header))
8719           ;; This is a pseudo-article.
8720           (if (assq 'name header)
8721               (gnus-copy-file (cdr (assq 'name header)))
8722             (gnus-message 1 "Article %d is unsaveable" article))
8723         ;; This is a real article.
8724         (save-window-excursion
8725           (gnus-summary-select-article t nil nil article))
8726         (save-excursion
8727           (set-buffer save-buffer)
8728           (erase-buffer)
8729           (insert-buffer-substring gnus-original-article-buffer))
8730         (setq file (gnus-article-save save-buffer file num))
8731         (gnus-summary-remove-process-mark article)
8732         (unless not-saved
8733           (gnus-summary-set-saved-mark article))))
8734     (gnus-kill-buffer save-buffer)
8735     (gnus-summary-position-point)
8736     (gnus-set-mode-line 'summary)
8737     n))
8738
8739 (defun gnus-summary-pipe-output (&optional arg)
8740   "Pipe the current article to a subprocess.
8741 If N is a positive number, pipe the N next articles.
8742 If N is a negative number, pipe the N previous articles.
8743 If N is nil and any articles have been marked with the process mark,
8744 pipe those articles instead."
8745   (interactive "P")
8746   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8747     (gnus-summary-save-article arg t))
8748   (gnus-configure-windows 'pipe))
8749
8750 (defun gnus-summary-save-article-mail (&optional arg)
8751   "Append the current article to an mail file.
8752 If N is a positive number, save the N next articles.
8753 If N is a negative number, save the N previous articles.
8754 If N is nil and any articles have been marked with the process mark,
8755 save those articles instead."
8756   (interactive "P")
8757   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8758     (gnus-summary-save-article arg)))
8759
8760 (defun gnus-summary-save-article-rmail (&optional arg)
8761   "Append the current article to an rmail file.
8762 If N is a positive number, save the N next articles.
8763 If N is a negative number, save the N previous articles.
8764 If N is nil and any articles have been marked with the process mark,
8765 save those articles instead."
8766   (interactive "P")
8767   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8768     (gnus-summary-save-article arg)))
8769
8770 (defun gnus-summary-save-article-file (&optional arg)
8771   "Append the current article to a file.
8772 If N is a positive number, save the N next articles.
8773 If N is a negative number, save the N previous articles.
8774 If N is nil and any articles have been marked with the process mark,
8775 save those articles instead."
8776   (interactive "P")
8777   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8778     (gnus-summary-save-article arg)))
8779
8780 (defun gnus-summary-write-article-file (&optional arg)
8781   "Write the current article to a file, deleting the previous file.
8782 If N is a positive number, save the N next articles.
8783 If N is a negative number, save the N previous articles.
8784 If N is nil and any articles have been marked with the process mark,
8785 save those articles instead."
8786   (interactive "P")
8787   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8788     (gnus-summary-save-article arg)))
8789
8790 (defun gnus-summary-save-article-body-file (&optional arg)
8791   "Append the current article body to a file.
8792 If N is a positive number, save the N next articles.
8793 If N is a negative number, save the N previous articles.
8794 If N is nil and any articles have been marked with the process mark,
8795 save those articles instead."
8796   (interactive "P")
8797   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8798     (gnus-summary-save-article arg)))
8799
8800 (defun gnus-summary-pipe-message (program)
8801   "Pipe the current article through PROGRAM."
8802   (interactive "sProgram: ")
8803   (gnus-summary-select-article)
8804   (let ((mail-header-separator ""))
8805     (gnus-eval-in-buffer-window gnus-article-buffer
8806       (save-restriction
8807         (widen)
8808         (let ((start (window-start))
8809               buffer-read-only)
8810           (message-pipe-buffer-body program)
8811           (set-window-start (get-buffer-window (current-buffer)) start))))))
8812
8813 (defun gnus-get-split-value (methods)
8814   "Return a value based on the split METHODS."
8815   (let (split-name method result match)
8816     (when methods
8817       (save-excursion
8818         (set-buffer gnus-original-article-buffer)
8819         (save-restriction
8820           (nnheader-narrow-to-headers)
8821           (while methods
8822             (goto-char (point-min))
8823             (setq method (pop methods))
8824             (setq match (car method))
8825             (when (cond
8826                    ((stringp match)
8827                     ;; Regular expression.
8828                     (ignore-errors
8829                       (re-search-forward match nil t)))
8830                    ((gnus-functionp match)
8831                     ;; Function.
8832                     (save-restriction
8833                       (widen)
8834                       (setq result (funcall match gnus-newsgroup-name))))
8835                    ((consp match)
8836                     ;; Form.
8837                     (save-restriction
8838                       (widen)
8839                       (setq result (eval match)))))
8840               (setq split-name (append (cdr method) split-name))
8841               (cond ((stringp result)
8842                      (push (expand-file-name
8843                             result gnus-article-save-directory)
8844                            split-name))
8845                     ((consp result)
8846                      (setq split-name (append result split-name)))))))))
8847     (nreverse split-name)))
8848
8849 (defun gnus-valid-move-group-p (group)
8850   (and (boundp group)
8851        (symbol-name group)
8852        (symbol-value group)
8853        (gnus-get-function (gnus-find-method-for-group
8854                            (symbol-name group)) 'request-accept-article t)))
8855
8856 (defun gnus-read-move-group-name (prompt default articles prefix)
8857   "Read a group name."
8858   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8859          (minibuffer-confirm-incomplete nil) ; XEmacs
8860          (prom
8861           (format "%s %s to:"
8862                   prompt
8863                   (if (> (length articles) 1)
8864                       (format "these %d articles" (length articles))
8865                     "this article")))
8866          (to-newsgroup
8867           (cond
8868            ((null split-name)
8869             (gnus-completing-read default prom
8870                                   gnus-active-hashtb
8871                                   'gnus-valid-move-group-p
8872                                   nil prefix
8873                                   'gnus-group-history))
8874            ((= 1 (length split-name))
8875             (gnus-completing-read (car split-name) prom
8876                                   gnus-active-hashtb
8877                                   'gnus-valid-move-group-p
8878                                   nil nil
8879                                   'gnus-group-history))
8880            (t
8881             (gnus-completing-read nil prom
8882                                   (mapcar (lambda (el) (list el))
8883                                           (nreverse split-name))
8884                                   nil nil nil
8885                                   'gnus-group-history)))))
8886     (when to-newsgroup
8887       (if (or (string= to-newsgroup "")
8888               (string= to-newsgroup prefix))
8889           (setq to-newsgroup default))
8890       (unless to-newsgroup
8891         (error "No group name entered"))
8892       (or (gnus-active to-newsgroup)
8893           (gnus-activate-group to-newsgroup)
8894           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8895                                      to-newsgroup))
8896               (or (and (gnus-request-create-group
8897                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8898                        (gnus-activate-group to-newsgroup nil nil
8899                                             (gnus-group-name-to-method
8900                                              to-newsgroup)))
8901                   (error "Couldn't create group %s" to-newsgroup)))
8902           (error "No such group: %s" to-newsgroup)))
8903     to-newsgroup))
8904
8905 (defun gnus-summary-save-parts (type dir n reverse)
8906   "Save parts matching TYPE to DIR.
8907 If REVERSE, save parts that do not match TYPE."
8908   (interactive
8909    (list (read-string "Save parts of type: " "image/.*")
8910          (read-file-name "Save to directory: " t nil t)
8911          current-prefix-arg))
8912   (gnus-summary-iterate n
8913     (let ((gnus-display-mime-function nil)
8914           (gnus-inhibit-treatment t))
8915       (gnus-summary-select-article))
8916     (save-excursion
8917       (set-buffer gnus-article-buffer)
8918       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8919         (when handles
8920           (gnus-summary-save-parts-1 type dir handles reverse)
8921           (mm-destroy-parts handles))))))
8922
8923 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8924   (if (stringp (car handle))
8925       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8926               (cdr handle))
8927     (when (if reverse
8928               (not (string-match type (mm-handle-media-type handle)))
8929             (string-match type (mm-handle-media-type handle)))
8930       (let ((file (expand-file-name
8931                    (file-name-nondirectory
8932                     (or
8933                      (mail-content-type-get
8934                       (mm-handle-disposition handle) 'filename)
8935                      (concat gnus-newsgroup-name "." gnus-current-article)))
8936                    dir)))
8937         (unless (file-exists-p file)
8938           (mm-save-part-to-file handle file))))))
8939
8940 ;; Summary extract commands
8941
8942 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8943   (let ((buffer-read-only nil)
8944         (article (gnus-summary-article-number))
8945         after-article b e)
8946     (unless (gnus-summary-goto-subject article)
8947       (error "No such article: %d" article))
8948     (gnus-summary-position-point)
8949     ;; If all commands are to be bunched up on one line, we collect
8950     ;; them here.
8951     (unless gnus-view-pseudos-separately
8952       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8953             files action)
8954         (while ps
8955           (setq action (cdr (assq 'action (car ps))))
8956           (setq files (list (cdr (assq 'name (car ps)))))
8957           (while (and ps (cdr ps)
8958                       (string= (or action "1")
8959                                (or (cdr (assq 'action (cadr ps))) "2")))
8960             (push (cdr (assq 'name (cadr ps))) files)
8961             (setcdr ps (cddr ps)))
8962           (when files
8963             (when (not (string-match "%s" action))
8964               (push " " files))
8965             (push " " files)
8966             (when (assq 'execute (car ps))
8967               (setcdr (assq 'execute (car ps))
8968                       (funcall (if (string-match "%s" action)
8969                                    'format 'concat)
8970                                action
8971                                (mapconcat
8972                                 (lambda (f)
8973                                   (if (equal f " ")
8974                                       f
8975                                     (gnus-quote-arg-for-sh-or-csh f)))
8976                                 files " ")))))
8977           (setq ps (cdr ps)))))
8978     (if (and gnus-view-pseudos (not not-view))
8979         (while pslist
8980           (when (assq 'execute (car pslist))
8981             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8982                                   (eq gnus-view-pseudos 'not-confirm)))
8983           (setq pslist (cdr pslist)))
8984       (save-excursion
8985         (while pslist
8986           (setq after-article (or (cdr (assq 'article (car pslist)))
8987                                   (gnus-summary-article-number)))
8988           (gnus-summary-goto-subject after-article)
8989           (forward-line 1)
8990           (setq b (point))
8991           (insert "    " (file-name-nondirectory
8992                           (cdr (assq 'name (car pslist))))
8993                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8994           (setq e (point))
8995           (forward-line -1)             ; back to `b'
8996           (gnus-add-text-properties
8997            b (1- e) (list 'gnus-number gnus-reffed-article-number
8998                           gnus-mouse-face-prop gnus-mouse-face))
8999           (gnus-data-enter
9000            after-article gnus-reffed-article-number
9001            gnus-unread-mark b (car pslist) 0 (- e b))
9002           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9003           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9004           (setq pslist (cdr pslist)))))))
9005
9006 (defun gnus-pseudos< (p1 p2)
9007   (let ((c1 (cdr (assq 'action p1)))
9008         (c2 (cdr (assq 'action p2))))
9009     (and c1 c2 (string< c1 c2))))
9010
9011 (defun gnus-request-pseudo-article (props)
9012   (cond ((assq 'execute props)
9013          (gnus-execute-command (cdr (assq 'execute props)))))
9014   (let ((gnus-current-article (gnus-summary-article-number)))
9015     (gnus-run-hooks 'gnus-mark-article-hook)))
9016
9017 (defun gnus-execute-command (command &optional automatic)
9018   (save-excursion
9019     (gnus-article-setup-buffer)
9020     (set-buffer gnus-article-buffer)
9021     (setq buffer-read-only nil)
9022     (let ((command (if automatic command
9023                      (read-string "Command: " (cons command 0)))))
9024       (erase-buffer)
9025       (insert "$ " command "\n\n")
9026       (if gnus-view-pseudo-asynchronously
9027           (start-process "gnus-execute" (current-buffer) shell-file-name
9028                          shell-command-switch command)
9029         (call-process shell-file-name nil t nil
9030                       shell-command-switch command)))))
9031
9032 ;; Summary kill commands.
9033
9034 (defun gnus-summary-edit-global-kill (article)
9035   "Edit the \"global\" kill file."
9036   (interactive (list (gnus-summary-article-number)))
9037   (gnus-group-edit-global-kill article))
9038
9039 (defun gnus-summary-edit-local-kill ()
9040   "Edit a local kill file applied to the current newsgroup."
9041   (interactive)
9042   (setq gnus-current-headers (gnus-summary-article-header))
9043   (gnus-group-edit-local-kill
9044    (gnus-summary-article-number) gnus-newsgroup-name))
9045
9046 ;;; Header reading.
9047
9048 (defun gnus-read-header (id &optional header)
9049   "Read the headers of article ID and enter them into the Gnus system."
9050   (let ((group gnus-newsgroup-name)
9051         (gnus-override-method
9052          (and (gnus-news-group-p gnus-newsgroup-name)
9053               gnus-refer-article-method))
9054         where)
9055     ;; First we check to see whether the header in question is already
9056     ;; fetched.
9057     (if (stringp id)
9058         ;; This is a Message-ID.
9059         (setq header (or header (gnus-id-to-header id)))
9060       ;; This is an article number.
9061       (setq header (or header (gnus-summary-article-header id))))
9062     (if (and header
9063              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9064         ;; We have found the header.
9065         header
9066       ;; If this is a sparse article, we have to nix out its
9067       ;; previous entry in the thread hashtb.
9068       (when (and header
9069                  (gnus-summary-article-sparse-p (mail-header-number header)))
9070         (let* ((parent (gnus-parent-id (mail-header-references header)))
9071                (thread (and parent (gnus-id-to-thread parent))))
9072           (when thread
9073             (delq (assq header thread) thread))))
9074       ;; We have to really fetch the header to this article.
9075       (save-excursion
9076         (set-buffer nntp-server-buffer)
9077         (when (setq where (gnus-request-head id group))
9078           (nnheader-fold-continuation-lines)
9079           (goto-char (point-max))
9080           (insert ".\n")
9081           (goto-char (point-min))
9082           (insert "211 ")
9083           (princ (cond
9084                   ((numberp id) id)
9085                   ((cdr where) (cdr where))
9086                   (header (mail-header-number header))
9087                   (t gnus-reffed-article-number))
9088                  (current-buffer))
9089           (insert " Article retrieved.\n"))
9090         (if (or (not where)
9091                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9092             ()                          ; Malformed head.
9093           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9094             (when (and (stringp id)
9095                        (not (string= (gnus-group-real-name group)
9096                                      (car where))))
9097               ;; If we fetched by Message-ID and the article came
9098               ;; from a different group, we fudge some bogus article
9099               ;; numbers for this article.
9100               (mail-header-set-number header gnus-reffed-article-number))
9101             (save-excursion
9102               (set-buffer gnus-summary-buffer)
9103               (decf gnus-reffed-article-number)
9104               (gnus-remove-header (mail-header-number header))
9105               (push header gnus-newsgroup-headers)
9106               (setq gnus-current-headers header)
9107               (push (mail-header-number header) gnus-newsgroup-limit)))
9108           header)))))
9109
9110 (defun gnus-remove-header (number)
9111   "Remove header NUMBER from `gnus-newsgroup-headers'."
9112   (if (and gnus-newsgroup-headers
9113            (= number (mail-header-number (car gnus-newsgroup-headers))))
9114       (pop gnus-newsgroup-headers)
9115     (let ((headers gnus-newsgroup-headers))
9116       (while (and (cdr headers)
9117                   (not (= number (mail-header-number (cadr headers)))))
9118         (pop headers))
9119       (when (cdr headers)
9120         (setcdr headers (cddr headers))))))
9121
9122 ;;;
9123 ;;; summary highlights
9124 ;;;
9125
9126 (defun gnus-highlight-selected-summary ()
9127   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9128   ;; Highlight selected article in summary buffer
9129   (when gnus-summary-selected-face
9130     (save-excursion
9131       (let* ((beg (progn (beginning-of-line) (point)))
9132              (end (progn (end-of-line) (point)))
9133              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9134              (from (if (get-text-property beg gnus-mouse-face-prop)
9135                        beg
9136                      (or (next-single-property-change
9137                           beg gnus-mouse-face-prop nil end)
9138                          beg)))
9139              (to
9140               (if (= from end)
9141                   (- from 2)
9142                 (or (next-single-property-change
9143                      from gnus-mouse-face-prop nil end)
9144                     end))))
9145         ;; If no mouse-face prop on line we will have to = from = end,
9146         ;; so we highlight the entire line instead.
9147         (when (= (+ to 2) from)
9148           (setq from beg)
9149           (setq to end))
9150         (if gnus-newsgroup-selected-overlay
9151             ;; Move old overlay.
9152             (gnus-move-overlay
9153              gnus-newsgroup-selected-overlay from to (current-buffer))
9154           ;; Create new overlay.
9155           (gnus-overlay-put
9156            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9157            'face gnus-summary-selected-face))))))
9158
9159 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9160 (defun gnus-summary-highlight-line ()
9161   "Highlight current line according to `gnus-summary-highlight'."
9162   (let* ((list gnus-summary-highlight)
9163          (p (point))
9164          (end (progn (end-of-line) (point)))
9165          ;; now find out where the line starts and leave point there.
9166          (beg (progn (beginning-of-line) (point)))
9167          (article (gnus-summary-article-number))
9168          (score (or (cdr (assq (or article gnus-current-article)
9169                                gnus-newsgroup-scored))
9170                     gnus-summary-default-score 0))
9171          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9172          (inhibit-read-only t))
9173     ;; Eval the cars of the lists until we find a match.
9174     (let ((default gnus-summary-default-score))
9175       (while (and list
9176                   (not (eval (caar list))))
9177         (setq list (cdr list))))
9178     (let ((face (cdar list)))
9179       (unless (eq face (get-text-property beg 'face))
9180         (gnus-put-text-property-excluding-characters-with-faces
9181          beg end 'face
9182          (setq face (if (boundp face) (symbol-value face) face)))
9183         (when gnus-summary-highlight-line-function
9184           (funcall gnus-summary-highlight-line-function article face))))
9185     (goto-char p)))
9186
9187 (defun gnus-update-read-articles (group unread &optional compute)
9188   "Update the list of read articles in GROUP."
9189   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9190          (entry (gnus-gethash group gnus-newsrc-hashtb))
9191          (info (nth 2 entry))
9192          (prev 1)
9193          (unread (sort (copy-sequence unread) '<))
9194          read)
9195     (if (or (not info) (not active))
9196         ;; There is no info on this group if it was, in fact,
9197         ;; killed.  Gnus stores no information on killed groups, so
9198         ;; there's nothing to be done.
9199         ;; One could store the information somewhere temporarily,
9200         ;; perhaps...  Hmmm...
9201         ()
9202       ;; Remove any negative articles numbers.
9203       (while (and unread (< (car unread) 0))
9204         (setq unread (cdr unread)))
9205       ;; Remove any expired article numbers
9206       (while (and unread (< (car unread) (car active)))
9207         (setq unread (cdr unread)))
9208       ;; Compute the ranges of read articles by looking at the list of
9209       ;; unread articles.
9210       (while unread
9211         (when (/= (car unread) prev)
9212           (push (if (= prev (1- (car unread))) prev
9213                   (cons prev (1- (car unread))))
9214                 read))
9215         (setq prev (1+ (car unread)))
9216         (setq unread (cdr unread)))
9217       (when (<= prev (cdr active))
9218         (push (cons prev (cdr active)) read))
9219       (setq read (if (> (length read) 1) (nreverse read) read))
9220       (if compute
9221           read
9222         (save-excursion
9223           (set-buffer gnus-group-buffer)
9224           (gnus-undo-register
9225             `(progn
9226                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9227                (gnus-info-set-read ',info ',(gnus-info-read info))
9228                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9229                (gnus-group-update-group ,group t))))
9230        ;; Propagate the read marks to the backend.
9231        (if (gnus-check-backend-function 'request-set-mark group)
9232            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9233                  (add (gnus-remove-from-range read (gnus-info-read info))))
9234              (when (or add del)
9235                (unless (gnus-check-group group)
9236                  (error "Can't open server for %s" group))
9237                (gnus-request-set-mark
9238                 group (delq nil (list (if add (list add 'add '(read)))
9239                                       (if del (list del 'del '(read)))))))))
9240         ;; Enter this list into the group info.
9241         (gnus-info-set-read info read)
9242         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9243         (gnus-get-unread-articles-in-group info (gnus-active group))
9244         t))))
9245
9246 (defun gnus-offer-save-summaries ()
9247   "Offer to save all active summary buffers."
9248   (save-excursion
9249     (let ((buflist (buffer-list))
9250           buffers bufname)
9251       ;; Go through all buffers and find all summaries.
9252       (while buflist
9253         (and (setq bufname (buffer-name (car buflist)))
9254              (string-match "Summary" bufname)
9255              (save-excursion
9256                (set-buffer bufname)
9257                ;; We check that this is, indeed, a summary buffer.
9258                (and (eq major-mode 'gnus-summary-mode)
9259                     ;; Also make sure this isn't bogus.
9260                     gnus-newsgroup-prepared
9261                     ;; Also make sure that this isn't a dead summary buffer.
9262                     (not gnus-dead-summary-mode)))
9263              (push bufname buffers))
9264         (setq buflist (cdr buflist)))
9265       ;; Go through all these summary buffers and offer to save them.
9266       (when buffers
9267         (map-y-or-n-p
9268          "Update summary buffer %s? "
9269          (lambda (buf)
9270            (switch-to-buffer buf)
9271            (gnus-summary-exit))
9272          buffers)))))
9273
9274
9275 ;;; @ for mime-partial
9276 ;;;
9277
9278 (defun gnus-request-partial-message ()
9279   (save-excursion
9280     (let ((number (gnus-summary-article-number))
9281           (group gnus-newsgroup-name)
9282           (mother gnus-article-buffer))
9283       (set-buffer (get-buffer-create " *Partial Article*"))
9284       (erase-buffer)
9285       (setq mime-preview-buffer mother)
9286       (gnus-request-article-this-buffer number group)
9287       (mime-parse-buffer)
9288       )))
9289
9290 (autoload 'mime-combine-message/partial-pieces-automatically
9291   "mime-partial"
9292   "Internal method to combine message/partial messages automatically.")
9293
9294 (mime-add-condition
9295  'action '((type . message)(subtype . partial)
9296            (major-mode . gnus-original-article-mode)
9297            (method . mime-combine-message/partial-pieces-automatically)
9298            (summary-buffer-exp . gnus-summary-buffer)
9299            (request-partial-message-method . gnus-request-partial-message)
9300            ))
9301
9302
9303 ;;; @ for message/rfc822
9304 ;;;
9305
9306 (defun gnus-mime-extract-message/rfc822 (entity situation)
9307   (let (group article num cwin swin cur)
9308     (with-current-buffer (mime-entity-buffer entity)
9309       (save-restriction
9310         (narrow-to-region (mime-entity-body-start entity)
9311                           (mime-entity-body-end entity))
9312         (setq group (or (cdr (assq 'group situation))
9313                         (completing-read "Group: "
9314                                          gnus-active-hashtb
9315                                          nil
9316                                          (gnus-read-active-file-p)
9317                                          gnus-newsgroup-name))
9318               article (gnus-request-accept-article group)
9319               )
9320         ))
9321     (when (and (consp article)
9322                (numberp (setq article (cdr article))))
9323       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9324             cwin (get-buffer-window (current-buffer) t)
9325             )
9326       (save-window-excursion
9327         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9328             (select-window swin)
9329           (set-buffer gnus-summary-buffer)
9330           )
9331         (setq cur gnus-current-article)
9332         (forward-line num)
9333         (let (gnus-show-threads)
9334           (gnus-summary-goto-subject article t)
9335           )
9336         (gnus-summary-clear-mark-forward 1)
9337         (gnus-summary-goto-subject cur)
9338         )
9339       (when (and cwin (window-frame cwin))
9340         (select-frame (window-frame cwin))
9341         )
9342       (when (boundp 'mime-acting-situation-to-override)
9343         (set-alist 'mime-acting-situation-to-override
9344                    'group
9345                    group)
9346         (set-alist 'mime-acting-situation-to-override
9347                    'after-method
9348                    `(progn
9349                       (save-current-buffer
9350                         (set-buffer gnus-group-buffer)
9351                         (gnus-activate-group ,group)
9352                         )
9353                       (gnus-summary-goto-article ,cur
9354                                                  gnus-show-all-headers)
9355                       ))
9356         (set-alist 'mime-acting-situation-to-override
9357                    'number num)
9358         )
9359       )))
9360
9361 (mime-add-condition
9362  'action '((type . message)(subtype . rfc822)
9363            (major-mode . gnus-original-article-mode)
9364            (method . gnus-mime-extract-message/rfc822)
9365            (mode . "extract")
9366            ))
9367
9368 (mime-add-condition
9369  'action '((type . message)(subtype . news)
9370            (major-mode . gnus-original-article-mode)
9371            (method . gnus-mime-extract-message/rfc822)
9372            (mode . "extract")
9373            ))
9374
9375 (defun gnus-mime-extract-multipart (entity situation)
9376   (let ((children (mime-entity-children entity))
9377         mime-acting-situation-to-override
9378         f)
9379     (while children
9380       (mime-play-entity (car children)
9381                         (cons (assq 'mode situation)
9382                               mime-acting-situation-to-override))
9383       (setq children (cdr children)))
9384     (if (setq f (cdr (assq 'after-method
9385                            mime-acting-situation-to-override)))
9386         (eval f)
9387       )))  
9388
9389 (mime-add-condition
9390  'action '((type . multipart)
9391            (method . gnus-mime-extract-multipart)
9392            (mode . "extract")
9393            )
9394  'with-default)
9395
9396
9397 ;;; @ end
9398 ;;;
9399
9400 (defun gnus-summary-setup-default-charset ()
9401   "Setup newsgroup default charset."
9402   (if (equal gnus-newsgroup-name "nndraft:drafts")
9403       (setq gnus-newsgroup-charset nil)
9404   (let* ((name (and gnus-newsgroup-name
9405                    (gnus-group-real-name gnus-newsgroup-name)))
9406          (ignored-charsets 
9407           (or gnus-newsgroup-ephemeral-ignored-charsets
9408               (append
9409                (and gnus-newsgroup-name
9410                     (or (gnus-group-find-parameter gnus-newsgroup-name
9411                                                    'ignored-charsets t)
9412                         (let ((alist gnus-group-ignored-charsets-alist)
9413                            elem (charsets nil))
9414                           (while (setq elem (pop alist))
9415                             (when (and name
9416                                        (string-match (car elem) name))
9417                               (setq alist nil
9418                                     charsets (cdr elem))))
9419                           charsets))))
9420               gnus-newsgroup-ignored-charsets)))
9421     (setq gnus-newsgroup-charset
9422           (or gnus-newsgroup-ephemeral-charset
9423               (and gnus-newsgroup-name
9424                    (or (gnus-group-find-parameter gnus-newsgroup-name
9425                                                   'charset)
9426                        (let ((alist gnus-group-charset-alist)
9427                              elem (charset nil))
9428                          (while (setq elem (pop alist))
9429                            (when (and name
9430                                       (string-match (car elem) name))
9431                              (setq alist nil
9432                                    charset (cadr elem))))
9433                          charset)))
9434               gnus-default-charset))
9435     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9436          ignored-charsets))))
9437
9438 ;;;
9439 ;;; Mime Commands
9440 ;;;
9441
9442 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9443   "Display the current article buffer fully MIME-buttonized.
9444 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9445 treated as multipart/mixed."
9446   (interactive "P")
9447   (require 'gnus-art)
9448   (let ((gnus-unbuttonized-mime-types nil)
9449         (gnus-mime-display-multipart-as-mixed show-all-parts))
9450     (gnus-summary-show-article)))
9451
9452 (defun gnus-summary-repair-multipart (article)
9453   "Add a Content-Type header to a multipart article without one."
9454   (interactive (list (gnus-summary-article-number)))
9455   (gnus-with-article article
9456     (message-narrow-to-head)
9457     (goto-char (point-max))
9458     (widen)
9459     (when (search-forward "\n--" nil t)
9460       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9461         (message-narrow-to-head)
9462         (message-remove-header "Mime-Version")
9463         (message-remove-header "Content-Type")
9464         (goto-char (point-max))
9465         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9466                         separator))
9467         (insert "Mime-Version: 1.0\n")
9468         (widen))))
9469   (let (gnus-mark-article-hook)
9470     (gnus-summary-select-article t t nil article)))
9471
9472 (defun gnus-summary-toggle-display-buttonized ()
9473   "Toggle the buttonizing of the article buffer."
9474   (interactive)
9475   (require 'gnus-art)
9476   (if (setq gnus-inhibit-mime-unbuttonizing
9477             (not gnus-inhibit-mime-unbuttonizing))
9478       (let ((gnus-unbuttonized-mime-types nil))
9479         (gnus-summary-show-article))
9480     (gnus-summary-show-article)))
9481
9482 ;;;
9483 ;;; with article
9484 ;;;
9485
9486 (defmacro gnus-with-article (article &rest forms)
9487   "Select ARTICLE and perform FORMS in the original article buffer.
9488 Then replace the article with the result."
9489   `(progn
9490      ;; We don't want the article to be marked as read.
9491      (let (gnus-mark-article-hook)
9492        (gnus-summary-select-article t t nil ,article))
9493      (set-buffer gnus-original-article-buffer)
9494      ,@forms
9495      (if (not (gnus-check-backend-function
9496                'request-replace-article (car gnus-article-current)))
9497          (gnus-message 5 "Read-only group; not replacing")
9498        (unless (gnus-request-replace-article
9499                 ,article (car gnus-article-current)
9500                 (current-buffer) t)
9501          (error "Couldn't replace article")))
9502      ;; The cache and backlog have to be flushed somewhat.
9503      (when gnus-keep-backlog
9504        (gnus-backlog-remove-article
9505         (car gnus-article-current) (cdr gnus-article-current)))
9506      (when gnus-use-cache
9507        (gnus-cache-update-article
9508         (car gnus-article-current) (cdr gnus-article-current)))))
9509
9510 (put 'gnus-with-article 'lisp-indent-function 1)
9511 (put 'gnus-with-article 'edebug-form-spec '(form body))
9512
9513 ;;;
9514 ;;; Generic summary marking commands
9515 ;;;
9516
9517 (defvar gnus-summary-marking-alist
9518   '((read gnus-del-mark "d")
9519     (unread gnus-unread-mark "u")
9520     (ticked gnus-ticked-mark "!")
9521     (dormant gnus-dormant-mark "?")
9522     (expirable gnus-expirable-mark "e"))
9523   "An alist of names/marks/keystrokes.")
9524
9525 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9526 (defvar gnus-summary-mark-map)
9527
9528 (defun gnus-summary-make-all-marking-commands ()
9529   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9530   (dolist (elem gnus-summary-marking-alist)
9531     (apply 'gnus-summary-make-marking-command elem)))
9532
9533 (defun gnus-summary-make-marking-command (name mark keystroke)
9534   (let ((map (make-sparse-keymap)))
9535     (define-key gnus-summary-generic-mark-map keystroke map)
9536     (dolist (lway `((next "next" next nil "n")
9537                     (next-unread "next unread" next t "N")
9538                     (prev "previous" prev nil "p")
9539                     (prev-unread "previous unread" prev t "P")
9540                     (nomove "" nil nil ,keystroke)))
9541       (let ((func (gnus-summary-make-marking-command-1
9542                    mark (car lway) lway name)))
9543         (setq func (eval func))
9544         (define-key map (nth 4 lway) func)))))
9545       
9546 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9547   `(defun ,(intern
9548             (format "gnus-summary-put-mark-as-%s%s"
9549                     name (if (eq way 'nomove)
9550                              ""
9551                            (concat "-" (symbol-name way)))))
9552      (n)
9553      ,(format
9554        "Mark the current article as %s%s.
9555 If N, the prefix, then repeat N times.
9556 If N is negative, move in reverse order.
9557 The difference between N and the actual number of articles marked is
9558 returned."
9559        name (cadr lway))
9560      (interactive "p")
9561      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9562     
9563 (defun gnus-summary-generic-mark (n mark move unread)
9564   "Mark N articles with MARK."
9565   (unless (eq major-mode 'gnus-summary-mode)
9566     (error "This command can only be used in the summary buffer"))
9567   (gnus-summary-show-thread)
9568   (let ((nummove
9569          (cond
9570           ((eq move 'next) 1)
9571           ((eq move 'prev) -1)
9572           (t 0))))
9573     (if (zerop nummove)
9574         (setq n 1)
9575       (when (< n 0)
9576         (setq n (abs n)
9577               nummove (* -1 nummove))))
9578     (while (and (> n 0)
9579                 (gnus-summary-mark-article nil mark)
9580                 (zerop (gnus-summary-next-subject nummove unread t)))
9581       (setq n (1- n)))
9582     (when (/= 0 n)
9583       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9584     (gnus-summary-recenter)
9585     (gnus-summary-position-point)
9586     (gnus-set-mode-line 'summary)
9587     n))
9588
9589 (gnus-summary-make-all-marking-commands)
9590
9591 (gnus-ems-redefine)
9592
9593 (provide 'gnus-sum)
9594
9595 (run-hooks 'gnus-sum-load-hook)
9596
9597 ;;; gnus-sum.el ends here