fb0663f16a5f108059504f49ec7127daa1433035
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, many commands will go to the next unread article.
172 This applies to marking commands as well as other commands that
173 \"naturally\" select the next article, like, for instance, `SPC' at
174 the end of an article.
175 If nil, only the marking commands will go to the next (un)read article.
176 If `never', commands that usually go to the next unread article, will
177 go to the next article, whether it is read or not."
178   :group 'gnus-summary-marks
179   :link '(custom-manual "(gnus)Setting Marks")
180   :type '(choice (const :tag "off" nil)
181                  (const never)
182                  (sexp :menu-tag "on" t)))
183
184 (defcustom gnus-summary-default-score 0
185   "*Default article score level.
186 All scores generated by the score files will be added to this score.
187 If this variable is nil, scoring will be disabled."
188   :group 'gnus-score-default
189   :type '(choice (const :tag "disable")
190                  integer))
191
192 (defcustom gnus-summary-zcore-fuzz 0
193   "*Fuzziness factor for the zcore in the summary buffer.
194 Articles with scores closer than this to `gnus-summary-default-score'
195 will not be marked."
196   :group 'gnus-summary-format
197   :type 'integer)
198
199 (defcustom gnus-simplify-subject-fuzzy-regexp nil
200   "*Strings to be removed when doing fuzzy matches.
201 This can either be a regular expression or list of regular expressions
202 that will be removed from subject strings if fuzzy subject
203 simplification is selected."
204   :group 'gnus-thread
205   :type '(repeat regexp))
206
207 (defcustom gnus-show-threads t
208   "*If non-nil, display threads in summary mode."
209   :group 'gnus-thread
210   :type 'boolean)
211
212 (defcustom gnus-thread-hide-subtree nil
213   "*If non-nil, hide all threads initially.
214 If threads are hidden, you have to run the command
215 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
216 to expose hidden threads."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-killed t
221   "*If non-nil, hide killed threads automatically."
222   :group 'gnus-thread
223   :type 'boolean)
224
225 (defcustom gnus-thread-ignore-subject t
226   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
227 If nil, articles that have different subjects from their parents will
228 start separate threads."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-operation-ignore-subject t
233   "*If non-nil, subjects will be ignored when doing thread commands.
234 This affects commands like `gnus-summary-kill-thread' and
235 `gnus-summary-lower-thread'.
236
237 If this variable is nil, articles in the same thread with different
238 subjects will not be included in the operation in question.  If this
239 variable is `fuzzy', only articles that have subjects that are fuzzily
240 equal will be included."
241   :group 'gnus-thread
242   :type '(choice (const :tag "off" nil)
243                  (const fuzzy)
244                  (sexp :tag "on" t)))
245
246 (defcustom gnus-thread-indent-level 4
247   "*Number that says how much each sub-thread should be indented."
248   :group 'gnus-thread
249   :type 'integer)
250
251 (defcustom gnus-auto-extend-newsgroup t
252   "*If non-nil, extend newsgroup forward and backward when requested."
253   :group 'gnus-summary-choose
254   :type 'boolean)
255
256 (defcustom gnus-auto-select-first t
257   "*If nil, don't select the first unread article when entering a group.
258 If this variable is `best', select the highest-scored unread article
259 in the group.  If t, select the first unread article.
260
261 This variable can also be a function to place point on a likely
262 subject line.  Useful values include `gnus-summary-first-unread-subject',
263 `gnus-summary-first-unread-article' and
264 `gnus-summary-best-unread-article'.
265
266 If you want to prevent automatic selection of the first unread article
267 in some newsgroups, set the variable to nil in
268 `gnus-select-group-hook'."
269   :group 'gnus-group-select
270   :type '(choice (const :tag "none" nil)
271                  (const best)
272                  (sexp :menu-tag "first" t)
273                  (function-item gnus-summary-first-unread-subject)
274                  (function-item gnus-summary-first-unread-article)
275                  (function-item gnus-summary-best-unread-article)))
276
277 (defcustom gnus-auto-select-next t
278   "*If non-nil, offer to go to the next group from the end of the previous.
279 If the value is t and the next newsgroup is empty, Gnus will exit
280 summary mode and go back to group mode.  If the value is neither nil
281 nor t, Gnus will select the following unread newsgroup.  In
282 particular, if the value is the symbol `quietly', the next unread
283 newsgroup will be selected without any confirmation, and if it is
284 `almost-quietly', the next group will be selected without any
285 confirmation if you are located on the last article in the group.
286 Finally, if this variable is `slightly-quietly', the `Z n' command
287 will go to the next group without confirmation."
288   :group 'gnus-summary-maneuvering
289   :type '(choice (const :tag "off" nil)
290                  (const quietly)
291                  (const almost-quietly)
292                  (const slightly-quietly)
293                  (sexp :menu-tag "on" t)))
294
295 (defcustom gnus-auto-select-same nil
296   "*If non-nil, select the next article with the same subject.
297 If there are no more articles with the same subject, go to
298 the first unread article."
299   :group 'gnus-summary-maneuvering
300   :type 'boolean)
301
302 (defcustom gnus-summary-check-current nil
303   "*If non-nil, consider the current article when moving.
304 The \"unread\" movement commands will stay on the same line if the
305 current article is unread."
306   :group 'gnus-summary-maneuvering
307   :type 'boolean)
308
309 (defcustom gnus-auto-center-summary t
310   "*If non-nil, always center the current summary buffer.
311 In particular, if `vertical' do only vertical recentering.  If non-nil
312 and non-`vertical', do both horizontal and vertical recentering."
313   :group 'gnus-summary-maneuvering
314   :type '(choice (const :tag "none" nil)
315                  (const vertical)
316                  (integer :tag "height")
317                  (sexp :menu-tag "both" t)))
318
319 (defcustom gnus-show-all-headers nil
320   "*If non-nil, don't hide any headers."
321   :group 'gnus-article-hiding
322   :group 'gnus-article-headers
323   :type 'boolean)
324
325 (defcustom gnus-summary-ignore-duplicates nil
326   "*If non-nil, ignore articles with identical Message-ID headers."
327   :group 'gnus-summary
328   :type 'boolean)
329
330 (defcustom gnus-single-article-buffer t
331   "*If non-nil, display all articles in the same buffer.
332 If nil, each group will get its own article buffer."
333   :group 'gnus-article-various
334   :type 'boolean)
335
336 (defcustom gnus-break-pages t
337   "*If non-nil, do page breaking on articles.
338 The page delimiter is specified by the `gnus-page-delimiter'
339 variable."
340   :group 'gnus-article-various
341   :type 'boolean)
342
343 (defcustom gnus-move-split-methods nil
344   "*Variable used to suggest where articles are to be moved to.
345 It uses the same syntax as the `gnus-split-methods' variable."
346   :group 'gnus-summary-mail
347   :type '(repeat (choice (list :value (fun) function)
348                          (cons :value ("" "") regexp (repeat string))
349                          (sexp :value nil))))
350
351 (defcustom gnus-unread-mark ?  ;Whitespace
352   "*Mark used for unread articles."
353   :group 'gnus-summary-marks
354   :type 'character)
355
356 (defcustom gnus-ticked-mark ?!
357   "*Mark used for ticked articles."
358   :group 'gnus-summary-marks
359   :type 'character)
360
361 (defcustom gnus-dormant-mark ??
362   "*Mark used for dormant articles."
363   :group 'gnus-summary-marks
364   :type 'character)
365
366 (defcustom gnus-del-mark ?r
367   "*Mark used for del'd articles."
368   :group 'gnus-summary-marks
369   :type 'character)
370
371 (defcustom gnus-read-mark ?R
372   "*Mark used for read articles."
373   :group 'gnus-summary-marks
374   :type 'character)
375
376 (defcustom gnus-expirable-mark ?E
377   "*Mark used for expirable articles."
378   :group 'gnus-summary-marks
379   :type 'character)
380
381 (defcustom gnus-killed-mark ?K
382   "*Mark used for killed articles."
383   :group 'gnus-summary-marks
384   :type 'character)
385
386 (defcustom gnus-souped-mark ?F
387   "*Mark used for killed articles."
388   :group 'gnus-summary-marks
389   :type 'character)
390
391 (defcustom gnus-kill-file-mark ?X
392   "*Mark used for articles killed by kill files."
393   :group 'gnus-summary-marks
394   :type 'character)
395
396 (defcustom gnus-low-score-mark ?Y
397   "*Mark used for articles with a low score."
398   :group 'gnus-summary-marks
399   :type 'character)
400
401 (defcustom gnus-catchup-mark ?C
402   "*Mark used for articles that are caught up."
403   :group 'gnus-summary-marks
404   :type 'character)
405
406 (defcustom gnus-replied-mark ?A
407   "*Mark used for articles that have been replied to."
408   :group 'gnus-summary-marks
409   :type 'character)
410
411 (defcustom gnus-cached-mark ?*
412   "*Mark used for articles that are in the cache."
413   :group 'gnus-summary-marks
414   :type 'character)
415
416 (defcustom gnus-saved-mark ?S
417   "*Mark used for articles that have been saved to."
418   :group 'gnus-summary-marks
419   :type 'character)
420
421 (defcustom gnus-ancient-mark ?O
422   "*Mark used for ancient articles."
423   :group 'gnus-summary-marks
424   :type 'character)
425
426 (defcustom gnus-sparse-mark ?Q
427   "*Mark used for sparsely reffed articles."
428   :group 'gnus-summary-marks
429   :type 'character)
430
431 (defcustom gnus-canceled-mark ?G
432   "*Mark used for canceled articles."
433   :group 'gnus-summary-marks
434   :type 'character)
435
436 (defcustom gnus-duplicate-mark ?M
437   "*Mark used for duplicate articles."
438   :group 'gnus-summary-marks
439   :type 'character)
440
441 (defcustom gnus-undownloaded-mark ?@
442   "*Mark used for articles that weren't downloaded."
443   :group 'gnus-summary-marks
444   :type 'character)
445
446 (defcustom gnus-downloadable-mark ?%
447   "*Mark used for articles that are to be downloaded."
448   :group 'gnus-summary-marks
449   :type 'character)
450
451 (defcustom gnus-unsendable-mark ?=
452   "*Mark used for articles that won't be sent."
453   :group 'gnus-summary-marks
454   :type 'character)
455
456 (defcustom gnus-score-over-mark ?+
457   "*Score mark used for articles with high scores."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-score-below-mark ?-
462   "*Score mark used for articles with low scores."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-empty-thread-mark ?  ;Whitespace
467   "*There is no thread under the article."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-not-empty-thread-mark ?=
472   "*There is a thread under the article."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-view-pseudo-asynchronously nil
477   "*If non-nil, Gnus will view pseudo-articles asynchronously."
478   :group 'gnus-extract-view
479   :type 'boolean)
480
481 (defcustom gnus-auto-expirable-marks
482   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
483         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
484         gnus-souped-mark gnus-duplicate-mark)
485   "*The list of marks converted into expiration if a group is auto-expirable."
486   :group 'gnus-summary
487   :type '(repeat character))
488
489 (defcustom gnus-inhibit-user-auto-expire t
490   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
491   :group 'gnus-summary
492   :type 'boolean)
493
494 (defcustom gnus-view-pseudos nil
495   "*If `automatic', pseudo-articles will be viewed automatically.
496 If `not-confirm', pseudos will be viewed automatically, and the user
497 will not be asked to confirm the command."
498   :group 'gnus-extract-view
499   :type '(choice (const :tag "off" nil)
500                  (const automatic)
501                  (const not-confirm)))
502
503 (defcustom gnus-view-pseudos-separately t
504   "*If non-nil, one pseudo-article will be created for each file to be viewed.
505 If nil, all files that use the same viewing command will be given as a
506 list of parameters to that command."
507   :group 'gnus-extract-view
508   :type 'boolean)
509
510 (defcustom gnus-insert-pseudo-articles t
511   "*If non-nil, insert pseudo-articles when decoding articles."
512   :group 'gnus-extract-view
513   :type 'boolean)
514
515 (defcustom gnus-summary-dummy-line-format
516   "  %(:                          :%) %S\n"
517   "*The format specification for the dummy roots in the summary buffer.
518 It works along the same lines as a normal formatting string,
519 with some simple extensions.
520
521 %S  The subject"
522   :group 'gnus-threading
523   :type 'string)
524
525 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
526   "*The format specification for the summary mode line.
527 It works along the same lines as a normal formatting string,
528 with some simple extensions:
529
530 %G  Group name
531 %p  Unprefixed group name
532 %A  Current article number
533 %z  Current article score
534 %V  Gnus version
535 %U  Number of unread articles in the group
536 %e  Number of unselected articles in the group
537 %Z  A string with unread/unselected article counts
538 %g  Shortish group name
539 %S  Subject of the current article
540 %u  User-defined spec
541 %s  Current score file name
542 %d  Number of dormant articles
543 %r  Number of articles that have been marked as read in this session
544 %E  Number of articles expunged by the score files"
545   :group 'gnus-summary-format
546   :type 'string)
547
548 (defcustom gnus-list-identifiers nil
549   "Regexp that matches list identifiers to be removed from subject.
550 This can also be a list of regexps."
551   :group 'gnus-summary-format
552   :group 'gnus-article-hiding
553   :type '(choice (const :tag "none" nil)
554                  (regexp :value ".*")
555                  (repeat :value (".*") regexp)))
556
557 (defcustom gnus-summary-mark-below 0
558   "*Mark all articles with a score below this variable as read.
559 This variable is local to each summary buffer and usually set by the
560 score file."
561   :group 'gnus-score-default
562   :type 'integer)
563
564 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
565   "*List of functions used for sorting articles in the summary buffer.
566 This variable is only used when not using a threaded display."
567   :group 'gnus-summary-sort
568   :type '(repeat (choice (function-item gnus-article-sort-by-number)
569                          (function-item gnus-article-sort-by-author)
570                          (function-item gnus-article-sort-by-subject)
571                          (function-item gnus-article-sort-by-date)
572                          (function-item gnus-article-sort-by-score)
573                          (function :tag "other"))))
574
575 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
576   "*List of functions used for sorting threads in the summary buffer.
577 By default, threads are sorted by article number.
578
579 Each function takes two threads and return non-nil if the first thread
580 should be sorted before the other.  If you use more than one function,
581 the primary sort function should be the last.  You should probably
582 always include `gnus-thread-sort-by-number' in the list of sorting
583 functions -- preferably first.
584
585 Ready-made functions include `gnus-thread-sort-by-number',
586 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
587 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
588 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
589   :group 'gnus-summary-sort
590   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
591                          (function-item gnus-thread-sort-by-author)
592                          (function-item gnus-thread-sort-by-subject)
593                          (function-item gnus-thread-sort-by-date)
594                          (function-item gnus-thread-sort-by-score)
595                          (function-item gnus-thread-sort-by-total-score)
596                          (function :tag "other"))))
597
598 (defcustom gnus-thread-score-function '+
599   "*Function used for calculating the total score of a thread.
600
601 The function is called with the scores of the article and each
602 subthread and should then return the score of the thread.
603
604 Some functions you can use are `+', `max', or `min'."
605   :group 'gnus-summary-sort
606   :type 'function)
607
608 (defcustom gnus-summary-expunge-below nil
609   "All articles that have a score less than this variable will be expunged.
610 This variable is local to the summary buffers."
611   :group 'gnus-score-default
612   :type '(choice (const :tag "off" nil)
613                  integer))
614
615 (defcustom gnus-thread-expunge-below nil
616   "All threads that have a total score less than this variable will be expunged.
617 See `gnus-thread-score-function' for en explanation of what a
618 \"thread score\" is.
619
620 This variable is local to the summary buffers."
621   :group 'gnus-threading
622   :group 'gnus-score-default
623   :type '(choice (const :tag "off" nil)
624                  integer))
625
626 (defcustom gnus-summary-mode-hook nil
627   "*A hook for Gnus summary mode.
628 This hook is run before any variables are set in the summary buffer."
629   :group 'gnus-summary-various
630   :type 'hook)
631
632 (defcustom gnus-summary-menu-hook nil
633   "*Hook run after the creation of the summary mode menu."
634   :group 'gnus-summary-visual
635   :type 'hook)
636
637 (defcustom gnus-summary-exit-hook nil
638   "*A hook called on exit from the summary buffer.
639 It will be called with point in the group buffer."
640   :group 'gnus-summary-exit
641   :type 'hook)
642
643 (defcustom gnus-summary-prepare-hook nil
644   "*A hook called after the summary buffer has been generated.
645 If you want to modify the summary buffer, you can use this hook."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-summary-prepared-hook nil
650   "*A hook called as the last thing after the summary buffer has been generated."
651   :group 'gnus-summary-various
652   :type 'hook)
653
654 (defcustom gnus-summary-generate-hook nil
655   "*A hook run just before generating the summary buffer.
656 This hook is commonly used to customize threading variables and the
657 like."
658   :group 'gnus-summary-various
659   :type 'hook)
660
661 (defcustom gnus-select-group-hook nil
662   "*A hook called when a newsgroup is selected.
663
664 If you'd like to simplify subjects like the
665 `gnus-summary-next-same-subject' command does, you can use the
666 following hook:
667
668  (setq gnus-select-group-hook
669       (list
670         (lambda ()
671           (mapcar (lambda (header)
672                      (mail-header-set-subject
673                       header
674                       (gnus-simplify-subject
675                        (mail-header-subject header) 're-only)))
676                   gnus-newsgroup-headers))))"
677   :group 'gnus-group-select
678   :type 'hook)
679
680 (defcustom gnus-select-article-hook nil
681   "*A hook called when an article is selected."
682   :group 'gnus-summary-choose
683   :type 'hook)
684
685 (defcustom gnus-visual-mark-article-hook
686   (list 'gnus-highlight-selected-summary)
687   "*Hook run after selecting an article in the summary buffer.
688 It is meant to be used for highlighting the article in some way.  It
689 is not run if `gnus-visual' is nil."
690   :group 'gnus-summary-visual
691   :type 'hook)
692
693 (defcustom gnus-parse-headers-hook nil
694   "*A hook called before parsing the headers."
695   :group 'gnus-various
696   :type 'hook)
697
698 (defcustom gnus-exit-group-hook nil
699   "*A hook called when exiting summary mode.
700 This hook is not called from the non-updating exit commands like `Q'."
701   :group 'gnus-various
702   :type 'hook)
703
704 (defcustom gnus-summary-update-hook
705   (list 'gnus-summary-highlight-line)
706   "*A hook called when a summary line is changed.
707 The hook will not be called if `gnus-visual' is nil.
708
709 The default function `gnus-summary-highlight-line' will
710 highlight the line according to the `gnus-summary-highlight'
711 variable."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
716   "*A hook called when an article is selected for the first time.
717 The hook is intended to mark an article as read (or unread)
718 automatically when it is selected."
719   :group 'gnus-summary-choose
720   :type 'hook)
721
722 (defcustom gnus-group-no-more-groups-hook nil
723   "*A hook run when returning to group mode having no more (unread) groups."
724   :group 'gnus-group-select
725   :type 'hook)
726
727 (defcustom gnus-ps-print-hook nil
728   "*A hook run before ps-printing something from Gnus."
729   :group 'gnus-summary
730   :type 'hook)
731
732 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
733   "Face used for highlighting the current article in the summary buffer."
734   :group 'gnus-summary-visual
735   :type 'face)
736
737 (defcustom gnus-summary-highlight
738   '(((= mark gnus-canceled-mark)
739      . gnus-summary-cancelled-face)
740     ((and (> score default)
741           (or (= mark gnus-dormant-mark)
742               (= mark gnus-ticked-mark)))
743      . gnus-summary-high-ticked-face)
744     ((and (< score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-low-ticked-face)
748     ((or (= mark gnus-dormant-mark)
749          (= mark gnus-ticked-mark))
750      . gnus-summary-normal-ticked-face)
751     ((and (> score default) (= mark gnus-ancient-mark))
752      . gnus-summary-high-ancient-face)
753     ((and (< score default) (= mark gnus-ancient-mark))
754      . gnus-summary-low-ancient-face)
755     ((= mark gnus-ancient-mark)
756      . gnus-summary-normal-ancient-face)
757     ((and (> score default) (= mark gnus-unread-mark))
758      . gnus-summary-high-unread-face)
759     ((and (< score default) (= mark gnus-unread-mark))
760      . gnus-summary-low-unread-face)
761     ((= mark gnus-unread-mark)
762      . gnus-summary-normal-unread-face)
763     ((and (> score default) (memq mark (list gnus-downloadable-mark
764                                              gnus-undownloaded-mark)))
765      . gnus-summary-high-unread-face)
766     ((and (< score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-low-unread-face)
769     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
770      . gnus-summary-normal-unread-face)
771     ((> score default)
772      . gnus-summary-high-read-face)
773     ((< score default)
774      . gnus-summary-low-read-face)
775     (t
776      . gnus-summary-normal-read-face))
777   "*Controls the highlighting of summary buffer lines.
778
779 A list of (FORM . FACE) pairs.  When deciding how a a particular
780 summary line should be displayed, each form is evaluated.  The content
781 of the face field after the first true form is used.  You can change
782 how those summary lines are displayed, by editing the face field.
783
784 You can use the following variables in the FORM field.
785
786 score:   The articles score
787 default: The default article score.
788 below:   The score below which articles are automatically marked as read.
789 mark:    The articles mark."
790   :group 'gnus-summary-visual
791   :type '(repeat (cons (sexp :tag "Form" nil)
792                        face)))
793
794 (defcustom gnus-alter-header-function nil
795   "Function called to allow alteration of article header structures.
796 The function is called with one parameter, the article header vector,
797 which it may alter in any way.")
798
799 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
800   "Variable that says which function should be used to decode a string with encoded words.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
818     ("^relcom\\>" koi8-r)
819     ("^fido7\\>" koi8-r)
820     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
821     ("^israel\\>" iso-8859-1)
822     ("^han\\>" euc-kr)
823     ("^alt.chinese.text.big5\\>" chinese-big5)
824     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
825     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
826     (".*" iso-8859-1))
827   "Alist of regexps (to match group names) and default charsets to be used when reading."
828   :type '(repeat (list (regexp :tag "Group")
829                        (symbol :tag "Charset")))
830   :group 'gnus-charset)
831
832 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
833   "List of charsets that should be ignored.
834 When these charsets are used in the \"charset\" parameter, the
835 default charset will be used instead."
836   :type '(repeat symbol)
837   :group 'gnus-charset)
838
839 (defcustom gnus-group-ignored-charsets-alist 
840   '(("alt\\.chinese\\.text" iso-8859-1))
841   "Alist of regexps (to match group names) and charsets that should be ignored.
842 When these charsets are used in the \"charset\" parameter, the
843 default charset will be used instead."
844   :type '(repeat (cons (regexp :tag "Group")
845                        (repeat symbol)))
846   :group 'gnus-charset)
847
848 (defcustom gnus-group-highlight-words-alist nil
849   "Alist of group regexps and highlight regexps.
850 This variable uses the same syntax as `gnus-emphasis-alist'."
851   :type '(repeat (cons (regexp :tag "Group")
852                        (repeat (list (regexp :tag "Highlight regexp")
853                                      (number :tag "Group for entire word" 0)
854                                      (number :tag "Group for displayed part" 0)
855                                      (symbol :tag "Face" 
856                                              gnus-emphasis-highlight-words)))))
857   :group 'gnus-summary-visual)
858
859 (defcustom gnus-summary-show-article-charset-alist
860   nil
861   "Alist of number and charset.
862 The article will be shown with the charset corresponding to the
863 numbered argument.
864 For example: ((1 . cn-gb-2312) (2 . big5))."
865   :type '(repeat (cons (number :tag "Argument" 1)
866                        (symbol :tag "Charset")))
867   :group 'gnus-charset)
868
869 (defcustom gnus-preserve-marks t
870   "Whether marks are preserved when moving, copying and respooling messages."
871   :type 'boolean
872   :group 'gnus-summary-marks)
873
874 ;;; Internal variables
875
876 (defvar gnus-article-mime-handles nil)
877 (defvar gnus-article-decoded-p nil)
878 (defvar gnus-scores-exclude-files nil)
879 (defvar gnus-page-broken nil)
880 (defvar gnus-inhibit-mime-unbuttonizing nil)
881
882 (defvar gnus-original-article nil)
883 (defvar gnus-article-internal-prepare-hook nil)
884 (defvar gnus-newsgroup-process-stack nil)
885
886 (defvar gnus-thread-indent-array nil)
887 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
888 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
889   "Function called to sort the articles within a thread after it has been gathered together.")
890
891 ;; Avoid highlighting in kill files.
892 (defvar gnus-summary-inhibit-highlight nil)
893 (defvar gnus-newsgroup-selected-overlay nil)
894 (defvar gnus-inhibit-limiting nil)
895 (defvar gnus-newsgroup-adaptive-score-file nil)
896 (defvar gnus-current-score-file nil)
897 (defvar gnus-current-move-group nil)
898 (defvar gnus-current-copy-group nil)
899 (defvar gnus-current-crosspost-group nil)
900
901 (defvar gnus-newsgroup-dependencies nil)
902 (defvar gnus-newsgroup-adaptive nil)
903 (defvar gnus-summary-display-article-function nil)
904 (defvar gnus-summary-highlight-line-function nil
905   "Function called after highlighting a summary line.")
906
907 (defvar gnus-summary-line-format-alist
908   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
909     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
910     (?s gnus-tmp-subject-or-nil ?s)
911     (?n gnus-tmp-name ?s)
912     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
913         ?s)
914     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
915             gnus-tmp-from) ?s)
916     (?F gnus-tmp-from ?s)
917     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
918     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
919     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
920     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
921     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
922     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
923     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
924     (?L gnus-tmp-lines ?d)
925     (?I gnus-tmp-indentation ?s)
926     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
927     (?R gnus-tmp-replied ?c)
928     (?\[ gnus-tmp-opening-bracket ?c)
929     (?\] gnus-tmp-closing-bracket ?c)
930     (?\> (make-string gnus-tmp-level ? ) ?s)
931     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
932     (?i gnus-tmp-score ?d)
933     (?z gnus-tmp-score-char ?c)
934     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
935     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
936     (?U gnus-tmp-unread ?c)
937     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
938     (?t (gnus-summary-number-of-articles-in-thread
939          (and (boundp 'thread) (car thread)) gnus-tmp-level)
940         ?d)
941     (?e (gnus-summary-number-of-articles-in-thread
942          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
943         ?c)
944     (?u gnus-tmp-user-defined ?s)
945     (?P (gnus-pick-line-number) ?d))
946   "An alist of format specifications that can appear in summary lines,
947 and what variables they correspond with, along with the type of the
948 variable (string, integer, character, etc).")
949
950 (defvar gnus-summary-dummy-line-format-alist
951   `((?S gnus-tmp-subject ?s)
952     (?N gnus-tmp-number ?d)
953     (?u gnus-tmp-user-defined ?s)))
954
955 (defvar gnus-summary-mode-line-format-alist
956   `((?G gnus-tmp-group-name ?s)
957     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
958     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
959     (?A gnus-tmp-article-number ?d)
960     (?Z gnus-tmp-unread-and-unselected ?s)
961     (?V gnus-version ?s)
962     (?U gnus-tmp-unread-and-unticked ?d)
963     (?S gnus-tmp-subject ?s)
964     (?e gnus-tmp-unselected ?d)
965     (?u gnus-tmp-user-defined ?s)
966     (?d (length gnus-newsgroup-dormant) ?d)
967     (?t (length gnus-newsgroup-marked) ?d)
968     (?r (length gnus-newsgroup-reads) ?d)
969     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
970     (?E gnus-newsgroup-expunged-tally ?d)
971     (?s (gnus-current-score-file-nondirectory) ?s)))
972
973 (defvar gnus-last-search-regexp nil
974   "Default regexp for article search command.")
975
976 (defvar gnus-last-shell-command nil
977   "Default shell command on article.")
978
979 (defvar gnus-newsgroup-begin nil)
980 (defvar gnus-newsgroup-end nil)
981 (defvar gnus-newsgroup-last-rmail nil)
982 (defvar gnus-newsgroup-last-mail nil)
983 (defvar gnus-newsgroup-last-folder nil)
984 (defvar gnus-newsgroup-last-file nil)
985 (defvar gnus-newsgroup-auto-expire nil)
986 (defvar gnus-newsgroup-active nil)
987
988 (defvar gnus-newsgroup-data nil)
989 (defvar gnus-newsgroup-data-reverse nil)
990 (defvar gnus-newsgroup-limit nil)
991 (defvar gnus-newsgroup-limits nil)
992
993 (defvar gnus-newsgroup-unreads nil
994   "List of unread articles in the current newsgroup.")
995
996 (defvar gnus-newsgroup-unselected nil
997   "List of unselected unread articles in the current newsgroup.")
998
999 (defvar gnus-newsgroup-reads nil
1000   "Alist of read articles and article marks in the current newsgroup.")
1001
1002 (defvar gnus-newsgroup-expunged-tally nil)
1003
1004 (defvar gnus-newsgroup-marked nil
1005   "List of ticked articles in the current newsgroup (a subset of unread art).")
1006
1007 (defvar gnus-newsgroup-killed nil
1008   "List of ranges of articles that have been through the scoring process.")
1009
1010 (defvar gnus-newsgroup-cached nil
1011   "List of articles that come from the article cache.")
1012
1013 (defvar gnus-newsgroup-saved nil
1014   "List of articles that have been saved.")
1015
1016 (defvar gnus-newsgroup-kill-headers nil)
1017
1018 (defvar gnus-newsgroup-replied nil
1019   "List of articles that have been replied to in the current newsgroup.")
1020
1021 (defvar gnus-newsgroup-expirable nil
1022   "List of articles in the current newsgroup that can be expired.")
1023
1024 (defvar gnus-newsgroup-processable nil
1025   "List of articles in the current newsgroup that can be processed.")
1026
1027 (defvar gnus-newsgroup-downloadable nil
1028   "List of articles in the current newsgroup that can be processed.")
1029
1030 (defvar gnus-newsgroup-undownloaded nil
1031   "List of articles in the current newsgroup that haven't been downloaded..")
1032
1033 (defvar gnus-newsgroup-unsendable nil
1034   "List of articles in the current newsgroup that won't be sent.")
1035
1036 (defvar gnus-newsgroup-bookmarks nil
1037   "List of articles in the current newsgroup that have bookmarks.")
1038
1039 (defvar gnus-newsgroup-dormant nil
1040   "List of dormant articles in the current newsgroup.")
1041
1042 (defvar gnus-newsgroup-scored nil
1043   "List of scored articles in the current newsgroup.")
1044
1045 (defvar gnus-newsgroup-headers nil
1046   "List of article headers in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-threads nil)
1049
1050 (defvar gnus-newsgroup-prepared nil
1051   "Whether the current group has been prepared properly.")
1052
1053 (defvar gnus-newsgroup-ancient nil
1054   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-sparse nil)
1057
1058 (defvar gnus-current-article nil)
1059 (defvar gnus-article-current nil)
1060 (defvar gnus-current-headers nil)
1061 (defvar gnus-have-all-headers nil)
1062 (defvar gnus-last-article nil)
1063 (defvar gnus-newsgroup-history nil)
1064 (defvar gnus-newsgroup-charset nil)
1065 (defvar gnus-newsgroup-ephemeral-charset nil)
1066 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1067
1068 (defconst gnus-summary-local-variables
1069   '(gnus-newsgroup-name
1070     gnus-newsgroup-begin gnus-newsgroup-end
1071     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1072     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1073     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1074     gnus-newsgroup-unselected gnus-newsgroup-marked
1075     gnus-newsgroup-reads gnus-newsgroup-saved
1076     gnus-newsgroup-replied gnus-newsgroup-expirable
1077     gnus-newsgroup-processable gnus-newsgroup-killed
1078     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1079     gnus-newsgroup-unsendable
1080     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1081     gnus-newsgroup-headers gnus-newsgroup-threads
1082     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1083     gnus-current-article gnus-current-headers gnus-have-all-headers
1084     gnus-last-article gnus-article-internal-prepare-hook
1085     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1086     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1087     gnus-thread-expunge-below
1088     gnus-score-alist gnus-current-score-file
1089     (gnus-summary-expunge-below . global)
1090     (gnus-summary-mark-below . global)
1091     gnus-newsgroup-active gnus-scores-exclude-files
1092     gnus-newsgroup-history gnus-newsgroup-ancient
1093     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1094     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1095     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1096     (gnus-newsgroup-expunged-tally . 0)
1097     gnus-cache-removable-articles gnus-newsgroup-cached
1098     gnus-newsgroup-data gnus-newsgroup-data-reverse
1099     gnus-newsgroup-limit gnus-newsgroup-limits
1100     gnus-newsgroup-charset)
1101   "Variables that are buffer-local to the summary buffers.")
1102
1103 ;; Byte-compiler warning.
1104 (defvar gnus-article-mode-map)
1105
1106 ;; MIME stuff.
1107
1108 (defvar gnus-decode-encoded-word-methods
1109   '(mail-decode-encoded-word-string)
1110   "List of methods used to decode encoded words.
1111
1112 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1113 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1114 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1115 whose names match REGEXP.
1116
1117 For example:
1118 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1119  mail-decode-encoded-word-string
1120  (\"chinese\" . rfc1843-decode-string))
1121 ")
1122
1123 (defvar gnus-decode-encoded-word-methods-cache nil)
1124
1125 (defun gnus-multi-decode-encoded-word-string (string)
1126   "Apply the functions from `gnus-encoded-word-methods' that match."
1127   (unless (and gnus-decode-encoded-word-methods-cache
1128                (eq gnus-newsgroup-name
1129                    (car gnus-decode-encoded-word-methods-cache)))
1130     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1131     (mapcar (lambda (x)
1132               (if (symbolp x)
1133                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1134                 (if (and gnus-newsgroup-name
1135                          (string-match (car x) gnus-newsgroup-name))
1136                     (nconc gnus-decode-encoded-word-methods-cache
1137                            (list (cdr x))))))
1138           gnus-decode-encoded-word-methods))
1139   (let ((xlist gnus-decode-encoded-word-methods-cache))
1140     (pop xlist)
1141     (while xlist
1142       (setq string (funcall (pop xlist) string))))
1143   string)
1144
1145 ;; Subject simplification.
1146
1147 (defun gnus-simplify-whitespace (str)
1148   "Remove excessive whitespace."
1149   (let ((mystr str))
1150     ;; Multiple spaces.
1151     (while (string-match "[ \t][ \t]+" mystr)
1152       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1153                           " "
1154                           (substring mystr (match-end 0)))))
1155     ;; Leading spaces.
1156     (when (string-match "^[ \t]+" mystr)
1157       (setq mystr (substring mystr (match-end 0))))
1158     ;; Trailing spaces.
1159     (when (string-match "[ \t]+$" mystr)
1160       (setq mystr (substring mystr 0 (match-beginning 0))))
1161     mystr))
1162
1163 (defsubst gnus-simplify-subject-re (subject)
1164   "Remove \"Re:\" from subject lines."
1165   (if (string-match "^[Rr][Ee]: *" subject)
1166       (substring subject (match-end 0))
1167     subject))
1168
1169 (defun gnus-simplify-subject (subject &optional re-only)
1170   "Remove `Re:' and words in parentheses.
1171 If RE-ONLY is non-nil, strip leading `Re:'s only."
1172   (let ((case-fold-search t))           ;Ignore case.
1173     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1174     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1175       (setq subject (substring subject (match-end 0))))
1176     ;; Remove uninteresting prefixes.
1177     (when (and (not re-only)
1178                gnus-simplify-ignored-prefixes
1179                (string-match gnus-simplify-ignored-prefixes subject))
1180       (setq subject (substring subject (match-end 0))))
1181     ;; Remove words in parentheses from end.
1182     (unless re-only
1183       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1184         (setq subject (substring subject 0 (match-beginning 0)))))
1185     ;; Return subject string.
1186     subject))
1187
1188 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1189 ;; all whitespace.
1190 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1191   (goto-char (point-min))
1192   (while (re-search-forward regexp nil t)
1193     (replace-match (or newtext ""))))
1194
1195 (defun gnus-simplify-buffer-fuzzy ()
1196   "Simplify string in the buffer fuzzily.
1197 The string in the accessible portion of the current buffer is simplified.
1198 It is assumed to be a single-line subject.
1199 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1200 matter is removed.  Additional things can be deleted by setting
1201 gnus-simplify-subject-fuzzy-regexp."
1202   (let ((case-fold-search t)
1203         (modified-tick))
1204     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1205
1206     (while (not (eq modified-tick (buffer-modified-tick)))
1207       (setq modified-tick (buffer-modified-tick))
1208       (cond
1209        ((listp gnus-simplify-subject-fuzzy-regexp)
1210         (mapcar 'gnus-simplify-buffer-fuzzy-step
1211                 gnus-simplify-subject-fuzzy-regexp))
1212        (gnus-simplify-subject-fuzzy-regexp
1213         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1214       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1215       (gnus-simplify-buffer-fuzzy-step
1216        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1217       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1218
1219     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1220     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1221     (gnus-simplify-buffer-fuzzy-step " $")
1222     (gnus-simplify-buffer-fuzzy-step "^ +")))
1223
1224 (defun gnus-simplify-subject-fuzzy (subject)
1225   "Simplify a subject string fuzzily.
1226 See `gnus-simplify-buffer-fuzzy' for details."
1227   (save-excursion
1228     (gnus-set-work-buffer)
1229     (let ((case-fold-search t))
1230       ;; Remove uninteresting prefixes.
1231       (when (and gnus-simplify-ignored-prefixes
1232                  (string-match gnus-simplify-ignored-prefixes subject))
1233         (setq subject (substring subject (match-end 0))))
1234       (insert subject)
1235       (inline (gnus-simplify-buffer-fuzzy))
1236       (buffer-string))))
1237
1238 (defsubst gnus-simplify-subject-fully (subject)
1239   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1240   (cond
1241    (gnus-simplify-subject-functions
1242     (gnus-map-function gnus-simplify-subject-functions subject))
1243    ((null gnus-summary-gather-subject-limit)
1244     (gnus-simplify-subject-re subject))
1245    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1246     (gnus-simplify-subject-fuzzy subject))
1247    ((numberp gnus-summary-gather-subject-limit)
1248     (gnus-limit-string (gnus-simplify-subject-re subject)
1249                        gnus-summary-gather-subject-limit))
1250    (t
1251     subject)))
1252
1253 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1254   "Check whether two subjects are equal.
1255 If optional argument simple-first is t, first argument is already
1256 simplified."
1257   (cond
1258    ((null simple-first)
1259     (equal (gnus-simplify-subject-fully s1)
1260            (gnus-simplify-subject-fully s2)))
1261    (t
1262     (equal s1
1263            (gnus-simplify-subject-fully s2)))))
1264
1265 (defun gnus-summary-bubble-group ()
1266   "Increase the score of the current group.
1267 This is a handy function to add to `gnus-summary-exit-hook' to
1268 increase the score of each group you read."
1269   (gnus-group-add-score gnus-newsgroup-name))
1270
1271 \f
1272 ;;;
1273 ;;; Gnus summary mode
1274 ;;;
1275
1276 (put 'gnus-summary-mode 'mode-class 'special)
1277
1278 (when t
1279   ;; Non-orthogonal keys
1280
1281   (gnus-define-keys gnus-summary-mode-map
1282     " " gnus-summary-next-page
1283     "\177" gnus-summary-prev-page
1284     [delete] gnus-summary-prev-page
1285     [backspace] gnus-summary-prev-page
1286     "\r" gnus-summary-scroll-up
1287     "\M-\r" gnus-summary-scroll-down
1288     "n" gnus-summary-next-unread-article
1289     "p" gnus-summary-prev-unread-article
1290     "N" gnus-summary-next-article
1291     "P" gnus-summary-prev-article
1292     "\M-\C-n" gnus-summary-next-same-subject
1293     "\M-\C-p" gnus-summary-prev-same-subject
1294     "\M-n" gnus-summary-next-unread-subject
1295     "\M-p" gnus-summary-prev-unread-subject
1296     "." gnus-summary-first-unread-article
1297     "," gnus-summary-best-unread-article
1298     "\M-s" gnus-summary-search-article-forward
1299     "\M-r" gnus-summary-search-article-backward
1300     "<" gnus-summary-beginning-of-article
1301     ">" gnus-summary-end-of-article
1302     "j" gnus-summary-goto-article
1303     "^" gnus-summary-refer-parent-article
1304     "\M-^" gnus-summary-refer-article
1305     "u" gnus-summary-tick-article-forward
1306     "!" gnus-summary-tick-article-forward
1307     "U" gnus-summary-tick-article-backward
1308     "d" gnus-summary-mark-as-read-forward
1309     "D" gnus-summary-mark-as-read-backward
1310     "E" gnus-summary-mark-as-expirable
1311     "\M-u" gnus-summary-clear-mark-forward
1312     "\M-U" gnus-summary-clear-mark-backward
1313     "k" gnus-summary-kill-same-subject-and-select
1314     "\C-k" gnus-summary-kill-same-subject
1315     "\M-\C-k" gnus-summary-kill-thread
1316     "\M-\C-l" gnus-summary-lower-thread
1317     "e" gnus-summary-edit-article
1318     "#" gnus-summary-mark-as-processable
1319     "\M-#" gnus-summary-unmark-as-processable
1320     "\M-\C-t" gnus-summary-toggle-threads
1321     "\M-\C-s" gnus-summary-show-thread
1322     "\M-\C-h" gnus-summary-hide-thread
1323     "\M-\C-f" gnus-summary-next-thread
1324     "\M-\C-b" gnus-summary-prev-thread
1325     "\M-\C-u" gnus-summary-up-thread
1326     "\M-\C-d" gnus-summary-down-thread
1327     "&" gnus-summary-execute-command
1328     "c" gnus-summary-catchup-and-exit
1329     "\C-w" gnus-summary-mark-region-as-read
1330     "\C-t" gnus-summary-toggle-truncation
1331     "?" gnus-summary-mark-as-dormant
1332     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1333     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1334     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1335     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1336     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1337     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1338     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1339     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1340     "=" gnus-summary-expand-window
1341     "\C-x\C-s" gnus-summary-reselect-current-group
1342     "\M-g" gnus-summary-rescan-group
1343     "w" gnus-summary-stop-page-breaking
1344     "\C-c\C-r" gnus-summary-caesar-message
1345     "f" gnus-summary-followup
1346     "F" gnus-summary-followup-with-original
1347     "C" gnus-summary-cancel-article
1348     "r" gnus-summary-reply
1349     "R" gnus-summary-reply-with-original
1350     "\C-c\C-f" gnus-summary-mail-forward
1351     "o" gnus-summary-save-article
1352     "\C-o" gnus-summary-save-article-mail
1353     "|" gnus-summary-pipe-output
1354     "\M-k" gnus-summary-edit-local-kill
1355     "\M-K" gnus-summary-edit-global-kill
1356     ;; "V" gnus-version
1357     "\C-c\C-d" gnus-summary-describe-group
1358     "q" gnus-summary-exit
1359     "Q" gnus-summary-exit-no-update
1360     "\C-c\C-i" gnus-info-find-node
1361     gnus-mouse-2 gnus-mouse-pick-article
1362     "m" gnus-summary-mail-other-window
1363     "a" gnus-summary-post-news
1364     "x" gnus-summary-limit-to-unread
1365     "s" gnus-summary-isearch-article
1366     "t" gnus-summary-toggle-header
1367     "g" gnus-summary-show-article
1368     "l" gnus-summary-goto-last-article
1369     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1370     "\C-d" gnus-summary-enter-digest-group
1371     "\M-\C-d" gnus-summary-read-document
1372     "\M-\C-e" gnus-summary-edit-parameters
1373     "\M-\C-a" gnus-summary-customize-parameters
1374     "\C-c\C-b" gnus-bug
1375     "*" gnus-cache-enter-article
1376     "\M-*" gnus-cache-remove-article
1377     "\M-&" gnus-summary-universal-argument
1378     "\C-l" gnus-recenter
1379     "I" gnus-summary-increase-score
1380     "L" gnus-summary-lower-score
1381     "\M-i" gnus-symbolic-argument
1382     "h" gnus-summary-select-article-buffer
1383
1384     "b" gnus-article-view-part
1385     "\M-t" gnus-summary-toggle-display-buttonized
1386
1387     "V" gnus-summary-score-map
1388     "X" gnus-uu-extract-map
1389     "S" gnus-summary-send-map)
1390
1391   ;; Sort of orthogonal keymap
1392   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1393     "t" gnus-summary-tick-article-forward
1394     "!" gnus-summary-tick-article-forward
1395     "d" gnus-summary-mark-as-read-forward
1396     "r" gnus-summary-mark-as-read-forward
1397     "c" gnus-summary-clear-mark-forward
1398     " " gnus-summary-clear-mark-forward
1399     "e" gnus-summary-mark-as-expirable
1400     "x" gnus-summary-mark-as-expirable
1401     "?" gnus-summary-mark-as-dormant
1402     "b" gnus-summary-set-bookmark
1403     "B" gnus-summary-remove-bookmark
1404     "#" gnus-summary-mark-as-processable
1405     "\M-#" gnus-summary-unmark-as-processable
1406     "S" gnus-summary-limit-include-expunged
1407     "C" gnus-summary-catchup
1408     "H" gnus-summary-catchup-to-here
1409     "\C-c" gnus-summary-catchup-all
1410     "k" gnus-summary-kill-same-subject-and-select
1411     "K" gnus-summary-kill-same-subject
1412     "P" gnus-uu-mark-map)
1413
1414   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1415     "c" gnus-summary-clear-above
1416     "u" gnus-summary-tick-above
1417     "m" gnus-summary-mark-above
1418     "k" gnus-summary-kill-below)
1419
1420   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1421     "/" gnus-summary-limit-to-subject
1422     "n" gnus-summary-limit-to-articles
1423     "w" gnus-summary-pop-limit
1424     "s" gnus-summary-limit-to-subject
1425     "a" gnus-summary-limit-to-author
1426     "u" gnus-summary-limit-to-unread
1427     "m" gnus-summary-limit-to-marks
1428     "M" gnus-summary-limit-exclude-marks
1429     "v" gnus-summary-limit-to-score
1430     "*" gnus-summary-limit-include-cached
1431     "D" gnus-summary-limit-include-dormant
1432     "T" gnus-summary-limit-include-thread
1433     "d" gnus-summary-limit-exclude-dormant
1434     "t" gnus-summary-limit-to-age
1435     "x" gnus-summary-limit-to-extra 
1436     "E" gnus-summary-limit-include-expunged
1437     "c" gnus-summary-limit-exclude-childless-dormant
1438     "C" gnus-summary-limit-mark-excluded-as-read)
1439
1440   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1441     "n" gnus-summary-next-unread-article
1442     "p" gnus-summary-prev-unread-article
1443     "N" gnus-summary-next-article
1444     "P" gnus-summary-prev-article
1445     "\C-n" gnus-summary-next-same-subject
1446     "\C-p" gnus-summary-prev-same-subject
1447     "\M-n" gnus-summary-next-unread-subject
1448     "\M-p" gnus-summary-prev-unread-subject
1449     "f" gnus-summary-first-unread-article
1450     "b" gnus-summary-best-unread-article
1451     "j" gnus-summary-goto-article
1452     "g" gnus-summary-goto-subject
1453     "l" gnus-summary-goto-last-article
1454     "o" gnus-summary-pop-article)
1455
1456   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1457     "k" gnus-summary-kill-thread
1458     "l" gnus-summary-lower-thread
1459     "i" gnus-summary-raise-thread
1460     "T" gnus-summary-toggle-threads
1461     "t" gnus-summary-rethread-current
1462     "^" gnus-summary-reparent-thread
1463     "s" gnus-summary-show-thread
1464     "S" gnus-summary-show-all-threads
1465     "h" gnus-summary-hide-thread
1466     "H" gnus-summary-hide-all-threads
1467     "n" gnus-summary-next-thread
1468     "p" gnus-summary-prev-thread
1469     "u" gnus-summary-up-thread
1470     "o" gnus-summary-top-thread
1471     "d" gnus-summary-down-thread
1472     "#" gnus-uu-mark-thread
1473     "\M-#" gnus-uu-unmark-thread)
1474
1475   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1476     "g" gnus-summary-prepare
1477     "c" gnus-summary-insert-cached-articles)
1478
1479   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1480     "c" gnus-summary-catchup-and-exit
1481     "C" gnus-summary-catchup-all-and-exit
1482     "E" gnus-summary-exit-no-update
1483     "Q" gnus-summary-exit
1484     "Z" gnus-summary-exit
1485     "n" gnus-summary-catchup-and-goto-next-group
1486     "R" gnus-summary-reselect-current-group
1487     "G" gnus-summary-rescan-group
1488     "N" gnus-summary-next-group
1489     "s" gnus-summary-save-newsrc
1490     "P" gnus-summary-prev-group)
1491
1492   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1493     " " gnus-summary-next-page
1494     "n" gnus-summary-next-page
1495     "\177" gnus-summary-prev-page
1496     [delete] gnus-summary-prev-page
1497     "p" gnus-summary-prev-page
1498     "\r" gnus-summary-scroll-up
1499     "\M-\r" gnus-summary-scroll-down
1500     "<" gnus-summary-beginning-of-article
1501     ">" gnus-summary-end-of-article
1502     "b" gnus-summary-beginning-of-article
1503     "e" gnus-summary-end-of-article
1504     "^" gnus-summary-refer-parent-article
1505     "r" gnus-summary-refer-parent-article
1506     "D" gnus-summary-enter-digest-group
1507     "R" gnus-summary-refer-references
1508     "T" gnus-summary-refer-thread
1509     "g" gnus-summary-show-article
1510     "s" gnus-summary-isearch-article
1511     "P" gnus-summary-print-article
1512     "t" gnus-article-babel)
1513
1514   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1515     "b" gnus-article-add-buttons
1516     "B" gnus-article-add-buttons-to-head
1517     "o" gnus-article-treat-overstrike
1518     "e" gnus-article-emphasize
1519     "w" gnus-article-fill-cited-article
1520     "Q" gnus-article-fill-long-lines
1521     "C" gnus-article-capitalize-sentences
1522     "c" gnus-article-remove-cr
1523     "q" gnus-article-de-quoted-unreadable
1524     "f" gnus-article-display-x-face
1525     "l" gnus-summary-stop-page-breaking
1526     "r" gnus-summary-caesar-message
1527     "t" gnus-article-hide-headers
1528     "v" gnus-summary-verbose-headers
1529     "H" gnus-article-strip-headers-in-body
1530     "d" gnus-article-treat-dumbquotes)
1531
1532   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1533     "a" gnus-article-hide
1534     "h" gnus-article-hide-headers
1535     "b" gnus-article-hide-boring-headers
1536     "s" gnus-article-hide-signature
1537     "c" gnus-article-hide-citation
1538     "C" gnus-article-hide-citation-in-followups
1539     "l" gnus-article-hide-list-identifiers
1540     "p" gnus-article-hide-pgp
1541     "B" gnus-article-strip-banner
1542     "P" gnus-article-hide-pem
1543     "\C-c" gnus-article-hide-citation-maybe)
1544
1545   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1546     "a" gnus-article-highlight
1547     "h" gnus-article-highlight-headers
1548     "c" gnus-article-highlight-citation
1549     "s" gnus-article-highlight-signature)
1550
1551   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1552     "w" gnus-article-decode-mime-words
1553     "c" gnus-article-decode-charset
1554     "v" gnus-mime-view-all-parts
1555     "b" gnus-article-view-part)
1556
1557   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1558     "z" gnus-article-date-ut
1559     "u" gnus-article-date-ut
1560     "l" gnus-article-date-local
1561     "e" gnus-article-date-lapsed
1562     "o" gnus-article-date-original
1563     "i" gnus-article-date-iso8601
1564     "s" gnus-article-date-user)
1565
1566   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1567     "t" gnus-article-remove-trailing-blank-lines
1568     "l" gnus-article-strip-leading-blank-lines
1569     "m" gnus-article-strip-multiple-blank-lines
1570     "a" gnus-article-strip-blank-lines
1571     "A" gnus-article-strip-all-blank-lines
1572     "s" gnus-article-strip-leading-space
1573     "e" gnus-article-strip-trailing-space)
1574
1575   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1576     "v" gnus-version
1577     "f" gnus-summary-fetch-faq
1578     "d" gnus-summary-describe-group
1579     "h" gnus-summary-describe-briefly
1580     "i" gnus-info-find-node)
1581
1582   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1583     "e" gnus-summary-expire-articles
1584     "\M-\C-e" gnus-summary-expire-articles-now
1585     "\177" gnus-summary-delete-article
1586     [delete] gnus-summary-delete-article
1587     [backspace] gnus-summary-delete-article
1588     "m" gnus-summary-move-article
1589     "r" gnus-summary-respool-article
1590     "w" gnus-summary-edit-article
1591     "c" gnus-summary-copy-article
1592     "B" gnus-summary-crosspost-article
1593     "q" gnus-summary-respool-query
1594     "t" gnus-summary-respool-trace
1595     "i" gnus-summary-import-article
1596     "p" gnus-summary-article-posted-p)
1597
1598   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1599     "o" gnus-summary-save-article
1600     "m" gnus-summary-save-article-mail
1601     "F" gnus-summary-write-article-file
1602     "r" gnus-summary-save-article-rmail
1603     "f" gnus-summary-save-article-file
1604     "b" gnus-summary-save-article-body-file
1605     "h" gnus-summary-save-article-folder
1606     "v" gnus-summary-save-article-vm
1607     "p" gnus-summary-pipe-output
1608     "s" gnus-soup-add-article)
1609
1610   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1611     "b" gnus-summary-display-buttonized
1612     "m" gnus-summary-repair-multipart
1613     "v" gnus-article-view-part
1614     "o" gnus-article-save-part
1615     "c" gnus-article-copy-part
1616     "e" gnus-article-externalize-part
1617     "i" gnus-article-inline-part
1618     "|" gnus-article-pipe-part))
1619
1620 (defun gnus-summary-make-menu-bar ()
1621   (gnus-turn-off-edit-menu 'summary)
1622
1623   (unless (boundp 'gnus-summary-misc-menu)
1624
1625     (easy-menu-define
1626      gnus-summary-kill-menu gnus-summary-mode-map ""
1627      (cons
1628       "Score"
1629       (nconc
1630        (list
1631         ["Enter score..." gnus-summary-score-entry t]
1632         ["Customize" gnus-score-customize t])
1633        (gnus-make-score-map 'increase)
1634        (gnus-make-score-map 'lower)
1635        '(("Mark"
1636           ["Kill below" gnus-summary-kill-below t]
1637           ["Mark above" gnus-summary-mark-above t]
1638           ["Tick above" gnus-summary-tick-above t]
1639           ["Clear above" gnus-summary-clear-above t])
1640          ["Current score" gnus-summary-current-score t]
1641          ["Set score" gnus-summary-set-score t]
1642          ["Switch current score file..." gnus-score-change-score-file t]
1643          ["Set mark below..." gnus-score-set-mark-below t]
1644          ["Set expunge below..." gnus-score-set-expunge-below t]
1645          ["Edit current score file" gnus-score-edit-current-scores t]
1646          ["Edit score file" gnus-score-edit-file t]
1647          ["Trace score" gnus-score-find-trace t]
1648          ["Find words" gnus-score-find-favourite-words t]
1649          ["Rescore buffer" gnus-summary-rescore t]
1650          ["Increase score..." gnus-summary-increase-score t]
1651          ["Lower score..." gnus-summary-lower-score t]))))
1652
1653     ;; Define both the Article menu in the summary buffer and the equivalent
1654     ;; Commands menu in the article buffer here for consistency.
1655     (let ((innards
1656            '(("Hide"
1657               ["All" gnus-article-hide t]
1658               ["Headers" gnus-article-hide-headers t]
1659               ["Signature" gnus-article-hide-signature t]
1660               ["Citation" gnus-article-hide-citation t]
1661               ["List identifiers" gnus-article-hide-list-identifiers t]
1662               ["PGP" gnus-article-hide-pgp t]
1663               ["Banner" gnus-article-strip-banner t]
1664               ["Boring headers" gnus-article-hide-boring-headers t])
1665              ("Highlight"
1666               ["All" gnus-article-highlight t]
1667               ["Headers" gnus-article-highlight-headers t]
1668               ["Signature" gnus-article-highlight-signature t]
1669               ["Citation" gnus-article-highlight-citation t])
1670              ("MIME"
1671               ["Words" gnus-article-decode-mime-words t]
1672               ["Charset" gnus-article-decode-charset t]
1673               ["QP" gnus-article-de-quoted-unreadable t]
1674               ["View all" gnus-mime-view-all-parts t])
1675              ("Date"
1676               ["Local" gnus-article-date-local t]
1677               ["ISO8601" gnus-article-date-iso8601 t]
1678               ["UT" gnus-article-date-ut t]
1679               ["Original" gnus-article-date-original t]
1680               ["Lapsed" gnus-article-date-lapsed t]
1681               ["User-defined" gnus-article-date-user t])
1682              ("Washing"
1683               ("Remove Blanks"
1684                ["Leading" gnus-article-strip-leading-blank-lines t]
1685                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1686                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1687                ["All of the above" gnus-article-strip-blank-lines t]
1688                ["All" gnus-article-strip-all-blank-lines t]
1689                ["Leading space" gnus-article-strip-leading-space t]
1690                ["Trailing space" gnus-article-strip-trailing-space t])
1691               ["Overstrike" gnus-article-treat-overstrike t]
1692               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1693               ["Emphasis" gnus-article-emphasize t]
1694               ["Word wrap" gnus-article-fill-cited-article t]
1695               ["Fill long lines" gnus-article-fill-long-lines t]
1696               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1697               ["CR" gnus-article-remove-cr t]
1698               ["Show X-Face" gnus-article-display-x-face t]
1699               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1700               ["Rot 13" gnus-summary-caesar-message t]
1701               ["Unix pipe" gnus-summary-pipe-message t]
1702               ["Add buttons" gnus-article-add-buttons t]
1703               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1704               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1705               ["Verbose header" gnus-summary-verbose-headers t]
1706               ["Toggle header" gnus-summary-toggle-header t]
1707               ["HZ" gnus-article-decode-HZ t])
1708              ("Output"
1709               ["Save in default format" gnus-summary-save-article t]
1710               ["Save in file" gnus-summary-save-article-file t]
1711               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1712               ["Save in MH folder" gnus-summary-save-article-folder t]
1713               ["Save in VM folder" gnus-summary-save-article-vm t]
1714               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1715               ["Save body in file" gnus-summary-save-article-body-file t]
1716               ["Pipe through a filter" gnus-summary-pipe-output t]
1717               ["Add to SOUP packet" gnus-soup-add-article t]
1718               ["Print" gnus-summary-print-article t])
1719              ("Backend"
1720               ["Respool article..." gnus-summary-respool-article t]
1721               ["Move article..." gnus-summary-move-article
1722                (gnus-check-backend-function
1723                 'request-move-article gnus-newsgroup-name)]
1724               ["Copy article..." gnus-summary-copy-article t]
1725               ["Crosspost article..." gnus-summary-crosspost-article
1726                (gnus-check-backend-function
1727                 'request-replace-article gnus-newsgroup-name)]
1728               ["Import file..." gnus-summary-import-article t]
1729               ["Check if posted" gnus-summary-article-posted-p t]
1730               ["Edit article" gnus-summary-edit-article
1731                (not (gnus-group-read-only-p))]
1732               ["Delete article" gnus-summary-delete-article
1733                (gnus-check-backend-function
1734                 'request-expire-articles gnus-newsgroup-name)]
1735               ["Query respool" gnus-summary-respool-query t]
1736               ["Trace respool" gnus-summary-respool-trace t]
1737               ["Delete expirable articles" gnus-summary-expire-articles-now
1738                (gnus-check-backend-function
1739                 'request-expire-articles gnus-newsgroup-name)])
1740              ("Extract"
1741               ["Uudecode" gnus-uu-decode-uu t]
1742               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1743               ["Unshar" gnus-uu-decode-unshar t]
1744               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1745               ["Save" gnus-uu-decode-save t]
1746               ["Binhex" gnus-uu-decode-binhex t]
1747               ["Postscript" gnus-uu-decode-postscript t])
1748              ("Cache"
1749               ["Enter article" gnus-cache-enter-article t]
1750               ["Remove article" gnus-cache-remove-article t])
1751              ["Translate" gnus-article-babel t]
1752              ["Select article buffer" gnus-summary-select-article-buffer t]
1753              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1754              ["Isearch article..." gnus-summary-isearch-article t]
1755              ["Beginning of the article" gnus-summary-beginning-of-article t]
1756              ["End of the article" gnus-summary-end-of-article t]
1757              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1758              ["Fetch referenced articles" gnus-summary-refer-references t]
1759              ["Fetch current thread" gnus-summary-refer-thread t]
1760              ["Fetch article with id..." gnus-summary-refer-article t]
1761              ["Redisplay" gnus-summary-show-article t])))
1762       (easy-menu-define
1763        gnus-summary-article-menu gnus-summary-mode-map ""
1764        (cons "Article" innards))
1765
1766       (easy-menu-define
1767        gnus-article-commands-menu gnus-article-mode-map ""
1768        (cons "Commands" innards)))
1769
1770     (easy-menu-define
1771      gnus-summary-thread-menu gnus-summary-mode-map ""
1772      '("Threads"
1773        ["Toggle threading" gnus-summary-toggle-threads t]
1774        ["Hide threads" gnus-summary-hide-all-threads t]
1775        ["Show threads" gnus-summary-show-all-threads t]
1776        ["Hide thread" gnus-summary-hide-thread t]
1777        ["Show thread" gnus-summary-show-thread t]
1778        ["Go to next thread" gnus-summary-next-thread t]
1779        ["Go to previous thread" gnus-summary-prev-thread t]
1780        ["Go down thread" gnus-summary-down-thread t]
1781        ["Go up thread" gnus-summary-up-thread t]
1782        ["Top of thread" gnus-summary-top-thread t]
1783        ["Mark thread as read" gnus-summary-kill-thread t]
1784        ["Lower thread score" gnus-summary-lower-thread t]
1785        ["Raise thread score" gnus-summary-raise-thread t]
1786        ["Rethread current" gnus-summary-rethread-current t]))
1787
1788     (easy-menu-define
1789      gnus-summary-post-menu gnus-summary-mode-map ""
1790      '("Post"
1791        ["Post an article" gnus-summary-post-news t]
1792        ["Followup" gnus-summary-followup t]
1793        ["Followup and yank" gnus-summary-followup-with-original t]
1794        ["Supersede article" gnus-summary-supersede-article t]
1795        ["Cancel article" gnus-summary-cancel-article t]
1796        ["Reply" gnus-summary-reply t]
1797        ["Reply and yank" gnus-summary-reply-with-original t]
1798        ["Wide reply" gnus-summary-wide-reply t]
1799        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1800        ["Mail forward" gnus-summary-mail-forward t]
1801        ["Post forward" gnus-summary-post-forward t]
1802        ["Digest and mail" gnus-uu-digest-mail-forward t]
1803        ["Digest and post" gnus-uu-digest-post-forward t]
1804        ["Resend message" gnus-summary-resend-message t]
1805        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1806        ["Send a mail" gnus-summary-mail-other-window t]
1807        ["Uuencode and post" gnus-uu-post-news t]
1808        ["Followup via news" gnus-summary-followup-to-mail t]
1809        ["Followup via news and yank"
1810         gnus-summary-followup-to-mail-with-original t]
1811        ;;("Draft"
1812        ;;["Send" gnus-summary-send-draft t]
1813        ;;["Send bounced" gnus-resend-bounced-mail t])
1814        ))
1815
1816     (easy-menu-define
1817      gnus-summary-misc-menu gnus-summary-mode-map ""
1818      '("Misc"
1819        ("Mark Read"
1820         ["Mark as read" gnus-summary-mark-as-read-forward t]
1821         ["Mark same subject and select"
1822          gnus-summary-kill-same-subject-and-select t]
1823         ["Mark same subject" gnus-summary-kill-same-subject t]
1824         ["Catchup" gnus-summary-catchup t]
1825         ["Catchup all" gnus-summary-catchup-all t]
1826         ["Catchup to here" gnus-summary-catchup-to-here t]
1827         ["Catchup region" gnus-summary-mark-region-as-read t]
1828         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1829        ("Mark Various"
1830         ["Tick" gnus-summary-tick-article-forward t]
1831         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1832         ["Remove marks" gnus-summary-clear-mark-forward t]
1833         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1834         ["Set bookmark" gnus-summary-set-bookmark t]
1835         ["Remove bookmark" gnus-summary-remove-bookmark t])
1836        ("Mark Limit"
1837         ["Marks..." gnus-summary-limit-to-marks t]
1838         ["Subject..." gnus-summary-limit-to-subject t]
1839         ["Author..." gnus-summary-limit-to-author t]
1840         ["Age..." gnus-summary-limit-to-age t]
1841         ["Extra..." gnus-summary-limit-to-extra t]
1842         ["Score" gnus-summary-limit-to-score t]
1843         ["Unread" gnus-summary-limit-to-unread t]
1844         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1845         ["Articles" gnus-summary-limit-to-articles t]
1846         ["Pop limit" gnus-summary-pop-limit t]
1847         ["Show dormant" gnus-summary-limit-include-dormant t]
1848         ["Hide childless dormant"
1849          gnus-summary-limit-exclude-childless-dormant t]
1850         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1851         ["Hide marked" gnus-summary-limit-exclude-marks t]
1852         ["Show expunged" gnus-summary-show-all-expunged t])
1853        ("Process Mark"
1854         ["Set mark" gnus-summary-mark-as-processable t]
1855         ["Remove mark" gnus-summary-unmark-as-processable t]
1856         ["Remove all marks" gnus-summary-unmark-all-processable t]
1857         ["Mark above" gnus-uu-mark-over t]
1858         ["Mark series" gnus-uu-mark-series t]
1859         ["Mark region" gnus-uu-mark-region t]
1860         ["Unmark region" gnus-uu-unmark-region t]
1861         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1862         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1863         ["Mark all" gnus-uu-mark-all t]
1864         ["Mark buffer" gnus-uu-mark-buffer t]
1865         ["Mark sparse" gnus-uu-mark-sparse t]
1866         ["Mark thread" gnus-uu-mark-thread t]
1867         ["Unmark thread" gnus-uu-unmark-thread t]
1868         ("Process Mark Sets"
1869          ["Kill" gnus-summary-kill-process-mark t]
1870          ["Yank" gnus-summary-yank-process-mark
1871           gnus-newsgroup-process-stack]
1872          ["Save" gnus-summary-save-process-mark t]))
1873        ("Scroll article"
1874         ["Page forward" gnus-summary-next-page t]
1875         ["Page backward" gnus-summary-prev-page t]
1876         ["Line forward" gnus-summary-scroll-up t])
1877        ("Move"
1878         ["Next unread article" gnus-summary-next-unread-article t]
1879         ["Previous unread article" gnus-summary-prev-unread-article t]
1880         ["Next article" gnus-summary-next-article t]
1881         ["Previous article" gnus-summary-prev-article t]
1882         ["Next unread subject" gnus-summary-next-unread-subject t]
1883         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1884         ["Next article same subject" gnus-summary-next-same-subject t]
1885         ["Previous article same subject" gnus-summary-prev-same-subject t]
1886         ["First unread article" gnus-summary-first-unread-article t]
1887         ["Best unread article" gnus-summary-best-unread-article t]
1888         ["Go to subject number..." gnus-summary-goto-subject t]
1889         ["Go to article number..." gnus-summary-goto-article t]
1890         ["Go to the last article" gnus-summary-goto-last-article t]
1891         ["Pop article off history" gnus-summary-pop-article t])
1892        ("Sort"
1893         ["Sort by number" gnus-summary-sort-by-number t]
1894         ["Sort by author" gnus-summary-sort-by-author t]
1895         ["Sort by subject" gnus-summary-sort-by-subject t]
1896         ["Sort by date" gnus-summary-sort-by-date t]
1897         ["Sort by score" gnus-summary-sort-by-score t]
1898         ["Sort by lines" gnus-summary-sort-by-lines t]
1899         ["Sort by characters" gnus-summary-sort-by-chars t])
1900        ("Help"
1901         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1902         ["Describe group" gnus-summary-describe-group t]
1903         ["Read manual" gnus-info-find-node t])
1904        ("Modes"
1905         ["Pick and read" gnus-pick-mode t]
1906         ["Binary" gnus-binary-mode t])
1907        ("Regeneration"
1908         ["Regenerate" gnus-summary-prepare t]
1909         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1910         ["Toggle threading" gnus-summary-toggle-threads t])
1911        ["Filter articles..." gnus-summary-execute-command t]
1912        ["Run command on subjects..." gnus-summary-universal-argument t]
1913        ["Search articles forward..." gnus-summary-search-article-forward t]
1914        ["Search articles backward..." gnus-summary-search-article-backward t]
1915        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1916        ["Expand window" gnus-summary-expand-window t]
1917        ["Expire expirable articles" gnus-summary-expire-articles
1918         (gnus-check-backend-function
1919          'request-expire-articles gnus-newsgroup-name)]
1920        ["Edit local kill file" gnus-summary-edit-local-kill t]
1921        ["Edit main kill file" gnus-summary-edit-global-kill t]
1922        ["Edit group parameters" gnus-summary-edit-parameters t]
1923        ["Customize group parameters" gnus-summary-customize-parameters t]
1924        ["Send a bug report" gnus-bug t]
1925        ("Exit"
1926         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1927         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1928         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1929         ["Exit group" gnus-summary-exit t]
1930         ["Exit group without updating" gnus-summary-exit-no-update t]
1931         ["Exit and goto next group" gnus-summary-next-group t]
1932         ["Exit and goto prev group" gnus-summary-prev-group t]
1933         ["Reselect group" gnus-summary-reselect-current-group t]
1934         ["Rescan group" gnus-summary-rescan-group t]
1935         ["Update dribble" gnus-summary-save-newsrc t])))
1936
1937     (gnus-run-hooks 'gnus-summary-menu-hook)))
1938
1939 (defun gnus-score-set-default (var value)
1940   "A version of set that updates the GNU Emacs menu-bar."
1941   (set var value)
1942   ;; It is the message that forces the active status to be updated.
1943   (message ""))
1944
1945 (defun gnus-make-score-map (type)
1946   "Make a summary score map of type TYPE."
1947   (if t
1948       nil
1949     (let ((headers '(("author" "from" string)
1950                      ("subject" "subject" string)
1951                      ("article body" "body" string)
1952                      ("article head" "head" string)
1953                      ("xref" "xref" string)
1954                      ("extra header" "extra" string)
1955                      ("lines" "lines" number)
1956                      ("followups to author" "followup" string)))
1957           (types '((number ("less than" <)
1958                            ("greater than" >)
1959                            ("equal" =))
1960                    (string ("substring" s)
1961                            ("exact string" e)
1962                            ("fuzzy string" f)
1963                            ("regexp" r))))
1964           (perms '(("temporary" (current-time-string))
1965                    ("permanent" nil)
1966                    ("immediate" now)))
1967           header)
1968       (list
1969        (apply
1970         'nconc
1971         (list
1972          (if (eq type 'lower)
1973              "Lower score"
1974            "Increase score"))
1975         (let (outh)
1976           (while headers
1977             (setq header (car headers))
1978             (setq outh
1979                   (cons
1980                    (apply
1981                     'nconc
1982                     (list (car header))
1983                     (let ((ts (cdr (assoc (nth 2 header) types)))
1984                           outt)
1985                       (while ts
1986                         (setq outt
1987                               (cons
1988                                (apply
1989                                 'nconc
1990                                 (list (caar ts))
1991                                 (let ((ps perms)
1992                                       outp)
1993                                   (while ps
1994                                     (setq outp
1995                                           (cons
1996                                            (vector
1997                                             (caar ps)
1998                                             (list
1999                                              'gnus-summary-score-entry
2000                                              (nth 1 header)
2001                                              (if (or (string= (nth 1 header)
2002                                                               "head")
2003                                                      (string= (nth 1 header)
2004                                                               "body"))
2005                                                  ""
2006                                                (list 'gnus-summary-header
2007                                                      (nth 1 header)))
2008                                              (list 'quote (nth 1 (car ts)))
2009                                              (list 'gnus-score-delta-default
2010                                                    nil)
2011                                              (nth 1 (car ps))
2012                                              t)
2013                                             t)
2014                                            outp))
2015                                     (setq ps (cdr ps)))
2016                                   (list (nreverse outp))))
2017                                outt))
2018                         (setq ts (cdr ts)))
2019                       (list (nreverse outt))))
2020                    outh))
2021             (setq headers (cdr headers)))
2022           (list (nreverse outh))))))))
2023
2024 \f
2025
2026 (defun gnus-summary-mode (&optional group)
2027   "Major mode for reading articles.
2028
2029 All normal editing commands are switched off.
2030 \\<gnus-summary-mode-map>
2031 Each line in this buffer represents one article.  To read an
2032 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2033 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2034 respectively.
2035
2036 You can also post articles and send mail from this buffer.  To
2037 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2038 of an article, type `\\[gnus-summary-reply]'.
2039
2040 There are approx. one gazillion commands you can execute in this
2041 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2042
2043 The following commands are available:
2044
2045 \\{gnus-summary-mode-map}"
2046   (interactive)
2047   (when (gnus-visual-p 'summary-menu 'menu)
2048     (gnus-summary-make-menu-bar))
2049   (kill-all-local-variables)
2050   (gnus-summary-make-local-variables)
2051   (gnus-make-thread-indent-array)
2052   (gnus-simplify-mode-line)
2053   (setq major-mode 'gnus-summary-mode)
2054   (setq mode-name "Summary")
2055   (make-local-variable 'minor-mode-alist)
2056   (use-local-map gnus-summary-mode-map)
2057   (buffer-disable-undo)
2058   (setq buffer-read-only t)             ;Disable modification
2059   (setq truncate-lines t)
2060   (setq selective-display t)
2061   (setq selective-display-ellipses t)   ;Display `...'
2062   (gnus-summary-set-display-table)
2063   (gnus-set-default-directory)
2064   (setq gnus-newsgroup-name group)
2065   (make-local-variable 'gnus-summary-line-format)
2066   (make-local-variable 'gnus-summary-line-format-spec)
2067   (make-local-variable 'gnus-summary-dummy-line-format)
2068   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2069   (make-local-variable 'gnus-summary-mark-positions)
2070   (make-local-hook 'pre-command-hook)
2071   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2072   (gnus-run-hooks 'gnus-summary-mode-hook)
2073   (mm-enable-multibyte)
2074   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2075   (gnus-update-summary-mark-positions))
2076
2077 (defun gnus-summary-make-local-variables ()
2078   "Make all the local summary buffer variables."
2079   (let (global)
2080     (dolist (local gnus-summary-local-variables)
2081       (if (consp local)
2082           (progn
2083             (if (eq (cdr local) 'global)
2084                 ;; Copy the global value of the variable.
2085                 (setq global (symbol-value (car local)))
2086               ;; Use the value from the list.
2087               (setq global (eval (cdr local))))
2088             (set (make-local-variable (car local)) global))
2089         ;; Simple nil-valued local variable.
2090         (set (make-local-variable local) nil)))))
2091
2092 (defun gnus-summary-clear-local-variables ()
2093   (let ((locals gnus-summary-local-variables))
2094     (while locals
2095       (if (consp (car locals))
2096           (and (vectorp (caar locals))
2097                (set (caar locals) nil))
2098         (and (vectorp (car locals))
2099              (set (car locals) nil)))
2100       (setq locals (cdr locals)))))
2101
2102 ;; Summary data functions.
2103
2104 (defmacro gnus-data-number (data)
2105   `(car ,data))
2106
2107 (defmacro gnus-data-set-number (data number)
2108   `(setcar ,data ,number))
2109
2110 (defmacro gnus-data-mark (data)
2111   `(nth 1 ,data))
2112
2113 (defmacro gnus-data-set-mark (data mark)
2114   `(setcar (nthcdr 1 ,data) ,mark))
2115
2116 (defmacro gnus-data-pos (data)
2117   `(nth 2 ,data))
2118
2119 (defmacro gnus-data-set-pos (data pos)
2120   `(setcar (nthcdr 2 ,data) ,pos))
2121
2122 (defmacro gnus-data-header (data)
2123   `(nth 3 ,data))
2124
2125 (defmacro gnus-data-set-header (data header)
2126   `(setf (nth 3 ,data) ,header))
2127
2128 (defmacro gnus-data-level (data)
2129   `(nth 4 ,data))
2130
2131 (defmacro gnus-data-unread-p (data)
2132   `(= (nth 1 ,data) gnus-unread-mark))
2133
2134 (defmacro gnus-data-read-p (data)
2135   `(/= (nth 1 ,data) gnus-unread-mark))
2136
2137 (defmacro gnus-data-pseudo-p (data)
2138   `(consp (nth 3 ,data)))
2139
2140 (defmacro gnus-data-find (number)
2141   `(assq ,number gnus-newsgroup-data))
2142
2143 (defmacro gnus-data-find-list (number &optional data)
2144   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2145      (memq (assq ,number bdata)
2146            bdata)))
2147
2148 (defmacro gnus-data-make (number mark pos header level)
2149   `(list ,number ,mark ,pos ,header ,level))
2150
2151 (defun gnus-data-enter (after-article number mark pos header level offset)
2152   (let ((data (gnus-data-find-list after-article)))
2153     (unless data
2154       (error "No such article: %d" after-article))
2155     (setcdr data (cons (gnus-data-make number mark pos header level)
2156                        (cdr data)))
2157     (setq gnus-newsgroup-data-reverse nil)
2158     (gnus-data-update-list (cddr data) offset)))
2159
2160 (defun gnus-data-enter-list (after-article list &optional offset)
2161   (when list
2162     (let ((data (and after-article (gnus-data-find-list after-article)))
2163           (ilist list))
2164       (if (not (or data
2165                    after-article))
2166           (let ((odata gnus-newsgroup-data))
2167             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2168             (when offset
2169               (gnus-data-update-list odata offset)))
2170         ;; Find the last element in the list to be spliced into the main
2171         ;; list.
2172         (while (cdr list)
2173           (setq list (cdr list)))
2174         (if (not data)
2175             (progn
2176               (setcdr list gnus-newsgroup-data)
2177               (setq gnus-newsgroup-data ilist)
2178               (when offset
2179                 (gnus-data-update-list (cdr list) offset)))
2180           (setcdr list (cdr data))
2181           (setcdr data ilist)
2182           (when offset
2183             (gnus-data-update-list (cdr list) offset))))
2184       (setq gnus-newsgroup-data-reverse nil))))
2185
2186 (defun gnus-data-remove (article &optional offset)
2187   (let ((data gnus-newsgroup-data))
2188     (if (= (gnus-data-number (car data)) article)
2189         (progn
2190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2191                 gnus-newsgroup-data-reverse nil)
2192           (when offset
2193             (gnus-data-update-list gnus-newsgroup-data offset)))
2194       (while (cdr data)
2195         (when (= (gnus-data-number (cadr data)) article)
2196           (setcdr data (cddr data))
2197           (when offset
2198             (gnus-data-update-list (cdr data) offset))
2199           (setq data nil
2200                 gnus-newsgroup-data-reverse nil))
2201         (setq data (cdr data))))))
2202
2203 (defmacro gnus-data-list (backward)
2204   `(if ,backward
2205        (or gnus-newsgroup-data-reverse
2206            (setq gnus-newsgroup-data-reverse
2207                  (reverse gnus-newsgroup-data)))
2208      gnus-newsgroup-data))
2209
2210 (defun gnus-data-update-list (data offset)
2211   "Add OFFSET to the POS of all data entries in DATA."
2212   (setq gnus-newsgroup-data-reverse nil)
2213   (while data
2214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2215     (setq data (cdr data))))
2216
2217 (defun gnus-summary-article-pseudo-p (article)
2218   "Say whether this article is a pseudo article or not."
2219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2220
2221 (defmacro gnus-summary-article-sparse-p (article)
2222   "Say whether this article is a sparse article or not."
2223   `(memq ,article gnus-newsgroup-sparse))
2224
2225 (defmacro gnus-summary-article-ancient-p (article)
2226   "Say whether this article is a sparse article or not."
2227   `(memq ,article gnus-newsgroup-ancient))
2228
2229 (defun gnus-article-parent-p (number)
2230   "Say whether this article is a parent or not."
2231   (let ((data (gnus-data-find-list number)))
2232     (and (cdr data)                     ; There has to be an article after...
2233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2234             (gnus-data-level (nth 1 data))))))
2235
2236 (defun gnus-article-children (number)
2237   "Return a list of all children to NUMBER."
2238   (let* ((data (gnus-data-find-list number))
2239          (level (gnus-data-level (car data)))
2240          children)
2241     (setq data (cdr data))
2242     (while (and data
2243                 (= (gnus-data-level (car data)) (1+ level)))
2244       (push (gnus-data-number (car data)) children)
2245       (setq data (cdr data)))
2246     children))
2247
2248 (defmacro gnus-summary-skip-intangible ()
2249   "If the current article is intangible, then jump to a different article."
2250   '(let ((to (get-text-property (point) 'gnus-intangible)))
2251      (and to (gnus-summary-goto-subject to))))
2252
2253 (defmacro gnus-summary-article-intangible-p ()
2254   "Say whether this article is intangible or not."
2255   '(get-text-property (point) 'gnus-intangible))
2256
2257 (defun gnus-article-read-p (article)
2258   "Say whether ARTICLE is read or not."
2259   (not (or (memq article gnus-newsgroup-marked)
2260            (memq article gnus-newsgroup-unreads)
2261            (memq article gnus-newsgroup-unselected)
2262            (memq article gnus-newsgroup-dormant))))
2263
2264 ;; Some summary mode macros.
2265
2266 (defmacro gnus-summary-article-number ()
2267   "The article number of the article on the current line.
2268 If there isn's an article number here, then we return the current
2269 article number."
2270   '(progn
2271      (gnus-summary-skip-intangible)
2272      (or (get-text-property (point) 'gnus-number)
2273          (gnus-summary-last-subject))))
2274
2275 (defmacro gnus-summary-article-header (&optional number)
2276   "Return the header of article NUMBER."
2277   `(gnus-data-header (gnus-data-find
2278                       ,(or number '(gnus-summary-article-number)))))
2279
2280 (defmacro gnus-summary-thread-level (&optional number)
2281   "Return the level of thread that starts with article NUMBER."
2282   `(if (and (eq gnus-summary-make-false-root 'dummy)
2283             (get-text-property (point) 'gnus-intangible))
2284        0
2285      (gnus-data-level (gnus-data-find
2286                        ,(or number '(gnus-summary-article-number))))))
2287
2288 (defmacro gnus-summary-article-mark (&optional number)
2289   "Return the mark of article NUMBER."
2290   `(gnus-data-mark (gnus-data-find
2291                     ,(or number '(gnus-summary-article-number)))))
2292
2293 (defmacro gnus-summary-article-pos (&optional number)
2294   "Return the position of the line of article NUMBER."
2295   `(gnus-data-pos (gnus-data-find
2296                    ,(or number '(gnus-summary-article-number)))))
2297
2298 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2299 (defmacro gnus-summary-article-subject (&optional number)
2300   "Return current subject string or nil if nothing."
2301   `(let ((headers
2302           ,(if number
2303                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2304              '(gnus-data-header (assq (gnus-summary-article-number)
2305                                       gnus-newsgroup-data)))))
2306      (and headers
2307           (vectorp headers)
2308           (mail-header-subject headers))))
2309
2310 (defmacro gnus-summary-article-score (&optional number)
2311   "Return current article score."
2312   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2313                   gnus-newsgroup-scored))
2314        gnus-summary-default-score 0))
2315
2316 (defun gnus-summary-article-children (&optional number)
2317   "Return a list of article numbers that are children of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2319          (level (gnus-data-level (car data)))
2320          l children)
2321     (while (and (setq data (cdr data))
2322                 (> (setq l (gnus-data-level (car data))) level))
2323       (and (= (1+ level) l)
2324            (push (gnus-data-number (car data))
2325                  children)))
2326     (nreverse children)))
2327
2328 (defun gnus-summary-article-parent (&optional number)
2329   "Return the article number of the parent of article NUMBER."
2330   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2331                                     (gnus-data-list t)))
2332          (level (gnus-data-level (car data))))
2333     (if (zerop level)
2334         ()                              ; This is a root.
2335       ;; We search until we find an article with a level less than
2336       ;; this one.  That function has to be the parent.
2337       (while (and (setq data (cdr data))
2338                   (not (< (gnus-data-level (car data)) level))))
2339       (and data (gnus-data-number (car data))))))
2340
2341 (defun gnus-unread-mark-p (mark)
2342   "Say whether MARK is the unread mark."
2343   (= mark gnus-unread-mark))
2344
2345 (defun gnus-read-mark-p (mark)
2346   "Say whether MARK is one of the marks that mark as read.
2347 This is all marks except unread, ticked, dormant, and expirable."
2348   (not (or (= mark gnus-unread-mark)
2349            (= mark gnus-ticked-mark)
2350            (= mark gnus-dormant-mark)
2351            (= mark gnus-expirable-mark))))
2352
2353 (defmacro gnus-article-mark (number)
2354   "Return the MARK of article NUMBER.
2355 This macro should only be used when computing the mark the \"first\"
2356 time; i.e., when generating the summary lines.  After that,
2357 `gnus-summary-article-mark' should be used to examine the
2358 marks of articles."
2359   `(cond
2360     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2361     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2362     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2363     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2364     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2365     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2366     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2367     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2368            gnus-ancient-mark))))
2369
2370 ;; Saving hidden threads.
2371
2372 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2373 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2374
2375 (defmacro gnus-save-hidden-threads (&rest forms)
2376   "Save hidden threads, eval FORMS, and restore the hidden threads."
2377   (let ((config (make-symbol "config")))
2378     `(let ((,config (gnus-hidden-threads-configuration)))
2379        (unwind-protect
2380            (save-excursion
2381              ,@forms)
2382          (gnus-restore-hidden-threads-configuration ,config)))))
2383
2384 (defun gnus-data-compute-positions ()
2385   "Compute the positions of all articles."
2386   (setq gnus-newsgroup-data-reverse nil)
2387   (let ((data gnus-newsgroup-data))
2388     (save-excursion
2389       (gnus-save-hidden-threads
2390         (gnus-summary-show-all-threads)
2391         (goto-char (point-min))
2392         (while data
2393           (while (get-text-property (point) 'gnus-intangible)
2394             (forward-line 1))
2395           (gnus-data-set-pos (car data) (+ (point) 3))
2396           (setq data (cdr data))
2397           (forward-line 1))))))
2398
2399 (defun gnus-hidden-threads-configuration ()
2400   "Return the current hidden threads configuration."
2401   (save-excursion
2402     (let (config)
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (push (1- (point)) config))
2406       config)))
2407
2408 (defun gnus-restore-hidden-threads-configuration (config)
2409   "Restore hidden threads configuration from CONFIG."
2410   (let (point buffer-read-only)
2411     (while (setq point (pop config))
2412       (when (and (< point (point-max))
2413                  (goto-char point)
2414                  (eq (char-after) ?\n))
2415         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2416
2417 ;; Various summary mode internalish functions.
2418
2419 (defun gnus-mouse-pick-article (e)
2420   (interactive "e")
2421   (mouse-set-point e)
2422   (gnus-summary-next-page nil t))
2423
2424 (defun gnus-summary-set-display-table ()
2425   ;; Change the display table.  Odd characters have a tendency to mess
2426   ;; up nicely formatted displays - we make all possible glyphs
2427   ;; display only a single character.
2428
2429   ;; We start from the standard display table, if any.
2430   (let ((table (or (copy-sequence standard-display-table)
2431                    (make-display-table)))
2432         (i 32))
2433     ;; Nix out all the control chars...
2434     (while (>= (setq i (1- i)) 0)
2435       (aset table i [??]))
2436     ;; ... but not newline and cr, of course.  (cr is necessary for the
2437     ;; selective display).
2438     (aset table ?\n nil)
2439     (aset table ?\r nil)
2440     ;; We keep TAB as well.
2441     (aset table ?\t nil)
2442     ;; We nix out any glyphs over 126 that are not set already.
2443     (let ((i 256))
2444       (while (>= (setq i (1- i)) 127)
2445         ;; Only modify if the entry is nil.
2446         (unless (aref table i)
2447           (aset table i [??]))))
2448     (setq buffer-display-table table)))
2449
2450 (defun gnus-summary-setup-buffer (group)
2451   "Initialize summary buffer."
2452   (let ((buffer (concat "*Summary " group "*")))
2453     (if (get-buffer buffer)
2454         (progn
2455           (set-buffer buffer)
2456           (setq gnus-summary-buffer (current-buffer))
2457           (not gnus-newsgroup-prepared))
2458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2459       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2460       (gnus-summary-mode group)
2461       (when gnus-carpal
2462         (gnus-carpal-setup-buffer 'summary))
2463       (unless gnus-single-article-buffer
2464         (make-local-variable 'gnus-article-buffer)
2465         (make-local-variable 'gnus-article-current)
2466         (make-local-variable 'gnus-original-article-buffer))
2467       (setq gnus-newsgroup-name group)
2468       t)))
2469
2470 (defun gnus-set-global-variables ()
2471   ;; Set the global equivalents of the summary buffer-local variables
2472   ;; to the latest values they had.  These reflect the summary buffer
2473   ;; that was in action when the last article was fetched.
2474   (when (eq major-mode 'gnus-summary-mode)
2475     (setq gnus-summary-buffer (current-buffer))
2476     (let ((name gnus-newsgroup-name)
2477           (marked gnus-newsgroup-marked)
2478           (unread gnus-newsgroup-unreads)
2479           (headers gnus-current-headers)
2480           (data gnus-newsgroup-data)
2481           (summary gnus-summary-buffer)
2482           (article-buffer gnus-article-buffer)
2483           (original gnus-original-article-buffer)
2484           (gac gnus-article-current)
2485           (reffed gnus-reffed-article-number)
2486           (score-file gnus-current-score-file)
2487           (default-charset gnus-newsgroup-charset))
2488       (save-excursion
2489         (set-buffer gnus-group-buffer)
2490         (setq gnus-newsgroup-name name
2491               gnus-newsgroup-marked marked
2492               gnus-newsgroup-unreads unread
2493               gnus-current-headers headers
2494               gnus-newsgroup-data data
2495               gnus-article-current gac
2496               gnus-summary-buffer summary
2497               gnus-article-buffer article-buffer
2498               gnus-original-article-buffer original
2499               gnus-reffed-article-number reffed
2500               gnus-current-score-file score-file
2501               gnus-newsgroup-charset default-charset)
2502         ;; The article buffer also has local variables.
2503         (when (gnus-buffer-live-p gnus-article-buffer)
2504           (set-buffer gnus-article-buffer)
2505           (setq gnus-summary-buffer summary))))))
2506
2507 (defun gnus-summary-article-unread-p (article)
2508   "Say whether ARTICLE is unread or not."
2509   (memq article gnus-newsgroup-unreads))
2510
2511 (defun gnus-summary-first-article-p (&optional article)
2512   "Return whether ARTICLE is the first article in the buffer."
2513   (if (not (setq article (or article (gnus-summary-article-number))))
2514       nil
2515     (eq article (caar gnus-newsgroup-data))))
2516
2517 (defun gnus-summary-last-article-p (&optional article)
2518   "Return whether ARTICLE is the last article in the buffer."
2519   (if (not (setq article (or article (gnus-summary-article-number))))
2520       ;; All non-existent numbers are the last article.  :-)
2521       t
2522     (not (cdr (gnus-data-find-list article)))))
2523
2524 (defun gnus-make-thread-indent-array ()
2525   (let ((n 200))
2526     (unless (and gnus-thread-indent-array
2527                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2528       (setq gnus-thread-indent-array (make-vector 201 "")
2529             gnus-thread-indent-array-level gnus-thread-indent-level)
2530       (while (>= n 0)
2531         (aset gnus-thread-indent-array n
2532               (make-string (* n gnus-thread-indent-level) ? ))
2533         (setq n (1- n))))))
2534
2535 (defun gnus-update-summary-mark-positions ()
2536   "Compute where the summary marks are to go."
2537   (save-excursion
2538     (when (gnus-buffer-exists-p gnus-summary-buffer)
2539       (set-buffer gnus-summary-buffer))
2540     (let ((gnus-replied-mark 129)
2541           (gnus-score-below-mark 130)
2542           (gnus-score-over-mark 130)
2543           (gnus-download-mark 131)
2544           (spec gnus-summary-line-format-spec)
2545           gnus-visual pos)
2546       (save-excursion
2547         (gnus-set-work-buffer)
2548         (let ((gnus-summary-line-format-spec spec)
2549               (gnus-newsgroup-downloadable '((0 . t))))
2550           (gnus-summary-insert-line
2551            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2552           (goto-char (point-min))
2553           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2554                                              (- (point) 2)))))
2555           (goto-char (point-min))
2556           (push (cons 'replied (and (search-forward "\201" nil t)
2557                                     (- (point) 2)))
2558                 pos)
2559           (goto-char (point-min))
2560           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2561                 pos)
2562           (goto-char (point-min))
2563           (push (cons 'download
2564                       (and (search-forward "\203" nil t) (- (point) 2)))
2565                 pos)))
2566       (setq gnus-summary-mark-positions pos))))
2567
2568 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2569   "Insert a dummy root in the summary buffer."
2570   (beginning-of-line)
2571   (gnus-add-text-properties
2572    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2573    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2574
2575 (defun gnus-summary-from-or-to-or-newsgroups (header)
2576   (let ((to (cdr (assq 'To (mail-header-extra header))))
2577         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2578         (mail-parse-charset gnus-newsgroup-charset)
2579         (mail-parse-ignored-charsets 
2580          (save-excursion (set-buffer gnus-summary-buffer)
2581                          gnus-newsgroup-ignored-charsets)))
2582     (cond
2583      ((and to
2584            gnus-ignored-from-addresses
2585            (string-match gnus-ignored-from-addresses
2586                          (mail-header-from header)))
2587       (concat "-> "
2588               (or (car (funcall gnus-extract-address-components
2589                                 (funcall
2590                                  gnus-decode-encoded-word-function to)))
2591                   (funcall gnus-decode-encoded-word-function to))))
2592      ((and newsgroups
2593            gnus-ignored-from-addresses
2594            (string-match gnus-ignored-from-addresses
2595                          (mail-header-from header)))
2596       (concat "=> " newsgroups))
2597      (t
2598       (or (car (funcall gnus-extract-address-components
2599                         (mail-header-from header)))
2600           (mail-header-from header))))))
2601
2602 (defun gnus-summary-insert-line (gnus-tmp-header
2603                                  gnus-tmp-level gnus-tmp-current
2604                                  gnus-tmp-unread gnus-tmp-replied
2605                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2606                                  &optional gnus-tmp-dummy gnus-tmp-score
2607                                  gnus-tmp-process)
2608   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2609          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2610          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2611          (gnus-tmp-score-char
2612           (if (or (null gnus-summary-default-score)
2613                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2614                       gnus-summary-zcore-fuzz))
2615               ?  ;Whitespace
2616             (if (< gnus-tmp-score gnus-summary-default-score)
2617                 gnus-score-below-mark gnus-score-over-mark)))
2618          (gnus-tmp-replied
2619           (cond (gnus-tmp-process gnus-process-mark)
2620                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2621                  gnus-cached-mark)
2622                 (gnus-tmp-replied gnus-replied-mark)
2623                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2624                  gnus-saved-mark)
2625                 (t gnus-unread-mark)))
2626          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2627          (gnus-tmp-name
2628           (cond
2629            ((string-match "<[^>]+> *$" gnus-tmp-from)
2630             (let ((beg (match-beginning 0)))
2631               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2632                        (substring gnus-tmp-from (1+ (match-beginning 0))
2633                                   (1- (match-end 0))))
2634                   (substring gnus-tmp-from 0 beg))))
2635            ((string-match "(.+)" gnus-tmp-from)
2636             (substring gnus-tmp-from
2637                        (1+ (match-beginning 0)) (1- (match-end 0))))
2638            (t gnus-tmp-from)))
2639          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2640          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2641          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2642          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2643          (buffer-read-only nil))
2644     (when (string= gnus-tmp-name "")
2645       (setq gnus-tmp-name gnus-tmp-from))
2646     (unless (numberp gnus-tmp-lines)
2647       (setq gnus-tmp-lines 0))
2648     (gnus-put-text-property
2649      (point)
2650      (progn (eval gnus-summary-line-format-spec) (point))
2651      'gnus-number gnus-tmp-number)
2652     (when (gnus-visual-p 'summary-highlight 'highlight)
2653       (forward-line -1)
2654       (gnus-run-hooks 'gnus-summary-update-hook)
2655       (forward-line 1))))
2656
2657 (defun gnus-summary-update-line (&optional dont-update)
2658   ;; Update summary line after change.
2659   (when (and gnus-summary-default-score
2660              (not gnus-summary-inhibit-highlight))
2661     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2662            (article (gnus-summary-article-number))
2663            (score (gnus-summary-article-score article)))
2664       (unless dont-update
2665         (if (and gnus-summary-mark-below
2666                  (< (gnus-summary-article-score)
2667                     gnus-summary-mark-below))
2668             ;; This article has a low score, so we mark it as read.
2669             (when (memq article gnus-newsgroup-unreads)
2670               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2671           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2672             ;; This article was previously marked as read on account
2673             ;; of a low score, but now it has risen, so we mark it as
2674             ;; unread.
2675             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2676         (gnus-summary-update-mark
2677          (if (or (null gnus-summary-default-score)
2678                  (<= (abs (- score gnus-summary-default-score))
2679                      gnus-summary-zcore-fuzz))
2680              ?  ;Whitespace
2681            (if (< score gnus-summary-default-score)
2682                gnus-score-below-mark gnus-score-over-mark))
2683          'score))
2684       ;; Do visual highlighting.
2685       (when (gnus-visual-p 'summary-highlight 'highlight)
2686         (gnus-run-hooks 'gnus-summary-update-hook)))))
2687
2688 (defvar gnus-tmp-new-adopts nil)
2689
2690 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2691   "Return the number of articles in THREAD.
2692 This may be 0 in some cases -- if none of the articles in
2693 the thread are to be displayed."
2694   (let* ((number
2695           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2696           (cond
2697            ((not (listp thread))
2698             1)
2699            ((and (consp thread) (cdr thread))
2700             (apply
2701              '+ 1 (mapcar
2702                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2703            ((null thread)
2704             1)
2705            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2706             1)
2707            (t 0))))
2708     (when (and level (zerop level) gnus-tmp-new-adopts)
2709       (incf number
2710             (apply '+ (mapcar
2711                        'gnus-summary-number-of-articles-in-thread
2712                        gnus-tmp-new-adopts))))
2713     (if char
2714         (if (> number 1) gnus-not-empty-thread-mark
2715           gnus-empty-thread-mark)
2716       number)))
2717
2718 (defun gnus-summary-set-local-parameters (group)
2719   "Go through the local params of GROUP and set all variable specs in that list."
2720   (let ((params (gnus-group-find-parameter group))
2721         elem)
2722     (while params
2723       (setq elem (car params)
2724             params (cdr params))
2725       (and (consp elem)                 ; Has to be a cons.
2726            (consp (cdr elem))           ; The cdr has to be a list.
2727            (symbolp (car elem))         ; Has to be a symbol in there.
2728            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2729            (ignore-errors               ; So we set it.
2730              (make-local-variable (car elem))
2731              (set (car elem) (eval (nth 1 elem))))))))
2732
2733 (defun gnus-summary-read-group (group &optional show-all no-article
2734                                       kill-buffer no-display backward
2735                                       select-articles)
2736   "Start reading news in newsgroup GROUP.
2737 If SHOW-ALL is non-nil, already read articles are also listed.
2738 If NO-ARTICLE is non-nil, no article is selected initially.
2739 If NO-DISPLAY, don't generate a summary buffer."
2740   (let (result)
2741     (while (and group
2742                 (null (setq result
2743                             (let ((gnus-auto-select-next nil))
2744                               (or (gnus-summary-read-group-1
2745                                    group show-all no-article
2746                                    kill-buffer no-display
2747                                    select-articles)
2748                                   (setq show-all nil
2749                                         select-articles nil)))))
2750                 (eq gnus-auto-select-next 'quietly))
2751       (set-buffer gnus-group-buffer)
2752       ;; The entry function called above goes to the next
2753       ;; group automatically, so we go two groups back
2754       ;; if we are searching for the previous group.
2755       (when backward
2756         (gnus-group-prev-unread-group 2))
2757       (if (not (equal group (gnus-group-group-name)))
2758           (setq group (gnus-group-group-name))
2759         (setq group nil)))
2760     result))
2761
2762 (defun gnus-summary-read-group-1 (group show-all no-article
2763                                         kill-buffer no-display
2764                                         &optional select-articles)
2765   ;; Killed foreign groups can't be entered.
2766   (when (and (not (gnus-group-native-p group))
2767              (not (gnus-gethash group gnus-newsrc-hashtb)))
2768     (error "Dead non-native groups can't be entered"))
2769   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2770   (let* ((new-group (gnus-summary-setup-buffer group))
2771          (quit-config (gnus-group-quit-config group))
2772          (did-select (and new-group (gnus-select-newsgroup
2773                                      group show-all select-articles))))
2774     (cond
2775      ;; This summary buffer exists already, so we just select it.
2776      ((not new-group)
2777       (gnus-set-global-variables)
2778       (when kill-buffer
2779         (gnus-kill-or-deaden-summary kill-buffer))
2780       (gnus-configure-windows 'summary 'force)
2781       (gnus-set-mode-line 'summary)
2782       (gnus-summary-position-point)
2783       (message "")
2784       t)
2785      ;; We couldn't select this group.
2786      ((null did-select)
2787       (when (and (eq major-mode 'gnus-summary-mode)
2788                  (not (equal (current-buffer) kill-buffer)))
2789         (kill-buffer (current-buffer))
2790         (if (not quit-config)
2791             (progn
2792               ;; Update the info -- marks might need to be removed,
2793               ;; for instance.
2794               (gnus-summary-update-info)
2795               (set-buffer gnus-group-buffer)
2796               (gnus-group-jump-to-group group)
2797               (gnus-group-next-unread-group 1))
2798           (gnus-handle-ephemeral-exit quit-config)))
2799       (gnus-message 3 "Can't select group")
2800       nil)
2801      ;; The user did a `C-g' while prompting for number of articles,
2802      ;; so we exit this group.
2803      ((eq did-select 'quit)
2804       (and (eq major-mode 'gnus-summary-mode)
2805            (not (equal (current-buffer) kill-buffer))
2806            (kill-buffer (current-buffer)))
2807       (when kill-buffer
2808         (gnus-kill-or-deaden-summary kill-buffer))
2809       (if (not quit-config)
2810           (progn
2811             (set-buffer gnus-group-buffer)
2812             (gnus-group-jump-to-group group)
2813             (gnus-group-next-unread-group 1)
2814             (gnus-configure-windows 'group 'force))
2815         (gnus-handle-ephemeral-exit quit-config))
2816       ;; Finally signal the quit.
2817       (signal 'quit nil))
2818      ;; The group was successfully selected.
2819      (t
2820       (gnus-set-global-variables)
2821       ;; Save the active value in effect when the group was entered.
2822       (setq gnus-newsgroup-active
2823             (gnus-copy-sequence
2824              (gnus-active gnus-newsgroup-name)))
2825       ;; You can change the summary buffer in some way with this hook.
2826       (gnus-run-hooks 'gnus-select-group-hook)
2827       ;; Set any local variables in the group parameters.
2828       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2829       (gnus-update-format-specifications
2830        nil 'summary 'summary-mode 'summary-dummy)
2831       (gnus-update-summary-mark-positions)
2832       ;; Do score processing.
2833       (when gnus-use-scoring
2834         (gnus-possibly-score-headers))
2835       ;; Check whether to fill in the gaps in the threads.
2836       (when gnus-build-sparse-threads
2837         (gnus-build-sparse-threads))
2838       ;; Find the initial limit.
2839       (if gnus-show-threads
2840           (if show-all
2841               (let ((gnus-newsgroup-dormant nil))
2842                 (gnus-summary-initial-limit show-all))
2843             (gnus-summary-initial-limit show-all))
2844         ;; When untreaded, all articles are always shown.
2845         (setq gnus-newsgroup-limit
2846               (mapcar
2847                (lambda (header) (mail-header-number header))
2848                gnus-newsgroup-headers)))
2849       ;; Generate the summary buffer.
2850       (unless no-display
2851         (gnus-summary-prepare))
2852       (when gnus-use-trees
2853         (gnus-tree-open group)
2854         (setq gnus-summary-highlight-line-function
2855               'gnus-tree-highlight-article))
2856       ;; If the summary buffer is empty, but there are some low-scored
2857       ;; articles or some excluded dormants, we include these in the
2858       ;; buffer.
2859       (when (and (zerop (buffer-size))
2860                  (not no-display))
2861         (cond (gnus-newsgroup-dormant
2862                (gnus-summary-limit-include-dormant))
2863               ((and gnus-newsgroup-scored show-all)
2864                (gnus-summary-limit-include-expunged t))))
2865       ;; Function `gnus-apply-kill-file' must be called in this hook.
2866       (gnus-run-hooks 'gnus-apply-kill-hook)
2867       (if (and (zerop (buffer-size))
2868                (not no-display))
2869           (progn
2870             ;; This newsgroup is empty.
2871             (gnus-summary-catchup-and-exit nil t)
2872             (gnus-message 6 "No unread news")
2873             (when kill-buffer
2874               (gnus-kill-or-deaden-summary kill-buffer))
2875             ;; Return nil from this function.
2876             nil)
2877         ;; Hide conversation thread subtrees.  We cannot do this in
2878         ;; gnus-summary-prepare-hook since kill processing may not
2879         ;; work with hidden articles.
2880         (and gnus-show-threads
2881              gnus-thread-hide-subtree
2882              (gnus-summary-hide-all-threads))
2883         (when kill-buffer
2884           (gnus-kill-or-deaden-summary kill-buffer))
2885         ;; Show first unread article if requested.
2886         (if (and (not no-article)
2887                  (not no-display)
2888                  gnus-newsgroup-unreads
2889                  gnus-auto-select-first)
2890             (progn
2891               (gnus-configure-windows 'summary)
2892               (cond
2893                ((eq gnus-auto-select-first 'best)
2894                 (gnus-summary-best-unread-article))
2895                ((eq gnus-auto-select-first t)
2896                 (gnus-summary-first-unread-article))
2897                ((gnus-functionp gnus-auto-select-first)
2898                 (funcall gnus-auto-select-first))))
2899           ;; Don't select any articles, just move point to the first
2900           ;; article in the group.
2901           (goto-char (point-min))
2902           (gnus-summary-position-point)
2903           (gnus-configure-windows 'summary 'force)
2904           (gnus-set-mode-line 'summary))
2905         (when (get-buffer-window gnus-group-buffer t)
2906           ;; Gotta use windows, because recenter does weird stuff if
2907           ;; the current buffer ain't the displayed window.
2908           (let ((owin (selected-window)))
2909             (select-window (get-buffer-window gnus-group-buffer t))
2910             (when (gnus-group-goto-group group)
2911               (recenter))
2912             (select-window owin)))
2913         ;; Mark this buffer as "prepared".
2914         (setq gnus-newsgroup-prepared t)
2915         (gnus-run-hooks 'gnus-summary-prepared-hook)
2916         t)))))
2917
2918 (defun gnus-summary-prepare ()
2919   "Generate the summary buffer."
2920   (interactive)
2921   (let ((buffer-read-only nil))
2922     (erase-buffer)
2923     (setq gnus-newsgroup-data nil
2924           gnus-newsgroup-data-reverse nil)
2925     (gnus-run-hooks 'gnus-summary-generate-hook)
2926     ;; Generate the buffer, either with threads or without.
2927     (when gnus-newsgroup-headers
2928       (gnus-summary-prepare-threads
2929        (if gnus-show-threads
2930            (gnus-sort-gathered-threads
2931             (funcall gnus-summary-thread-gathering-function
2932                      (gnus-sort-threads
2933                       (gnus-cut-threads (gnus-make-threads)))))
2934          ;; Unthreaded display.
2935          (gnus-sort-articles gnus-newsgroup-headers))))
2936     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2937     ;; Call hooks for modifying summary buffer.
2938     (goto-char (point-min))
2939     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2940
2941 (defsubst gnus-general-simplify-subject (subject)
2942   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2943   (setq subject
2944         (cond
2945          ;; Truncate the subject.
2946          (gnus-simplify-subject-functions
2947           (gnus-map-function gnus-simplify-subject-functions subject))
2948          ((numberp gnus-summary-gather-subject-limit)
2949           (setq subject (gnus-simplify-subject-re subject))
2950           (if (> (length subject) gnus-summary-gather-subject-limit)
2951               (substring subject 0 gnus-summary-gather-subject-limit)
2952             subject))
2953          ;; Fuzzily simplify it.
2954          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2955           (gnus-simplify-subject-fuzzy subject))
2956          ;; Just remove the leading "Re:".
2957          (t
2958           (gnus-simplify-subject-re subject))))
2959
2960   (if (and gnus-summary-gather-exclude-subject
2961            (string-match gnus-summary-gather-exclude-subject subject))
2962       nil                               ; This article shouldn't be gathered
2963     subject))
2964
2965 (defun gnus-summary-simplify-subject-query ()
2966   "Query where the respool algorithm would put this article."
2967   (interactive)
2968   (gnus-summary-select-article)
2969   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2970
2971 (defun gnus-gather-threads-by-subject (threads)
2972   "Gather threads by looking at Subject headers."
2973   (if (not gnus-summary-make-false-root)
2974       threads
2975     (let ((hashtb (gnus-make-hashtable 1024))
2976           (prev threads)
2977           (result threads)
2978           subject hthread whole-subject)
2979       (while threads
2980         (setq subject (gnus-general-simplify-subject
2981                        (setq whole-subject (mail-header-subject
2982                                             (caar threads)))))
2983         (when subject
2984           (if (setq hthread (gnus-gethash subject hashtb))
2985               (progn
2986                 ;; We enter a dummy root into the thread, if we
2987                 ;; haven't done that already.
2988                 (unless (stringp (caar hthread))
2989                   (setcar hthread (list whole-subject (car hthread))))
2990                 ;; We add this new gathered thread to this gathered
2991                 ;; thread.
2992                 (setcdr (car hthread)
2993                         (nconc (cdar hthread) (list (car threads))))
2994                 ;; Remove it from the list of threads.
2995                 (setcdr prev (cdr threads))
2996                 (setq threads prev))
2997             ;; Enter this thread into the hash table.
2998             (gnus-sethash subject threads hashtb)))
2999         (setq prev threads)
3000         (setq threads (cdr threads)))
3001       result)))
3002
3003 (defun gnus-gather-threads-by-references (threads)
3004   "Gather threads by looking at References headers."
3005   (let ((idhashtb (gnus-make-hashtable 1024))
3006         (thhashtb (gnus-make-hashtable 1024))
3007         (prev threads)
3008         (result threads)
3009         ids references id gthread gid entered ref)
3010     (while threads
3011       (when (setq references (mail-header-references (caar threads)))
3012         (setq id (mail-header-id (caar threads))
3013               ids (gnus-split-references references)
3014               entered nil)
3015         (while (setq ref (pop ids))
3016           (setq ids (delete ref ids))
3017           (if (not (setq gid (gnus-gethash ref idhashtb)))
3018               (progn
3019                 (gnus-sethash ref id idhashtb)
3020                 (gnus-sethash id threads thhashtb))
3021             (setq gthread (gnus-gethash gid thhashtb))
3022             (unless entered
3023               ;; We enter a dummy root into the thread, if we
3024               ;; haven't done that already.
3025               (unless (stringp (caar gthread))
3026                 (setcar gthread (list (mail-header-subject (caar gthread))
3027                                       (car gthread))))
3028               ;; We add this new gathered thread to this gathered
3029               ;; thread.
3030               (setcdr (car gthread)
3031                       (nconc (cdar gthread) (list (car threads)))))
3032             ;; Add it into the thread hash table.
3033             (gnus-sethash id gthread thhashtb)
3034             (setq entered t)
3035             ;; Remove it from the list of threads.
3036             (setcdr prev (cdr threads))
3037             (setq threads prev))))
3038       (setq prev threads)
3039       (setq threads (cdr threads)))
3040     result))
3041
3042 (defun gnus-sort-gathered-threads (threads)
3043   "Sort subtreads inside each gathered thread by article number."
3044   (let ((result threads))
3045     (while threads
3046       (when (stringp (caar threads))
3047         (setcdr (car threads)
3048                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3049       (setq threads (cdr threads)))
3050     result))
3051
3052 (defun gnus-thread-loop-p (root thread)
3053   "Say whether ROOT is in THREAD."
3054   (let ((stack (list thread))
3055         (infloop 0)
3056         th)
3057     (while (setq thread (pop stack))
3058       (setq th (cdr thread))
3059       (while (and th
3060                   (not (eq (caar th) root)))
3061         (pop th))
3062       (if th
3063           ;; We have found a loop.
3064           (let (ref-dep)
3065             (setcdr thread (delq (car th) (cdr thread)))
3066             (if (boundp (setq ref-dep (intern "none"
3067                                               gnus-newsgroup-dependencies)))
3068                 (setcdr (symbol-value ref-dep)
3069                         (nconc (cdr (symbol-value ref-dep))
3070                                (list (car th))))
3071               (set ref-dep (list nil (car th))))
3072             (setq infloop 1
3073                   stack nil))
3074         ;; Push all the subthreads onto the stack.
3075         (push (cdr thread) stack)))
3076     infloop))
3077
3078 (defun gnus-make-threads ()
3079   "Go through the dependency hashtb and find the roots.  Return all threads."
3080   (let (threads)
3081     (while (catch 'infloop
3082              (mapatoms
3083               (lambda (refs)
3084                 ;; Deal with self-referencing References loops.
3085                 (when (and (car (symbol-value refs))
3086                            (not (zerop
3087                                  (apply
3088                                   '+
3089                                   (mapcar
3090                                    (lambda (thread)
3091                                      (gnus-thread-loop-p
3092                                       (car (symbol-value refs)) thread))
3093                                    (cdr (symbol-value refs)))))))
3094                   (setq threads nil)
3095                   (throw 'infloop t))
3096                 (unless (car (symbol-value refs))
3097                   ;; These threads do not refer back to any other articles,
3098                   ;; so they're roots.
3099                   (setq threads (append (cdr (symbol-value refs)) threads))))
3100               gnus-newsgroup-dependencies)))
3101     threads))
3102
3103 ;; Build the thread tree.
3104 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3105   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3106
3107 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3108 if it was already present.
3109
3110 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3111 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3112 Message-IDs will be renamed be renamed to a unique Message-ID before
3113 being entered.
3114
3115 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3116   (let* ((id (mail-header-id header))
3117          (id-dep (and id (intern id dependencies)))
3118          ref ref-dep ref-header)
3119     ;; Enter this `header' in the `dependencies' table.
3120     (cond
3121      ((not id-dep)
3122       (setq header nil))
3123      ;; The first two cases do the normal part: enter a new `header'
3124      ;; in the `dependencies' table.
3125      ((not (boundp id-dep))
3126       (set id-dep (list header)))
3127      ((null (car (symbol-value id-dep)))
3128       (setcar (symbol-value id-dep) header))
3129
3130      ;; From here the `header' was already present in the
3131      ;; `dependencies' table.
3132      (force-new
3133       ;; Overrides an existing entry;
3134       ;; just set the header part of the entry.
3135       (setcar (symbol-value id-dep) header))
3136
3137      ;; Renames the existing `header' to a unique Message-ID.
3138      ((not gnus-summary-ignore-duplicates)
3139       ;; An article with this Message-ID has already been seen.
3140       ;; We rename the Message-ID.
3141       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3142            (list header))
3143       (mail-header-set-id header id))
3144
3145      ;; The last case ignores an existing entry, except it adds any
3146      ;; additional Xrefs (in case the two articles came from different
3147      ;; servers.
3148      ;; Also sets `header' to `nil' meaning that the `dependencies'
3149      ;; table was *not* modified.
3150      (t
3151       (mail-header-set-xref
3152        (car (symbol-value id-dep))
3153        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3154                    "")
3155                (or (mail-header-xref header) "")))
3156       (setq header nil)))
3157
3158     (when header
3159       ;; First check if that we are not creating a References loop.
3160       (setq ref (gnus-parent-id (mail-header-references header)))
3161       (while (and ref
3162                   (setq ref-dep (intern-soft ref dependencies))
3163                   (boundp ref-dep)
3164                   (setq ref-header (car (symbol-value ref-dep))))
3165         (if (string= id ref)
3166             ;; Yuk!  This is a reference loop.  Make the article be a
3167             ;; root article.
3168             (progn
3169               (mail-header-set-references (car (symbol-value id-dep)) "none")
3170               (setq ref nil))
3171           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3172       (setq ref (gnus-parent-id (mail-header-references header)))
3173       (setq ref-dep (intern (or ref "none") dependencies))
3174       (if (boundp ref-dep)
3175           (setcdr (symbol-value ref-dep)
3176                   (nconc (cdr (symbol-value ref-dep))
3177                          (list (symbol-value id-dep))))
3178         (set ref-dep (list nil (symbol-value id-dep)))))
3179     header))
3180
3181 (defun gnus-build-sparse-threads ()
3182   (let ((headers gnus-newsgroup-headers)
3183         (mail-parse-charset gnus-newsgroup-charset)
3184         (gnus-summary-ignore-duplicates t)
3185         header references generation relations
3186         subject child end new-child date)
3187     ;; First we create an alist of generations/relations, where
3188     ;; generations is how much we trust the relation, and the relation
3189     ;; is parent/child.
3190     (gnus-message 7 "Making sparse threads...")
3191     (save-excursion
3192       (nnheader-set-temp-buffer " *gnus sparse threads*")
3193       (while (setq header (pop headers))
3194         (when (and (setq references (mail-header-references header))
3195                    (not (string= references "")))
3196           (insert references)
3197           (setq child (mail-header-id header)
3198                 subject (mail-header-subject header)
3199                 date (mail-header-date header)
3200                 generation 0)
3201           (while (search-backward ">" nil t)
3202             (setq end (1+ (point)))
3203             (when (search-backward "<" nil t)
3204               (setq new-child (buffer-substring (point) end))
3205               (push (list (incf generation)
3206                           child (setq child new-child)
3207                           subject date)
3208                     relations)))
3209           (when child
3210             (push (list (1+ generation) child nil subject) relations))
3211           (erase-buffer)))
3212       (kill-buffer (current-buffer)))
3213     ;; Sort over trustworthiness.
3214     (mapcar
3215      (lambda (relation)
3216        (when (gnus-dependencies-add-header
3217               (make-full-mail-header
3218                gnus-reffed-article-number
3219                (nth 3 relation) "" (or (nth 4 relation) "")
3220                (nth 1 relation)
3221                (or (nth 2 relation) "") 0 0 "")
3222               gnus-newsgroup-dependencies nil)
3223          (push gnus-reffed-article-number gnus-newsgroup-limit)
3224          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3225          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3226                gnus-newsgroup-reads)
3227          (decf gnus-reffed-article-number)))
3228      (sort relations 'car-less-than-car))
3229     (gnus-message 7 "Making sparse threads...done")))
3230
3231 (defun gnus-build-old-threads ()
3232   ;; Look at all the articles that refer back to old articles, and
3233   ;; fetch the headers for the articles that aren't there.  This will
3234   ;; build complete threads - if the roots haven't been expired by the
3235   ;; server, that is.
3236   (let ((mail-parse-charset gnus-newsgroup-charset)
3237         id heads)
3238     (mapatoms
3239      (lambda (refs)
3240        (when (not (car (symbol-value refs)))
3241          (setq heads (cdr (symbol-value refs)))
3242          (while heads
3243            (if (memq (mail-header-number (caar heads))
3244                      gnus-newsgroup-dormant)
3245                (setq heads (cdr heads))
3246              (setq id (symbol-name refs))
3247              (while (and (setq id (gnus-build-get-header id))
3248                          (not (car (gnus-id-to-thread id)))))
3249              (setq heads nil)))))
3250      gnus-newsgroup-dependencies)))
3251
3252 ;; This function has to be called with point after the article number
3253 ;; on the beginning of the line.
3254 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3255   (let ((eol (gnus-point-at-eol))
3256         (buffer (current-buffer))
3257         header)
3258
3259     ;; overview: [num subject from date id refs chars lines misc]
3260     (unwind-protect
3261         (progn
3262           (narrow-to-region (point) eol)
3263           (unless (eobp)
3264             (forward-char))
3265
3266           (setq header
3267                 (make-full-mail-header
3268                  number                 ; number
3269                  (funcall gnus-decode-encoded-word-function
3270                           (nnheader-nov-field)) ; subject
3271                  (funcall gnus-decode-encoded-word-function
3272                           (nnheader-nov-field)) ; from
3273                  (nnheader-nov-field)   ; date
3274                  (nnheader-nov-read-message-id) ; id
3275                  (nnheader-nov-field)   ; refs
3276                  (nnheader-nov-read-integer) ; chars
3277                  (nnheader-nov-read-integer) ; lines
3278                  (unless (eobp)
3279                    (nnheader-nov-field)) ; misc
3280                  (nnheader-nov-parse-extra)))) ; extra
3281
3282       (widen))
3283
3284     (when gnus-alter-header-function
3285       (funcall gnus-alter-header-function header))
3286     (gnus-dependencies-add-header header dependencies force-new)))
3287
3288 (defun gnus-build-get-header (id)
3289   ;; Look through the buffer of NOV lines and find the header to
3290   ;; ID.  Enter this line into the dependencies hash table, and return
3291   ;; the id of the parent article (if any).
3292   (let ((deps gnus-newsgroup-dependencies)
3293         found header)
3294     (prog1
3295         (save-excursion
3296           (set-buffer nntp-server-buffer)
3297           (let ((case-fold-search nil))
3298             (goto-char (point-min))
3299             (while (and (not found)
3300                         (search-forward id nil t))
3301               (beginning-of-line)
3302               (setq found (looking-at
3303                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3304                                    (regexp-quote id))))
3305               (or found (beginning-of-line 2)))
3306             (when found
3307               (beginning-of-line)
3308               (and
3309                (setq header (gnus-nov-parse-line
3310                              (read (current-buffer)) deps))
3311                (gnus-parent-id (mail-header-references header))))))
3312       (when header
3313         (let ((number (mail-header-number header)))
3314           (push number gnus-newsgroup-limit)
3315           (push header gnus-newsgroup-headers)
3316           (if (memq number gnus-newsgroup-unselected)
3317               (progn
3318                 (push number gnus-newsgroup-unreads)
3319                 (setq gnus-newsgroup-unselected
3320                       (delq number gnus-newsgroup-unselected)))
3321             (push number gnus-newsgroup-ancient)))))))
3322
3323 (defun gnus-build-all-threads ()
3324   "Read all the headers."
3325   (let ((gnus-summary-ignore-duplicates t)
3326         (mail-parse-charset gnus-newsgroup-charset)
3327         (dependencies gnus-newsgroup-dependencies)
3328         header article)
3329     (save-excursion
3330       (set-buffer nntp-server-buffer)
3331       (let ((case-fold-search nil))
3332         (goto-char (point-min))
3333         (while (not (eobp))
3334           (ignore-errors
3335             (setq article (read (current-buffer))
3336                   header (gnus-nov-parse-line article dependencies)))
3337           (when header
3338             (save-excursion
3339               (set-buffer gnus-summary-buffer)
3340               (push header gnus-newsgroup-headers)
3341               (if (memq (setq article (mail-header-number header))
3342                         gnus-newsgroup-unselected)
3343                   (progn
3344                     (push article gnus-newsgroup-unreads)
3345                     (setq gnus-newsgroup-unselected
3346                           (delq article gnus-newsgroup-unselected)))
3347                 (push article gnus-newsgroup-ancient)))
3348             (forward-line 1)))))))
3349
3350 (defun gnus-summary-update-article-line (article header)
3351   "Update the line for ARTICLE using HEADERS."
3352   (let* ((id (mail-header-id header))
3353          (thread (gnus-id-to-thread id)))
3354     (unless thread
3355       (error "Article in no thread"))
3356     ;; Update the thread.
3357     (setcar thread header)
3358     (gnus-summary-goto-subject article)
3359     (let* ((datal (gnus-data-find-list article))
3360            (data (car datal))
3361            (length (when (cdr datal)
3362                      (- (gnus-data-pos data)
3363                         (gnus-data-pos (cadr datal)))))
3364            (buffer-read-only nil)
3365            (level (gnus-summary-thread-level)))
3366       (gnus-delete-line)
3367       (gnus-summary-insert-line
3368        header level nil (gnus-article-mark article)
3369        (memq article gnus-newsgroup-replied)
3370        (memq article gnus-newsgroup-expirable)
3371        ;; Only insert the Subject string when it's different
3372        ;; from the previous Subject string.
3373        (if (gnus-subject-equal
3374             (condition-case ()
3375                 (mail-header-subject
3376                  (gnus-data-header
3377                   (cadr
3378                    (gnus-data-find-list
3379                     article
3380                     (gnus-data-list t)))))
3381               ;; Error on the side of excessive subjects.
3382               (error ""))
3383             (mail-header-subject header))
3384            ""
3385          (mail-header-subject header))
3386        nil (cdr (assq article gnus-newsgroup-scored))
3387        (memq article gnus-newsgroup-processable))
3388       (when length
3389         (gnus-data-update-list
3390          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3391
3392 (defun gnus-summary-update-article (article &optional iheader)
3393   "Update ARTICLE in the summary buffer."
3394   (set-buffer gnus-summary-buffer)
3395   (let* ((header (gnus-summary-article-header article))
3396          (id (mail-header-id header))
3397          (data (gnus-data-find article))
3398          (thread (gnus-id-to-thread id))
3399          (references (mail-header-references header))
3400          (parent
3401           (gnus-id-to-thread
3402            (or (gnus-parent-id
3403                 (when (and references
3404                            (not (equal "" references)))
3405                   references))
3406                "none")))
3407          (buffer-read-only nil)
3408          (old (car thread)))
3409     (when thread
3410       (unless iheader
3411         (setcar thread nil)
3412         (when parent
3413           (delq thread parent)))
3414       (if (gnus-summary-insert-subject id header)
3415           ;; Set the (possibly) new article number in the data structure.
3416           (gnus-data-set-number data (gnus-id-to-article id))
3417         (setcar thread old)
3418         nil))))
3419
3420 (defun gnus-rebuild-thread (id &optional line)
3421   "Rebuild the thread containing ID.
3422 If LINE, insert the rebuilt thread starting on line LINE."
3423   (let ((buffer-read-only nil)
3424         old-pos current thread data)
3425     (if (not gnus-show-threads)
3426         (setq thread (list (car (gnus-id-to-thread id))))
3427       ;; Get the thread this article is part of.
3428       (setq thread (gnus-remove-thread id)))
3429     (setq old-pos (gnus-point-at-bol))
3430     (setq current (save-excursion
3431                     (and (zerop (forward-line -1))
3432                          (gnus-summary-article-number))))
3433     ;; If this is a gathered thread, we have to go some re-gathering.
3434     (when (stringp (car thread))
3435       (let ((subject (car thread))
3436             roots thr)
3437         (setq thread (cdr thread))
3438         (while thread
3439           (unless (memq (setq thr (gnus-id-to-thread
3440                                    (gnus-root-id
3441                                     (mail-header-id (caar thread)))))
3442                         roots)
3443             (push thr roots))
3444           (setq thread (cdr thread)))
3445         ;; We now have all (unique) roots.
3446         (if (= (length roots) 1)
3447             ;; All the loose roots are now one solid root.
3448             (setq thread (car roots))
3449           (setq thread (cons subject (gnus-sort-threads roots))))))
3450     (let (threads)
3451       ;; We then insert this thread into the summary buffer.
3452       (when line
3453         (goto-char (point-min))
3454         (forward-line (1- line)))
3455       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3456         (if gnus-show-threads
3457             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3458           (gnus-summary-prepare-unthreaded thread))
3459         (setq data (nreverse gnus-newsgroup-data))
3460         (setq threads gnus-newsgroup-threads))
3461       ;; We splice the new data into the data structure.
3462       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3463       ;;!!! then we want to insert at the beginning of the buffer.
3464       ;;!!! That happens to be true with Gnus now, but that may
3465       ;;!!! change in the future.  Perhaps.
3466       (gnus-data-enter-list
3467        (if line nil current) data (- (point) old-pos))
3468       (setq gnus-newsgroup-threads
3469             (nconc threads gnus-newsgroup-threads))
3470       (gnus-data-compute-positions))))
3471
3472 (defun gnus-number-to-header (number)
3473   "Return the header for article NUMBER."
3474   (let ((headers gnus-newsgroup-headers))
3475     (while (and headers
3476                 (not (= number (mail-header-number (car headers)))))
3477       (pop headers))
3478     (when headers
3479       (car headers))))
3480
3481 (defun gnus-parent-headers (in-headers &optional generation)
3482   "Return the headers of the GENERATIONeth parent of HEADERS."
3483   (unless generation
3484     (setq generation 1))
3485   (let ((parent t)
3486         (headers in-headers)
3487         references)
3488     (while (and parent
3489                 (not (zerop generation))
3490                 (setq references (mail-header-references headers)))
3491       (setq headers (if (and references
3492                              (setq parent (gnus-parent-id references)))
3493                         (car (gnus-id-to-thread parent))
3494                       nil))
3495       (decf generation))
3496     (and (not (eq headers in-headers))
3497          headers)))
3498
3499 (defun gnus-id-to-thread (id)
3500   "Return the (sub-)thread where ID appears."
3501   (gnus-gethash id gnus-newsgroup-dependencies))
3502
3503 (defun gnus-id-to-article (id)
3504   "Return the article number of ID."
3505   (let ((thread (gnus-id-to-thread id)))
3506     (when (and thread
3507                (car thread))
3508       (mail-header-number (car thread)))))
3509
3510 (defun gnus-id-to-header (id)
3511   "Return the article headers of ID."
3512   (car (gnus-id-to-thread id)))
3513
3514 (defun gnus-article-displayed-root-p (article)
3515   "Say whether ARTICLE is a root(ish) article."
3516   (let ((level (gnus-summary-thread-level article))
3517         (refs (mail-header-references  (gnus-summary-article-header article)))
3518         particle)
3519     (cond
3520      ((null level) nil)
3521      ((zerop level) t)
3522      ((null refs) t)
3523      ((null (gnus-parent-id refs)) t)
3524      ((and (= 1 level)
3525            (null (setq particle (gnus-id-to-article
3526                                  (gnus-parent-id refs))))
3527            (null (gnus-summary-thread-level particle)))))))
3528
3529 (defun gnus-root-id (id)
3530   "Return the id of the root of the thread where ID appears."
3531   (let (last-id prev)
3532     (while (and id (setq prev (car (gnus-id-to-thread id))))
3533       (setq last-id id
3534             id (gnus-parent-id (mail-header-references prev))))
3535     last-id))
3536
3537 (defun gnus-articles-in-thread (thread)
3538   "Return the list of articles in THREAD."
3539   (cons (mail-header-number (car thread))
3540         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3541
3542 (defun gnus-remove-thread (id &optional dont-remove)
3543   "Remove the thread that has ID in it."
3544   (let (headers thread last-id)
3545     ;; First go up in this thread until we find the root.
3546     (setq last-id (gnus-root-id id)
3547           headers (message-flatten-list (gnus-id-to-thread last-id)))
3548     ;; We have now found the real root of this thread.  It might have
3549     ;; been gathered into some loose thread, so we have to search
3550     ;; through the threads to find the thread we wanted.
3551     (let ((threads gnus-newsgroup-threads)
3552           sub)
3553       (while threads
3554         (setq sub (car threads))
3555         (if (stringp (car sub))
3556             ;; This is a gathered thread, so we look at the roots
3557             ;; below it to find whether this article is in this
3558             ;; gathered root.
3559             (progn
3560               (setq sub (cdr sub))
3561               (while sub
3562                 (when (member (caar sub) headers)
3563                   (setq thread (car threads)
3564                         threads nil
3565                         sub nil))
3566                 (setq sub (cdr sub))))
3567           ;; It's an ordinary thread, so we check it.
3568           (when (eq (car sub) (car headers))
3569             (setq thread sub
3570                   threads nil)))
3571         (setq threads (cdr threads)))
3572       ;; If this article is in no thread, then it's a root.
3573       (if thread
3574           (unless dont-remove
3575             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3576         (setq thread (gnus-id-to-thread last-id)))
3577       (when thread
3578         (prog1
3579             thread                      ; We return this thread.
3580           (unless dont-remove
3581             (if (stringp (car thread))
3582                 (progn
3583                   ;; If we use dummy roots, then we have to remove the
3584                   ;; dummy root as well.
3585                   (when (eq gnus-summary-make-false-root 'dummy)
3586                     ;; We go to the dummy root by going to
3587                     ;; the first sub-"thread", and then one line up.
3588                     (gnus-summary-goto-article
3589                      (mail-header-number (caadr thread)))
3590                     (forward-line -1)
3591                     (gnus-delete-line)
3592                     (gnus-data-compute-positions))
3593                   (setq thread (cdr thread))
3594                   (while thread
3595                     (gnus-remove-thread-1 (car thread))
3596                     (setq thread (cdr thread))))
3597               (gnus-summary-show-all-threads)
3598               (gnus-remove-thread-1 thread))))))))
3599
3600 (defun gnus-remove-thread-1 (thread)
3601   "Remove the thread THREAD recursively."
3602   (let ((number (mail-header-number (pop thread)))
3603         d)
3604     (setq thread (reverse thread))
3605     (while thread
3606       (gnus-remove-thread-1 (pop thread)))
3607     (when (setq d (gnus-data-find number))
3608       (goto-char (gnus-data-pos d))
3609       (gnus-data-remove
3610        number
3611        (- (gnus-point-at-bol)
3612           (prog1
3613               (1+ (gnus-point-at-eol))
3614             (gnus-delete-line)))))))
3615
3616 (defun gnus-sort-threads (threads)
3617   "Sort THREADS."
3618   (if (not gnus-thread-sort-functions)
3619       threads
3620     (gnus-message 8 "Sorting threads...")
3621     (prog1
3622         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3623       (gnus-message 8 "Sorting threads...done"))))
3624
3625 (defun gnus-sort-articles (articles)
3626   "Sort ARTICLES."
3627   (when gnus-article-sort-functions
3628     (gnus-message 7 "Sorting articles...")
3629     (prog1
3630         (setq gnus-newsgroup-headers
3631               (sort articles (gnus-make-sort-function
3632                               gnus-article-sort-functions)))
3633       (gnus-message 7 "Sorting articles...done"))))
3634
3635 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3636 (defmacro gnus-thread-header (thread)
3637   ;; Return header of first article in THREAD.
3638   ;; Note that THREAD must never, ever be anything else than a variable -
3639   ;; using some other form will lead to serious barfage.
3640   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3641   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3642   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3643         (vector thread) 2))
3644
3645 (defsubst gnus-article-sort-by-number (h1 h2)
3646   "Sort articles by article number."
3647   (< (mail-header-number h1)
3648      (mail-header-number h2)))
3649
3650 (defun gnus-thread-sort-by-number (h1 h2)
3651   "Sort threads by root article number."
3652   (gnus-article-sort-by-number
3653    (gnus-thread-header h1) (gnus-thread-header h2)))
3654
3655 (defsubst gnus-article-sort-by-lines (h1 h2)
3656   "Sort articles by article Lines header."
3657   (< (mail-header-lines h1)
3658      (mail-header-lines h2)))
3659
3660 (defun gnus-thread-sort-by-lines (h1 h2)
3661   "Sort threads by root article Lines header."
3662   (gnus-article-sort-by-lines
3663    (gnus-thread-header h1) (gnus-thread-header h2)))
3664
3665 (defsubst gnus-article-sort-by-chars (h1 h2)
3666   "Sort articles by octet length."
3667   (< (mail-header-chars h1)
3668      (mail-header-chars h2)))
3669
3670 (defun gnus-thread-sort-by-chars (h1 h2)
3671   "Sort threads by root article octet length."
3672   (gnus-article-sort-by-chars
3673    (gnus-thread-header h1) (gnus-thread-header h2)))
3674
3675 (defsubst gnus-article-sort-by-author (h1 h2)
3676   "Sort articles by root author."
3677   (string-lessp
3678    (let ((extract (funcall
3679                    gnus-extract-address-components
3680                    (mail-header-from h1))))
3681      (or (car extract) (cadr extract) ""))
3682    (let ((extract (funcall
3683                    gnus-extract-address-components
3684                    (mail-header-from h2))))
3685      (or (car extract) (cadr extract) ""))))
3686
3687 (defun gnus-thread-sort-by-author (h1 h2)
3688   "Sort threads by root author."
3689   (gnus-article-sort-by-author
3690    (gnus-thread-header h1)  (gnus-thread-header h2)))
3691
3692 (defsubst gnus-article-sort-by-subject (h1 h2)
3693   "Sort articles by root subject."
3694   (string-lessp
3695    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3696    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3697
3698 (defun gnus-thread-sort-by-subject (h1 h2)
3699   "Sort threads by root subject."
3700   (gnus-article-sort-by-subject
3701    (gnus-thread-header h1) (gnus-thread-header h2)))
3702
3703 (defsubst gnus-article-sort-by-date (h1 h2)
3704   "Sort articles by root article date."
3705   (time-less-p
3706    (gnus-date-get-time (mail-header-date h1))
3707    (gnus-date-get-time (mail-header-date h2))))
3708
3709 (defun gnus-thread-sort-by-date (h1 h2)
3710   "Sort threads by root article date."
3711   (gnus-article-sort-by-date
3712    (gnus-thread-header h1) (gnus-thread-header h2)))
3713
3714 (defsubst gnus-article-sort-by-score (h1 h2)
3715   "Sort articles by root article score.
3716 Unscored articles will be counted as having a score of zero."
3717   (> (or (cdr (assq (mail-header-number h1)
3718                     gnus-newsgroup-scored))
3719          gnus-summary-default-score 0)
3720      (or (cdr (assq (mail-header-number h2)
3721                     gnus-newsgroup-scored))
3722          gnus-summary-default-score 0)))
3723
3724 (defun gnus-thread-sort-by-score (h1 h2)
3725   "Sort threads by root article score."
3726   (gnus-article-sort-by-score
3727    (gnus-thread-header h1) (gnus-thread-header h2)))
3728
3729 (defun gnus-thread-sort-by-total-score (h1 h2)
3730   "Sort threads by the sum of all scores in the thread.
3731 Unscored articles will be counted as having a score of zero."
3732   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3733
3734 (defun gnus-thread-total-score (thread)
3735   ;; This function find the total score of THREAD.
3736   (cond ((null thread)
3737          0)
3738         ((consp thread)
3739          (if (stringp (car thread))
3740              (apply gnus-thread-score-function 0
3741                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3742            (gnus-thread-total-score-1 thread)))
3743         (t
3744          (gnus-thread-total-score-1 (list thread)))))
3745
3746 (defun gnus-thread-total-score-1 (root)
3747   ;; This function find the total score of the thread below ROOT.
3748   (setq root (car root))
3749   (apply gnus-thread-score-function
3750          (or (append
3751               (mapcar 'gnus-thread-total-score
3752                       (cdr (gnus-id-to-thread (mail-header-id root))))
3753               (when (> (mail-header-number root) 0)
3754                 (list (or (cdr (assq (mail-header-number root)
3755                                      gnus-newsgroup-scored))
3756                           gnus-summary-default-score 0))))
3757              (list gnus-summary-default-score)
3758              '(0))))
3759
3760 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3761 (defvar gnus-tmp-prev-subject nil)
3762 (defvar gnus-tmp-false-parent nil)
3763 (defvar gnus-tmp-root-expunged nil)
3764 (defvar gnus-tmp-dummy-line nil)
3765
3766 (defvar gnus-tmp-header)
3767 (defun gnus-extra-header (type &optional header)
3768   "Return the extra header of TYPE."
3769   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3770       ""))
3771
3772 (defun gnus-summary-prepare-threads (threads)
3773   "Prepare summary buffer from THREADS and indentation LEVEL.
3774 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3775 or a straight list of headers."
3776   (gnus-message 7 "Generating summary...")
3777
3778   (setq gnus-newsgroup-threads threads)
3779   (beginning-of-line)
3780
3781   (let ((gnus-tmp-level 0)
3782         (default-score (or gnus-summary-default-score 0))
3783         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3784         thread number subject stack state gnus-tmp-gathered beg-match
3785         new-roots gnus-tmp-new-adopts thread-end
3786         gnus-tmp-header gnus-tmp-unread
3787         gnus-tmp-replied gnus-tmp-subject-or-nil
3788         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3789         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3790         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3791
3792     (setq gnus-tmp-prev-subject nil)
3793
3794     (if (vectorp (car threads))
3795         ;; If this is a straight (sic) list of headers, then a
3796         ;; threaded summary display isn't required, so we just create
3797         ;; an unthreaded one.
3798         (gnus-summary-prepare-unthreaded threads)
3799
3800       ;; Do the threaded display.
3801
3802       (while (or threads stack gnus-tmp-new-adopts new-roots)
3803
3804         (if (and (= gnus-tmp-level 0)
3805                  (or (not stack)
3806                      (= (caar stack) 0))
3807                  (not gnus-tmp-false-parent)
3808                  (or gnus-tmp-new-adopts new-roots))
3809             (if gnus-tmp-new-adopts
3810                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3811                       thread (list (car gnus-tmp-new-adopts))
3812                       gnus-tmp-header (caar thread)
3813                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3814               (when new-roots
3815                 (setq thread (list (car new-roots))
3816                       gnus-tmp-header (caar thread)
3817                       new-roots (cdr new-roots))))
3818
3819           (if threads
3820               ;; If there are some threads, we do them before the
3821               ;; threads on the stack.
3822               (setq thread threads
3823                     gnus-tmp-header (caar thread))
3824             ;; There were no current threads, so we pop something off
3825             ;; the stack.
3826             (setq state (car stack)
3827                   gnus-tmp-level (car state)
3828                   thread (cdr state)
3829                   stack (cdr stack)
3830                   gnus-tmp-header (caar thread))))
3831
3832         (setq gnus-tmp-false-parent nil)
3833         (setq gnus-tmp-root-expunged nil)
3834         (setq thread-end nil)
3835
3836         (if (stringp gnus-tmp-header)
3837             ;; The header is a dummy root.
3838             (cond
3839              ((eq gnus-summary-make-false-root 'adopt)
3840               ;; We let the first article adopt the rest.
3841               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3842                                                (cddar thread)))
3843               (setq gnus-tmp-gathered
3844                     (nconc (mapcar
3845                             (lambda (h) (mail-header-number (car h)))
3846                             (cddar thread))
3847                            gnus-tmp-gathered))
3848               (setq thread (cons (list (caar thread)
3849                                        (cadar thread))
3850                                  (cdr thread)))
3851               (setq gnus-tmp-level -1
3852                     gnus-tmp-false-parent t))
3853              ((eq gnus-summary-make-false-root 'empty)
3854               ;; We print adopted articles with empty subject fields.
3855               (setq gnus-tmp-gathered
3856                     (nconc (mapcar
3857                             (lambda (h) (mail-header-number (car h)))
3858                             (cddar thread))
3859                            gnus-tmp-gathered))
3860               (setq gnus-tmp-level -1))
3861              ((eq gnus-summary-make-false-root 'dummy)
3862               ;; We remember that we probably want to output a dummy
3863               ;; root.
3864               (setq gnus-tmp-dummy-line gnus-tmp-header)
3865               (setq gnus-tmp-prev-subject gnus-tmp-header))
3866              (t
3867               ;; We do not make a root for the gathered
3868               ;; sub-threads at all.
3869               (setq gnus-tmp-level -1)))
3870
3871           (setq number (mail-header-number gnus-tmp-header)
3872                 subject (mail-header-subject gnus-tmp-header))
3873
3874           (cond
3875            ;; If the thread has changed subject, we might want to make
3876            ;; this subthread into a root.
3877            ((and (null gnus-thread-ignore-subject)
3878                  (not (zerop gnus-tmp-level))
3879                  gnus-tmp-prev-subject
3880                  (not (inline
3881                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3882             (setq new-roots (nconc new-roots (list (car thread)))
3883                   thread-end t
3884                   gnus-tmp-header nil))
3885            ;; If the article lies outside the current limit,
3886            ;; then we do not display it.
3887            ((not (memq number gnus-newsgroup-limit))
3888             (setq gnus-tmp-gathered
3889                   (nconc (mapcar
3890                           (lambda (h) (mail-header-number (car h)))
3891                           (cdar thread))
3892                          gnus-tmp-gathered))
3893             (setq gnus-tmp-new-adopts (if (cdar thread)
3894                                           (append gnus-tmp-new-adopts
3895                                                   (cdar thread))
3896                                         gnus-tmp-new-adopts)
3897                   thread-end t
3898                   gnus-tmp-header nil)
3899             (when (zerop gnus-tmp-level)
3900               (setq gnus-tmp-root-expunged t)))
3901            ;; Perhaps this article is to be marked as read?
3902            ((and gnus-summary-mark-below
3903                  (< (or (cdr (assq number gnus-newsgroup-scored))
3904                         default-score)
3905                     gnus-summary-mark-below)
3906                  ;; Don't touch sparse articles.
3907                  (not (gnus-summary-article-sparse-p number))
3908                  (not (gnus-summary-article-ancient-p number)))
3909             (setq gnus-newsgroup-unreads
3910                   (delq number gnus-newsgroup-unreads))
3911             (if gnus-newsgroup-auto-expire
3912                 (push number gnus-newsgroup-expirable)
3913               (push (cons number gnus-low-score-mark)
3914                     gnus-newsgroup-reads))))
3915
3916           (when gnus-tmp-header
3917             ;; We may have an old dummy line to output before this
3918             ;; article.
3919             (when (and gnus-tmp-dummy-line
3920                        (gnus-subject-equal
3921                         gnus-tmp-dummy-line
3922                         (mail-header-subject gnus-tmp-header)))
3923               (gnus-summary-insert-dummy-line
3924                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3925               (setq gnus-tmp-dummy-line nil))
3926
3927             ;; Compute the mark.
3928             (setq gnus-tmp-unread (gnus-article-mark number))
3929
3930             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3931                                   gnus-tmp-header gnus-tmp-level)
3932                   gnus-newsgroup-data)
3933
3934             ;; Actually insert the line.
3935             (setq
3936              gnus-tmp-subject-or-nil
3937              (cond
3938               ((and gnus-thread-ignore-subject
3939                     gnus-tmp-prev-subject
3940                     (not (inline (gnus-subject-equal
3941                                   gnus-tmp-prev-subject subject))))
3942                subject)
3943               ((zerop gnus-tmp-level)
3944                (if (and (eq gnus-summary-make-false-root 'empty)
3945                         (memq number gnus-tmp-gathered)
3946                         gnus-tmp-prev-subject
3947                         (inline (gnus-subject-equal
3948                                  gnus-tmp-prev-subject subject)))
3949                    gnus-summary-same-subject
3950                  subject))
3951               (t gnus-summary-same-subject)))
3952             (if (and (eq gnus-summary-make-false-root 'adopt)
3953                      (= gnus-tmp-level 1)
3954                      (memq number gnus-tmp-gathered))
3955                 (setq gnus-tmp-opening-bracket ?\<
3956                       gnus-tmp-closing-bracket ?\>)
3957               (setq gnus-tmp-opening-bracket ?\[
3958                     gnus-tmp-closing-bracket ?\]))
3959             (setq
3960              gnus-tmp-indentation
3961              (aref gnus-thread-indent-array gnus-tmp-level)
3962              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3963              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3964                                 gnus-summary-default-score 0)
3965              gnus-tmp-score-char
3966              (if (or (null gnus-summary-default-score)
3967                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3968                          gnus-summary-zcore-fuzz))
3969                  ?  ;Whitespace
3970                (if (< gnus-tmp-score gnus-summary-default-score)
3971                    gnus-score-below-mark gnus-score-over-mark))
3972              gnus-tmp-replied
3973              (cond ((memq number gnus-newsgroup-processable)
3974                     gnus-process-mark)
3975                    ((memq number gnus-newsgroup-cached)
3976                     gnus-cached-mark)
3977                    ((memq number gnus-newsgroup-replied)
3978                     gnus-replied-mark)
3979                    ((memq number gnus-newsgroup-saved)
3980                     gnus-saved-mark)
3981                    (t gnus-unread-mark))
3982              gnus-tmp-from (mail-header-from gnus-tmp-header)
3983              gnus-tmp-name
3984              (cond
3985               ((string-match "<[^>]+> *$" gnus-tmp-from)
3986                (setq beg-match (match-beginning 0))
3987                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3988                         (substring gnus-tmp-from (1+ (match-beginning 0))
3989                                    (1- (match-end 0))))
3990                    (substring gnus-tmp-from 0 beg-match)))
3991               ((string-match "(.+)" gnus-tmp-from)
3992                (substring gnus-tmp-from
3993                           (1+ (match-beginning 0)) (1- (match-end 0))))
3994               (t gnus-tmp-from)))
3995             (when (string= gnus-tmp-name "")
3996               (setq gnus-tmp-name gnus-tmp-from))
3997             (unless (numberp gnus-tmp-lines)
3998               (setq gnus-tmp-lines 0))
3999             (gnus-put-text-property
4000              (point)
4001              (progn (eval gnus-summary-line-format-spec) (point))
4002              'gnus-number number)
4003             (when gnus-visual-p
4004               (forward-line -1)
4005               (gnus-run-hooks 'gnus-summary-update-hook)
4006               (forward-line 1))
4007
4008             (setq gnus-tmp-prev-subject subject)))
4009
4010         (when (nth 1 thread)
4011           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4012         (incf gnus-tmp-level)
4013         (setq threads (if thread-end nil (cdar thread)))
4014         (unless threads
4015           (setq gnus-tmp-level 0)))))
4016   (gnus-message 7 "Generating summary...done"))
4017
4018 (defun gnus-summary-prepare-unthreaded (headers)
4019   "Generate an unthreaded summary buffer based on HEADERS."
4020   (let (header number mark)
4021
4022     (beginning-of-line)
4023
4024     (while headers
4025       ;; We may have to root out some bad articles...
4026       (when (memq (setq number (mail-header-number
4027                                 (setq header (pop headers))))
4028                   gnus-newsgroup-limit)
4029         ;; Mark article as read when it has a low score.
4030         (when (and gnus-summary-mark-below
4031                    (< (or (cdr (assq number gnus-newsgroup-scored))
4032                           gnus-summary-default-score 0)
4033                       gnus-summary-mark-below)
4034                    (not (gnus-summary-article-ancient-p number)))
4035           (setq gnus-newsgroup-unreads
4036                 (delq number gnus-newsgroup-unreads))
4037           (if gnus-newsgroup-auto-expire
4038               (push number gnus-newsgroup-expirable)
4039             (push (cons number gnus-low-score-mark)
4040                   gnus-newsgroup-reads)))
4041
4042         (setq mark (gnus-article-mark number))
4043         (push (gnus-data-make number mark (1+ (point)) header 0)
4044               gnus-newsgroup-data)
4045         (gnus-summary-insert-line
4046          header 0 number
4047          mark (memq number gnus-newsgroup-replied)
4048          (memq number gnus-newsgroup-expirable)
4049          (mail-header-subject header) nil
4050          (cdr (assq number gnus-newsgroup-scored))
4051          (memq number gnus-newsgroup-processable))))))
4052
4053 (defun gnus-summary-remove-list-identifiers ()
4054   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4055   (let ((regexp (if (stringp gnus-list-identifiers)
4056                     gnus-list-identifiers
4057                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4058     (dolist (header gnus-newsgroup-headers)
4059       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4060                           (mail-header-subject header))
4061         (mail-header-set-subject
4062          header (concat (substring (mail-header-subject header)
4063                                    0 (match-beginning 2))
4064                         (substring (mail-header-subject header)
4065                                    (match-end 2))))))))
4066
4067 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4068   "Select newsgroup GROUP.
4069 If READ-ALL is non-nil, all articles in the group are selected.
4070 If SELECT-ARTICLES, only select those articles from GROUP."
4071   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4072          ;;!!! Dirty hack; should be removed.
4073          (gnus-summary-ignore-duplicates
4074           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4075               t
4076             gnus-summary-ignore-duplicates))
4077          (info (nth 2 entry))
4078          articles fetched-articles cached)
4079
4080     (unless (gnus-check-server
4081              (setq gnus-current-select-method
4082                    (gnus-find-method-for-group group)))
4083       (error "Couldn't open server"))
4084
4085     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4086         (gnus-activate-group group)     ; Or we can activate it...
4087         (progn                          ; Or we bug out.
4088           (when (equal major-mode 'gnus-summary-mode)
4089             (kill-buffer (current-buffer)))
4090           (error "Couldn't request group %s: %s"
4091                  group (gnus-status-message group))))
4092
4093     (unless (gnus-request-group group t)
4094       (when (equal major-mode 'gnus-summary-mode)
4095         (kill-buffer (current-buffer)))
4096       (error "Couldn't request group %s: %s"
4097              group (gnus-status-message group)))
4098
4099     (setq gnus-newsgroup-name group)
4100     (setq gnus-newsgroup-unselected nil)
4101     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4102     (gnus-summary-setup-default-charset)
4103
4104     ;; Adjust and set lists of article marks.
4105     (when info
4106       (gnus-adjust-marked-articles info))
4107
4108     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4109     (when (gnus-virtual-group-p group)
4110       (setq cached gnus-newsgroup-cached))
4111
4112     (setq gnus-newsgroup-unreads
4113           (gnus-set-difference
4114            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4115            gnus-newsgroup-dormant))
4116
4117     (setq gnus-newsgroup-processable nil)
4118
4119     (gnus-update-read-articles group gnus-newsgroup-unreads)
4120
4121     (if (setq articles select-articles)
4122         (setq gnus-newsgroup-unselected
4123               (gnus-sorted-intersection
4124                gnus-newsgroup-unreads
4125                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4126       (setq articles (gnus-articles-to-read group read-all)))
4127
4128     (cond
4129      ((null articles)
4130       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4131       'quit)
4132      ((eq articles 0) nil)
4133      (t
4134       ;; Init the dependencies hash table.
4135       (setq gnus-newsgroup-dependencies
4136             (gnus-make-hashtable (length articles)))
4137       (gnus-set-global-variables)
4138       ;; Retrieve the headers and read them in.
4139       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4140       (setq gnus-newsgroup-headers
4141             (if (eq 'nov
4142                     (setq gnus-headers-retrieved-by
4143                           (gnus-retrieve-headers
4144                            articles gnus-newsgroup-name
4145                            ;; We might want to fetch old headers, but
4146                            ;; not if there is only 1 article.
4147                            (and (or (and
4148                                      (not (eq gnus-fetch-old-headers 'some))
4149                                      (not (numberp gnus-fetch-old-headers)))
4150                                     (> (length articles) 1))
4151                                 gnus-fetch-old-headers))))
4152                 (gnus-get-newsgroup-headers-xover
4153                  articles nil nil gnus-newsgroup-name t)
4154               (gnus-get-newsgroup-headers)))
4155       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4156
4157       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4158       (when cached
4159         (setq gnus-newsgroup-cached cached))
4160
4161       ;; Suppress duplicates?
4162       (when gnus-suppress-duplicates
4163         (gnus-dup-suppress-articles))
4164
4165       ;; Set the initial limit.
4166       (setq gnus-newsgroup-limit (copy-sequence articles))
4167       ;; Remove canceled articles from the list of unread articles.
4168       (setq gnus-newsgroup-unreads
4169             (gnus-set-sorted-intersection
4170              gnus-newsgroup-unreads
4171              (setq fetched-articles
4172                    (mapcar (lambda (headers) (mail-header-number headers))
4173                            gnus-newsgroup-headers))))
4174       ;; Removed marked articles that do not exist.
4175       (gnus-update-missing-marks
4176        (gnus-sorted-complement fetched-articles articles))
4177       ;; We might want to build some more threads first.
4178       (when (and gnus-fetch-old-headers
4179                  (eq gnus-headers-retrieved-by 'nov))
4180         (if (eq gnus-fetch-old-headers 'invisible)
4181             (gnus-build-all-threads)
4182           (gnus-build-old-threads)))
4183       ;; Let the Gnus agent mark articles as read.
4184       (when gnus-agent
4185         (gnus-agent-get-undownloaded-list))
4186       ;; Remove list identifiers from subject
4187       (when gnus-list-identifiers
4188         (gnus-summary-remove-list-identifiers))
4189       ;; Check whether auto-expire is to be done in this group.
4190       (setq gnus-newsgroup-auto-expire
4191             (gnus-group-auto-expirable-p group))
4192       ;; Set up the article buffer now, if necessary.
4193       (unless gnus-single-article-buffer
4194         (gnus-article-setup-buffer))
4195       ;; First and last article in this newsgroup.
4196       (when gnus-newsgroup-headers
4197         (setq gnus-newsgroup-begin
4198               (mail-header-number (car gnus-newsgroup-headers))
4199               gnus-newsgroup-end
4200               (mail-header-number
4201                (gnus-last-element gnus-newsgroup-headers))))
4202       ;; GROUP is successfully selected.
4203       (or gnus-newsgroup-headers t)))))
4204
4205 (defun gnus-articles-to-read (group &optional read-all)
4206   ;; Find out what articles the user wants to read.
4207   (let* ((articles
4208           ;; Select all articles if `read-all' is non-nil, or if there
4209           ;; are no unread articles.
4210           (if (or read-all
4211                   (and (zerop (length gnus-newsgroup-marked))
4212                        (zerop (length gnus-newsgroup-unreads)))
4213                   (eq (gnus-group-find-parameter group 'display)
4214                       'all))
4215               (or
4216                (gnus-uncompress-range (gnus-active group))
4217                (gnus-cache-articles-in-group group))
4218             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4219                           (copy-sequence gnus-newsgroup-unreads))
4220                   '<)))
4221          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4222          (scored (length scored-list))
4223          (number (length articles))
4224          (marked (+ (length gnus-newsgroup-marked)
4225                     (length gnus-newsgroup-dormant)))
4226          (select
4227           (cond
4228            ((numberp read-all)
4229             read-all)
4230            (t
4231             (condition-case ()
4232                 (cond
4233                  ((and (or (<= scored marked) (= scored number))
4234                        (numberp gnus-large-newsgroup)
4235                        (> number gnus-large-newsgroup))
4236                   (let ((input
4237                          (read-string
4238                           (format
4239                            "How many articles from %s (default %d): "
4240                            (gnus-limit-string gnus-newsgroup-name 35)
4241                            number))))
4242                     (if (string-match "^[ \t]*$" input) number input)))
4243                  ((and (> scored marked) (< scored number)
4244                        (> (- scored number) 20))
4245                   (let ((input
4246                          (read-string
4247                           (format "%s %s (%d scored, %d total): "
4248                                   "How many articles from"
4249                                   group scored number))))
4250                     (if (string-match "^[ \t]*$" input)
4251                         number input)))
4252                  (t number))
4253               (quit nil))))))
4254     (setq select (if (stringp select) (string-to-number select) select))
4255     (if (or (null select) (zerop select))
4256         select
4257       (if (and (not (zerop scored)) (<= (abs select) scored))
4258           (progn
4259             (setq articles (sort scored-list '<))
4260             (setq number (length articles)))
4261         (setq articles (copy-sequence articles)))
4262
4263       (when (< (abs select) number)
4264         (if (< select 0)
4265             ;; Select the N oldest articles.
4266             (setcdr (nthcdr (1- (abs select)) articles) nil)
4267           ;; Select the N most recent articles.
4268           (setq articles (nthcdr (- number select) articles))))
4269       (setq gnus-newsgroup-unselected
4270             (gnus-sorted-intersection
4271              gnus-newsgroup-unreads
4272              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4273       articles)))
4274
4275 (defun gnus-killed-articles (killed articles)
4276   (let (out)
4277     (while articles
4278       (when (inline (gnus-member-of-range (car articles) killed))
4279         (push (car articles) out))
4280       (setq articles (cdr articles)))
4281     out))
4282
4283 (defun gnus-uncompress-marks (marks)
4284   "Uncompress the mark ranges in MARKS."
4285   (let ((uncompressed '(score bookmark))
4286         out)
4287     (while marks
4288       (if (memq (caar marks) uncompressed)
4289           (push (car marks) out)
4290         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4291       (setq marks (cdr marks)))
4292     out))
4293
4294 (defun gnus-adjust-marked-articles (info)
4295   "Set all article lists and remove all marks that are no longer valid."
4296   (let* ((marked-lists (gnus-info-marks info))
4297          (active (gnus-active (gnus-info-group info)))
4298          (min (car active))
4299          (max (cdr active))
4300          (types gnus-article-mark-lists)
4301          (uncompressed '(score bookmark killed))
4302          marks var articles article mark)
4303
4304     (while marked-lists
4305       (setq marks (pop marked-lists))
4306       (set (setq var (intern (format "gnus-newsgroup-%s"
4307                                      (car (rassq (setq mark (car marks))
4308                                                  types)))))
4309            (if (memq (car marks) uncompressed) (cdr marks)
4310              (gnus-uncompress-range (cdr marks))))
4311
4312       (setq articles (symbol-value var))
4313
4314       ;; All articles have to be subsets of the active articles.
4315       (cond
4316        ;; Adjust "simple" lists.
4317        ((memq mark '(tick dormant expire reply save))
4318         (while articles
4319           (when (or (< (setq article (pop articles)) min) (> article max))
4320             (set var (delq article (symbol-value var))))))
4321        ;; Adjust assocs.
4322        ((memq mark uncompressed)
4323         (when (not (listp (cdr (symbol-value var))))
4324           (set var (list (symbol-value var))))
4325         (when (not (listp (cdr articles)))
4326           (setq articles (list articles)))
4327         (while articles
4328           (when (or (not (consp (setq article (pop articles))))
4329                     (< (car article) min)
4330                     (> (car article) max))
4331             (set var (delq article (symbol-value var))))))))))
4332
4333 (defun gnus-update-missing-marks (missing)
4334   "Go through the list of MISSING articles and remove them from the mark lists."
4335   (when missing
4336     (let ((types gnus-article-mark-lists)
4337           var m)
4338       ;; Go through all types.
4339       (while types
4340         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4341         (when (symbol-value var)
4342           ;; This list has articles.  So we delete all missing articles
4343           ;; from it.
4344           (setq m missing)
4345           (while m
4346             (set var (delq (pop m) (symbol-value var)))))))))
4347
4348 (defun gnus-update-marks ()
4349   "Enter the various lists of marked articles into the newsgroup info list."
4350   (let ((types gnus-article-mark-lists)
4351         (info (gnus-get-info gnus-newsgroup-name))
4352         (uncompressed '(score bookmark killed))
4353         type list newmarked symbol delta-marks)
4354     (when info
4355       ;; Add all marks lists to the list of marks lists.
4356       (while (setq type (pop types))
4357         (setq list (symbol-value
4358                     (setq symbol
4359                           (intern (format "gnus-newsgroup-%s"
4360                                           (car type))))))
4361
4362         (when list
4363           ;; Get rid of the entries of the articles that have the
4364           ;; default score.
4365           (when (and (eq (cdr type) 'score)
4366                      gnus-save-score
4367                      list)
4368             (let* ((arts list)
4369                    (prev (cons nil list))
4370                    (all prev))
4371               (while arts
4372                 (if (or (not (consp (car arts)))
4373                         (= (cdar arts) gnus-summary-default-score))
4374                     (setcdr prev (cdr arts))
4375                   (setq prev arts))
4376                 (setq arts (cdr arts)))
4377               (setq list (cdr all)))))
4378
4379         (unless (memq (cdr type) uncompressed)
4380           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4381        
4382         (when (gnus-check-backend-function
4383                'request-set-mark gnus-newsgroup-name)
4384           ;; uncompressed:s are not proper flags (they are cons cells)
4385           ;; cache is a internal gnus flag
4386           (unless (memq (cdr type) (cons 'cache uncompressed))
4387             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4388                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4389                    (add (gnus-remove-from-range
4390                          (gnus-copy-sequence list) old)))
4391               (when add
4392                 (push (list add 'add (list (cdr type))) delta-marks))
4393               (when del
4394                 (push (list del 'del (list (cdr type))) delta-marks)))))
4395           
4396         (when list
4397           (push (cons (cdr type) list) newmarked)))
4398
4399       (when delta-marks
4400         (unless (gnus-check-group gnus-newsgroup-name)
4401           (error "Can't open server for %s" gnus-newsgroup-name))
4402         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4403           
4404       ;; Enter these new marks into the info of the group.
4405       (if (nthcdr 3 info)
4406           (setcar (nthcdr 3 info) newmarked)
4407         ;; Add the marks lists to the end of the info.
4408         (when newmarked
4409           (setcdr (nthcdr 2 info) (list newmarked))))
4410
4411       ;; Cut off the end of the info if there's nothing else there.
4412       (let ((i 5))
4413         (while (and (> i 2)
4414                     (not (nth i info)))
4415           (when (nthcdr (decf i) info)
4416             (setcdr (nthcdr i info) nil)))))))
4417
4418 (defun gnus-set-mode-line (where)
4419   "This function sets the mode line of the article or summary buffers.
4420 If WHERE is `summary', the summary mode line format will be used."
4421   ;; Is this mode line one we keep updated?
4422   (when (and (memq where gnus-updated-mode-lines)
4423              (symbol-value
4424               (intern (format "gnus-%s-mode-line-format-spec" where))))
4425     (let (mode-string)
4426       (save-excursion
4427         ;; We evaluate this in the summary buffer since these
4428         ;; variables are buffer-local to that buffer.
4429         (set-buffer gnus-summary-buffer)
4430         ;; We bind all these variables that are used in the `eval' form
4431         ;; below.
4432         (let* ((mformat (symbol-value
4433                          (intern
4434                           (format "gnus-%s-mode-line-format-spec" where))))
4435                (gnus-tmp-group-name gnus-newsgroup-name)
4436                (gnus-tmp-article-number (or gnus-current-article 0))
4437                (gnus-tmp-unread gnus-newsgroup-unreads)
4438                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4439                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4440                (gnus-tmp-unread-and-unselected
4441                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4442                             (zerop gnus-tmp-unselected))
4443                        "")
4444                       ((zerop gnus-tmp-unselected)
4445                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4446                       (t (format "{%d(+%d) more}"
4447                                  gnus-tmp-unread-and-unticked
4448                                  gnus-tmp-unselected))))
4449                (gnus-tmp-subject
4450                 (if (and gnus-current-headers
4451                          (vectorp gnus-current-headers))
4452                     (gnus-mode-string-quote
4453                      (mail-header-subject gnus-current-headers))
4454                   ""))
4455                bufname-length max-len
4456                gnus-tmp-header);; passed as argument to any user-format-funcs
4457           (setq mode-string (eval mformat))
4458           (setq bufname-length (if (string-match "%b" mode-string)
4459                                    (- (length
4460                                        (buffer-name
4461                                         (if (eq where 'summary)
4462                                             nil
4463                                           (get-buffer gnus-article-buffer))))
4464                                       2)
4465                                  0))
4466           (setq max-len (max 4 (if gnus-mode-non-string-length
4467                                    (- (window-width)
4468                                       gnus-mode-non-string-length
4469                                       bufname-length)
4470                                  (length mode-string))))
4471           ;; We might have to chop a bit of the string off...
4472           (when (> (length mode-string) max-len)
4473             (setq mode-string
4474                   (concat (truncate-string-to-width mode-string (- max-len 3))
4475                           "...")))
4476           ;; Pad the mode string a bit.
4477           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4478       ;; Update the mode line.
4479       (setq mode-line-buffer-identification
4480             (gnus-mode-line-buffer-identification (list mode-string)))
4481       (set-buffer-modified-p t))))
4482
4483 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4484   "Go through the HEADERS list and add all Xrefs to a hash table.
4485 The resulting hash table is returned, or nil if no Xrefs were found."
4486   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4487          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4488          (xref-hashtb (gnus-make-hashtable))
4489          start group entry number xrefs header)
4490     (while headers
4491       (setq header (pop headers))
4492       (when (and (setq xrefs (mail-header-xref header))
4493                  (not (memq (setq number (mail-header-number header))
4494                             unreads)))
4495         (setq start 0)
4496         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4497           (setq start (match-end 0))
4498           (setq group (if prefix
4499                           (concat prefix (substring xrefs (match-beginning 1)
4500                                                     (match-end 1)))
4501                         (substring xrefs (match-beginning 1) (match-end 1))))
4502           (setq number
4503                 (string-to-int (substring xrefs (match-beginning 2)
4504                                           (match-end 2))))
4505           (if (setq entry (gnus-gethash group xref-hashtb))
4506               (setcdr entry (cons number (cdr entry)))
4507             (gnus-sethash group (cons number nil) xref-hashtb)))))
4508     (and start xref-hashtb)))
4509
4510 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4511   "Look through all the headers and mark the Xrefs as read."
4512   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4513         name entry info xref-hashtb idlist method nth4)
4514     (save-excursion
4515       (set-buffer gnus-group-buffer)
4516       (when (setq xref-hashtb
4517                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4518         (mapatoms
4519          (lambda (group)
4520            (unless (string= from-newsgroup (setq name (symbol-name group)))
4521              (setq idlist (symbol-value group))
4522              ;; Dead groups are not updated.
4523              (and (prog1
4524                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4525                             info (nth 2 entry))
4526                     (when (stringp (setq nth4 (gnus-info-method info)))
4527                       (setq nth4 (gnus-server-to-method nth4))))
4528                   ;; Only do the xrefs if the group has the same
4529                   ;; select method as the group we have just read.
4530                   (or (gnus-methods-equal-p
4531                        nth4 (gnus-find-method-for-group from-newsgroup))
4532                       virtual
4533                       (equal nth4 (setq method (gnus-find-method-for-group
4534                                                 from-newsgroup)))
4535                       (and (equal (car nth4) (car method))
4536                            (equal (nth 1 nth4) (nth 1 method))))
4537                   gnus-use-cross-reference
4538                   (or (not (eq gnus-use-cross-reference t))
4539                       virtual
4540                       ;; Only do cross-references on subscribed
4541                       ;; groups, if that is what is wanted.
4542                       (<= (gnus-info-level info) gnus-level-subscribed))
4543                   (gnus-group-make-articles-read name idlist))))
4544          xref-hashtb)))))
4545
4546 (defun gnus-compute-read-articles (group articles)
4547   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4548          (info (nth 2 entry))
4549          (active (gnus-active group))
4550          ninfo)
4551     (when entry
4552       ;; First peel off all invalid article numbers.
4553       (when active
4554         (let ((ids articles)
4555               id first)
4556           (while (setq id (pop ids))
4557             (when (and first (> id (cdr active)))
4558               ;; We'll end up in this situation in one particular
4559               ;; obscure situation.  If you re-scan a group and get
4560               ;; a new article that is cross-posted to a different
4561               ;; group that has not been re-scanned, you might get
4562               ;; crossposted article that has a higher number than
4563               ;; Gnus believes possible.  So we re-activate this
4564               ;; group as well.  This might mean doing the
4565               ;; crossposting thingy will *increase* the number
4566               ;; of articles in some groups.  Tsk, tsk.
4567               (setq active (or (gnus-activate-group group) active)))
4568             (when (or (> id (cdr active))
4569                       (< id (car active)))
4570               (setq articles (delq id articles))))))
4571       ;; If the read list is nil, we init it.
4572       (if (and active
4573                (null (gnus-info-read info))
4574                (> (car active) 1))
4575           (setq ninfo (cons 1 (1- (car active))))
4576         (setq ninfo (gnus-info-read info)))
4577       ;; Then we add the read articles to the range.
4578       (gnus-add-to-range
4579        ninfo (setq articles (sort articles '<))))))
4580
4581 (defun gnus-group-make-articles-read (group articles)
4582   "Update the info of GROUP to say that ARTICLES are read."
4583   (let* ((num 0)
4584          (entry (gnus-gethash group gnus-newsrc-hashtb))
4585          (info (nth 2 entry))
4586          (active (gnus-active group))
4587          range)
4588     (when entry
4589       (setq range (gnus-compute-read-articles group articles))
4590       (save-excursion
4591         (set-buffer gnus-group-buffer)
4592         (gnus-undo-register
4593           `(progn
4594              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4595              (gnus-info-set-read ',info ',(gnus-info-read info))
4596              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4597              (gnus-group-update-group ,group t))))
4598       ;; Add the read articles to the range.
4599       (gnus-info-set-read info range)
4600       ;; Then we have to re-compute how many unread
4601       ;; articles there are in this group.
4602       (when active
4603         (cond
4604          ((not range)
4605           (setq num (- (1+ (cdr active)) (car active))))
4606          ((not (listp (cdr range)))
4607           (setq num (- (cdr active) (- (1+ (cdr range))
4608                                        (car range)))))
4609          (t
4610           (while range
4611             (if (numberp (car range))
4612                 (setq num (1+ num))
4613               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4614             (setq range (cdr range)))
4615           (setq num (- (cdr active) num))))
4616         ;; Update the number of unread articles.
4617         (setcar entry num)
4618         ;; Update the group buffer.
4619         (gnus-group-update-group group t)))))
4620
4621 (defvar gnus-newsgroup-none-id 0)
4622
4623 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4624   (let ((cur nntp-server-buffer)
4625         (dependencies
4626          (or dependencies
4627              (save-excursion (set-buffer gnus-summary-buffer)
4628                              gnus-newsgroup-dependencies)))
4629         headers id end ref
4630         (mail-parse-charset gnus-newsgroup-charset)
4631         (mail-parse-ignored-charsets 
4632          (save-excursion (condition-case nil
4633                              (set-buffer gnus-summary-buffer)
4634                            (error))
4635                          gnus-newsgroup-ignored-charsets)))
4636     (save-excursion
4637       (set-buffer nntp-server-buffer)
4638       ;; Translate all TAB characters into SPACE characters.
4639       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4640       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4641       (gnus-run-hooks 'gnus-parse-headers-hook)
4642       (let ((case-fold-search t)
4643             in-reply-to header p lines chars)
4644         (goto-char (point-min))
4645         ;; Search to the beginning of the next header.  Error messages
4646         ;; do not begin with 2 or 3.
4647         (while (re-search-forward "^[23][0-9]+ " nil t)
4648           (setq id nil
4649                 ref nil)
4650           ;; This implementation of this function, with nine
4651           ;; search-forwards instead of the one re-search-forward and
4652           ;; a case (which basically was the old function) is actually
4653           ;; about twice as fast, even though it looks messier.  You
4654           ;; can't have everything, I guess.  Speed and elegance
4655           ;; doesn't always go hand in hand.
4656           (setq
4657            header
4658            (vector
4659             ;; Number.
4660             (prog1
4661                 (read cur)
4662               (end-of-line)
4663               (setq p (point))
4664               (narrow-to-region (point)
4665                                 (or (and (search-forward "\n.\n" nil t)
4666                                          (- (point) 2))
4667                                     (point))))
4668             ;; Subject.
4669             (progn
4670               (goto-char p)
4671               (if (search-forward "\nsubject: " nil t)
4672                   (funcall gnus-decode-encoded-word-function
4673                            (nnheader-header-value))
4674                 "(none)"))
4675             ;; From.
4676             (progn
4677               (goto-char p)
4678               (if (search-forward "\nfrom: " nil t)
4679                   (funcall gnus-decode-encoded-word-function
4680                            (nnheader-header-value))
4681                 "(nobody)"))
4682             ;; Date.
4683             (progn
4684               (goto-char p)
4685               (if (search-forward "\ndate: " nil t)
4686                   (nnheader-header-value) ""))
4687             ;; Message-ID.
4688             (progn
4689               (goto-char p)
4690               (setq id (if (re-search-forward
4691                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4692                            ;; We do it this way to make sure the Message-ID
4693                            ;; is (somewhat) syntactically valid.
4694                            (buffer-substring (match-beginning 1)
4695                                              (match-end 1))
4696                          ;; If there was no message-id, we just fake one
4697                          ;; to make subsequent routines simpler.
4698                          (nnheader-generate-fake-message-id))))
4699             ;; References.
4700             (progn
4701               (goto-char p)
4702               (if (search-forward "\nreferences: " nil t)
4703                   (progn
4704                     (setq end (point))
4705                     (prog1
4706                         (nnheader-header-value)
4707                       (setq ref
4708                             (buffer-substring
4709                              (progn
4710                                (end-of-line)
4711                                (search-backward ">" end t)
4712                                (1+ (point)))
4713                              (progn
4714                                (search-backward "<" end t)
4715                                (point))))))
4716                 ;; Get the references from the in-reply-to header if there
4717                 ;; were no references and the in-reply-to header looks
4718                 ;; promising.
4719                 (if (and (search-forward "\nin-reply-to: " nil t)
4720                          (setq in-reply-to (nnheader-header-value))
4721                          (string-match "<[^>]+>" in-reply-to))
4722                     (let (ref2)
4723                       (setq ref (substring in-reply-to (match-beginning 0)
4724                                            (match-end 0)))
4725                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4726                         (setq ref2 (substring in-reply-to (match-beginning 0)
4727                                               (match-end 0)))
4728                         (when (> (length ref2) (length ref))
4729                           (setq ref ref2)))
4730                       ref)
4731                   (setq ref nil))))
4732             ;; Chars.
4733             (progn
4734               (goto-char p)
4735               (if (search-forward "\nchars: " nil t)
4736                   (if (numberp (setq chars (ignore-errors (read cur))))
4737                       chars 0)
4738                 0))
4739             ;; Lines.
4740             (progn
4741               (goto-char p)
4742               (if (search-forward "\nlines: " nil t)
4743                   (if (numberp (setq lines (ignore-errors (read cur))))
4744                       lines 0)
4745                 0))
4746             ;; Xref.
4747             (progn
4748               (goto-char p)
4749               (and (search-forward "\nxref: " nil t)
4750                    (nnheader-header-value)))
4751             ;; Extra.
4752             (when gnus-extra-headers
4753               (let ((extra gnus-extra-headers)
4754                     out)
4755                 (while extra
4756                   (goto-char p)
4757                   (when (search-forward
4758                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4759                     (push (cons (car extra) (nnheader-header-value))
4760                           out))
4761                   (pop extra))
4762                 out))))
4763           (when (equal id ref)
4764             (setq ref nil))
4765
4766           (when gnus-alter-header-function
4767             (funcall gnus-alter-header-function header)
4768             (setq id (mail-header-id header)
4769                   ref (gnus-parent-id (mail-header-references header))))
4770
4771           (when (setq header
4772                       (gnus-dependencies-add-header
4773                        header dependencies force-new))
4774             (push header headers))
4775           (goto-char (point-max))
4776           (widen))
4777         (nreverse headers)))))
4778
4779 ;; Goes through the xover lines and returns a list of vectors
4780 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4781                                                   force-new dependencies
4782                                                   group also-fetch-heads)
4783   "Parse the news overview data in the server buffer, and return a
4784 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4785   ;; Get the Xref when the users reads the articles since most/some
4786   ;; NNTP servers do not include Xrefs when using XOVER.
4787   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4788   (let ((mail-parse-charset gnus-newsgroup-charset)
4789         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4790         (cur nntp-server-buffer)
4791         (dependencies (or dependencies gnus-newsgroup-dependencies))
4792         number headers header)
4793     (save-excursion
4794       (set-buffer nntp-server-buffer)
4795       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4796       ;; Allow the user to mangle the headers before parsing them.
4797       (gnus-run-hooks 'gnus-parse-headers-hook)
4798       (goto-char (point-min))
4799       (while (not (eobp))
4800         (condition-case ()
4801             (while (and sequence (not (eobp)))
4802               (setq number (read cur))
4803               (while (and sequence
4804                           (< (car sequence) number))
4805                 (setq sequence (cdr sequence)))
4806               (and sequence
4807                    (eq number (car sequence))
4808                    (progn
4809                      (setq sequence (cdr sequence))
4810                      (setq header (inline
4811                                     (gnus-nov-parse-line
4812                                      number dependencies force-new))))
4813                    (push header headers))
4814               (forward-line 1))
4815           (error
4816            (gnus-error 4 "Strange nov line (%d)"
4817                        (count-lines (point-min) (point)))))
4818         (forward-line 1))
4819       ;; A common bug in inn is that if you have posted an article and
4820       ;; then retrieves the active file, it will answer correctly --
4821       ;; the new article is included.  However, a NOV entry for the
4822       ;; article may not have been generated yet, so this may fail.
4823       ;; We work around this problem by retrieving the last few
4824       ;; headers using HEAD.
4825       (if (or (not also-fetch-heads)
4826               (not sequence))
4827           ;; We (probably) got all the headers.
4828           (nreverse headers)
4829         (let ((gnus-nov-is-evil t))
4830           (nconc
4831            (nreverse headers)
4832            (when (gnus-retrieve-headers sequence group)
4833              (gnus-get-newsgroup-headers))))))))
4834
4835 (defun gnus-article-get-xrefs ()
4836   "Fill in the Xref value in `gnus-current-headers', if necessary.
4837 This is meant to be called in `gnus-article-internal-prepare-hook'."
4838   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4839                                  gnus-current-headers)))
4840     (or (not gnus-use-cross-reference)
4841         (not headers)
4842         (and (mail-header-xref headers)
4843              (not (string= (mail-header-xref headers) "")))
4844         (let ((case-fold-search t)
4845               xref)
4846           (save-restriction
4847             (nnheader-narrow-to-headers)
4848             (goto-char (point-min))
4849             (when (or (and (not (eobp))
4850                            (eq (downcase (char-after)) ?x)
4851                            (looking-at "Xref:"))
4852                       (search-forward "\nXref:" nil t))
4853               (goto-char (1+ (match-end 0)))
4854               (setq xref (buffer-substring (point)
4855                                            (progn (end-of-line) (point))))
4856               (mail-header-set-xref headers xref)))))))
4857
4858 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4859   "Find article ID and insert the summary line for that article.
4860 OLD-HEADER can either be a header or a line number to insert
4861 the subject line on."
4862   (let* ((line (and (numberp old-header) old-header))
4863          (old-header (and (vectorp old-header) old-header))
4864          (header (cond ((and old-header use-old-header)
4865                         old-header)
4866                        ((and (numberp id)
4867                              (gnus-number-to-header id))
4868                         (gnus-number-to-header id))
4869                        (t
4870                         (gnus-read-header id))))
4871          (number (and (numberp id) id))
4872          d)
4873     (when header
4874       ;; Rebuild the thread that this article is part of and go to the
4875       ;; article we have fetched.
4876       (when (and (not gnus-show-threads)
4877                  old-header)
4878         (when (and number
4879                    (setq d (gnus-data-find (mail-header-number old-header))))
4880           (goto-char (gnus-data-pos d))
4881           (gnus-data-remove
4882            number
4883            (- (gnus-point-at-bol)
4884               (prog1
4885                   (1+ (gnus-point-at-eol))
4886                 (gnus-delete-line))))))
4887       (when old-header
4888         (mail-header-set-number header (mail-header-number old-header)))
4889       (setq gnus-newsgroup-sparse
4890             (delq (setq number (mail-header-number header))
4891                   gnus-newsgroup-sparse))
4892       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4893       (push number gnus-newsgroup-limit)
4894       (gnus-rebuild-thread (mail-header-id header) line)
4895       (gnus-summary-goto-subject number nil t))
4896     (when (and (numberp number)
4897                (> number 0))
4898       ;; We have to update the boundaries even if we can't fetch the
4899       ;; article if ID is a number -- so that the next `P' or `N'
4900       ;; command will fetch the previous (or next) article even
4901       ;; if the one we tried to fetch this time has been canceled.
4902       (when (> number gnus-newsgroup-end)
4903         (setq gnus-newsgroup-end number))
4904       (when (< number gnus-newsgroup-begin)
4905         (setq gnus-newsgroup-begin number))
4906       (setq gnus-newsgroup-unselected
4907             (delq number gnus-newsgroup-unselected)))
4908     ;; Report back a success?
4909     (and header (mail-header-number header))))
4910
4911 ;;; Process/prefix in the summary buffer
4912
4913 (defun gnus-summary-work-articles (n)
4914   "Return a list of articles to be worked upon.
4915 The prefix argument, the list of process marked articles, and the
4916 current article will be taken into consideration."
4917   (save-excursion
4918     (set-buffer gnus-summary-buffer)
4919     (cond
4920      (n
4921       ;; A numerical prefix has been given.
4922       (setq n (prefix-numeric-value n))
4923       (let ((backward (< n 0))
4924             (n (abs (prefix-numeric-value n)))
4925             articles article)
4926         (save-excursion
4927           (while
4928               (and (> n 0)
4929                    (push (setq article (gnus-summary-article-number))
4930                          articles)
4931                    (if backward
4932                        (gnus-summary-find-prev nil article)
4933                      (gnus-summary-find-next nil article)))
4934             (decf n)))
4935         (nreverse articles)))
4936      ((and (gnus-region-active-p) (mark))
4937       (message "region active")
4938       ;; Work on the region between point and mark.
4939       (let ((max (max (point) (mark)))
4940             articles article)
4941         (save-excursion
4942           (goto-char (min (min (point) (mark))))
4943           (while
4944               (and
4945                (push (setq article (gnus-summary-article-number)) articles)
4946                (gnus-summary-find-next nil article)
4947                (< (point) max)))
4948           (nreverse articles))))
4949      (gnus-newsgroup-processable
4950       ;; There are process-marked articles present.
4951       ;; Save current state.
4952       (gnus-summary-save-process-mark)
4953       ;; Return the list.
4954       (reverse gnus-newsgroup-processable))
4955      (t
4956       ;; Just return the current article.
4957       (list (gnus-summary-article-number))))))
4958
4959 (defmacro gnus-summary-iterate (arg &rest forms)
4960   "Iterate over the process/prefixed articles and do FORMS.
4961 ARG is the interactive prefix given to the command.  FORMS will be
4962 executed with point over the summary line of the articles."
4963   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4964     `(let ((,articles (gnus-summary-work-articles ,arg)))
4965        (while ,articles
4966          (gnus-summary-goto-subject (car ,articles))
4967          ,@forms
4968          (pop ,articles)))))
4969
4970 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4971 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4972
4973 (defun gnus-summary-save-process-mark ()
4974   "Push the current set of process marked articles on the stack."
4975   (interactive)
4976   (push (copy-sequence gnus-newsgroup-processable)
4977         gnus-newsgroup-process-stack))
4978
4979 (defun gnus-summary-kill-process-mark ()
4980   "Push the current set of process marked articles on the stack and unmark."
4981   (interactive)
4982   (gnus-summary-save-process-mark)
4983   (gnus-summary-unmark-all-processable))
4984
4985 (defun gnus-summary-yank-process-mark ()
4986   "Pop the last process mark state off the stack and restore it."
4987   (interactive)
4988   (unless gnus-newsgroup-process-stack
4989     (error "Empty mark stack"))
4990   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4991
4992 (defun gnus-summary-process-mark-set (set)
4993   "Make SET into the current process marked articles."
4994   (gnus-summary-unmark-all-processable)
4995   (while set
4996     (gnus-summary-set-process-mark (pop set))))
4997
4998 ;;; Searching and stuff
4999
5000 (defun gnus-summary-search-group (&optional backward use-level)
5001   "Search for next unread newsgroup.
5002 If optional argument BACKWARD is non-nil, search backward instead."
5003   (save-excursion
5004     (set-buffer gnus-group-buffer)
5005     (when (gnus-group-search-forward
5006            backward nil (if use-level (gnus-group-group-level) nil))
5007       (gnus-group-group-name))))
5008
5009 (defun gnus-summary-best-group (&optional exclude-group)
5010   "Find the name of the best unread group.
5011 If EXCLUDE-GROUP, do not go to this group."
5012   (save-excursion
5013     (set-buffer gnus-group-buffer)
5014     (save-excursion
5015       (gnus-group-best-unread-group exclude-group))))
5016
5017 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5018   (if backward (gnus-summary-find-prev)
5019     (let* ((dummy (gnus-summary-article-intangible-p))
5020            (article (or article (gnus-summary-article-number)))
5021            (arts (gnus-data-find-list article))
5022            result)
5023       (when (and (not dummy)
5024                  (or (not gnus-summary-check-current)
5025                      (not unread)
5026                      (not (gnus-data-unread-p (car arts)))))
5027         (setq arts (cdr arts)))
5028       (when (setq result
5029                   (if unread
5030                       (progn
5031                         (while arts
5032                           (when (or (and undownloaded
5033                                          (eq gnus-undownloaded-mark
5034                                              (gnus-data-mark (car arts))))
5035                                     (gnus-data-unread-p (car arts)))
5036                             (setq result (car arts)
5037                                   arts nil))
5038                           (setq arts (cdr arts)))
5039                         result)
5040                     (car arts)))
5041         (goto-char (gnus-data-pos result))
5042         (gnus-data-number result)))))
5043
5044 (defun gnus-summary-find-prev (&optional unread article)
5045   (let* ((eobp (eobp))
5046          (article (or article (gnus-summary-article-number)))
5047          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5048          result)
5049     (when (and (not eobp)
5050                (or (not gnus-summary-check-current)
5051                    (not unread)
5052                    (not (gnus-data-unread-p (car arts)))))
5053       (setq arts (cdr arts)))
5054     (when (setq result
5055                 (if unread
5056                     (progn
5057                       (while arts
5058                         (when (gnus-data-unread-p (car arts))
5059                           (setq result (car arts)
5060                                 arts nil))
5061                         (setq arts (cdr arts)))
5062                       result)
5063                   (car arts)))
5064       (goto-char (gnus-data-pos result))
5065       (gnus-data-number result))))
5066
5067 (defun gnus-summary-find-subject (subject &optional unread backward article)
5068   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5069          (article (or article (gnus-summary-article-number)))
5070          (articles (gnus-data-list backward))
5071          (arts (gnus-data-find-list article articles))
5072          result)
5073     (when (or (not gnus-summary-check-current)
5074               (not unread)
5075               (not (gnus-data-unread-p (car arts))))
5076       (setq arts (cdr arts)))
5077     (while arts
5078       (and (or (not unread)
5079                (gnus-data-unread-p (car arts)))
5080            (vectorp (gnus-data-header (car arts)))
5081            (gnus-subject-equal
5082             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5083            (setq result (car arts)
5084                  arts nil))
5085       (setq arts (cdr arts)))
5086     (and result
5087          (goto-char (gnus-data-pos result))
5088          (gnus-data-number result))))
5089
5090 (defun gnus-summary-search-forward (&optional unread subject backward)
5091   "Search forward for an article.
5092 If UNREAD, look for unread articles.  If SUBJECT, look for
5093 articles with that subject.  If BACKWARD, search backward instead."
5094   (cond (subject (gnus-summary-find-subject subject unread backward))
5095         (backward (gnus-summary-find-prev unread))
5096         (t (gnus-summary-find-next unread))))
5097
5098 (defun gnus-recenter (&optional n)
5099   "Center point in window and redisplay frame.
5100 Also do horizontal recentering."
5101   (interactive "P")
5102   (when (and gnus-auto-center-summary
5103              (not (eq gnus-auto-center-summary 'vertical)))
5104     (gnus-horizontal-recenter))
5105   (recenter n))
5106
5107 (defun gnus-summary-recenter ()
5108   "Center point in the summary window.
5109 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5110 displayed, no centering will be performed."
5111   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5112   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5113   (let* ((top (cond ((< (window-height) 4) 0)
5114                     ((< (window-height) 7) 1)
5115                     (t (if (numberp gnus-auto-center-summary)
5116                            gnus-auto-center-summary
5117                          2))))
5118          (height (1- (window-height)))
5119          (bottom (save-excursion (goto-char (point-max))
5120                                  (forward-line (- height))
5121                                  (point)))
5122          (window (get-buffer-window (current-buffer))))
5123     ;; The user has to want it.
5124     (when gnus-auto-center-summary
5125       (when (get-buffer-window gnus-article-buffer)
5126         ;; Only do recentering when the article buffer is displayed,
5127         ;; Set the window start to either `bottom', which is the biggest
5128         ;; possible valid number, or the second line from the top,
5129         ;; whichever is the least.
5130         (set-window-start
5131          window (min bottom (save-excursion
5132                               (forward-line (- top)) (point)))
5133          t))
5134       ;; Do horizontal recentering while we're at it.
5135       (when (and (get-buffer-window (current-buffer) t)
5136                  (not (eq gnus-auto-center-summary 'vertical)))
5137         (let ((selected (selected-window)))
5138           (select-window (get-buffer-window (current-buffer) t))
5139           (gnus-summary-position-point)
5140           (gnus-horizontal-recenter)
5141           (select-window selected))))))
5142
5143 (defun gnus-summary-jump-to-group (newsgroup)
5144   "Move point to NEWSGROUP in group mode buffer."
5145   ;; Keep update point of group mode buffer if visible.
5146   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5147       (save-window-excursion
5148         ;; Take care of tree window mode.
5149         (when (get-buffer-window gnus-group-buffer)
5150           (pop-to-buffer gnus-group-buffer))
5151         (gnus-group-jump-to-group newsgroup))
5152     (save-excursion
5153       ;; Take care of tree window mode.
5154       (if (get-buffer-window gnus-group-buffer)
5155           (pop-to-buffer gnus-group-buffer)
5156         (set-buffer gnus-group-buffer))
5157       (gnus-group-jump-to-group newsgroup))))
5158
5159 ;; This function returns a list of article numbers based on the
5160 ;; difference between the ranges of read articles in this group and
5161 ;; the range of active articles.
5162 (defun gnus-list-of-unread-articles (group)
5163   (let* ((read (gnus-info-read (gnus-get-info group)))
5164          (active (or (gnus-active group) (gnus-activate-group group)))
5165          (last (cdr active))
5166          first nlast unread)
5167     ;; If none are read, then all are unread.
5168     (if (not read)
5169         (setq first (car active))
5170       ;; If the range of read articles is a single range, then the
5171       ;; first unread article is the article after the last read
5172       ;; article.  Sounds logical, doesn't it?
5173       (if (and (not (listp (cdr read)))
5174                (or (< (car read) (car active))
5175                    (progn (setq read (list read))
5176                           nil)))
5177           (setq first (max (car active) (1+ (cdr read))))
5178         ;; `read' is a list of ranges.
5179         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5180                                   (caar read)))
5181                   1)
5182           (setq first (car active)))
5183         (while read
5184           (when first
5185             (while (< first nlast)
5186               (push first unread)
5187               (setq first (1+ first))))
5188           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5189           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5190           (setq read (cdr read)))))
5191     ;; And add the last unread articles.
5192     (while (<= first last)
5193       (push first unread)
5194       (setq first (1+ first)))
5195     ;; Return the list of unread articles.
5196     (delq 0 (nreverse unread))))
5197
5198 (defun gnus-list-of-read-articles (group)
5199   "Return a list of unread, unticked and non-dormant articles."
5200   (let* ((info (gnus-get-info group))
5201          (marked (gnus-info-marks info))
5202          (active (gnus-active group)))
5203     (and info active
5204          (gnus-set-difference
5205           (gnus-sorted-complement
5206            (gnus-uncompress-range active)
5207            (gnus-list-of-unread-articles group))
5208           (append
5209            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5210            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5211
5212 ;; Various summary commands
5213
5214 (defun gnus-summary-select-article-buffer ()
5215   "Reconfigure windows to show article buffer."
5216   (interactive)
5217   (if (not (gnus-buffer-live-p gnus-article-buffer))
5218       (error "There is no article buffer for this summary buffer")
5219     (gnus-configure-windows 'article)
5220     (select-window (get-buffer-window gnus-article-buffer))))
5221
5222 (defun gnus-summary-universal-argument (arg)
5223   "Perform any operation on all articles that are process/prefixed."
5224   (interactive "P")
5225   (let ((articles (gnus-summary-work-articles arg))
5226         func article)
5227     (if (eq
5228          (setq
5229           func
5230           (key-binding
5231            (read-key-sequence
5232             (substitute-command-keys
5233              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5234          'undefined)
5235         (gnus-error 1 "Undefined key")
5236       (save-excursion
5237         (while articles
5238           (gnus-summary-goto-subject (setq article (pop articles)))
5239           (let (gnus-newsgroup-processable)
5240             (command-execute func))
5241           (gnus-summary-remove-process-mark article)))))
5242   (gnus-summary-position-point))
5243
5244 (defun gnus-summary-toggle-truncation (&optional arg)
5245   "Toggle truncation of summary lines.
5246 With arg, turn line truncation on iff arg is positive."
5247   (interactive "P")
5248   (setq truncate-lines
5249         (if (null arg) (not truncate-lines)
5250           (> (prefix-numeric-value arg) 0)))
5251   (redraw-display))
5252
5253 (defun gnus-summary-reselect-current-group (&optional all rescan)
5254   "Exit and then reselect the current newsgroup.
5255 The prefix argument ALL means to select all articles."
5256   (interactive "P")
5257   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5258     (error "Ephemeral groups can't be reselected"))
5259   (let ((current-subject (gnus-summary-article-number))
5260         (group gnus-newsgroup-name))
5261     (setq gnus-newsgroup-begin nil)
5262     (gnus-summary-exit)
5263     ;; We have to adjust the point of group mode buffer because
5264     ;; point was moved to the next unread newsgroup by exiting.
5265     (gnus-summary-jump-to-group group)
5266     (when rescan
5267       (save-excursion
5268         (gnus-group-get-new-news-this-group 1)))
5269     (gnus-group-read-group all t)
5270     (gnus-summary-goto-subject current-subject nil t)))
5271
5272 (defun gnus-summary-rescan-group (&optional all)
5273   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5274   (interactive "P")
5275   (gnus-summary-reselect-current-group all t))
5276
5277 (defun gnus-summary-update-info (&optional non-destructive)
5278   (save-excursion
5279     (let ((group gnus-newsgroup-name))
5280       (when group
5281         (when gnus-newsgroup-kill-headers
5282           (setq gnus-newsgroup-killed
5283                 (gnus-compress-sequence
5284                  (nconc
5285                   (gnus-set-sorted-intersection
5286                    (gnus-uncompress-range gnus-newsgroup-killed)
5287                    (setq gnus-newsgroup-unselected
5288                          (sort gnus-newsgroup-unselected '<)))
5289                   (setq gnus-newsgroup-unreads
5290                         (sort gnus-newsgroup-unreads '<)))
5291                  t)))
5292         (unless (listp (cdr gnus-newsgroup-killed))
5293           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5294         (let ((headers gnus-newsgroup-headers))
5295           ;; Set the new ranges of read articles.
5296           (save-excursion
5297             (set-buffer gnus-group-buffer)
5298             (gnus-undo-force-boundary))
5299           (gnus-update-read-articles
5300            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5301           ;; Set the current article marks.
5302           (let ((gnus-newsgroup-scored
5303                  (if (and (not gnus-save-score)
5304                           (not non-destructive))
5305                      nil
5306                    gnus-newsgroup-scored)))
5307             (save-excursion
5308               (gnus-update-marks)))
5309           ;; Do the cross-ref thing.
5310           (when gnus-use-cross-reference
5311             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5312           ;; Do not switch windows but change the buffer to work.
5313           (set-buffer gnus-group-buffer)
5314           (unless (gnus-ephemeral-group-p group)
5315             (gnus-group-update-group group)))))))
5316
5317 (defun gnus-summary-save-newsrc (&optional force)
5318   "Save the current number of read/marked articles in the dribble buffer.
5319 The dribble buffer will then be saved.
5320 If FORCE (the prefix), also save the .newsrc file(s)."
5321   (interactive "P")
5322   (gnus-summary-update-info t)
5323   (if force
5324       (gnus-save-newsrc-file)
5325     (gnus-dribble-save)))
5326
5327 (defun gnus-summary-exit (&optional temporary)
5328   "Exit reading current newsgroup, and then return to group selection mode.
5329 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5330   (interactive)
5331   (gnus-set-global-variables)
5332   (when (gnus-buffer-live-p gnus-article-buffer)
5333     (save-excursion
5334       (set-buffer gnus-article-buffer)
5335       (mm-destroy-parts gnus-article-mime-handles)))
5336   (gnus-kill-save-kill-buffer)
5337   (gnus-async-halt-prefetch)
5338   (let* ((group gnus-newsgroup-name)
5339          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5340          (mode major-mode)
5341          (group-point nil)
5342          (buf (current-buffer)))
5343     (unless quit-config
5344       ;; Do adaptive scoring, and possibly save score files.
5345       (when gnus-newsgroup-adaptive
5346         (gnus-score-adaptive))
5347       (when gnus-use-scoring
5348         (gnus-score-save)))
5349     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5350     ;; If we have several article buffers, we kill them at exit.
5351     (unless gnus-single-article-buffer
5352       (gnus-kill-buffer gnus-original-article-buffer)
5353       (setq gnus-article-current nil))
5354     (when gnus-use-cache
5355       (gnus-cache-possibly-remove-articles)
5356       (gnus-cache-save-buffers))
5357     (gnus-async-prefetch-remove-group group)
5358     (when gnus-suppress-duplicates
5359       (gnus-dup-enter-articles))
5360     (when gnus-use-trees
5361       (gnus-tree-close group))
5362     (when gnus-use-cache
5363       (gnus-cache-write-active))
5364     ;; Remove entries for this group.
5365     (nnmail-purge-split-history (gnus-group-real-name group))
5366     ;; Make all changes in this group permanent.
5367     (unless quit-config
5368       (gnus-run-hooks 'gnus-exit-group-hook)
5369       (gnus-summary-update-info))
5370     (gnus-close-group group)
5371     ;; Make sure where we were, and go to next newsgroup.
5372     (set-buffer gnus-group-buffer)
5373     (unless quit-config
5374       (gnus-group-jump-to-group group))
5375     (gnus-run-hooks 'gnus-summary-exit-hook)
5376     (unless (or quit-config
5377                 ;; If this group has disappeared from the summary
5378                 ;; buffer, don't skip forwards.
5379                 (not (string= group (gnus-group-group-name))))
5380       (gnus-group-next-unread-group 1))
5381     (setq group-point (point))
5382     (if temporary
5383         nil                             ;Nothing to do.
5384       ;; If we have several article buffers, we kill them at exit.
5385       (unless gnus-single-article-buffer
5386         (gnus-kill-buffer gnus-article-buffer)
5387         (gnus-kill-buffer gnus-original-article-buffer)
5388         (setq gnus-article-current nil))
5389       (set-buffer buf)
5390       (if (not gnus-kill-summary-on-exit)
5391           (gnus-deaden-summary)
5392         ;; We set all buffer-local variables to nil.  It is unclear why
5393         ;; this is needed, but if we don't, buffer-local variables are
5394         ;; not garbage-collected, it seems.  This would the lead to en
5395         ;; ever-growing Emacs.
5396         (gnus-summary-clear-local-variables)
5397         (when (get-buffer gnus-article-buffer)
5398           (bury-buffer gnus-article-buffer))
5399         ;; We clear the global counterparts of the buffer-local
5400         ;; variables as well, just to be on the safe side.
5401         (set-buffer gnus-group-buffer)
5402         (gnus-summary-clear-local-variables)
5403         ;; Return to group mode buffer.
5404         (when (eq mode 'gnus-summary-mode)
5405           (gnus-kill-buffer buf)))
5406       (setq gnus-current-select-method gnus-select-method)
5407       (pop-to-buffer gnus-group-buffer)
5408       (if (not quit-config)
5409           (progn
5410             (goto-char group-point)
5411             (gnus-configure-windows 'group 'force))
5412         (gnus-handle-ephemeral-exit quit-config))
5413       ;; Clear the current group name.
5414       (unless quit-config
5415         (setq gnus-newsgroup-name nil)))))
5416
5417 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5418 (defun gnus-summary-exit-no-update (&optional no-questions)
5419   "Quit reading current newsgroup without updating read article info."
5420   (interactive)
5421   (let* ((group gnus-newsgroup-name)
5422          (quit-config (gnus-group-quit-config group)))
5423     (when (or no-questions
5424               gnus-expert-user
5425               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5426       (gnus-async-halt-prefetch)
5427       (mapcar 'funcall
5428               (delq 'gnus-summary-expire-articles
5429                     (copy-sequence gnus-summary-prepare-exit-hook)))
5430       (when (gnus-buffer-live-p gnus-article-buffer)
5431         (save-excursion
5432           (set-buffer gnus-article-buffer)
5433           (mm-destroy-parts gnus-article-mime-handles)))
5434       ;; If we have several article buffers, we kill them at exit.
5435       (unless gnus-single-article-buffer
5436         (gnus-kill-buffer gnus-article-buffer)
5437         (gnus-kill-buffer gnus-original-article-buffer)
5438         (setq gnus-article-current nil))
5439       (if (not gnus-kill-summary-on-exit)
5440           (gnus-deaden-summary)
5441         (gnus-close-group group)
5442         (gnus-summary-clear-local-variables)
5443         (set-buffer gnus-group-buffer)
5444         (gnus-summary-clear-local-variables)
5445         (when (get-buffer gnus-summary-buffer)
5446           (kill-buffer gnus-summary-buffer)))
5447       (unless gnus-single-article-buffer
5448         (setq gnus-article-current nil))
5449       (when gnus-use-trees
5450         (gnus-tree-close group))
5451       (gnus-async-prefetch-remove-group group)
5452       (when (get-buffer gnus-article-buffer)
5453         (bury-buffer gnus-article-buffer))
5454       ;; Return to the group buffer.
5455       (gnus-configure-windows 'group 'force)
5456       ;; Clear the current group name.
5457       (setq gnus-newsgroup-name nil)
5458       (when (equal (gnus-group-group-name) group)
5459         (gnus-group-next-unread-group 1))
5460       (when quit-config
5461         (gnus-handle-ephemeral-exit quit-config)))))
5462
5463 (defun gnus-handle-ephemeral-exit (quit-config)
5464   "Handle movement when leaving an ephemeral group.
5465 The state which existed when entering the ephemeral is reset."
5466   (if (not (buffer-name (car quit-config)))
5467       (gnus-configure-windows 'group 'force)
5468     (set-buffer (car quit-config))
5469     (cond ((eq major-mode 'gnus-summary-mode)
5470            (gnus-set-global-variables))
5471           ((eq major-mode 'gnus-article-mode)
5472            (save-excursion
5473              ;; The `gnus-summary-buffer' variable may point
5474              ;; to the old summary buffer when using a single
5475              ;; article buffer.
5476              (unless (gnus-buffer-live-p gnus-summary-buffer)
5477                (set-buffer gnus-group-buffer))
5478              (set-buffer gnus-summary-buffer)
5479              (gnus-set-global-variables))))
5480     (if (or (eq (cdr quit-config) 'article)
5481             (eq (cdr quit-config) 'pick))
5482         (progn
5483           ;; The current article may be from the ephemeral group
5484           ;; thus it is best that we reload this article
5485           (gnus-summary-show-article)
5486           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5487               (gnus-configure-windows 'pick 'force)
5488             (gnus-configure-windows (cdr quit-config) 'force)))
5489       (gnus-configure-windows (cdr quit-config) 'force))
5490     (when (eq major-mode 'gnus-summary-mode)
5491       (gnus-summary-next-subject 1 nil t)
5492       (gnus-summary-recenter)
5493       (gnus-summary-position-point))))
5494
5495 ;;; Dead summaries.
5496
5497 (defvar gnus-dead-summary-mode-map nil)
5498
5499 (unless gnus-dead-summary-mode-map
5500   (setq gnus-dead-summary-mode-map (make-keymap))
5501   (suppress-keymap gnus-dead-summary-mode-map)
5502   (substitute-key-definition
5503    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5504   (let ((keys '("\C-d" "\r" "\177" [delete])))
5505     (while keys
5506       (define-key gnus-dead-summary-mode-map
5507         (pop keys) 'gnus-summary-wake-up-the-dead))))
5508
5509 (defvar gnus-dead-summary-mode nil
5510   "Minor mode for Gnus summary buffers.")
5511
5512 (defun gnus-dead-summary-mode (&optional arg)
5513   "Minor mode for Gnus summary buffers."
5514   (interactive "P")
5515   (when (eq major-mode 'gnus-summary-mode)
5516     (make-local-variable 'gnus-dead-summary-mode)
5517     (setq gnus-dead-summary-mode
5518           (if (null arg) (not gnus-dead-summary-mode)
5519             (> (prefix-numeric-value arg) 0)))
5520     (when gnus-dead-summary-mode
5521       (gnus-add-minor-mode
5522        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5523
5524 (defun gnus-deaden-summary ()
5525   "Make the current summary buffer into a dead summary buffer."
5526   ;; Kill any previous dead summary buffer.
5527   (when (and gnus-dead-summary
5528              (buffer-name gnus-dead-summary))
5529     (save-excursion
5530       (set-buffer gnus-dead-summary)
5531       (when gnus-dead-summary-mode
5532         (kill-buffer (current-buffer)))))
5533   ;; Make this the current dead summary.
5534   (setq gnus-dead-summary (current-buffer))
5535   (gnus-dead-summary-mode 1)
5536   (let ((name (buffer-name)))
5537     (when (string-match "Summary" name)
5538       (rename-buffer
5539        (concat (substring name 0 (match-beginning 0)) "Dead "
5540                (substring name (match-beginning 0)))
5541        t)
5542       (bury-buffer))))
5543
5544 (defun gnus-kill-or-deaden-summary (buffer)
5545   "Kill or deaden the summary BUFFER."
5546   (save-excursion
5547     (when (and (buffer-name buffer)
5548                (not gnus-single-article-buffer))
5549       (save-excursion
5550         (set-buffer buffer)
5551         (gnus-kill-buffer gnus-article-buffer)
5552         (gnus-kill-buffer gnus-original-article-buffer)))
5553     (cond (gnus-kill-summary-on-exit
5554            (when (and gnus-use-trees
5555                       (gnus-buffer-exists-p buffer))
5556              (save-excursion
5557                (set-buffer buffer)
5558                (gnus-tree-close gnus-newsgroup-name)))
5559            (gnus-kill-buffer buffer))
5560           ((gnus-buffer-exists-p buffer)
5561            (save-excursion
5562              (set-buffer buffer)
5563              (gnus-deaden-summary))))))
5564
5565 (defun gnus-summary-wake-up-the-dead (&rest args)
5566   "Wake up the dead summary buffer."
5567   (interactive)
5568   (gnus-dead-summary-mode -1)
5569   (let ((name (buffer-name)))
5570     (when (string-match "Dead " name)
5571       (rename-buffer
5572        (concat (substring name 0 (match-beginning 0))
5573                (substring name (match-end 0)))
5574        t)))
5575   (gnus-message 3 "This dead summary is now alive again"))
5576
5577 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5578 (defun gnus-summary-fetch-faq (&optional faq-dir)
5579   "Fetch the FAQ for the current group.
5580 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5581 in."
5582   (interactive
5583    (list
5584     (when current-prefix-arg
5585       (completing-read
5586        "Faq dir: " (and (listp gnus-group-faq-directory)
5587                         (mapcar (lambda (file) (list file))
5588                                 gnus-group-faq-directory))))))
5589   (let (gnus-faq-buffer)
5590     (when (setq gnus-faq-buffer
5591                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5592       (gnus-configure-windows 'summary-faq))))
5593
5594 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5595 (defun gnus-summary-describe-group (&optional force)
5596   "Describe the current newsgroup."
5597   (interactive "P")
5598   (gnus-group-describe-group force gnus-newsgroup-name))
5599
5600 (defun gnus-summary-describe-briefly ()
5601   "Describe summary mode commands briefly."
5602   (interactive)
5603   (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")))
5604
5605 ;; Walking around group mode buffer from summary mode.
5606
5607 (defun gnus-summary-next-group (&optional no-article target-group backward)
5608   "Exit current newsgroup and then select next unread newsgroup.
5609 If prefix argument NO-ARTICLE is non-nil, no article is selected
5610 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5611 previous group instead."
5612   (interactive "P")
5613   ;; Stop pre-fetching.
5614   (gnus-async-halt-prefetch)
5615   (let ((current-group gnus-newsgroup-name)
5616         (current-buffer (current-buffer))
5617         entered)
5618     ;; First we semi-exit this group to update Xrefs and all variables.
5619     ;; We can't do a real exit, because the window conf must remain
5620     ;; the same in case the user is prompted for info, and we don't
5621     ;; want the window conf to change before that...
5622     (gnus-summary-exit t)
5623     (while (not entered)
5624       ;; Then we find what group we are supposed to enter.
5625       (set-buffer gnus-group-buffer)
5626       (gnus-group-jump-to-group current-group)
5627       (setq target-group
5628             (or target-group
5629                 (if (eq gnus-keep-same-level 'best)
5630                     (gnus-summary-best-group gnus-newsgroup-name)
5631                   (gnus-summary-search-group backward gnus-keep-same-level))))
5632       (if (not target-group)
5633           ;; There are no further groups, so we return to the group
5634           ;; buffer.
5635           (progn
5636             (gnus-message 5 "Returning to the group buffer")
5637             (setq entered t)
5638             (when (gnus-buffer-live-p current-buffer)
5639               (set-buffer current-buffer)
5640               (gnus-summary-exit))
5641             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5642         ;; We try to enter the target group.
5643         (gnus-group-jump-to-group target-group)
5644         (let ((unreads (gnus-group-group-unread)))
5645           (if (and (or (eq t unreads)
5646                        (and unreads (not (zerop unreads))))
5647                    (gnus-summary-read-group
5648                     target-group nil no-article
5649                     (and (buffer-name current-buffer) current-buffer)
5650                     nil backward))
5651               (setq entered t)
5652             (setq current-group target-group
5653                   target-group nil)))))))
5654
5655 (defun gnus-summary-prev-group (&optional no-article)
5656   "Exit current newsgroup and then select previous unread newsgroup.
5657 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5658   (interactive "P")
5659   (gnus-summary-next-group no-article nil t))
5660
5661 ;; Walking around summary lines.
5662
5663 (defun gnus-summary-first-subject (&optional unread undownloaded)
5664   "Go to the first unread subject.
5665 If UNREAD is non-nil, go to the first unread article.
5666 Returns the article selected or nil if there are no unread articles."
5667   (interactive "P")
5668   (prog1
5669       (cond
5670        ;; Empty summary.
5671        ((null gnus-newsgroup-data)
5672         (gnus-message 3 "No articles in the group")
5673         nil)
5674        ;; Pick the first article.
5675        ((not unread)
5676         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5677         (gnus-data-number (car gnus-newsgroup-data)))
5678        ;; No unread articles.
5679        ((null gnus-newsgroup-unreads)
5680         (gnus-message 3 "No more unread articles")
5681         nil)
5682        ;; Find the first unread article.
5683        (t
5684         (let ((data gnus-newsgroup-data))
5685           (while (and data
5686                       (and (not (and undownloaded
5687                                      (eq gnus-undownloaded-mark
5688                                          (gnus-data-mark (car data)))))
5689                            (not (gnus-data-unread-p (car data)))))
5690             (setq data (cdr data)))
5691           (when data
5692             (goto-char (gnus-data-pos (car data)))
5693             (gnus-data-number (car data))))))
5694     (gnus-summary-position-point)))
5695
5696 (defun gnus-summary-next-subject (n &optional unread dont-display)
5697   "Go to next N'th summary line.
5698 If N is negative, go to the previous N'th subject line.
5699 If UNREAD is non-nil, only unread articles are selected.
5700 The difference between N and the actual number of steps taken is
5701 returned."
5702   (interactive "p")
5703   (let ((backward (< n 0))
5704         (n (abs n)))
5705     (while (and (> n 0)
5706                 (if backward
5707                     (gnus-summary-find-prev unread)
5708                   (gnus-summary-find-next unread)))
5709       (unless (zerop (setq n (1- n)))
5710         (gnus-summary-show-thread)))
5711     (when (/= 0 n)
5712       (gnus-message 7 "No more%s articles"
5713                     (if unread " unread" "")))
5714     (unless dont-display
5715       (gnus-summary-recenter)
5716       (gnus-summary-position-point))
5717     n))
5718
5719 (defun gnus-summary-next-unread-subject (n)
5720   "Go to next N'th unread summary line."
5721   (interactive "p")
5722   (gnus-summary-next-subject n t))
5723
5724 (defun gnus-summary-prev-subject (n &optional unread)
5725   "Go to previous N'th summary line.
5726 If optional argument UNREAD is non-nil, only unread article is selected."
5727   (interactive "p")
5728   (gnus-summary-next-subject (- n) unread))
5729
5730 (defun gnus-summary-prev-unread-subject (n)
5731   "Go to previous N'th unread summary line."
5732   (interactive "p")
5733   (gnus-summary-next-subject (- n) t))
5734
5735 (defun gnus-summary-goto-subject (article &optional force silent)
5736   "Go the subject line of ARTICLE.
5737 If FORCE, also allow jumping to articles not currently shown."
5738   (interactive "nArticle number: ")
5739   (let ((b (point))
5740         (data (gnus-data-find article)))
5741     ;; We read in the article if we have to.
5742     (and (not data)
5743          force
5744          (gnus-summary-insert-subject
5745           article
5746           (if (or (numberp force) (vectorp force)) force)
5747           t)
5748          (setq data (gnus-data-find article)))
5749     (goto-char b)
5750     (if (not data)
5751         (progn
5752           (unless silent
5753             (gnus-message 3 "Can't find article %d" article))
5754           nil)
5755       (goto-char (gnus-data-pos data))
5756       (gnus-summary-position-point)
5757       article)))
5758
5759 ;; Walking around summary lines with displaying articles.
5760
5761 (defun gnus-summary-expand-window (&optional arg)
5762   "Make the summary buffer take up the entire Emacs frame.
5763 Given a prefix, will force an `article' buffer configuration."
5764   (interactive "P")
5765   (if arg
5766       (gnus-configure-windows 'article 'force)
5767     (gnus-configure-windows 'summary 'force)))
5768
5769 (defun gnus-summary-display-article (article &optional all-header)
5770   "Display ARTICLE in article buffer."
5771   (gnus-set-global-variables)
5772   (if (null article)
5773       nil
5774     (prog1
5775         (if gnus-summary-display-article-function
5776             (funcall gnus-summary-display-article-function article all-header)
5777           (gnus-article-prepare article all-header))
5778       (gnus-run-hooks 'gnus-select-article-hook)
5779       (when (and gnus-current-article
5780                  (not (zerop gnus-current-article)))
5781         (gnus-summary-goto-subject gnus-current-article))
5782       (gnus-summary-recenter)
5783       (when (and gnus-use-trees gnus-show-threads)
5784         (gnus-possibly-generate-tree article)
5785         (gnus-highlight-selected-tree article))
5786       ;; Successfully display article.
5787       (gnus-article-set-window-start
5788        (cdr (assq article gnus-newsgroup-bookmarks))))))
5789
5790 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5791   "Select the current article.
5792 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5793 non-nil, the article will be re-fetched even if it already present in
5794 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5795 be displayed."
5796   ;; Make sure we are in the summary buffer to work around bbdb bug.
5797   (unless (eq major-mode 'gnus-summary-mode)
5798     (set-buffer gnus-summary-buffer))
5799   (let ((article (or article (gnus-summary-article-number)))
5800         (all-headers (not (not all-headers))) ;Must be T or NIL.
5801         gnus-summary-display-article-function)
5802     (and (not pseudo)
5803          (gnus-summary-article-pseudo-p article)
5804          (error "This is a pseudo-article"))
5805     (save-excursion
5806       (set-buffer gnus-summary-buffer)
5807       (if (or (and gnus-single-article-buffer
5808                    (or (null gnus-current-article)
5809                        (null gnus-article-current)
5810                        (null (get-buffer gnus-article-buffer))
5811                        (not (eq article (cdr gnus-article-current)))
5812                        (not (equal (car gnus-article-current)
5813                                    gnus-newsgroup-name))))
5814               (and (not gnus-single-article-buffer)
5815                    (or (null gnus-current-article)
5816                        (not (eq gnus-current-article article))))
5817               force)
5818           ;; The requested article is different from the current article.
5819           (progn
5820             (gnus-summary-display-article article all-headers)
5821             (when (or all-headers gnus-show-all-headers)
5822               (gnus-article-show-all-headers))
5823             (gnus-article-set-window-start
5824              (cdr (assq article gnus-newsgroup-bookmarks)))
5825             article)
5826         (when (or all-headers gnus-show-all-headers)
5827           (gnus-article-show-all-headers))
5828         'old))))
5829
5830 (defun gnus-summary-set-current-mark (&optional current-mark)
5831   "Obsolete function."
5832   nil)
5833
5834 (defun gnus-summary-next-article (&optional unread subject backward push)
5835   "Select the next article.
5836 If UNREAD, only unread articles are selected.
5837 If SUBJECT, only articles with SUBJECT are selected.
5838 If BACKWARD, the previous article is selected instead of the next."
5839   (interactive "P")
5840   (cond
5841    ;; Is there such an article?
5842    ((and (gnus-summary-search-forward unread subject backward)
5843          (or (gnus-summary-display-article (gnus-summary-article-number))
5844              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5845     (gnus-summary-position-point))
5846    ;; If not, we try the first unread, if that is wanted.
5847    ((and subject
5848          gnus-auto-select-same
5849          (gnus-summary-first-unread-article))
5850     (gnus-summary-position-point)
5851     (gnus-message 6 "Wrapped"))
5852    ;; Try to get next/previous article not displayed in this group.
5853    ((and gnus-auto-extend-newsgroup
5854          (not unread) (not subject))
5855     (gnus-summary-goto-article
5856      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5857      nil (count-lines (point-min) (point))))
5858    ;; Go to next/previous group.
5859    (t
5860     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5861       (gnus-summary-jump-to-group gnus-newsgroup-name))
5862     (let ((cmd last-command-char)
5863           (point
5864            (save-excursion
5865              (set-buffer gnus-group-buffer)
5866              (point)))
5867           (group
5868            (if (eq gnus-keep-same-level 'best)
5869                (gnus-summary-best-group gnus-newsgroup-name)
5870              (gnus-summary-search-group backward gnus-keep-same-level))))
5871       ;; For some reason, the group window gets selected.  We change
5872       ;; it back.
5873       (select-window (get-buffer-window (current-buffer)))
5874       ;; Select next unread newsgroup automagically.
5875       (cond
5876        ((or (not gnus-auto-select-next)
5877             (not cmd))
5878         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5879        ((or (eq gnus-auto-select-next 'quietly)
5880             (and (eq gnus-auto-select-next 'slightly-quietly)
5881                  push)
5882             (and (eq gnus-auto-select-next 'almost-quietly)
5883                  (gnus-summary-last-article-p)))
5884         ;; Select quietly.
5885         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5886             (gnus-summary-exit)
5887           (gnus-message 7 "No more%s articles (%s)..."
5888                         (if unread " unread" "")
5889                         (if group (concat "selecting " group)
5890                           "exiting"))
5891           (gnus-summary-next-group nil group backward)))
5892        (t
5893         (when (gnus-key-press-event-p last-input-event)
5894           (gnus-summary-walk-group-buffer
5895            gnus-newsgroup-name cmd unread backward point))))))))
5896
5897 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5898   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5899                       (?\C-p (gnus-group-prev-unread-group 1))))
5900         (cursor-in-echo-area t)
5901         keve key group ended)
5902     (save-excursion
5903       (set-buffer gnus-group-buffer)
5904       (goto-char start)
5905       (setq group
5906             (if (eq gnus-keep-same-level 'best)
5907                 (gnus-summary-best-group gnus-newsgroup-name)
5908               (gnus-summary-search-group backward gnus-keep-same-level))))
5909     (while (not ended)
5910       (gnus-message
5911        5 "No more%s articles%s" (if unread " unread" "")
5912        (if (and group
5913                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5914            (format " (Type %s for %s [%s])"
5915                    (single-key-description cmd) group
5916                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5917          (format " (Type %s to exit %s)"
5918                  (single-key-description cmd)
5919                  gnus-newsgroup-name)))
5920       ;; Confirm auto selection.
5921       (setq key (car (setq keve (gnus-read-event-char))))
5922       (setq ended t)
5923       (cond
5924        ((assq key keystrokes)
5925         (let ((obuf (current-buffer)))
5926           (switch-to-buffer gnus-group-buffer)
5927           (when group
5928             (gnus-group-jump-to-group group))
5929           (eval (cadr (assq key keystrokes)))
5930           (setq group (gnus-group-group-name))
5931           (switch-to-buffer obuf))
5932         (setq ended nil))
5933        ((equal key cmd)
5934         (if (or (not group)
5935                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5936             (gnus-summary-exit)
5937           (gnus-summary-next-group nil group backward)))
5938        (t
5939         (push (cdr keve) unread-command-events))))))
5940
5941 (defun gnus-summary-next-unread-article ()
5942   "Select unread article after current one."
5943   (interactive)
5944   (gnus-summary-next-article
5945    (or (not (eq gnus-summary-goto-unread 'never))
5946        (gnus-summary-last-article-p (gnus-summary-article-number)))
5947    (and gnus-auto-select-same
5948         (gnus-summary-article-subject))))
5949
5950 (defun gnus-summary-prev-article (&optional unread subject)
5951   "Select the article after the current one.
5952 If UNREAD is non-nil, only unread articles are selected."
5953   (interactive "P")
5954   (gnus-summary-next-article unread subject t))
5955
5956 (defun gnus-summary-prev-unread-article ()
5957   "Select unread article before current one."
5958   (interactive)
5959   (gnus-summary-prev-article
5960    (or (not (eq gnus-summary-goto-unread 'never))
5961        (gnus-summary-first-article-p (gnus-summary-article-number)))
5962    (and gnus-auto-select-same
5963         (gnus-summary-article-subject))))
5964
5965 (defun gnus-summary-next-page (&optional lines circular)
5966   "Show next page of the selected article.
5967 If at the end of the current article, select the next article.
5968 LINES says how many lines should be scrolled up.
5969
5970 If CIRCULAR is non-nil, go to the start of the article instead of
5971 selecting the next article when reaching the end of the current
5972 article."
5973   (interactive "P")
5974   (setq gnus-summary-buffer (current-buffer))
5975   (gnus-set-global-variables)
5976   (let ((article (gnus-summary-article-number))
5977         (article-window (get-buffer-window gnus-article-buffer t))
5978         endp)
5979     ;; If the buffer is empty, we have no article.
5980     (unless article
5981       (error "No article to select"))
5982     (gnus-configure-windows 'article)
5983     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5984         (if (and (eq gnus-summary-goto-unread 'never)
5985                  (not (gnus-summary-last-article-p article)))
5986             (gnus-summary-next-article)
5987           (gnus-summary-next-unread-article))
5988       (if (or (null gnus-current-article)
5989               (null gnus-article-current)
5990               (/= article (cdr gnus-article-current))
5991               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5992           ;; Selected subject is different from current article's.
5993           (gnus-summary-display-article article)
5994         (when article-window
5995           (gnus-eval-in-buffer-window gnus-article-buffer
5996             (setq endp (gnus-article-next-page lines)))
5997           (when endp
5998             (cond (circular
5999                    (gnus-summary-beginning-of-article))
6000                   (lines
6001                    (gnus-message 3 "End of message"))
6002                   ((null lines)
6003                    (if (and (eq gnus-summary-goto-unread 'never)
6004                             (not (gnus-summary-last-article-p article)))
6005                        (gnus-summary-next-article)
6006                      (gnus-summary-next-unread-article))))))))
6007     (gnus-summary-recenter)
6008     (gnus-summary-position-point)))
6009
6010 (defun gnus-summary-prev-page (&optional lines move)
6011   "Show previous page of selected article.
6012 Argument LINES specifies lines to be scrolled down.
6013 If MOVE, move to the previous unread article if point is at
6014 the beginning of the buffer."
6015   (interactive "P")
6016   (let ((article (gnus-summary-article-number))
6017         (article-window (get-buffer-window gnus-article-buffer t))
6018         endp)
6019     (gnus-configure-windows 'article)
6020     (if (or (null gnus-current-article)
6021             (null gnus-article-current)
6022             (/= article (cdr gnus-article-current))
6023             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6024         ;; Selected subject is different from current article's.
6025         (gnus-summary-display-article article)
6026       (gnus-summary-recenter)
6027       (when article-window
6028         (gnus-eval-in-buffer-window gnus-article-buffer
6029           (setq endp (gnus-article-prev-page lines)))
6030         (when (and move endp)
6031           (cond (lines
6032                  (gnus-message 3 "Beginning of message"))
6033                 ((null lines)
6034                  (if (and (eq gnus-summary-goto-unread 'never)
6035                           (not (gnus-summary-first-article-p article)))
6036                      (gnus-summary-prev-article)
6037                    (gnus-summary-prev-unread-article))))))))
6038   (gnus-summary-position-point))
6039
6040 (defun gnus-summary-prev-page-or-article (&optional lines)
6041   "Show previous page of selected article.
6042 Argument LINES specifies lines to be scrolled down.
6043 If at the beginning of the article, go to the next article."
6044   (interactive "P")
6045   (gnus-summary-prev-page lines t))
6046
6047 (defun gnus-summary-scroll-up (lines)
6048   "Scroll up (or down) one line current article.
6049 Argument LINES specifies lines to be scrolled up (or down if negative)."
6050   (interactive "p")
6051   (gnus-configure-windows 'article)
6052   (gnus-summary-show-thread)
6053   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6054     (gnus-eval-in-buffer-window gnus-article-buffer
6055       (cond ((> lines 0)
6056              (when (gnus-article-next-page lines)
6057                (gnus-message 3 "End of message")))
6058             ((< lines 0)
6059              (gnus-article-prev-page (- lines))))))
6060   (gnus-summary-recenter)
6061   (gnus-summary-position-point))
6062
6063 (defun gnus-summary-scroll-down (lines)
6064   "Scroll down (or up) one line current article.
6065 Argument LINES specifies lines to be scrolled down (or up if negative)."
6066   (interactive "p")
6067   (gnus-summary-scroll-up (- lines)))
6068
6069 (defun gnus-summary-next-same-subject ()
6070   "Select next article which has the same subject as current one."
6071   (interactive)
6072   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6073
6074 (defun gnus-summary-prev-same-subject ()
6075   "Select previous article which has the same subject as current one."
6076   (interactive)
6077   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6078
6079 (defun gnus-summary-next-unread-same-subject ()
6080   "Select next unread article which has the same subject as current one."
6081   (interactive)
6082   (gnus-summary-next-article t (gnus-summary-article-subject)))
6083
6084 (defun gnus-summary-prev-unread-same-subject ()
6085   "Select previous unread article which has the same subject as current one."
6086   (interactive)
6087   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6088
6089 (defun gnus-summary-first-unread-article ()
6090   "Select the first unread article.
6091 Return nil if there are no unread articles."
6092   (interactive)
6093   (prog1
6094       (when (gnus-summary-first-subject t)
6095         (gnus-summary-show-thread)
6096         (gnus-summary-first-subject t)
6097         (gnus-summary-display-article (gnus-summary-article-number)))
6098     (gnus-summary-position-point)))
6099
6100 (defun gnus-summary-first-unread-subject ()
6101   "Place the point on the subject line of the first unread article.
6102 Return nil if there are no unread articles."
6103   (interactive)
6104   (prog1
6105       (when (gnus-summary-first-subject t)
6106         (gnus-summary-show-thread)
6107         (gnus-summary-first-subject t))
6108     (gnus-summary-position-point)))
6109
6110 (defun gnus-summary-first-article ()
6111   "Select the first article.
6112 Return nil if there are no articles."
6113   (interactive)
6114   (prog1
6115       (when (gnus-summary-first-subject)
6116         (gnus-summary-show-thread)
6117         (gnus-summary-first-subject)
6118         (gnus-summary-display-article (gnus-summary-article-number)))
6119     (gnus-summary-position-point)))
6120
6121 (defun gnus-summary-best-unread-article ()
6122   "Select the unread article with the highest score."
6123   (interactive)
6124   (let ((best -1000000)
6125         (data gnus-newsgroup-data)
6126         article score)
6127     (while data
6128       (and (gnus-data-unread-p (car data))
6129            (> (setq score
6130                     (gnus-summary-article-score (gnus-data-number (car data))))
6131               best)
6132            (setq best score
6133                  article (gnus-data-number (car data))))
6134       (setq data (cdr data)))
6135     (prog1
6136         (if article
6137             (gnus-summary-goto-article article)
6138           (error "No unread articles"))
6139       (gnus-summary-position-point))))
6140
6141 (defun gnus-summary-last-subject ()
6142   "Go to the last displayed subject line in the group."
6143   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6144     (when article
6145       (gnus-summary-goto-subject article))))
6146
6147 (defun gnus-summary-goto-article (article &optional all-headers force)
6148   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6149 If ALL-HEADERS is non-nil, no header lines are hidden.
6150 If FORCE, go to the article even if it isn't displayed.  If FORCE
6151 is a number, it is the line the article is to be displayed on."
6152   (interactive
6153    (list
6154     (completing-read
6155      "Article number or Message-ID: "
6156      (mapcar (lambda (number) (list (int-to-string number)))
6157              gnus-newsgroup-limit))
6158     current-prefix-arg
6159     t))
6160   (prog1
6161       (if (and (stringp article)
6162                (string-match "@" article))
6163           (gnus-summary-refer-article article)
6164         (when (stringp article)
6165           (setq article (string-to-number article)))
6166         (if (gnus-summary-goto-subject article force)
6167             (gnus-summary-display-article article all-headers)
6168           (gnus-message 4 "Couldn't go to article %s" article) nil))
6169     (gnus-summary-position-point)))
6170
6171 (defun gnus-summary-goto-last-article ()
6172   "Go to the previously read article."
6173   (interactive)
6174   (prog1
6175       (when gnus-last-article
6176         (gnus-summary-goto-article gnus-last-article nil t))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-pop-article (number)
6180   "Pop one article off the history and go to the previous.
6181 NUMBER articles will be popped off."
6182   (interactive "p")
6183   (let (to)
6184     (setq gnus-newsgroup-history
6185           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6186     (if to
6187         (gnus-summary-goto-article (car to) nil t)
6188       (error "Article history empty")))
6189   (gnus-summary-position-point))
6190
6191 ;; Summary commands and functions for limiting the summary buffer.
6192
6193 (defun gnus-summary-limit-to-articles (n)
6194   "Limit the summary buffer to the next N articles.
6195 If not given a prefix, use the process marked articles instead."
6196   (interactive "P")
6197   (prog1
6198       (let ((articles (gnus-summary-work-articles n)))
6199         (setq gnus-newsgroup-processable nil)
6200         (gnus-summary-limit articles))
6201     (gnus-summary-position-point)))
6202
6203 (defun gnus-summary-pop-limit (&optional total)
6204   "Restore the previous limit.
6205 If given a prefix, remove all limits."
6206   (interactive "P")
6207   (when total
6208     (setq gnus-newsgroup-limits
6209           (list (mapcar (lambda (h) (mail-header-number h))
6210                         gnus-newsgroup-headers))))
6211   (unless gnus-newsgroup-limits
6212     (error "No limit to pop"))
6213   (prog1
6214       (gnus-summary-limit nil 'pop)
6215     (gnus-summary-position-point)))
6216
6217 (defun gnus-summary-limit-to-subject (subject &optional header)
6218   "Limit the summary buffer to articles that have subjects that match a regexp."
6219   (interactive "sLimit to subject (regexp): ")
6220   (unless header
6221     (setq header "subject"))
6222   (when (not (equal "" subject))
6223     (prog1
6224         (let ((articles (gnus-summary-find-matching
6225                          (or header "subject") subject 'all)))
6226           (unless articles
6227             (error "Found no matches for \"%s\"" subject))
6228           (gnus-summary-limit articles))
6229       (gnus-summary-position-point))))
6230
6231 (defun gnus-summary-limit-to-author (from)
6232   "Limit the summary buffer to articles that have authors that match a regexp."
6233   (interactive "sLimit to author (regexp): ")
6234   (gnus-summary-limit-to-subject from "from"))
6235
6236 (defun gnus-summary-limit-to-age (age &optional younger-p)
6237   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6238 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6239 articles that are younger than AGE days."
6240   (interactive
6241    (let ((younger current-prefix-arg)
6242          (days-got nil)
6243          days)
6244      (while (not days-got)
6245        (setq days (if younger
6246                       (read-string "Limit to articles within (in days): ")
6247                     (read-string "Limit to articles old than (in days): ")))
6248        (when (> (length days) 0)
6249          (setq days (read days)))
6250        (if (numberp days)
6251            (setq days-got t)
6252          (message "Please enter a number.")
6253          (sleep-for 1)))
6254      (list days younger)))
6255   (prog1
6256       (let ((data gnus-newsgroup-data)
6257             (cutoff (days-to-time age))
6258             articles d date is-younger)
6259         (while (setq d (pop data))
6260           (when (and (vectorp (gnus-data-header d))
6261                      (setq date (mail-header-date (gnus-data-header d))))
6262             (setq is-younger (time-less-p
6263                               (time-since (condition-case ()
6264                                               (date-to-time date)
6265                                             (error '(0 0))))
6266                               cutoff))
6267             (when (if younger-p
6268                       is-younger
6269                     (not is-younger))
6270               (push (gnus-data-number d) articles))))
6271         (gnus-summary-limit (nreverse articles)))
6272     (gnus-summary-position-point)))
6273
6274 (defun gnus-summary-limit-to-extra (header regexp)
6275   "Limit the summary buffer to articles that match an 'extra' header."
6276   (interactive
6277    (let ((header
6278           (intern
6279            (gnus-completing-read
6280             (symbol-name (car gnus-extra-headers))      
6281             "Limit extra header:"       
6282             (mapcar (lambda (x) 
6283                       (cons (symbol-name x) x))
6284                     gnus-extra-headers)
6285             nil                 
6286             t))))
6287      (list header
6288            (read-string (format "Limit to header %s (regexp): " header)))))
6289   (when (not (equal "" regexp))
6290     (prog1
6291         (let ((articles (gnus-summary-find-matching
6292                          (cons 'extra header) regexp 'all)))
6293           (unless articles
6294             (error "Found no matches for \"%s\"" regexp))
6295           (gnus-summary-limit articles))
6296       (gnus-summary-position-point))))
6297
6298 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6299 (make-obsolete
6300  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6301
6302 (defun gnus-summary-limit-to-unread (&optional all)
6303   "Limit the summary buffer to articles that are not marked as read.
6304 If ALL is non-nil, limit strictly to unread articles."
6305   (interactive "P")
6306   (if all
6307       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6308     (gnus-summary-limit-to-marks
6309      ;; Concat all the marks that say that an article is read and have
6310      ;; those removed.
6311      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6312            gnus-killed-mark gnus-kill-file-mark
6313            gnus-low-score-mark gnus-expirable-mark
6314            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6315            gnus-duplicate-mark gnus-souped-mark)
6316      'reverse)))
6317
6318 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6319 (make-obsolete 'gnus-summary-delete-marked-with
6320                'gnus-summary-limit-exlude-marks)
6321
6322 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6323   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6324 If REVERSE, limit the summary buffer to articles that are marked
6325 with MARKS.  MARKS can either be a string of marks or a list of marks.
6326 Returns how many articles were removed."
6327   (interactive "sMarks: ")
6328   (gnus-summary-limit-to-marks marks t))
6329
6330 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6331   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6332 If REVERSE (the prefix), limit the summary buffer to articles that are
6333 not marked with MARKS.  MARKS can either be a string of marks or a
6334 list of marks.
6335 Returns how many articles were removed."
6336   (interactive "sMarks: \nP")
6337   (prog1
6338       (let ((data gnus-newsgroup-data)
6339             (marks (if (listp marks) marks
6340                      (append marks nil))) ; Transform to list.
6341             articles)
6342         (while data
6343           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6344                   (memq (gnus-data-mark (car data)) marks))
6345             (push (gnus-data-number (car data)) articles))
6346           (setq data (cdr data)))
6347         (gnus-summary-limit articles))
6348     (gnus-summary-position-point)))
6349
6350 (defun gnus-summary-limit-to-score (&optional score)
6351   "Limit to articles with score at or above SCORE."
6352   (interactive "P")
6353   (setq score (if score
6354                   (prefix-numeric-value score)
6355                 (or gnus-summary-default-score 0)))
6356   (let ((data gnus-newsgroup-data)
6357         articles)
6358     (while data
6359       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6360                 score)
6361         (push (gnus-data-number (car data)) articles))
6362       (setq data (cdr data)))
6363     (prog1
6364         (gnus-summary-limit articles)
6365       (gnus-summary-position-point))))
6366
6367 (defun gnus-summary-limit-include-thread (id)
6368   "Display all the hidden articles that in the current thread."
6369   (interactive (list (mail-header-id (gnus-summary-article-header))))
6370   (let ((articles (gnus-articles-in-thread
6371                    (gnus-id-to-thread (gnus-root-id id)))))
6372     (prog1
6373         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6374       (gnus-summary-position-point))))
6375
6376 (defun gnus-summary-limit-include-dormant ()
6377   "Display all the hidden articles that are marked as dormant.
6378 Note that this command only works on a subset of the articles currently
6379 fetched for this group."
6380   (interactive)
6381   (unless gnus-newsgroup-dormant
6382     (error "There are no dormant articles in this group"))
6383   (prog1
6384       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6385     (gnus-summary-position-point)))
6386
6387 (defun gnus-summary-limit-exclude-dormant ()
6388   "Hide all dormant articles."
6389   (interactive)
6390   (prog1
6391       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6392     (gnus-summary-position-point)))
6393
6394 (defun gnus-summary-limit-exclude-childless-dormant ()
6395   "Hide all dormant articles that have no children."
6396   (interactive)
6397   (let ((data (gnus-data-list t))
6398         articles d children)
6399     ;; Find all articles that are either not dormant or have
6400     ;; children.
6401     (while (setq d (pop data))
6402       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6403                 (and (setq children
6404                            (gnus-article-children (gnus-data-number d)))
6405                      (let (found)
6406                        (while children
6407                          (when (memq (car children) articles)
6408                            (setq children nil
6409                                  found t))
6410                          (pop children))
6411                        found)))
6412         (push (gnus-data-number d) articles)))
6413     ;; Do the limiting.
6414     (prog1
6415         (gnus-summary-limit articles)
6416       (gnus-summary-position-point))))
6417
6418 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6419   "Mark all unread excluded articles as read.
6420 If ALL, mark even excluded ticked and dormants as read."
6421   (interactive "P")
6422   (let ((articles (gnus-sorted-complement
6423                    (sort
6424                     (mapcar (lambda (h) (mail-header-number h))
6425                             gnus-newsgroup-headers)
6426                     '<)
6427                    (sort gnus-newsgroup-limit '<)))
6428         article)
6429     (setq gnus-newsgroup-unreads
6430           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6431     (if all
6432         (setq gnus-newsgroup-dormant nil
6433               gnus-newsgroup-marked nil
6434               gnus-newsgroup-reads
6435               (nconc
6436                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6437                gnus-newsgroup-reads))
6438       (while (setq article (pop articles))
6439         (unless (or (memq article gnus-newsgroup-dormant)
6440                     (memq article gnus-newsgroup-marked))
6441           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6442
6443 (defun gnus-summary-limit (articles &optional pop)
6444   (if pop
6445       ;; We pop the previous limit off the stack and use that.
6446       (setq articles (car gnus-newsgroup-limits)
6447             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6448     ;; We use the new limit, so we push the old limit on the stack.
6449     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6450   ;; Set the limit.
6451   (setq gnus-newsgroup-limit articles)
6452   (let ((total (length gnus-newsgroup-data))
6453         (data (gnus-data-find-list (gnus-summary-article-number)))
6454         (gnus-summary-mark-below nil)   ; Inhibit this.
6455         found)
6456     ;; This will do all the work of generating the new summary buffer
6457     ;; according to the new limit.
6458     (gnus-summary-prepare)
6459     ;; Hide any threads, possibly.
6460     (and gnus-show-threads
6461          gnus-thread-hide-subtree
6462          (gnus-summary-hide-all-threads))
6463     ;; Try to return to the article you were at, or one in the
6464     ;; neighborhood.
6465     (when data
6466       ;; We try to find some article after the current one.
6467       (while data
6468         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6469           (setq data nil
6470                 found t))
6471         (setq data (cdr data))))
6472     (unless found
6473       ;; If there is no data, that means that we were after the last
6474       ;; article.  The same goes when we can't find any articles
6475       ;; after the current one.
6476       (goto-char (point-max))
6477       (gnus-summary-find-prev))
6478     (gnus-set-mode-line 'summary)
6479     ;; We return how many articles were removed from the summary
6480     ;; buffer as a result of the new limit.
6481     (- total (length gnus-newsgroup-data))))
6482
6483 (defsubst gnus-invisible-cut-children (threads)
6484   (let ((num 0))
6485     (while threads
6486       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6487         (incf num))
6488       (pop threads))
6489     (< num 2)))
6490
6491 (defsubst gnus-cut-thread (thread)
6492   "Go forwards in the thread until we find an article that we want to display."
6493   (when (or (eq gnus-fetch-old-headers 'some)
6494             (eq gnus-fetch-old-headers 'invisible)
6495             (numberp gnus-fetch-old-headers)
6496             (eq gnus-build-sparse-threads 'some)
6497             (eq gnus-build-sparse-threads 'more))
6498     ;; Deal with old-fetched headers and sparse threads.
6499     (while (and
6500             thread
6501             (or
6502              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6503              (gnus-summary-article-ancient-p
6504               (mail-header-number (car thread))))
6505             (if (or (<= (length (cdr thread)) 1)
6506                     (eq gnus-fetch-old-headers 'invisible))
6507                 (setq gnus-newsgroup-limit
6508                       (delq (mail-header-number (car thread))
6509                             gnus-newsgroup-limit)
6510                       thread (cadr thread))
6511               (when (gnus-invisible-cut-children (cdr thread))
6512                 (let ((th (cdr thread)))
6513                   (while th
6514                     (if (memq (mail-header-number (caar th))
6515                               gnus-newsgroup-limit)
6516                         (setq thread (car th)
6517                               th nil)
6518                       (setq th (cdr th))))))))))
6519   thread)
6520
6521 (defun gnus-cut-threads (threads)
6522   "Cut off all uninteresting articles from the beginning of threads."
6523   (when (or (eq gnus-fetch-old-headers 'some)
6524             (eq gnus-fetch-old-headers 'invisible)
6525             (numberp gnus-fetch-old-headers)
6526             (eq gnus-build-sparse-threads 'some)
6527             (eq gnus-build-sparse-threads 'more))
6528     (let ((th threads))
6529       (while th
6530         (setcar th (gnus-cut-thread (car th)))
6531         (setq th (cdr th)))))
6532   ;; Remove nixed out threads.
6533   (delq nil threads))
6534
6535 (defun gnus-summary-initial-limit (&optional show-if-empty)
6536   "Figure out what the initial limit is supposed to be on group entry.
6537 This entails weeding out unwanted dormants, low-scored articles,
6538 fetch-old-headers verbiage, and so on."
6539   ;; Most groups have nothing to remove.
6540   (if (or gnus-inhibit-limiting
6541           (and (null gnus-newsgroup-dormant)
6542                (not (eq gnus-fetch-old-headers 'some))
6543                (not (numberp gnus-fetch-old-headers))
6544                (not (eq gnus-fetch-old-headers 'invisible))
6545                (null gnus-summary-expunge-below)
6546                (not (eq gnus-build-sparse-threads 'some))
6547                (not (eq gnus-build-sparse-threads 'more))
6548                (null gnus-thread-expunge-below)
6549                (not gnus-use-nocem)))
6550       ()                                ; Do nothing.
6551     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6552     (setq gnus-newsgroup-limit nil)
6553     (mapatoms
6554      (lambda (node)
6555        (unless (car (symbol-value node))
6556          ;; These threads have no parents -- they are roots.
6557          (let ((nodes (cdr (symbol-value node)))
6558                thread)
6559            (while nodes
6560              (if (and gnus-thread-expunge-below
6561                       (< (gnus-thread-total-score (car nodes))
6562                          gnus-thread-expunge-below))
6563                  (gnus-expunge-thread (pop nodes))
6564                (setq thread (pop nodes))
6565                (gnus-summary-limit-children thread))))))
6566      gnus-newsgroup-dependencies)
6567     ;; If this limitation resulted in an empty group, we might
6568     ;; pop the previous limit and use it instead.
6569     (when (and (not gnus-newsgroup-limit)
6570                show-if-empty)
6571       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6572     gnus-newsgroup-limit))
6573
6574 (defun gnus-summary-limit-children (thread)
6575   "Return 1 if this subthread is visible and 0 if it is not."
6576   ;; First we get the number of visible children to this thread.  This
6577   ;; is done by recursing down the thread using this function, so this
6578   ;; will really go down to a leaf article first, before slowly
6579   ;; working its way up towards the root.
6580   (when thread
6581     (let ((children
6582            (if (cdr thread)
6583                (apply '+ (mapcar 'gnus-summary-limit-children
6584                                  (cdr thread)))
6585              0))
6586           (number (mail-header-number (car thread)))
6587           score)
6588       (if (and
6589            (not (memq number gnus-newsgroup-marked))
6590            (or
6591             ;; If this article is dormant and has absolutely no visible
6592             ;; children, then this article isn't visible.
6593             (and (memq number gnus-newsgroup-dormant)
6594                  (zerop children))
6595             ;; If this is "fetch-old-headered" and there is no
6596             ;; visible children, then we don't want this article.
6597             (and (or (eq gnus-fetch-old-headers 'some)
6598                      (numberp gnus-fetch-old-headers))
6599                  (gnus-summary-article-ancient-p number)
6600                  (zerop children))
6601             ;; If this is "fetch-old-headered" and `invisible', then
6602             ;; we don't want this article.
6603             (and (eq gnus-fetch-old-headers 'invisible)
6604                  (gnus-summary-article-ancient-p number))
6605             ;; If this is a sparsely inserted article with no children,
6606             ;; we don't want it.
6607             (and (eq gnus-build-sparse-threads 'some)
6608                  (gnus-summary-article-sparse-p number)
6609                  (zerop children))
6610             ;; If we use expunging, and this article is really
6611             ;; low-scored, then we don't want this article.
6612             (when (and gnus-summary-expunge-below
6613                        (< (setq score
6614                                 (or (cdr (assq number gnus-newsgroup-scored))
6615                                     gnus-summary-default-score))
6616                           gnus-summary-expunge-below))
6617               ;; We increase the expunge-tally here, but that has
6618               ;; nothing to do with the limits, really.
6619               (incf gnus-newsgroup-expunged-tally)
6620               ;; We also mark as read here, if that's wanted.
6621               (when (and gnus-summary-mark-below
6622                          (< score gnus-summary-mark-below))
6623                 (setq gnus-newsgroup-unreads
6624                       (delq number gnus-newsgroup-unreads))
6625                 (if gnus-newsgroup-auto-expire
6626                     (push number gnus-newsgroup-expirable)
6627                   (push (cons number gnus-low-score-mark)
6628                         gnus-newsgroup-reads)))
6629               t)
6630             ;; Check NoCeM things.
6631             (if (and gnus-use-nocem
6632                      (gnus-nocem-unwanted-article-p
6633                       (mail-header-id (car thread))))
6634                 (progn
6635                   (setq gnus-newsgroup-unreads
6636                         (delq number gnus-newsgroup-unreads))
6637                   t))))
6638           ;; Nope, invisible article.
6639           0
6640         ;; Ok, this article is to be visible, so we add it to the limit
6641         ;; and return 1.
6642         (push number gnus-newsgroup-limit)
6643         1))))
6644
6645 (defun gnus-expunge-thread (thread)
6646   "Mark all articles in THREAD as read."
6647   (let* ((number (mail-header-number (car thread))))
6648     (incf gnus-newsgroup-expunged-tally)
6649     ;; We also mark as read here, if that's wanted.
6650     (setq gnus-newsgroup-unreads
6651           (delq number gnus-newsgroup-unreads))
6652     (if gnus-newsgroup-auto-expire
6653         (push number gnus-newsgroup-expirable)
6654       (push (cons number gnus-low-score-mark)
6655             gnus-newsgroup-reads)))
6656   ;; Go recursively through all subthreads.
6657   (mapcar 'gnus-expunge-thread (cdr thread)))
6658
6659 ;; Summary article oriented commands
6660
6661 (defun gnus-summary-refer-parent-article (n)
6662   "Refer parent article N times.
6663 If N is negative, go to ancestor -N instead.
6664 The difference between N and the number of articles fetched is returned."
6665   (interactive "p")
6666   (let ((skip 1)
6667         error header ref)
6668     (when (not (natnump n))
6669       (setq skip (abs n)
6670             n 1))
6671     (while (and (> n 0)
6672                 (not error))
6673       (setq header (gnus-summary-article-header))
6674       (if (and (eq (mail-header-number header)
6675                    (cdr gnus-article-current))
6676                (equal gnus-newsgroup-name
6677                       (car gnus-article-current)))
6678           ;; If we try to find the parent of the currently
6679           ;; displayed article, then we take a look at the actual
6680           ;; References header, since this is slightly more
6681           ;; reliable than the References field we got from the
6682           ;; server.
6683           (save-excursion
6684             (set-buffer gnus-original-article-buffer)
6685             (nnheader-narrow-to-headers)
6686             (unless (setq ref (message-fetch-field "references"))
6687               (setq ref (message-fetch-field "in-reply-to")))
6688             (widen))
6689         (setq ref
6690               ;; It's not the current article, so we take a bet on
6691               ;; the value we got from the server.
6692               (mail-header-references header)))
6693       (if (and ref
6694                (not (equal ref "")))
6695           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6696             (gnus-message 1 "Couldn't find parent"))
6697         (gnus-message 1 "No references in article %d"
6698                       (gnus-summary-article-number))
6699         (setq error t))
6700       (decf n))
6701     (gnus-summary-position-point)
6702     n))
6703
6704 (defun gnus-summary-refer-references ()
6705   "Fetch all articles mentioned in the References header.
6706 Return the number of articles fetched."
6707   (interactive)
6708   (let ((ref (mail-header-references (gnus-summary-article-header)))
6709         (current (gnus-summary-article-number))
6710         (n 0))
6711     (if (or (not ref)
6712             (equal ref ""))
6713         (error "No References in the current article")
6714       ;; For each Message-ID in the References header...
6715       (while (string-match "<[^>]*>" ref)
6716         (incf n)
6717         ;; ... fetch that article.
6718         (gnus-summary-refer-article
6719          (prog1 (match-string 0 ref)
6720            (setq ref (substring ref (match-end 0))))))
6721       (gnus-summary-goto-subject current)
6722       (gnus-summary-position-point)
6723       n)))
6724
6725 (defun gnus-summary-refer-thread (&optional limit)
6726   "Fetch all articles in the current thread.
6727 If LIMIT (the numerical prefix), fetch that many old headers instead
6728 of what's specified by the `gnus-refer-thread-limit' variable."
6729   (interactive "P")
6730   (let ((id (mail-header-id (gnus-summary-article-header)))
6731         (limit (if limit (prefix-numeric-value limit)
6732                  gnus-refer-thread-limit)))
6733     ;; We want to fetch LIMIT *old* headers, but we also have to
6734     ;; re-fetch all the headers in the current buffer, because many of
6735     ;; them may be undisplayed.  So we adjust LIMIT.
6736     (when (numberp limit)
6737       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6738     (unless (eq gnus-fetch-old-headers 'invisible)
6739       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6740       ;; Retrieve the headers and read them in.
6741       (if (eq (gnus-retrieve-headers
6742                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6743               'nov)
6744           (gnus-build-all-threads)
6745         (error "Can't fetch thread from backends that don't support NOV"))
6746       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6747     (gnus-summary-limit-include-thread id)))
6748
6749 (defun gnus-summary-refer-article (message-id)
6750   "Fetch an article specified by MESSAGE-ID."
6751   (interactive "sMessage-ID: ")
6752   (when (and (stringp message-id)
6753              (not (zerop (length message-id))))
6754     ;; Construct the correct Message-ID if necessary.
6755     ;; Suggested by tale@pawl.rpi.edu.
6756     (unless (string-match "^<" message-id)
6757       (setq message-id (concat "<" message-id)))
6758     (unless (string-match ">$" message-id)
6759       (setq message-id (concat message-id ">")))
6760     (let* ((header (gnus-id-to-header message-id))
6761            (sparse (and header
6762                         (gnus-summary-article-sparse-p
6763                          (mail-header-number header))
6764                         (memq (mail-header-number header)
6765                               gnus-newsgroup-limit)))
6766            number)
6767       (cond
6768        ;; If the article is present in the buffer we just go to it.
6769        ((and header
6770              (or (not (gnus-summary-article-sparse-p
6771                        (mail-header-number header)))
6772                  sparse))
6773         (prog1
6774             (gnus-summary-goto-article
6775              (mail-header-number header) nil t)
6776           (when sparse
6777             (gnus-summary-update-article (mail-header-number header)))))
6778        (t
6779         ;; We fetch the article.
6780         (catch 'found
6781           (dolist (gnus-override-method (gnus-refer-article-methods))
6782             (gnus-check-server gnus-override-method)
6783             ;; Fetch the header, and display the article.
6784             (when (setq number (gnus-summary-insert-subject message-id))
6785               (gnus-summary-select-article nil nil nil number)
6786               (throw 'found t)))
6787           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6788
6789 (defun gnus-refer-article-methods ()
6790   "Return a list of referrable methods."
6791   (cond
6792    ;; No method, so we default to current and native.
6793    ((null gnus-refer-article-method)
6794     (list gnus-current-select-method gnus-select-method))
6795    ;; Current.
6796    ((eq 'current gnus-refer-article-method)
6797     (list gnus-current-select-method))
6798    ;; List of select methods.
6799    ((not (stringp (cadr gnus-refer-article-method)))
6800     (let (out)
6801       (dolist (method gnus-refer-article-method)
6802         (push (if (eq 'current method)
6803                   gnus-current-select-method
6804                 method)
6805               out))
6806       (nreverse out)))
6807    ;; One single select method.
6808    (t
6809     (list gnus-refer-article-method))))
6810
6811 (defun gnus-summary-edit-parameters ()
6812   "Edit the group parameters of the current group."
6813   (interactive)
6814   (gnus-group-edit-group gnus-newsgroup-name 'params))
6815
6816 (defun gnus-summary-customize-parameters ()
6817   "Customize the group parameters of the current group."
6818   (interactive)
6819   (gnus-group-customize gnus-newsgroup-name))
6820
6821 (defun gnus-summary-enter-digest-group (&optional force)
6822   "Enter an nndoc group based on the current article.
6823 If FORCE, force a digest interpretation.  If not, try
6824 to guess what the document format is."
6825   (interactive "P")
6826   (let ((conf gnus-current-window-configuration))
6827     (save-excursion
6828       (gnus-summary-select-article))
6829     (setq gnus-current-window-configuration conf)
6830     (let* ((name (format "%s-%d"
6831                          (gnus-group-prefixed-name
6832                           gnus-newsgroup-name (list 'nndoc ""))
6833                          (save-excursion
6834                            (set-buffer gnus-summary-buffer)
6835                            gnus-current-article)))
6836            (ogroup gnus-newsgroup-name)
6837            (params (append (gnus-info-params (gnus-get-info ogroup))
6838                            (list (cons 'to-group ogroup))
6839                            (list (cons 'save-article-group ogroup))))
6840            (case-fold-search t)
6841            (buf (current-buffer))
6842            dig to-address)
6843       (save-excursion
6844         (set-buffer gnus-original-article-buffer)
6845         ;; Have the digest group inherit the main mail address of
6846         ;; the parent article.
6847         (when (setq to-address (or (message-fetch-field "reply-to")
6848                                    (message-fetch-field "from")))
6849           (setq params (append (list (cons 'to-address to-address)))))
6850         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6851         (insert-buffer-substring gnus-original-article-buffer)
6852         ;; Remove lines that may lead nndoc to misinterpret the
6853         ;; document type.
6854         (narrow-to-region
6855          (goto-char (point-min))
6856          (or (search-forward "\n\n" nil t) (point)))
6857         (goto-char (point-min))
6858         (delete-matching-lines "^Path:\\|^From ")
6859         (widen))
6860       (unwind-protect
6861           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6862                     (gnus-newsgroup-ephemeral-ignored-charsets
6863                      gnus-newsgroup-ignored-charsets))
6864                 (gnus-group-read-ephemeral-group
6865                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6866                               (nndoc-article-type
6867                                ,(if force 'mbox 'guess))) t))
6868               ;; Make all postings to this group go to the parent group.
6869               (nconc (gnus-info-params (gnus-get-info name))
6870                      params)
6871             ;; Couldn't select this doc group.
6872             (switch-to-buffer buf)
6873             (gnus-set-global-variables)
6874             (gnus-configure-windows 'summary)
6875             (gnus-message 3 "Article couldn't be entered?"))
6876         (kill-buffer dig)))))
6877
6878 (defun gnus-summary-read-document (n)
6879   "Open a new group based on the current article(s).
6880 This will allow you to read digests and other similar
6881 documents as newsgroups.
6882 Obeys the standard process/prefix convention."
6883   (interactive "P")
6884   (let* ((articles (gnus-summary-work-articles n))
6885          (ogroup gnus-newsgroup-name)
6886          (params (append (gnus-info-params (gnus-get-info ogroup))
6887                          (list (cons 'to-group ogroup))))
6888          article group egroup groups vgroup)
6889     (while (setq article (pop articles))
6890       (setq group (format "%s-%d" gnus-newsgroup-name article))
6891       (gnus-summary-remove-process-mark article)
6892       (when (gnus-summary-display-article article)
6893         (save-excursion
6894           (with-temp-buffer
6895             (insert-buffer-substring gnus-original-article-buffer)
6896             ;; Remove some headers that may lead nndoc to make
6897             ;; the wrong guess.
6898             (message-narrow-to-head)
6899             (goto-char (point-min))
6900             (delete-matching-lines "^\\(Path\\):\\|^From ")
6901             (widen)
6902             (if (setq egroup
6903                       (gnus-group-read-ephemeral-group
6904                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6905                                      (nndoc-article-type guess))
6906                        t nil t))
6907                 (progn
6908                   ;; Make all postings to this group go to the parent group.
6909                   (nconc (gnus-info-params (gnus-get-info egroup))
6910                          params)
6911                   (push egroup groups))
6912               ;; Couldn't select this doc group.
6913               (gnus-error 3 "Article couldn't be entered"))))))
6914     ;; Now we have selected all the documents.
6915     (cond
6916      ((not groups)
6917       (error "None of the articles could be interpreted as documents"))
6918      ((gnus-group-read-ephemeral-group
6919        (setq vgroup (format
6920                      "nnvirtual:%s-%s" gnus-newsgroup-name
6921                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6922        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6923        t
6924        (cons (current-buffer) 'summary)))
6925      (t
6926       (error "Couldn't select virtual nndoc group")))))
6927
6928 (defun gnus-summary-isearch-article (&optional regexp-p)
6929   "Do incremental search forward on the current article.
6930 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6931   (interactive "P")
6932   (gnus-summary-select-article)
6933   (gnus-configure-windows 'article)
6934   (gnus-eval-in-buffer-window gnus-article-buffer
6935     (save-restriction
6936       (widen)
6937       (isearch-forward regexp-p))))
6938
6939 (defun gnus-summary-search-article-forward (regexp &optional backward)
6940   "Search for an article containing REGEXP forward.
6941 If BACKWARD, search backward instead."
6942   (interactive
6943    (list (read-string
6944           (format "Search article %s (regexp%s): "
6945                   (if current-prefix-arg "backward" "forward")
6946                   (if gnus-last-search-regexp
6947                       (concat ", default " gnus-last-search-regexp)
6948                     "")))
6949          current-prefix-arg))
6950   (if (string-equal regexp "")
6951       (setq regexp (or gnus-last-search-regexp ""))
6952     (setq gnus-last-search-regexp regexp))
6953   (if (gnus-summary-search-article regexp backward)
6954       (gnus-summary-show-thread)
6955     (error "Search failed: \"%s\"" regexp)))
6956
6957 (defun gnus-summary-search-article-backward (regexp)
6958   "Search for an article containing REGEXP backward."
6959   (interactive
6960    (list (read-string
6961           (format "Search article backward (regexp%s): "
6962                   (if gnus-last-search-regexp
6963                       (concat ", default " gnus-last-search-regexp)
6964                     "")))))
6965   (gnus-summary-search-article-forward regexp 'backward))
6966
6967 (defun gnus-summary-search-article (regexp &optional backward)
6968   "Search for an article containing REGEXP.
6969 Optional argument BACKWARD means do search for backward.
6970 `gnus-select-article-hook' is not called during the search."
6971   ;; We have to require this here to make sure that the following
6972   ;; dynamic binding isn't shadowed by autoloading.
6973   (require 'gnus-async)
6974   (require 'gnus-art)
6975   (let ((gnus-select-article-hook nil)  ;Disable hook.
6976         (gnus-article-prepare-hook nil)
6977         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6978         (gnus-use-article-prefetch nil)
6979         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6980         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6981         (sum (current-buffer))
6982         (gnus-display-mime-function nil)
6983         (found nil)
6984         point)
6985     (gnus-save-hidden-threads
6986       (gnus-summary-select-article)
6987       (set-buffer gnus-article-buffer)
6988       (goto-char (window-point (get-buffer-window (current-buffer))))
6989       (when backward
6990         (forward-line -1))
6991       (while (not found)
6992         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6993         (if (if backward
6994                 (re-search-backward regexp nil t)
6995               (re-search-forward regexp nil t))
6996             ;; We found the regexp.
6997             (progn
6998               (setq found 'found)
6999               (beginning-of-line)
7000               (set-window-start
7001                (get-buffer-window (current-buffer))
7002                (point))
7003               (forward-line 1)
7004               (set-window-point
7005                (get-buffer-window (current-buffer))
7006                (point))
7007               (set-buffer sum)
7008               (setq point (point)))
7009           ;; We didn't find it, so we go to the next article.
7010           (set-buffer sum)
7011           (setq found 'not)
7012           (while (eq found 'not)
7013             (if (not (if backward (gnus-summary-find-prev)
7014                        (gnus-summary-find-next)))
7015                 ;; No more articles.
7016                 (setq found t)
7017               ;; Select the next article and adjust point.
7018               (unless (gnus-summary-article-sparse-p
7019                        (gnus-summary-article-number))
7020                 (setq found nil)
7021                 (gnus-summary-select-article)
7022                 (set-buffer gnus-article-buffer)
7023                 (widen)
7024                 (goto-char (if backward (point-max) (point-min))))))))
7025       (gnus-message 7 ""))
7026     ;; Return whether we found the regexp.
7027     (when (eq found 'found)
7028       (goto-char point)
7029       (gnus-summary-show-thread)
7030       (gnus-summary-goto-subject gnus-current-article)
7031       (gnus-summary-position-point)
7032       t)))
7033
7034 (defun gnus-summary-find-matching (header regexp &optional backward unread
7035                                           not-case-fold)
7036   "Return a list of all articles that match REGEXP on HEADER.
7037 The search stars on the current article and goes forwards unless
7038 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7039 If UNREAD is non-nil, only unread articles will
7040 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7041 in the comparisons."
7042   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7043                 (gnus-data-find-list
7044                  (gnus-summary-article-number) (gnus-data-list backward))))
7045         (case-fold-search (not not-case-fold))
7046         articles d func)
7047     (if (consp header)
7048         (if (eq (car header) 'extra)
7049             (setq func
7050                   `(lambda (h)
7051                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7052                          "")))
7053           (error "%s is an invalid header" header))
7054       (unless (fboundp (intern (concat "mail-header-" header)))
7055         (error "%s is not a valid header" header))
7056       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7057     (while data
7058       (setq d (car data))
7059       (and (or (not unread)             ; We want all articles...
7060                (gnus-data-unread-p d))  ; Or just unreads.
7061            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7062            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7063            (push (gnus-data-number d) articles)) ; Success!
7064       (setq data (cdr data)))
7065     (nreverse articles)))
7066
7067 (defun gnus-summary-execute-command (header regexp command &optional backward)
7068   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7069 If HEADER is an empty string (or nil), the match is done on the entire
7070 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7071   (interactive
7072    (list (let ((completion-ignore-case t))
7073            (completing-read
7074             "Header name: "
7075             (mapcar (lambda (string) (list string))
7076                     '("Number" "Subject" "From" "Lines" "Date"
7077                       "Message-ID" "Xref" "References" "Body"))
7078             nil 'require-match))
7079          (read-string "Regexp: ")
7080          (read-key-sequence "Command: ")
7081          current-prefix-arg))
7082   (when (equal header "Body")
7083     (setq header ""))
7084   ;; Hidden thread subtrees must be searched as well.
7085   (gnus-summary-show-all-threads)
7086   ;; We don't want to change current point nor window configuration.
7087   (save-excursion
7088     (save-window-excursion
7089       (gnus-message 6 "Executing %s..." (key-description command))
7090       ;; We'd like to execute COMMAND interactively so as to give arguments.
7091       (gnus-execute header regexp
7092                     `(call-interactively ',(key-binding command))
7093                     backward)
7094       (gnus-message 6 "Executing %s...done" (key-description command)))))
7095
7096 (defun gnus-summary-beginning-of-article ()
7097   "Scroll the article back to the beginning."
7098   (interactive)
7099   (gnus-summary-select-article)
7100   (gnus-configure-windows 'article)
7101   (gnus-eval-in-buffer-window gnus-article-buffer
7102     (widen)
7103     (goto-char (point-min))
7104     (when gnus-page-broken
7105       (gnus-narrow-to-page))))
7106
7107 (defun gnus-summary-end-of-article ()
7108   "Scroll to the end of the article."
7109   (interactive)
7110   (gnus-summary-select-article)
7111   (gnus-configure-windows 'article)
7112   (gnus-eval-in-buffer-window gnus-article-buffer
7113     (widen)
7114     (goto-char (point-max))
7115     (recenter -3)
7116     (when gnus-page-broken
7117       (gnus-narrow-to-page))))
7118
7119 (defun gnus-summary-print-article (&optional filename n)
7120   "Generate and print a PostScript image of the N next (mail) articles.
7121
7122 If N is negative, print the N previous articles.  If N is nil and articles
7123 have been marked with the process mark, print these instead.
7124
7125 If the optional first argument FILENAME is nil, send the image to the
7126 printer.  If FILENAME is a string, save the PostScript image in a file with
7127 that name.  If FILENAME is a number, prompt the user for the name of the file
7128 to save in."
7129   (interactive (list (ps-print-preprint current-prefix-arg)
7130                      current-prefix-arg))
7131   (dolist (article (gnus-summary-work-articles n))
7132     (gnus-summary-select-article nil nil 'pseudo article)
7133     (gnus-eval-in-buffer-window gnus-article-buffer
7134       (let ((buffer (generate-new-buffer " *print*")))
7135         (unwind-protect
7136             (progn
7137               (copy-to-buffer buffer (point-min) (point-max))
7138               (set-buffer buffer)
7139               (gnus-article-delete-invisible-text)
7140               (let ((ps-left-header
7141                      (list
7142                       (concat "("
7143                               (mail-header-subject gnus-current-headers) ")")
7144                       (concat "("
7145                               (mail-header-from gnus-current-headers) ")")))
7146                     (ps-right-header
7147                      (list
7148                       "/pagenumberstring load"
7149                       (concat "("
7150                               (mail-header-date gnus-current-headers) ")"))))
7151                 (gnus-run-hooks 'gnus-ps-print-hook)
7152                 (save-excursion
7153                   (ps-print-buffer-with-faces filename))))
7154           (kill-buffer buffer))))))
7155
7156 (defun gnus-summary-show-article (&optional arg)
7157   "Force re-fetching of the current article.
7158 If ARG (the prefix) is a number, show the article with the charset 
7159 defined in `gnus-summary-show-article-charset-alist', or the charset
7160 inputed.
7161 If ARG (the prefix) is non-nil and not a number, show the raw article 
7162 without any article massaging functions being run."
7163   (interactive "P")
7164   (cond 
7165    ((numberp arg)
7166     (let ((gnus-newsgroup-charset 
7167            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7168                (read-coding-system "Charset: ")))
7169           (gnus-newsgroup-ignored-charsets 'gnus-all))
7170       (gnus-summary-select-article nil 'force)))
7171    ((not arg)
7172     ;; Select the article the normal way.
7173     (gnus-summary-select-article nil 'force))
7174    (t
7175     ;; We have to require this here to make sure that the following
7176     ;; dynamic binding isn't shadowed by autoloading.
7177     (require 'gnus-async)
7178     (require 'gnus-art)
7179     ;; Bind the article treatment functions to nil.
7180     (let ((gnus-have-all-headers t)
7181           gnus-article-prepare-hook
7182           gnus-article-decode-hook
7183           gnus-display-mime-function
7184           gnus-break-pages)
7185       ;; Destroy any MIME parts.
7186       (when (gnus-buffer-live-p gnus-article-buffer)
7187         (save-excursion
7188           (set-buffer gnus-article-buffer)
7189           (mm-destroy-parts gnus-article-mime-handles)))
7190       (gnus-summary-select-article nil 'force))))
7191   (gnus-summary-goto-subject gnus-current-article)
7192   (gnus-summary-position-point))
7193
7194 (defun gnus-summary-verbose-headers (&optional arg)
7195   "Toggle permanent full header display.
7196 If ARG is a positive number, turn header display on.
7197 If ARG is a negative number, turn header display off."
7198   (interactive "P")
7199   (setq gnus-show-all-headers
7200         (cond ((or (not (numberp arg))
7201                    (zerop arg))
7202                (not gnus-show-all-headers))
7203               ((natnump arg)
7204                t)))
7205   (gnus-summary-show-article))
7206
7207 (defun gnus-summary-toggle-header (&optional arg)
7208   "Show the headers if they are hidden, or hide them if they are shown.
7209 If ARG is a positive number, show the entire header.
7210 If ARG is a negative number, hide the unwanted header lines."
7211   (interactive "P")
7212   (save-excursion
7213     (set-buffer gnus-article-buffer)
7214     (save-restriction
7215       (let* ((buffer-read-only nil)
7216              (inhibit-point-motion-hooks t)
7217              hidden e)
7218         (setq hidden
7219               (if (numberp arg)
7220                   (>= arg 0)
7221                 (save-restriction 
7222                   (article-narrow-to-head)
7223                   (gnus-article-hidden-text-p 'headers))))
7224         (goto-char (point-min))
7225         (when (search-forward "\n\n" nil t)
7226           (delete-region (point-min) (1- (point))))
7227         (goto-char (point-min))
7228         (save-excursion
7229           (set-buffer gnus-original-article-buffer)
7230           (goto-char (point-min))
7231           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7232         (insert-buffer-substring gnus-original-article-buffer 1 e)
7233         (save-restriction
7234           (narrow-to-region (point-min) (point))
7235           (article-decode-encoded-words)
7236           (if  hidden
7237               (let ((gnus-treat-hide-headers nil)
7238                     (gnus-treat-hide-boring-headers nil))
7239                 (setq gnus-article-wash-types
7240                       (delq 'headers gnus-article-wash-types))
7241                 (gnus-treat-article 'head))
7242             (gnus-treat-article 'head)))
7243         (gnus-set-mode-line 'article)))))
7244
7245 (defun gnus-summary-show-all-headers ()
7246   "Make all header lines visible."
7247   (interactive)
7248   (gnus-article-show-all-headers))
7249
7250 (defun gnus-summary-caesar-message (&optional arg)
7251   "Caesar rotate the current article by 13.
7252 The numerical prefix specifies how many places to rotate each letter
7253 forward."
7254   (interactive "P")
7255   (gnus-summary-select-article)
7256   (let ((mail-header-separator ""))
7257     (gnus-eval-in-buffer-window gnus-article-buffer
7258       (save-restriction
7259         (widen)
7260         (let ((start (window-start))
7261               buffer-read-only)
7262           (message-caesar-buffer-body arg)
7263           (set-window-start (get-buffer-window (current-buffer)) start))))))
7264
7265 (defun gnus-summary-stop-page-breaking ()
7266   "Stop page breaking in the current article."
7267   (interactive)
7268   (gnus-summary-select-article)
7269   (gnus-eval-in-buffer-window gnus-article-buffer
7270     (widen)
7271     (when (gnus-visual-p 'page-marker)
7272       (let ((buffer-read-only nil))
7273         (gnus-remove-text-with-property 'gnus-prev)
7274         (gnus-remove-text-with-property 'gnus-next))
7275       (setq gnus-page-broken nil))))
7276
7277 (defun gnus-summary-move-article (&optional n to-newsgroup
7278                                             select-method action)
7279   "Move the current article to a different newsgroup.
7280 If N is a positive number, move the N next articles.
7281 If N is a negative number, move the N previous articles.
7282 If N is nil and any articles have been marked with the process mark,
7283 move those articles instead.
7284 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7285 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7286 re-spool using this method.
7287
7288 For this function to work, both the current newsgroup and the
7289 newsgroup that you want to move to have to support the `request-move'
7290 and `request-accept' functions.
7291
7292 ACTION can be either `move' (the default), `crosspost' or `copy'."
7293   (interactive "P")
7294   (unless action
7295     (setq action 'move))
7296   ;; Disable marking as read.
7297   (let (gnus-mark-article-hook)
7298     (save-window-excursion
7299       (gnus-summary-select-article)))
7300   ;; Check whether the source group supports the required functions.
7301   (cond ((and (eq action 'move)
7302               (not (gnus-check-backend-function
7303                     'request-move-article gnus-newsgroup-name)))
7304          (error "The current group does not support article moving"))
7305         ((and (eq action 'crosspost)
7306               (not (gnus-check-backend-function
7307                     'request-replace-article gnus-newsgroup-name)))
7308          (error "The current group does not support article editing")))
7309   (let ((articles (gnus-summary-work-articles n))
7310         (prefix (if (gnus-check-backend-function
7311                     'request-move-article gnus-newsgroup-name)
7312                     (gnus-group-real-prefix gnus-newsgroup-name)
7313                   ""))
7314         (names '((move "Move" "Moving")
7315                  (copy "Copy" "Copying")
7316                  (crosspost "Crosspost" "Crossposting")))
7317         (copy-buf (save-excursion
7318                     (nnheader-set-temp-buffer " *copy article*")))
7319         art-group to-method new-xref article to-groups)
7320     (unless (assq action names)
7321       (error "Unknown action %s" action))
7322     ;; Read the newsgroup name.
7323     (when (and (not to-newsgroup)
7324                (not select-method))
7325       (setq to-newsgroup
7326             (gnus-read-move-group-name
7327              (cadr (assq action names))
7328              (symbol-value (intern (format "gnus-current-%s-group" action)))
7329              articles prefix))
7330       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7331     (setq to-method (or select-method
7332                         (gnus-group-name-to-method to-newsgroup)))
7333     ;; Check the method we are to move this article to...
7334     (unless (gnus-check-backend-function
7335              'request-accept-article (car to-method))
7336       (error "%s does not support article copying" (car to-method)))
7337     (unless (gnus-check-server to-method)
7338       (error "Can't open server %s" (car to-method)))
7339     (gnus-message 6 "%s to %s: %s..."
7340                   (caddr (assq action names))
7341                   (or (car select-method) to-newsgroup) articles)
7342     (while articles
7343       (setq article (pop articles))
7344       (setq
7345        art-group
7346        (cond
7347         ;; Move the article.
7348         ((eq action 'move)
7349          ;; Remove this article from future suppression.
7350          (gnus-dup-unsuppress-article article)
7351          (gnus-request-move-article
7352           article                       ; Article to move
7353           gnus-newsgroup-name           ; From newsgroup
7354           (nth 1 (gnus-find-method-for-group
7355                   gnus-newsgroup-name)) ; Server
7356           (list 'gnus-request-accept-article
7357                 to-newsgroup (list 'quote select-method)
7358                 (not articles) t)       ; Accept form
7359           (not articles)))              ; Only save nov last time
7360         ;; Copy the article.
7361         ((eq action 'copy)
7362          (save-excursion
7363            (set-buffer copy-buf)
7364            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7365              (gnus-request-accept-article
7366               to-newsgroup select-method (not articles) t))))
7367         ;; Crosspost the article.
7368         ((eq action 'crosspost)
7369          (let ((xref (message-tokenize-header
7370                       (mail-header-xref (gnus-summary-article-header article))
7371                       " ")))
7372            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7373                                   ":" article))
7374            (unless xref
7375              (setq xref (list (system-name))))
7376            (setq new-xref
7377                  (concat
7378                   (mapconcat 'identity
7379                              (delete "Xref:" (delete new-xref xref))
7380                              " ")
7381                   " " new-xref))
7382            (save-excursion
7383              (set-buffer copy-buf)
7384              ;; First put the article in the destination group.
7385              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7386              (when (consp (setq art-group
7387                                 (gnus-request-accept-article
7388                                  to-newsgroup select-method (not articles))))
7389                (setq new-xref (concat new-xref " " (car art-group)
7390                                       ":" (cdr art-group)))
7391                ;; Now we have the new Xrefs header, so we insert
7392                ;; it and replace the new article.
7393                (nnheader-replace-header "Xref" new-xref)
7394                (gnus-request-replace-article
7395                 (cdr art-group) to-newsgroup (current-buffer))
7396                art-group))))))
7397       (cond
7398        ((not art-group)
7399         (gnus-message 1 "Couldn't %s article %s: %s"
7400                       (cadr (assq action names)) article
7401                       (nnheader-get-report (car to-method))))
7402        ((eq art-group 'junk)
7403         (when (eq action 'move)
7404           (gnus-summary-mark-article article gnus-canceled-mark)
7405           (gnus-message 4 "Deleted article %s" article)))
7406        (t
7407         (let* ((pto-group (gnus-group-prefixed-name
7408                            (car art-group) to-method))
7409                (entry
7410                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7411                (info (nth 2 entry))
7412                (to-group (gnus-info-group info))
7413                to-marks)
7414           ;; Update the group that has been moved to.
7415           (when (and info
7416                      (memq action '(move copy)))
7417             (unless (member to-group to-groups)
7418               (push to-group to-groups))
7419
7420             (unless (memq article gnus-newsgroup-unreads)
7421               (push 'read to-marks)
7422               (gnus-info-set-read
7423                info (gnus-add-to-range (gnus-info-read info)
7424                                        (list (cdr art-group)))))
7425
7426             ;; See whether the article is to be put in the cache.
7427             (let ((marks gnus-article-mark-lists)
7428                   (to-article (cdr art-group)))
7429
7430               ;; Enter the article into the cache in the new group,
7431               ;; if that is required.
7432               (when gnus-use-cache
7433                 (gnus-cache-possibly-enter-article
7434                  to-group to-article
7435                  (memq article gnus-newsgroup-marked)
7436                  (memq article gnus-newsgroup-dormant)
7437                  (memq article gnus-newsgroup-unreads)))
7438
7439               (when gnus-preserve-marks 
7440                 ;; Copy any marks over to the new group.
7441                 (when (and (equal to-group gnus-newsgroup-name)
7442                            (not (memq article gnus-newsgroup-unreads)))
7443                   ;; Mark this article as read in this group.
7444                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7445                   (setcdr (gnus-active to-group) to-article)
7446                   (setcdr gnus-newsgroup-active to-article))
7447
7448                 (while marks
7449                   (when (memq article (symbol-value
7450                                        (intern (format "gnus-newsgroup-%s"
7451                                                        (caar marks)))))
7452                     (push (cdar marks) to-marks)
7453                     ;; If the other group is the same as this group,
7454                     ;; then we have to add the mark to the list.
7455                     (when (equal to-group gnus-newsgroup-name)
7456                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7457                            (cons to-article
7458                                  (symbol-value
7459                                   (intern (format "gnus-newsgroup-%s"
7460                                                   (caar marks)))))))
7461                     ;; Copy the marks to other group.
7462                     (gnus-add-marked-articles
7463                      to-group (cdar marks) (list to-article) info))
7464                   (setq marks (cdr marks)))
7465
7466                 (gnus-request-set-mark to-group (list (list (list to-article)
7467                                                             'set
7468                                                             to-marks))))
7469
7470               (gnus-dribble-enter
7471                (concat "(gnus-group-set-info '"
7472                        (gnus-prin1-to-string (gnus-get-info to-group))
7473                        ")"))))
7474
7475           ;; Update the Xref header in this article to point to
7476           ;; the new crossposted article we have just created.
7477           (when (eq action 'crosspost)
7478             (save-excursion
7479               (set-buffer copy-buf)
7480               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7481               (nnheader-replace-header "Xref" new-xref)
7482               (gnus-request-replace-article
7483                article gnus-newsgroup-name (current-buffer)))))
7484
7485         ;;;!!!Why is this necessary?
7486         (set-buffer gnus-summary-buffer)
7487
7488         (gnus-summary-goto-subject article)
7489         (when (eq action 'move)
7490           (gnus-summary-mark-article article gnus-canceled-mark))))
7491       (gnus-summary-remove-process-mark article))
7492     ;; Re-activate all groups that have been moved to.
7493     (while to-groups
7494       (save-excursion
7495         (set-buffer gnus-group-buffer)
7496         (when (gnus-group-goto-group (car to-groups) t)
7497           (gnus-group-get-new-news-this-group 1 t))
7498         (pop to-groups)))
7499
7500     (gnus-kill-buffer copy-buf)
7501     (gnus-summary-position-point)
7502     (gnus-set-mode-line 'summary)))
7503
7504 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7505   "Move the current article to a different newsgroup.
7506 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7507 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7508 re-spool using this method."
7509   (interactive "P")
7510   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7511
7512 (defun gnus-summary-crosspost-article (&optional n)
7513   "Crosspost the current article to some other group."
7514   (interactive "P")
7515   (gnus-summary-move-article n nil nil 'crosspost))
7516
7517 (defcustom gnus-summary-respool-default-method nil
7518   "Default method for respooling an article.
7519 If nil, use to the current newsgroup method."
7520   :type '(choice (gnus-select-method :value (nnml ""))
7521                  (const nil))
7522   :group 'gnus-summary-mail)
7523
7524 (defun gnus-summary-respool-article (&optional n method)
7525   "Respool the current article.
7526 The article will be squeezed through the mail spooling process again,
7527 which means that it will be put in some mail newsgroup or other
7528 depending on `nnmail-split-methods'.
7529 If N is a positive number, respool the N next articles.
7530 If N is a negative number, respool the N previous articles.
7531 If N is nil and any articles have been marked with the process mark,
7532 respool those articles instead.
7533
7534 Respooling can be done both from mail groups and \"real\" newsgroups.
7535 In the former case, the articles in question will be moved from the
7536 current group into whatever groups they are destined to.  In the
7537 latter case, they will be copied into the relevant groups."
7538   (interactive
7539    (list current-prefix-arg
7540          (let* ((methods (gnus-methods-using 'respool))
7541                 (methname
7542                  (symbol-name (or gnus-summary-respool-default-method
7543                                   (car (gnus-find-method-for-group
7544                                         gnus-newsgroup-name)))))
7545                 (method
7546                  (gnus-completing-read
7547                   methname "What backend do you want to use when respooling?"
7548                   methods nil t nil 'gnus-mail-method-history))
7549                 ms)
7550            (cond
7551             ((zerop (length (setq ms (gnus-servers-using-backend
7552                                       (intern method)))))
7553              (list (intern method) ""))
7554             ((= 1 (length ms))
7555              (car ms))
7556             (t
7557              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7558                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7559                            ms-alist))))))))
7560   (unless method
7561     (error "No method given for respooling"))
7562   (if (assoc (symbol-name
7563               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7564              (gnus-methods-using 'respool))
7565       (gnus-summary-move-article n nil method)
7566     (gnus-summary-copy-article n nil method)))
7567
7568 (defun gnus-summary-import-article (file)
7569   "Import an arbitrary file into a mail newsgroup."
7570   (interactive "fImport file: ")
7571   (let ((group gnus-newsgroup-name)
7572         (now (current-time))
7573         atts lines)
7574     (unless (gnus-check-backend-function 'request-accept-article group)
7575       (error "%s does not support article importing" group))
7576     (or (file-readable-p file)
7577         (not (file-regular-p file))
7578         (error "Can't read %s" file))
7579     (save-excursion
7580       (set-buffer (gnus-get-buffer-create " *import file*"))
7581       (erase-buffer)
7582       (insert-file-contents file)
7583       (goto-char (point-min))
7584       (unless (nnheader-article-p)
7585         ;; This doesn't look like an article, so we fudge some headers.
7586         (setq atts (file-attributes file)
7587               lines (count-lines (point-min) (point-max)))
7588         (insert "From: " (read-string "From: ") "\n"
7589                 "Subject: " (read-string "Subject: ") "\n"
7590                 "Date: " (message-make-date (nth 5 atts))
7591                 "\n"
7592                 "Message-ID: " (message-make-message-id) "\n"
7593                 "Lines: " (int-to-string lines) "\n"
7594                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7595       (gnus-request-accept-article group nil t)
7596       (kill-buffer (current-buffer)))))
7597
7598 (defun gnus-summary-article-posted-p ()
7599   "Say whether the current (mail) article is available from news as well.
7600 This will be the case if the article has both been mailed and posted."
7601   (interactive)
7602   (let ((id (mail-header-references (gnus-summary-article-header)))
7603         (gnus-override-method (car (gnus-refer-article-methods))))
7604     (if (gnus-request-head id "")
7605         (gnus-message 2 "The current message was found on %s"
7606                       gnus-override-method)
7607       (gnus-message 2 "The current message couldn't be found on %s"
7608                     gnus-override-method)
7609       nil)))
7610
7611 (defun gnus-summary-expire-articles (&optional now)
7612   "Expire all articles that are marked as expirable in the current group."
7613   (interactive)
7614   (when (gnus-check-backend-function
7615          'request-expire-articles gnus-newsgroup-name)
7616     ;; This backend supports expiry.
7617     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7618            (expirable (if total
7619                           (progn
7620                             ;; We need to update the info for
7621                             ;; this group for `gnus-list-of-read-articles'
7622                             ;; to give us the right answer.
7623                             (gnus-run-hooks 'gnus-exit-group-hook)
7624                             (gnus-summary-update-info)
7625                             (gnus-list-of-read-articles gnus-newsgroup-name))
7626                         (setq gnus-newsgroup-expirable
7627                               (sort gnus-newsgroup-expirable '<))))
7628            (expiry-wait (if now 'immediate
7629                           (gnus-group-find-parameter
7630                            gnus-newsgroup-name 'expiry-wait)))
7631            es)
7632       (when expirable
7633         ;; There are expirable articles in this group, so we run them
7634         ;; through the expiry process.
7635         (gnus-message 6 "Expiring articles...")
7636         (unless (gnus-check-group gnus-newsgroup-name)
7637           (error "Can't open server for %s" gnus-newsgroup-name))
7638         ;; The list of articles that weren't expired is returned.
7639         (save-excursion
7640           (if expiry-wait
7641               (let ((nnmail-expiry-wait-function nil)
7642                     (nnmail-expiry-wait expiry-wait))
7643                 (setq es (gnus-request-expire-articles
7644                           expirable gnus-newsgroup-name)))
7645             (setq es (gnus-request-expire-articles
7646                       expirable gnus-newsgroup-name))))
7647         (unless total
7648           (setq gnus-newsgroup-expirable es))
7649         ;; We go through the old list of expirable, and mark all
7650         ;; really expired articles as nonexistent.
7651         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7652           (let ((gnus-use-cache nil))
7653             (while expirable
7654               (unless (memq (car expirable) es)
7655                 (when (gnus-data-find (car expirable))
7656                   (gnus-summary-mark-article
7657                    (car expirable) gnus-canceled-mark)))
7658               (setq expirable (cdr expirable)))))
7659         (gnus-message 6 "Expiring articles...done")))))
7660
7661 (defun gnus-summary-expire-articles-now ()
7662   "Expunge all expirable articles in the current group.
7663 This means that *all* articles that are marked as expirable will be
7664 deleted forever, right now."
7665   (interactive)
7666   (or gnus-expert-user
7667       (gnus-yes-or-no-p
7668        "Are you really, really, really sure you want to delete all these messages? ")
7669       (error "Phew!"))
7670   (gnus-summary-expire-articles t))
7671
7672 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7673 (defun gnus-summary-delete-article (&optional n)
7674   "Delete the N next (mail) articles.
7675 This command actually deletes articles.  This is not a marking
7676 command.  The article will disappear forever from your life, never to
7677 return.
7678 If N is negative, delete backwards.
7679 If N is nil and articles have been marked with the process mark,
7680 delete these instead."
7681   (interactive "P")
7682   (unless (gnus-check-backend-function 'request-expire-articles
7683                                        gnus-newsgroup-name)
7684     (error "The current newsgroup does not support article deletion"))
7685   ;; Compute the list of articles to delete.
7686   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7687         not-deleted)
7688     (if (and gnus-novice-user
7689              (not (gnus-yes-or-no-p
7690                    (format "Do you really want to delete %s forever? "
7691                            (if (> (length articles) 1)
7692                                (format "these %s articles" (length articles))
7693                              "this article")))))
7694         ()
7695       ;; Delete the articles.
7696       (setq not-deleted (gnus-request-expire-articles
7697                          articles gnus-newsgroup-name 'force))
7698       (while articles
7699         (gnus-summary-remove-process-mark (car articles))
7700         ;; The backend might not have been able to delete the article
7701         ;; after all.
7702         (unless (memq (car articles) not-deleted)
7703           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7704         (setq articles (cdr articles)))
7705       (when not-deleted
7706         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7707     (gnus-summary-position-point)
7708     (gnus-set-mode-line 'summary)
7709     not-deleted))
7710
7711 (defun gnus-summary-edit-article (&optional force)
7712   "Edit the current article.
7713 This will have permanent effect only in mail groups.
7714 If FORCE is non-nil, allow editing of articles even in read-only
7715 groups."
7716   (interactive "P")
7717   (save-excursion
7718     (set-buffer gnus-summary-buffer)
7719     (let ((mail-parse-charset gnus-newsgroup-charset)
7720           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7721       (gnus-set-global-variables)
7722       (when (and (not force)
7723                  (gnus-group-read-only-p))
7724         (error "The current newsgroup does not support article editing"))
7725       (gnus-summary-show-article t)
7726       (gnus-article-edit-article
7727        'mime-to-mml
7728        `(lambda (no-highlight)
7729           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7730                 (mail-parse-ignored-charsets 
7731                  ',gnus-newsgroup-ignored-charsets))
7732             (mml-to-mime)
7733             (gnus-summary-edit-article-done
7734              ,(or (mail-header-references gnus-current-headers) "")
7735              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7736
7737 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7738
7739 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7740                                                  no-highlight)
7741   "Make edits to the current article permanent."
7742   (interactive)
7743   ;; Replace the article.
7744   (let ((buf (current-buffer)))
7745     (with-temp-buffer
7746       (insert-buffer-substring buf)
7747       (if (and (not read-only)
7748                (not (gnus-request-replace-article
7749                      (cdr gnus-article-current) (car gnus-article-current)
7750                      (current-buffer) t)))
7751           (error "Couldn't replace article")
7752         ;; Update the summary buffer.
7753         (if (and references
7754                  (equal (message-tokenize-header references " ")
7755                         (message-tokenize-header
7756                          (or (message-fetch-field "references") "") " ")))
7757             ;; We only have to update this line.
7758             (save-excursion
7759               (save-restriction
7760                 (message-narrow-to-head)
7761                 (let ((head (buffer-string))
7762                       header)
7763                   (with-temp-buffer
7764                     (insert (format "211 %d Article retrieved.\n"
7765                                     (cdr gnus-article-current)))
7766                     (insert head)
7767                     (insert ".\n")
7768                     (let ((nntp-server-buffer (current-buffer)))
7769                       (setq header (car (gnus-get-newsgroup-headers
7770                                          (save-excursion
7771                                            (set-buffer gnus-summary-buffer)
7772                                            gnus-newsgroup-dependencies)
7773                                          t))))
7774                     (save-excursion
7775                       (set-buffer gnus-summary-buffer)
7776                       (gnus-data-set-header
7777                        (gnus-data-find (cdr gnus-article-current))
7778                        header)
7779                       (gnus-summary-update-article-line
7780                        (cdr gnus-article-current) header))))))
7781           ;; Update threads.
7782           (set-buffer (or buffer gnus-summary-buffer))
7783           (gnus-summary-update-article (cdr gnus-article-current)))
7784         ;; Prettify the article buffer again.
7785         (unless no-highlight
7786           (save-excursion
7787             (set-buffer gnus-article-buffer)
7788             ;;;!!! Fix this -- article should be rehighlighted.
7789             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7790             (set-buffer gnus-original-article-buffer)
7791             (gnus-request-article
7792              (cdr gnus-article-current)
7793              (car gnus-article-current) (current-buffer))))
7794         ;; Prettify the summary buffer line.
7795         (when (gnus-visual-p 'summary-highlight 'highlight)
7796           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7797
7798 (defun gnus-summary-edit-wash (key)
7799   "Perform editing command KEY in the article buffer."
7800   (interactive
7801    (list
7802     (progn
7803       (message "%s" (concat (this-command-keys) "- "))
7804       (read-char))))
7805   (message "")
7806   (gnus-summary-edit-article)
7807   (execute-kbd-macro (concat (this-command-keys) key))
7808   (gnus-article-edit-done))
7809
7810 ;;; Respooling
7811
7812 (defun gnus-summary-respool-query (&optional silent trace)
7813   "Query where the respool algorithm would put this article."
7814   (interactive)
7815   (let (gnus-mark-article-hook)
7816     (gnus-summary-select-article)
7817     (save-excursion
7818       (set-buffer gnus-original-article-buffer)
7819       (save-restriction
7820         (message-narrow-to-head)
7821         (let ((groups (nnmail-article-group 'identity trace)))
7822           (unless silent
7823             (if groups
7824                 (message "This message would go to %s"
7825                          (mapconcat 'car groups ", "))
7826               (message "This message would go to no groups"))
7827             groups))))))
7828
7829 (defun gnus-summary-respool-trace ()
7830   "Trace where the respool algorithm would put this article.
7831 Display a buffer showing all fancy splitting patterns which matched."
7832   (interactive)
7833   (gnus-summary-respool-query nil t))
7834
7835 ;; Summary marking commands.
7836
7837 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7838   "Mark articles which has the same subject as read, and then select the next.
7839 If UNMARK is positive, remove any kind of mark.
7840 If UNMARK is negative, tick articles."
7841   (interactive "P")
7842   (when unmark
7843     (setq unmark (prefix-numeric-value unmark)))
7844   (let ((count
7845          (gnus-summary-mark-same-subject
7846           (gnus-summary-article-subject) unmark)))
7847     ;; Select next unread article.  If auto-select-same mode, should
7848     ;; select the first unread article.
7849     (gnus-summary-next-article t (and gnus-auto-select-same
7850                                       (gnus-summary-article-subject)))
7851     (gnus-message 7 "%d article%s marked as %s"
7852                   count (if (= count 1) " is" "s are")
7853                   (if unmark "unread" "read"))))
7854
7855 (defun gnus-summary-kill-same-subject (&optional unmark)
7856   "Mark articles which has the same subject as read.
7857 If UNMARK is positive, remove any kind of mark.
7858 If UNMARK is negative, tick articles."
7859   (interactive "P")
7860   (when unmark
7861     (setq unmark (prefix-numeric-value unmark)))
7862   (let ((count
7863          (gnus-summary-mark-same-subject
7864           (gnus-summary-article-subject) unmark)))
7865     ;; If marked as read, go to next unread subject.
7866     (when (null unmark)
7867       ;; Go to next unread subject.
7868       (gnus-summary-next-subject 1 t))
7869     (gnus-message 7 "%d articles are marked as %s"
7870                   count (if unmark "unread" "read"))))
7871
7872 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7873   "Mark articles with same SUBJECT as read, and return marked number.
7874 If optional argument UNMARK is positive, remove any kinds of marks.
7875 If optional argument UNMARK is negative, mark articles as unread instead."
7876   (let ((count 1))
7877     (save-excursion
7878       (cond
7879        ((null unmark)                   ; Mark as read.
7880         (while (and
7881                 (progn
7882                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7883                   (gnus-summary-show-thread) t)
7884                 (gnus-summary-find-subject subject))
7885           (setq count (1+ count))))
7886        ((> unmark 0)                    ; Tick.
7887         (while (and
7888                 (progn
7889                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7890                   (gnus-summary-show-thread) t)
7891                 (gnus-summary-find-subject subject))
7892           (setq count (1+ count))))
7893        (t                               ; Mark as unread.
7894         (while (and
7895                 (progn
7896                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7897                   (gnus-summary-show-thread) t)
7898                 (gnus-summary-find-subject subject))
7899           (setq count (1+ count)))))
7900       (gnus-set-mode-line 'summary)
7901       ;; Return the number of marked articles.
7902       count)))
7903
7904 (defun gnus-summary-mark-as-processable (n &optional unmark)
7905   "Set the process mark on the next N articles.
7906 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7907 the process mark instead.  The difference between N and the actual
7908 number of articles marked is returned."
7909   (interactive "p")
7910   (let ((backward (< n 0))
7911         (n (abs n)))
7912     (while (and
7913             (> n 0)
7914             (if unmark
7915                 (gnus-summary-remove-process-mark
7916                  (gnus-summary-article-number))
7917               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7918             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7919       (setq n (1- n)))
7920     (when (/= 0 n)
7921       (gnus-message 7 "No more articles"))
7922     (gnus-summary-recenter)
7923     (gnus-summary-position-point)
7924     n))
7925
7926 (defun gnus-summary-unmark-as-processable (n)
7927   "Remove the process mark from the next N articles.
7928 If N is negative, unmark backward instead.  The difference between N and
7929 the actual number of articles unmarked is returned."
7930   (interactive "p")
7931   (gnus-summary-mark-as-processable n t))
7932
7933 (defun gnus-summary-unmark-all-processable ()
7934   "Remove the process mark from all articles."
7935   (interactive)
7936   (save-excursion
7937     (while gnus-newsgroup-processable
7938       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7939   (gnus-summary-position-point))
7940
7941 (defun gnus-summary-mark-as-expirable (n)
7942   "Mark N articles forward as expirable.
7943 If N is negative, mark backward instead.  The difference between N and
7944 the actual number of articles marked is returned."
7945   (interactive "p")
7946   (gnus-summary-mark-forward n gnus-expirable-mark))
7947
7948 (defun gnus-summary-mark-article-as-replied (article)
7949   "Mark ARTICLE replied and update the summary line."
7950   (push article gnus-newsgroup-replied)
7951   (let ((buffer-read-only nil))
7952     (when (gnus-summary-goto-subject article nil t)
7953       (gnus-summary-update-secondary-mark article))))
7954
7955 (defun gnus-summary-set-bookmark (article)
7956   "Set a bookmark in current article."
7957   (interactive (list (gnus-summary-article-number)))
7958   (when (or (not (get-buffer gnus-article-buffer))
7959             (not gnus-current-article)
7960             (not gnus-article-current)
7961             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7962     (error "No current article selected"))
7963   ;; Remove old bookmark, if one exists.
7964   (let ((old (assq article gnus-newsgroup-bookmarks)))
7965     (when old
7966       (setq gnus-newsgroup-bookmarks
7967             (delq old gnus-newsgroup-bookmarks))))
7968   ;; Set the new bookmark, which is on the form
7969   ;; (article-number . line-number-in-body).
7970   (push
7971    (cons article
7972          (save-excursion
7973            (set-buffer gnus-article-buffer)
7974            (count-lines
7975             (min (point)
7976                  (save-excursion
7977                    (goto-char (point-min))
7978                    (search-forward "\n\n" nil t)
7979                    (point)))
7980             (point))))
7981    gnus-newsgroup-bookmarks)
7982   (gnus-message 6 "A bookmark has been added to the current article."))
7983
7984 (defun gnus-summary-remove-bookmark (article)
7985   "Remove the bookmark from the current article."
7986   (interactive (list (gnus-summary-article-number)))
7987   ;; Remove old bookmark, if one exists.
7988   (let ((old (assq article gnus-newsgroup-bookmarks)))
7989     (if old
7990         (progn
7991           (setq gnus-newsgroup-bookmarks
7992                 (delq old gnus-newsgroup-bookmarks))
7993           (gnus-message 6 "Removed bookmark."))
7994       (gnus-message 6 "No bookmark in current article."))))
7995
7996 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7997 (defun gnus-summary-mark-as-dormant (n)
7998   "Mark N articles forward as dormant.
7999 If N is negative, mark backward instead.  The difference between N and
8000 the actual number of articles marked is returned."
8001   (interactive "p")
8002   (gnus-summary-mark-forward n gnus-dormant-mark))
8003
8004 (defun gnus-summary-set-process-mark (article)
8005   "Set the process mark on ARTICLE and update the summary line."
8006   (setq gnus-newsgroup-processable
8007         (cons article
8008               (delq article gnus-newsgroup-processable)))
8009   (when (gnus-summary-goto-subject article)
8010     (gnus-summary-show-thread)
8011     (gnus-summary-goto-subject article)
8012     (gnus-summary-update-secondary-mark article)))
8013
8014 (defun gnus-summary-remove-process-mark (article)
8015   "Remove the process mark from ARTICLE and update the summary line."
8016   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8017   (when (gnus-summary-goto-subject article)
8018     (gnus-summary-show-thread)
8019     (gnus-summary-goto-subject article)
8020     (gnus-summary-update-secondary-mark article)))
8021
8022 (defun gnus-summary-set-saved-mark (article)
8023   "Set the process mark on ARTICLE and update the summary line."
8024   (push article gnus-newsgroup-saved)
8025   (when (gnus-summary-goto-subject article)
8026     (gnus-summary-update-secondary-mark article)))
8027
8028 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8029   "Mark N articles as read forwards.
8030 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8031 The difference between N and the actual number of articles marked is
8032 returned.
8033 Iff NO-EXPIRE, auto-expiry will be inhibited."
8034   (interactive "p")
8035   (gnus-summary-show-thread)
8036   (let ((backward (< n 0))
8037         (gnus-summary-goto-unread
8038          (and gnus-summary-goto-unread
8039               (not (eq gnus-summary-goto-unread 'never))
8040               (not (memq mark (list gnus-unread-mark
8041                                     gnus-ticked-mark gnus-dormant-mark)))))
8042         (n (abs n))
8043         (mark (or mark gnus-del-mark)))
8044     (while (and (> n 0)
8045                 (gnus-summary-mark-article nil mark no-expire)
8046                 (zerop (gnus-summary-next-subject
8047                         (if backward -1 1)
8048                         (and gnus-summary-goto-unread
8049                              (not (eq gnus-summary-goto-unread 'never)))
8050                         t)))
8051       (setq n (1- n)))
8052     (when (/= 0 n)
8053       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8054     (gnus-summary-recenter)
8055     (gnus-summary-position-point)
8056     (gnus-set-mode-line 'summary)
8057     n))
8058
8059 (defun gnus-summary-mark-article-as-read (mark)
8060   "Mark the current article quickly as read with MARK."
8061   (let ((article (gnus-summary-article-number)))
8062     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8063     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8064     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8065     (push (cons article mark) gnus-newsgroup-reads)
8066     ;; Possibly remove from cache, if that is used.
8067     (when gnus-use-cache
8068       (gnus-cache-enter-remove-article article))
8069     ;; Allow the backend to change the mark.
8070     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8071     ;; Check for auto-expiry.
8072     (when (and gnus-newsgroup-auto-expire
8073                (memq mark gnus-auto-expirable-marks))
8074       (setq mark gnus-expirable-mark)
8075       ;; Let the backend know about the mark change.
8076       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8077       (push article gnus-newsgroup-expirable))
8078     ;; Set the mark in the buffer.
8079     (gnus-summary-update-mark mark 'unread)
8080     t))
8081
8082 (defun gnus-summary-mark-article-as-unread (mark)
8083   "Mark the current article quickly as unread with MARK."
8084   (let* ((article (gnus-summary-article-number))
8085          (old-mark (gnus-summary-article-mark article)))
8086     ;; Allow the backend to change the mark.
8087     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8088     (if (eq mark old-mark)
8089         t
8090       (if (<= article 0)
8091           (progn
8092             (gnus-error 1 "Can't mark negative article numbers")
8093             nil)
8094         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8095         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8096         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8097         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8098         (cond ((= mark gnus-ticked-mark)
8099                (push article gnus-newsgroup-marked))
8100               ((= mark gnus-dormant-mark)
8101                (push article gnus-newsgroup-dormant))
8102               (t
8103                (push article gnus-newsgroup-unreads)))
8104         (gnus-pull article gnus-newsgroup-reads)
8105
8106         ;; See whether the article is to be put in the cache.
8107         (and gnus-use-cache
8108              (vectorp (gnus-summary-article-header article))
8109              (save-excursion
8110                (gnus-cache-possibly-enter-article
8111                 gnus-newsgroup-name article
8112                 (= mark gnus-ticked-mark)
8113                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8114
8115         ;; Fix the mark.
8116         (gnus-summary-update-mark mark 'unread)
8117         t))))
8118
8119 (defun gnus-summary-mark-article (&optional article mark no-expire)
8120   "Mark ARTICLE with MARK.  MARK can be any character.
8121 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8122 `??' (dormant) and `?E' (expirable).
8123 If MARK is nil, then the default character `?r' is used.
8124 If ARTICLE is nil, then the article on the current line will be
8125 marked.
8126 Iff NO-EXPIRE, auto-expiry will be inhibited."
8127   ;; The mark might be a string.
8128   (when (stringp mark)
8129     (setq mark (aref mark 0)))
8130   ;; If no mark is given, then we check auto-expiring.
8131   (when (null mark)
8132     (setq mark gnus-del-mark))
8133   (when (and (not no-expire)
8134              gnus-newsgroup-auto-expire
8135              (memq mark gnus-auto-expirable-marks))
8136     (setq mark gnus-expirable-mark))
8137   (let ((article (or article (gnus-summary-article-number)))
8138         (old-mark (gnus-summary-article-mark article)))
8139     ;; Allow the backend to change the mark.
8140     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8141     (if (eq mark old-mark)
8142         t
8143       (unless article
8144         (error "No article on current line"))
8145       (if (not (if (or (= mark gnus-unread-mark)
8146                        (= mark gnus-ticked-mark)
8147                        (= mark gnus-dormant-mark))
8148                    (gnus-mark-article-as-unread article mark)
8149                  (gnus-mark-article-as-read article mark)))
8150           t
8151         ;; See whether the article is to be put in the cache.
8152         (and gnus-use-cache
8153              (not (= mark gnus-canceled-mark))
8154              (vectorp (gnus-summary-article-header article))
8155              (save-excursion
8156                (gnus-cache-possibly-enter-article
8157                 gnus-newsgroup-name article
8158                 (= mark gnus-ticked-mark)
8159                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8160
8161         (when (gnus-summary-goto-subject article nil t)
8162           (let ((buffer-read-only nil))
8163             (gnus-summary-show-thread)
8164             ;; Fix the mark.
8165             (gnus-summary-update-mark mark 'unread)
8166             t))))))
8167
8168 (defun gnus-summary-update-secondary-mark (article)
8169   "Update the secondary (read, process, cache) mark."
8170   (gnus-summary-update-mark
8171    (cond ((memq article gnus-newsgroup-processable)
8172           gnus-process-mark)
8173          ((memq article gnus-newsgroup-cached)
8174           gnus-cached-mark)
8175          ((memq article gnus-newsgroup-replied)
8176           gnus-replied-mark)
8177          ((memq article gnus-newsgroup-saved)
8178           gnus-saved-mark)
8179          (t gnus-unread-mark))
8180    'replied)
8181   (when (gnus-visual-p 'summary-highlight 'highlight)
8182     (gnus-run-hooks 'gnus-summary-update-hook))
8183   t)
8184
8185 (defun gnus-summary-update-mark (mark type)
8186   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8187         (buffer-read-only nil))
8188     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8189     (when forward
8190       (when (looking-at "\r")
8191         (incf forward))
8192       (when (<= (+ forward (point)) (point-max))
8193         ;; Go to the right position on the line.
8194         (goto-char (+ forward (point)))
8195         ;; Replace the old mark with the new mark.
8196         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8197         ;; Optionally update the marks by some user rule.
8198         (when (eq type 'unread)
8199           (gnus-data-set-mark
8200            (gnus-data-find (gnus-summary-article-number)) mark)
8201           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8202
8203 (defun gnus-mark-article-as-read (article &optional mark)
8204   "Enter ARTICLE in the pertinent lists and remove it from others."
8205   ;; Make the article expirable.
8206   (let ((mark (or mark gnus-del-mark)))
8207     (if (= mark gnus-expirable-mark)
8208         (push article gnus-newsgroup-expirable)
8209       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8210     ;; Remove from unread and marked lists.
8211     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8212     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8213     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8214     (push (cons article mark) gnus-newsgroup-reads)
8215     ;; Possibly remove from cache, if that is used.
8216     (when gnus-use-cache
8217       (gnus-cache-enter-remove-article article))
8218     t))
8219
8220 (defun gnus-mark-article-as-unread (article &optional mark)
8221   "Enter ARTICLE in the pertinent lists and remove it from others."
8222   (let ((mark (or mark gnus-ticked-mark)))
8223     (if (<= article 0)
8224         (progn
8225           (gnus-error 1 "Can't mark negative article numbers")
8226           nil)
8227       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8228             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8229             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8230             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8231
8232       ;; Unsuppress duplicates?
8233       (when gnus-suppress-duplicates
8234         (gnus-dup-unsuppress-article article))
8235
8236       (cond ((= mark gnus-ticked-mark)
8237              (push article gnus-newsgroup-marked))
8238             ((= mark gnus-dormant-mark)
8239              (push article gnus-newsgroup-dormant))
8240             (t
8241              (push article gnus-newsgroup-unreads)))
8242       (gnus-pull article gnus-newsgroup-reads)
8243       t)))
8244
8245 (defalias 'gnus-summary-mark-as-unread-forward
8246   'gnus-summary-tick-article-forward)
8247 (make-obsolete 'gnus-summary-mark-as-unread-forward
8248                'gnus-summary-tick-article-forward)
8249 (defun gnus-summary-tick-article-forward (n)
8250   "Tick N articles forwards.
8251 If N is negative, tick backwards instead.
8252 The difference between N and the number of articles ticked is returned."
8253   (interactive "p")
8254   (gnus-summary-mark-forward n gnus-ticked-mark))
8255
8256 (defalias 'gnus-summary-mark-as-unread-backward
8257   'gnus-summary-tick-article-backward)
8258 (make-obsolete 'gnus-summary-mark-as-unread-backward
8259                'gnus-summary-tick-article-backward)
8260 (defun gnus-summary-tick-article-backward (n)
8261   "Tick N articles backwards.
8262 The difference between N and the number of articles ticked is returned."
8263   (interactive "p")
8264   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8265
8266 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8267 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8268 (defun gnus-summary-tick-article (&optional article clear-mark)
8269   "Mark current article as unread.
8270 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8271 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8272   (interactive)
8273   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8274                                        gnus-ticked-mark)))
8275
8276 (defun gnus-summary-mark-as-read-forward (n)
8277   "Mark N articles as read forwards.
8278 If N is negative, mark backwards instead.
8279 The difference between N and the actual number of articles marked is
8280 returned."
8281   (interactive "p")
8282   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8283
8284 (defun gnus-summary-mark-as-read-backward (n)
8285   "Mark the N articles as read backwards.
8286 The difference between N and the actual number of articles marked is
8287 returned."
8288   (interactive "p")
8289   (gnus-summary-mark-forward
8290    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8291
8292 (defun gnus-summary-mark-as-read (&optional article mark)
8293   "Mark current article as read.
8294 ARTICLE specifies the article to be marked as read.
8295 MARK specifies a string to be inserted at the beginning of the line."
8296   (gnus-summary-mark-article article mark))
8297
8298 (defun gnus-summary-clear-mark-forward (n)
8299   "Clear marks from N articles forward.
8300 If N is negative, clear backward instead.
8301 The difference between N and the number of marks cleared is returned."
8302   (interactive "p")
8303   (gnus-summary-mark-forward n gnus-unread-mark))
8304
8305 (defun gnus-summary-clear-mark-backward (n)
8306   "Clear marks from N articles backward.
8307 The difference between N and the number of marks cleared is returned."
8308   (interactive "p")
8309   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8310
8311 (defun gnus-summary-mark-unread-as-read ()
8312   "Intended to be used by `gnus-summary-mark-article-hook'."
8313   (when (memq gnus-current-article gnus-newsgroup-unreads)
8314     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8315
8316 (defun gnus-summary-mark-read-and-unread-as-read ()
8317   "Intended to be used by `gnus-summary-mark-article-hook'."
8318   (let ((mark (gnus-summary-article-mark)))
8319     (when (or (gnus-unread-mark-p mark)
8320               (gnus-read-mark-p mark))
8321       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8322
8323 (defun gnus-summary-mark-region-as-read (point mark all)
8324   "Mark all unread articles between point and mark as read.
8325 If given a prefix, mark all articles between point and mark as read,
8326 even ticked and dormant ones."
8327   (interactive "r\nP")
8328   (save-excursion
8329     (let (article)
8330       (goto-char point)
8331       (beginning-of-line)
8332       (while (and
8333               (< (point) mark)
8334               (progn
8335                 (when (or all
8336                           (memq (setq article (gnus-summary-article-number))
8337                                 gnus-newsgroup-unreads))
8338                   (gnus-summary-mark-article article gnus-del-mark))
8339                 t)
8340               (gnus-summary-find-next))))))
8341
8342 (defun gnus-summary-mark-below (score mark)
8343   "Mark articles with score less than SCORE with MARK."
8344   (interactive "P\ncMark: ")
8345   (setq score (if score
8346                   (prefix-numeric-value score)
8347                 (or gnus-summary-default-score 0)))
8348   (save-excursion
8349     (set-buffer gnus-summary-buffer)
8350     (goto-char (point-min))
8351     (while
8352         (progn
8353           (and (< (gnus-summary-article-score) score)
8354                (gnus-summary-mark-article nil mark))
8355           (gnus-summary-find-next)))))
8356
8357 (defun gnus-summary-kill-below (&optional score)
8358   "Mark articles with score below SCORE as read."
8359   (interactive "P")
8360   (gnus-summary-mark-below score gnus-killed-mark))
8361
8362 (defun gnus-summary-clear-above (&optional score)
8363   "Clear all marks from articles with score above SCORE."
8364   (interactive "P")
8365   (gnus-summary-mark-above score gnus-unread-mark))
8366
8367 (defun gnus-summary-tick-above (&optional score)
8368   "Tick all articles with score above SCORE."
8369   (interactive "P")
8370   (gnus-summary-mark-above score gnus-ticked-mark))
8371
8372 (defun gnus-summary-mark-above (score mark)
8373   "Mark articles with score over SCORE with MARK."
8374   (interactive "P\ncMark: ")
8375   (setq score (if score
8376                   (prefix-numeric-value score)
8377                 (or gnus-summary-default-score 0)))
8378   (save-excursion
8379     (set-buffer gnus-summary-buffer)
8380     (goto-char (point-min))
8381     (while (and (progn
8382                   (when (> (gnus-summary-article-score) score)
8383                     (gnus-summary-mark-article nil mark))
8384                   t)
8385                 (gnus-summary-find-next)))))
8386
8387 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8388 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8389 (defun gnus-summary-limit-include-expunged (&optional no-error)
8390   "Display all the hidden articles that were expunged for low scores."
8391   (interactive)
8392   (let ((buffer-read-only nil))
8393     (let ((scored gnus-newsgroup-scored)
8394           headers h)
8395       (while scored
8396         (unless (gnus-summary-goto-subject (caar scored))
8397           (and (setq h (gnus-summary-article-header (caar scored)))
8398                (< (cdar scored) gnus-summary-expunge-below)
8399                (push h headers)))
8400         (setq scored (cdr scored)))
8401       (if (not headers)
8402           (when (not no-error)
8403             (error "No expunged articles hidden"))
8404         (goto-char (point-min))
8405         (gnus-summary-prepare-unthreaded (nreverse headers))
8406         (goto-char (point-min))
8407         (gnus-summary-position-point)
8408         t))))
8409
8410 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8411   "Mark all unread articles in this newsgroup as read.
8412 If prefix argument ALL is non-nil, ticked and dormant articles will
8413 also be marked as read.
8414 If QUIETLY is non-nil, no questions will be asked.
8415 If TO-HERE is non-nil, it should be a point in the buffer.  All
8416 articles before this point will be marked as read.
8417 Note that this function will only catch up the unread article
8418 in the current summary buffer limitation.
8419 The number of articles marked as read is returned."
8420   (interactive "P")
8421   (prog1
8422       (save-excursion
8423         (when (or quietly
8424                   (not gnus-interactive-catchup) ;Without confirmation?
8425                   gnus-expert-user
8426                   (gnus-y-or-n-p
8427                    (if all
8428                        "Mark absolutely all articles as read? "
8429                      "Mark all unread articles as read? ")))
8430           (if (and not-mark
8431                    (not gnus-newsgroup-adaptive)
8432                    (not gnus-newsgroup-auto-expire)
8433                    (not gnus-suppress-duplicates)
8434                    (or (not gnus-use-cache)
8435                        (eq gnus-use-cache 'passive)))
8436               (progn
8437                 (when all
8438                   (setq gnus-newsgroup-marked nil
8439                         gnus-newsgroup-dormant nil))
8440                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8441             ;; We actually mark all articles as canceled, which we
8442             ;; have to do when using auto-expiry or adaptive scoring.
8443             (gnus-summary-show-all-threads)
8444             (when (gnus-summary-first-subject (not all) t)
8445               (while (and
8446                       (if to-here (< (point) to-here) t)
8447                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8448                       (gnus-summary-find-next (not all) nil nil t))))
8449             (gnus-set-mode-line 'summary))
8450           t))
8451     (gnus-summary-position-point)))
8452
8453 (defun gnus-summary-catchup-to-here (&optional all)
8454   "Mark all unticked articles before the current one as read.
8455 If ALL is non-nil, also mark ticked and dormant articles as read."
8456   (interactive "P")
8457   (save-excursion
8458     (gnus-save-hidden-threads
8459       (let ((beg (point)))
8460         ;; We check that there are unread articles.
8461         (when (or all (gnus-summary-find-prev))
8462           (gnus-summary-catchup all t beg)))))
8463   (gnus-summary-position-point))
8464
8465 (defun gnus-summary-catchup-all (&optional quietly)
8466   "Mark all articles in this newsgroup as read."
8467   (interactive "P")
8468   (gnus-summary-catchup t quietly))
8469
8470 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8471   "Mark all unread articles in this group as read, then exit.
8472 If prefix argument ALL is non-nil, all articles are marked as read."
8473   (interactive "P")
8474   (when (gnus-summary-catchup all quietly nil 'fast)
8475     ;; Select next newsgroup or exit.
8476     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8477              (eq gnus-auto-select-next 'quietly))
8478         (gnus-summary-next-group nil)
8479       (gnus-summary-exit))))
8480
8481 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8482   "Mark all articles in this newsgroup as read, and then exit."
8483   (interactive "P")
8484   (gnus-summary-catchup-and-exit t quietly))
8485
8486 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8487   "Mark all articles in this group as read and select the next group.
8488 If given a prefix, mark all articles, unread as well as ticked, as
8489 read."
8490   (interactive "P")
8491   (save-excursion
8492     (gnus-summary-catchup all))
8493   (gnus-summary-next-group))
8494
8495 ;; Thread-based commands.
8496
8497 (defun gnus-summary-articles-in-thread (&optional article)
8498   "Return a list of all articles in the current thread.
8499 If ARTICLE is non-nil, return all articles in the thread that starts
8500 with that article."
8501   (let* ((article (or article (gnus-summary-article-number)))
8502          (data (gnus-data-find-list article))
8503          (top-level (gnus-data-level (car data)))
8504          (top-subject
8505           (cond ((null gnus-thread-operation-ignore-subject)
8506                  (gnus-simplify-subject-re
8507                   (mail-header-subject (gnus-data-header (car data)))))
8508                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8509                  (gnus-simplify-subject-fuzzy
8510                   (mail-header-subject (gnus-data-header (car data)))))
8511                 (t nil)))
8512          (end-point (save-excursion
8513                       (if (gnus-summary-go-to-next-thread)
8514                           (point) (point-max))))
8515          articles)
8516     (while (and data
8517                 (< (gnus-data-pos (car data)) end-point))
8518       (when (or (not top-subject)
8519                 (string= top-subject
8520                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8521                              (gnus-simplify-subject-fuzzy
8522                               (mail-header-subject
8523                                (gnus-data-header (car data))))
8524                            (gnus-simplify-subject-re
8525                             (mail-header-subject
8526                              (gnus-data-header (car data)))))))
8527         (push (gnus-data-number (car data)) articles))
8528       (unless (and (setq data (cdr data))
8529                    (> (gnus-data-level (car data)) top-level))
8530         (setq data nil)))
8531     ;; Return the list of articles.
8532     (nreverse articles)))
8533
8534 (defun gnus-summary-rethread-current ()
8535   "Rethread the thread the current article is part of."
8536   (interactive)
8537   (let* ((gnus-show-threads t)
8538          (article (gnus-summary-article-number))
8539          (id (mail-header-id (gnus-summary-article-header)))
8540          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8541     (unless id
8542       (error "No article on the current line"))
8543     (gnus-rebuild-thread id)
8544     (gnus-summary-goto-subject article)))
8545
8546 (defun gnus-summary-reparent-thread ()
8547   "Make the current article child of the marked (or previous) article.
8548
8549 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8550 is non-nil or the Subject: of both articles are the same."
8551   (interactive)
8552   (unless (not (gnus-group-read-only-p))
8553     (error "The current newsgroup does not support article editing"))
8554   (unless (<= (length gnus-newsgroup-processable) 1)
8555     (error "No more than one article may be marked"))
8556   (save-window-excursion
8557     (let ((gnus-article-buffer " *reparent*")
8558           (current-article (gnus-summary-article-number))
8559           ;; First grab the marked article, otherwise one line up.
8560           (parent-article (if (not (null gnus-newsgroup-processable))
8561                               (car gnus-newsgroup-processable)
8562                             (save-excursion
8563                               (if (eq (forward-line -1) 0)
8564                                   (gnus-summary-article-number)
8565                                 (error "Beginning of summary buffer"))))))
8566       (unless (not (eq current-article parent-article))
8567         (error "An article may not be self-referential"))
8568       (let ((message-id (mail-header-id
8569                          (gnus-summary-article-header parent-article))))
8570         (unless (and message-id (not (equal message-id "")))
8571           (error "No message-id in desired parent"))
8572         (gnus-with-article current-article
8573           (goto-char (point-min))
8574           (if (re-search-forward "^References: " nil t)
8575               (progn
8576                 (re-search-forward "^[^ \t]" nil t)
8577                 (forward-line -1)
8578                 (end-of-line)
8579                 (insert " " message-id))
8580             (insert "References: " message-id "\n")))
8581         (set-buffer gnus-summary-buffer)
8582         (gnus-summary-unmark-all-processable)
8583         (gnus-summary-update-article current-article)
8584         (gnus-summary-rethread-current)
8585         (gnus-message 3 "Article %d is now the child of article %d"
8586                       current-article parent-article)))))
8587
8588 (defun gnus-summary-toggle-threads (&optional arg)
8589   "Toggle showing conversation threads.
8590 If ARG is positive number, turn showing conversation threads on."
8591   (interactive "P")
8592   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8593     (setq gnus-show-threads
8594           (if (null arg) (not gnus-show-threads)
8595             (> (prefix-numeric-value arg) 0)))
8596     (gnus-summary-prepare)
8597     (gnus-summary-goto-subject current)
8598     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8599     (gnus-summary-position-point)))
8600
8601 (defun gnus-summary-show-all-threads ()
8602   "Show all threads."
8603   (interactive)
8604   (save-excursion
8605     (let ((buffer-read-only nil))
8606       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8607   (gnus-summary-position-point))
8608
8609 (defun gnus-summary-show-thread ()
8610   "Show thread subtrees.
8611 Returns nil if no thread was there to be shown."
8612   (interactive)
8613   (let ((buffer-read-only nil)
8614         (orig (point))
8615         ;; first goto end then to beg, to have point at beg after let
8616         (end (progn (end-of-line) (point)))
8617         (beg (progn (beginning-of-line) (point))))
8618     (prog1
8619         ;; Any hidden lines here?
8620         (search-forward "\r" end t)
8621       (subst-char-in-region beg end ?\^M ?\n t)
8622       (goto-char orig)
8623       (gnus-summary-position-point))))
8624
8625 (defun gnus-summary-hide-all-threads ()
8626   "Hide all thread subtrees."
8627   (interactive)
8628   (save-excursion
8629     (goto-char (point-min))
8630     (gnus-summary-hide-thread)
8631     (while (zerop (gnus-summary-next-thread 1 t))
8632       (gnus-summary-hide-thread)))
8633   (gnus-summary-position-point))
8634
8635 (defun gnus-summary-hide-thread ()
8636   "Hide thread subtrees.
8637 Returns nil if no threads were there to be hidden."
8638   (interactive)
8639   (let ((buffer-read-only nil)
8640         (start (point))
8641         (article (gnus-summary-article-number)))
8642     (goto-char start)
8643     ;; Go forward until either the buffer ends or the subthread
8644     ;; ends.
8645     (when (and (not (eobp))
8646                (or (zerop (gnus-summary-next-thread 1 t))
8647                    (goto-char (point-max))))
8648       (prog1
8649           (if (and (> (point) start)
8650                    (search-backward "\n" start t))
8651               (progn
8652                 (subst-char-in-region start (point) ?\n ?\^M)
8653                 (gnus-summary-goto-subject article))
8654             (goto-char start)
8655             nil)))))
8656
8657 (defun gnus-summary-go-to-next-thread (&optional previous)
8658   "Go to the same level (or less) next thread.
8659 If PREVIOUS is non-nil, go to previous thread instead.
8660 Return the article number moved to, or nil if moving was impossible."
8661   (let ((level (gnus-summary-thread-level))
8662         (way (if previous -1 1))
8663         (beg (point)))
8664     (forward-line way)
8665     (while (and (not (eobp))
8666                 (< level (gnus-summary-thread-level)))
8667       (forward-line way))
8668     (if (eobp)
8669         (progn
8670           (goto-char beg)
8671           nil)
8672       (setq beg (point))
8673       (prog1
8674           (gnus-summary-article-number)
8675         (goto-char beg)))))
8676
8677 (defun gnus-summary-next-thread (n &optional silent)
8678   "Go to the same level next N'th thread.
8679 If N is negative, search backward instead.
8680 Returns the difference between N and the number of skips actually
8681 done.
8682
8683 If SILENT, don't output messages."
8684   (interactive "p")
8685   (let ((backward (< n 0))
8686         (n (abs n)))
8687     (while (and (> n 0)
8688                 (gnus-summary-go-to-next-thread backward))
8689       (decf n))
8690     (unless silent
8691       (gnus-summary-position-point))
8692     (when (and (not silent) (/= 0 n))
8693       (gnus-message 7 "No more threads"))
8694     n))
8695
8696 (defun gnus-summary-prev-thread (n)
8697   "Go to the same level previous N'th thread.
8698 Returns the difference between N and the number of skips actually
8699 done."
8700   (interactive "p")
8701   (gnus-summary-next-thread (- n)))
8702
8703 (defun gnus-summary-go-down-thread ()
8704   "Go down one level in the current thread."
8705   (let ((children (gnus-summary-article-children)))
8706     (when children
8707       (gnus-summary-goto-subject (car children)))))
8708
8709 (defun gnus-summary-go-up-thread ()
8710   "Go up one level in the current thread."
8711   (let ((parent (gnus-summary-article-parent)))
8712     (when parent
8713       (gnus-summary-goto-subject parent))))
8714
8715 (defun gnus-summary-down-thread (n)
8716   "Go down thread N steps.
8717 If N is negative, go up instead.
8718 Returns the difference between N and how many steps down that were
8719 taken."
8720   (interactive "p")
8721   (let ((up (< n 0))
8722         (n (abs n)))
8723     (while (and (> n 0)
8724                 (if up (gnus-summary-go-up-thread)
8725                   (gnus-summary-go-down-thread)))
8726       (setq n (1- n)))
8727     (gnus-summary-position-point)
8728     (when (/= 0 n)
8729       (gnus-message 7 "Can't go further"))
8730     n))
8731
8732 (defun gnus-summary-up-thread (n)
8733   "Go up thread N steps.
8734 If N is negative, go up instead.
8735 Returns the difference between N and how many steps down that were
8736 taken."
8737   (interactive "p")
8738   (gnus-summary-down-thread (- n)))
8739
8740 (defun gnus-summary-top-thread ()
8741   "Go to the top of the thread."
8742   (interactive)
8743   (while (gnus-summary-go-up-thread))
8744   (gnus-summary-article-number))
8745
8746 (defun gnus-summary-kill-thread (&optional unmark)
8747   "Mark articles under current thread as read.
8748 If the prefix argument is positive, remove any kinds of marks.
8749 If the prefix argument is negative, tick articles instead."
8750   (interactive "P")
8751   (when unmark
8752     (setq unmark (prefix-numeric-value unmark)))
8753   (let ((articles (gnus-summary-articles-in-thread)))
8754     (save-excursion
8755       ;; Expand the thread.
8756       (gnus-summary-show-thread)
8757       ;; Mark all the articles.
8758       (while articles
8759         (gnus-summary-goto-subject (car articles))
8760         (cond ((null unmark)
8761                (gnus-summary-mark-article-as-read gnus-killed-mark))
8762               ((> unmark 0)
8763                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8764               (t
8765                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8766         (setq articles (cdr articles))))
8767     ;; Hide killed subtrees.
8768     (and (null unmark)
8769          gnus-thread-hide-killed
8770          (gnus-summary-hide-thread))
8771     ;; If marked as read, go to next unread subject.
8772     (when (null unmark)
8773       ;; Go to next unread subject.
8774       (gnus-summary-next-subject 1 t)))
8775   (gnus-set-mode-line 'summary))
8776
8777 ;; Summary sorting commands
8778
8779 (defun gnus-summary-sort-by-number (&optional reverse)
8780   "Sort the summary buffer by article number.
8781 Argument REVERSE means reverse order."
8782   (interactive "P")
8783   (gnus-summary-sort 'number reverse))
8784
8785 (defun gnus-summary-sort-by-author (&optional reverse)
8786   "Sort the summary buffer by author name alphabetically.
8787 If case-fold-search is non-nil, case of letters is ignored.
8788 Argument REVERSE means reverse order."
8789   (interactive "P")
8790   (gnus-summary-sort 'author reverse))
8791
8792 (defun gnus-summary-sort-by-subject (&optional reverse)
8793   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8794 If case-fold-search is non-nil, case of letters is ignored.
8795 Argument REVERSE means reverse order."
8796   (interactive "P")
8797   (gnus-summary-sort 'subject reverse))
8798
8799 (defun gnus-summary-sort-by-date (&optional reverse)
8800   "Sort the summary buffer by date.
8801 Argument REVERSE means reverse order."
8802   (interactive "P")
8803   (gnus-summary-sort 'date reverse))
8804
8805 (defun gnus-summary-sort-by-score (&optional reverse)
8806   "Sort the summary buffer by score.
8807 Argument REVERSE means reverse order."
8808   (interactive "P")
8809   (gnus-summary-sort 'score reverse))
8810
8811 (defun gnus-summary-sort-by-lines (&optional reverse)
8812   "Sort the summary buffer by the number of lines.
8813 Argument REVERSE means reverse order."
8814   (interactive "P")
8815   (gnus-summary-sort 'lines reverse))
8816
8817 (defun gnus-summary-sort-by-chars (&optional reverse)
8818   "Sort the summary buffer by article length.
8819 Argument REVERSE means reverse order."
8820   (interactive "P")
8821   (gnus-summary-sort 'chars reverse))   
8822
8823 (defun gnus-summary-sort (predicate reverse)
8824   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8825   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8826          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8827          (gnus-thread-sort-functions
8828           (if (not reverse)
8829               thread
8830             `(lambda (t1 t2)
8831                (,thread t2 t1))))
8832          (gnus-article-sort-functions
8833           (if (not reverse)
8834               article
8835             `(lambda (t1 t2)
8836                (,article t2 t1))))
8837          (buffer-read-only)
8838          (gnus-summary-prepare-hook nil))
8839     ;; We do the sorting by regenerating the threads.
8840     (gnus-summary-prepare)
8841     ;; Hide subthreads if needed.
8842     (when (and gnus-show-threads gnus-thread-hide-subtree)
8843       (gnus-summary-hide-all-threads))))
8844
8845 ;; Summary saving commands.
8846
8847 (defun gnus-summary-save-article (&optional n not-saved)
8848   "Save the current article using the default saver function.
8849 If N is a positive number, save the N next articles.
8850 If N is a negative number, save the N previous articles.
8851 If N is nil and any articles have been marked with the process mark,
8852 save those articles instead.
8853 The variable `gnus-default-article-saver' specifies the saver function."
8854   (interactive "P")
8855   (let* ((articles (gnus-summary-work-articles n))
8856          (save-buffer (save-excursion
8857                         (nnheader-set-temp-buffer " *Gnus Save*")))
8858          (num (length articles))
8859          header file)
8860     (dolist (article articles)
8861       (setq header (gnus-summary-article-header article))
8862       (if (not (vectorp header))
8863           ;; This is a pseudo-article.
8864           (if (assq 'name header)
8865               (gnus-copy-file (cdr (assq 'name header)))
8866             (gnus-message 1 "Article %d is unsaveable" article))
8867         ;; This is a real article.
8868         (save-window-excursion
8869           (gnus-summary-select-article t nil nil article))
8870         (save-excursion
8871           (set-buffer save-buffer)
8872           (erase-buffer)
8873           (insert-buffer-substring gnus-original-article-buffer))
8874         (setq file (gnus-article-save save-buffer file num))
8875         (gnus-summary-remove-process-mark article)
8876         (unless not-saved
8877           (gnus-summary-set-saved-mark article))))
8878     (gnus-kill-buffer save-buffer)
8879     (gnus-summary-position-point)
8880     (gnus-set-mode-line 'summary)
8881     n))
8882
8883 (defun gnus-summary-pipe-output (&optional arg)
8884   "Pipe the current article to a subprocess.
8885 If N is a positive number, pipe the N next articles.
8886 If N is a negative number, pipe the N previous articles.
8887 If N is nil and any articles have been marked with the process mark,
8888 pipe those articles instead."
8889   (interactive "P")
8890   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8891     (gnus-summary-save-article arg t))
8892   (gnus-configure-windows 'pipe))
8893
8894 (defun gnus-summary-save-article-mail (&optional arg)
8895   "Append the current article to an mail file.
8896 If N is a positive number, save the N next articles.
8897 If N is a negative number, save the N previous articles.
8898 If N is nil and any articles have been marked with the process mark,
8899 save those articles instead."
8900   (interactive "P")
8901   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8902     (gnus-summary-save-article arg)))
8903
8904 (defun gnus-summary-save-article-rmail (&optional arg)
8905   "Append the current article to an rmail file.
8906 If N is a positive number, save the N next articles.
8907 If N is a negative number, save the N previous articles.
8908 If N is nil and any articles have been marked with the process mark,
8909 save those articles instead."
8910   (interactive "P")
8911   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8912     (gnus-summary-save-article arg)))
8913
8914 (defun gnus-summary-save-article-file (&optional arg)
8915   "Append the current article to a file.
8916 If N is a positive number, save the N next articles.
8917 If N is a negative number, save the N previous articles.
8918 If N is nil and any articles have been marked with the process mark,
8919 save those articles instead."
8920   (interactive "P")
8921   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8922     (gnus-summary-save-article arg)))
8923
8924 (defun gnus-summary-write-article-file (&optional arg)
8925   "Write the current article to a file, deleting the previous file.
8926 If N is a positive number, save the N next articles.
8927 If N is a negative number, save the N previous articles.
8928 If N is nil and any articles have been marked with the process mark,
8929 save those articles instead."
8930   (interactive "P")
8931   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8932     (gnus-summary-save-article arg)))
8933
8934 (defun gnus-summary-save-article-body-file (&optional arg)
8935   "Append the current article body to a file.
8936 If N is a positive number, save the N next articles.
8937 If N is a negative number, save the N previous articles.
8938 If N is nil and any articles have been marked with the process mark,
8939 save those articles instead."
8940   (interactive "P")
8941   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8942     (gnus-summary-save-article arg)))
8943
8944 (defun gnus-summary-pipe-message (program)
8945   "Pipe the current article through PROGRAM."
8946   (interactive "sProgram: ")
8947   (gnus-summary-select-article)
8948   (let ((mail-header-separator ""))
8949     (gnus-eval-in-buffer-window gnus-article-buffer
8950       (save-restriction
8951         (widen)
8952         (let ((start (window-start))
8953               buffer-read-only)
8954           (message-pipe-buffer-body program)
8955           (set-window-start (get-buffer-window (current-buffer)) start))))))
8956
8957 (defun gnus-get-split-value (methods)
8958   "Return a value based on the split METHODS."
8959   (let (split-name method result match)
8960     (when methods
8961       (save-excursion
8962         (set-buffer gnus-original-article-buffer)
8963         (save-restriction
8964           (nnheader-narrow-to-headers)
8965           (while methods
8966             (goto-char (point-min))
8967             (setq method (pop methods))
8968             (setq match (car method))
8969             (when (cond
8970                    ((stringp match)
8971                     ;; Regular expression.
8972                     (ignore-errors
8973                       (re-search-forward match nil t)))
8974                    ((gnus-functionp match)
8975                     ;; Function.
8976                     (save-restriction
8977                       (widen)
8978                       (setq result (funcall match gnus-newsgroup-name))))
8979                    ((consp match)
8980                     ;; Form.
8981                     (save-restriction
8982                       (widen)
8983                       (setq result (eval match)))))
8984               (setq split-name (append (cdr method) split-name))
8985               (cond ((stringp result)
8986                      (push (expand-file-name
8987                             result gnus-article-save-directory)
8988                            split-name))
8989                     ((consp result)
8990                      (setq split-name (append result split-name)))))))))
8991     (nreverse split-name)))
8992
8993 (defun gnus-valid-move-group-p (group)
8994   (and (boundp group)
8995        (symbol-name group)
8996        (symbol-value group)
8997        (gnus-get-function (gnus-find-method-for-group
8998                            (symbol-name group)) 'request-accept-article t)))
8999
9000 (defun gnus-read-move-group-name (prompt default articles prefix)
9001   "Read a group name."
9002   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9003          (minibuffer-confirm-incomplete nil) ; XEmacs
9004          (prom
9005           (format "%s %s to:"
9006                   prompt
9007                   (if (> (length articles) 1)
9008                       (format "these %d articles" (length articles))
9009                     "this article")))
9010          (to-newsgroup
9011           (cond
9012            ((null split-name)
9013             (gnus-completing-read default prom
9014                                   gnus-active-hashtb
9015                                   'gnus-valid-move-group-p
9016                                   nil prefix
9017                                   'gnus-group-history))
9018            ((= 1 (length split-name))
9019             (gnus-completing-read (car split-name) prom
9020                                   gnus-active-hashtb
9021                                   'gnus-valid-move-group-p
9022                                   nil nil
9023                                   'gnus-group-history))
9024            (t
9025             (gnus-completing-read nil prom
9026                                   (mapcar (lambda (el) (list el))
9027                                           (nreverse split-name))
9028                                   nil nil nil
9029                                   'gnus-group-history)))))
9030     (when to-newsgroup
9031       (if (or (string= to-newsgroup "")
9032               (string= to-newsgroup prefix))
9033           (setq to-newsgroup default))
9034       (unless to-newsgroup
9035         (error "No group name entered"))
9036       (or (gnus-active to-newsgroup)
9037           (gnus-activate-group to-newsgroup)
9038           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9039                                      to-newsgroup))
9040               (or (and (gnus-request-create-group
9041                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9042                        (gnus-activate-group
9043                         to-newsgroup nil nil
9044                         (gnus-group-name-to-method to-newsgroup))
9045                        (gnus-subscribe-group to-newsgroup))
9046                   (error "Couldn't create group %s" to-newsgroup)))
9047           (error "No such group: %s" to-newsgroup)))
9048     to-newsgroup))
9049
9050 (defun gnus-summary-save-parts (type dir n &optional reverse)
9051   "Save parts matching TYPE to DIR.
9052 If REVERSE, save parts that do not match TYPE."
9053   (interactive
9054    (list (read-string "Save parts of type: " "image/.*")
9055          (read-file-name "Save to directory: " nil nil t)
9056          current-prefix-arg))
9057   (gnus-summary-iterate n
9058     (let ((gnus-display-mime-function nil)
9059           (gnus-inhibit-treatment t))
9060       (gnus-summary-select-article))
9061     (save-excursion
9062       (set-buffer gnus-article-buffer)
9063       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9064         (when handles
9065           (gnus-summary-save-parts-1 type dir handles reverse)
9066           (mm-destroy-parts handles))))))
9067
9068 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9069   (if (stringp (car handle))
9070       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9071               (cdr handle))
9072     (when (if reverse
9073               (not (string-match type (mm-handle-media-type handle)))
9074             (string-match type (mm-handle-media-type handle)))
9075       (let ((file (expand-file-name
9076                    (file-name-nondirectory
9077                     (or
9078                      (mail-content-type-get
9079                       (mm-handle-disposition handle) 'filename)
9080                      (concat gnus-newsgroup-name "." gnus-current-article)))
9081                    dir)))
9082         (unless (file-exists-p file)
9083           (mm-save-part-to-file handle file))))))
9084
9085 ;; Summary extract commands
9086
9087 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9088   (let ((buffer-read-only nil)
9089         (article (gnus-summary-article-number))
9090         after-article b e)
9091     (unless (gnus-summary-goto-subject article)
9092       (error "No such article: %d" article))
9093     (gnus-summary-position-point)
9094     ;; If all commands are to be bunched up on one line, we collect
9095     ;; them here.
9096     (unless gnus-view-pseudos-separately
9097       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9098             files action)
9099         (while ps
9100           (setq action (cdr (assq 'action (car ps))))
9101           (setq files (list (cdr (assq 'name (car ps)))))
9102           (while (and ps (cdr ps)
9103                       (string= (or action "1")
9104                                (or (cdr (assq 'action (cadr ps))) "2")))
9105             (push (cdr (assq 'name (cadr ps))) files)
9106             (setcdr ps (cddr ps)))
9107           (when files
9108             (when (not (string-match "%s" action))
9109               (push " " files))
9110             (push " " files)
9111             (when (assq 'execute (car ps))
9112               (setcdr (assq 'execute (car ps))
9113                       (funcall (if (string-match "%s" action)
9114                                    'format 'concat)
9115                                action
9116                                (mapconcat
9117                                 (lambda (f)
9118                                   (if (equal f " ")
9119                                       f
9120                                     (mm-quote-arg f)))
9121                                 files " ")))))
9122           (setq ps (cdr ps)))))
9123     (if (and gnus-view-pseudos (not not-view))
9124         (while pslist
9125           (when (assq 'execute (car pslist))
9126             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9127                                   (eq gnus-view-pseudos 'not-confirm)))
9128           (setq pslist (cdr pslist)))
9129       (save-excursion
9130         (while pslist
9131           (setq after-article (or (cdr (assq 'article (car pslist)))
9132                                   (gnus-summary-article-number)))
9133           (gnus-summary-goto-subject after-article)
9134           (forward-line 1)
9135           (setq b (point))
9136           (insert "    " (file-name-nondirectory
9137                           (cdr (assq 'name (car pslist))))
9138                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9139           (setq e (point))
9140           (forward-line -1)             ; back to `b'
9141           (gnus-add-text-properties
9142            b (1- e) (list 'gnus-number gnus-reffed-article-number
9143                           gnus-mouse-face-prop gnus-mouse-face))
9144           (gnus-data-enter
9145            after-article gnus-reffed-article-number
9146            gnus-unread-mark b (car pslist) 0 (- e b))
9147           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9148           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9149           (setq pslist (cdr pslist)))))))
9150
9151 (defun gnus-pseudos< (p1 p2)
9152   (let ((c1 (cdr (assq 'action p1)))
9153         (c2 (cdr (assq 'action p2))))
9154     (and c1 c2 (string< c1 c2))))
9155
9156 (defun gnus-request-pseudo-article (props)
9157   (cond ((assq 'execute props)
9158          (gnus-execute-command (cdr (assq 'execute props)))))
9159   (let ((gnus-current-article (gnus-summary-article-number)))
9160     (gnus-run-hooks 'gnus-mark-article-hook)))
9161
9162 (defun gnus-execute-command (command &optional automatic)
9163   (save-excursion
9164     (gnus-article-setup-buffer)
9165     (set-buffer gnus-article-buffer)
9166     (setq buffer-read-only nil)
9167     (let ((command (if automatic command
9168                      (read-string "Command: " (cons command 0)))))
9169       (erase-buffer)
9170       (insert "$ " command "\n\n")
9171       (if gnus-view-pseudo-asynchronously
9172           (start-process "gnus-execute" (current-buffer) shell-file-name
9173                          shell-command-switch command)
9174         (call-process shell-file-name nil t nil
9175                       shell-command-switch command)))))
9176
9177 ;; Summary kill commands.
9178
9179 (defun gnus-summary-edit-global-kill (article)
9180   "Edit the \"global\" kill file."
9181   (interactive (list (gnus-summary-article-number)))
9182   (gnus-group-edit-global-kill article))
9183
9184 (defun gnus-summary-edit-local-kill ()
9185   "Edit a local kill file applied to the current newsgroup."
9186   (interactive)
9187   (setq gnus-current-headers (gnus-summary-article-header))
9188   (gnus-group-edit-local-kill
9189    (gnus-summary-article-number) gnus-newsgroup-name))
9190
9191 ;;; Header reading.
9192
9193 (defun gnus-read-header (id &optional header)
9194   "Read the headers of article ID and enter them into the Gnus system."
9195   (let ((group gnus-newsgroup-name)
9196         (gnus-override-method
9197          (or
9198           gnus-override-method
9199           (and (gnus-news-group-p gnus-newsgroup-name)
9200                (car (gnus-refer-article-methods)))))
9201         where)
9202     ;; First we check to see whether the header in question is already
9203     ;; fetched.
9204     (if (stringp id)
9205         ;; This is a Message-ID.
9206         (setq header (or header (gnus-id-to-header id)))
9207       ;; This is an article number.
9208       (setq header (or header (gnus-summary-article-header id))))
9209     (if (and header
9210              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9211         ;; We have found the header.
9212         header
9213       ;; If this is a sparse article, we have to nix out its
9214       ;; previous entry in the thread hashtb.
9215       (when (and header
9216                  (gnus-summary-article-sparse-p (mail-header-number header)))
9217         (let* ((parent (gnus-parent-id (mail-header-references header)))
9218                (thread (and parent (gnus-id-to-thread parent))))
9219           (when thread
9220             (delq (assq header thread) thread))))
9221       ;; We have to really fetch the header to this article.
9222       (save-excursion
9223         (set-buffer nntp-server-buffer)
9224         (when (setq where (gnus-request-head id group))
9225           (nnheader-fold-continuation-lines)
9226           (goto-char (point-max))
9227           (insert ".\n")
9228           (goto-char (point-min))
9229           (insert "211 ")
9230           (princ (cond
9231                   ((numberp id) id)
9232                   ((cdr where) (cdr where))
9233                   (header (mail-header-number header))
9234                   (t gnus-reffed-article-number))
9235                  (current-buffer))
9236           (insert " Article retrieved.\n"))
9237         (if (or (not where)
9238                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9239             ()                          ; Malformed head.
9240           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9241             (when (and (stringp id)
9242                        (not (string= (gnus-group-real-name group)
9243                                      (car where))))
9244               ;; If we fetched by Message-ID and the article came
9245               ;; from a different group, we fudge some bogus article
9246               ;; numbers for this article.
9247               (mail-header-set-number header gnus-reffed-article-number))
9248             (save-excursion
9249               (set-buffer gnus-summary-buffer)
9250               (decf gnus-reffed-article-number)
9251               (gnus-remove-header (mail-header-number header))
9252               (push header gnus-newsgroup-headers)
9253               (setq gnus-current-headers header)
9254               (push (mail-header-number header) gnus-newsgroup-limit)))
9255           header)))))
9256
9257 (defun gnus-remove-header (number)
9258   "Remove header NUMBER from `gnus-newsgroup-headers'."
9259   (if (and gnus-newsgroup-headers
9260            (= number (mail-header-number (car gnus-newsgroup-headers))))
9261       (pop gnus-newsgroup-headers)
9262     (let ((headers gnus-newsgroup-headers))
9263       (while (and (cdr headers)
9264                   (not (= number (mail-header-number (cadr headers)))))
9265         (pop headers))
9266       (when (cdr headers)
9267         (setcdr headers (cddr headers))))))
9268
9269 ;;;
9270 ;;; summary highlights
9271 ;;;
9272
9273 (defun gnus-highlight-selected-summary ()
9274   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9275   ;; Highlight selected article in summary buffer
9276   (when gnus-summary-selected-face
9277     (save-excursion
9278       (let* ((beg (progn (beginning-of-line) (point)))
9279              (end (progn (end-of-line) (point)))
9280              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9281              (from (if (get-text-property beg gnus-mouse-face-prop)
9282                        beg
9283                      (or (next-single-property-change
9284                           beg gnus-mouse-face-prop nil end)
9285                          beg)))
9286              (to
9287               (if (= from end)
9288                   (- from 2)
9289                 (or (next-single-property-change
9290                      from gnus-mouse-face-prop nil end)
9291                     end))))
9292         ;; If no mouse-face prop on line we will have to = from = end,
9293         ;; so we highlight the entire line instead.
9294         (when (= (+ to 2) from)
9295           (setq from beg)
9296           (setq to end))
9297         (if gnus-newsgroup-selected-overlay
9298             ;; Move old overlay.
9299             (gnus-move-overlay
9300              gnus-newsgroup-selected-overlay from to (current-buffer))
9301           ;; Create new overlay.
9302           (gnus-overlay-put
9303            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9304            'face gnus-summary-selected-face))))))
9305
9306 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9307 (defun gnus-summary-highlight-line ()
9308   "Highlight current line according to `gnus-summary-highlight'."
9309   (let* ((list gnus-summary-highlight)
9310          (p (point))
9311          (end (progn (end-of-line) (point)))
9312          ;; now find out where the line starts and leave point there.
9313          (beg (progn (beginning-of-line) (point)))
9314          (article (gnus-summary-article-number))
9315          (score (or (cdr (assq (or article gnus-current-article)
9316                                gnus-newsgroup-scored))
9317                     gnus-summary-default-score 0))
9318          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9319          (inhibit-read-only t))
9320     ;; Eval the cars of the lists until we find a match.
9321     (let ((default gnus-summary-default-score))
9322       (while (and list
9323                   (not (eval (caar list))))
9324         (setq list (cdr list))))
9325     (let ((face (cdar list)))
9326       (unless (eq face (get-text-property beg 'face))
9327         (gnus-put-text-property-excluding-characters-with-faces
9328          beg end 'face
9329          (setq face (if (boundp face) (symbol-value face) face)))
9330         (when gnus-summary-highlight-line-function
9331           (funcall gnus-summary-highlight-line-function article face))))
9332     (goto-char p)))
9333
9334 (defun gnus-update-read-articles (group unread &optional compute)
9335   "Update the list of read articles in GROUP."
9336   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9337          (entry (gnus-gethash group gnus-newsrc-hashtb))
9338          (info (nth 2 entry))
9339          (prev 1)
9340          (unread (sort (copy-sequence unread) '<))
9341          read)
9342     (if (or (not info) (not active))
9343         ;; There is no info on this group if it was, in fact,
9344         ;; killed.  Gnus stores no information on killed groups, so
9345         ;; there's nothing to be done.
9346         ;; One could store the information somewhere temporarily,
9347         ;; perhaps...  Hmmm...
9348         ()
9349       ;; Remove any negative articles numbers.
9350       (while (and unread (< (car unread) 0))
9351         (setq unread (cdr unread)))
9352       ;; Remove any expired article numbers
9353       (while (and unread (< (car unread) (car active)))
9354         (setq unread (cdr unread)))
9355       ;; Compute the ranges of read articles by looking at the list of
9356       ;; unread articles.
9357       (while unread
9358         (when (/= (car unread) prev)
9359           (push (if (= prev (1- (car unread))) prev
9360                   (cons prev (1- (car unread))))
9361                 read))
9362         (setq prev (1+ (car unread)))
9363         (setq unread (cdr unread)))
9364       (when (<= prev (cdr active))
9365         (push (cons prev (cdr active)) read))
9366       (setq read (if (> (length read) 1) (nreverse read) read))
9367       (if compute
9368           read
9369         (save-excursion
9370           (set-buffer gnus-group-buffer)
9371           (gnus-undo-register
9372             `(progn
9373                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9374                (gnus-info-set-read ',info ',(gnus-info-read info))
9375                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9376                (gnus-group-update-group ,group t))))
9377         ;; Propagate the read marks to the backend.
9378         (if (gnus-check-backend-function 'request-set-mark group)
9379             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9380                   (add (gnus-remove-from-range read (gnus-info-read info))))
9381               (when (or add del)
9382                 (unless (gnus-check-group group)
9383                   (error "Can't open server for %s" group))
9384                 (gnus-request-set-mark
9385                  group (delq nil (list (if add (list add 'add '(read)))
9386                                        (if del (list del 'del '(read)))))))))
9387         ;; Enter this list into the group info.
9388         (gnus-info-set-read info read)
9389         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9390         (gnus-get-unread-articles-in-group info (gnus-active group))
9391         t))))
9392
9393 (defun gnus-offer-save-summaries ()
9394   "Offer to save all active summary buffers."
9395   (save-excursion
9396     (let ((buflist (buffer-list))
9397           buffers bufname)
9398       ;; Go through all buffers and find all summaries.
9399       (while buflist
9400         (and (setq bufname (buffer-name (car buflist)))
9401              (string-match "Summary" bufname)
9402              (save-excursion
9403                (set-buffer bufname)
9404                ;; We check that this is, indeed, a summary buffer.
9405                (and (eq major-mode 'gnus-summary-mode)
9406                     ;; Also make sure this isn't bogus.
9407                     gnus-newsgroup-prepared
9408                     ;; Also make sure that this isn't a dead summary buffer.
9409                     (not gnus-dead-summary-mode)))
9410              (push bufname buffers))
9411         (setq buflist (cdr buflist)))
9412       ;; Go through all these summary buffers and offer to save them.
9413       (when buffers
9414         (map-y-or-n-p
9415          "Update summary buffer %s? "
9416          (lambda (buf)
9417            (switch-to-buffer buf)
9418            (gnus-summary-exit))
9419          buffers)))))
9420
9421 (defun gnus-summary-setup-default-charset ()
9422   "Setup newsgroup default charset."
9423   (if (equal gnus-newsgroup-name "nndraft:drafts")
9424       (setq gnus-newsgroup-charset nil)
9425     (let* ((name (and gnus-newsgroup-name
9426                       (gnus-group-real-name gnus-newsgroup-name)))
9427            (ignored-charsets 
9428             (or gnus-newsgroup-ephemeral-ignored-charsets
9429                 (append
9430                  (and gnus-newsgroup-name
9431                       (or (gnus-group-find-parameter gnus-newsgroup-name
9432                                                      'ignored-charsets t)
9433                           (let ((alist gnus-group-ignored-charsets-alist)
9434                                 elem (charsets nil))
9435                             (while (setq elem (pop alist))
9436                               (when (and name
9437                                          (string-match (car elem) name))
9438                                 (setq alist nil
9439                                       charsets (cdr elem))))
9440                             charsets)))
9441                  gnus-newsgroup-ignored-charsets))))
9442       (setq gnus-newsgroup-charset
9443             (or gnus-newsgroup-ephemeral-charset
9444                 (and gnus-newsgroup-name
9445                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9446                          (let ((alist gnus-group-charset-alist)
9447                                elem charset)
9448                            (while (setq elem (pop alist))
9449                              (when (and name
9450                                         (string-match (car elem) name))
9451                                (setq alist nil
9452                                      charset (cadr elem))))
9453                            charset)))
9454                 gnus-default-charset))
9455       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9456            ignored-charsets))))
9457
9458 ;;;
9459 ;;; Mime Commands
9460 ;;;
9461
9462 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9463   "Display the current article buffer fully MIME-buttonized.
9464 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9465 treated as multipart/mixed."
9466   (interactive "P")
9467   (require 'gnus-art)
9468   (let ((gnus-unbuttonized-mime-types nil)
9469         (gnus-mime-display-multipart-as-mixed show-all-parts))
9470     (gnus-summary-show-article)))
9471
9472 (defun gnus-summary-repair-multipart (article)
9473   "Add a Content-Type header to a multipart article without one."
9474   (interactive (list (gnus-summary-article-number)))
9475   (gnus-with-article article
9476     (message-narrow-to-head)
9477     (goto-char (point-max))
9478     (widen)
9479     (when (search-forward "\n--" nil t)
9480       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9481         (message-narrow-to-head)
9482         (message-remove-header "Mime-Version")
9483         (message-remove-header "Content-Type")
9484         (goto-char (point-max))
9485         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9486                         separator))
9487         (insert "Mime-Version: 1.0\n")
9488         (widen))))
9489   (let (gnus-mark-article-hook)
9490     (gnus-summary-select-article t t nil article)))
9491
9492 (defun gnus-summary-toggle-display-buttonized ()
9493   "Toggle the buttonizing of the article buffer."
9494   (interactive)
9495   (require 'gnus-art)
9496   (if (setq gnus-inhibit-mime-unbuttonizing
9497             (not gnus-inhibit-mime-unbuttonizing))
9498       (let ((gnus-unbuttonized-mime-types nil))
9499         (gnus-summary-show-article))
9500     (gnus-summary-show-article)))
9501
9502 ;;;
9503 ;;; with article
9504 ;;;
9505
9506 (defmacro gnus-with-article (article &rest forms)
9507   "Select ARTICLE and perform FORMS in the original article buffer.
9508 Then replace the article with the result."
9509   `(progn
9510      ;; We don't want the article to be marked as read.
9511      (let (gnus-mark-article-hook)
9512        (gnus-summary-select-article t t nil ,article))
9513      (set-buffer gnus-original-article-buffer)
9514      ,@forms
9515      (if (not (gnus-check-backend-function
9516                'request-replace-article (car gnus-article-current)))
9517          (gnus-message 5 "Read-only group; not replacing")
9518        (unless (gnus-request-replace-article
9519                 ,article (car gnus-article-current)
9520                 (current-buffer) t)
9521          (error "Couldn't replace article")))
9522      ;; The cache and backlog have to be flushed somewhat.
9523      (when gnus-keep-backlog
9524        (gnus-backlog-remove-article
9525         (car gnus-article-current) (cdr gnus-article-current)))
9526      (when gnus-use-cache
9527        (gnus-cache-update-article
9528         (car gnus-article-current) (cdr gnus-article-current)))))
9529
9530 (put 'gnus-with-article 'lisp-indent-function 1)
9531 (put 'gnus-with-article 'edebug-form-spec '(form body))
9532
9533 ;;;
9534 ;;; Generic summary marking commands
9535 ;;;
9536
9537 (defvar gnus-summary-marking-alist
9538   '((read gnus-del-mark "d")
9539     (unread gnus-unread-mark "u")
9540     (ticked gnus-ticked-mark "!")
9541     (dormant gnus-dormant-mark "?")
9542     (expirable gnus-expirable-mark "e"))
9543   "An alist of names/marks/keystrokes.")
9544
9545 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9546 (defvar gnus-summary-mark-map)
9547
9548 (defun gnus-summary-make-all-marking-commands ()
9549   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9550   (dolist (elem gnus-summary-marking-alist)
9551     (apply 'gnus-summary-make-marking-command elem)))
9552
9553 (defun gnus-summary-make-marking-command (name mark keystroke)
9554   (let ((map (make-sparse-keymap)))
9555     (define-key gnus-summary-generic-mark-map keystroke map)
9556     (dolist (lway `((next "next" next nil "n")
9557                     (next-unread "next unread" next t "N")
9558                     (prev "previous" prev nil "p")
9559                     (prev-unread "previous unread" prev t "P")
9560                     (nomove "" nil nil ,keystroke)))
9561       (let ((func (gnus-summary-make-marking-command-1
9562                    mark (car lway) lway name)))
9563         (setq func (eval func))
9564         (define-key map (nth 4 lway) func)))))
9565       
9566 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9567   `(defun ,(intern
9568             (format "gnus-summary-put-mark-as-%s%s"
9569                     name (if (eq way 'nomove)
9570                              ""
9571                            (concat "-" (symbol-name way)))))
9572      (n)
9573      ,(format
9574        "Mark the current article as %s%s.
9575 If N, the prefix, then repeat N times.
9576 If N is negative, move in reverse order.
9577 The difference between N and the actual number of articles marked is
9578 returned."
9579        name (cadr lway))
9580      (interactive "p")
9581      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9582     
9583 (defun gnus-summary-generic-mark (n mark move unread)
9584   "Mark N articles with MARK."
9585   (unless (eq major-mode 'gnus-summary-mode)
9586     (error "This command can only be used in the summary buffer"))
9587   (gnus-summary-show-thread)
9588   (let ((nummove
9589          (cond
9590           ((eq move 'next) 1)
9591           ((eq move 'prev) -1)
9592           (t 0))))
9593     (if (zerop nummove)
9594         (setq n 1)
9595       (when (< n 0)
9596         (setq n (abs n)
9597               nummove (* -1 nummove))))
9598     (while (and (> n 0)
9599                 (gnus-summary-mark-article nil mark)
9600                 (zerop (gnus-summary-next-subject nummove unread t)))
9601       (setq n (1- n)))
9602     (when (/= 0 n)
9603       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9604     (gnus-summary-recenter)
9605     (gnus-summary-position-point)
9606     (gnus-set-mode-line 'summary)
9607     n))
9608
9609 (gnus-summary-make-all-marking-commands)
9610
9611 (gnus-ems-redefine)
9612
9613 (provide 'gnus-sum)
9614
9615 (run-hooks 'gnus-sum-load-hook)
9616
9617 ;;; gnus-sum.el ends here