2000-11-06 14:03:10 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40 (autoload 'gnus-cache-write-active "gnus-cache")
41
42 (defcustom gnus-kill-summary-on-exit t
43   "*If non-nil, kill the summary buffer when you exit from it.
44 If nil, the summary will become a \"*Dead Summary*\" buffer, and
45 it will be killed sometime later."
46   :group 'gnus-summary-exit
47   :type 'boolean)
48
49 (defcustom gnus-fetch-old-headers nil
50   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
51 If an unread article in the group refers to an older, already read (or
52 just marked as read) article, the old article will not normally be
53 displayed in the Summary buffer.  If this variable is non-nil, Gnus
54 will attempt to grab the headers to the old articles, and thereby
55 build complete threads.  If it has the value `some', only enough
56 headers to connect otherwise loose threads will be displayed.  This
57 variable can also be a number.  In that case, no more than that number
58 of old headers will be fetched.  If it has the value `invisible', all
59 old headers will be fetched, but none will be displayed.
60
61 The server has to support NOV for any of this to work."
62   :group 'gnus-thread
63   :type '(choice (const :tag "off" nil)
64                  (const some)
65                  number
66                  (sexp :menu-tag "other" t)))
67
68 (defcustom gnus-refer-thread-limit 200
69   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
70 If t, fetch all the available old headers."
71   :group 'gnus-thread
72   :type '(choice number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-summary-make-false-root 'adopt
76   "*nil means that Gnus won't gather loose threads.
77 If the root of a thread has expired or been read in a previous
78 session, the information necessary to build a complete thread has been
79 lost.  Instead of having many small sub-threads from this original thread
80 scattered all over the summary buffer, Gnus can gather them.
81
82 If non-nil, Gnus will try to gather all loose sub-threads from an
83 original thread into one large thread.
84
85 If this variable is non-nil, it should be one of `none', `adopt',
86 `dummy' or `empty'.
87
88 If this variable is `none', Gnus will not make a false root, but just
89 present the sub-threads after another.
90 If this variable is `dummy', Gnus will create a dummy root that will
91 have all the sub-threads as children.
92 If this variable is `adopt', Gnus will make one of the \"children\"
93 the parent and mark all the step-children as such.
94 If this variable is `empty', the \"children\" are printed with empty
95 subject fields.  (Or rather, they will be printed with a string
96 given by the `gnus-summary-same-subject' variable.)"
97   :group 'gnus-thread
98   :type '(choice (const :tag "off" nil)
99                  (const none)
100                  (const dummy)
101                  (const adopt)
102                  (const empty)))
103
104 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
105   "*A regexp to match subjects to be excluded from loose thread gathering.
106 As loose thread gathering is done on subjects only, that means that
107 there can be many false gatherings performed.  By rooting out certain
108 common subjects, gathering might become saner."
109   :group 'gnus-thread
110   :type 'regexp)
111
112 (defcustom gnus-summary-gather-subject-limit nil
113   "*Maximum length of subject comparisons when gathering loose threads.
114 Use nil to compare full subjects.  Setting this variable to a low
115 number will help gather threads that have been corrupted by
116 newsreaders chopping off subject lines, but it might also mean that
117 unrelated articles that have subject that happen to begin with the
118 same few characters will be incorrectly gathered.
119
120 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
121 comparing subjects."
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const fuzzy)
125                  (sexp :menu-tag "on" t)))
126
127 (defcustom gnus-simplify-subject-functions nil
128   "List of functions taking a string argument that simplify subjects.
129 The functions are applied recursively.
130
131 Useful functions to put in this list include: `gnus-simplify-subject-re',
132 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
133   :group 'gnus-thread
134   :type '(repeat function))
135
136 (defcustom gnus-simplify-ignored-prefixes nil
137   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  regexp))
141
142 (defcustom gnus-build-sparse-threads nil
143   "*If non-nil, fill in the gaps in threads.
144 If `some', only fill in the gaps that are needed to tie loose threads
145 together.  If `more', fill in all leaf nodes that Gnus can find.  If
146 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  (const some)
150                  (const more)
151                  (sexp :menu-tag "all" t)))
152
153 (defcustom gnus-summary-thread-gathering-function
154   'gnus-gather-threads-by-subject
155   "*Function used for gathering loose threads.
156 There are two pre-defined functions: `gnus-gather-threads-by-subject',
157 which only takes Subjects into consideration; and
158 `gnus-gather-threads-by-references', which compared the References
159 headers of the articles to find matches."
160   :group 'gnus-thread
161   :type '(radio (function-item gnus-gather-threads-by-subject)
162                 (function-item gnus-gather-threads-by-references)
163                 (function :tag "other")))
164
165 (defcustom gnus-summary-same-subject ""
166   "*String indicating that the current article has the same subject as the previous.
167 This variable will only be used if the value of
168 `gnus-summary-make-false-root' is `empty'."
169   :group 'gnus-summary-format
170   :type 'string)
171
172 (defcustom gnus-summary-goto-unread t
173   "*If t, many commands will go to the next unread article.
174 This applies to marking commands as well as other commands that
175 \"naturally\" select the next article, like, for instance, `SPC' at
176 the end of an article.
177
178 If nil, the marking commands do NOT go to the next unread article
179 (they go to the next article instead).  If `never', commands that
180 usually go to the next unread article, will go to the next article,
181 whether it is read or not."
182   :group 'gnus-summary-marks
183   :link '(custom-manual "(gnus)Setting Marks")
184   :type '(choice (const :tag "off" nil)
185                  (const never)
186                  (sexp :menu-tag "on" t)))
187
188 (defcustom gnus-summary-default-score 0
189   "*Default article score level.
190 All scores generated by the score files will be added to this score.
191 If this variable is nil, scoring will be disabled."
192   :group 'gnus-score-default
193   :type '(choice (const :tag "disable")
194                  integer))
195
196 (defcustom gnus-summary-zcore-fuzz 0
197   "*Fuzziness factor for the zcore in the summary buffer.
198 Articles with scores closer than this to `gnus-summary-default-score'
199 will not be marked."
200   :group 'gnus-summary-format
201   :type 'integer)
202
203 (defcustom gnus-simplify-subject-fuzzy-regexp nil
204   "*Strings to be removed when doing fuzzy matches.
205 This can either be a regular expression or list of regular expressions
206 that will be removed from subject strings if fuzzy subject
207 simplification is selected."
208   :group 'gnus-thread
209   :type '(repeat regexp))
210
211 (defcustom gnus-show-threads t
212   "*If non-nil, display threads in summary mode."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-subtree nil
217   "*If non-nil, hide all threads initially.
218 If threads are hidden, you have to run the command
219 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
220 to expose hidden threads."
221   :group 'gnus-thread
222   :type 'boolean)
223
224 (defcustom gnus-thread-hide-killed t
225   "*If non-nil, hide killed threads automatically."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-ignore-subject t
230   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
231 If nil, articles that have different subjects from their parents will
232 start separate threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-operation-ignore-subject t
237   "*If non-nil, subjects will be ignored when doing thread commands.
238 This affects commands like `gnus-summary-kill-thread' and
239 `gnus-summary-lower-thread'.
240
241 If this variable is nil, articles in the same thread with different
242 subjects will not be included in the operation in question.  If this
243 variable is `fuzzy', only articles that have subjects that are fuzzily
244 equal will be included."
245   :group 'gnus-thread
246   :type '(choice (const :tag "off" nil)
247                  (const fuzzy)
248                  (sexp :tag "on" t)))
249
250 (defcustom gnus-thread-indent-level 4
251   "*Number that says how much each sub-thread should be indented."
252   :group 'gnus-thread
253   :type 'integer)
254
255 (defcustom gnus-auto-extend-newsgroup t
256   "*If non-nil, extend newsgroup forward and backward when requested."
257   :group 'gnus-summary-choose
258   :type 'boolean)
259
260 (defcustom gnus-auto-select-first t
261   "*If nil, don't select the first unread article when entering a group.
262 If this variable is `best', select the highest-scored unread article
263 in the group.  If t, select the first unread article.
264
265 This variable can also be a function to place point on a likely
266 subject line.  Useful values include `gnus-summary-first-unread-subject',
267 `gnus-summary-first-unread-article' and
268 `gnus-summary-best-unread-article'.
269
270 If you want to prevent automatic selection of the first unread article
271 in some newsgroups, set the variable to nil in
272 `gnus-select-group-hook'."
273   :group 'gnus-group-select
274   :type '(choice (const :tag "none" nil)
275                  (const best)
276                  (sexp :menu-tag "first" t)
277                  (function-item gnus-summary-first-unread-subject)
278                  (function-item gnus-summary-first-unread-article)
279                  (function-item gnus-summary-best-unread-article)))
280
281 (defcustom gnus-auto-select-next t
282   "*If non-nil, offer to go to the next group from the end of the previous.
283 If the value is t and the next newsgroup is empty, Gnus will exit
284 summary mode and go back to group mode.  If the value is neither nil
285 nor t, Gnus will select the following unread newsgroup.  In
286 particular, if the value is the symbol `quietly', the next unread
287 newsgroup will be selected without any confirmation, and if it is
288 `almost-quietly', the next group will be selected without any
289 confirmation if you are located on the last article in the group.
290 Finally, if this variable is `slightly-quietly', the `Z n' command
291 will go to the next group without confirmation."
292   :group 'gnus-summary-maneuvering
293   :type '(choice (const :tag "off" nil)
294                  (const quietly)
295                  (const almost-quietly)
296                  (const slightly-quietly)
297                  (sexp :menu-tag "on" t)))
298
299 (defcustom gnus-auto-select-same nil
300   "*If non-nil, select the next article with the same subject.
301 If there are no more articles with the same subject, go to
302 the first unread article."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-summary-check-current nil
307   "*If non-nil, consider the current article when moving.
308 The \"unread\" movement commands will stay on the same line if the
309 current article is unread."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-auto-center-summary t
314   "*If non-nil, always center the current summary buffer.
315 In particular, if `vertical' do only vertical recentering.  If non-nil
316 and non-`vertical', do both horizontal and vertical recentering."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "none" nil)
319                  (const vertical)
320                  (integer :tag "height")
321                  (sexp :menu-tag "both" t)))
322
323 (defcustom gnus-show-all-headers nil
324   "*If non-nil, don't hide any headers."
325   :group 'gnus-article-hiding
326   :group 'gnus-article-headers
327   :type 'boolean)
328
329 (defcustom gnus-summary-ignore-duplicates nil
330   "*If non-nil, ignore articles with identical Message-ID headers."
331   :group 'gnus-summary
332   :type 'boolean)
333
334 (defcustom gnus-single-article-buffer t
335   "*If non-nil, display all articles in the same buffer.
336 If nil, each group will get its own article buffer."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-break-pages t
341   "*If non-nil, do page breaking on articles.
342 The page delimiter is specified by the `gnus-page-delimiter'
343 variable."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable."
350   :group 'gnus-summary-mail
351   :type '(repeat (choice (list :value (fun) function)
352                          (cons :value ("" "") regexp (repeat string))
353                          (sexp :value nil))))
354
355 (defcustom gnus-unread-mark ?  ;Whitespace
356   "*Mark used for unread articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-ticked-mark ?!
361   "*Mark used for ticked articles."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-dormant-mark ??
366   "*Mark used for dormant articles."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-del-mark ?r
371   "*Mark used for del'd articles."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-read-mark ?R
376   "*Mark used for read articles."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-expirable-mark ?E
381   "*Mark used for expirable articles."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-killed-mark ?K
386   "*Mark used for killed articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-souped-mark ?F
391   "*Mark used for killed articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-kill-file-mark ?X
396   "*Mark used for articles killed by kill files."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-low-score-mark ?Y
401   "*Mark used for articles with a low score."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-catchup-mark ?C
406   "*Mark used for articles that are caught up."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-replied-mark ?A
411   "*Mark used for articles that have been replied to."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-cached-mark ?*
416   "*Mark used for articles that are in the cache."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-saved-mark ?S
421   "*Mark used for articles that have been saved to."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ancient-mark ?O
426   "*Mark used for ancient articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-sparse-mark ?Q
431   "*Mark used for sparsely reffed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-canceled-mark ?G
436   "*Mark used for canceled articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-duplicate-mark ?M
441   "*Mark used for duplicate articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-undownloaded-mark ?@
446   "*Mark used for articles that weren't downloaded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-downloadable-mark ?%
451   "*Mark used for articles that are to be downloaded."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-unsendable-mark ?=
456   "*Mark used for articles that won't be sent."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-score-over-mark ?+
461   "*Score mark used for articles with high scores."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-score-below-mark ?-
466   "*Score mark used for articles with low scores."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-empty-thread-mark ?  ;Whitespace
471   "*There is no thread under the article."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-not-empty-thread-mark ?=
476   "*There is a thread under the article."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-view-pseudo-asynchronously nil
481   "*If non-nil, Gnus will view pseudo-articles asynchronously."
482   :group 'gnus-extract-view
483   :type 'boolean)
484
485 (defcustom gnus-auto-expirable-marks
486   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
487         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
488         gnus-souped-mark gnus-duplicate-mark)
489   "*The list of marks converted into expiration if a group is auto-expirable."
490   :group 'gnus-summary
491   :type '(repeat character))
492
493 (defcustom gnus-inhibit-user-auto-expire t
494   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
495   :group 'gnus-summary
496   :type 'boolean)
497
498 (defcustom gnus-view-pseudos nil
499   "*If `automatic', pseudo-articles will be viewed automatically.
500 If `not-confirm', pseudos will be viewed automatically, and the user
501 will not be asked to confirm the command."
502   :group 'gnus-extract-view
503   :type '(choice (const :tag "off" nil)
504                  (const automatic)
505                  (const not-confirm)))
506
507 (defcustom gnus-view-pseudos-separately t
508   "*If non-nil, one pseudo-article will be created for each file to be viewed.
509 If nil, all files that use the same viewing command will be given as a
510 list of parameters to that command."
511   :group 'gnus-extract-view
512   :type 'boolean)
513
514 (defcustom gnus-insert-pseudo-articles t
515   "*If non-nil, insert pseudo-articles when decoding articles."
516   :group 'gnus-extract-view
517   :type 'boolean)
518
519 (defcustom gnus-summary-dummy-line-format
520   "  %(:                          :%) %S\n"
521   "*The format specification for the dummy roots in the summary buffer.
522 It works along the same lines as a normal formatting string,
523 with some simple extensions.
524
525 %S  The subject"
526   :group 'gnus-threading
527   :type 'string)
528
529 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
530   "*The format specification for the summary mode line.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions:
533
534 %G  Group name
535 %p  Unprefixed group name
536 %A  Current article number
537 %z  Current article score
538 %V  Gnus version
539 %U  Number of unread articles in the group
540 %e  Number of unselected articles in the group
541 %Z  A string with unread/unselected article counts
542 %g  Shortish group name
543 %S  Subject of the current article
544 %u  User-defined spec
545 %s  Current score file name
546 %d  Number of dormant articles
547 %r  Number of articles that have been marked as read in this session
548 %E  Number of articles expunged by the score files"
549   :group 'gnus-summary-format
550   :type 'string)
551
552 (defcustom gnus-list-identifiers nil
553   "Regexp that matches list identifiers to be removed from subject.
554 This can also be a list of regexps."
555   :group 'gnus-summary-format
556   :group 'gnus-article-hiding
557   :type '(choice (const :tag "none" nil)
558                  (regexp :value ".*")
559                  (repeat :value (".*") regexp)))
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-threading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook nil
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting summary mode.
704 This hook is not called from the non-updating exit commands like `Q'."
705   :group 'gnus-various
706   :type 'hook)
707
708 (defcustom gnus-summary-update-hook
709   (list 'gnus-summary-highlight-line)
710   "*A hook called when a summary line is changed.
711 The hook will not be called if `gnus-visual' is nil.
712
713 The default function `gnus-summary-highlight-line' will
714 highlight the line according to the `gnus-summary-highlight'
715 variable."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
720   "*A hook called when an article is selected for the first time.
721 The hook is intended to mark an article as read (or unread)
722 automatically when it is selected."
723   :group 'gnus-summary-choose
724   :type 'hook)
725
726 (defcustom gnus-group-no-more-groups-hook nil
727   "*A hook run when returning to group mode having no more (unread) groups."
728   :group 'gnus-group-select
729   :type 'hook)
730
731 (defcustom gnus-ps-print-hook nil
732   "*A hook run before ps-printing something from Gnus."
733   :group 'gnus-summary
734   :type 'hook)
735
736 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
737   "Face used for highlighting the current article in the summary buffer."
738   :group 'gnus-summary-visual
739   :type 'face)
740
741 (defcustom gnus-summary-highlight
742   '(((= mark gnus-canceled-mark)
743      . gnus-summary-cancelled-face)
744     ((and (> score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-high-ticked-face)
748     ((and (< score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-low-ticked-face)
752     ((or (= mark gnus-dormant-mark)
753          (= mark gnus-ticked-mark))
754      . gnus-summary-normal-ticked-face)
755     ((and (> score default) (= mark gnus-ancient-mark))
756      . gnus-summary-high-ancient-face)
757     ((and (< score default) (= mark gnus-ancient-mark))
758      . gnus-summary-low-ancient-face)
759     ((= mark gnus-ancient-mark)
760      . gnus-summary-normal-ancient-face)
761     ((and (> score default) (= mark gnus-unread-mark))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (= mark gnus-unread-mark))
764      . gnus-summary-low-unread-face)
765     ((= mark gnus-unread-mark)
766      . gnus-summary-normal-unread-face)
767     ((and (> score default) (memq mark (list gnus-downloadable-mark
768                                              gnus-undownloaded-mark)))
769      . gnus-summary-high-unread-face)
770     ((and (< score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-low-unread-face)
773     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
774      . gnus-summary-normal-unread-face)
775     ((> score default)
776      . gnus-summary-high-read-face)
777     ((< score default)
778      . gnus-summary-low-read-face)
779     (t
780      . gnus-summary-normal-read-face))
781   "*Controls the highlighting of summary buffer lines.
782
783 A list of (FORM . FACE) pairs.  When deciding how a a particular
784 summary line should be displayed, each form is evaluated.  The content
785 of the face field after the first true form is used.  You can change
786 how those summary lines are displayed, by editing the face field.
787
788 You can use the following variables in the FORM field.
789
790 score:   The articles score
791 default: The default article score.
792 below:   The score below which articles are automatically marked as read.
793 mark:    The articles mark."
794   :group 'gnus-summary-visual
795   :type '(repeat (cons (sexp :tag "Form" nil)
796                        face)))
797
798 (defcustom gnus-alter-header-function nil
799   "Function called to allow alteration of article header structures.
800 The function is called with one parameter, the article header vector,
801 which it may alter in any way.")
802
803 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
804   "Variable that says which function should be used to decode a string with encoded words.")
805
806 (defcustom gnus-extra-headers nil
807   "*Extra headers to parse."
808   :group 'gnus-summary
809   :type '(repeat symbol))
810
811 (defcustom gnus-ignored-from-addresses
812   (and user-mail-address (regexp-quote user-mail-address))
813   "*Regexp of From headers that may be suppressed in favor of To headers."
814   :group 'gnus-summary
815   :type 'regexp)
816
817 (defcustom gnus-group-charset-alist
818   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
819     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
820     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
821     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
822     ("^relcom\\>" koi8-r)
823     ("^fido7\\>" koi8-r)
824     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
825     ("^israel\\>" iso-8859-1)
826     ("^han\\>" euc-kr)
827     ("^alt.chinese.text.big5\\>" chinese-big5)
828     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
829     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
830     (".*" iso-8859-1))
831   "Alist of regexps (to match group names) and default charsets to be used when reading."
832   :type '(repeat (list (regexp :tag "Group")
833                        (symbol :tag "Charset")))
834   :group 'gnus-charset)
835
836 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
837   "List of charsets that should be ignored.
838 When these charsets are used in the \"charset\" parameter, the
839 default charset will be used instead."
840   :type '(repeat symbol)
841   :group 'gnus-charset)
842
843 (defcustom gnus-group-ignored-charsets-alist
844   '(("alt\\.chinese\\.text" iso-8859-1))
845   "Alist of regexps (to match group names) and charsets that should be ignored.
846 When these charsets are used in the \"charset\" parameter, the
847 default charset will be used instead."
848   :type '(repeat (cons (regexp :tag "Group")
849                        (repeat symbol)))
850   :group 'gnus-charset)
851
852 (defcustom gnus-group-highlight-words-alist nil
853   "Alist of group regexps and highlight regexps.
854 This variable uses the same syntax as `gnus-emphasis-alist'."
855   :type '(repeat (cons (regexp :tag "Group")
856                        (repeat (list (regexp :tag "Highlight regexp")
857                                      (number :tag "Group for entire word" 0)
858                                      (number :tag "Group for displayed part" 0)
859                                      (symbol :tag "Face"
860                                              gnus-emphasis-highlight-words)))))
861   :group 'gnus-summary-visual)
862
863 (defcustom gnus-summary-show-article-charset-alist
864   nil
865   "Alist of number and charset.
866 The article will be shown with the charset corresponding to the
867 numbered argument.
868 For example: ((1 . cn-gb-2312) (2 . big5))."
869   :type '(repeat (cons (number :tag "Argument" 1)
870                        (symbol :tag "Charset")))
871   :group 'gnus-charset)
872
873 (defcustom gnus-preserve-marks t
874   "Whether marks are preserved when moving, copying and respooling messages."
875   :type 'boolean
876   :group 'gnus-summary-marks)
877
878 (defcustom gnus-alter-articles-to-read-function nil
879   "Function to be called to alter the list of articles to be selected."
880   :type 'function
881   :group 'gnus-summary)
882
883 (defcustom gnus-orphan-score nil
884   "*All orphans get this score added.  Set in the score file."
885   :group 'gnus-score-default
886   :type '(choice (const nil)
887                  integer))
888
889 (defcustom gnus-summary-save-parts-default-mime "image/.*"
890   "*A regexp to match MIME parts when saving multiple parts of a message
891 with gnus-summary-save-parts (X m). This regexp will be used by default
892 when prompting the user for which type of files to save."
893   :group 'gnus-summary
894   :type 'regexp)
895
896
897 ;;; Internal variables
898
899 (defvar gnus-article-mime-handles nil)
900 (defvar gnus-article-decoded-p nil)
901 (defvar gnus-article-charset nil)
902 (defvar gnus-article-ignored-charsets nil)
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has been gathered together.")
915
916 (defvar gnus-summary-save-parts-type-history nil)
917 (defvar gnus-summary-save-parts-last-directory nil)
918
919 ;; Avoid highlighting in kill files.
920 (defvar gnus-summary-inhibit-highlight nil)
921 (defvar gnus-newsgroup-selected-overlay nil)
922 (defvar gnus-inhibit-limiting nil)
923 (defvar gnus-newsgroup-adaptive-score-file nil)
924 (defvar gnus-current-score-file nil)
925 (defvar gnus-current-move-group nil)
926 (defvar gnus-current-copy-group nil)
927 (defvar gnus-current-crosspost-group nil)
928
929 (defvar gnus-newsgroup-dependencies nil)
930 (defvar gnus-newsgroup-adaptive nil)
931 (defvar gnus-summary-display-article-function nil)
932 (defvar gnus-summary-highlight-line-function nil
933   "Function called after highlighting a summary line.")
934
935 (defvar gnus-summary-line-format-alist
936   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
937     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
938     (?s gnus-tmp-subject-or-nil ?s)
939     (?n gnus-tmp-name ?s)
940     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
941         ?s)
942     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
943             gnus-tmp-from) ?s)
944     (?F gnus-tmp-from ?s)
945     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
946     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
947     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
948     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
949     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
950     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
951     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
952     (?L gnus-tmp-lines ?d)
953     (?I gnus-tmp-indentation ?s)
954     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
955     (?R gnus-tmp-replied ?c)
956     (?\[ gnus-tmp-opening-bracket ?c)
957     (?\] gnus-tmp-closing-bracket ?c)
958     (?\> (make-string gnus-tmp-level ? ) ?s)
959     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
960     (?i gnus-tmp-score ?d)
961     (?z gnus-tmp-score-char ?c)
962     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
963     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
964     (?U gnus-tmp-unread ?c)
965     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
966     (?t (gnus-summary-number-of-articles-in-thread
967          (and (boundp 'thread) (car thread)) gnus-tmp-level)
968         ?d)
969     (?e (gnus-summary-number-of-articles-in-thread
970          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
971         ?c)
972     (?u gnus-tmp-user-defined ?s)
973     (?P (gnus-pick-line-number) ?d))
974   "An alist of format specifications that can appear in summary lines.
975 These are paired with what variables they correspond with, along with
976 the type of the variable (string, integer, character, etc).")
977
978 (defvar gnus-summary-dummy-line-format-alist
979   `((?S gnus-tmp-subject ?s)
980     (?N gnus-tmp-number ?d)
981     (?u gnus-tmp-user-defined ?s)))
982
983 (defvar gnus-summary-mode-line-format-alist
984   `((?G gnus-tmp-group-name ?s)
985     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
986     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
987     (?A gnus-tmp-article-number ?d)
988     (?Z gnus-tmp-unread-and-unselected ?s)
989     (?V gnus-version ?s)
990     (?U gnus-tmp-unread-and-unticked ?d)
991     (?S gnus-tmp-subject ?s)
992     (?e gnus-tmp-unselected ?d)
993     (?u gnus-tmp-user-defined ?s)
994     (?d (length gnus-newsgroup-dormant) ?d)
995     (?t (length gnus-newsgroup-marked) ?d)
996     (?r (length gnus-newsgroup-reads) ?d)
997     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
998     (?E gnus-newsgroup-expunged-tally ?d)
999     (?s (gnus-current-score-file-nondirectory) ?s)))
1000
1001 (defvar gnus-last-search-regexp nil
1002   "Default regexp for article search command.")
1003
1004 (defvar gnus-last-shell-command nil
1005   "Default shell command on article.")
1006
1007 (defvar gnus-newsgroup-begin nil)
1008 (defvar gnus-newsgroup-end nil)
1009 (defvar gnus-newsgroup-last-rmail nil)
1010 (defvar gnus-newsgroup-last-mail nil)
1011 (defvar gnus-newsgroup-last-folder nil)
1012 (defvar gnus-newsgroup-last-file nil)
1013 (defvar gnus-newsgroup-auto-expire nil)
1014 (defvar gnus-newsgroup-active nil)
1015
1016 (defvar gnus-newsgroup-data nil)
1017 (defvar gnus-newsgroup-data-reverse nil)
1018 (defvar gnus-newsgroup-limit nil)
1019 (defvar gnus-newsgroup-limits nil)
1020
1021 (defvar gnus-newsgroup-unreads nil
1022   "List of unread articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-unselected nil
1025   "List of unselected unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-reads nil
1028   "Alist of read articles and article marks in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-expunged-tally nil)
1031
1032 (defvar gnus-newsgroup-marked nil
1033   "List of ticked articles in the current newsgroup (a subset of unread art).")
1034
1035 (defvar gnus-newsgroup-killed nil
1036   "List of ranges of articles that have been through the scoring process.")
1037
1038 (defvar gnus-newsgroup-cached nil
1039   "List of articles that come from the article cache.")
1040
1041 (defvar gnus-newsgroup-saved nil
1042   "List of articles that have been saved.")
1043
1044 (defvar gnus-newsgroup-kill-headers nil)
1045
1046 (defvar gnus-newsgroup-replied nil
1047   "List of articles that have been replied to in the current newsgroup.")
1048
1049 (defvar gnus-newsgroup-expirable nil
1050   "List of articles in the current newsgroup that can be expired.")
1051
1052 (defvar gnus-newsgroup-processable nil
1053   "List of articles in the current newsgroup that can be processed.")
1054
1055 (defvar gnus-newsgroup-downloadable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-undownloaded nil
1059   "List of articles in the current newsgroup that haven't been downloaded..")
1060
1061 (defvar gnus-newsgroup-unsendable nil
1062   "List of articles in the current newsgroup that won't be sent.")
1063
1064 (defvar gnus-newsgroup-bookmarks nil
1065   "List of articles in the current newsgroup that have bookmarks.")
1066
1067 (defvar gnus-newsgroup-dormant nil
1068   "List of dormant articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-scored nil
1071   "List of scored articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-headers nil
1074   "List of article headers in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-threads nil)
1077
1078 (defvar gnus-newsgroup-prepared nil
1079   "Whether the current group has been prepared properly.")
1080
1081 (defvar gnus-newsgroup-ancient nil
1082   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-sparse nil)
1085
1086 (defvar gnus-current-article nil)
1087 (defvar gnus-article-current nil)
1088 (defvar gnus-current-headers nil)
1089 (defvar gnus-have-all-headers nil)
1090 (defvar gnus-last-article nil)
1091 (defvar gnus-newsgroup-history nil)
1092 (defvar gnus-newsgroup-charset nil)
1093 (defvar gnus-newsgroup-ephemeral-charset nil)
1094 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1095
1096 (defvar gnus-article-before-search nil)
1097
1098 (defconst gnus-summary-local-variables
1099   '(gnus-newsgroup-name
1100     gnus-newsgroup-begin gnus-newsgroup-end
1101     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1102     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1103     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1104     gnus-newsgroup-unselected gnus-newsgroup-marked
1105     gnus-newsgroup-reads gnus-newsgroup-saved
1106     gnus-newsgroup-replied gnus-newsgroup-expirable
1107     gnus-newsgroup-processable gnus-newsgroup-killed
1108     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1109     gnus-newsgroup-unsendable
1110     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1111     gnus-newsgroup-headers gnus-newsgroup-threads
1112     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1113     gnus-current-article gnus-current-headers gnus-have-all-headers
1114     gnus-last-article gnus-article-internal-prepare-hook
1115     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1116     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1117     gnus-thread-expunge-below
1118     gnus-score-alist gnus-current-score-file
1119     (gnus-summary-expunge-below . global)
1120     (gnus-summary-mark-below . global)
1121     (gnus-orphan-score . global)
1122     gnus-newsgroup-active gnus-scores-exclude-files
1123     gnus-newsgroup-history gnus-newsgroup-ancient
1124     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1125     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1126     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1127     (gnus-newsgroup-expunged-tally . 0)
1128     gnus-cache-removable-articles gnus-newsgroup-cached
1129     gnus-newsgroup-data gnus-newsgroup-data-reverse
1130     gnus-newsgroup-limit gnus-newsgroup-limits
1131     gnus-newsgroup-charset)
1132   "Variables that are buffer-local to the summary buffers.")
1133
1134 (defvar gnus-newsgroup-variables nil
1135   "Variables that have separate values in the newsgroups.")
1136
1137 ;; Byte-compiler warning.
1138 (defvar gnus-article-mode-map)
1139
1140 ;; MIME stuff.
1141
1142 (defvar gnus-decode-encoded-word-methods
1143   '(mail-decode-encoded-word-string)
1144   "List of methods used to decode encoded words.
1145
1146 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1147 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1148 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1149 whose names match REGEXP.
1150
1151 For example:
1152 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1153  mail-decode-encoded-word-string
1154  (\"chinese\" . rfc1843-decode-string))")
1155
1156 (defvar gnus-decode-encoded-word-methods-cache nil)
1157
1158 (defun gnus-multi-decode-encoded-word-string (string)
1159   "Apply the functions from `gnus-encoded-word-methods' that match."
1160   (unless (and gnus-decode-encoded-word-methods-cache
1161                (eq gnus-newsgroup-name
1162                    (car gnus-decode-encoded-word-methods-cache)))
1163     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1164     (mapcar (lambda (x)
1165               (if (symbolp x)
1166                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1167                 (if (and gnus-newsgroup-name
1168                          (string-match (car x) gnus-newsgroup-name))
1169                     (nconc gnus-decode-encoded-word-methods-cache
1170                            (list (cdr x))))))
1171           gnus-decode-encoded-word-methods))
1172   (let ((xlist gnus-decode-encoded-word-methods-cache))
1173     (pop xlist)
1174     (while xlist
1175       (setq string (funcall (pop xlist) string))))
1176   string)
1177
1178 ;; Subject simplification.
1179
1180 (defun gnus-simplify-whitespace (str)
1181   "Remove excessive whitespace from STR."
1182   (let ((mystr str))
1183     ;; Multiple spaces.
1184     (while (string-match "[ \t][ \t]+" mystr)
1185       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1186                           " "
1187                           (substring mystr (match-end 0)))))
1188     ;; Leading spaces.
1189     (when (string-match "^[ \t]+" mystr)
1190       (setq mystr (substring mystr (match-end 0))))
1191     ;; Trailing spaces.
1192     (when (string-match "[ \t]+$" mystr)
1193       (setq mystr (substring mystr 0 (match-beginning 0))))
1194     mystr))
1195
1196 (defsubst gnus-simplify-subject-re (subject)
1197   "Remove \"Re:\" from subject lines."
1198   (if (string-match "^[Rr][Ee]: *" subject)
1199       (substring subject (match-end 0))
1200     subject))
1201
1202 (defun gnus-simplify-subject (subject &optional re-only)
1203   "Remove `Re:' and words in parentheses.
1204 If RE-ONLY is non-nil, strip leading `Re:'s only."
1205   (let ((case-fold-search t))           ;Ignore case.
1206     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1207     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1208       (setq subject (substring subject (match-end 0))))
1209     ;; Remove uninteresting prefixes.
1210     (when (and (not re-only)
1211                gnus-simplify-ignored-prefixes
1212                (string-match gnus-simplify-ignored-prefixes subject))
1213       (setq subject (substring subject (match-end 0))))
1214     ;; Remove words in parentheses from end.
1215     (unless re-only
1216       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1217         (setq subject (substring subject 0 (match-beginning 0)))))
1218     ;; Return subject string.
1219     subject))
1220
1221 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1222 ;; all whitespace.
1223 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1224   (goto-char (point-min))
1225   (while (re-search-forward regexp nil t)
1226     (replace-match (or newtext ""))))
1227
1228 (defun gnus-simplify-buffer-fuzzy ()
1229   "Simplify string in the buffer fuzzily.
1230 The string in the accessible portion of the current buffer is simplified.
1231 It is assumed to be a single-line subject.
1232 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1233 matter is removed.  Additional things can be deleted by setting
1234 `gnus-simplify-subject-fuzzy-regexp'."
1235   (let ((case-fold-search t)
1236         (modified-tick))
1237     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1238
1239     (while (not (eq modified-tick (buffer-modified-tick)))
1240       (setq modified-tick (buffer-modified-tick))
1241       (cond
1242        ((listp gnus-simplify-subject-fuzzy-regexp)
1243         (mapcar 'gnus-simplify-buffer-fuzzy-step
1244                 gnus-simplify-subject-fuzzy-regexp))
1245        (gnus-simplify-subject-fuzzy-regexp
1246         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1247       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1248       (gnus-simplify-buffer-fuzzy-step
1249        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1250       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1251
1252     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1253     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1254     (gnus-simplify-buffer-fuzzy-step " $")
1255     (gnus-simplify-buffer-fuzzy-step "^ +")))
1256
1257 (defun gnus-simplify-subject-fuzzy (subject)
1258   "Simplify a subject string fuzzily.
1259 See `gnus-simplify-buffer-fuzzy' for details."
1260   (save-excursion
1261     (gnus-set-work-buffer)
1262     (let ((case-fold-search t))
1263       ;; Remove uninteresting prefixes.
1264       (when (and gnus-simplify-ignored-prefixes
1265                  (string-match gnus-simplify-ignored-prefixes subject))
1266         (setq subject (substring subject (match-end 0))))
1267       (insert subject)
1268       (inline (gnus-simplify-buffer-fuzzy))
1269       (buffer-string))))
1270
1271 (defsubst gnus-simplify-subject-fully (subject)
1272   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1273   (cond
1274    (gnus-simplify-subject-functions
1275     (gnus-map-function gnus-simplify-subject-functions subject))
1276    ((null gnus-summary-gather-subject-limit)
1277     (gnus-simplify-subject-re subject))
1278    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1279     (gnus-simplify-subject-fuzzy subject))
1280    ((numberp gnus-summary-gather-subject-limit)
1281     (gnus-limit-string (gnus-simplify-subject-re subject)
1282                        gnus-summary-gather-subject-limit))
1283    (t
1284     subject)))
1285
1286 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1287   "Check whether two subjects are equal.
1288 If optional argument simple-first is t, first argument is already
1289 simplified."
1290   (cond
1291    ((null simple-first)
1292     (equal (gnus-simplify-subject-fully s1)
1293            (gnus-simplify-subject-fully s2)))
1294    (t
1295     (equal s1
1296            (gnus-simplify-subject-fully s2)))))
1297
1298 (defun gnus-summary-bubble-group ()
1299   "Increase the score of the current group.
1300 This is a handy function to add to `gnus-summary-exit-hook' to
1301 increase the score of each group you read."
1302   (gnus-group-add-score gnus-newsgroup-name))
1303
1304 \f
1305 ;;;
1306 ;;; Gnus summary mode
1307 ;;;
1308
1309 (put 'gnus-summary-mode 'mode-class 'special)
1310
1311 (when t
1312   ;; Non-orthogonal keys
1313
1314   (gnus-define-keys gnus-summary-mode-map
1315     " " gnus-summary-next-page
1316     "\177" gnus-summary-prev-page
1317     [delete] gnus-summary-prev-page
1318     [backspace] gnus-summary-prev-page
1319     "\r" gnus-summary-scroll-up
1320     "\M-\r" gnus-summary-scroll-down
1321     "n" gnus-summary-next-unread-article
1322     "p" gnus-summary-prev-unread-article
1323     "N" gnus-summary-next-article
1324     "P" gnus-summary-prev-article
1325     "\M-\C-n" gnus-summary-next-same-subject
1326     "\M-\C-p" gnus-summary-prev-same-subject
1327     "\M-n" gnus-summary-next-unread-subject
1328     "\M-p" gnus-summary-prev-unread-subject
1329     "." gnus-summary-first-unread-article
1330     "," gnus-summary-best-unread-article
1331     "\M-s" gnus-summary-search-article-forward
1332     "\M-r" gnus-summary-search-article-backward
1333     "<" gnus-summary-beginning-of-article
1334     ">" gnus-summary-end-of-article
1335     "j" gnus-summary-goto-article
1336     "^" gnus-summary-refer-parent-article
1337     "\M-^" gnus-summary-refer-article
1338     "u" gnus-summary-tick-article-forward
1339     "!" gnus-summary-tick-article-forward
1340     "U" gnus-summary-tick-article-backward
1341     "d" gnus-summary-mark-as-read-forward
1342     "D" gnus-summary-mark-as-read-backward
1343     "E" gnus-summary-mark-as-expirable
1344     "\M-u" gnus-summary-clear-mark-forward
1345     "\M-U" gnus-summary-clear-mark-backward
1346     "k" gnus-summary-kill-same-subject-and-select
1347     "\C-k" gnus-summary-kill-same-subject
1348     "\M-\C-k" gnus-summary-kill-thread
1349     "\M-\C-l" gnus-summary-lower-thread
1350     "e" gnus-summary-edit-article
1351     "#" gnus-summary-mark-as-processable
1352     "\M-#" gnus-summary-unmark-as-processable
1353     "\M-\C-t" gnus-summary-toggle-threads
1354     "\M-\C-s" gnus-summary-show-thread
1355     "\M-\C-h" gnus-summary-hide-thread
1356     "\M-\C-f" gnus-summary-next-thread
1357     "\M-\C-b" gnus-summary-prev-thread
1358     [(meta down)] gnus-summary-next-thread
1359     [(meta up)] gnus-summary-prev-thread
1360     "\M-\C-u" gnus-summary-up-thread
1361     "\M-\C-d" gnus-summary-down-thread
1362     "&" gnus-summary-execute-command
1363     "c" gnus-summary-catchup-and-exit
1364     "\C-w" gnus-summary-mark-region-as-read
1365     "\C-t" gnus-summary-toggle-truncation
1366     "?" gnus-summary-mark-as-dormant
1367     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1368     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1369     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1370     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1371     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1372     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1373     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1374     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1375     "=" gnus-summary-expand-window
1376     "\C-x\C-s" gnus-summary-reselect-current-group
1377     "\M-g" gnus-summary-rescan-group
1378     "w" gnus-summary-stop-page-breaking
1379     "\C-c\C-r" gnus-summary-caesar-message
1380     "f" gnus-summary-followup
1381     "F" gnus-summary-followup-with-original
1382     "C" gnus-summary-cancel-article
1383     "r" gnus-summary-reply
1384     "R" gnus-summary-reply-with-original
1385     "\C-c\C-f" gnus-summary-mail-forward
1386     "o" gnus-summary-save-article
1387     "\C-o" gnus-summary-save-article-mail
1388     "|" gnus-summary-pipe-output
1389     "\M-k" gnus-summary-edit-local-kill
1390     "\M-K" gnus-summary-edit-global-kill
1391     ;; "V" gnus-version
1392     "\C-c\C-d" gnus-summary-describe-group
1393     "q" gnus-summary-exit
1394     "Q" gnus-summary-exit-no-update
1395     "\C-c\C-i" gnus-info-find-node
1396     gnus-mouse-2 gnus-mouse-pick-article
1397     "m" gnus-summary-mail-other-window
1398     "a" gnus-summary-post-news
1399     "x" gnus-summary-limit-to-unread
1400     "s" gnus-summary-isearch-article
1401     "t" gnus-summary-toggle-header
1402     "g" gnus-summary-show-article
1403     "l" gnus-summary-goto-last-article
1404     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1405     "\C-d" gnus-summary-enter-digest-group
1406     "\M-\C-d" gnus-summary-read-document
1407     "\M-\C-e" gnus-summary-edit-parameters
1408     "\M-\C-a" gnus-summary-customize-parameters
1409     "\C-c\C-b" gnus-bug
1410     "*" gnus-cache-enter-article
1411     "\M-*" gnus-cache-remove-article
1412     "\M-&" gnus-summary-universal-argument
1413     "\C-l" gnus-recenter
1414     "I" gnus-summary-increase-score
1415     "L" gnus-summary-lower-score
1416     "\M-i" gnus-symbolic-argument
1417     "h" gnus-summary-select-article-buffer
1418
1419     "b" gnus-article-view-part
1420     "\M-t" gnus-summary-toggle-display-buttonized
1421
1422     "V" gnus-summary-score-map
1423     "X" gnus-uu-extract-map
1424     "S" gnus-summary-send-map)
1425
1426   ;; Sort of orthogonal keymap
1427   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1428     "t" gnus-summary-tick-article-forward
1429     "!" gnus-summary-tick-article-forward
1430     "d" gnus-summary-mark-as-read-forward
1431     "r" gnus-summary-mark-as-read-forward
1432     "c" gnus-summary-clear-mark-forward
1433     " " gnus-summary-clear-mark-forward
1434     "e" gnus-summary-mark-as-expirable
1435     "x" gnus-summary-mark-as-expirable
1436     "?" gnus-summary-mark-as-dormant
1437     "b" gnus-summary-set-bookmark
1438     "B" gnus-summary-remove-bookmark
1439     "#" gnus-summary-mark-as-processable
1440     "\M-#" gnus-summary-unmark-as-processable
1441     "S" gnus-summary-limit-include-expunged
1442     "C" gnus-summary-catchup
1443     "H" gnus-summary-catchup-to-here
1444     "\C-c" gnus-summary-catchup-all
1445     "k" gnus-summary-kill-same-subject-and-select
1446     "K" gnus-summary-kill-same-subject
1447     "P" gnus-uu-mark-map)
1448
1449   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1450     "c" gnus-summary-clear-above
1451     "u" gnus-summary-tick-above
1452     "m" gnus-summary-mark-above
1453     "k" gnus-summary-kill-below)
1454
1455   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1456     "/" gnus-summary-limit-to-subject
1457     "n" gnus-summary-limit-to-articles
1458     "w" gnus-summary-pop-limit
1459     "s" gnus-summary-limit-to-subject
1460     "a" gnus-summary-limit-to-author
1461     "u" gnus-summary-limit-to-unread
1462     "m" gnus-summary-limit-to-marks
1463     "M" gnus-summary-limit-exclude-marks
1464     "v" gnus-summary-limit-to-score
1465     "*" gnus-summary-limit-include-cached
1466     "D" gnus-summary-limit-include-dormant
1467     "T" gnus-summary-limit-include-thread
1468     "d" gnus-summary-limit-exclude-dormant
1469     "t" gnus-summary-limit-to-age
1470     "x" gnus-summary-limit-to-extra
1471     "E" gnus-summary-limit-include-expunged
1472     "c" gnus-summary-limit-exclude-childless-dormant
1473     "C" gnus-summary-limit-mark-excluded-as-read)
1474
1475   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1476     "n" gnus-summary-next-unread-article
1477     "p" gnus-summary-prev-unread-article
1478     "N" gnus-summary-next-article
1479     "P" gnus-summary-prev-article
1480     "\C-n" gnus-summary-next-same-subject
1481     "\C-p" gnus-summary-prev-same-subject
1482     "\M-n" gnus-summary-next-unread-subject
1483     "\M-p" gnus-summary-prev-unread-subject
1484     "f" gnus-summary-first-unread-article
1485     "b" gnus-summary-best-unread-article
1486     "j" gnus-summary-goto-article
1487     "g" gnus-summary-goto-subject
1488     "l" gnus-summary-goto-last-article
1489     "o" gnus-summary-pop-article)
1490
1491   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1492     "k" gnus-summary-kill-thread
1493     "l" gnus-summary-lower-thread
1494     "i" gnus-summary-raise-thread
1495     "T" gnus-summary-toggle-threads
1496     "t" gnus-summary-rethread-current
1497     "^" gnus-summary-reparent-thread
1498     "s" gnus-summary-show-thread
1499     "S" gnus-summary-show-all-threads
1500     "h" gnus-summary-hide-thread
1501     "H" gnus-summary-hide-all-threads
1502     "n" gnus-summary-next-thread
1503     "p" gnus-summary-prev-thread
1504     "u" gnus-summary-up-thread
1505     "o" gnus-summary-top-thread
1506     "d" gnus-summary-down-thread
1507     "#" gnus-uu-mark-thread
1508     "\M-#" gnus-uu-unmark-thread)
1509
1510   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1511     "g" gnus-summary-prepare
1512     "c" gnus-summary-insert-cached-articles)
1513
1514   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1515     "c" gnus-summary-catchup-and-exit
1516     "C" gnus-summary-catchup-all-and-exit
1517     "E" gnus-summary-exit-no-update
1518     "Q" gnus-summary-exit
1519     "Z" gnus-summary-exit
1520     "n" gnus-summary-catchup-and-goto-next-group
1521     "R" gnus-summary-reselect-current-group
1522     "G" gnus-summary-rescan-group
1523     "N" gnus-summary-next-group
1524     "s" gnus-summary-save-newsrc
1525     "P" gnus-summary-prev-group)
1526
1527   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1528     " " gnus-summary-next-page
1529     "n" gnus-summary-next-page
1530     "\177" gnus-summary-prev-page
1531     [delete] gnus-summary-prev-page
1532     "p" gnus-summary-prev-page
1533     "\r" gnus-summary-scroll-up
1534     "\M-\r" gnus-summary-scroll-down
1535     "<" gnus-summary-beginning-of-article
1536     ">" gnus-summary-end-of-article
1537     "b" gnus-summary-beginning-of-article
1538     "e" gnus-summary-end-of-article
1539     "^" gnus-summary-refer-parent-article
1540     "r" gnus-summary-refer-parent-article
1541     "D" gnus-summary-enter-digest-group
1542     "R" gnus-summary-refer-references
1543     "T" gnus-summary-refer-thread
1544     "g" gnus-summary-show-article
1545     "s" gnus-summary-isearch-article
1546     "P" gnus-summary-print-article
1547     "t" gnus-article-babel)
1548
1549   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1550     "b" gnus-article-add-buttons
1551     "B" gnus-article-add-buttons-to-head
1552     "o" gnus-article-treat-overstrike
1553     "e" gnus-article-emphasize
1554     "w" gnus-article-fill-cited-article
1555     "Q" gnus-article-fill-long-lines
1556     "C" gnus-article-capitalize-sentences
1557     "c" gnus-article-remove-cr
1558     "q" gnus-article-de-quoted-unreadable
1559     "6" gnus-article-de-base64-unreadable
1560     "Z" gnus-article-decode-HZ
1561     "h" gnus-article-wash-html
1562     "s" gnus-summary-force-verify-and-decrypt
1563     "f" gnus-article-display-x-face
1564     "l" gnus-summary-stop-page-breaking
1565     "r" gnus-summary-caesar-message
1566     "t" gnus-summary-toggle-header
1567     "v" gnus-summary-verbose-headers
1568     "H" gnus-article-strip-headers-in-body
1569     "d" gnus-article-treat-dumbquotes)
1570
1571   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1572     "a" gnus-article-hide
1573     "h" gnus-article-hide-headers
1574     "b" gnus-article-hide-boring-headers
1575     "s" gnus-article-hide-signature
1576     "c" gnus-article-hide-citation
1577     "C" gnus-article-hide-citation-in-followups
1578     "l" gnus-article-hide-list-identifiers
1579     "p" gnus-article-hide-pgp
1580     "B" gnus-article-strip-banner
1581     "P" gnus-article-hide-pem
1582     "\C-c" gnus-article-hide-citation-maybe)
1583
1584   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1585     "a" gnus-article-highlight
1586     "h" gnus-article-highlight-headers
1587     "c" gnus-article-highlight-citation
1588     "s" gnus-article-highlight-signature)
1589
1590   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1591     "w" gnus-article-decode-mime-words
1592     "c" gnus-article-decode-charset
1593     "v" gnus-mime-view-all-parts
1594     "b" gnus-article-view-part)
1595
1596   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1597     "z" gnus-article-date-ut
1598     "u" gnus-article-date-ut
1599     "l" gnus-article-date-local
1600     "e" gnus-article-date-lapsed
1601     "o" gnus-article-date-original
1602     "i" gnus-article-date-iso8601
1603     "s" gnus-article-date-user)
1604
1605   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1606     "t" gnus-article-remove-trailing-blank-lines
1607     "l" gnus-article-strip-leading-blank-lines
1608     "m" gnus-article-strip-multiple-blank-lines
1609     "a" gnus-article-strip-blank-lines
1610     "A" gnus-article-strip-all-blank-lines
1611     "s" gnus-article-strip-leading-space
1612     "e" gnus-article-strip-trailing-space)
1613
1614   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1615     "v" gnus-version
1616     "f" gnus-summary-fetch-faq
1617     "d" gnus-summary-describe-group
1618     "h" gnus-summary-describe-briefly
1619     "i" gnus-info-find-node)
1620
1621   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1622     "e" gnus-summary-expire-articles
1623     "\M-\C-e" gnus-summary-expire-articles-now
1624     "\177" gnus-summary-delete-article
1625     [delete] gnus-summary-delete-article
1626     [backspace] gnus-summary-delete-article
1627     "m" gnus-summary-move-article
1628     "r" gnus-summary-respool-article
1629     "w" gnus-summary-edit-article
1630     "c" gnus-summary-copy-article
1631     "B" gnus-summary-crosspost-article
1632     "q" gnus-summary-respool-query
1633     "t" gnus-summary-respool-trace
1634     "i" gnus-summary-import-article
1635     "p" gnus-summary-article-posted-p)
1636
1637   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1638     "o" gnus-summary-save-article
1639     "m" gnus-summary-save-article-mail
1640     "F" gnus-summary-write-article-file
1641     "r" gnus-summary-save-article-rmail
1642     "f" gnus-summary-save-article-file
1643     "b" gnus-summary-save-article-body-file
1644     "h" gnus-summary-save-article-folder
1645     "v" gnus-summary-save-article-vm
1646     "p" gnus-summary-pipe-output
1647     "s" gnus-soup-add-article)
1648
1649   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1650     "b" gnus-summary-display-buttonized
1651     "m" gnus-summary-repair-multipart
1652     "v" gnus-article-view-part
1653     "o" gnus-article-save-part
1654     "c" gnus-article-copy-part
1655     "C" gnus-article-view-part-as-charset
1656     "e" gnus-article-externalize-part
1657     "i" gnus-article-inline-part
1658     "|" gnus-article-pipe-part))
1659
1660 (defun gnus-summary-make-menu-bar ()
1661   (gnus-turn-off-edit-menu 'summary)
1662
1663   (unless (boundp 'gnus-summary-misc-menu)
1664
1665     (easy-menu-define
1666      gnus-summary-kill-menu gnus-summary-mode-map ""
1667      (cons
1668       "Score"
1669       (nconc
1670        (list
1671         ["Enter score..." gnus-summary-score-entry t]
1672         ["Customize" gnus-score-customize t])
1673        (gnus-make-score-map 'increase)
1674        (gnus-make-score-map 'lower)
1675        '(("Mark"
1676           ["Kill below" gnus-summary-kill-below t]
1677           ["Mark above" gnus-summary-mark-above t]
1678           ["Tick above" gnus-summary-tick-above t]
1679           ["Clear above" gnus-summary-clear-above t])
1680          ["Current score" gnus-summary-current-score t]
1681          ["Set score" gnus-summary-set-score t]
1682          ["Switch current score file..." gnus-score-change-score-file t]
1683          ["Set mark below..." gnus-score-set-mark-below t]
1684          ["Set expunge below..." gnus-score-set-expunge-below t]
1685          ["Edit current score file" gnus-score-edit-current-scores t]
1686          ["Edit score file" gnus-score-edit-file t]
1687          ["Trace score" gnus-score-find-trace t]
1688          ["Find words" gnus-score-find-favourite-words t]
1689          ["Rescore buffer" gnus-summary-rescore t]
1690          ["Increase score..." gnus-summary-increase-score t]
1691          ["Lower score..." gnus-summary-lower-score t]))))
1692
1693     ;; Define both the Article menu in the summary buffer and the equivalent
1694     ;; Commands menu in the article buffer here for consistency.
1695     (let ((innards
1696            '(("Hide"
1697               ["All" gnus-article-hide t]
1698               ["Headers" gnus-article-hide-headers t]
1699               ["Signature" gnus-article-hide-signature t]
1700               ["Citation" gnus-article-hide-citation t]
1701               ["List identifiers" gnus-article-hide-list-identifiers t]
1702               ["PGP" gnus-article-hide-pgp t]
1703               ["Banner" gnus-article-strip-banner t]
1704               ["Boring headers" gnus-article-hide-boring-headers t])
1705              ("Highlight"
1706               ["All" gnus-article-highlight t]
1707               ["Headers" gnus-article-highlight-headers t]
1708               ["Signature" gnus-article-highlight-signature t]
1709               ["Citation" gnus-article-highlight-citation t])
1710              ("MIME"
1711               ["Words" gnus-article-decode-mime-words t]
1712               ["Charset" gnus-article-decode-charset t]
1713               ["QP" gnus-article-de-quoted-unreadable t]
1714               ["Base64" gnus-article-de-base64-unreadable t]
1715               ["View all" gnus-mime-view-all-parts t]
1716               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt])
1717              ("Date"
1718               ["Local" gnus-article-date-local t]
1719               ["ISO8601" gnus-article-date-iso8601 t]
1720               ["UT" gnus-article-date-ut t]
1721               ["Original" gnus-article-date-original t]
1722               ["Lapsed" gnus-article-date-lapsed t]
1723               ["User-defined" gnus-article-date-user t])
1724              ("Washing"
1725               ("Remove Blanks"
1726                ["Leading" gnus-article-strip-leading-blank-lines t]
1727                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1728                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1729                ["All of the above" gnus-article-strip-blank-lines t]
1730                ["All" gnus-article-strip-all-blank-lines t]
1731                ["Leading space" gnus-article-strip-leading-space t]
1732                ["Trailing space" gnus-article-strip-trailing-space t])
1733               ["Overstrike" gnus-article-treat-overstrike t]
1734               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1735               ["Emphasis" gnus-article-emphasize t]
1736               ["Word wrap" gnus-article-fill-cited-article t]
1737               ["Fill long lines" gnus-article-fill-long-lines t]
1738               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1739               ["CR" gnus-article-remove-cr t]
1740               ["Show X-Face" gnus-article-display-x-face t]
1741               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1742               ["Base64" gnus-article-de-base64-unreadable t]
1743               ["Rot 13" gnus-summary-caesar-message t]
1744               ["Unix pipe" gnus-summary-pipe-message t]
1745               ["Add buttons" gnus-article-add-buttons t]
1746               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1747               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1748               ["Verbose header" gnus-summary-verbose-headers t]
1749               ["Toggle header" gnus-summary-toggle-header t]
1750               ["Html" gnus-article-wash-html t]
1751               ["HZ" gnus-article-decode-HZ t])
1752              ("Output"
1753               ["Save in default format" gnus-summary-save-article t]
1754               ["Save in file" gnus-summary-save-article-file t]
1755               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1756               ["Save in MH folder" gnus-summary-save-article-folder t]
1757               ["Save in VM folder" gnus-summary-save-article-vm t]
1758               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1759               ["Save body in file" gnus-summary-save-article-body-file t]
1760               ["Pipe through a filter" gnus-summary-pipe-output t]
1761               ["Add to SOUP packet" gnus-soup-add-article t]
1762               ["Print" gnus-summary-print-article t])
1763              ("Backend"
1764               ["Respool article..." gnus-summary-respool-article t]
1765               ["Move article..." gnus-summary-move-article
1766                (gnus-check-backend-function
1767                 'request-move-article gnus-newsgroup-name)]
1768               ["Copy article..." gnus-summary-copy-article t]
1769               ["Crosspost article..." gnus-summary-crosspost-article
1770                (gnus-check-backend-function
1771                 'request-replace-article gnus-newsgroup-name)]
1772               ["Import file..." gnus-summary-import-article t]
1773               ["Check if posted" gnus-summary-article-posted-p t]
1774               ["Edit article" gnus-summary-edit-article
1775                (not (gnus-group-read-only-p))]
1776               ["Delete article" gnus-summary-delete-article
1777                (gnus-check-backend-function
1778                 'request-expire-articles gnus-newsgroup-name)]
1779               ["Query respool" gnus-summary-respool-query t]
1780               ["Trace respool" gnus-summary-respool-trace t]
1781               ["Delete expirable articles" gnus-summary-expire-articles-now
1782                (gnus-check-backend-function
1783                 'request-expire-articles gnus-newsgroup-name)])
1784              ("Extract"
1785               ["Uudecode" gnus-uu-decode-uu t]
1786               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1787               ["Unshar" gnus-uu-decode-unshar t]
1788               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1789               ["Save" gnus-uu-decode-save t]
1790               ["Binhex" gnus-uu-decode-binhex t]
1791               ["Postscript" gnus-uu-decode-postscript t])
1792              ("Cache"
1793               ["Enter article" gnus-cache-enter-article t]
1794               ["Remove article" gnus-cache-remove-article t])
1795              ["Translate" gnus-article-babel t]
1796              ["Select article buffer" gnus-summary-select-article-buffer t]
1797              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1798              ["Isearch article..." gnus-summary-isearch-article t]
1799              ["Beginning of the article" gnus-summary-beginning-of-article t]
1800              ["End of the article" gnus-summary-end-of-article t]
1801              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1802              ["Fetch referenced articles" gnus-summary-refer-references t]
1803              ["Fetch current thread" gnus-summary-refer-thread t]
1804              ["Fetch article with id..." gnus-summary-refer-article t]
1805              ["Redisplay" gnus-summary-show-article t])))
1806       (easy-menu-define
1807        gnus-summary-article-menu gnus-summary-mode-map ""
1808        (cons "Article" innards))
1809
1810       (easy-menu-define
1811        gnus-article-commands-menu gnus-article-mode-map ""
1812        (cons "Commands" innards)))
1813
1814     (easy-menu-define
1815      gnus-summary-thread-menu gnus-summary-mode-map ""
1816      '("Threads"
1817        ["Toggle threading" gnus-summary-toggle-threads t]
1818        ["Hide threads" gnus-summary-hide-all-threads t]
1819        ["Show threads" gnus-summary-show-all-threads t]
1820        ["Hide thread" gnus-summary-hide-thread t]
1821        ["Show thread" gnus-summary-show-thread t]
1822        ["Go to next thread" gnus-summary-next-thread t]
1823        ["Go to previous thread" gnus-summary-prev-thread t]
1824        ["Go down thread" gnus-summary-down-thread t]
1825        ["Go up thread" gnus-summary-up-thread t]
1826        ["Top of thread" gnus-summary-top-thread t]
1827        ["Mark thread as read" gnus-summary-kill-thread t]
1828        ["Lower thread score" gnus-summary-lower-thread t]
1829        ["Raise thread score" gnus-summary-raise-thread t]
1830        ["Rethread current" gnus-summary-rethread-current t]))
1831
1832     (easy-menu-define
1833      gnus-summary-post-menu gnus-summary-mode-map ""
1834      '("Post"
1835        ["Post an article" gnus-summary-post-news t]
1836        ["Followup" gnus-summary-followup t]
1837        ["Followup and yank" gnus-summary-followup-with-original t]
1838        ["Supersede article" gnus-summary-supersede-article t]
1839        ["Cancel article" gnus-summary-cancel-article t]
1840        ["Reply" gnus-summary-reply t]
1841        ["Reply and yank" gnus-summary-reply-with-original t]
1842        ["Wide reply" gnus-summary-wide-reply t]
1843        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1844        ["Mail forward" gnus-summary-mail-forward t]
1845        ["Post forward" gnus-summary-post-forward t]
1846        ["Digest and mail" gnus-uu-digest-mail-forward t]
1847        ["Digest and post" gnus-uu-digest-post-forward t]
1848        ["Resend message" gnus-summary-resend-message t]
1849        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1850        ["Send a mail" gnus-summary-mail-other-window t]
1851        ["Uuencode and post" gnus-uu-post-news t]
1852        ["Followup via news" gnus-summary-followup-to-mail t]
1853        ["Followup via news and yank"
1854         gnus-summary-followup-to-mail-with-original t]
1855        ;;("Draft"
1856        ;;["Send" gnus-summary-send-draft t]
1857        ;;["Send bounced" gnus-resend-bounced-mail t])
1858        ))
1859
1860     (easy-menu-define
1861      gnus-summary-misc-menu gnus-summary-mode-map ""
1862      '("Misc"
1863        ("Mark Read"
1864         ["Mark as read" gnus-summary-mark-as-read-forward t]
1865         ["Mark same subject and select"
1866          gnus-summary-kill-same-subject-and-select t]
1867         ["Mark same subject" gnus-summary-kill-same-subject t]
1868         ["Catchup" gnus-summary-catchup t]
1869         ["Catchup all" gnus-summary-catchup-all t]
1870         ["Catchup to here" gnus-summary-catchup-to-here t]
1871         ["Catchup region" gnus-summary-mark-region-as-read t]
1872         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1873        ("Mark Various"
1874         ["Tick" gnus-summary-tick-article-forward t]
1875         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1876         ["Remove marks" gnus-summary-clear-mark-forward t]
1877         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1878         ["Set bookmark" gnus-summary-set-bookmark t]
1879         ["Remove bookmark" gnus-summary-remove-bookmark t])
1880        ("Mark Limit"
1881         ["Marks..." gnus-summary-limit-to-marks t]
1882         ["Subject..." gnus-summary-limit-to-subject t]
1883         ["Author..." gnus-summary-limit-to-author t]
1884         ["Age..." gnus-summary-limit-to-age t]
1885         ["Extra..." gnus-summary-limit-to-extra t]
1886         ["Score" gnus-summary-limit-to-score t]
1887         ["Unread" gnus-summary-limit-to-unread t]
1888         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1889         ["Articles" gnus-summary-limit-to-articles t]
1890         ["Pop limit" gnus-summary-pop-limit t]
1891         ["Show dormant" gnus-summary-limit-include-dormant t]
1892         ["Hide childless dormant"
1893          gnus-summary-limit-exclude-childless-dormant t]
1894         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1895         ["Hide marked" gnus-summary-limit-exclude-marks t]
1896         ["Show expunged" gnus-summary-show-all-expunged t])
1897        ("Process Mark"
1898         ["Set mark" gnus-summary-mark-as-processable t]
1899         ["Remove mark" gnus-summary-unmark-as-processable t]
1900         ["Remove all marks" gnus-summary-unmark-all-processable t]
1901         ["Mark above" gnus-uu-mark-over t]
1902         ["Mark series" gnus-uu-mark-series t]
1903         ["Mark region" gnus-uu-mark-region t]
1904         ["Unmark region" gnus-uu-unmark-region t]
1905         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1906         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1907         ["Mark all" gnus-uu-mark-all t]
1908         ["Mark buffer" gnus-uu-mark-buffer t]
1909         ["Mark sparse" gnus-uu-mark-sparse t]
1910         ["Mark thread" gnus-uu-mark-thread t]
1911         ["Unmark thread" gnus-uu-unmark-thread t]
1912         ("Process Mark Sets"
1913          ["Kill" gnus-summary-kill-process-mark t]
1914          ["Yank" gnus-summary-yank-process-mark
1915           gnus-newsgroup-process-stack]
1916          ["Save" gnus-summary-save-process-mark t]))
1917        ("Scroll article"
1918         ["Page forward" gnus-summary-next-page t]
1919         ["Page backward" gnus-summary-prev-page t]
1920         ["Line forward" gnus-summary-scroll-up t])
1921        ("Move"
1922         ["Next unread article" gnus-summary-next-unread-article t]
1923         ["Previous unread article" gnus-summary-prev-unread-article t]
1924         ["Next article" gnus-summary-next-article t]
1925         ["Previous article" gnus-summary-prev-article t]
1926         ["Next unread subject" gnus-summary-next-unread-subject t]
1927         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1928         ["Next article same subject" gnus-summary-next-same-subject t]
1929         ["Previous article same subject" gnus-summary-prev-same-subject t]
1930         ["First unread article" gnus-summary-first-unread-article t]
1931         ["Best unread article" gnus-summary-best-unread-article t]
1932         ["Go to subject number..." gnus-summary-goto-subject t]
1933         ["Go to article number..." gnus-summary-goto-article t]
1934         ["Go to the last article" gnus-summary-goto-last-article t]
1935         ["Pop article off history" gnus-summary-pop-article t])
1936        ("Sort"
1937         ["Sort by number" gnus-summary-sort-by-number t]
1938         ["Sort by author" gnus-summary-sort-by-author t]
1939         ["Sort by subject" gnus-summary-sort-by-subject t]
1940         ["Sort by date" gnus-summary-sort-by-date t]
1941         ["Sort by score" gnus-summary-sort-by-score t]
1942         ["Sort by lines" gnus-summary-sort-by-lines t]
1943         ["Sort by characters" gnus-summary-sort-by-chars t])
1944        ("Help"
1945         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1946         ["Describe group" gnus-summary-describe-group t]
1947         ["Read manual" gnus-info-find-node t])
1948        ("Modes"
1949         ["Pick and read" gnus-pick-mode t]
1950         ["Binary" gnus-binary-mode t])
1951        ("Regeneration"
1952         ["Regenerate" gnus-summary-prepare t]
1953         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1954         ["Toggle threading" gnus-summary-toggle-threads t])
1955        ["Filter articles..." gnus-summary-execute-command t]
1956        ["Run command on subjects..." gnus-summary-universal-argument t]
1957        ["Search articles forward..." gnus-summary-search-article-forward t]
1958        ["Search articles backward..." gnus-summary-search-article-backward t]
1959        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1960        ["Expand window" gnus-summary-expand-window t]
1961        ["Expire expirable articles" gnus-summary-expire-articles
1962         (gnus-check-backend-function
1963          'request-expire-articles gnus-newsgroup-name)]
1964        ["Edit local kill file" gnus-summary-edit-local-kill t]
1965        ["Edit main kill file" gnus-summary-edit-global-kill t]
1966        ["Edit group parameters" gnus-summary-edit-parameters t]
1967        ["Customize group parameters" gnus-summary-customize-parameters t]
1968        ["Send a bug report" gnus-bug t]
1969        ("Exit"
1970         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1971         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1972         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1973         ["Exit group" gnus-summary-exit t]
1974         ["Exit group without updating" gnus-summary-exit-no-update t]
1975         ["Exit and goto next group" gnus-summary-next-group t]
1976         ["Exit and goto prev group" gnus-summary-prev-group t]
1977         ["Reselect group" gnus-summary-reselect-current-group t]
1978         ["Rescan group" gnus-summary-rescan-group t]
1979         ["Update dribble" gnus-summary-save-newsrc t])))
1980
1981     (gnus-run-hooks 'gnus-summary-menu-hook)))
1982
1983 (defun gnus-score-set-default (var value)
1984   "A version of set that updates the GNU Emacs menu-bar."
1985   (set var value)
1986   ;; It is the message that forces the active status to be updated.
1987   (message ""))
1988
1989 (defun gnus-make-score-map (type)
1990   "Make a summary score map of type TYPE."
1991   (if t
1992       nil
1993     (let ((headers '(("author" "from" string)
1994                      ("subject" "subject" string)
1995                      ("article body" "body" string)
1996                      ("article head" "head" string)
1997                      ("xref" "xref" string)
1998                      ("extra header" "extra" string)
1999                      ("lines" "lines" number)
2000                      ("followups to author" "followup" string)))
2001           (types '((number ("less than" <)
2002                            ("greater than" >)
2003                            ("equal" =))
2004                    (string ("substring" s)
2005                            ("exact string" e)
2006                            ("fuzzy string" f)
2007                            ("regexp" r))))
2008           (perms '(("temporary" (current-time-string))
2009                    ("permanent" nil)
2010                    ("immediate" now)))
2011           header)
2012       (list
2013        (apply
2014         'nconc
2015         (list
2016          (if (eq type 'lower)
2017              "Lower score"
2018            "Increase score"))
2019         (let (outh)
2020           (while headers
2021             (setq header (car headers))
2022             (setq outh
2023                   (cons
2024                    (apply
2025                     'nconc
2026                     (list (car header))
2027                     (let ((ts (cdr (assoc (nth 2 header) types)))
2028                           outt)
2029                       (while ts
2030                         (setq outt
2031                               (cons
2032                                (apply
2033                                 'nconc
2034                                 (list (caar ts))
2035                                 (let ((ps perms)
2036                                       outp)
2037                                   (while ps
2038                                     (setq outp
2039                                           (cons
2040                                            (vector
2041                                             (caar ps)
2042                                             (list
2043                                              'gnus-summary-score-entry
2044                                              (nth 1 header)
2045                                              (if (or (string= (nth 1 header)
2046                                                               "head")
2047                                                      (string= (nth 1 header)
2048                                                               "body"))
2049                                                  ""
2050                                                (list 'gnus-summary-header
2051                                                      (nth 1 header)))
2052                                              (list 'quote (nth 1 (car ts)))
2053                                              (list 'gnus-score-delta-default
2054                                                    nil)
2055                                              (nth 1 (car ps))
2056                                              t)
2057                                             t)
2058                                            outp))
2059                                     (setq ps (cdr ps)))
2060                                   (list (nreverse outp))))
2061                                outt))
2062                         (setq ts (cdr ts)))
2063                       (list (nreverse outt))))
2064                    outh))
2065             (setq headers (cdr headers)))
2066           (list (nreverse outh))))))))
2067
2068 \f
2069
2070 (defun gnus-summary-mode (&optional group)
2071   "Major mode for reading articles.
2072
2073 All normal editing commands are switched off.
2074 \\<gnus-summary-mode-map>
2075 Each line in this buffer represents one article.  To read an
2076 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2077 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2078 respectively.
2079
2080 You can also post articles and send mail from this buffer.  To
2081 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2082 of an article, type `\\[gnus-summary-reply]'.
2083
2084 There are approx. one gazillion commands you can execute in this
2085 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2086
2087 The following commands are available:
2088
2089 \\{gnus-summary-mode-map}"
2090   (interactive)
2091   (when (gnus-visual-p 'summary-menu 'menu)
2092     (gnus-summary-make-menu-bar))
2093   (kill-all-local-variables)
2094   (gnus-summary-make-local-variables)
2095   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2096     (gnus-summary-make-local-variables))
2097   (gnus-make-thread-indent-array)
2098   (gnus-simplify-mode-line)
2099   (setq major-mode 'gnus-summary-mode)
2100   (setq mode-name "Summary")
2101   (make-local-variable 'minor-mode-alist)
2102   (use-local-map gnus-summary-mode-map)
2103   (buffer-disable-undo)
2104   (setq buffer-read-only t)             ;Disable modification
2105   (setq truncate-lines t)
2106   (setq selective-display t)
2107   (setq selective-display-ellipses t)   ;Display `...'
2108   (gnus-summary-set-display-table)
2109   (gnus-set-default-directory)
2110   (setq gnus-newsgroup-name group)
2111   (make-local-variable 'gnus-summary-line-format)
2112   (make-local-variable 'gnus-summary-line-format-spec)
2113   (make-local-variable 'gnus-summary-dummy-line-format)
2114   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2115   (make-local-variable 'gnus-summary-mark-positions)
2116   (make-local-hook 'pre-command-hook)
2117   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2118   (gnus-run-hooks 'gnus-summary-mode-hook)
2119   (mm-enable-multibyte-mule4)
2120   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2121   (gnus-update-summary-mark-positions))
2122
2123 (defun gnus-summary-make-local-variables ()
2124   "Make all the local summary buffer variables."
2125   (let (global)
2126     (dolist (local gnus-summary-local-variables)
2127       (if (consp local)
2128           (progn
2129             (if (eq (cdr local) 'global)
2130                 ;; Copy the global value of the variable.
2131                 (setq global (symbol-value (car local)))
2132               ;; Use the value from the list.
2133               (setq global (eval (cdr local))))
2134             (set (make-local-variable (car local)) global))
2135         ;; Simple nil-valued local variable.
2136         (set (make-local-variable local) nil)))))
2137
2138 (defun gnus-summary-clear-local-variables ()
2139   (let ((locals gnus-summary-local-variables))
2140     (while locals
2141       (if (consp (car locals))
2142           (and (vectorp (caar locals))
2143                (set (caar locals) nil))
2144         (and (vectorp (car locals))
2145              (set (car locals) nil)))
2146       (setq locals (cdr locals)))))
2147
2148 ;; Summary data functions.
2149
2150 (defmacro gnus-data-number (data)
2151   `(car ,data))
2152
2153 (defmacro gnus-data-set-number (data number)
2154   `(setcar ,data ,number))
2155
2156 (defmacro gnus-data-mark (data)
2157   `(nth 1 ,data))
2158
2159 (defmacro gnus-data-set-mark (data mark)
2160   `(setcar (nthcdr 1 ,data) ,mark))
2161
2162 (defmacro gnus-data-pos (data)
2163   `(nth 2 ,data))
2164
2165 (defmacro gnus-data-set-pos (data pos)
2166   `(setcar (nthcdr 2 ,data) ,pos))
2167
2168 (defmacro gnus-data-header (data)
2169   `(nth 3 ,data))
2170
2171 (defmacro gnus-data-set-header (data header)
2172   `(setf (nth 3 ,data) ,header))
2173
2174 (defmacro gnus-data-level (data)
2175   `(nth 4 ,data))
2176
2177 (defmacro gnus-data-unread-p (data)
2178   `(= (nth 1 ,data) gnus-unread-mark))
2179
2180 (defmacro gnus-data-read-p (data)
2181   `(/= (nth 1 ,data) gnus-unread-mark))
2182
2183 (defmacro gnus-data-pseudo-p (data)
2184   `(consp (nth 3 ,data)))
2185
2186 (defmacro gnus-data-find (number)
2187   `(assq ,number gnus-newsgroup-data))
2188
2189 (defmacro gnus-data-find-list (number &optional data)
2190   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2191      (memq (assq ,number bdata)
2192            bdata)))
2193
2194 (defmacro gnus-data-make (number mark pos header level)
2195   `(list ,number ,mark ,pos ,header ,level))
2196
2197 (defun gnus-data-enter (after-article number mark pos header level offset)
2198   (let ((data (gnus-data-find-list after-article)))
2199     (unless data
2200       (error "No such article: %d" after-article))
2201     (setcdr data (cons (gnus-data-make number mark pos header level)
2202                        (cdr data)))
2203     (setq gnus-newsgroup-data-reverse nil)
2204     (gnus-data-update-list (cddr data) offset)))
2205
2206 (defun gnus-data-enter-list (after-article list &optional offset)
2207   (when list
2208     (let ((data (and after-article (gnus-data-find-list after-article)))
2209           (ilist list))
2210       (if (not (or data
2211                    after-article))
2212           (let ((odata gnus-newsgroup-data))
2213             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2214             (when offset
2215               (gnus-data-update-list odata offset)))
2216         ;; Find the last element in the list to be spliced into the main
2217         ;; list.
2218         (while (cdr list)
2219           (setq list (cdr list)))
2220         (if (not data)
2221             (progn
2222               (setcdr list gnus-newsgroup-data)
2223               (setq gnus-newsgroup-data ilist)
2224               (when offset
2225                 (gnus-data-update-list (cdr list) offset)))
2226           (setcdr list (cdr data))
2227           (setcdr data ilist)
2228           (when offset
2229             (gnus-data-update-list (cdr list) offset))))
2230       (setq gnus-newsgroup-data-reverse nil))))
2231
2232 (defun gnus-data-remove (article &optional offset)
2233   (let ((data gnus-newsgroup-data))
2234     (if (= (gnus-data-number (car data)) article)
2235         (progn
2236           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2237                 gnus-newsgroup-data-reverse nil)
2238           (when offset
2239             (gnus-data-update-list gnus-newsgroup-data offset)))
2240       (while (cdr data)
2241         (when (= (gnus-data-number (cadr data)) article)
2242           (setcdr data (cddr data))
2243           (when offset
2244             (gnus-data-update-list (cdr data) offset))
2245           (setq data nil
2246                 gnus-newsgroup-data-reverse nil))
2247         (setq data (cdr data))))))
2248
2249 (defmacro gnus-data-list (backward)
2250   `(if ,backward
2251        (or gnus-newsgroup-data-reverse
2252            (setq gnus-newsgroup-data-reverse
2253                  (reverse gnus-newsgroup-data)))
2254      gnus-newsgroup-data))
2255
2256 (defun gnus-data-update-list (data offset)
2257   "Add OFFSET to the POS of all data entries in DATA."
2258   (setq gnus-newsgroup-data-reverse nil)
2259   (while data
2260     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2261     (setq data (cdr data))))
2262
2263 (defun gnus-summary-article-pseudo-p (article)
2264   "Say whether this article is a pseudo article or not."
2265   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2266
2267 (defmacro gnus-summary-article-sparse-p (article)
2268   "Say whether this article is a sparse article or not."
2269   `(memq ,article gnus-newsgroup-sparse))
2270
2271 (defmacro gnus-summary-article-ancient-p (article)
2272   "Say whether this article is a sparse article or not."
2273   `(memq ,article gnus-newsgroup-ancient))
2274
2275 (defun gnus-article-parent-p (number)
2276   "Say whether this article is a parent or not."
2277   (let ((data (gnus-data-find-list number)))
2278     (and (cdr data)                     ; There has to be an article after...
2279          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2280             (gnus-data-level (nth 1 data))))))
2281
2282 (defun gnus-article-children (number)
2283   "Return a list of all children to NUMBER."
2284   (let* ((data (gnus-data-find-list number))
2285          (level (gnus-data-level (car data)))
2286          children)
2287     (setq data (cdr data))
2288     (while (and data
2289                 (= (gnus-data-level (car data)) (1+ level)))
2290       (push (gnus-data-number (car data)) children)
2291       (setq data (cdr data)))
2292     children))
2293
2294 (defmacro gnus-summary-skip-intangible ()
2295   "If the current article is intangible, then jump to a different article."
2296   '(let ((to (get-text-property (point) 'gnus-intangible)))
2297      (and to (gnus-summary-goto-subject to))))
2298
2299 (defmacro gnus-summary-article-intangible-p ()
2300   "Say whether this article is intangible or not."
2301   '(get-text-property (point) 'gnus-intangible))
2302
2303 (defun gnus-article-read-p (article)
2304   "Say whether ARTICLE is read or not."
2305   (not (or (memq article gnus-newsgroup-marked)
2306            (memq article gnus-newsgroup-unreads)
2307            (memq article gnus-newsgroup-unselected)
2308            (memq article gnus-newsgroup-dormant))))
2309
2310 ;; Some summary mode macros.
2311
2312 (defmacro gnus-summary-article-number ()
2313   "The article number of the article on the current line.
2314 If there isn's an article number here, then we return the current
2315 article number."
2316   '(progn
2317      (gnus-summary-skip-intangible)
2318      (or (get-text-property (point) 'gnus-number)
2319          (gnus-summary-last-subject))))
2320
2321 (defmacro gnus-summary-article-header (&optional number)
2322   "Return the header of article NUMBER."
2323   `(gnus-data-header (gnus-data-find
2324                       ,(or number '(gnus-summary-article-number)))))
2325
2326 (defmacro gnus-summary-thread-level (&optional number)
2327   "Return the level of thread that starts with article NUMBER."
2328   `(if (and (eq gnus-summary-make-false-root 'dummy)
2329             (get-text-property (point) 'gnus-intangible))
2330        0
2331      (gnus-data-level (gnus-data-find
2332                        ,(or number '(gnus-summary-article-number))))))
2333
2334 (defmacro gnus-summary-article-mark (&optional number)
2335   "Return the mark of article NUMBER."
2336   `(gnus-data-mark (gnus-data-find
2337                     ,(or number '(gnus-summary-article-number)))))
2338
2339 (defmacro gnus-summary-article-pos (&optional number)
2340   "Return the position of the line of article NUMBER."
2341   `(gnus-data-pos (gnus-data-find
2342                    ,(or number '(gnus-summary-article-number)))))
2343
2344 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2345 (defmacro gnus-summary-article-subject (&optional number)
2346   "Return current subject string or nil if nothing."
2347   `(let ((headers
2348           ,(if number
2349                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2350              '(gnus-data-header (assq (gnus-summary-article-number)
2351                                       gnus-newsgroup-data)))))
2352      (and headers
2353           (vectorp headers)
2354           (mail-header-subject headers))))
2355
2356 (defmacro gnus-summary-article-score (&optional number)
2357   "Return current article score."
2358   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2359                   gnus-newsgroup-scored))
2360        gnus-summary-default-score 0))
2361
2362 (defun gnus-summary-article-children (&optional number)
2363   "Return a list of article numbers that are children of article NUMBER."
2364   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2365          (level (gnus-data-level (car data)))
2366          l children)
2367     (while (and (setq data (cdr data))
2368                 (> (setq l (gnus-data-level (car data))) level))
2369       (and (= (1+ level) l)
2370            (push (gnus-data-number (car data))
2371                  children)))
2372     (nreverse children)))
2373
2374 (defun gnus-summary-article-parent (&optional number)
2375   "Return the article number of the parent of article NUMBER."
2376   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2377                                     (gnus-data-list t)))
2378          (level (gnus-data-level (car data))))
2379     (if (zerop level)
2380         ()                              ; This is a root.
2381       ;; We search until we find an article with a level less than
2382       ;; this one.  That function has to be the parent.
2383       (while (and (setq data (cdr data))
2384                   (not (< (gnus-data-level (car data)) level))))
2385       (and data (gnus-data-number (car data))))))
2386
2387 (defun gnus-unread-mark-p (mark)
2388   "Say whether MARK is the unread mark."
2389   (= mark gnus-unread-mark))
2390
2391 (defun gnus-read-mark-p (mark)
2392   "Say whether MARK is one of the marks that mark as read.
2393 This is all marks except unread, ticked, dormant, and expirable."
2394   (not (or (= mark gnus-unread-mark)
2395            (= mark gnus-ticked-mark)
2396            (= mark gnus-dormant-mark)
2397            (= mark gnus-expirable-mark))))
2398
2399 (defmacro gnus-article-mark (number)
2400   "Return the MARK of article NUMBER.
2401 This macro should only be used when computing the mark the \"first\"
2402 time; i.e., when generating the summary lines.  After that,
2403 `gnus-summary-article-mark' should be used to examine the
2404 marks of articles."
2405   `(cond
2406     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2407     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2408     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2409     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2410     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2411     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2412     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2413     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2414            gnus-ancient-mark))))
2415
2416 ;; Saving hidden threads.
2417
2418 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2419 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2420
2421 (defmacro gnus-save-hidden-threads (&rest forms)
2422   "Save hidden threads, eval FORMS, and restore the hidden threads."
2423   (let ((config (make-symbol "config")))
2424     `(let ((,config (gnus-hidden-threads-configuration)))
2425        (unwind-protect
2426            (save-excursion
2427              ,@forms)
2428          (gnus-restore-hidden-threads-configuration ,config)))))
2429
2430 (defun gnus-data-compute-positions ()
2431   "Compute the positions of all articles."
2432   (setq gnus-newsgroup-data-reverse nil)
2433   (let ((data gnus-newsgroup-data))
2434     (save-excursion
2435       (gnus-save-hidden-threads
2436         (gnus-summary-show-all-threads)
2437         (goto-char (point-min))
2438         (while data
2439           (while (get-text-property (point) 'gnus-intangible)
2440             (forward-line 1))
2441           (gnus-data-set-pos (car data) (+ (point) 3))
2442           (setq data (cdr data))
2443           (forward-line 1))))))
2444
2445 (defun gnus-hidden-threads-configuration ()
2446   "Return the current hidden threads configuration."
2447   (save-excursion
2448     (let (config)
2449       (goto-char (point-min))
2450       (while (search-forward "\r" nil t)
2451         (push (1- (point)) config))
2452       config)))
2453
2454 (defun gnus-restore-hidden-threads-configuration (config)
2455   "Restore hidden threads configuration from CONFIG."
2456   (save-excursion
2457     (let (point buffer-read-only)
2458       (while (setq point (pop config))
2459         (when (and (< point (point-max))
2460                    (goto-char point)
2461                    (eq (char-after) ?\n))
2462           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2463
2464 ;; Various summary mode internalish functions.
2465
2466 (defun gnus-mouse-pick-article (e)
2467   (interactive "e")
2468   (mouse-set-point e)
2469   (gnus-summary-next-page nil t))
2470
2471 (defun gnus-summary-set-display-table ()
2472   "Change the display table.
2473 Odd characters have a tendency to mess
2474 up nicely formatted displays - we make all possible glyphs
2475 display only a single character."
2476
2477   ;; We start from the standard display table, if any.
2478   (let ((table (or (copy-sequence standard-display-table)
2479                    (make-display-table)))
2480         (i 32))
2481     ;; Nix out all the control chars...
2482     (while (>= (setq i (1- i)) 0)
2483       (aset table i [??]))
2484     ;; ... but not newline and cr, of course.  (cr is necessary for the
2485     ;; selective display).
2486     (aset table ?\n nil)
2487     (aset table ?\r nil)
2488     ;; We keep TAB as well.
2489     (aset table ?\t nil)
2490     ;; We nix out any glyphs over 126 that are not set already.
2491     (let ((i 256))
2492       (while (>= (setq i (1- i)) 127)
2493         ;; Only modify if the entry is nil.
2494         (unless (aref table i)
2495           (aset table i [??]))))
2496     (setq buffer-display-table table)))
2497
2498 (defun gnus-summary-setup-buffer (group)
2499   "Initialize summary buffer."
2500   (let ((buffer (concat "*Summary " group "*")))
2501     (if (get-buffer buffer)
2502         (progn
2503           (set-buffer buffer)
2504           (setq gnus-summary-buffer (current-buffer))
2505           (not gnus-newsgroup-prepared))
2506       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2507       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2508       (gnus-summary-mode group)
2509       (when gnus-carpal
2510         (gnus-carpal-setup-buffer 'summary))
2511       (unless gnus-single-article-buffer
2512         (make-local-variable 'gnus-article-buffer)
2513         (make-local-variable 'gnus-article-current)
2514         (make-local-variable 'gnus-original-article-buffer))
2515       (setq gnus-newsgroup-name group)
2516       t)))
2517
2518 (defun gnus-set-global-variables ()
2519   "Set the global equivalents of the buffer-local variables.
2520 They are set to the latest values they had.  These reflect the summary
2521 buffer that was in action when the last article was fetched."
2522   (when (eq major-mode 'gnus-summary-mode)
2523     (setq gnus-summary-buffer (current-buffer))
2524     (let ((name gnus-newsgroup-name)
2525           (marked gnus-newsgroup-marked)
2526           (unread gnus-newsgroup-unreads)
2527           (headers gnus-current-headers)
2528           (data gnus-newsgroup-data)
2529           (summary gnus-summary-buffer)
2530           (article-buffer gnus-article-buffer)
2531           (original gnus-original-article-buffer)
2532           (gac gnus-article-current)
2533           (reffed gnus-reffed-article-number)
2534           (score-file gnus-current-score-file)
2535           (default-charset gnus-newsgroup-charset)
2536           vlist)
2537       (let ((locals gnus-newsgroup-variables))
2538         (while locals
2539           (if (consp (car locals))
2540               (push (eval (caar locals)) vlist)
2541             (push (eval (car locals)) vlist))
2542           (setq locals (cdr locals)))
2543         (setq vlist (nreverse vlist)))
2544       (save-excursion
2545         (set-buffer gnus-group-buffer)
2546         (setq gnus-newsgroup-name name
2547               gnus-newsgroup-marked marked
2548               gnus-newsgroup-unreads unread
2549               gnus-current-headers headers
2550               gnus-newsgroup-data data
2551               gnus-article-current gac
2552               gnus-summary-buffer summary
2553               gnus-article-buffer article-buffer
2554               gnus-original-article-buffer original
2555               gnus-reffed-article-number reffed
2556               gnus-current-score-file score-file
2557               gnus-newsgroup-charset default-charset)
2558         (let ((locals gnus-newsgroup-variables))
2559           (while locals
2560             (if (consp (car locals))
2561                 (set (caar locals) (pop vlist))
2562               (set (car locals) (pop vlist)))
2563             (setq locals (cdr locals))))
2564         ;; The article buffer also has local variables.
2565         (when (gnus-buffer-live-p gnus-article-buffer)
2566           (set-buffer gnus-article-buffer)
2567           (setq gnus-summary-buffer summary))))))
2568
2569 (defun gnus-summary-article-unread-p (article)
2570   "Say whether ARTICLE is unread or not."
2571   (memq article gnus-newsgroup-unreads))
2572
2573 (defun gnus-summary-first-article-p (&optional article)
2574   "Return whether ARTICLE is the first article in the buffer."
2575   (if (not (setq article (or article (gnus-summary-article-number))))
2576       nil
2577     (eq article (caar gnus-newsgroup-data))))
2578
2579 (defun gnus-summary-last-article-p (&optional article)
2580   "Return whether ARTICLE is the last article in the buffer."
2581   (if (not (setq article (or article (gnus-summary-article-number))))
2582       ;; All non-existent numbers are the last article.  :-)
2583       t
2584     (not (cdr (gnus-data-find-list article)))))
2585
2586 (defun gnus-make-thread-indent-array ()
2587   (let ((n 200))
2588     (unless (and gnus-thread-indent-array
2589                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2590       (setq gnus-thread-indent-array (make-vector 201 "")
2591             gnus-thread-indent-array-level gnus-thread-indent-level)
2592       (while (>= n 0)
2593         (aset gnus-thread-indent-array n
2594               (make-string (* n gnus-thread-indent-level) ? ))
2595         (setq n (1- n))))))
2596
2597 (defun gnus-update-summary-mark-positions ()
2598   "Compute where the summary marks are to go."
2599   (save-excursion
2600     (when (gnus-buffer-exists-p gnus-summary-buffer)
2601       (set-buffer gnus-summary-buffer))
2602     (let ((gnus-replied-mark 129)
2603           (gnus-score-below-mark 130)
2604           (gnus-score-over-mark 130)
2605           (gnus-download-mark 131)
2606           (spec gnus-summary-line-format-spec)
2607           gnus-visual pos)
2608       (save-excursion
2609         (gnus-set-work-buffer)
2610         (let ((gnus-summary-line-format-spec spec)
2611               (gnus-newsgroup-downloadable '((0 . t))))
2612           (gnus-summary-insert-line
2613            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2614           (goto-char (point-min))
2615           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2616                                              (- (point) 2)))))
2617           (goto-char (point-min))
2618           (push (cons 'replied (and (search-forward "\201" nil t)
2619                                     (- (point) 2)))
2620                 pos)
2621           (goto-char (point-min))
2622           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2623                 pos)
2624           (goto-char (point-min))
2625           (push (cons 'download
2626                       (and (search-forward "\203" nil t) (- (point) 2)))
2627                 pos)))
2628       (setq gnus-summary-mark-positions pos))))
2629
2630 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2631   "Insert a dummy root in the summary buffer."
2632   (beginning-of-line)
2633   (gnus-add-text-properties
2634    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2635    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2636
2637 (defun gnus-summary-from-or-to-or-newsgroups (header)
2638   (let ((to (cdr (assq 'To (mail-header-extra header))))
2639         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2640         (mail-parse-charset gnus-newsgroup-charset)
2641         (mail-parse-ignored-charsets
2642          (save-excursion (set-buffer gnus-summary-buffer)
2643                          gnus-newsgroup-ignored-charsets)))
2644     (cond
2645      ((and to
2646            gnus-ignored-from-addresses
2647            (string-match gnus-ignored-from-addresses
2648                          (mail-header-from header)))
2649       (concat "-> "
2650               (or (car (funcall gnus-extract-address-components
2651                                 (funcall
2652                                  gnus-decode-encoded-word-function to)))
2653                   (funcall gnus-decode-encoded-word-function to))))
2654      ((and newsgroups
2655            gnus-ignored-from-addresses
2656            (string-match gnus-ignored-from-addresses
2657                          (mail-header-from header)))
2658       (concat "=> " newsgroups))
2659      (t
2660       (or (car (funcall gnus-extract-address-components
2661                         (mail-header-from header)))
2662           (mail-header-from header))))))
2663
2664 (defun gnus-summary-insert-line (gnus-tmp-header
2665                                  gnus-tmp-level gnus-tmp-current
2666                                  gnus-tmp-unread gnus-tmp-replied
2667                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2668                                  &optional gnus-tmp-dummy gnus-tmp-score
2669                                  gnus-tmp-process)
2670   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2671          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2672          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2673          (gnus-tmp-score-char
2674           (if (or (null gnus-summary-default-score)
2675                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2676                       gnus-summary-zcore-fuzz))
2677               ?  ;Whitespace
2678             (if (< gnus-tmp-score gnus-summary-default-score)
2679                 gnus-score-below-mark gnus-score-over-mark)))
2680          (gnus-tmp-replied
2681           (cond (gnus-tmp-process gnus-process-mark)
2682                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2683                  gnus-cached-mark)
2684                 (gnus-tmp-replied gnus-replied-mark)
2685                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2686                  gnus-saved-mark)
2687                 (t gnus-unread-mark)))
2688          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2689          (gnus-tmp-name
2690           (cond
2691            ((string-match "<[^>]+> *$" gnus-tmp-from)
2692             (let ((beg (match-beginning 0)))
2693               (or (and (string-match "^\".+\"" gnus-tmp-from)
2694                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2695                   (substring gnus-tmp-from 0 beg))))
2696            ((string-match "(.+)" gnus-tmp-from)
2697             (substring gnus-tmp-from
2698                        (1+ (match-beginning 0)) (1- (match-end 0))))
2699            (t gnus-tmp-from)))
2700          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2701          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2702          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2703          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2704          (buffer-read-only nil))
2705     (when (string= gnus-tmp-name "")
2706       (setq gnus-tmp-name gnus-tmp-from))
2707     (unless (numberp gnus-tmp-lines)
2708       (setq gnus-tmp-lines 0))
2709     (gnus-put-text-property
2710      (point)
2711      (progn (eval gnus-summary-line-format-spec) (point))
2712      'gnus-number gnus-tmp-number)
2713     (when (gnus-visual-p 'summary-highlight 'highlight)
2714       (forward-line -1)
2715       (gnus-run-hooks 'gnus-summary-update-hook)
2716       (forward-line 1))))
2717
2718 (defun gnus-summary-update-line (&optional dont-update)
2719   "Update summary line after change."
2720   (when (and gnus-summary-default-score
2721              (not gnus-summary-inhibit-highlight))
2722     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2723            (article (gnus-summary-article-number))
2724            (score (gnus-summary-article-score article)))
2725       (unless dont-update
2726         (if (and gnus-summary-mark-below
2727                  (< (gnus-summary-article-score)
2728                     gnus-summary-mark-below))
2729             ;; This article has a low score, so we mark it as read.
2730             (when (memq article gnus-newsgroup-unreads)
2731               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2732           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2733             ;; This article was previously marked as read on account
2734             ;; of a low score, but now it has risen, so we mark it as
2735             ;; unread.
2736             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2737         (gnus-summary-update-mark
2738          (if (or (null gnus-summary-default-score)
2739                  (<= (abs (- score gnus-summary-default-score))
2740                      gnus-summary-zcore-fuzz))
2741              ?  ;Whitespace
2742            (if (< score gnus-summary-default-score)
2743                gnus-score-below-mark gnus-score-over-mark))
2744          'score))
2745       ;; Do visual highlighting.
2746       (when (gnus-visual-p 'summary-highlight 'highlight)
2747         (gnus-run-hooks 'gnus-summary-update-hook)))))
2748
2749 (defvar gnus-tmp-new-adopts nil)
2750
2751 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2752   "Return the number of articles in THREAD.
2753 This may be 0 in some cases -- if none of the articles in
2754 the thread are to be displayed."
2755   (let* ((number
2756           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2757           (cond
2758            ((not (listp thread))
2759             1)
2760            ((and (consp thread) (cdr thread))
2761             (apply
2762              '+ 1 (mapcar
2763                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2764            ((null thread)
2765             1)
2766            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2767             1)
2768            (t 0))))
2769     (when (and level (zerop level) gnus-tmp-new-adopts)
2770       (incf number
2771             (apply '+ (mapcar
2772                        'gnus-summary-number-of-articles-in-thread
2773                        gnus-tmp-new-adopts))))
2774     (if char
2775         (if (> number 1) gnus-not-empty-thread-mark
2776           gnus-empty-thread-mark)
2777       number)))
2778
2779 (defun gnus-summary-set-local-parameters (group)
2780   "Go through the local params of GROUP and set all variable specs in that list."
2781   (let ((params (gnus-group-find-parameter group))
2782         elem)
2783     (while params
2784       (setq elem (car params)
2785             params (cdr params))
2786       (and (consp elem)                 ; Has to be a cons.
2787            (consp (cdr elem))           ; The cdr has to be a list.
2788            (symbolp (car elem))         ; Has to be a symbol in there.
2789            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2790            (ignore-errors               ; So we set it.
2791              (make-local-variable (car elem))
2792              (set (car elem) (eval (nth 1 elem))))))))
2793
2794 (defun gnus-summary-read-group (group &optional show-all no-article
2795                                       kill-buffer no-display backward
2796                                       select-articles)
2797   "Start reading news in newsgroup GROUP.
2798 If SHOW-ALL is non-nil, already read articles are also listed.
2799 If NO-ARTICLE is non-nil, no article is selected initially.
2800 If NO-DISPLAY, don't generate a summary buffer."
2801   (let (result)
2802     (while (and group
2803                 (null (setq result
2804                             (let ((gnus-auto-select-next nil))
2805                               (or (gnus-summary-read-group-1
2806                                    group show-all no-article
2807                                    kill-buffer no-display
2808                                    select-articles)
2809                                   (setq show-all nil
2810                                         select-articles nil)))))
2811                 (eq gnus-auto-select-next 'quietly))
2812       (set-buffer gnus-group-buffer)
2813       ;; The entry function called above goes to the next
2814       ;; group automatically, so we go two groups back
2815       ;; if we are searching for the previous group.
2816       (when backward
2817         (gnus-group-prev-unread-group 2))
2818       (if (not (equal group (gnus-group-group-name)))
2819           (setq group (gnus-group-group-name))
2820         (setq group nil)))
2821     result))
2822
2823 (defun gnus-summary-read-group-1 (group show-all no-article
2824                                         kill-buffer no-display
2825                                         &optional select-articles)
2826   ;; Killed foreign groups can't be entered.
2827   (when (and (not (gnus-group-native-p group))
2828              (not (gnus-gethash group gnus-newsrc-hashtb)))
2829     (error "Dead non-native groups can't be entered"))
2830   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2831   (let* ((new-group (gnus-summary-setup-buffer group))
2832          (quit-config (gnus-group-quit-config group))
2833          (did-select (and new-group (gnus-select-newsgroup
2834                                      group show-all select-articles))))
2835     (cond
2836      ;; This summary buffer exists already, so we just select it.
2837      ((not new-group)
2838       (gnus-set-global-variables)
2839       (when kill-buffer
2840         (gnus-kill-or-deaden-summary kill-buffer))
2841       (gnus-configure-windows 'summary 'force)
2842       (gnus-set-mode-line 'summary)
2843       (gnus-summary-position-point)
2844       (message "")
2845       t)
2846      ;; We couldn't select this group.
2847      ((null did-select)
2848       (when (and (eq major-mode 'gnus-summary-mode)
2849                  (not (equal (current-buffer) kill-buffer)))
2850         (kill-buffer (current-buffer))
2851         (if (not quit-config)
2852             (progn
2853               ;; Update the info -- marks might need to be removed,
2854               ;; for instance.
2855               (gnus-summary-update-info)
2856               (set-buffer gnus-group-buffer)
2857               (gnus-group-jump-to-group group)
2858               (gnus-group-next-unread-group 1))
2859           (gnus-handle-ephemeral-exit quit-config)))
2860       (gnus-message 3 "Can't select group")
2861       nil)
2862      ;; The user did a `C-g' while prompting for number of articles,
2863      ;; so we exit this group.
2864      ((eq did-select 'quit)
2865       (and (eq major-mode 'gnus-summary-mode)
2866            (not (equal (current-buffer) kill-buffer))
2867            (kill-buffer (current-buffer)))
2868       (when kill-buffer
2869         (gnus-kill-or-deaden-summary kill-buffer))
2870       (if (not quit-config)
2871           (progn
2872             (set-buffer gnus-group-buffer)
2873             (gnus-group-jump-to-group group)
2874             (gnus-group-next-unread-group 1)
2875             (gnus-configure-windows 'group 'force))
2876         (gnus-handle-ephemeral-exit quit-config))
2877       ;; Finally signal the quit.
2878       (signal 'quit nil))
2879      ;; The group was successfully selected.
2880      (t
2881       (gnus-set-global-variables)
2882       ;; Save the active value in effect when the group was entered.
2883       (setq gnus-newsgroup-active
2884             (gnus-copy-sequence
2885              (gnus-active gnus-newsgroup-name)))
2886       ;; You can change the summary buffer in some way with this hook.
2887       (gnus-run-hooks 'gnus-select-group-hook)
2888       ;; Set any local variables in the group parameters.
2889       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2890       (gnus-update-format-specifications
2891        nil 'summary 'summary-mode 'summary-dummy)
2892       (gnus-update-summary-mark-positions)
2893       ;; Do score processing.
2894       (when gnus-use-scoring
2895         (gnus-possibly-score-headers))
2896       ;; Check whether to fill in the gaps in the threads.
2897       (when gnus-build-sparse-threads
2898         (gnus-build-sparse-threads))
2899       ;; Find the initial limit.
2900       (if gnus-show-threads
2901           (if show-all
2902               (let ((gnus-newsgroup-dormant nil))
2903                 (gnus-summary-initial-limit show-all))
2904             (gnus-summary-initial-limit show-all))
2905         ;; When untreaded, all articles are always shown.
2906         (setq gnus-newsgroup-limit
2907               (mapcar
2908                (lambda (header) (mail-header-number header))
2909                gnus-newsgroup-headers)))
2910       ;; Generate the summary buffer.
2911       (unless no-display
2912         (gnus-summary-prepare))
2913       (when gnus-use-trees
2914         (gnus-tree-open group)
2915         (setq gnus-summary-highlight-line-function
2916               'gnus-tree-highlight-article))
2917       ;; If the summary buffer is empty, but there are some low-scored
2918       ;; articles or some excluded dormants, we include these in the
2919       ;; buffer.
2920       (when (and (zerop (buffer-size))
2921                  (not no-display))
2922         (cond (gnus-newsgroup-dormant
2923                (gnus-summary-limit-include-dormant))
2924               ((and gnus-newsgroup-scored show-all)
2925                (gnus-summary-limit-include-expunged t))))
2926       ;; Function `gnus-apply-kill-file' must be called in this hook.
2927       (gnus-run-hooks 'gnus-apply-kill-hook)
2928       (if (and (zerop (buffer-size))
2929                (not no-display))
2930           (progn
2931             ;; This newsgroup is empty.
2932             (gnus-summary-catchup-and-exit nil t)
2933             (gnus-message 6 "No unread news")
2934             (when kill-buffer
2935               (gnus-kill-or-deaden-summary kill-buffer))
2936             ;; Return nil from this function.
2937             nil)
2938         ;; Hide conversation thread subtrees.  We cannot do this in
2939         ;; gnus-summary-prepare-hook since kill processing may not
2940         ;; work with hidden articles.
2941         (and gnus-show-threads
2942              gnus-thread-hide-subtree
2943              (gnus-summary-hide-all-threads))
2944         (when kill-buffer
2945           (gnus-kill-or-deaden-summary kill-buffer))
2946         ;; Show first unread article if requested.
2947         (if (and (not no-article)
2948                  (not no-display)
2949                  gnus-newsgroup-unreads
2950                  gnus-auto-select-first)
2951             (progn
2952               (gnus-configure-windows 'summary)
2953               (cond
2954                ((eq gnus-auto-select-first 'best)
2955                 (gnus-summary-best-unread-article))
2956                ((eq gnus-auto-select-first t)
2957                 (gnus-summary-first-unread-article))
2958                ((gnus-functionp gnus-auto-select-first)
2959                 (funcall gnus-auto-select-first))))
2960           ;; Don't select any articles, just move point to the first
2961           ;; article in the group.
2962           (goto-char (point-min))
2963           (gnus-summary-position-point)
2964           (gnus-configure-windows 'summary 'force)
2965           (gnus-set-mode-line 'summary))
2966         (when (get-buffer-window gnus-group-buffer t)
2967           ;; Gotta use windows, because recenter does weird stuff if
2968           ;; the current buffer ain't the displayed window.
2969           (let ((owin (selected-window)))
2970             (select-window (get-buffer-window gnus-group-buffer t))
2971             (when (gnus-group-goto-group group)
2972               (recenter))
2973             (select-window owin)))
2974         ;; Mark this buffer as "prepared".
2975         (setq gnus-newsgroup-prepared t)
2976         (gnus-run-hooks 'gnus-summary-prepared-hook)
2977         t)))))
2978
2979 (defun gnus-summary-prepare ()
2980   "Generate the summary buffer."
2981   (interactive)
2982   (let ((buffer-read-only nil))
2983     (erase-buffer)
2984     (setq gnus-newsgroup-data nil
2985           gnus-newsgroup-data-reverse nil)
2986     (gnus-run-hooks 'gnus-summary-generate-hook)
2987     ;; Generate the buffer, either with threads or without.
2988     (when gnus-newsgroup-headers
2989       (gnus-summary-prepare-threads
2990        (if gnus-show-threads
2991            (gnus-sort-gathered-threads
2992             (funcall gnus-summary-thread-gathering-function
2993                      (gnus-sort-threads
2994                       (gnus-cut-threads (gnus-make-threads)))))
2995          ;; Unthreaded display.
2996          (gnus-sort-articles gnus-newsgroup-headers))))
2997     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2998     ;; Call hooks for modifying summary buffer.
2999     (goto-char (point-min))
3000     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3001
3002 (defsubst gnus-general-simplify-subject (subject)
3003   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3004   (setq subject
3005         (cond
3006          ;; Truncate the subject.
3007          (gnus-simplify-subject-functions
3008           (gnus-map-function gnus-simplify-subject-functions subject))
3009          ((numberp gnus-summary-gather-subject-limit)
3010           (setq subject (gnus-simplify-subject-re subject))
3011           (if (> (length subject) gnus-summary-gather-subject-limit)
3012               (substring subject 0 gnus-summary-gather-subject-limit)
3013             subject))
3014          ;; Fuzzily simplify it.
3015          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3016           (gnus-simplify-subject-fuzzy subject))
3017          ;; Just remove the leading "Re:".
3018          (t
3019           (gnus-simplify-subject-re subject))))
3020
3021   (if (and gnus-summary-gather-exclude-subject
3022            (string-match gnus-summary-gather-exclude-subject subject))
3023       nil                               ; This article shouldn't be gathered
3024     subject))
3025
3026 (defun gnus-summary-simplify-subject-query ()
3027   "Query where the respool algorithm would put this article."
3028   (interactive)
3029   (gnus-summary-select-article)
3030   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3031
3032 (defun gnus-gather-threads-by-subject (threads)
3033   "Gather threads by looking at Subject headers."
3034   (if (not gnus-summary-make-false-root)
3035       threads
3036     (let ((hashtb (gnus-make-hashtable 1024))
3037           (prev threads)
3038           (result threads)
3039           subject hthread whole-subject)
3040       (while threads
3041         (setq subject (gnus-general-simplify-subject
3042                        (setq whole-subject (mail-header-subject
3043                                             (caar threads)))))
3044         (when subject
3045           (if (setq hthread (gnus-gethash subject hashtb))
3046               (progn
3047                 ;; We enter a dummy root into the thread, if we
3048                 ;; haven't done that already.
3049                 (unless (stringp (caar hthread))
3050                   (setcar hthread (list whole-subject (car hthread))))
3051                 ;; We add this new gathered thread to this gathered
3052                 ;; thread.
3053                 (setcdr (car hthread)
3054                         (nconc (cdar hthread) (list (car threads))))
3055                 ;; Remove it from the list of threads.
3056                 (setcdr prev (cdr threads))
3057                 (setq threads prev))
3058             ;; Enter this thread into the hash table.
3059             (gnus-sethash subject threads hashtb)))
3060         (setq prev threads)
3061         (setq threads (cdr threads)))
3062       result)))
3063
3064 (defun gnus-gather-threads-by-references (threads)
3065   "Gather threads by looking at References headers."
3066   (let ((idhashtb (gnus-make-hashtable 1024))
3067         (thhashtb (gnus-make-hashtable 1024))
3068         (prev threads)
3069         (result threads)
3070         ids references id gthread gid entered ref)
3071     (while threads
3072       (when (setq references (mail-header-references (caar threads)))
3073         (setq id (mail-header-id (caar threads))
3074               ids (gnus-split-references references)
3075               entered nil)
3076         (while (setq ref (pop ids))
3077           (setq ids (delete ref ids))
3078           (if (not (setq gid (gnus-gethash ref idhashtb)))
3079               (progn
3080                 (gnus-sethash ref id idhashtb)
3081                 (gnus-sethash id threads thhashtb))
3082             (setq gthread (gnus-gethash gid thhashtb))
3083             (unless entered
3084               ;; We enter a dummy root into the thread, if we
3085               ;; haven't done that already.
3086               (unless (stringp (caar gthread))
3087                 (setcar gthread (list (mail-header-subject (caar gthread))
3088                                       (car gthread))))
3089               ;; We add this new gathered thread to this gathered
3090               ;; thread.
3091               (setcdr (car gthread)
3092                       (nconc (cdar gthread) (list (car threads)))))
3093             ;; Add it into the thread hash table.
3094             (gnus-sethash id gthread thhashtb)
3095             (setq entered t)
3096             ;; Remove it from the list of threads.
3097             (setcdr prev (cdr threads))
3098             (setq threads prev))))
3099       (setq prev threads)
3100       (setq threads (cdr threads)))
3101     result))
3102
3103 (defun gnus-sort-gathered-threads (threads)
3104   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3105   (let ((result threads))
3106     (while threads
3107       (when (stringp (caar threads))
3108         (setcdr (car threads)
3109                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3110       (setq threads (cdr threads)))
3111     result))
3112
3113 (defun gnus-thread-loop-p (root thread)
3114   "Say whether ROOT is in THREAD."
3115   (let ((stack (list thread))
3116         (infloop 0)
3117         th)
3118     (while (setq thread (pop stack))
3119       (setq th (cdr thread))
3120       (while (and th
3121                   (not (eq (caar th) root)))
3122         (pop th))
3123       (if th
3124           ;; We have found a loop.
3125           (let (ref-dep)
3126             (setcdr thread (delq (car th) (cdr thread)))
3127             (if (boundp (setq ref-dep (intern "none"
3128                                               gnus-newsgroup-dependencies)))
3129                 (setcdr (symbol-value ref-dep)
3130                         (nconc (cdr (symbol-value ref-dep))
3131                                (list (car th))))
3132               (set ref-dep (list nil (car th))))
3133             (setq infloop 1
3134                   stack nil))
3135         ;; Push all the subthreads onto the stack.
3136         (push (cdr thread) stack)))
3137     infloop))
3138
3139 (defun gnus-make-threads ()
3140   "Go through the dependency hashtb and find the roots.  Return all threads."
3141   (let (threads)
3142     (while (catch 'infloop
3143              (mapatoms
3144               (lambda (refs)
3145                 ;; Deal with self-referencing References loops.
3146                 (when (and (car (symbol-value refs))
3147                            (not (zerop
3148                                  (apply
3149                                   '+
3150                                   (mapcar
3151                                    (lambda (thread)
3152                                      (gnus-thread-loop-p
3153                                       (car (symbol-value refs)) thread))
3154                                    (cdr (symbol-value refs)))))))
3155                   (setq threads nil)
3156                   (throw 'infloop t))
3157                 (unless (car (symbol-value refs))
3158                   ;; These threads do not refer back to any other articles,
3159                   ;; so they're roots.
3160                   (setq threads (append (cdr (symbol-value refs)) threads))))
3161               gnus-newsgroup-dependencies)))
3162     threads))
3163
3164 ;; Build the thread tree.
3165 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3166   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3167
3168 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3169 if it was already present.
3170
3171 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3172 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3173 Message-IDs will be renamed be renamed to a unique Message-ID before
3174 being entered.
3175
3176 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3177   (let* ((id (mail-header-id header))
3178          (id-dep (and id (intern id dependencies)))
3179          ref ref-dep ref-header)
3180     ;; Enter this `header' in the `dependencies' table.
3181     (cond
3182      ((not id-dep)
3183       (setq header nil))
3184      ;; The first two cases do the normal part: enter a new `header'
3185      ;; in the `dependencies' table.
3186      ((not (boundp id-dep))
3187       (set id-dep (list header)))
3188      ((null (car (symbol-value id-dep)))
3189       (setcar (symbol-value id-dep) header))
3190
3191      ;; From here the `header' was already present in the
3192      ;; `dependencies' table.
3193      (force-new
3194       ;; Overrides an existing entry;
3195       ;; just set the header part of the entry.
3196       (setcar (symbol-value id-dep) header))
3197
3198      ;; Renames the existing `header' to a unique Message-ID.
3199      ((not gnus-summary-ignore-duplicates)
3200       ;; An article with this Message-ID has already been seen.
3201       ;; We rename the Message-ID.
3202       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3203            (list header))
3204       (mail-header-set-id header id))
3205
3206      ;; The last case ignores an existing entry, except it adds any
3207      ;; additional Xrefs (in case the two articles came from different
3208      ;; servers.
3209      ;; Also sets `header' to `nil' meaning that the `dependencies'
3210      ;; table was *not* modified.
3211      (t
3212       (mail-header-set-xref
3213        (car (symbol-value id-dep))
3214        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3215                    "")
3216                (or (mail-header-xref header) "")))
3217       (setq header nil)))
3218
3219     (when header
3220       ;; First check if that we are not creating a References loop.
3221       (setq ref (gnus-parent-id (mail-header-references header)))
3222       (while (and ref
3223                   (setq ref-dep (intern-soft ref dependencies))
3224                   (boundp ref-dep)
3225                   (setq ref-header (car (symbol-value ref-dep))))
3226         (if (string= id ref)
3227             ;; Yuk!  This is a reference loop.  Make the article be a
3228             ;; root article.
3229             (progn
3230               (mail-header-set-references (car (symbol-value id-dep)) "none")
3231               (setq ref nil))
3232           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3233       (setq ref (gnus-parent-id (mail-header-references header)))
3234       (setq ref-dep (intern (or ref "none") dependencies))
3235       (if (boundp ref-dep)
3236           (setcdr (symbol-value ref-dep)
3237                   (nconc (cdr (symbol-value ref-dep))
3238                          (list (symbol-value id-dep))))
3239         (set ref-dep (list nil (symbol-value id-dep)))))
3240     header))
3241
3242 (defun gnus-build-sparse-threads ()
3243   (let ((headers gnus-newsgroup-headers)
3244         (mail-parse-charset gnus-newsgroup-charset)
3245         (gnus-summary-ignore-duplicates t)
3246         header references generation relations
3247         subject child end new-child date)
3248     ;; First we create an alist of generations/relations, where
3249     ;; generations is how much we trust the relation, and the relation
3250     ;; is parent/child.
3251     (gnus-message 7 "Making sparse threads...")
3252     (save-excursion
3253       (nnheader-set-temp-buffer " *gnus sparse threads*")
3254       (while (setq header (pop headers))
3255         (when (and (setq references (mail-header-references header))
3256                    (not (string= references "")))
3257           (insert references)
3258           (setq child (mail-header-id header)
3259                 subject (mail-header-subject header)
3260                 date (mail-header-date header)
3261                 generation 0)
3262           (while (search-backward ">" nil t)
3263             (setq end (1+ (point)))
3264             (when (search-backward "<" nil t)
3265               (setq new-child (buffer-substring (point) end))
3266               (push (list (incf generation)
3267                           child (setq child new-child)
3268                           subject date)
3269                     relations)))
3270           (when child
3271             (push (list (1+ generation) child nil subject) relations))
3272           (erase-buffer)))
3273       (kill-buffer (current-buffer)))
3274     ;; Sort over trustworthiness.
3275     (mapcar
3276      (lambda (relation)
3277        (when (gnus-dependencies-add-header
3278               (make-full-mail-header
3279                gnus-reffed-article-number
3280                (nth 3 relation) "" (or (nth 4 relation) "")
3281                (nth 1 relation)
3282                (or (nth 2 relation) "") 0 0 "")
3283               gnus-newsgroup-dependencies nil)
3284          (push gnus-reffed-article-number gnus-newsgroup-limit)
3285          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3286          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3287                gnus-newsgroup-reads)
3288          (decf gnus-reffed-article-number)))
3289      (sort relations 'car-less-than-car))
3290     (gnus-message 7 "Making sparse threads...done")))
3291
3292 (defun gnus-build-old-threads ()
3293   ;; Look at all the articles that refer back to old articles, and
3294   ;; fetch the headers for the articles that aren't there.  This will
3295   ;; build complete threads - if the roots haven't been expired by the
3296   ;; server, that is.
3297   (let ((mail-parse-charset gnus-newsgroup-charset)
3298         id heads)
3299     (mapatoms
3300      (lambda (refs)
3301        (when (not (car (symbol-value refs)))
3302          (setq heads (cdr (symbol-value refs)))
3303          (while heads
3304            (if (memq (mail-header-number (caar heads))
3305                      gnus-newsgroup-dormant)
3306                (setq heads (cdr heads))
3307              (setq id (symbol-name refs))
3308              (while (and (setq id (gnus-build-get-header id))
3309                          (not (car (gnus-id-to-thread id)))))
3310              (setq heads nil)))))
3311      gnus-newsgroup-dependencies)))
3312
3313 ;; This function has to be called with point after the article number
3314 ;; on the beginning of the line.
3315 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3316   (let ((eol (gnus-point-at-eol))
3317         (buffer (current-buffer))
3318         header)
3319
3320     ;; overview: [num subject from date id refs chars lines misc]
3321     (unwind-protect
3322         (progn
3323           (narrow-to-region (point) eol)
3324           (unless (eobp)
3325             (forward-char))
3326
3327           (setq header
3328                 (make-full-mail-header
3329                  number                 ; number
3330                  (funcall gnus-decode-encoded-word-function
3331                           (nnheader-nov-field)) ; subject
3332                  (funcall gnus-decode-encoded-word-function
3333                           (nnheader-nov-field)) ; from
3334                  (nnheader-nov-field)   ; date
3335                  (nnheader-nov-read-message-id) ; id
3336                  (nnheader-nov-field)   ; refs
3337                  (nnheader-nov-read-integer) ; chars
3338                  (nnheader-nov-read-integer) ; lines
3339                  (unless (eobp)
3340                    (if (looking-at "Xref: ")
3341                        (goto-char (match-end 0)))
3342                    (nnheader-nov-field)) ; Xref
3343                  (nnheader-nov-parse-extra)))) ; extra
3344
3345       (widen))
3346
3347     (when gnus-alter-header-function
3348       (funcall gnus-alter-header-function header))
3349     (gnus-dependencies-add-header header dependencies force-new)))
3350
3351 (defun gnus-build-get-header (id)
3352   "Look through the buffer of NOV lines and find the header to ID.
3353 Enter this line into the dependencies hash table, and return
3354 the id of the parent article (if any)."
3355   (let ((deps gnus-newsgroup-dependencies)
3356         found header)
3357     (prog1
3358         (save-excursion
3359           (set-buffer nntp-server-buffer)
3360           (let ((case-fold-search nil))
3361             (goto-char (point-min))
3362             (while (and (not found)
3363                         (search-forward id nil t))
3364               (beginning-of-line)
3365               (setq found (looking-at
3366                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3367                                    (regexp-quote id))))
3368               (or found (beginning-of-line 2)))
3369             (when found
3370               (beginning-of-line)
3371               (and
3372                (setq header (gnus-nov-parse-line
3373                              (read (current-buffer)) deps))
3374                (gnus-parent-id (mail-header-references header))))))
3375       (when header
3376         (let ((number (mail-header-number header)))
3377           (push number gnus-newsgroup-limit)
3378           (push header gnus-newsgroup-headers)
3379           (if (memq number gnus-newsgroup-unselected)
3380               (progn
3381                 (push number gnus-newsgroup-unreads)
3382                 (setq gnus-newsgroup-unselected
3383                       (delq number gnus-newsgroup-unselected)))
3384             (push number gnus-newsgroup-ancient)))))))
3385
3386 (defun gnus-build-all-threads ()
3387   "Read all the headers."
3388   (let ((gnus-summary-ignore-duplicates t)
3389         (mail-parse-charset gnus-newsgroup-charset)
3390         (dependencies gnus-newsgroup-dependencies)
3391         header article)
3392     (save-excursion
3393       (set-buffer nntp-server-buffer)
3394       (let ((case-fold-search nil))
3395         (goto-char (point-min))
3396         (while (not (eobp))
3397           (ignore-errors
3398             (setq article (read (current-buffer))
3399                   header (gnus-nov-parse-line article dependencies)))
3400           (when header
3401             (save-excursion
3402               (set-buffer gnus-summary-buffer)
3403               (push header gnus-newsgroup-headers)
3404               (if (memq (setq article (mail-header-number header))
3405                         gnus-newsgroup-unselected)
3406                   (progn
3407                     (push article gnus-newsgroup-unreads)
3408                     (setq gnus-newsgroup-unselected
3409                           (delq article gnus-newsgroup-unselected)))
3410                 (push article gnus-newsgroup-ancient)))
3411             (forward-line 1)))))))
3412
3413 (defun gnus-summary-update-article-line (article header)
3414   "Update the line for ARTICLE using HEADERS."
3415   (let* ((id (mail-header-id header))
3416          (thread (gnus-id-to-thread id)))
3417     (unless thread
3418       (error "Article in no thread"))
3419     ;; Update the thread.
3420     (setcar thread header)
3421     (gnus-summary-goto-subject article)
3422     (let* ((datal (gnus-data-find-list article))
3423            (data (car datal))
3424            (length (when (cdr datal)
3425                      (- (gnus-data-pos data)
3426                         (gnus-data-pos (cadr datal)))))
3427            (buffer-read-only nil)
3428            (level (gnus-summary-thread-level)))
3429       (gnus-delete-line)
3430       (gnus-summary-insert-line
3431        header level nil (gnus-article-mark article)
3432        (memq article gnus-newsgroup-replied)
3433        (memq article gnus-newsgroup-expirable)
3434        ;; Only insert the Subject string when it's different
3435        ;; from the previous Subject string.
3436        (if (and
3437             gnus-show-threads
3438             (gnus-subject-equal
3439              (condition-case ()
3440                  (mail-header-subject
3441                   (gnus-data-header
3442                    (cadr
3443                     (gnus-data-find-list
3444                      article
3445                      (gnus-data-list t)))))
3446                ;; Error on the side of excessive subjects.
3447                (error ""))
3448              (mail-header-subject header)))
3449            ""
3450          (mail-header-subject header))
3451        nil (cdr (assq article gnus-newsgroup-scored))
3452        (memq article gnus-newsgroup-processable))
3453       (when length
3454         (gnus-data-update-list
3455          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3456
3457 (defun gnus-summary-update-article (article &optional iheader)
3458   "Update ARTICLE in the summary buffer."
3459   (set-buffer gnus-summary-buffer)
3460   (let* ((header (gnus-summary-article-header article))
3461          (id (mail-header-id header))
3462          (data (gnus-data-find article))
3463          (thread (gnus-id-to-thread id))
3464          (references (mail-header-references header))
3465          (parent
3466           (gnus-id-to-thread
3467            (or (gnus-parent-id
3468                 (when (and references
3469                            (not (equal "" references)))
3470                   references))
3471                "none")))
3472          (buffer-read-only nil)
3473          (old (car thread)))
3474     (when thread
3475       (unless iheader
3476         (setcar thread nil)
3477         (when parent
3478           (delq thread parent)))
3479       (if (gnus-summary-insert-subject id header)
3480           ;; Set the (possibly) new article number in the data structure.
3481           (gnus-data-set-number data (gnus-id-to-article id))
3482         (setcar thread old)
3483         nil))))
3484
3485 (defun gnus-rebuild-thread (id &optional line)
3486   "Rebuild the thread containing ID.
3487 If LINE, insert the rebuilt thread starting on line LINE."
3488   (let ((buffer-read-only nil)
3489         old-pos current thread data)
3490     (if (not gnus-show-threads)
3491         (setq thread (list (car (gnus-id-to-thread id))))
3492       ;; Get the thread this article is part of.
3493       (setq thread (gnus-remove-thread id)))
3494     (setq old-pos (gnus-point-at-bol))
3495     (setq current (save-excursion
3496                     (and (zerop (forward-line -1))
3497                          (gnus-summary-article-number))))
3498     ;; If this is a gathered thread, we have to go some re-gathering.
3499     (when (stringp (car thread))
3500       (let ((subject (car thread))
3501             roots thr)
3502         (setq thread (cdr thread))
3503         (while thread
3504           (unless (memq (setq thr (gnus-id-to-thread
3505                                    (gnus-root-id
3506                                     (mail-header-id (caar thread)))))
3507                         roots)
3508             (push thr roots))
3509           (setq thread (cdr thread)))
3510         ;; We now have all (unique) roots.
3511         (if (= (length roots) 1)
3512             ;; All the loose roots are now one solid root.
3513             (setq thread (car roots))
3514           (setq thread (cons subject (gnus-sort-threads roots))))))
3515     (let (threads)
3516       ;; We then insert this thread into the summary buffer.
3517       (when line
3518         (goto-char (point-min))
3519         (forward-line (1- line)))
3520       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3521         (if gnus-show-threads
3522             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3523           (gnus-summary-prepare-unthreaded thread))
3524         (setq data (nreverse gnus-newsgroup-data))
3525         (setq threads gnus-newsgroup-threads))
3526       ;; We splice the new data into the data structure.
3527       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3528       ;;!!! then we want to insert at the beginning of the buffer.
3529       ;;!!! That happens to be true with Gnus now, but that may
3530       ;;!!! change in the future.  Perhaps.
3531       (gnus-data-enter-list
3532        (if line nil current) data (- (point) old-pos))
3533       (setq gnus-newsgroup-threads
3534             (nconc threads gnus-newsgroup-threads))
3535       (gnus-data-compute-positions))))
3536
3537 (defun gnus-number-to-header (number)
3538   "Return the header for article NUMBER."
3539   (let ((headers gnus-newsgroup-headers))
3540     (while (and headers
3541                 (not (= number (mail-header-number (car headers)))))
3542       (pop headers))
3543     (when headers
3544       (car headers))))
3545
3546 (defun gnus-parent-headers (in-headers &optional generation)
3547   "Return the headers of the GENERATIONeth parent of HEADERS."
3548   (unless generation
3549     (setq generation 1))
3550   (let ((parent t)
3551         (headers in-headers)
3552         references)
3553     (while (and parent
3554                 (not (zerop generation))
3555                 (setq references (mail-header-references headers)))
3556       (setq headers (if (and references
3557                              (setq parent (gnus-parent-id references)))
3558                         (car (gnus-id-to-thread parent))
3559                       nil))
3560       (decf generation))
3561     (and (not (eq headers in-headers))
3562          headers)))
3563
3564 (defun gnus-id-to-thread (id)
3565   "Return the (sub-)thread where ID appears."
3566   (gnus-gethash id gnus-newsgroup-dependencies))
3567
3568 (defun gnus-id-to-article (id)
3569   "Return the article number of ID."
3570   (let ((thread (gnus-id-to-thread id)))
3571     (when (and thread
3572                (car thread))
3573       (mail-header-number (car thread)))))
3574
3575 (defun gnus-id-to-header (id)
3576   "Return the article headers of ID."
3577   (car (gnus-id-to-thread id)))
3578
3579 (defun gnus-article-displayed-root-p (article)
3580   "Say whether ARTICLE is a root(ish) article."
3581   (let ((level (gnus-summary-thread-level article))
3582         (refs (mail-header-references  (gnus-summary-article-header article)))
3583         particle)
3584     (cond
3585      ((null level) nil)
3586      ((zerop level) t)
3587      ((null refs) t)
3588      ((null (gnus-parent-id refs)) t)
3589      ((and (= 1 level)
3590            (null (setq particle (gnus-id-to-article
3591                                  (gnus-parent-id refs))))
3592            (null (gnus-summary-thread-level particle)))))))
3593
3594 (defun gnus-root-id (id)
3595   "Return the id of the root of the thread where ID appears."
3596   (let (last-id prev)
3597     (while (and id (setq prev (car (gnus-id-to-thread id))))
3598       (setq last-id id
3599             id (gnus-parent-id (mail-header-references prev))))
3600     last-id))
3601
3602 (defun gnus-articles-in-thread (thread)
3603   "Return the list of articles in THREAD."
3604   (cons (mail-header-number (car thread))
3605         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3606
3607 (defun gnus-remove-thread (id &optional dont-remove)
3608   "Remove the thread that has ID in it."
3609   (let (headers thread last-id)
3610     ;; First go up in this thread until we find the root.
3611     (setq last-id (gnus-root-id id)
3612           headers (message-flatten-list (gnus-id-to-thread last-id)))
3613     ;; We have now found the real root of this thread.  It might have
3614     ;; been gathered into some loose thread, so we have to search
3615     ;; through the threads to find the thread we wanted.
3616     (let ((threads gnus-newsgroup-threads)
3617           sub)
3618       (while threads
3619         (setq sub (car threads))
3620         (if (stringp (car sub))
3621             ;; This is a gathered thread, so we look at the roots
3622             ;; below it to find whether this article is in this
3623             ;; gathered root.
3624             (progn
3625               (setq sub (cdr sub))
3626               (while sub
3627                 (when (member (caar sub) headers)
3628                   (setq thread (car threads)
3629                         threads nil
3630                         sub nil))
3631                 (setq sub (cdr sub))))
3632           ;; It's an ordinary thread, so we check it.
3633           (when (eq (car sub) (car headers))
3634             (setq thread sub
3635                   threads nil)))
3636         (setq threads (cdr threads)))
3637       ;; If this article is in no thread, then it's a root.
3638       (if thread
3639           (unless dont-remove
3640             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3641         (setq thread (gnus-id-to-thread last-id)))
3642       (when thread
3643         (prog1
3644             thread                      ; We return this thread.
3645           (unless dont-remove
3646             (if (stringp (car thread))
3647                 (progn
3648                   ;; If we use dummy roots, then we have to remove the
3649                   ;; dummy root as well.
3650                   (when (eq gnus-summary-make-false-root 'dummy)
3651                     ;; We go to the dummy root by going to
3652                     ;; the first sub-"thread", and then one line up.
3653                     (gnus-summary-goto-article
3654                      (mail-header-number (caadr thread)))
3655                     (forward-line -1)
3656                     (gnus-delete-line)
3657                     (gnus-data-compute-positions))
3658                   (setq thread (cdr thread))
3659                   (while thread
3660                     (gnus-remove-thread-1 (car thread))
3661                     (setq thread (cdr thread))))
3662               (gnus-remove-thread-1 thread))))))))
3663
3664 (defun gnus-remove-thread-1 (thread)
3665   "Remove the thread THREAD recursively."
3666   (let ((number (mail-header-number (pop thread)))
3667         d)
3668     (setq thread (reverse thread))
3669     (while thread
3670       (gnus-remove-thread-1 (pop thread)))
3671     (when (setq d (gnus-data-find number))
3672       (goto-char (gnus-data-pos d))
3673       (gnus-summary-show-thread)
3674       (gnus-data-remove
3675        number
3676        (- (gnus-point-at-bol)
3677           (prog1
3678               (1+ (gnus-point-at-eol))
3679             (gnus-delete-line)))))))
3680
3681 (defun gnus-sort-threads-1 (threads func)
3682   (sort (mapcar (lambda (thread)
3683                   (cons (car thread)
3684                         (and (cdr thread)
3685                              (gnus-sort-threads-1 (cdr thread) func))))
3686                 threads) func))
3687
3688 (defun gnus-sort-threads (threads)
3689   "Sort THREADS."
3690   (if (not gnus-thread-sort-functions)
3691       threads
3692     (gnus-message 8 "Sorting threads...")
3693     (prog1
3694         (gnus-sort-threads-1 
3695          threads 
3696          (gnus-make-sort-function gnus-thread-sort-functions))
3697       (gnus-message 8 "Sorting threads...done"))))
3698
3699 (defun gnus-sort-articles (articles)
3700   "Sort ARTICLES."
3701   (when gnus-article-sort-functions
3702     (gnus-message 7 "Sorting articles...")
3703     (prog1
3704         (setq gnus-newsgroup-headers
3705               (sort articles (gnus-make-sort-function
3706                               gnus-article-sort-functions)))
3707       (gnus-message 7 "Sorting articles...done"))))
3708
3709 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3710 (defmacro gnus-thread-header (thread)
3711   "Return header of first article in THREAD.
3712 Note that THREAD must never, ever be anything else than a variable -
3713 using some other form will lead to serious barfage."
3714   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3715   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3716   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3717         (vector thread) 2))
3718
3719 (defsubst gnus-article-sort-by-number (h1 h2)
3720   "Sort articles by article number."
3721   (< (mail-header-number h1)
3722      (mail-header-number h2)))
3723
3724 (defun gnus-thread-sort-by-number (h1 h2)
3725   "Sort threads by root article number."
3726   (gnus-article-sort-by-number
3727    (gnus-thread-header h1) (gnus-thread-header h2)))
3728
3729 (defsubst gnus-article-sort-by-lines (h1 h2)
3730   "Sort articles by article Lines header."
3731   (< (mail-header-lines h1)
3732      (mail-header-lines h2)))
3733
3734 (defun gnus-thread-sort-by-lines (h1 h2)
3735   "Sort threads by root article Lines header."
3736   (gnus-article-sort-by-lines
3737    (gnus-thread-header h1) (gnus-thread-header h2)))
3738
3739 (defsubst gnus-article-sort-by-chars (h1 h2)
3740   "Sort articles by octet length."
3741   (< (mail-header-chars h1)
3742      (mail-header-chars h2)))
3743
3744 (defun gnus-thread-sort-by-chars (h1 h2)
3745   "Sort threads by root article octet length."
3746   (gnus-article-sort-by-chars
3747    (gnus-thread-header h1) (gnus-thread-header h2)))
3748
3749 (defsubst gnus-article-sort-by-author (h1 h2)
3750   "Sort articles by root author."
3751   (string-lessp
3752    (let ((extract (funcall
3753                    gnus-extract-address-components
3754                    (mail-header-from h1))))
3755      (or (car extract) (cadr extract) ""))
3756    (let ((extract (funcall
3757                    gnus-extract-address-components
3758                    (mail-header-from h2))))
3759      (or (car extract) (cadr extract) ""))))
3760
3761 (defun gnus-thread-sort-by-author (h1 h2)
3762   "Sort threads by root author."
3763   (gnus-article-sort-by-author
3764    (gnus-thread-header h1)  (gnus-thread-header h2)))
3765
3766 (defsubst gnus-article-sort-by-subject (h1 h2)
3767   "Sort articles by root subject."
3768   (string-lessp
3769    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3770    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3771
3772 (defun gnus-thread-sort-by-subject (h1 h2)
3773   "Sort threads by root subject."
3774   (gnus-article-sort-by-subject
3775    (gnus-thread-header h1) (gnus-thread-header h2)))
3776
3777 (defsubst gnus-article-sort-by-date (h1 h2)
3778   "Sort articles by root article date."
3779   (time-less-p
3780    (gnus-date-get-time (mail-header-date h1))
3781    (gnus-date-get-time (mail-header-date h2))))
3782
3783 (defun gnus-thread-sort-by-date (h1 h2)
3784   "Sort threads by root article date."
3785   (gnus-article-sort-by-date
3786    (gnus-thread-header h1) (gnus-thread-header h2)))
3787
3788 (defsubst gnus-article-sort-by-score (h1 h2)
3789   "Sort articles by root article score.
3790 Unscored articles will be counted as having a score of zero."
3791   (> (or (cdr (assq (mail-header-number h1)
3792                     gnus-newsgroup-scored))
3793          gnus-summary-default-score 0)
3794      (or (cdr (assq (mail-header-number h2)
3795                     gnus-newsgroup-scored))
3796          gnus-summary-default-score 0)))
3797
3798 (defun gnus-thread-sort-by-score (h1 h2)
3799   "Sort threads by root article score."
3800   (gnus-article-sort-by-score
3801    (gnus-thread-header h1) (gnus-thread-header h2)))
3802
3803 (defun gnus-thread-sort-by-total-score (h1 h2)
3804   "Sort threads by the sum of all scores in the thread.
3805 Unscored articles will be counted as having a score of zero."
3806   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3807
3808 (defun gnus-thread-total-score (thread)
3809   ;; This function find the total score of THREAD.
3810   (cond ((null thread)
3811          0)
3812         ((consp thread)
3813          (if (stringp (car thread))
3814              (apply gnus-thread-score-function 0
3815                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3816            (gnus-thread-total-score-1 thread)))
3817         (t
3818          (gnus-thread-total-score-1 (list thread)))))
3819
3820 (defun gnus-thread-total-score-1 (root)
3821   ;; This function find the total score of the thread below ROOT.
3822   (setq root (car root))
3823   (apply gnus-thread-score-function
3824          (or (append
3825               (mapcar 'gnus-thread-total-score
3826                       (cdr (gnus-id-to-thread (mail-header-id root))))
3827               (when (> (mail-header-number root) 0)
3828                 (list (or (cdr (assq (mail-header-number root)
3829                                      gnus-newsgroup-scored))
3830                           gnus-summary-default-score 0))))
3831              (list gnus-summary-default-score)
3832              '(0))))
3833
3834 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3835 (defvar gnus-tmp-prev-subject nil)
3836 (defvar gnus-tmp-false-parent nil)
3837 (defvar gnus-tmp-root-expunged nil)
3838 (defvar gnus-tmp-dummy-line nil)
3839
3840 (defvar gnus-tmp-header)
3841 (defun gnus-extra-header (type &optional header)
3842   "Return the extra header of TYPE."
3843   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3844       ""))
3845
3846 (defun gnus-summary-prepare-threads (threads)
3847   "Prepare summary buffer from THREADS and indentation LEVEL.
3848 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3849 or a straight list of headers."
3850   (gnus-message 7 "Generating summary...")
3851
3852   (setq gnus-newsgroup-threads threads)
3853   (beginning-of-line)
3854
3855   (let ((gnus-tmp-level 0)
3856         (default-score (or gnus-summary-default-score 0))
3857         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3858         thread number subject stack state gnus-tmp-gathered beg-match
3859         new-roots gnus-tmp-new-adopts thread-end
3860         gnus-tmp-header gnus-tmp-unread
3861         gnus-tmp-replied gnus-tmp-subject-or-nil
3862         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3863         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3864         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3865
3866     (setq gnus-tmp-prev-subject nil)
3867
3868     (if (vectorp (car threads))
3869         ;; If this is a straight (sic) list of headers, then a
3870         ;; threaded summary display isn't required, so we just create
3871         ;; an unthreaded one.
3872         (gnus-summary-prepare-unthreaded threads)
3873
3874       ;; Do the threaded display.
3875
3876       (while (or threads stack gnus-tmp-new-adopts new-roots)
3877
3878         (if (and (= gnus-tmp-level 0)
3879                  (or (not stack)
3880                      (= (caar stack) 0))
3881                  (not gnus-tmp-false-parent)
3882                  (or gnus-tmp-new-adopts new-roots))
3883             (if gnus-tmp-new-adopts
3884                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3885                       thread (list (car gnus-tmp-new-adopts))
3886                       gnus-tmp-header (caar thread)
3887                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3888               (when new-roots
3889                 (setq thread (list (car new-roots))
3890                       gnus-tmp-header (caar thread)
3891                       new-roots (cdr new-roots))))
3892
3893           (if threads
3894               ;; If there are some threads, we do them before the
3895               ;; threads on the stack.
3896               (setq thread threads
3897                     gnus-tmp-header (caar thread))
3898             ;; There were no current threads, so we pop something off
3899             ;; the stack.
3900             (setq state (car stack)
3901                   gnus-tmp-level (car state)
3902                   thread (cdr state)
3903                   stack (cdr stack)
3904                   gnus-tmp-header (caar thread))))
3905
3906         (setq gnus-tmp-false-parent nil)
3907         (setq gnus-tmp-root-expunged nil)
3908         (setq thread-end nil)
3909
3910         (if (stringp gnus-tmp-header)
3911             ;; The header is a dummy root.
3912             (cond
3913              ((eq gnus-summary-make-false-root 'adopt)
3914               ;; We let the first article adopt the rest.
3915               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3916                                                (cddar thread)))
3917               (setq gnus-tmp-gathered
3918                     (nconc (mapcar
3919                             (lambda (h) (mail-header-number (car h)))
3920                             (cddar thread))
3921                            gnus-tmp-gathered))
3922               (setq thread (cons (list (caar thread)
3923                                        (cadar thread))
3924                                  (cdr thread)))
3925               (setq gnus-tmp-level -1
3926                     gnus-tmp-false-parent t))
3927              ((eq gnus-summary-make-false-root 'empty)
3928               ;; We print adopted articles with empty subject fields.
3929               (setq gnus-tmp-gathered
3930                     (nconc (mapcar
3931                             (lambda (h) (mail-header-number (car h)))
3932                             (cddar thread))
3933                            gnus-tmp-gathered))
3934               (setq gnus-tmp-level -1))
3935              ((eq gnus-summary-make-false-root 'dummy)
3936               ;; We remember that we probably want to output a dummy
3937               ;; root.
3938               (setq gnus-tmp-dummy-line gnus-tmp-header)
3939               (setq gnus-tmp-prev-subject gnus-tmp-header))
3940              (t
3941               ;; We do not make a root for the gathered
3942               ;; sub-threads at all.
3943               (setq gnus-tmp-level -1)))
3944
3945           (setq number (mail-header-number gnus-tmp-header)
3946                 subject (mail-header-subject gnus-tmp-header))
3947
3948           (cond
3949            ;; If the thread has changed subject, we might want to make
3950            ;; this subthread into a root.
3951            ((and (null gnus-thread-ignore-subject)
3952                  (not (zerop gnus-tmp-level))
3953                  gnus-tmp-prev-subject
3954                  (not (inline
3955                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3956             (setq new-roots (nconc new-roots (list (car thread)))
3957                   thread-end t
3958                   gnus-tmp-header nil))
3959            ;; If the article lies outside the current limit,
3960            ;; then we do not display it.
3961            ((not (memq number gnus-newsgroup-limit))
3962             (setq gnus-tmp-gathered
3963                   (nconc (mapcar
3964                           (lambda (h) (mail-header-number (car h)))
3965                           (cdar thread))
3966                          gnus-tmp-gathered))
3967             (setq gnus-tmp-new-adopts (if (cdar thread)
3968                                           (append gnus-tmp-new-adopts
3969                                                   (cdar thread))
3970                                         gnus-tmp-new-adopts)
3971                   thread-end t
3972                   gnus-tmp-header nil)
3973             (when (zerop gnus-tmp-level)
3974               (setq gnus-tmp-root-expunged t)))
3975            ;; Perhaps this article is to be marked as read?
3976            ((and gnus-summary-mark-below
3977                  (< (or (cdr (assq number gnus-newsgroup-scored))
3978                         default-score)
3979                     gnus-summary-mark-below)
3980                  ;; Don't touch sparse articles.
3981                  (not (gnus-summary-article-sparse-p number))
3982                  (not (gnus-summary-article-ancient-p number)))
3983             (setq gnus-newsgroup-unreads
3984                   (delq number gnus-newsgroup-unreads))
3985             (if gnus-newsgroup-auto-expire
3986                 (push number gnus-newsgroup-expirable)
3987               (push (cons number gnus-low-score-mark)
3988                     gnus-newsgroup-reads))))
3989
3990           (when gnus-tmp-header
3991             ;; We may have an old dummy line to output before this
3992             ;; article.
3993             (when (and gnus-tmp-dummy-line
3994                        (gnus-subject-equal
3995                         gnus-tmp-dummy-line
3996                         (mail-header-subject gnus-tmp-header)))
3997               (gnus-summary-insert-dummy-line
3998                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3999               (setq gnus-tmp-dummy-line nil))
4000
4001             ;; Compute the mark.
4002             (setq gnus-tmp-unread (gnus-article-mark number))
4003
4004             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4005                                   gnus-tmp-header gnus-tmp-level)
4006                   gnus-newsgroup-data)
4007
4008             ;; Actually insert the line.
4009             (setq
4010              gnus-tmp-subject-or-nil
4011              (cond
4012               ((and gnus-thread-ignore-subject
4013                     gnus-tmp-prev-subject
4014                     (not (inline (gnus-subject-equal
4015                                   gnus-tmp-prev-subject subject))))
4016                subject)
4017               ((zerop gnus-tmp-level)
4018                (if (and (eq gnus-summary-make-false-root 'empty)
4019                         (memq number gnus-tmp-gathered)
4020                         gnus-tmp-prev-subject
4021                         (inline (gnus-subject-equal
4022                                  gnus-tmp-prev-subject subject)))
4023                    gnus-summary-same-subject
4024                  subject))
4025               (t gnus-summary-same-subject)))
4026             (if (and (eq gnus-summary-make-false-root 'adopt)
4027                      (= gnus-tmp-level 1)
4028                      (memq number gnus-tmp-gathered))
4029                 (setq gnus-tmp-opening-bracket ?\<
4030                       gnus-tmp-closing-bracket ?\>)
4031               (setq gnus-tmp-opening-bracket ?\[
4032                     gnus-tmp-closing-bracket ?\]))
4033             (setq
4034              gnus-tmp-indentation
4035              (aref gnus-thread-indent-array gnus-tmp-level)
4036              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4037              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4038                                 gnus-summary-default-score 0)
4039              gnus-tmp-score-char
4040              (if (or (null gnus-summary-default-score)
4041                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4042                          gnus-summary-zcore-fuzz))
4043                  ?  ;Whitespace
4044                (if (< gnus-tmp-score gnus-summary-default-score)
4045                    gnus-score-below-mark gnus-score-over-mark))
4046              gnus-tmp-replied
4047              (cond ((memq number gnus-newsgroup-processable)
4048                     gnus-process-mark)
4049                    ((memq number gnus-newsgroup-cached)
4050                     gnus-cached-mark)
4051                    ((memq number gnus-newsgroup-replied)
4052                     gnus-replied-mark)
4053                    ((memq number gnus-newsgroup-saved)
4054                     gnus-saved-mark)
4055                    (t gnus-unread-mark))
4056              gnus-tmp-from (mail-header-from gnus-tmp-header)
4057              gnus-tmp-name
4058              (cond
4059               ((string-match "<[^>]+> *$" gnus-tmp-from)
4060                (setq beg-match (match-beginning 0))
4061                (or (and (string-match "^\".+\"" gnus-tmp-from)
4062                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4063                    (substring gnus-tmp-from 0 beg-match)))
4064               ((string-match "(.+)" gnus-tmp-from)
4065                (substring gnus-tmp-from
4066                           (1+ (match-beginning 0)) (1- (match-end 0))))
4067               (t gnus-tmp-from)))
4068             (when (string= gnus-tmp-name "")
4069               (setq gnus-tmp-name gnus-tmp-from))
4070             (unless (numberp gnus-tmp-lines)
4071               (setq gnus-tmp-lines 0))
4072             (gnus-put-text-property
4073              (point)
4074              (progn (eval gnus-summary-line-format-spec) (point))
4075              'gnus-number number)
4076             (when gnus-visual-p
4077               (forward-line -1)
4078               (gnus-run-hooks 'gnus-summary-update-hook)
4079               (forward-line 1))
4080
4081             (setq gnus-tmp-prev-subject subject)))
4082
4083         (when (nth 1 thread)
4084           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4085         (incf gnus-tmp-level)
4086         (setq threads (if thread-end nil (cdar thread)))
4087         (unless threads
4088           (setq gnus-tmp-level 0)))))
4089   (gnus-message 7 "Generating summary...done"))
4090
4091 (defun gnus-summary-prepare-unthreaded (headers)
4092   "Generate an unthreaded summary buffer based on HEADERS."
4093   (let (header number mark)
4094
4095     (beginning-of-line)
4096
4097     (while headers
4098       ;; We may have to root out some bad articles...
4099       (when (memq (setq number (mail-header-number
4100                                 (setq header (pop headers))))
4101                   gnus-newsgroup-limit)
4102         ;; Mark article as read when it has a low score.
4103         (when (and gnus-summary-mark-below
4104                    (< (or (cdr (assq number gnus-newsgroup-scored))
4105                           gnus-summary-default-score 0)
4106                       gnus-summary-mark-below)
4107                    (not (gnus-summary-article-ancient-p number)))
4108           (setq gnus-newsgroup-unreads
4109                 (delq number gnus-newsgroup-unreads))
4110           (if gnus-newsgroup-auto-expire
4111               (push number gnus-newsgroup-expirable)
4112             (push (cons number gnus-low-score-mark)
4113                   gnus-newsgroup-reads)))
4114
4115         (setq mark (gnus-article-mark number))
4116         (push (gnus-data-make number mark (1+ (point)) header 0)
4117               gnus-newsgroup-data)
4118         (gnus-summary-insert-line
4119          header 0 number
4120          mark (memq number gnus-newsgroup-replied)
4121          (memq number gnus-newsgroup-expirable)
4122          (mail-header-subject header) nil
4123          (cdr (assq number gnus-newsgroup-scored))
4124          (memq number gnus-newsgroup-processable))))))
4125
4126 (defun gnus-summary-remove-list-identifiers ()
4127   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4128   (let ((regexp (if (stringp gnus-list-identifiers)
4129                     gnus-list-identifiers
4130                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4131     (dolist (header gnus-newsgroup-headers)
4132       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4133                                   " *\\)\\)+\\(Re: +\\)?\\)")
4134                           (mail-header-subject header))
4135         (mail-header-set-subject
4136          header (concat (substring (mail-header-subject header)
4137                                    0 (match-beginning 1))
4138                         (or
4139                          (match-string 3 (mail-header-subject header))
4140                          (match-string 5 (mail-header-subject header)))
4141                         (substring (mail-header-subject header)
4142                                    (match-end 1))))))))
4143
4144 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4145   "Select newsgroup GROUP.
4146 If READ-ALL is non-nil, all articles in the group are selected.
4147 If SELECT-ARTICLES, only select those articles from GROUP."
4148   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4149          ;;!!! Dirty hack; should be removed.
4150          (gnus-summary-ignore-duplicates
4151           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4152               t
4153             gnus-summary-ignore-duplicates))
4154          (info (nth 2 entry))
4155          articles fetched-articles cached)
4156
4157     (unless (gnus-check-server
4158              (setq gnus-current-select-method
4159                    (gnus-find-method-for-group group)))
4160       (error "Couldn't open server"))
4161
4162     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4163         (gnus-activate-group group)     ; Or we can activate it...
4164         (progn                          ; Or we bug out.
4165           (when (equal major-mode 'gnus-summary-mode)
4166             (kill-buffer (current-buffer)))
4167           (error "Couldn't request group %s: %s"
4168                  group (gnus-status-message group))))
4169
4170     (unless (gnus-request-group group t)
4171       (when (equal major-mode 'gnus-summary-mode)
4172         (kill-buffer (current-buffer)))
4173       (error "Couldn't request group %s: %s"
4174              group (gnus-status-message group)))
4175
4176     (setq gnus-newsgroup-name group)
4177     (setq gnus-newsgroup-unselected nil)
4178     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4179     (gnus-summary-setup-default-charset)
4180
4181     ;; Adjust and set lists of article marks.
4182     (when info
4183       (gnus-adjust-marked-articles info))
4184
4185     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4186     (when (gnus-virtual-group-p group)
4187       (setq cached gnus-newsgroup-cached))
4188
4189     (setq gnus-newsgroup-unreads
4190           (gnus-set-difference
4191            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4192            gnus-newsgroup-dormant))
4193
4194     (setq gnus-newsgroup-processable nil)
4195
4196     (gnus-update-read-articles group gnus-newsgroup-unreads)
4197
4198     (if (setq articles select-articles)
4199         (setq gnus-newsgroup-unselected
4200               (gnus-sorted-intersection
4201                gnus-newsgroup-unreads
4202                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4203       (setq articles (gnus-articles-to-read group read-all)))
4204
4205     (cond
4206      ((null articles)
4207       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4208       'quit)
4209      ((eq articles 0) nil)
4210      (t
4211       ;; Init the dependencies hash table.
4212       (setq gnus-newsgroup-dependencies
4213             (gnus-make-hashtable (length articles)))
4214       (gnus-set-global-variables)
4215       ;; Retrieve the headers and read them in.
4216       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4217       (setq gnus-newsgroup-headers
4218             (if (eq 'nov
4219                     (setq gnus-headers-retrieved-by
4220                           (gnus-retrieve-headers
4221                            articles gnus-newsgroup-name
4222                            ;; We might want to fetch old headers, but
4223                            ;; not if there is only 1 article.
4224                            (and (or (and
4225                                      (not (eq gnus-fetch-old-headers 'some))
4226                                      (not (numberp gnus-fetch-old-headers)))
4227                                     (> (length articles) 1))
4228                                 gnus-fetch-old-headers))))
4229                 (gnus-get-newsgroup-headers-xover
4230                  articles nil nil gnus-newsgroup-name t)
4231               (gnus-get-newsgroup-headers)))
4232       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4233
4234       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4235       (when cached
4236         (setq gnus-newsgroup-cached cached))
4237
4238       ;; Suppress duplicates?
4239       (when gnus-suppress-duplicates
4240         (gnus-dup-suppress-articles))
4241
4242       ;; Set the initial limit.
4243       (setq gnus-newsgroup-limit (copy-sequence articles))
4244       ;; Remove canceled articles from the list of unread articles.
4245       (setq gnus-newsgroup-unreads
4246             (gnus-set-sorted-intersection
4247              gnus-newsgroup-unreads
4248              (setq fetched-articles
4249                    (mapcar (lambda (headers) (mail-header-number headers))
4250                            gnus-newsgroup-headers))))
4251       ;; Removed marked articles that do not exist.
4252       (gnus-update-missing-marks
4253        (gnus-sorted-complement fetched-articles articles))
4254       ;; We might want to build some more threads first.
4255       (when (and gnus-fetch-old-headers
4256                  (eq gnus-headers-retrieved-by 'nov))
4257         (if (eq gnus-fetch-old-headers 'invisible)
4258             (gnus-build-all-threads)
4259           (gnus-build-old-threads)))
4260       ;; Let the Gnus agent mark articles as read.
4261       (when gnus-agent
4262         (gnus-agent-get-undownloaded-list))
4263       ;; Remove list identifiers from subject
4264       (when gnus-list-identifiers
4265         (gnus-summary-remove-list-identifiers))
4266       ;; Check whether auto-expire is to be done in this group.
4267       (setq gnus-newsgroup-auto-expire
4268             (gnus-group-auto-expirable-p group))
4269       ;; Set up the article buffer now, if necessary.
4270       (unless gnus-single-article-buffer
4271         (gnus-article-setup-buffer))
4272       ;; First and last article in this newsgroup.
4273       (when gnus-newsgroup-headers
4274         (setq gnus-newsgroup-begin
4275               (mail-header-number (car gnus-newsgroup-headers))
4276               gnus-newsgroup-end
4277               (mail-header-number
4278                (gnus-last-element gnus-newsgroup-headers))))
4279       ;; GROUP is successfully selected.
4280       (or gnus-newsgroup-headers t)))))
4281
4282 (defun gnus-articles-to-read (group &optional read-all)
4283   "Find out what articles the user wants to read."
4284   (let* ((articles
4285           ;; Select all articles if `read-all' is non-nil, or if there
4286           ;; are no unread articles.
4287           (if (or read-all
4288                   (and (zerop (length gnus-newsgroup-marked))
4289                        (zerop (length gnus-newsgroup-unreads)))
4290                   (eq (gnus-group-find-parameter group 'display)
4291                       'all))
4292               (or
4293                (gnus-uncompress-range (gnus-active group))
4294                (gnus-cache-articles-in-group group))
4295             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4296                           (copy-sequence gnus-newsgroup-unreads))
4297                   '<)))
4298          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4299          (scored (length scored-list))
4300          (number (length articles))
4301          (marked (+ (length gnus-newsgroup-marked)
4302                     (length gnus-newsgroup-dormant)))
4303          (select
4304           (cond
4305            ((numberp read-all)
4306             read-all)
4307            (t
4308             (condition-case ()
4309                 (cond
4310                  ((and (or (<= scored marked) (= scored number))
4311                        (numberp gnus-large-newsgroup)
4312                        (> number gnus-large-newsgroup))
4313                   (let ((input
4314                          (read-string
4315                           (format
4316                            "How many articles from %s (default %d): "
4317                            (gnus-limit-string gnus-newsgroup-name 35)
4318                            number))))
4319                     (if (string-match "^[ \t]*$" input) number input)))
4320                  ((and (> scored marked) (< scored number)
4321                        (> (- scored number) 20))
4322                   (let ((input
4323                          (read-string
4324                           (format "%s %s (%d scored, %d total): "
4325                                   "How many articles from"
4326                                   group scored number))))
4327                     (if (string-match "^[ \t]*$" input)
4328                         number input)))
4329                  (t number))
4330               (quit
4331                (message "Quit getting the articles to read")
4332                nil))))))
4333     (setq select (if (stringp select) (string-to-number select) select))
4334     (if (or (null select) (zerop select))
4335         select
4336       (if (and (not (zerop scored)) (<= (abs select) scored))
4337           (progn
4338             (setq articles (sort scored-list '<))
4339             (setq number (length articles)))
4340         (setq articles (copy-sequence articles)))
4341
4342       (when (< (abs select) number)
4343         (if (< select 0)
4344             ;; Select the N oldest articles.
4345             (setcdr (nthcdr (1- (abs select)) articles) nil)
4346           ;; Select the N most recent articles.
4347           (setq articles (nthcdr (- number select) articles))))
4348       (setq gnus-newsgroup-unselected
4349             (gnus-sorted-intersection
4350              gnus-newsgroup-unreads
4351              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4352       (when gnus-alter-articles-to-read-function
4353         (setq gnus-newsgroup-unreads
4354               (sort 
4355                (funcall gnus-alter-articles-to-read-function
4356                         gnus-newsgroup-name gnus-newsgroup-unreads)
4357                '<)))
4358       articles)))
4359
4360 (defun gnus-killed-articles (killed articles)
4361   (let (out)
4362     (while articles
4363       (when (inline (gnus-member-of-range (car articles) killed))
4364         (push (car articles) out))
4365       (setq articles (cdr articles)))
4366     out))
4367
4368 (defun gnus-uncompress-marks (marks)
4369   "Uncompress the mark ranges in MARKS."
4370   (let ((uncompressed '(score bookmark))
4371         out)
4372     (while marks
4373       (if (memq (caar marks) uncompressed)
4374           (push (car marks) out)
4375         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4376       (setq marks (cdr marks)))
4377     out))
4378
4379 (defun gnus-adjust-marked-articles (info)
4380   "Set all article lists and remove all marks that are no longer valid."
4381   (let* ((marked-lists (gnus-info-marks info))
4382          (active (gnus-active (gnus-info-group info)))
4383          (min (car active))
4384          (max (cdr active))
4385          (types gnus-article-mark-lists)
4386          (uncompressed '(score bookmark killed))
4387          marks var articles article mark)
4388
4389     (while marked-lists
4390       (setq marks (pop marked-lists))
4391       (set (setq var (intern (format "gnus-newsgroup-%s"
4392                                      (car (rassq (setq mark (car marks))
4393                                                  types)))))
4394            (if (memq (car marks) uncompressed) (cdr marks)
4395              (gnus-uncompress-range (cdr marks))))
4396
4397       (setq articles (symbol-value var))
4398
4399       ;; All articles have to be subsets of the active articles.
4400       (cond
4401        ;; Adjust "simple" lists.
4402        ((memq mark '(tick dormant expire reply save))
4403         (while articles
4404           (when (or (< (setq article (pop articles)) min) (> article max))
4405             (set var (delq article (symbol-value var))))))
4406        ;; Adjust assocs.
4407        ((memq mark uncompressed)
4408         (when (not (listp (cdr (symbol-value var))))
4409           (set var (list (symbol-value var))))
4410         (when (not (listp (cdr articles)))
4411           (setq articles (list articles)))
4412         (while articles
4413           (when (or (not (consp (setq article (pop articles))))
4414                     (< (car article) min)
4415                     (> (car article) max))
4416             (set var (delq article (symbol-value var))))))))))
4417
4418 (defun gnus-update-missing-marks (missing)
4419   "Go through the list of MISSING articles and remove them from the mark lists."
4420   (when missing
4421     (let ((types gnus-article-mark-lists)
4422           var m)
4423       ;; Go through all types.
4424       (while types
4425         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4426         (when (symbol-value var)
4427           ;; This list has articles.  So we delete all missing articles
4428           ;; from it.
4429           (setq m missing)
4430           (while m
4431             (set var (delq (pop m) (symbol-value var)))))))))
4432
4433 (defun gnus-update-marks ()
4434   "Enter the various lists of marked articles into the newsgroup info list."
4435   (let ((types gnus-article-mark-lists)
4436         (info (gnus-get-info gnus-newsgroup-name))
4437         (uncompressed '(score bookmark killed))
4438         type list newmarked symbol delta-marks)
4439     (when info
4440       ;; Add all marks lists to the list of marks lists.
4441       (while (setq type (pop types))
4442         (setq list (symbol-value
4443                     (setq symbol
4444                           (intern (format "gnus-newsgroup-%s"
4445                                           (car type))))))
4446
4447         (when list
4448           ;; Get rid of the entries of the articles that have the
4449           ;; default score.
4450           (when (and (eq (cdr type) 'score)
4451                      gnus-save-score
4452                      list)
4453             (let* ((arts list)
4454                    (prev (cons nil list))
4455                    (all prev))
4456               (while arts
4457                 (if (or (not (consp (car arts)))
4458                         (= (cdar arts) gnus-summary-default-score))
4459                     (setcdr prev (cdr arts))
4460                   (setq prev arts))
4461                 (setq arts (cdr arts)))
4462               (setq list (cdr all)))))
4463
4464         (unless (memq (cdr type) uncompressed)
4465           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4466        
4467         (when (gnus-check-backend-function
4468                'request-set-mark gnus-newsgroup-name)
4469           ;; propagate flags to server, with the following exceptions:
4470           ;; uncompressed:s are not proper flags (they are cons cells)
4471           ;; cache is a internal gnus flag
4472           ;; download are local to one gnus installation (well)
4473           ;; unsend are for nndraft groups only
4474           ;; xxx: generality of this?  this suits nnimap anyway
4475           (unless (memq (cdr type) (append '(cache download unsend)
4476                                            uncompressed))
4477             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4478                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4479                    (add (gnus-remove-from-range
4480                          (gnus-copy-sequence list) old)))
4481               (when add
4482                 (push (list add 'add (list (cdr type))) delta-marks))
4483               (when del
4484                 (push (list del 'del (list (cdr type))) delta-marks)))))
4485           
4486         (when list
4487           (push (cons (cdr type) list) newmarked)))
4488
4489       (when delta-marks
4490         (unless (gnus-check-group gnus-newsgroup-name)
4491           (error "Can't open server for %s" gnus-newsgroup-name))
4492         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4493           
4494       ;; Enter these new marks into the info of the group.
4495       (if (nthcdr 3 info)
4496           (setcar (nthcdr 3 info) newmarked)
4497         ;; Add the marks lists to the end of the info.
4498         (when newmarked
4499           (setcdr (nthcdr 2 info) (list newmarked))))
4500
4501       ;; Cut off the end of the info if there's nothing else there.
4502       (let ((i 5))
4503         (while (and (> i 2)
4504                     (not (nth i info)))
4505           (when (nthcdr (decf i) info)
4506             (setcdr (nthcdr i info) nil)))))))
4507
4508 (defun gnus-set-mode-line (where)
4509   "Set the mode line of the article or summary buffers.
4510 If WHERE is `summary', the summary mode line format will be used."
4511   ;; Is this mode line one we keep updated?
4512   (when (and (memq where gnus-updated-mode-lines)
4513              (symbol-value
4514               (intern (format "gnus-%s-mode-line-format-spec" where))))
4515     (let (mode-string)
4516       (save-excursion
4517         ;; We evaluate this in the summary buffer since these
4518         ;; variables are buffer-local to that buffer.
4519         (set-buffer gnus-summary-buffer)
4520         ;; We bind all these variables that are used in the `eval' form
4521         ;; below.
4522         (let* ((mformat (symbol-value
4523                          (intern
4524                           (format "gnus-%s-mode-line-format-spec" where))))
4525                (gnus-tmp-group-name (gnus-group-name-decode 
4526                                      gnus-newsgroup-name
4527                                      (gnus-group-name-charset 
4528                                       nil
4529                                       gnus-newsgroup-name)))
4530                (gnus-tmp-article-number (or gnus-current-article 0))
4531                (gnus-tmp-unread gnus-newsgroup-unreads)
4532                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4533                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4534                (gnus-tmp-unread-and-unselected
4535                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4536                             (zerop gnus-tmp-unselected))
4537                        "")
4538                       ((zerop gnus-tmp-unselected)
4539                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4540                       (t (format "{%d(+%d) more}"
4541                                  gnus-tmp-unread-and-unticked
4542                                  gnus-tmp-unselected))))
4543                (gnus-tmp-subject
4544                 (if (and gnus-current-headers
4545                          (vectorp gnus-current-headers))
4546                     (gnus-mode-string-quote
4547                      (mail-header-subject gnus-current-headers))
4548                   ""))
4549                bufname-length max-len
4550                gnus-tmp-header);; passed as argument to any user-format-funcs
4551           (setq mode-string (eval mformat))
4552           (setq bufname-length (if (string-match "%b" mode-string)
4553                                    (- (length
4554                                        (buffer-name
4555                                         (if (eq where 'summary)
4556                                             nil
4557                                           (get-buffer gnus-article-buffer))))
4558                                       2)
4559                                  0))
4560           (setq max-len (max 4 (if gnus-mode-non-string-length
4561                                    (- (window-width)
4562                                       gnus-mode-non-string-length
4563                                       bufname-length)
4564                                  (length mode-string))))
4565           ;; We might have to chop a bit of the string off...
4566           (when (> (length mode-string) max-len)
4567             (setq mode-string
4568                   (concat (truncate-string-to-width mode-string (- max-len 3))
4569                           "...")))
4570           ;; Pad the mode string a bit.
4571           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4572       ;; Update the mode line.
4573       (setq mode-line-buffer-identification
4574             (gnus-mode-line-buffer-identification (list mode-string)))
4575       (set-buffer-modified-p t))))
4576
4577 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4578   "Go through the HEADERS list and add all Xrefs to a hash table.
4579 The resulting hash table is returned, or nil if no Xrefs were found."
4580   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4581          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4582          (xref-hashtb (gnus-make-hashtable))
4583          start group entry number xrefs header)
4584     (while headers
4585       (setq header (pop headers))
4586       (when (and (setq xrefs (mail-header-xref header))
4587                  (not (memq (setq number (mail-header-number header))
4588                             unreads)))
4589         (setq start 0)
4590         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4591           (setq start (match-end 0))
4592           (setq group (if prefix
4593                           (concat prefix (substring xrefs (match-beginning 1)
4594                                                     (match-end 1)))
4595                         (substring xrefs (match-beginning 1) (match-end 1))))
4596           (setq number
4597                 (string-to-int (substring xrefs (match-beginning 2)
4598                                           (match-end 2))))
4599           (if (setq entry (gnus-gethash group xref-hashtb))
4600               (setcdr entry (cons number (cdr entry)))
4601             (gnus-sethash group (cons number nil) xref-hashtb)))))
4602     (and start xref-hashtb)))
4603
4604 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4605   "Look through all the headers and mark the Xrefs as read."
4606   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4607         name entry info xref-hashtb idlist method nth4)
4608     (save-excursion
4609       (set-buffer gnus-group-buffer)
4610       (when (setq xref-hashtb
4611                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4612         (mapatoms
4613          (lambda (group)
4614            (unless (string= from-newsgroup (setq name (symbol-name group)))
4615              (setq idlist (symbol-value group))
4616              ;; Dead groups are not updated.
4617              (and (prog1
4618                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4619                             info (nth 2 entry))
4620                     (when (stringp (setq nth4 (gnus-info-method info)))
4621                       (setq nth4 (gnus-server-to-method nth4))))
4622                   ;; Only do the xrefs if the group has the same
4623                   ;; select method as the group we have just read.
4624                   (or (gnus-methods-equal-p
4625                        nth4 (gnus-find-method-for-group from-newsgroup))
4626                       virtual
4627                       (equal nth4 (setq method (gnus-find-method-for-group
4628                                                 from-newsgroup)))
4629                       (and (equal (car nth4) (car method))
4630                            (equal (nth 1 nth4) (nth 1 method))))
4631                   gnus-use-cross-reference
4632                   (or (not (eq gnus-use-cross-reference t))
4633                       virtual
4634                       ;; Only do cross-references on subscribed
4635                       ;; groups, if that is what is wanted.
4636                       (<= (gnus-info-level info) gnus-level-subscribed))
4637                   (gnus-group-make-articles-read name idlist))))
4638          xref-hashtb)))))
4639
4640 (defun gnus-compute-read-articles (group articles)
4641   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4642          (info (nth 2 entry))
4643          (active (gnus-active group))
4644          ninfo)
4645     (when entry
4646       ;; First peel off all invalid article numbers.
4647       (when active
4648         (let ((ids articles)
4649               id first)
4650           (while (setq id (pop ids))
4651             (when (and first (> id (cdr active)))
4652               ;; We'll end up in this situation in one particular
4653               ;; obscure situation.  If you re-scan a group and get
4654               ;; a new article that is cross-posted to a different
4655               ;; group that has not been re-scanned, you might get
4656               ;; crossposted article that has a higher number than
4657               ;; Gnus believes possible.  So we re-activate this
4658               ;; group as well.  This might mean doing the
4659               ;; crossposting thingy will *increase* the number
4660               ;; of articles in some groups.  Tsk, tsk.
4661               (setq active (or (gnus-activate-group group) active)))
4662             (when (or (> id (cdr active))
4663                       (< id (car active)))
4664               (setq articles (delq id articles))))))
4665       ;; If the read list is nil, we init it.
4666       (if (and active
4667                (null (gnus-info-read info))
4668                (> (car active) 1))
4669           (setq ninfo (cons 1 (1- (car active))))
4670         (setq ninfo (gnus-info-read info)))
4671       ;; Then we add the read articles to the range.
4672       (gnus-add-to-range
4673        ninfo (setq articles (sort articles '<))))))
4674
4675 (defun gnus-group-make-articles-read (group articles)
4676   "Update the info of GROUP to say that ARTICLES are read."
4677   (let* ((num 0)
4678          (entry (gnus-gethash group gnus-newsrc-hashtb))
4679          (info (nth 2 entry))
4680          (active (gnus-active group))
4681          range)
4682     (when entry
4683       (setq range (gnus-compute-read-articles group articles))
4684       (save-excursion
4685         (set-buffer gnus-group-buffer)
4686         (gnus-undo-register
4687           `(progn
4688              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4689              (gnus-info-set-read ',info ',(gnus-info-read info))
4690              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4691              (gnus-group-update-group ,group t))))
4692       ;; Add the read articles to the range.
4693       (gnus-info-set-read info range)
4694       ;; Then we have to re-compute how many unread
4695       ;; articles there are in this group.
4696       (when active
4697         (cond
4698          ((not range)
4699           (setq num (- (1+ (cdr active)) (car active))))
4700          ((not (listp (cdr range)))
4701           (setq num (- (cdr active) (- (1+ (cdr range))
4702                                        (car range)))))
4703          (t
4704           (while range
4705             (if (numberp (car range))
4706                 (setq num (1+ num))
4707               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4708             (setq range (cdr range)))
4709           (setq num (- (cdr active) num))))
4710         ;; Update the number of unread articles.
4711         (setcar entry num)
4712         ;; Update the group buffer.
4713         (gnus-group-update-group group t)))))
4714
4715 (defvar gnus-newsgroup-none-id 0)
4716
4717 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4718   (let ((cur nntp-server-buffer)
4719         (dependencies
4720          (or dependencies
4721              (save-excursion (set-buffer gnus-summary-buffer)
4722                              gnus-newsgroup-dependencies)))
4723         headers id end ref
4724         (mail-parse-charset gnus-newsgroup-charset)
4725         (mail-parse-ignored-charsets
4726          (save-excursion (condition-case nil
4727                              (set-buffer gnus-summary-buffer)
4728                            (error))
4729                          gnus-newsgroup-ignored-charsets)))
4730     (save-excursion
4731       (set-buffer nntp-server-buffer)
4732       ;; Translate all TAB characters into SPACE characters.
4733       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4734       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4735       (gnus-run-hooks 'gnus-parse-headers-hook)
4736       (let ((case-fold-search t)
4737             in-reply-to header p lines chars)
4738         (goto-char (point-min))
4739         ;; Search to the beginning of the next header.  Error messages
4740         ;; do not begin with 2 or 3.
4741         (while (re-search-forward "^[23][0-9]+ " nil t)
4742           (setq id nil
4743                 ref nil)
4744           ;; This implementation of this function, with nine
4745           ;; search-forwards instead of the one re-search-forward and
4746           ;; a case (which basically was the old function) is actually
4747           ;; about twice as fast, even though it looks messier.  You
4748           ;; can't have everything, I guess.  Speed and elegance
4749           ;; doesn't always go hand in hand.
4750           (setq
4751            header
4752            (vector
4753             ;; Number.
4754             (prog1
4755                 (read cur)
4756               (end-of-line)
4757               (setq p (point))
4758               (narrow-to-region (point)
4759                                 (or (and (search-forward "\n.\n" nil t)
4760                                          (- (point) 2))
4761                                     (point))))
4762             ;; Subject.
4763             (progn
4764               (goto-char p)
4765               (if (search-forward "\nsubject: " nil t)
4766                   (funcall gnus-decode-encoded-word-function
4767                            (nnheader-header-value))
4768                 "(none)"))
4769             ;; From.
4770             (progn
4771               (goto-char p)
4772               (if (or (search-forward "\nfrom: " nil t)
4773                       (search-forward "\nfrom:" nil t))
4774                   (funcall gnus-decode-encoded-word-function
4775                            (nnheader-header-value))
4776                 "(nobody)"))
4777             ;; Date.
4778             (progn
4779               (goto-char p)
4780               (if (search-forward "\ndate: " nil t)
4781                   (nnheader-header-value) ""))
4782             ;; Message-ID.
4783             (progn
4784               (goto-char p)
4785               (setq id (if (re-search-forward
4786                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4787                            ;; We do it this way to make sure the Message-ID
4788                            ;; is (somewhat) syntactically valid.
4789                            (buffer-substring (match-beginning 1)
4790                                              (match-end 1))
4791                          ;; If there was no message-id, we just fake one
4792                          ;; to make subsequent routines simpler.
4793                          (nnheader-generate-fake-message-id))))
4794             ;; References.
4795             (progn
4796               (goto-char p)
4797               (if (search-forward "\nreferences: " nil t)
4798                   (progn
4799                     (setq end (point))
4800                     (prog1
4801                         (nnheader-header-value)
4802                       (setq ref
4803                             (buffer-substring
4804                              (progn
4805                                (end-of-line)
4806                                (search-backward ">" end t)
4807                                (1+ (point)))
4808                              (progn
4809                                (search-backward "<" end t)
4810                                (point))))))
4811                 ;; Get the references from the in-reply-to header if there
4812                 ;; were no references and the in-reply-to header looks
4813                 ;; promising.
4814                 (if (and (search-forward "\nin-reply-to: " nil t)
4815                          (setq in-reply-to (nnheader-header-value))
4816                          (string-match "<[^>]+>" in-reply-to))
4817                     (let (ref2)
4818                       (setq ref (substring in-reply-to (match-beginning 0)
4819                                            (match-end 0)))
4820                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4821                         (setq ref2 (substring in-reply-to (match-beginning 0)
4822                                               (match-end 0)))
4823                         (when (> (length ref2) (length ref))
4824                           (setq ref ref2)))
4825                       ref)
4826                   (setq ref nil))))
4827             ;; Chars.
4828             (progn
4829               (goto-char p)
4830               (if (search-forward "\nchars: " nil t)
4831                   (if (numberp (setq chars (ignore-errors (read cur))))
4832                       chars 0)
4833                 0))
4834             ;; Lines.
4835             (progn
4836               (goto-char p)
4837               (if (search-forward "\nlines: " nil t)
4838                   (if (numberp (setq lines (ignore-errors (read cur))))
4839                       lines 0)
4840                 0))
4841             ;; Xref.
4842             (progn
4843               (goto-char p)
4844               (and (search-forward "\nxref: " nil t)
4845                    (nnheader-header-value)))
4846             ;; Extra.
4847             (when gnus-extra-headers
4848               (let ((extra gnus-extra-headers)
4849                     out)
4850                 (while extra
4851                   (goto-char p)
4852                   (when (search-forward
4853                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4854                     (push (cons (car extra) (nnheader-header-value))
4855                           out))
4856                   (pop extra))
4857                 out))))
4858           (when (equal id ref)
4859             (setq ref nil))
4860
4861           (when gnus-alter-header-function
4862             (funcall gnus-alter-header-function header)
4863             (setq id (mail-header-id header)
4864                   ref (gnus-parent-id (mail-header-references header))))
4865
4866           (when (setq header
4867                       (gnus-dependencies-add-header
4868                        header dependencies force-new))
4869             (push header headers))
4870           (goto-char (point-max))
4871           (widen))
4872         (nreverse headers)))))
4873
4874 ;; Goes through the xover lines and returns a list of vectors
4875 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4876                                                   force-new dependencies
4877                                                   group also-fetch-heads)
4878   "Parse the news overview data in the server buffer.
4879 Return a list of headers that match SEQUENCE (see
4880 `nntp-retrieve-headers')."
4881   ;; Get the Xref when the users reads the articles since most/some
4882   ;; NNTP servers do not include Xrefs when using XOVER.
4883   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4884   (let ((mail-parse-charset gnus-newsgroup-charset)
4885         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4886         (cur nntp-server-buffer)
4887         (dependencies (or dependencies gnus-newsgroup-dependencies))
4888         number headers header)
4889     (save-excursion
4890       (set-buffer nntp-server-buffer)
4891       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4892       ;; Allow the user to mangle the headers before parsing them.
4893       (gnus-run-hooks 'gnus-parse-headers-hook)
4894       (goto-char (point-min))
4895       (while (not (eobp))
4896         (condition-case ()
4897             (while (and sequence (not (eobp)))
4898               (setq number (read cur))
4899               (while (and sequence
4900                           (< (car sequence) number))
4901                 (setq sequence (cdr sequence)))
4902               (and sequence
4903                    (eq number (car sequence))
4904                    (progn
4905                      (setq sequence (cdr sequence))
4906                      (setq header (inline
4907                                     (gnus-nov-parse-line
4908                                      number dependencies force-new))))
4909                    (push header headers))
4910               (forward-line 1))
4911           (error
4912            (gnus-error 4 "Strange nov line (%d)"
4913                        (count-lines (point-min) (point)))))
4914         (forward-line 1))
4915       ;; A common bug in inn is that if you have posted an article and
4916       ;; then retrieves the active file, it will answer correctly --
4917       ;; the new article is included.  However, a NOV entry for the
4918       ;; article may not have been generated yet, so this may fail.
4919       ;; We work around this problem by retrieving the last few
4920       ;; headers using HEAD.
4921       (if (or (not also-fetch-heads)
4922               (not sequence))
4923           ;; We (probably) got all the headers.
4924           (nreverse headers)
4925         (let ((gnus-nov-is-evil t))
4926           (nconc
4927            (nreverse headers)
4928            (when (gnus-retrieve-headers sequence group)
4929              (gnus-get-newsgroup-headers))))))))
4930
4931 (defun gnus-article-get-xrefs ()
4932   "Fill in the Xref value in `gnus-current-headers', if necessary.
4933 This is meant to be called in `gnus-article-internal-prepare-hook'."
4934   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4935                                  gnus-current-headers)))
4936     (or (not gnus-use-cross-reference)
4937         (not headers)
4938         (and (mail-header-xref headers)
4939              (not (string= (mail-header-xref headers) "")))
4940         (let ((case-fold-search t)
4941               xref)
4942           (save-restriction
4943             (nnheader-narrow-to-headers)
4944             (goto-char (point-min))
4945             (when (or (and (not (eobp))
4946                            (eq (downcase (char-after)) ?x)
4947                            (looking-at "Xref:"))
4948                       (search-forward "\nXref:" nil t))
4949               (goto-char (1+ (match-end 0)))
4950               (setq xref (buffer-substring (point)
4951                                            (progn (end-of-line) (point))))
4952               (mail-header-set-xref headers xref)))))))
4953
4954 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4955   "Find article ID and insert the summary line for that article.
4956 OLD-HEADER can either be a header or a line number to insert
4957 the subject line on."
4958   (let* ((line (and (numberp old-header) old-header))
4959          (old-header (and (vectorp old-header) old-header))
4960          (header (cond ((and old-header use-old-header)
4961                         old-header)
4962                        ((and (numberp id)
4963                              (gnus-number-to-header id))
4964                         (gnus-number-to-header id))
4965                        (t
4966                         (gnus-read-header id))))
4967          (number (and (numberp id) id))
4968          d)
4969     (when header
4970       ;; Rebuild the thread that this article is part of and go to the
4971       ;; article we have fetched.
4972       (when (and (not gnus-show-threads)
4973                  old-header)
4974         (when (and number
4975                    (setq d (gnus-data-find (mail-header-number old-header))))
4976           (goto-char (gnus-data-pos d))
4977           (gnus-data-remove
4978            number
4979            (- (gnus-point-at-bol)
4980               (prog1
4981                   (1+ (gnus-point-at-eol))
4982                 (gnus-delete-line))))))
4983       (when old-header
4984         (mail-header-set-number header (mail-header-number old-header)))
4985       (setq gnus-newsgroup-sparse
4986             (delq (setq number (mail-header-number header))
4987                   gnus-newsgroup-sparse))
4988       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4989       (push number gnus-newsgroup-limit)
4990       (gnus-rebuild-thread (mail-header-id header) line)
4991       (gnus-summary-goto-subject number nil t))
4992     (when (and (numberp number)
4993                (> number 0))
4994       ;; We have to update the boundaries even if we can't fetch the
4995       ;; article if ID is a number -- so that the next `P' or `N'
4996       ;; command will fetch the previous (or next) article even
4997       ;; if the one we tried to fetch this time has been canceled.
4998       (when (> number gnus-newsgroup-end)
4999         (setq gnus-newsgroup-end number))
5000       (when (< number gnus-newsgroup-begin)
5001         (setq gnus-newsgroup-begin number))
5002       (setq gnus-newsgroup-unselected
5003             (delq number gnus-newsgroup-unselected)))
5004     ;; Report back a success?
5005     (and header (mail-header-number header))))
5006
5007 ;;; Process/prefix in the summary buffer
5008
5009 (defun gnus-summary-work-articles (n)
5010   "Return a list of articles to be worked upon.
5011 The prefix argument, the list of process marked articles, and the
5012 current article will be taken into consideration."
5013   (save-excursion
5014     (set-buffer gnus-summary-buffer)
5015     (cond
5016      (n
5017       ;; A numerical prefix has been given.
5018       (setq n (prefix-numeric-value n))
5019       (let ((backward (< n 0))
5020             (n (abs (prefix-numeric-value n)))
5021             articles article)
5022         (save-excursion
5023           (while
5024               (and (> n 0)
5025                    (push (setq article (gnus-summary-article-number))
5026                          articles)
5027                    (if backward
5028                        (gnus-summary-find-prev nil article)
5029                      (gnus-summary-find-next nil article)))
5030             (decf n)))
5031         (nreverse articles)))
5032      ((and (gnus-region-active-p) (mark))
5033       (message "region active")
5034       ;; Work on the region between point and mark.
5035       (let ((max (max (point) (mark)))
5036             articles article)
5037         (save-excursion
5038           (goto-char (min (min (point) (mark))))
5039           (while
5040               (and
5041                (push (setq article (gnus-summary-article-number)) articles)
5042                (gnus-summary-find-next nil article)
5043                (< (point) max)))
5044           (nreverse articles))))
5045      (gnus-newsgroup-processable
5046       ;; There are process-marked articles present.
5047       ;; Save current state.
5048       (gnus-summary-save-process-mark)
5049       ;; Return the list.
5050       (reverse gnus-newsgroup-processable))
5051      (t
5052       ;; Just return the current article.
5053       (list (gnus-summary-article-number))))))
5054
5055 (defmacro gnus-summary-iterate (arg &rest forms)
5056   "Iterate over the process/prefixed articles and do FORMS.
5057 ARG is the interactive prefix given to the command.  FORMS will be
5058 executed with point over the summary line of the articles."
5059   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5060     `(let ((,articles (gnus-summary-work-articles ,arg)))
5061        (while ,articles
5062          (gnus-summary-goto-subject (car ,articles))
5063          ,@forms
5064          (pop ,articles)))))
5065
5066 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5067 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5068
5069 (defun gnus-summary-save-process-mark ()
5070   "Push the current set of process marked articles on the stack."
5071   (interactive)
5072   (push (copy-sequence gnus-newsgroup-processable)
5073         gnus-newsgroup-process-stack))
5074
5075 (defun gnus-summary-kill-process-mark ()
5076   "Push the current set of process marked articles on the stack and unmark."
5077   (interactive)
5078   (gnus-summary-save-process-mark)
5079   (gnus-summary-unmark-all-processable))
5080
5081 (defun gnus-summary-yank-process-mark ()
5082   "Pop the last process mark state off the stack and restore it."
5083   (interactive)
5084   (unless gnus-newsgroup-process-stack
5085     (error "Empty mark stack"))
5086   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5087
5088 (defun gnus-summary-process-mark-set (set)
5089   "Make SET into the current process marked articles."
5090   (gnus-summary-unmark-all-processable)
5091   (while set
5092     (gnus-summary-set-process-mark (pop set))))
5093
5094 ;;; Searching and stuff
5095
5096 (defun gnus-summary-search-group (&optional backward use-level)
5097   "Search for next unread newsgroup.
5098 If optional argument BACKWARD is non-nil, search backward instead."
5099   (save-excursion
5100     (set-buffer gnus-group-buffer)
5101     (when (gnus-group-search-forward
5102            backward nil (if use-level (gnus-group-group-level) nil))
5103       (gnus-group-group-name))))
5104
5105 (defun gnus-summary-best-group (&optional exclude-group)
5106   "Find the name of the best unread group.
5107 If EXCLUDE-GROUP, do not go to this group."
5108   (save-excursion
5109     (set-buffer gnus-group-buffer)
5110     (save-excursion
5111       (gnus-group-best-unread-group exclude-group))))
5112
5113 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5114   (if backward (gnus-summary-find-prev)
5115     (let* ((dummy (gnus-summary-article-intangible-p))
5116            (article (or article (gnus-summary-article-number)))
5117            (arts (gnus-data-find-list article))
5118            result)
5119       (when (and (not dummy)
5120                  (or (not gnus-summary-check-current)
5121                      (not unread)
5122                      (not (gnus-data-unread-p (car arts)))))
5123         (setq arts (cdr arts)))
5124       (when (setq result
5125                   (if unread
5126                       (progn
5127                         (while arts
5128                           (when (or (and undownloaded
5129                                          (eq gnus-undownloaded-mark
5130                                              (gnus-data-mark (car arts))))
5131                                     (gnus-data-unread-p (car arts)))
5132                             (setq result (car arts)
5133                                   arts nil))
5134                           (setq arts (cdr arts)))
5135                         result)
5136                     (car arts)))
5137         (goto-char (gnus-data-pos result))
5138         (gnus-data-number result)))))
5139
5140 (defun gnus-summary-find-prev (&optional unread article)
5141   (let* ((eobp (eobp))
5142          (article (or article (gnus-summary-article-number)))
5143          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5144          result)
5145     (when (and (not eobp)
5146                (or (not gnus-summary-check-current)
5147                    (not unread)
5148                    (not (gnus-data-unread-p (car arts)))))
5149       (setq arts (cdr arts)))
5150     (when (setq result
5151                 (if unread
5152                     (progn
5153                       (while arts
5154                         (when (gnus-data-unread-p (car arts))
5155                           (setq result (car arts)
5156                                 arts nil))
5157                         (setq arts (cdr arts)))
5158                       result)
5159                   (car arts)))
5160       (goto-char (gnus-data-pos result))
5161       (gnus-data-number result))))
5162
5163 (defun gnus-summary-find-subject (subject &optional unread backward article)
5164   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5165          (article (or article (gnus-summary-article-number)))
5166          (articles (gnus-data-list backward))
5167          (arts (gnus-data-find-list article articles))
5168          result)
5169     (when (or (not gnus-summary-check-current)
5170               (not unread)
5171               (not (gnus-data-unread-p (car arts))))
5172       (setq arts (cdr arts)))
5173     (while arts
5174       (and (or (not unread)
5175                (gnus-data-unread-p (car arts)))
5176            (vectorp (gnus-data-header (car arts)))
5177            (gnus-subject-equal
5178             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5179            (setq result (car arts)
5180                  arts nil))
5181       (setq arts (cdr arts)))
5182     (and result
5183          (goto-char (gnus-data-pos result))
5184          (gnus-data-number result))))
5185
5186 (defun gnus-summary-search-forward (&optional unread subject backward)
5187   "Search forward for an article.
5188 If UNREAD, look for unread articles.  If SUBJECT, look for
5189 articles with that subject.  If BACKWARD, search backward instead."
5190   (cond (subject (gnus-summary-find-subject subject unread backward))
5191         (backward (gnus-summary-find-prev unread))
5192         (t (gnus-summary-find-next unread))))
5193
5194 (defun gnus-recenter (&optional n)
5195   "Center point in window and redisplay frame.
5196 Also do horizontal recentering."
5197   (interactive "P")
5198   (when (and gnus-auto-center-summary
5199              (not (eq gnus-auto-center-summary 'vertical)))
5200     (gnus-horizontal-recenter))
5201   (recenter n))
5202
5203 (defun gnus-summary-recenter ()
5204   "Center point in the summary window.
5205 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5206 displayed, no centering will be performed."
5207   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5208   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5209   (interactive)
5210   (let* ((top (cond ((< (window-height) 4) 0)
5211                     ((< (window-height) 7) 1)
5212                     (t (if (numberp gnus-auto-center-summary)
5213                            gnus-auto-center-summary
5214                          2))))
5215          (height (1- (window-height)))
5216          (bottom (save-excursion (goto-char (point-max))
5217                                  (forward-line (- height))
5218                                  (point)))
5219          (window (get-buffer-window (current-buffer))))
5220     ;; The user has to want it.
5221     (when gnus-auto-center-summary
5222       (when (get-buffer-window gnus-article-buffer)
5223         ;; Only do recentering when the article buffer is displayed,
5224         ;; Set the window start to either `bottom', which is the biggest
5225         ;; possible valid number, or the second line from the top,
5226         ;; whichever is the least.
5227         (set-window-start
5228          window (min bottom (save-excursion
5229                               (forward-line (- top)) (point)))
5230          t))
5231       ;; Do horizontal recentering while we're at it.
5232       (when (and (get-buffer-window (current-buffer) t)
5233                  (not (eq gnus-auto-center-summary 'vertical)))
5234         (let ((selected (selected-window)))
5235           (select-window (get-buffer-window (current-buffer) t))
5236           (gnus-summary-position-point)
5237           (gnus-horizontal-recenter)
5238           (select-window selected))))))
5239
5240 (defun gnus-summary-jump-to-group (newsgroup)
5241   "Move point to NEWSGROUP in group mode buffer."
5242   ;; Keep update point of group mode buffer if visible.
5243   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5244       (save-window-excursion
5245         ;; Take care of tree window mode.
5246         (when (get-buffer-window gnus-group-buffer)
5247           (pop-to-buffer gnus-group-buffer))
5248         (gnus-group-jump-to-group newsgroup))
5249     (save-excursion
5250       ;; Take care of tree window mode.
5251       (if (get-buffer-window gnus-group-buffer)
5252           (pop-to-buffer gnus-group-buffer)
5253         (set-buffer gnus-group-buffer))
5254       (gnus-group-jump-to-group newsgroup))))
5255
5256 ;; This function returns a list of article numbers based on the
5257 ;; difference between the ranges of read articles in this group and
5258 ;; the range of active articles.
5259 (defun gnus-list-of-unread-articles (group)
5260   (let* ((read (gnus-info-read (gnus-get-info group)))
5261          (active (or (gnus-active group) (gnus-activate-group group)))
5262          (last (cdr active))
5263          first nlast unread)
5264     ;; If none are read, then all are unread.
5265     (if (not read)
5266         (setq first (car active))
5267       ;; If the range of read articles is a single range, then the
5268       ;; first unread article is the article after the last read
5269       ;; article.  Sounds logical, doesn't it?
5270       (if (and (not (listp (cdr read)))
5271                (or (< (car read) (car active))
5272                    (progn (setq read (list read))
5273                           nil)))
5274           (setq first (max (car active) (1+ (cdr read))))
5275         ;; `read' is a list of ranges.
5276         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5277                                   (caar read)))
5278                   1)
5279           (setq first (car active)))
5280         (while read
5281           (when first
5282             (while (< first nlast)
5283               (push first unread)
5284               (setq first (1+ first))))
5285           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5286           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5287           (setq read (cdr read)))))
5288     ;; And add the last unread articles.
5289     (while (<= first last)
5290       (push first unread)
5291       (setq first (1+ first)))
5292     ;; Return the list of unread articles.
5293     (delq 0 (nreverse unread))))
5294
5295 (defun gnus-list-of-read-articles (group)
5296   "Return a list of unread, unticked and non-dormant articles."
5297   (let* ((info (gnus-get-info group))
5298          (marked (gnus-info-marks info))
5299          (active (gnus-active group)))
5300     (and info active
5301          (gnus-set-difference
5302           (gnus-sorted-complement
5303            (gnus-uncompress-range active)
5304            (gnus-list-of-unread-articles group))
5305           (append
5306            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5307            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5308
5309 ;; Various summary commands
5310
5311 (defun gnus-summary-select-article-buffer ()
5312   "Reconfigure windows to show article buffer."
5313   (interactive)
5314   (if (not (gnus-buffer-live-p gnus-article-buffer))
5315       (error "There is no article buffer for this summary buffer")
5316     (gnus-configure-windows 'article)
5317     (select-window (get-buffer-window gnus-article-buffer))))
5318
5319 (defun gnus-summary-universal-argument (arg)
5320   "Perform any operation on all articles that are process/prefixed."
5321   (interactive "P")
5322   (let ((articles (gnus-summary-work-articles arg))
5323         func article)
5324     (if (eq
5325          (setq
5326           func
5327           (key-binding
5328            (read-key-sequence
5329             (substitute-command-keys
5330              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5331          'undefined)
5332         (gnus-error 1 "Undefined key")
5333       (save-excursion
5334         (while articles
5335           (gnus-summary-goto-subject (setq article (pop articles)))
5336           (let (gnus-newsgroup-processable)
5337             (command-execute func))
5338           (gnus-summary-remove-process-mark article)))))
5339   (gnus-summary-position-point))
5340
5341 (defun gnus-summary-toggle-truncation (&optional arg)
5342   "Toggle truncation of summary lines.
5343 With arg, turn line truncation on iff arg is positive."
5344   (interactive "P")
5345   (setq truncate-lines
5346         (if (null arg) (not truncate-lines)
5347           (> (prefix-numeric-value arg) 0)))
5348   (redraw-display))
5349
5350 (defun gnus-summary-reselect-current-group (&optional all rescan)
5351   "Exit and then reselect the current newsgroup.
5352 The prefix argument ALL means to select all articles."
5353   (interactive "P")
5354   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5355     (error "Ephemeral groups can't be reselected"))
5356   (let ((current-subject (gnus-summary-article-number))
5357         (group gnus-newsgroup-name))
5358     (setq gnus-newsgroup-begin nil)
5359     (gnus-summary-exit)
5360     ;; We have to adjust the point of group mode buffer because
5361     ;; point was moved to the next unread newsgroup by exiting.
5362     (gnus-summary-jump-to-group group)
5363     (when rescan
5364       (save-excursion
5365         (gnus-group-get-new-news-this-group 1)))
5366     (gnus-group-read-group all t)
5367     (gnus-summary-goto-subject current-subject nil t)))
5368
5369 (defun gnus-summary-rescan-group (&optional all)
5370   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5371   (interactive "P")
5372   (gnus-summary-reselect-current-group all t))
5373
5374 (defun gnus-summary-update-info (&optional non-destructive)
5375   (save-excursion
5376     (let ((group gnus-newsgroup-name))
5377       (when group
5378         (when gnus-newsgroup-kill-headers
5379           (setq gnus-newsgroup-killed
5380                 (gnus-compress-sequence
5381                  (nconc
5382                   (gnus-set-sorted-intersection
5383                    (gnus-uncompress-range gnus-newsgroup-killed)
5384                    (setq gnus-newsgroup-unselected
5385                          (sort gnus-newsgroup-unselected '<)))
5386                   (setq gnus-newsgroup-unreads
5387                         (sort gnus-newsgroup-unreads '<)))
5388                  t)))
5389         (unless (listp (cdr gnus-newsgroup-killed))
5390           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5391         (let ((headers gnus-newsgroup-headers))
5392           ;; Set the new ranges of read articles.
5393           (save-excursion
5394             (set-buffer gnus-group-buffer)
5395             (gnus-undo-force-boundary))
5396           (gnus-update-read-articles
5397            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5398           ;; Set the current article marks.
5399           (let ((gnus-newsgroup-scored
5400                  (if (and (not gnus-save-score)
5401                           (not non-destructive))
5402                      nil
5403                    gnus-newsgroup-scored)))
5404             (save-excursion
5405               (gnus-update-marks)))
5406           ;; Do the cross-ref thing.
5407           (when gnus-use-cross-reference
5408             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5409           ;; Do not switch windows but change the buffer to work.
5410           (set-buffer gnus-group-buffer)
5411           (unless (gnus-ephemeral-group-p group)
5412             (gnus-group-update-group group)))))))
5413
5414 (defun gnus-summary-save-newsrc (&optional force)
5415   "Save the current number of read/marked articles in the dribble buffer.
5416 The dribble buffer will then be saved.
5417 If FORCE (the prefix), also save the .newsrc file(s)."
5418   (interactive "P")
5419   (gnus-summary-update-info t)
5420   (if force
5421       (gnus-save-newsrc-file)
5422     (gnus-dribble-save)))
5423
5424 (defun gnus-summary-exit (&optional temporary)
5425   "Exit reading current newsgroup, and then return to group selection mode.
5426 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5427   (interactive)
5428   (gnus-set-global-variables)
5429   (when (gnus-buffer-live-p gnus-article-buffer)
5430     (save-excursion
5431       (set-buffer gnus-article-buffer)
5432       (mm-destroy-parts gnus-article-mime-handles)
5433       ;; Set it to nil for safety reason.
5434       (setq gnus-article-mime-handle-alist nil)
5435       (setq gnus-article-mime-handles nil)))
5436   (gnus-kill-save-kill-buffer)
5437   (gnus-async-halt-prefetch)
5438   (let* ((group gnus-newsgroup-name)
5439          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5440          (mode major-mode)
5441          (group-point nil)
5442          (buf (current-buffer)))
5443     (unless quit-config
5444       ;; Do adaptive scoring, and possibly save score files.
5445       (when gnus-newsgroup-adaptive
5446         (gnus-score-adaptive))
5447       (when gnus-use-scoring
5448         (gnus-score-save)))
5449     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5450     ;; If we have several article buffers, we kill them at exit.
5451     (unless gnus-single-article-buffer
5452       (gnus-kill-buffer gnus-original-article-buffer)
5453       (setq gnus-article-current nil))
5454     (when gnus-use-cache
5455       (gnus-cache-possibly-remove-articles)
5456       (gnus-cache-save-buffers))
5457     (gnus-async-prefetch-remove-group group)
5458     (when gnus-suppress-duplicates
5459       (gnus-dup-enter-articles))
5460     (when gnus-use-trees
5461       (gnus-tree-close group))
5462     (when gnus-use-cache
5463       (gnus-cache-write-active))
5464     ;; Remove entries for this group.
5465     (nnmail-purge-split-history (gnus-group-real-name group))
5466     ;; Make all changes in this group permanent.
5467     (unless quit-config
5468       (gnus-run-hooks 'gnus-exit-group-hook)
5469       (gnus-summary-update-info))
5470     (gnus-close-group group)
5471     ;; Make sure where we were, and go to next newsgroup.
5472     (set-buffer gnus-group-buffer)
5473     (unless quit-config
5474       (gnus-group-jump-to-group group))
5475     (gnus-run-hooks 'gnus-summary-exit-hook)
5476     (unless (or quit-config
5477                 ;; If this group has disappeared from the summary
5478                 ;; buffer, don't skip forwards.
5479                 (not (string= group (gnus-group-group-name))))
5480       (gnus-group-next-unread-group 1))
5481     (setq group-point (point))
5482     (if temporary
5483         nil                             ;Nothing to do.
5484       ;; If we have several article buffers, we kill them at exit.
5485       (unless gnus-single-article-buffer
5486         (gnus-kill-buffer gnus-article-buffer)
5487         (gnus-kill-buffer gnus-original-article-buffer)
5488         (setq gnus-article-current nil))
5489       (set-buffer buf)
5490       (if (not gnus-kill-summary-on-exit)
5491           (gnus-deaden-summary)
5492         ;; We set all buffer-local variables to nil.  It is unclear why
5493         ;; this is needed, but if we don't, buffer-local variables are
5494         ;; not garbage-collected, it seems.  This would the lead to en
5495         ;; ever-growing Emacs.
5496         (gnus-summary-clear-local-variables)
5497         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5498           (gnus-summary-clear-local-variables))
5499         (when (get-buffer gnus-article-buffer)
5500           (bury-buffer gnus-article-buffer))
5501         ;; We clear the global counterparts of the buffer-local
5502         ;; variables as well, just to be on the safe side.
5503         (set-buffer gnus-group-buffer)
5504         (gnus-summary-clear-local-variables)
5505         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5506           (gnus-summary-clear-local-variables))
5507         ;; Return to group mode buffer.
5508         (when (eq mode 'gnus-summary-mode)
5509           (gnus-kill-buffer buf)))
5510       (setq gnus-current-select-method gnus-select-method)
5511       (pop-to-buffer gnus-group-buffer)
5512       (if (not quit-config)
5513           (progn
5514             (goto-char group-point)
5515             (gnus-configure-windows 'group 'force))
5516         (gnus-handle-ephemeral-exit quit-config))
5517       ;; Clear the current group name.
5518       (unless quit-config
5519         (setq gnus-newsgroup-name nil)))))
5520
5521 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5522 (defun gnus-summary-exit-no-update (&optional no-questions)
5523   "Quit reading current newsgroup without updating read article info."
5524   (interactive)
5525   (let* ((group gnus-newsgroup-name)
5526          (quit-config (gnus-group-quit-config group)))
5527     (when (or no-questions
5528               gnus-expert-user
5529               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5530       (gnus-async-halt-prefetch)
5531       (mapcar 'funcall
5532               (delq 'gnus-summary-expire-articles
5533                     (copy-sequence gnus-summary-prepare-exit-hook)))
5534       (when (gnus-buffer-live-p gnus-article-buffer)
5535         (save-excursion
5536           (set-buffer gnus-article-buffer)
5537           (mm-destroy-parts gnus-article-mime-handles)
5538           ;; Set it to nil for safety reason.
5539           (setq gnus-article-mime-handle-alist nil)
5540           (setq gnus-article-mime-handles nil)))
5541       ;; If we have several article buffers, we kill them at exit.
5542       (unless gnus-single-article-buffer
5543         (gnus-kill-buffer gnus-article-buffer)
5544         (gnus-kill-buffer gnus-original-article-buffer)
5545         (setq gnus-article-current nil))
5546       (if (not gnus-kill-summary-on-exit)
5547           (gnus-deaden-summary)
5548         (gnus-close-group group)
5549         (gnus-summary-clear-local-variables)
5550         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5551           (gnus-summary-clear-local-variables))
5552         (set-buffer gnus-group-buffer)
5553         (gnus-summary-clear-local-variables)
5554         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5555           (gnus-summary-clear-local-variables))
5556         (when (get-buffer gnus-summary-buffer)
5557           (kill-buffer gnus-summary-buffer)))
5558       (unless gnus-single-article-buffer
5559         (setq gnus-article-current nil))
5560       (when gnus-use-trees
5561         (gnus-tree-close group))
5562       (gnus-async-prefetch-remove-group group)
5563       (when (get-buffer gnus-article-buffer)
5564         (bury-buffer gnus-article-buffer))
5565       ;; Return to the group buffer.
5566       (gnus-configure-windows 'group 'force)
5567       ;; Clear the current group name.
5568       (setq gnus-newsgroup-name nil)
5569       (when (equal (gnus-group-group-name) group)
5570         (gnus-group-next-unread-group 1))
5571       (when quit-config
5572         (gnus-handle-ephemeral-exit quit-config)))))
5573
5574 (defun gnus-handle-ephemeral-exit (quit-config)
5575   "Handle movement when leaving an ephemeral group.
5576 The state which existed when entering the ephemeral is reset."
5577   (if (not (buffer-name (car quit-config)))
5578       (gnus-configure-windows 'group 'force)
5579     (set-buffer (car quit-config))
5580     (cond ((eq major-mode 'gnus-summary-mode)
5581            (gnus-set-global-variables))
5582           ((eq major-mode 'gnus-article-mode)
5583            (save-excursion
5584              ;; The `gnus-summary-buffer' variable may point
5585              ;; to the old summary buffer when using a single
5586              ;; article buffer.
5587              (unless (gnus-buffer-live-p gnus-summary-buffer)
5588                (set-buffer gnus-group-buffer))
5589              (set-buffer gnus-summary-buffer)
5590              (gnus-set-global-variables))))
5591     (if (or (eq (cdr quit-config) 'article)
5592             (eq (cdr quit-config) 'pick))
5593         (progn
5594           ;; The current article may be from the ephemeral group
5595           ;; thus it is best that we reload this article
5596           (gnus-summary-show-article)
5597           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5598               (gnus-configure-windows 'pick 'force)
5599             (gnus-configure-windows (cdr quit-config) 'force)))
5600       (gnus-configure-windows (cdr quit-config) 'force))
5601     (when (eq major-mode 'gnus-summary-mode)
5602       (gnus-summary-next-subject 1 nil t)
5603       (gnus-summary-recenter)
5604       (gnus-summary-position-point))))
5605
5606 ;;; Dead summaries.
5607
5608 (defvar gnus-dead-summary-mode-map nil)
5609
5610 (unless gnus-dead-summary-mode-map
5611   (setq gnus-dead-summary-mode-map (make-keymap))
5612   (suppress-keymap gnus-dead-summary-mode-map)
5613   (substitute-key-definition
5614    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5615   (let ((keys '("\C-d" "\r" "\177" [delete])))
5616     (while keys
5617       (define-key gnus-dead-summary-mode-map
5618         (pop keys) 'gnus-summary-wake-up-the-dead))))
5619
5620 (defvar gnus-dead-summary-mode nil
5621   "Minor mode for Gnus summary buffers.")
5622
5623 (defun gnus-dead-summary-mode (&optional arg)
5624   "Minor mode for Gnus summary buffers."
5625   (interactive "P")
5626   (when (eq major-mode 'gnus-summary-mode)
5627     (make-local-variable 'gnus-dead-summary-mode)
5628     (setq gnus-dead-summary-mode
5629           (if (null arg) (not gnus-dead-summary-mode)
5630             (> (prefix-numeric-value arg) 0)))
5631     (when gnus-dead-summary-mode
5632       (gnus-add-minor-mode
5633        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5634
5635 (defun gnus-deaden-summary ()
5636   "Make the current summary buffer into a dead summary buffer."
5637   ;; Kill any previous dead summary buffer.
5638   (when (and gnus-dead-summary
5639              (buffer-name gnus-dead-summary))
5640     (save-excursion
5641       (set-buffer gnus-dead-summary)
5642       (when gnus-dead-summary-mode
5643         (kill-buffer (current-buffer)))))
5644   ;; Make this the current dead summary.
5645   (setq gnus-dead-summary (current-buffer))
5646   (gnus-dead-summary-mode 1)
5647   (let ((name (buffer-name)))
5648     (when (string-match "Summary" name)
5649       (rename-buffer
5650        (concat (substring name 0 (match-beginning 0)) "Dead "
5651                (substring name (match-beginning 0)))
5652        t)
5653       (bury-buffer))))
5654
5655 (defun gnus-kill-or-deaden-summary (buffer)
5656   "Kill or deaden the summary BUFFER."
5657   (save-excursion
5658     (when (and (buffer-name buffer)
5659                (not gnus-single-article-buffer))
5660       (save-excursion
5661         (set-buffer buffer)
5662         (gnus-kill-buffer gnus-article-buffer)
5663         (gnus-kill-buffer gnus-original-article-buffer)))
5664     (cond (gnus-kill-summary-on-exit
5665            (when (and gnus-use-trees
5666                       (gnus-buffer-exists-p buffer))
5667              (save-excursion
5668                (set-buffer buffer)
5669                (gnus-tree-close gnus-newsgroup-name)))
5670            (gnus-kill-buffer buffer))
5671           ((gnus-buffer-exists-p buffer)
5672            (save-excursion
5673              (set-buffer buffer)
5674              (gnus-deaden-summary))))))
5675
5676 (defun gnus-summary-wake-up-the-dead (&rest args)
5677   "Wake up the dead summary buffer."
5678   (interactive)
5679   (gnus-dead-summary-mode -1)
5680   (let ((name (buffer-name)))
5681     (when (string-match "Dead " name)
5682       (rename-buffer
5683        (concat (substring name 0 (match-beginning 0))
5684                (substring name (match-end 0)))
5685        t)))
5686   (gnus-message 3 "This dead summary is now alive again"))
5687
5688 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5689 (defun gnus-summary-fetch-faq (&optional faq-dir)
5690   "Fetch the FAQ for the current group.
5691 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5692 in."
5693   (interactive
5694    (list
5695     (when current-prefix-arg
5696       (completing-read
5697        "Faq dir: " (and (listp gnus-group-faq-directory)
5698                         (mapcar (lambda (file) (list file))
5699                                 gnus-group-faq-directory))))))
5700   (let (gnus-faq-buffer)
5701     (when (setq gnus-faq-buffer
5702                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5703       (gnus-configure-windows 'summary-faq))))
5704
5705 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5706 (defun gnus-summary-describe-group (&optional force)
5707   "Describe the current newsgroup."
5708   (interactive "P")
5709   (gnus-group-describe-group force gnus-newsgroup-name))
5710
5711 (defun gnus-summary-describe-briefly ()
5712   "Describe summary mode commands briefly."
5713   (interactive)
5714   (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")))
5715
5716 ;; Walking around group mode buffer from summary mode.
5717
5718 (defun gnus-summary-next-group (&optional no-article target-group backward)
5719   "Exit current newsgroup and then select next unread newsgroup.
5720 If prefix argument NO-ARTICLE is non-nil, no article is selected
5721 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5722 previous group instead."
5723   (interactive "P")
5724   ;; Stop pre-fetching.
5725   (gnus-async-halt-prefetch)
5726   (let ((current-group gnus-newsgroup-name)
5727         (current-buffer (current-buffer))
5728         entered)
5729     ;; First we semi-exit this group to update Xrefs and all variables.
5730     ;; We can't do a real exit, because the window conf must remain
5731     ;; the same in case the user is prompted for info, and we don't
5732     ;; want the window conf to change before that...
5733     (gnus-summary-exit t)
5734     (while (not entered)
5735       ;; Then we find what group we are supposed to enter.
5736       (set-buffer gnus-group-buffer)
5737       (gnus-group-jump-to-group current-group)
5738       (setq target-group
5739             (or target-group
5740                 (if (eq gnus-keep-same-level 'best)
5741                     (gnus-summary-best-group gnus-newsgroup-name)
5742                   (gnus-summary-search-group backward gnus-keep-same-level))))
5743       (if (not target-group)
5744           ;; There are no further groups, so we return to the group
5745           ;; buffer.
5746           (progn
5747             (gnus-message 5 "Returning to the group buffer")
5748             (setq entered t)
5749             (when (gnus-buffer-live-p current-buffer)
5750               (set-buffer current-buffer)
5751               (gnus-summary-exit))
5752             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5753         ;; We try to enter the target group.
5754         (gnus-group-jump-to-group target-group)
5755         (let ((unreads (gnus-group-group-unread)))
5756           (if (and (or (eq t unreads)
5757                        (and unreads (not (zerop unreads))))
5758                    (gnus-summary-read-group
5759                     target-group nil no-article
5760                     (and (buffer-name current-buffer) current-buffer)
5761                     nil backward))
5762               (setq entered t)
5763             (setq current-group target-group
5764                   target-group nil)))))))
5765
5766 (defun gnus-summary-prev-group (&optional no-article)
5767   "Exit current newsgroup and then select previous unread newsgroup.
5768 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5769   (interactive "P")
5770   (gnus-summary-next-group no-article nil t))
5771
5772 ;; Walking around summary lines.
5773
5774 (defun gnus-summary-first-subject (&optional unread undownloaded)
5775   "Go to the first unread subject.
5776 If UNREAD is non-nil, go to the first unread article.
5777 Returns the article selected or nil if there are no unread articles."
5778   (interactive "P")
5779   (prog1
5780       (cond
5781        ;; Empty summary.
5782        ((null gnus-newsgroup-data)
5783         (gnus-message 3 "No articles in the group")
5784         nil)
5785        ;; Pick the first article.
5786        ((not unread)
5787         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5788         (gnus-data-number (car gnus-newsgroup-data)))
5789        ;; No unread articles.
5790        ((null gnus-newsgroup-unreads)
5791         (gnus-message 3 "No more unread articles")
5792         nil)
5793        ;; Find the first unread article.
5794        (t
5795         (let ((data gnus-newsgroup-data))
5796           (while (and data
5797                       (and (not (and undownloaded
5798                                      (eq gnus-undownloaded-mark
5799                                          (gnus-data-mark (car data)))))
5800                            (not (gnus-data-unread-p (car data)))))
5801             (setq data (cdr data)))
5802           (when data
5803             (goto-char (gnus-data-pos (car data)))
5804             (gnus-data-number (car data))))))
5805     (gnus-summary-position-point)))
5806
5807 (defun gnus-summary-next-subject (n &optional unread dont-display)
5808   "Go to next N'th summary line.
5809 If N is negative, go to the previous N'th subject line.
5810 If UNREAD is non-nil, only unread articles are selected.
5811 The difference between N and the actual number of steps taken is
5812 returned."
5813   (interactive "p")
5814   (let ((backward (< n 0))
5815         (n (abs n)))
5816     (while (and (> n 0)
5817                 (if backward
5818                     (gnus-summary-find-prev unread)
5819                   (gnus-summary-find-next unread)))
5820       (unless (zerop (setq n (1- n)))
5821         (gnus-summary-show-thread)))
5822     (when (/= 0 n)
5823       (gnus-message 7 "No more%s articles"
5824                     (if unread " unread" "")))
5825     (unless dont-display
5826       (gnus-summary-recenter)
5827       (gnus-summary-position-point))
5828     n))
5829
5830 (defun gnus-summary-next-unread-subject (n)
5831   "Go to next N'th unread summary line."
5832   (interactive "p")
5833   (gnus-summary-next-subject n t))
5834
5835 (defun gnus-summary-prev-subject (n &optional unread)
5836   "Go to previous N'th summary line.
5837 If optional argument UNREAD is non-nil, only unread article is selected."
5838   (interactive "p")
5839   (gnus-summary-next-subject (- n) unread))
5840
5841 (defun gnus-summary-prev-unread-subject (n)
5842   "Go to previous N'th unread summary line."
5843   (interactive "p")
5844   (gnus-summary-next-subject (- n) t))
5845
5846 (defun gnus-summary-goto-subject (article &optional force silent)
5847   "Go the subject line of ARTICLE.
5848 If FORCE, also allow jumping to articles not currently shown."
5849   (interactive "nArticle number: ")
5850   (let ((b (point))
5851         (data (gnus-data-find article)))
5852     ;; We read in the article if we have to.
5853     (and (not data)
5854          force
5855          (gnus-summary-insert-subject
5856           article
5857           (if (or (numberp force) (vectorp force)) force)
5858           t)
5859          (setq data (gnus-data-find article)))
5860     (goto-char b)
5861     (if (not data)
5862         (progn
5863           (unless silent
5864             (gnus-message 3 "Can't find article %d" article))
5865           nil)
5866       (goto-char (gnus-data-pos data))
5867       (gnus-summary-position-point)
5868       article)))
5869
5870 ;; Walking around summary lines with displaying articles.
5871
5872 (defun gnus-summary-expand-window (&optional arg)
5873   "Make the summary buffer take up the entire Emacs frame.
5874 Given a prefix, will force an `article' buffer configuration."
5875   (interactive "P")
5876   (if arg
5877       (gnus-configure-windows 'article 'force)
5878     (gnus-configure-windows 'summary 'force)))
5879
5880 (defun gnus-summary-display-article (article &optional all-header)
5881   "Display ARTICLE in article buffer."
5882   (gnus-set-global-variables)
5883   (when (gnus-buffer-live-p gnus-article-buffer)
5884     (with-current-buffer gnus-article-buffer
5885       (setq gnus-article-charset gnus-newsgroup-charset)
5886       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5887       (mm-enable-multibyte-mule4)))
5888   (if (null article)
5889       nil
5890     (prog1
5891         (if gnus-summary-display-article-function
5892             (funcall gnus-summary-display-article-function article all-header)
5893           (gnus-article-prepare article all-header))
5894       (gnus-run-hooks 'gnus-select-article-hook)
5895       (when (and gnus-current-article
5896                  (not (zerop gnus-current-article)))
5897         (gnus-summary-goto-subject gnus-current-article))
5898       (gnus-summary-recenter)
5899       (when (and gnus-use-trees gnus-show-threads)
5900         (gnus-possibly-generate-tree article)
5901         (gnus-highlight-selected-tree article))
5902       ;; Successfully display article.
5903       (gnus-article-set-window-start
5904        (cdr (assq article gnus-newsgroup-bookmarks))))))
5905
5906 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5907   "Select the current article.
5908 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5909 non-nil, the article will be re-fetched even if it already present in
5910 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5911 be displayed."
5912   ;; Make sure we are in the summary buffer to work around bbdb bug.
5913   (unless (eq major-mode 'gnus-summary-mode)
5914     (set-buffer gnus-summary-buffer))
5915   (let ((article (or article (gnus-summary-article-number)))
5916         (all-headers (not (not all-headers))) ;Must be T or NIL.
5917         gnus-summary-display-article-function)
5918     (and (not pseudo)
5919          (gnus-summary-article-pseudo-p article)
5920          (error "This is a pseudo-article"))
5921     (save-excursion
5922       (set-buffer gnus-summary-buffer)
5923       (if (or (and gnus-single-article-buffer
5924                    (or (null gnus-current-article)
5925                        (null gnus-article-current)
5926                        (null (get-buffer gnus-article-buffer))
5927                        (not (eq article (cdr gnus-article-current)))
5928                        (not (equal (car gnus-article-current)
5929                                    gnus-newsgroup-name))))
5930               (and (not gnus-single-article-buffer)
5931                    (or (null gnus-current-article)
5932                        (not (eq gnus-current-article article))))
5933               force)
5934           ;; The requested article is different from the current article.
5935           (progn
5936             (gnus-summary-display-article article all-headers)
5937             (when (gnus-buffer-live-p gnus-article-buffer)
5938                (with-current-buffer gnus-article-buffer
5939                 (if (not gnus-article-decoded-p) ;; a local variable
5940                     (mm-disable-multibyte-mule4))))
5941             (when (or all-headers gnus-show-all-headers)
5942               (gnus-article-show-all-headers))
5943             (gnus-article-set-window-start
5944              (cdr (assq article gnus-newsgroup-bookmarks)))
5945             article)
5946         (when (or all-headers gnus-show-all-headers)
5947           (gnus-article-show-all-headers))
5948         'old))))
5949
5950 (defun gnus-summary-force-verify-and-decrypt ()
5951   (interactive)
5952   (let ((mm-verify-option 'known)
5953         (mm-decrypt-option 'known))
5954     (gnus-summary-select-article nil 'force)))
5955
5956 (defun gnus-summary-set-current-mark (&optional current-mark)
5957   "Obsolete function."
5958   nil)
5959
5960 (defun gnus-summary-next-article (&optional unread subject backward push)
5961   "Select the next article.
5962 If UNREAD, only unread articles are selected.
5963 If SUBJECT, only articles with SUBJECT are selected.
5964 If BACKWARD, the previous article is selected instead of the next."
5965   (interactive "P")
5966   (cond
5967    ;; Is there such an article?
5968    ((and (gnus-summary-search-forward unread subject backward)
5969          (or (gnus-summary-display-article (gnus-summary-article-number))
5970              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5971     (gnus-summary-position-point))
5972    ;; If not, we try the first unread, if that is wanted.
5973    ((and subject
5974          gnus-auto-select-same
5975          (gnus-summary-first-unread-article))
5976     (gnus-summary-position-point)
5977     (gnus-message 6 "Wrapped"))
5978    ;; Try to get next/previous article not displayed in this group.
5979    ((and gnus-auto-extend-newsgroup
5980          (not unread) (not subject))
5981     (gnus-summary-goto-article
5982      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5983      nil (count-lines (point-min) (point))))
5984    ;; Go to next/previous group.
5985    (t
5986     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5987       (gnus-summary-jump-to-group gnus-newsgroup-name))
5988     (let ((cmd last-command-char)
5989           (point
5990            (save-excursion
5991              (set-buffer gnus-group-buffer)
5992              (point)))
5993           (group
5994            (if (eq gnus-keep-same-level 'best)
5995                (gnus-summary-best-group gnus-newsgroup-name)
5996              (gnus-summary-search-group backward gnus-keep-same-level))))
5997       ;; For some reason, the group window gets selected.  We change
5998       ;; it back.
5999       (select-window (get-buffer-window (current-buffer)))
6000       ;; Select next unread newsgroup automagically.
6001       (cond
6002        ((or (not gnus-auto-select-next)
6003             (not cmd))
6004         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6005        ((or (eq gnus-auto-select-next 'quietly)
6006             (and (eq gnus-auto-select-next 'slightly-quietly)
6007                  push)
6008             (and (eq gnus-auto-select-next 'almost-quietly)
6009                  (gnus-summary-last-article-p)))
6010         ;; Select quietly.
6011         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6012             (gnus-summary-exit)
6013           (gnus-message 7 "No more%s articles (%s)..."
6014                         (if unread " unread" "")
6015                         (if group (concat "selecting " group)
6016                           "exiting"))
6017           (gnus-summary-next-group nil group backward)))
6018        (t
6019         (when (gnus-key-press-event-p last-input-event)
6020           (gnus-summary-walk-group-buffer
6021            gnus-newsgroup-name cmd unread backward point))))))))
6022
6023 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6024   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6025                       (?\C-p (gnus-group-prev-unread-group 1))))
6026         (cursor-in-echo-area t)
6027         keve key group ended)
6028     (save-excursion
6029       (set-buffer gnus-group-buffer)
6030       (goto-char start)
6031       (setq group
6032             (if (eq gnus-keep-same-level 'best)
6033                 (gnus-summary-best-group gnus-newsgroup-name)
6034               (gnus-summary-search-group backward gnus-keep-same-level))))
6035     (while (not ended)
6036       (gnus-message
6037        5 "No more%s articles%s" (if unread " unread" "")
6038        (if (and group
6039                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6040            (format " (Type %s for %s [%s])"
6041                    (single-key-description cmd) group
6042                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6043          (format " (Type %s to exit %s)"
6044                  (single-key-description cmd)
6045                  gnus-newsgroup-name)))
6046       ;; Confirm auto selection.
6047       (setq key (car (setq keve (gnus-read-event-char))))
6048       (setq ended t)
6049       (cond
6050        ((assq key keystrokes)
6051         (let ((obuf (current-buffer)))
6052           (switch-to-buffer gnus-group-buffer)
6053           (when group
6054             (gnus-group-jump-to-group group))
6055           (eval (cadr (assq key keystrokes)))
6056           (setq group (gnus-group-group-name))
6057           (switch-to-buffer obuf))
6058         (setq ended nil))
6059        ((equal key cmd)
6060         (if (or (not group)
6061                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6062             (gnus-summary-exit)
6063           (gnus-summary-next-group nil group backward)))
6064        (t
6065         (push (cdr keve) unread-command-events))))))
6066
6067 (defun gnus-summary-next-unread-article ()
6068   "Select unread article after current one."
6069   (interactive)
6070   (gnus-summary-next-article
6071    (or (not (eq gnus-summary-goto-unread 'never))
6072        (gnus-summary-last-article-p (gnus-summary-article-number)))
6073    (and gnus-auto-select-same
6074         (gnus-summary-article-subject))))
6075
6076 (defun gnus-summary-prev-article (&optional unread subject)
6077   "Select the article after the current one.
6078 If UNREAD is non-nil, only unread articles are selected."
6079   (interactive "P")
6080   (gnus-summary-next-article unread subject t))
6081
6082 (defun gnus-summary-prev-unread-article ()
6083   "Select unread article before current one."
6084   (interactive)
6085   (gnus-summary-prev-article
6086    (or (not (eq gnus-summary-goto-unread 'never))
6087        (gnus-summary-first-article-p (gnus-summary-article-number)))
6088    (and gnus-auto-select-same
6089         (gnus-summary-article-subject))))
6090
6091 (defun gnus-summary-next-page (&optional lines circular)
6092   "Show next page of the selected article.
6093 If at the end of the current article, select the next article.
6094 LINES says how many lines should be scrolled up.
6095
6096 If CIRCULAR is non-nil, go to the start of the article instead of
6097 selecting the next article when reaching the end of the current
6098 article."
6099   (interactive "P")
6100   (setq gnus-summary-buffer (current-buffer))
6101   (gnus-set-global-variables)
6102   (let ((article (gnus-summary-article-number))
6103         (article-window (get-buffer-window gnus-article-buffer t))
6104         endp)
6105     ;; If the buffer is empty, we have no article.
6106     (unless article
6107       (error "No article to select"))
6108     (gnus-configure-windows 'article)
6109     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6110         (if (and (eq gnus-summary-goto-unread 'never)
6111                  (not (gnus-summary-last-article-p article)))
6112             (gnus-summary-next-article)
6113           (gnus-summary-next-unread-article))
6114       (if (or (null gnus-current-article)
6115               (null gnus-article-current)
6116               (/= article (cdr gnus-article-current))
6117               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6118           ;; Selected subject is different from current article's.
6119           (gnus-summary-display-article article)
6120         (when article-window
6121           (gnus-eval-in-buffer-window gnus-article-buffer
6122             (setq endp (gnus-article-next-page lines)))
6123           (when endp
6124             (cond (circular
6125                    (gnus-summary-beginning-of-article))
6126                   (lines
6127                    (gnus-message 3 "End of message"))
6128                   ((null lines)
6129                    (if (and (eq gnus-summary-goto-unread 'never)
6130                             (not (gnus-summary-last-article-p article)))
6131                        (gnus-summary-next-article)
6132                      (gnus-summary-next-unread-article))))))))
6133     (gnus-summary-recenter)
6134     (gnus-summary-position-point)))
6135
6136 (defun gnus-summary-prev-page (&optional lines move)
6137   "Show previous page of selected article.
6138 Argument LINES specifies lines to be scrolled down.
6139 If MOVE, move to the previous unread article if point is at
6140 the beginning of the buffer."
6141   (interactive "P")
6142   (let ((article (gnus-summary-article-number))
6143         (article-window (get-buffer-window gnus-article-buffer t))
6144         endp)
6145     (gnus-configure-windows 'article)
6146     (if (or (null gnus-current-article)
6147             (null gnus-article-current)
6148             (/= article (cdr gnus-article-current))
6149             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6150         ;; Selected subject is different from current article's.
6151         (gnus-summary-display-article article)
6152       (gnus-summary-recenter)
6153       (when article-window
6154         (gnus-eval-in-buffer-window gnus-article-buffer
6155           (setq endp (gnus-article-prev-page lines)))
6156         (when (and move endp)
6157           (cond (lines
6158                  (gnus-message 3 "Beginning of message"))
6159                 ((null lines)
6160                  (if (and (eq gnus-summary-goto-unread 'never)
6161                           (not (gnus-summary-first-article-p article)))
6162                      (gnus-summary-prev-article)
6163                    (gnus-summary-prev-unread-article))))))))
6164   (gnus-summary-position-point))
6165
6166 (defun gnus-summary-prev-page-or-article (&optional lines)
6167   "Show previous page of selected article.
6168 Argument LINES specifies lines to be scrolled down.
6169 If at the beginning of the article, go to the next article."
6170   (interactive "P")
6171   (gnus-summary-prev-page lines t))
6172
6173 (defun gnus-summary-scroll-up (lines)
6174   "Scroll up (or down) one line current article.
6175 Argument LINES specifies lines to be scrolled up (or down if negative)."
6176   (interactive "p")
6177   (gnus-configure-windows 'article)
6178   (gnus-summary-show-thread)
6179   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6180     (gnus-eval-in-buffer-window gnus-article-buffer
6181       (cond ((> lines 0)
6182              (when (gnus-article-next-page lines)
6183                (gnus-message 3 "End of message")))
6184             ((< lines 0)
6185              (gnus-article-prev-page (- lines))))))
6186   (gnus-summary-recenter)
6187   (gnus-summary-position-point))
6188
6189 (defun gnus-summary-scroll-down (lines)
6190   "Scroll down (or up) one line current article.
6191 Argument LINES specifies lines to be scrolled down (or up if negative)."
6192   (interactive "p")
6193   (gnus-summary-scroll-up (- lines)))
6194
6195 (defun gnus-summary-next-same-subject ()
6196   "Select next article which has the same subject as current one."
6197   (interactive)
6198   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6199
6200 (defun gnus-summary-prev-same-subject ()
6201   "Select previous article which has the same subject as current one."
6202   (interactive)
6203   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6204
6205 (defun gnus-summary-next-unread-same-subject ()
6206   "Select next unread article which has the same subject as current one."
6207   (interactive)
6208   (gnus-summary-next-article t (gnus-summary-article-subject)))
6209
6210 (defun gnus-summary-prev-unread-same-subject ()
6211   "Select previous unread article which has the same subject as current one."
6212   (interactive)
6213   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6214
6215 (defun gnus-summary-first-unread-article ()
6216   "Select the first unread article.
6217 Return nil if there are no unread articles."
6218   (interactive)
6219   (prog1
6220       (when (gnus-summary-first-subject t)
6221         (gnus-summary-show-thread)
6222         (gnus-summary-first-subject t)
6223         (gnus-summary-display-article (gnus-summary-article-number)))
6224     (gnus-summary-position-point)))
6225
6226 (defun gnus-summary-first-unread-subject ()
6227   "Place the point on the subject line of the first unread article.
6228 Return nil if there are no unread articles."
6229   (interactive)
6230   (prog1
6231       (when (gnus-summary-first-subject t)
6232         (gnus-summary-show-thread)
6233         (gnus-summary-first-subject t))
6234     (gnus-summary-position-point)))
6235
6236 (defun gnus-summary-first-article ()
6237   "Select the first article.
6238 Return nil if there are no articles."
6239   (interactive)
6240   (prog1
6241       (when (gnus-summary-first-subject)
6242         (gnus-summary-show-thread)
6243         (gnus-summary-first-subject)
6244         (gnus-summary-display-article (gnus-summary-article-number)))
6245     (gnus-summary-position-point)))
6246
6247 (defun gnus-summary-best-unread-article ()
6248   "Select the unread article with the highest score."
6249   (interactive)
6250   (let ((best -1000000)
6251         (data gnus-newsgroup-data)
6252         article score)
6253     (while data
6254       (and (gnus-data-unread-p (car data))
6255            (> (setq score
6256                     (gnus-summary-article-score (gnus-data-number (car data))))
6257               best)
6258            (setq best score
6259                  article (gnus-data-number (car data))))
6260       (setq data (cdr data)))
6261     (prog1
6262         (if article
6263             (gnus-summary-goto-article article)
6264           (error "No unread articles"))
6265       (gnus-summary-position-point))))
6266
6267 (defun gnus-summary-last-subject ()
6268   "Go to the last displayed subject line in the group."
6269   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6270     (when article
6271       (gnus-summary-goto-subject article))))
6272
6273 (defun gnus-summary-goto-article (article &optional all-headers force)
6274   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6275 If ALL-HEADERS is non-nil, no header lines are hidden.
6276 If FORCE, go to the article even if it isn't displayed.  If FORCE
6277 is a number, it is the line the article is to be displayed on."
6278   (interactive
6279    (list
6280     (completing-read
6281      "Article number or Message-ID: "
6282      (mapcar (lambda (number) (list (int-to-string number)))
6283              gnus-newsgroup-limit))
6284     current-prefix-arg
6285     t))
6286   (prog1
6287       (if (and (stringp article)
6288                (string-match "@" article))
6289           (gnus-summary-refer-article article)
6290         (when (stringp article)
6291           (setq article (string-to-number article)))
6292         (if (gnus-summary-goto-subject article force)
6293             (gnus-summary-display-article article all-headers)
6294           (gnus-message 4 "Couldn't go to article %s" article) nil))
6295     (gnus-summary-position-point)))
6296
6297 (defun gnus-summary-goto-last-article ()
6298   "Go to the previously read article."
6299   (interactive)
6300   (prog1
6301       (when gnus-last-article
6302         (gnus-summary-goto-article gnus-last-article nil t))
6303     (gnus-summary-position-point)))
6304
6305 (defun gnus-summary-pop-article (number)
6306   "Pop one article off the history and go to the previous.
6307 NUMBER articles will be popped off."
6308   (interactive "p")
6309   (let (to)
6310     (setq gnus-newsgroup-history
6311           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6312     (if to
6313         (gnus-summary-goto-article (car to) nil t)
6314       (error "Article history empty")))
6315   (gnus-summary-position-point))
6316
6317 ;; Summary commands and functions for limiting the summary buffer.
6318
6319 (defun gnus-summary-limit-to-articles (n)
6320   "Limit the summary buffer to the next N articles.
6321 If not given a prefix, use the process marked articles instead."
6322   (interactive "P")
6323   (prog1
6324       (let ((articles (gnus-summary-work-articles n)))
6325         (setq gnus-newsgroup-processable nil)
6326         (gnus-summary-limit articles))
6327     (gnus-summary-position-point)))
6328
6329 (defun gnus-summary-pop-limit (&optional total)
6330   "Restore the previous limit.
6331 If given a prefix, remove all limits."
6332   (interactive "P")
6333   (when total
6334     (setq gnus-newsgroup-limits
6335           (list (mapcar (lambda (h) (mail-header-number h))
6336                         gnus-newsgroup-headers))))
6337   (unless gnus-newsgroup-limits
6338     (error "No limit to pop"))
6339   (prog1
6340       (gnus-summary-limit nil 'pop)
6341     (gnus-summary-position-point)))
6342
6343 (defun gnus-summary-limit-to-subject (subject &optional header)
6344   "Limit the summary buffer to articles that have subjects that match a regexp."
6345   (interactive "sLimit to subject (regexp): ")
6346   (unless header
6347     (setq header "subject"))
6348   (when (not (equal "" subject))
6349     (prog1
6350         (let ((articles (gnus-summary-find-matching
6351                          (or header "subject") subject 'all)))
6352           (unless articles
6353             (error "Found no matches for \"%s\"" subject))
6354           (gnus-summary-limit articles))
6355       (gnus-summary-position-point))))
6356
6357 (defun gnus-summary-limit-to-author (from)
6358   "Limit the summary buffer to articles that have authors that match a regexp."
6359   (interactive "sLimit to author (regexp): ")
6360   (gnus-summary-limit-to-subject from "from"))
6361
6362 (defun gnus-summary-limit-to-age (age &optional younger-p)
6363   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6364 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6365 articles that are younger than AGE days."
6366   (interactive
6367    (let ((younger current-prefix-arg)
6368          (days-got nil)
6369          days)
6370      (while (not days-got)
6371        (setq days (if younger
6372                       (read-string "Limit to articles within (in days): ")
6373                     (read-string "Limit to articles old than (in days): ")))
6374        (when (> (length days) 0)
6375          (setq days (read days)))
6376        (if (numberp days)
6377            (setq days-got t)
6378          (message "Please enter a number.")
6379          (sleep-for 1)))
6380      (list days younger)))
6381   (prog1
6382       (let ((data gnus-newsgroup-data)
6383             (cutoff (days-to-time age))
6384             articles d date is-younger)
6385         (while (setq d (pop data))
6386           (when (and (vectorp (gnus-data-header d))
6387                      (setq date (mail-header-date (gnus-data-header d))))
6388             (setq is-younger (time-less-p
6389                               (time-since (condition-case ()
6390                                               (date-to-time date)
6391                                             (error '(0 0))))
6392                               cutoff))
6393             (when (if younger-p
6394                       is-younger
6395                     (not is-younger))
6396               (push (gnus-data-number d) articles))))
6397         (gnus-summary-limit (nreverse articles)))
6398     (gnus-summary-position-point)))
6399
6400 (defun gnus-summary-limit-to-extra (header regexp)
6401   "Limit the summary buffer to articles that match an 'extra' header."
6402   (interactive
6403    (let ((header
6404           (intern
6405            (gnus-completing-read
6406             (symbol-name (car gnus-extra-headers))
6407             "Limit extra header:"
6408             (mapcar (lambda (x)
6409                       (cons (symbol-name x) x))
6410                     gnus-extra-headers)
6411             nil
6412             t))))
6413      (list header
6414            (read-string (format "Limit to header %s (regexp): " header)))))
6415   (when (not (equal "" regexp))
6416     (prog1
6417         (let ((articles (gnus-summary-find-matching
6418                          (cons 'extra header) regexp 'all)))
6419           (unless articles
6420             (error "Found no matches for \"%s\"" regexp))
6421           (gnus-summary-limit articles))
6422       (gnus-summary-position-point))))
6423
6424 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6425 (make-obsolete
6426  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6427
6428 (defun gnus-summary-limit-to-unread (&optional all)
6429   "Limit the summary buffer to articles that are not marked as read.
6430 If ALL is non-nil, limit strictly to unread articles."
6431   (interactive "P")
6432   (if all
6433       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6434     (gnus-summary-limit-to-marks
6435      ;; Concat all the marks that say that an article is read and have
6436      ;; those removed.
6437      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6438            gnus-killed-mark gnus-kill-file-mark
6439            gnus-low-score-mark gnus-expirable-mark
6440            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6441            gnus-duplicate-mark gnus-souped-mark)
6442      'reverse)))
6443
6444 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6445 (make-obsolete 'gnus-summary-delete-marked-with
6446                'gnus-summary-limit-exlude-marks)
6447
6448 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6449   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6450 If REVERSE, limit the summary buffer to articles that are marked
6451 with MARKS.  MARKS can either be a string of marks or a list of marks.
6452 Returns how many articles were removed."
6453   (interactive "sMarks: ")
6454   (gnus-summary-limit-to-marks marks t))
6455
6456 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6457   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6458 If REVERSE (the prefix), limit the summary buffer to articles that are
6459 not marked with MARKS.  MARKS can either be a string of marks or a
6460 list of marks.
6461 Returns how many articles were removed."
6462   (interactive "sMarks: \nP")
6463   (prog1
6464       (let ((data gnus-newsgroup-data)
6465             (marks (if (listp marks) marks
6466                      (append marks nil))) ; Transform to list.
6467             articles)
6468         (while data
6469           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6470                   (memq (gnus-data-mark (car data)) marks))
6471             (push (gnus-data-number (car data)) articles))
6472           (setq data (cdr data)))
6473         (gnus-summary-limit articles))
6474     (gnus-summary-position-point)))
6475
6476 (defun gnus-summary-limit-to-score (&optional score)
6477   "Limit to articles with score at or above SCORE."
6478   (interactive "P")
6479   (setq score (if score
6480                   (prefix-numeric-value score)
6481                 (or gnus-summary-default-score 0)))
6482   (let ((data gnus-newsgroup-data)
6483         articles)
6484     (while data
6485       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6486                 score)
6487         (push (gnus-data-number (car data)) articles))
6488       (setq data (cdr data)))
6489     (prog1
6490         (gnus-summary-limit articles)
6491       (gnus-summary-position-point))))
6492
6493 (defun gnus-summary-limit-include-thread (id)
6494   "Display all the hidden articles that in the current thread."
6495   (interactive (list (mail-header-id (gnus-summary-article-header))))
6496   (let ((articles (gnus-articles-in-thread
6497                    (gnus-id-to-thread (gnus-root-id id)))))
6498     (prog1
6499         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6500       (gnus-summary-position-point))))
6501
6502 (defun gnus-summary-limit-include-dormant ()
6503   "Display all the hidden articles that are marked as dormant.
6504 Note that this command only works on a subset of the articles currently
6505 fetched for this group."
6506   (interactive)
6507   (unless gnus-newsgroup-dormant
6508     (error "There are no dormant articles in this group"))
6509   (prog1
6510       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6511     (gnus-summary-position-point)))
6512
6513 (defun gnus-summary-limit-exclude-dormant ()
6514   "Hide all dormant articles."
6515   (interactive)
6516   (prog1
6517       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6518     (gnus-summary-position-point)))
6519
6520 (defun gnus-summary-limit-exclude-childless-dormant ()
6521   "Hide all dormant articles that have no children."
6522   (interactive)
6523   (let ((data (gnus-data-list t))
6524         articles d children)
6525     ;; Find all articles that are either not dormant or have
6526     ;; children.
6527     (while (setq d (pop data))
6528       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6529                 (and (setq children
6530                            (gnus-article-children (gnus-data-number d)))
6531                      (let (found)
6532                        (while children
6533                          (when (memq (car children) articles)
6534                            (setq children nil
6535                                  found t))
6536                          (pop children))
6537                        found)))
6538         (push (gnus-data-number d) articles)))
6539     ;; Do the limiting.
6540     (prog1
6541         (gnus-summary-limit articles)
6542       (gnus-summary-position-point))))
6543
6544 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6545   "Mark all unread excluded articles as read.
6546 If ALL, mark even excluded ticked and dormants as read."
6547   (interactive "P")
6548   (let ((articles (gnus-sorted-complement
6549                    (sort
6550                     (mapcar (lambda (h) (mail-header-number h))
6551                             gnus-newsgroup-headers)
6552                     '<)
6553                    (sort gnus-newsgroup-limit '<)))
6554         article)
6555     (setq gnus-newsgroup-unreads
6556           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6557     (if all
6558         (setq gnus-newsgroup-dormant nil
6559               gnus-newsgroup-marked nil
6560               gnus-newsgroup-reads
6561               (nconc
6562                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6563                gnus-newsgroup-reads))
6564       (while (setq article (pop articles))
6565         (unless (or (memq article gnus-newsgroup-dormant)
6566                     (memq article gnus-newsgroup-marked))
6567           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6568
6569 (defun gnus-summary-limit (articles &optional pop)
6570   (if pop
6571       ;; We pop the previous limit off the stack and use that.
6572       (setq articles (car gnus-newsgroup-limits)
6573             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6574     ;; We use the new limit, so we push the old limit on the stack.
6575     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6576   ;; Set the limit.
6577   (setq gnus-newsgroup-limit articles)
6578   (let ((total (length gnus-newsgroup-data))
6579         (data (gnus-data-find-list (gnus-summary-article-number)))
6580         (gnus-summary-mark-below nil)   ; Inhibit this.
6581         found)
6582     ;; This will do all the work of generating the new summary buffer
6583     ;; according to the new limit.
6584     (gnus-summary-prepare)
6585     ;; Hide any threads, possibly.
6586     (and gnus-show-threads
6587          gnus-thread-hide-subtree
6588          (gnus-summary-hide-all-threads))
6589     ;; Try to return to the article you were at, or one in the
6590     ;; neighborhood.
6591     (when data
6592       ;; We try to find some article after the current one.
6593       (while data
6594         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6595           (setq data nil
6596                 found t))
6597         (setq data (cdr data))))
6598     (unless found
6599       ;; If there is no data, that means that we were after the last
6600       ;; article.  The same goes when we can't find any articles
6601       ;; after the current one.
6602       (goto-char (point-max))
6603       (gnus-summary-find-prev))
6604     (gnus-set-mode-line 'summary)
6605     ;; We return how many articles were removed from the summary
6606     ;; buffer as a result of the new limit.
6607     (- total (length gnus-newsgroup-data))))
6608
6609 (defsubst gnus-invisible-cut-children (threads)
6610   (let ((num 0))
6611     (while threads
6612       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6613         (incf num))
6614       (pop threads))
6615     (< num 2)))
6616
6617 (defsubst gnus-cut-thread (thread)
6618   "Go forwards in the thread until we find an article that we want to display."
6619   (when (or (eq gnus-fetch-old-headers 'some)
6620             (eq gnus-fetch-old-headers 'invisible)
6621             (numberp gnus-fetch-old-headers)
6622             (eq gnus-build-sparse-threads 'some)
6623             (eq gnus-build-sparse-threads 'more))
6624     ;; Deal with old-fetched headers and sparse threads.
6625     (while (and
6626             thread
6627             (or
6628              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6629              (gnus-summary-article-ancient-p
6630               (mail-header-number (car thread))))
6631             (if (or (<= (length (cdr thread)) 1)
6632                     (eq gnus-fetch-old-headers 'invisible))
6633                 (setq gnus-newsgroup-limit
6634                       (delq (mail-header-number (car thread))
6635                             gnus-newsgroup-limit)
6636                       thread (cadr thread))
6637               (when (gnus-invisible-cut-children (cdr thread))
6638                 (let ((th (cdr thread)))
6639                   (while th
6640                     (if (memq (mail-header-number (caar th))
6641                               gnus-newsgroup-limit)
6642                         (setq thread (car th)
6643                               th nil)
6644                       (setq th (cdr th))))))))))
6645   thread)
6646
6647 (defun gnus-cut-threads (threads)
6648   "Cut off all uninteresting articles from the beginning of threads."
6649   (when (or (eq gnus-fetch-old-headers 'some)
6650             (eq gnus-fetch-old-headers 'invisible)
6651             (numberp gnus-fetch-old-headers)
6652             (eq gnus-build-sparse-threads 'some)
6653             (eq gnus-build-sparse-threads 'more))
6654     (let ((th threads))
6655       (while th
6656         (setcar th (gnus-cut-thread (car th)))
6657         (setq th (cdr th)))))
6658   ;; Remove nixed out threads.
6659   (delq nil threads))
6660
6661 (defun gnus-summary-initial-limit (&optional show-if-empty)
6662   "Figure out what the initial limit is supposed to be on group entry.
6663 This entails weeding out unwanted dormants, low-scored articles,
6664 fetch-old-headers verbiage, and so on."
6665   ;; Most groups have nothing to remove.
6666   (if (or gnus-inhibit-limiting
6667           (and (null gnus-newsgroup-dormant)
6668                (not (eq gnus-fetch-old-headers 'some))
6669                (not (numberp gnus-fetch-old-headers))
6670                (not (eq gnus-fetch-old-headers 'invisible))
6671                (null gnus-summary-expunge-below)
6672                (not (eq gnus-build-sparse-threads 'some))
6673                (not (eq gnus-build-sparse-threads 'more))
6674                (null gnus-thread-expunge-below)
6675                (not gnus-use-nocem)))
6676       ()                                ; Do nothing.
6677     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6678     (setq gnus-newsgroup-limit nil)
6679     (mapatoms
6680      (lambda (node)
6681        (unless (car (symbol-value node))
6682          ;; These threads have no parents -- they are roots.
6683          (let ((nodes (cdr (symbol-value node)))
6684                thread)
6685            (while nodes
6686              (if (and gnus-thread-expunge-below
6687                       (< (gnus-thread-total-score (car nodes))
6688                          gnus-thread-expunge-below))
6689                  (gnus-expunge-thread (pop nodes))
6690                (setq thread (pop nodes))
6691                (gnus-summary-limit-children thread))))))
6692      gnus-newsgroup-dependencies)
6693     ;; If this limitation resulted in an empty group, we might
6694     ;; pop the previous limit and use it instead.
6695     (when (and (not gnus-newsgroup-limit)
6696                show-if-empty)
6697       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6698     gnus-newsgroup-limit))
6699
6700 (defun gnus-summary-limit-children (thread)
6701   "Return 1 if this subthread is visible and 0 if it is not."
6702   ;; First we get the number of visible children to this thread.  This
6703   ;; is done by recursing down the thread using this function, so this
6704   ;; will really go down to a leaf article first, before slowly
6705   ;; working its way up towards the root.
6706   (when thread
6707     (let ((children
6708            (if (cdr thread)
6709                (apply '+ (mapcar 'gnus-summary-limit-children
6710                                  (cdr thread)))
6711              0))
6712           (number (mail-header-number (car thread)))
6713           score)
6714       (if (and
6715            (not (memq number gnus-newsgroup-marked))
6716            (or
6717             ;; If this article is dormant and has absolutely no visible
6718             ;; children, then this article isn't visible.
6719             (and (memq number gnus-newsgroup-dormant)
6720                  (zerop children))
6721             ;; If this is "fetch-old-headered" and there is no
6722             ;; visible children, then we don't want this article.
6723             (and (or (eq gnus-fetch-old-headers 'some)
6724                      (numberp gnus-fetch-old-headers))
6725                  (gnus-summary-article-ancient-p number)
6726                  (zerop children))
6727             ;; If this is "fetch-old-headered" and `invisible', then
6728             ;; we don't want this article.
6729             (and (eq gnus-fetch-old-headers 'invisible)
6730                  (gnus-summary-article-ancient-p number))
6731             ;; If this is a sparsely inserted article with no children,
6732             ;; we don't want it.
6733             (and (eq gnus-build-sparse-threads 'some)
6734                  (gnus-summary-article-sparse-p number)
6735                  (zerop children))
6736             ;; If we use expunging, and this article is really
6737             ;; low-scored, then we don't want this article.
6738             (when (and gnus-summary-expunge-below
6739                        (< (setq score
6740                                 (or (cdr (assq number gnus-newsgroup-scored))
6741                                     gnus-summary-default-score))
6742                           gnus-summary-expunge-below))
6743               ;; We increase the expunge-tally here, but that has
6744               ;; nothing to do with the limits, really.
6745               (incf gnus-newsgroup-expunged-tally)
6746               ;; We also mark as read here, if that's wanted.
6747               (when (and gnus-summary-mark-below
6748                          (< score gnus-summary-mark-below))
6749                 (setq gnus-newsgroup-unreads
6750                       (delq number gnus-newsgroup-unreads))
6751                 (if gnus-newsgroup-auto-expire
6752                     (push number gnus-newsgroup-expirable)
6753                   (push (cons number gnus-low-score-mark)
6754                         gnus-newsgroup-reads)))
6755               t)
6756             ;; Check NoCeM things.
6757             (if (and gnus-use-nocem
6758                      (gnus-nocem-unwanted-article-p
6759                       (mail-header-id (car thread))))
6760                 (progn
6761                   (setq gnus-newsgroup-unreads
6762                         (delq number gnus-newsgroup-unreads))
6763                   t))))
6764           ;; Nope, invisible article.
6765           0
6766         ;; Ok, this article is to be visible, so we add it to the limit
6767         ;; and return 1.
6768         (push number gnus-newsgroup-limit)
6769         1))))
6770
6771 (defun gnus-expunge-thread (thread)
6772   "Mark all articles in THREAD as read."
6773   (let* ((number (mail-header-number (car thread))))
6774     (incf gnus-newsgroup-expunged-tally)
6775     ;; We also mark as read here, if that's wanted.
6776     (setq gnus-newsgroup-unreads
6777           (delq number gnus-newsgroup-unreads))
6778     (if gnus-newsgroup-auto-expire
6779         (push number gnus-newsgroup-expirable)
6780       (push (cons number gnus-low-score-mark)
6781             gnus-newsgroup-reads)))
6782   ;; Go recursively through all subthreads.
6783   (mapcar 'gnus-expunge-thread (cdr thread)))
6784
6785 ;; Summary article oriented commands
6786
6787 (defun gnus-summary-refer-parent-article (n)
6788   "Refer parent article N times.
6789 If N is negative, go to ancestor -N instead.
6790 The difference between N and the number of articles fetched is returned."
6791   (interactive "p")
6792   (let ((skip 1)
6793         error header ref)
6794     (when (not (natnump n))
6795       (setq skip (abs n)
6796             n 1))
6797     (while (and (> n 0)
6798                 (not error))
6799       (setq header (gnus-summary-article-header))
6800       (if (and (eq (mail-header-number header)
6801                    (cdr gnus-article-current))
6802                (equal gnus-newsgroup-name
6803                       (car gnus-article-current)))
6804           ;; If we try to find the parent of the currently
6805           ;; displayed article, then we take a look at the actual
6806           ;; References header, since this is slightly more
6807           ;; reliable than the References field we got from the
6808           ;; server.
6809           (save-excursion
6810             (set-buffer gnus-original-article-buffer)
6811             (nnheader-narrow-to-headers)
6812             (unless (setq ref (message-fetch-field "references"))
6813               (setq ref (message-fetch-field "in-reply-to")))
6814             (widen))
6815         (setq ref
6816               ;; It's not the current article, so we take a bet on
6817               ;; the value we got from the server.
6818               (mail-header-references header)))
6819       (if (and ref
6820                (not (equal ref "")))
6821           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6822             (gnus-message 1 "Couldn't find parent"))
6823         (gnus-message 1 "No references in article %d"
6824                       (gnus-summary-article-number))
6825         (setq error t))
6826       (decf n))
6827     (gnus-summary-position-point)
6828     n))
6829
6830 (defun gnus-summary-refer-references ()
6831   "Fetch all articles mentioned in the References header.
6832 Return the number of articles fetched."
6833   (interactive)
6834   (let ((ref (mail-header-references (gnus-summary-article-header)))
6835         (current (gnus-summary-article-number))
6836         (n 0))
6837     (if (or (not ref)
6838             (equal ref ""))
6839         (error "No References in the current article")
6840       ;; For each Message-ID in the References header...
6841       (while (string-match "<[^>]*>" ref)
6842         (incf n)
6843         ;; ... fetch that article.
6844         (gnus-summary-refer-article
6845          (prog1 (match-string 0 ref)
6846            (setq ref (substring ref (match-end 0))))))
6847       (gnus-summary-goto-subject current)
6848       (gnus-summary-position-point)
6849       n)))
6850
6851 (defun gnus-summary-refer-thread (&optional limit)
6852   "Fetch all articles in the current thread.
6853 If LIMIT (the numerical prefix), fetch that many old headers instead
6854 of what's specified by the `gnus-refer-thread-limit' variable."
6855   (interactive "P")
6856   (let ((id (mail-header-id (gnus-summary-article-header)))
6857         (limit (if limit (prefix-numeric-value limit)
6858                  gnus-refer-thread-limit)))
6859     ;; We want to fetch LIMIT *old* headers, but we also have to
6860     ;; re-fetch all the headers in the current buffer, because many of
6861     ;; them may be undisplayed.  So we adjust LIMIT.
6862     (when (numberp limit)
6863       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6864     (unless (eq gnus-fetch-old-headers 'invisible)
6865       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6866       ;; Retrieve the headers and read them in.
6867       (if (eq (gnus-retrieve-headers
6868                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6869               'nov)
6870           (gnus-build-all-threads)
6871         (error "Can't fetch thread from backends that don't support NOV"))
6872       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6873     (gnus-summary-limit-include-thread id)))
6874
6875 (defun gnus-summary-refer-article (message-id)
6876   "Fetch an article specified by MESSAGE-ID."
6877   (interactive "sMessage-ID: ")
6878   (when (and (stringp message-id)
6879              (not (zerop (length message-id))))
6880     ;; Construct the correct Message-ID if necessary.
6881     ;; Suggested by tale@pawl.rpi.edu.
6882     (unless (string-match "^<" message-id)
6883       (setq message-id (concat "<" message-id)))
6884     (unless (string-match ">$" message-id)
6885       (setq message-id (concat message-id ">")))
6886     (let* ((header (gnus-id-to-header message-id))
6887            (sparse (and header
6888                         (gnus-summary-article-sparse-p
6889                          (mail-header-number header))
6890                         (memq (mail-header-number header)
6891                               gnus-newsgroup-limit)))
6892            number)
6893       (cond
6894        ;; If the article is present in the buffer we just go to it.
6895        ((and header
6896              (or (not (gnus-summary-article-sparse-p
6897                        (mail-header-number header)))
6898                  sparse))
6899         (prog1
6900             (gnus-summary-goto-article
6901              (mail-header-number header) nil t)
6902           (when sparse
6903             (gnus-summary-update-article (mail-header-number header)))))
6904        (t
6905         ;; We fetch the article.
6906         (catch 'found
6907           (dolist (gnus-override-method (gnus-refer-article-methods))
6908             (gnus-check-server gnus-override-method)
6909             ;; Fetch the header, and display the article.
6910             (when (setq number (gnus-summary-insert-subject message-id))
6911               (gnus-summary-select-article nil nil nil number)
6912               (throw 'found t)))
6913           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6914
6915 (defun gnus-refer-article-methods ()
6916   "Return a list of referrable methods."
6917   (cond
6918    ;; No method, so we default to current and native.
6919    ((null gnus-refer-article-method)
6920     (list gnus-current-select-method gnus-select-method))
6921    ;; Current.
6922    ((eq 'current gnus-refer-article-method)
6923     (list gnus-current-select-method))
6924    ;; List of select methods.
6925    ((not (and (symbolp (car gnus-refer-article-method))
6926               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6927     (let (out)
6928       (dolist (method gnus-refer-article-method)
6929         (push (if (eq 'current method)
6930                   gnus-current-select-method
6931                 method)
6932               out))
6933       (nreverse out)))
6934    ;; One single select method.
6935    (t
6936     (list gnus-refer-article-method))))
6937
6938 (defun gnus-summary-edit-parameters ()
6939   "Edit the group parameters of the current group."
6940   (interactive)
6941   (gnus-group-edit-group gnus-newsgroup-name 'params))
6942
6943 (defun gnus-summary-customize-parameters ()
6944   "Customize the group parameters of the current group."
6945   (interactive)
6946   (gnus-group-customize gnus-newsgroup-name))
6947
6948 (defun gnus-summary-enter-digest-group (&optional force)
6949   "Enter an nndoc group based on the current article.
6950 If FORCE, force a digest interpretation.  If not, try
6951 to guess what the document format is."
6952   (interactive "P")
6953   (let ((conf gnus-current-window-configuration))
6954     (save-excursion
6955       (gnus-summary-select-article))
6956     (setq gnus-current-window-configuration conf)
6957     (let* ((name (format "%s-%d"
6958                          (gnus-group-prefixed-name
6959                           gnus-newsgroup-name (list 'nndoc ""))
6960                          (save-excursion
6961                            (set-buffer gnus-summary-buffer)
6962                            gnus-current-article)))
6963            (ogroup gnus-newsgroup-name)
6964            (params (append (gnus-info-params (gnus-get-info ogroup))
6965                            (list (cons 'to-group ogroup))
6966                            (list (cons 'save-article-group ogroup))))
6967            (case-fold-search t)
6968            (buf (current-buffer))
6969            dig to-address)
6970       (save-excursion
6971         (set-buffer gnus-original-article-buffer)
6972         ;; Have the digest group inherit the main mail address of
6973         ;; the parent article.
6974         (when (setq to-address (or (message-fetch-field "reply-to")
6975                                    (message-fetch-field "from")))
6976           (setq params (append 
6977                         (list (cons 'to-address 
6978                                     (funcall gnus-decode-encoded-word-function
6979                                              to-address))))))
6980         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6981         (insert-buffer-substring gnus-original-article-buffer)
6982         ;; Remove lines that may lead nndoc to misinterpret the
6983         ;; document type.
6984         (narrow-to-region
6985          (goto-char (point-min))
6986          (or (search-forward "\n\n" nil t) (point)))
6987         (goto-char (point-min))
6988         (delete-matching-lines "^Path:\\|^From ")
6989         (widen))
6990       (unwind-protect
6991           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6992                     (gnus-newsgroup-ephemeral-ignored-charsets
6993                      gnus-newsgroup-ignored-charsets))
6994                 (gnus-group-read-ephemeral-group
6995                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6996                               (nndoc-article-type
6997                                ,(if force 'mbox 'guess))) t))
6998               ;; Make all postings to this group go to the parent group.
6999               (nconc (gnus-info-params (gnus-get-info name))
7000                      params)
7001             ;; Couldn't select this doc group.
7002             (switch-to-buffer buf)
7003             (gnus-set-global-variables)
7004             (gnus-configure-windows 'summary)
7005             (gnus-message 3 "Article couldn't be entered?"))
7006         (kill-buffer dig)))))
7007
7008 (defun gnus-summary-read-document (n)
7009   "Open a new group based on the current article(s).
7010 This will allow you to read digests and other similar
7011 documents as newsgroups.
7012 Obeys the standard process/prefix convention."
7013   (interactive "P")
7014   (let* ((articles (gnus-summary-work-articles n))
7015          (ogroup gnus-newsgroup-name)
7016          (params (append (gnus-info-params (gnus-get-info ogroup))
7017                          (list (cons 'to-group ogroup))))
7018          article group egroup groups vgroup)
7019     (while (setq article (pop articles))
7020       (setq group (format "%s-%d" gnus-newsgroup-name article))
7021       (gnus-summary-remove-process-mark article)
7022       (when (gnus-summary-display-article article)
7023         (save-excursion
7024           (with-temp-buffer
7025             (insert-buffer-substring gnus-original-article-buffer)
7026             ;; Remove some headers that may lead nndoc to make
7027             ;; the wrong guess.
7028             (message-narrow-to-head)
7029             (goto-char (point-min))
7030             (delete-matching-lines "^\\(Path\\):\\|^From ")
7031             (widen)
7032             (if (setq egroup
7033                       (gnus-group-read-ephemeral-group
7034                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7035                                      (nndoc-article-type guess))
7036                        t nil t))
7037                 (progn
7038                   ;; Make all postings to this group go to the parent group.
7039                   (nconc (gnus-info-params (gnus-get-info egroup))
7040                          params)
7041                   (push egroup groups))
7042               ;; Couldn't select this doc group.
7043               (gnus-error 3 "Article couldn't be entered"))))))
7044     ;; Now we have selected all the documents.
7045     (cond
7046      ((not groups)
7047       (error "None of the articles could be interpreted as documents"))
7048      ((gnus-group-read-ephemeral-group
7049        (setq vgroup (format
7050                      "nnvirtual:%s-%s" gnus-newsgroup-name
7051                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7052        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7053        t
7054        (cons (current-buffer) 'summary)))
7055      (t
7056       (error "Couldn't select virtual nndoc group")))))
7057
7058 (defun gnus-summary-isearch-article (&optional regexp-p)
7059   "Do incremental search forward on the current article.
7060 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7061   (interactive "P")
7062   (gnus-summary-select-article)
7063   (gnus-configure-windows 'article)
7064   (gnus-eval-in-buffer-window gnus-article-buffer
7065     (save-restriction
7066       (widen)
7067       (isearch-forward regexp-p))))
7068
7069 (defun gnus-summary-search-article-forward (regexp &optional backward)
7070   "Search for an article containing REGEXP forward.
7071 If BACKWARD, search backward instead."
7072   (interactive
7073    (list (read-string
7074           (format "Search article %s (regexp%s): "
7075                   (if current-prefix-arg "backward" "forward")
7076                   (if gnus-last-search-regexp
7077                       (concat ", default " gnus-last-search-regexp)
7078                     "")))
7079          current-prefix-arg))
7080   (if (string-equal regexp "")
7081       (setq regexp (or gnus-last-search-regexp ""))
7082     (setq gnus-last-search-regexp regexp)
7083     (setq gnus-article-before-search gnus-current-article))
7084   ;; Intentionally set gnus-last-article.
7085   (setq gnus-last-article gnus-article-before-search)
7086   (let ((gnus-last-article gnus-last-article))
7087     (if (gnus-summary-search-article regexp backward)
7088         (gnus-summary-show-thread)
7089       (error "Search failed: \"%s\"" regexp))))
7090
7091 (defun gnus-summary-search-article-backward (regexp)
7092   "Search for an article containing REGEXP backward."
7093   (interactive
7094    (list (read-string
7095           (format "Search article backward (regexp%s): "
7096                   (if gnus-last-search-regexp
7097                       (concat ", default " gnus-last-search-regexp)
7098                     "")))))
7099   (gnus-summary-search-article-forward regexp 'backward))
7100
7101 (defun gnus-summary-search-article (regexp &optional backward)
7102   "Search for an article containing REGEXP.
7103 Optional argument BACKWARD means do search for backward.
7104 `gnus-select-article-hook' is not called during the search."
7105   ;; We have to require this here to make sure that the following
7106   ;; dynamic binding isn't shadowed by autoloading.
7107   (require 'gnus-async)
7108   (require 'gnus-art)
7109   (let ((gnus-select-article-hook nil)  ;Disable hook.
7110         (gnus-article-prepare-hook nil)
7111         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7112         (gnus-use-article-prefetch nil)
7113         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7114         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7115         (sum (current-buffer))
7116         (gnus-display-mime-function nil)
7117         (found nil)
7118         point)
7119     (gnus-save-hidden-threads
7120       (gnus-summary-select-article)
7121       (set-buffer gnus-article-buffer)
7122       (goto-char (window-point (get-buffer-window (current-buffer))))
7123       (when backward
7124         (forward-line -1))
7125       (while (not found)
7126         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7127         (if (if backward
7128                 (re-search-backward regexp nil t)
7129               (re-search-forward regexp nil t))
7130             ;; We found the regexp.
7131             (progn
7132               (setq found 'found)
7133               (beginning-of-line)
7134               (set-window-start
7135                (get-buffer-window (current-buffer))
7136                (point))
7137               (forward-line 1)
7138               (set-window-point
7139                (get-buffer-window (current-buffer))
7140                (point))
7141               (set-buffer sum)
7142               (setq point (point)))
7143           ;; We didn't find it, so we go to the next article.
7144           (set-buffer sum)
7145           (setq found 'not)
7146           (while (eq found 'not)
7147             (if (not (if backward (gnus-summary-find-prev)
7148                        (gnus-summary-find-next)))
7149                 ;; No more articles.
7150                 (setq found t)
7151               ;; Select the next article and adjust point.
7152               (unless (gnus-summary-article-sparse-p
7153                        (gnus-summary-article-number))
7154                 (setq found nil)
7155                 (gnus-summary-select-article)
7156                 (set-buffer gnus-article-buffer)
7157                 (widen)
7158                 (goto-char (if backward (point-max) (point-min))))))))
7159       (gnus-message 7 ""))
7160     ;; Return whether we found the regexp.
7161     (when (eq found 'found)
7162       (goto-char point)
7163       (gnus-summary-show-thread)
7164       (gnus-summary-goto-subject gnus-current-article)
7165       (gnus-summary-position-point)
7166       t)))
7167
7168 (defun gnus-summary-find-matching (header regexp &optional backward unread
7169                                           not-case-fold)
7170   "Return a list of all articles that match REGEXP on HEADER.
7171 The search stars on the current article and goes forwards unless
7172 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7173 If UNREAD is non-nil, only unread articles will
7174 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7175 in the comparisons."
7176   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7177                 (gnus-data-find-list
7178                  (gnus-summary-article-number) (gnus-data-list backward))))
7179         (case-fold-search (not not-case-fold))
7180         articles d func)
7181     (if (consp header)
7182         (if (eq (car header) 'extra)
7183             (setq func
7184                   `(lambda (h)
7185                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7186                          "")))
7187           (error "%s is an invalid header" header))
7188       (unless (fboundp (intern (concat "mail-header-" header)))
7189         (error "%s is not a valid header" header))
7190       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7191     (while data
7192       (setq d (car data))
7193       (and (or (not unread)             ; We want all articles...
7194                (gnus-data-unread-p d))  ; Or just unreads.
7195            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7196            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7197            (push (gnus-data-number d) articles)) ; Success!
7198       (setq data (cdr data)))
7199     (nreverse articles)))
7200
7201 (defun gnus-summary-execute-command (header regexp command &optional backward)
7202   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7203 If HEADER is an empty string (or nil), the match is done on the entire
7204 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7205   (interactive
7206    (list (let ((completion-ignore-case t))
7207            (completing-read
7208             "Header name: "
7209             (mapcar (lambda (string) (list string))
7210                     '("Number" "Subject" "From" "Lines" "Date"
7211                       "Message-ID" "Xref" "References" "Body"))
7212             nil 'require-match))
7213          (read-string "Regexp: ")
7214          (read-key-sequence "Command: ")
7215          current-prefix-arg))
7216   (when (equal header "Body")
7217     (setq header ""))
7218   ;; Hidden thread subtrees must be searched as well.
7219   (gnus-summary-show-all-threads)
7220   ;; We don't want to change current point nor window configuration.
7221   (save-excursion
7222     (save-window-excursion
7223       (gnus-message 6 "Executing %s..." (key-description command))
7224       ;; We'd like to execute COMMAND interactively so as to give arguments.
7225       (gnus-execute header regexp
7226                     `(call-interactively ',(key-binding command))
7227                     backward)
7228       (gnus-message 6 "Executing %s...done" (key-description command)))))
7229
7230 (defun gnus-summary-beginning-of-article ()
7231   "Scroll the article back to the beginning."
7232   (interactive)
7233   (gnus-summary-select-article)
7234   (gnus-configure-windows 'article)
7235   (gnus-eval-in-buffer-window gnus-article-buffer
7236     (widen)
7237     (goto-char (point-min))
7238     (when gnus-page-broken
7239       (gnus-narrow-to-page))))
7240
7241 (defun gnus-summary-end-of-article ()
7242   "Scroll to the end of the article."
7243   (interactive)
7244   (gnus-summary-select-article)
7245   (gnus-configure-windows 'article)
7246   (gnus-eval-in-buffer-window gnus-article-buffer
7247     (widen)
7248     (goto-char (point-max))
7249     (recenter -3)
7250     (when gnus-page-broken
7251       (gnus-narrow-to-page))))
7252
7253 (defun gnus-summary-print-article (&optional filename n)
7254   "Generate and print a PostScript image of the N next (mail) articles.
7255
7256 If N is negative, print the N previous articles.  If N is nil and articles
7257 have been marked with the process mark, print these instead.
7258
7259 If the optional first argument FILENAME is nil, send the image to the
7260 printer.  If FILENAME is a string, save the PostScript image in a file with
7261 that name.  If FILENAME is a number, prompt the user for the name of the file
7262 to save in."
7263   (interactive (list (ps-print-preprint current-prefix-arg)
7264                      current-prefix-arg))
7265   (dolist (article (gnus-summary-work-articles n))
7266     (gnus-summary-select-article nil nil 'pseudo article)
7267     (gnus-eval-in-buffer-window gnus-article-buffer
7268       (let ((buffer (generate-new-buffer " *print*")))
7269         (unwind-protect
7270             (progn
7271               (copy-to-buffer buffer (point-min) (point-max))
7272               (set-buffer buffer)
7273               (gnus-article-delete-invisible-text)
7274               (let ((ps-left-header
7275                      (list
7276                       (concat "("
7277                               (mail-header-subject gnus-current-headers) ")")
7278                       (concat "("
7279                               (mail-header-from gnus-current-headers) ")")))
7280                     (ps-right-header
7281                      (list
7282                       "/pagenumberstring load"
7283                       (concat "("
7284                               (mail-header-date gnus-current-headers) ")"))))
7285                 (gnus-run-hooks 'gnus-ps-print-hook)
7286                 (save-excursion
7287                   (ps-print-buffer-with-faces filename))))
7288           (kill-buffer buffer))))))
7289
7290 (defun gnus-summary-show-article (&optional arg)
7291   "Force re-fetching of the current article.
7292 If ARG (the prefix) is a number, show the article with the charset
7293 defined in `gnus-summary-show-article-charset-alist', or the charset
7294 inputed.
7295 If ARG (the prefix) is non-nil and not a number, show the raw article
7296 without any article massaging functions being run."
7297   (interactive "P")
7298   (cond
7299    ((numberp arg)
7300     (let ((gnus-newsgroup-charset
7301            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7302                (read-coding-system "Charset: ")))
7303           (gnus-newsgroup-ignored-charsets 'gnus-all))
7304       (gnus-summary-select-article nil 'force)
7305       (let ((deps gnus-newsgroup-dependencies)
7306             head header)
7307         (save-excursion
7308           (set-buffer gnus-original-article-buffer)
7309           (save-restriction
7310             (message-narrow-to-head)
7311             (setq head (buffer-string)))
7312           (with-temp-buffer
7313             (insert (format "211 %d Article retrieved.\n"
7314                             (cdr gnus-article-current)))
7315             (insert head)
7316             (insert ".\n")
7317             (let ((nntp-server-buffer (current-buffer)))
7318               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7319         (gnus-data-set-header
7320          (gnus-data-find (cdr gnus-article-current))
7321          header)
7322         (gnus-summary-update-article-line
7323          (cdr gnus-article-current) header))))
7324    ((not arg)
7325     ;; Select the article the normal way.
7326     (gnus-summary-select-article nil 'force))
7327    (t
7328     ;; We have to require this here to make sure that the following
7329     ;; dynamic binding isn't shadowed by autoloading.
7330     (require 'gnus-async)
7331     (require 'gnus-art)
7332     ;; Bind the article treatment functions to nil.
7333     (let ((gnus-have-all-headers t)
7334           gnus-article-prepare-hook
7335           gnus-article-decode-hook
7336           gnus-display-mime-function
7337           gnus-break-pages)
7338       ;; Destroy any MIME parts.
7339       (when (gnus-buffer-live-p gnus-article-buffer)
7340         (save-excursion
7341           (set-buffer gnus-article-buffer)
7342           (mm-destroy-parts gnus-article-mime-handles)
7343           ;; Set it to nil for safety reason.
7344           (setq gnus-article-mime-handle-alist nil)
7345           (setq gnus-article-mime-handles nil)))
7346       (gnus-summary-select-article nil 'force))))
7347   (gnus-summary-goto-subject gnus-current-article)
7348   (gnus-summary-position-point))
7349
7350 (defun gnus-summary-verbose-headers (&optional arg)
7351   "Toggle permanent full header display.
7352 If ARG is a positive number, turn header display on.
7353 If ARG is a negative number, turn header display off."
7354   (interactive "P")
7355   (setq gnus-show-all-headers
7356         (cond ((or (not (numberp arg))
7357                    (zerop arg))
7358                (not gnus-show-all-headers))
7359               ((natnump arg)
7360                t)))
7361   (gnus-summary-show-article))
7362
7363 (defun gnus-summary-toggle-header (&optional arg)
7364   "Show the headers if they are hidden, or hide them if they are shown.
7365 If ARG is a positive number, show the entire header.
7366 If ARG is a negative number, hide the unwanted header lines."
7367   (interactive "P")
7368   (save-excursion
7369     (set-buffer gnus-article-buffer)
7370     (save-restriction
7371       (let* ((buffer-read-only nil)
7372              (inhibit-point-motion-hooks t)
7373              hidden e)
7374         (setq hidden
7375               (if (numberp arg)
7376                   (>= arg 0)
7377                 (save-restriction
7378                   (article-narrow-to-head)
7379                   (gnus-article-hidden-text-p 'headers))))
7380         (goto-char (point-min))
7381         (when (search-forward "\n\n" nil t)
7382           (delete-region (point-min) (1- (point))))
7383         (goto-char (point-min))
7384         (save-excursion
7385           (set-buffer gnus-original-article-buffer)
7386           (goto-char (point-min))
7387           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7388         (insert-buffer-substring gnus-original-article-buffer 1 e)
7389         (save-restriction
7390           (narrow-to-region (point-min) (point))
7391           (article-decode-encoded-words)
7392           (if  hidden
7393               (let ((gnus-treat-hide-headers nil)
7394                     (gnus-treat-hide-boring-headers nil))
7395                 (setq gnus-article-wash-types
7396                       (delq 'headers gnus-article-wash-types))
7397                 (gnus-treat-article 'head))
7398             (gnus-treat-article 'head)))
7399         (gnus-set-mode-line 'article)))))
7400
7401 (defun gnus-summary-show-all-headers ()
7402   "Make all header lines visible."
7403   (interactive)
7404   (gnus-article-show-all-headers))
7405
7406 (defun gnus-summary-caesar-message (&optional arg)
7407   "Caesar rotate the current article by 13.
7408 The numerical prefix specifies how many places to rotate each letter
7409 forward."
7410   (interactive "P")
7411   (gnus-summary-select-article)
7412   (let ((mail-header-separator ""))
7413     (gnus-eval-in-buffer-window gnus-article-buffer
7414       (save-restriction
7415         (widen)
7416         (let ((start (window-start))
7417               buffer-read-only)
7418           (message-caesar-buffer-body arg)
7419           (set-window-start (get-buffer-window (current-buffer)) start))))))
7420
7421 (defun gnus-summary-stop-page-breaking ()
7422   "Stop page breaking in the current article."
7423   (interactive)
7424   (gnus-summary-select-article)
7425   (gnus-eval-in-buffer-window gnus-article-buffer
7426     (widen)
7427     (when (gnus-visual-p 'page-marker)
7428       (let ((buffer-read-only nil))
7429         (gnus-remove-text-with-property 'gnus-prev)
7430         (gnus-remove-text-with-property 'gnus-next))
7431       (setq gnus-page-broken nil))))
7432
7433 (defun gnus-summary-move-article (&optional n to-newsgroup
7434                                             select-method action)
7435   "Move the current article to a different newsgroup.
7436 If N is a positive number, move the N next articles.
7437 If N is a negative number, move the N previous articles.
7438 If N is nil and any articles have been marked with the process mark,
7439 move those articles instead.
7440 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7441 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7442 re-spool using this method.
7443
7444 For this function to work, both the current newsgroup and the
7445 newsgroup that you want to move to have to support the `request-move'
7446 and `request-accept' functions.
7447
7448 ACTION can be either `move' (the default), `crosspost' or `copy'."
7449   (interactive "P")
7450   (unless action
7451     (setq action 'move))
7452   ;; Disable marking as read.
7453   (let (gnus-mark-article-hook)
7454     (save-window-excursion
7455       (gnus-summary-select-article)))
7456   ;; Check whether the source group supports the required functions.
7457   (cond ((and (eq action 'move)
7458               (not (gnus-check-backend-function
7459                     'request-move-article gnus-newsgroup-name)))
7460          (error "The current group does not support article moving"))
7461         ((and (eq action 'crosspost)
7462               (not (gnus-check-backend-function
7463                     'request-replace-article gnus-newsgroup-name)))
7464          (error "The current group does not support article editing")))
7465   (let ((articles (gnus-summary-work-articles n))
7466         (prefix (if (gnus-check-backend-function
7467                     'request-move-article gnus-newsgroup-name)
7468                     (gnus-group-real-prefix gnus-newsgroup-name)
7469                   ""))
7470         (names '((move "Move" "Moving")
7471                  (copy "Copy" "Copying")
7472                  (crosspost "Crosspost" "Crossposting")))
7473         (copy-buf (save-excursion
7474                     (nnheader-set-temp-buffer " *copy article*")))
7475         art-group to-method new-xref article to-groups)
7476     (unless (assq action names)
7477       (error "Unknown action %s" action))
7478     ;; Read the newsgroup name.
7479     (when (and (not to-newsgroup)
7480                (not select-method))
7481       (setq to-newsgroup
7482             (gnus-read-move-group-name
7483              (cadr (assq action names))
7484              (symbol-value (intern (format "gnus-current-%s-group" action)))
7485              articles prefix))
7486       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7487     (setq to-method (or select-method
7488                         (gnus-server-to-method
7489                          (gnus-group-method to-newsgroup))))
7490     ;; Check the method we are to move this article to...
7491     (unless (gnus-check-backend-function
7492              'request-accept-article (car to-method))
7493       (error "%s does not support article copying" (car to-method)))
7494     (unless (gnus-check-server to-method)
7495       (error "Can't open server %s" (car to-method)))
7496     (gnus-message 6 "%s to %s: %s..."
7497                   (caddr (assq action names))
7498                   (or (car select-method) to-newsgroup) articles)
7499     (while articles
7500       (setq article (pop articles))
7501       (setq
7502        art-group
7503        (cond
7504         ;; Move the article.
7505         ((eq action 'move)
7506          ;; Remove this article from future suppression.
7507          (gnus-dup-unsuppress-article article)
7508          (gnus-request-move-article
7509           article                       ; Article to move
7510           gnus-newsgroup-name           ; From newsgroup
7511           (nth 1 (gnus-find-method-for-group
7512                   gnus-newsgroup-name)) ; Server
7513           (list 'gnus-request-accept-article
7514                 to-newsgroup (list 'quote select-method)
7515                 (not articles) t)       ; Accept form
7516           (not articles)))              ; Only save nov last time
7517         ;; Copy the article.
7518         ((eq action 'copy)
7519          (save-excursion
7520            (set-buffer copy-buf)
7521            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7522              (gnus-request-accept-article
7523               to-newsgroup select-method (not articles) t))))
7524         ;; Crosspost the article.
7525         ((eq action 'crosspost)
7526          (let ((xref (message-tokenize-header
7527                       (mail-header-xref (gnus-summary-article-header article))
7528                       " ")))
7529            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7530                                   ":" article))
7531            (unless xref
7532              (setq xref (list (system-name))))
7533            (setq new-xref
7534                  (concat
7535                   (mapconcat 'identity
7536                              (delete "Xref:" (delete new-xref xref))
7537                              " ")
7538                   " " new-xref))
7539            (save-excursion
7540              (set-buffer copy-buf)
7541              ;; First put the article in the destination group.
7542              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7543              (when (consp (setq art-group
7544                                 (gnus-request-accept-article
7545                                  to-newsgroup select-method (not articles))))
7546                (setq new-xref (concat new-xref " " (car art-group)
7547                                       ":" (cdr art-group)))
7548                ;; Now we have the new Xrefs header, so we insert
7549                ;; it and replace the new article.
7550                (nnheader-replace-header "Xref" new-xref)
7551                (gnus-request-replace-article
7552                 (cdr art-group) to-newsgroup (current-buffer))
7553                art-group))))))
7554       (cond
7555        ((not art-group)
7556         (gnus-message 1 "Couldn't %s article %s: %s"
7557                       (cadr (assq action names)) article
7558                       (nnheader-get-report (car to-method))))
7559        ((eq art-group 'junk)
7560         (when (eq action 'move)
7561           (gnus-summary-mark-article article gnus-canceled-mark)
7562           (gnus-message 4 "Deleted article %s" article)))
7563        (t
7564         (let* ((pto-group (gnus-group-prefixed-name
7565                            (car art-group) to-method))
7566                (entry
7567                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7568                (info (nth 2 entry))
7569                (to-group (gnus-info-group info))
7570                to-marks)
7571           ;; Update the group that has been moved to.
7572           (when (and info
7573                      (memq action '(move copy)))
7574             (unless (member to-group to-groups)
7575               (push to-group to-groups))
7576
7577             (unless (memq article gnus-newsgroup-unreads)
7578               (push 'read to-marks)
7579               (gnus-info-set-read
7580                info (gnus-add-to-range (gnus-info-read info)
7581                                        (list (cdr art-group)))))
7582
7583             ;; See whether the article is to be put in the cache.
7584             (let ((marks gnus-article-mark-lists)
7585                   (to-article (cdr art-group)))
7586
7587               ;; Enter the article into the cache in the new group,
7588               ;; if that is required.
7589               (when gnus-use-cache
7590                 (gnus-cache-possibly-enter-article
7591                  to-group to-article
7592                  (memq article gnus-newsgroup-marked)
7593                  (memq article gnus-newsgroup-dormant)
7594                  (memq article gnus-newsgroup-unreads)))
7595
7596               (when gnus-preserve-marks
7597                 ;; Copy any marks over to the new group.
7598                 (when (and (equal to-group gnus-newsgroup-name)
7599                            (not (memq article gnus-newsgroup-unreads)))
7600                   ;; Mark this article as read in this group.
7601                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7602                   (setcdr (gnus-active to-group) to-article)
7603                   (setcdr gnus-newsgroup-active to-article))
7604
7605                 (while marks
7606                   (when (memq article (symbol-value
7607                                        (intern (format "gnus-newsgroup-%s"
7608                                                        (caar marks)))))
7609                     (push (cdar marks) to-marks)
7610                     ;; If the other group is the same as this group,
7611                     ;; then we have to add the mark to the list.
7612                     (when (equal to-group gnus-newsgroup-name)
7613                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7614                            (cons to-article
7615                                  (symbol-value
7616                                   (intern (format "gnus-newsgroup-%s"
7617                                                   (caar marks)))))))
7618                     ;; Copy the marks to other group.
7619                     (gnus-add-marked-articles
7620                      to-group (cdar marks) (list to-article) info))
7621                   (setq marks (cdr marks)))
7622
7623                 (gnus-request-set-mark to-group (list (list (list to-article)
7624                                                             'set
7625                                                             to-marks))))
7626
7627               (gnus-dribble-enter
7628                (concat "(gnus-group-set-info '"
7629                        (gnus-prin1-to-string (gnus-get-info to-group))
7630                        ")"))))
7631
7632           ;; Update the Xref header in this article to point to
7633           ;; the new crossposted article we have just created.
7634           (when (eq action 'crosspost)
7635             (save-excursion
7636               (set-buffer copy-buf)
7637               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7638               (nnheader-replace-header "Xref" new-xref)
7639               (gnus-request-replace-article
7640                article gnus-newsgroup-name (current-buffer)))))
7641
7642         ;;;!!!Why is this necessary?
7643         (set-buffer gnus-summary-buffer)
7644
7645         (gnus-summary-goto-subject article)
7646         (when (eq action 'move)
7647           (gnus-summary-mark-article article gnus-canceled-mark))))
7648       (gnus-summary-remove-process-mark article))
7649     ;; Re-activate all groups that have been moved to.
7650     (while to-groups
7651       (save-excursion
7652         (set-buffer gnus-group-buffer)
7653         (when (gnus-group-goto-group (car to-groups) t)
7654           (gnus-group-get-new-news-this-group 1 t))
7655         (pop to-groups)))
7656
7657     (gnus-kill-buffer copy-buf)
7658     (gnus-summary-position-point)
7659     (gnus-set-mode-line 'summary)))
7660
7661 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7662   "Move the current article to a different newsgroup.
7663 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7664 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7665 re-spool using this method."
7666   (interactive "P")
7667   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7668
7669 (defun gnus-summary-crosspost-article (&optional n)
7670   "Crosspost the current article to some other group."
7671   (interactive "P")
7672   (gnus-summary-move-article n nil nil 'crosspost))
7673
7674 (defcustom gnus-summary-respool-default-method nil
7675   "Default method for respooling an article.
7676 If nil, use to the current newsgroup method."
7677   :type '(choice (gnus-select-method :value (nnml ""))
7678                  (const nil))
7679   :group 'gnus-summary-mail)
7680
7681 (defun gnus-summary-respool-article (&optional n method)
7682   "Respool the current article.
7683 The article will be squeezed through the mail spooling process again,
7684 which means that it will be put in some mail newsgroup or other
7685 depending on `nnmail-split-methods'.
7686 If N is a positive number, respool the N next articles.
7687 If N is a negative number, respool the N previous articles.
7688 If N is nil and any articles have been marked with the process mark,
7689 respool those articles instead.
7690
7691 Respooling can be done both from mail groups and \"real\" newsgroups.
7692 In the former case, the articles in question will be moved from the
7693 current group into whatever groups they are destined to.  In the
7694 latter case, they will be copied into the relevant groups."
7695   (interactive
7696    (list current-prefix-arg
7697          (let* ((methods (gnus-methods-using 'respool))
7698                 (methname
7699                  (symbol-name (or gnus-summary-respool-default-method
7700                                   (car (gnus-find-method-for-group
7701                                         gnus-newsgroup-name)))))
7702                 (method
7703                  (gnus-completing-read
7704                   methname "What backend do you want to use when respooling?"
7705                   methods nil t nil 'gnus-mail-method-history))
7706                 ms)
7707            (cond
7708             ((zerop (length (setq ms (gnus-servers-using-backend
7709                                       (intern method)))))
7710              (list (intern method) ""))
7711             ((= 1 (length ms))
7712              (car ms))
7713             (t
7714              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7715                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7716                            ms-alist))))))))
7717   (unless method
7718     (error "No method given for respooling"))
7719   (if (assoc (symbol-name
7720               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7721              (gnus-methods-using 'respool))
7722       (gnus-summary-move-article n nil method)
7723     (gnus-summary-copy-article n nil method)))
7724
7725 (defun gnus-summary-import-article (file)
7726   "Import an arbitrary file into a mail newsgroup."
7727   (interactive "fImport file: ")
7728   (let ((group gnus-newsgroup-name)
7729         (now (current-time))
7730         atts lines)
7731     (unless (gnus-check-backend-function 'request-accept-article group)
7732       (error "%s does not support article importing" group))
7733     (or (file-readable-p file)
7734         (not (file-regular-p file))
7735         (error "Can't read %s" file))
7736     (save-excursion
7737       (set-buffer (gnus-get-buffer-create " *import file*"))
7738       (erase-buffer)
7739       (nnheader-insert-file-contents file)
7740       (goto-char (point-min))
7741       (unless (nnheader-article-p)
7742         ;; This doesn't look like an article, so we fudge some headers.
7743         (setq atts (file-attributes file)
7744               lines (count-lines (point-min) (point-max)))
7745         (insert "From: " (read-string "From: ") "\n"
7746                 "Subject: " (read-string "Subject: ") "\n"
7747                 "Date: " (message-make-date (nth 5 atts))
7748                 "\n"
7749                 "Message-ID: " (message-make-message-id) "\n"
7750                 "Lines: " (int-to-string lines) "\n"
7751                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7752       (gnus-request-accept-article group nil t)
7753       (kill-buffer (current-buffer)))))
7754
7755 (defun gnus-summary-article-posted-p ()
7756   "Say whether the current (mail) article is available from news as well.
7757 This will be the case if the article has both been mailed and posted."
7758   (interactive)
7759   (let ((id (mail-header-references (gnus-summary-article-header)))
7760         (gnus-override-method (car (gnus-refer-article-methods))))
7761     (if (gnus-request-head id "")
7762         (gnus-message 2 "The current message was found on %s"
7763                       gnus-override-method)
7764       (gnus-message 2 "The current message couldn't be found on %s"
7765                     gnus-override-method)
7766       nil)))
7767
7768 (defun gnus-summary-expire-articles (&optional now)
7769   "Expire all articles that are marked as expirable in the current group."
7770   (interactive)
7771   (when (gnus-check-backend-function
7772          'request-expire-articles gnus-newsgroup-name)
7773     ;; This backend supports expiry.
7774     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7775            (expirable (if total
7776                           (progn
7777                             ;; We need to update the info for
7778                             ;; this group for `gnus-list-of-read-articles'
7779                             ;; to give us the right answer.
7780                             (gnus-run-hooks 'gnus-exit-group-hook)
7781                             (gnus-summary-update-info)
7782                             (gnus-list-of-read-articles gnus-newsgroup-name))
7783                         (setq gnus-newsgroup-expirable
7784                               (sort gnus-newsgroup-expirable '<))))
7785            (expiry-wait (if now 'immediate
7786                           (gnus-group-find-parameter
7787                            gnus-newsgroup-name 'expiry-wait)))
7788            (nnmail-expiry-target
7789             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7790                 nnmail-expiry-target))
7791            es)
7792       (when expirable
7793         ;; There are expirable articles in this group, so we run them
7794         ;; through the expiry process.
7795         (gnus-message 6 "Expiring articles...")
7796         (unless (gnus-check-group gnus-newsgroup-name)
7797           (error "Can't open server for %s" gnus-newsgroup-name))
7798         ;; The list of articles that weren't expired is returned.
7799         (save-excursion
7800           (if expiry-wait
7801               (let ((nnmail-expiry-wait-function nil)
7802                     (nnmail-expiry-wait expiry-wait))
7803                 (setq es (gnus-request-expire-articles
7804                           expirable gnus-newsgroup-name)))
7805             (setq es (gnus-request-expire-articles
7806                       expirable gnus-newsgroup-name)))
7807           (unless total
7808             (setq gnus-newsgroup-expirable es))
7809           ;; We go through the old list of expirable, and mark all
7810           ;; really expired articles as nonexistent.
7811           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7812             (let ((gnus-use-cache nil))
7813               (while expirable
7814                 (unless (memq (car expirable) es)
7815                   (when (gnus-data-find (car expirable))
7816                     (gnus-summary-mark-article
7817                      (car expirable) gnus-canceled-mark)))
7818                 (setq expirable (cdr expirable))))))
7819         (gnus-message 6 "Expiring articles...done")))))
7820
7821 (defun gnus-summary-expire-articles-now ()
7822   "Expunge all expirable articles in the current group.
7823 This means that *all* articles that are marked as expirable will be
7824 deleted forever, right now."
7825   (interactive)
7826   (or gnus-expert-user
7827       (gnus-yes-or-no-p
7828        "Are you really, really, really sure you want to delete all these messages? ")
7829       (error "Phew!"))
7830   (gnus-summary-expire-articles t))
7831
7832 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7833 (defun gnus-summary-delete-article (&optional n)
7834   "Delete the N next (mail) articles.
7835 This command actually deletes articles.  This is not a marking
7836 command.  The article will disappear forever from your life, never to
7837 return.
7838 If N is negative, delete backwards.
7839 If N is nil and articles have been marked with the process mark,
7840 delete these instead."
7841   (interactive "P")
7842   (unless (gnus-check-backend-function 'request-expire-articles
7843                                        gnus-newsgroup-name)
7844     (error "The current newsgroup does not support article deletion"))
7845   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7846     (error "Couldn't open server"))
7847   ;; Compute the list of articles to delete.
7848   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7849         not-deleted)
7850     (if (and gnus-novice-user
7851              (not (gnus-yes-or-no-p
7852                    (format "Do you really want to delete %s forever? "
7853                            (if (> (length articles) 1)
7854                                (format "these %s articles" (length articles))
7855                              "this article")))))
7856         ()
7857       ;; Delete the articles.
7858       (setq not-deleted (gnus-request-expire-articles
7859                          articles gnus-newsgroup-name 'force))
7860       (while articles
7861         (gnus-summary-remove-process-mark (car articles))
7862         ;; The backend might not have been able to delete the article
7863         ;; after all.
7864         (unless (memq (car articles) not-deleted)
7865           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7866         (setq articles (cdr articles)))
7867       (when not-deleted
7868         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7869     (gnus-summary-position-point)
7870     (gnus-set-mode-line 'summary)
7871     not-deleted))
7872
7873 (defun gnus-summary-edit-article (&optional arg)
7874   "Edit the current article.
7875 This will have permanent effect only in mail groups.
7876 If ARG is nil, edit the decoded articles.
7877 If ARG is 1, edit the raw articles. 
7878 If ARG is 2, edit the raw articles even in read-only groups.
7879 If ARG is 3, edit the articles with the current handles.
7880 Otherwise, allow editing of articles even in read-only
7881 groups."
7882   (interactive "P")
7883   (let (force raw current-handles)
7884     (cond 
7885      ((null arg))
7886      ((eq arg 1) (setq raw t))
7887      ((eq arg 2) (setq raw t
7888                        force t))
7889      ((eq arg 3) (setq current-handles 
7890                        (and (gnus-buffer-live-p gnus-article-buffer)
7891                             (with-current-buffer gnus-article-buffer
7892                               (prog1
7893                                   gnus-article-mime-handles
7894                                   (setq gnus-article-mime-handles nil))))))
7895      (t (setq force t)))
7896     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7897         (error "Can't edit the raw article in group nndraft:drafts."))
7898     (save-excursion
7899       (set-buffer gnus-summary-buffer)
7900       (let ((mail-parse-charset gnus-newsgroup-charset)
7901             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7902         (gnus-set-global-variables)
7903         (when (and (not force)
7904                    (gnus-group-read-only-p))
7905           (error "The current newsgroup does not support article editing"))
7906         (gnus-summary-show-article t)
7907         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7908           (with-current-buffer gnus-article-buffer
7909             (mm-enable-multibyte-mule4)))
7910         (if (equal gnus-newsgroup-name "nndraft:drafts")
7911             (setq raw t))
7912         (gnus-article-edit-article
7913          (if raw 'ignore 
7914            `(lambda () 
7915               (let ((mbl mml-buffer-list))
7916                 (setq mml-buffer-list nil)
7917                 (mime-to-mml ,'current-handles)
7918                 (make-local-hook 'kill-buffer-hook)
7919                 (let ((mbl1 mml-buffer-list))
7920                   (setq mml-buffer-list mbl)
7921                   (set (make-local-variable 'mml-buffer-list) mbl1))
7922                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7923          `(lambda (no-highlight)
7924             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7925                   (message-options message-options)
7926                   (message-options-set-recipient)
7927                   (mail-parse-ignored-charsets 
7928                    ',gnus-newsgroup-ignored-charsets))
7929               ,(if (not raw) '(progn 
7930                                 (mml-to-mime)
7931                                 (mml-destroy-buffers)
7932                                 (remove-hook 'kill-buffer-hook 
7933                                              'mml-destroy-buffers t)
7934                                 (kill-local-variable 'mml-buffer-list)))
7935               (gnus-summary-edit-article-done
7936                ,(or (mail-header-references gnus-current-headers) "")
7937                ,(gnus-group-read-only-p) 
7938                ,gnus-summary-buffer no-highlight))))))))
7939
7940 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7941
7942 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7943                                                  no-highlight)
7944   "Make edits to the current article permanent."
7945   (interactive)
7946   (save-excursion
7947     ;; The buffer restriction contains the entire article if it exists.
7948     (when (article-goto-body)
7949       (let ((lines (count-lines (point) (point-max)))
7950             (length (- (point-max) (point)))
7951             (case-fold-search t)
7952             (body (copy-marker (point))))
7953         (goto-char (point-min))
7954         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
7955           (delete-region (match-beginning 1) (match-end 1))
7956           (insert (number-to-string length)))
7957         (goto-char (point-min))
7958         (when (re-search-forward
7959                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
7960           (delete-region (match-beginning 1) (match-end 1))
7961           (insert (number-to-string length)))
7962         (goto-char (point-min))
7963         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
7964           (delete-region (match-beginning 1) (match-end 1))
7965           (insert (number-to-string lines))))))
7966   ;; Replace the article.
7967   (let ((buf (current-buffer)))
7968     (with-temp-buffer
7969       (insert-buffer-substring buf)
7970       
7971       (if (and (not read-only)
7972                (not (gnus-request-replace-article
7973                      (cdr gnus-article-current) (car gnus-article-current)
7974                      (current-buffer) t)))
7975           (error "Couldn't replace article")
7976         ;; Update the summary buffer.
7977         (if (and references
7978                  (equal (message-tokenize-header references " ")
7979                         (message-tokenize-header
7980                          (or (message-fetch-field "references") "") " ")))
7981             ;; We only have to update this line.
7982             (save-excursion
7983               (save-restriction
7984                 (message-narrow-to-head)
7985                 (let ((head (buffer-string))
7986                       header)
7987                   (with-temp-buffer
7988                     (insert (format "211 %d Article retrieved.\n"
7989                                     (cdr gnus-article-current)))
7990                     (insert head)
7991                     (insert ".\n")
7992                     (let ((nntp-server-buffer (current-buffer)))
7993                       (setq header (car (gnus-get-newsgroup-headers
7994                                          (save-excursion
7995                                            (set-buffer gnus-summary-buffer)
7996                                            gnus-newsgroup-dependencies)
7997                                          t))))
7998                     (save-excursion
7999                       (set-buffer gnus-summary-buffer)
8000                       (gnus-data-set-header
8001                        (gnus-data-find (cdr gnus-article-current))
8002                        header)
8003                       (gnus-summary-update-article-line
8004                        (cdr gnus-article-current) header))))))
8005           ;; Update threads.
8006           (set-buffer (or buffer gnus-summary-buffer))
8007           (gnus-summary-update-article (cdr gnus-article-current)))
8008         ;; Prettify the article buffer again.
8009         (unless no-highlight
8010           (save-excursion
8011             (set-buffer gnus-article-buffer)
8012             ;;;!!! Fix this -- article should be rehighlighted.
8013             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8014             (set-buffer gnus-original-article-buffer)
8015             (gnus-request-article
8016              (cdr gnus-article-current)
8017              (car gnus-article-current) (current-buffer))))
8018         ;; Prettify the summary buffer line.
8019         (when (gnus-visual-p 'summary-highlight 'highlight)
8020           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8021
8022 (defun gnus-summary-edit-wash (key)
8023   "Perform editing command KEY in the article buffer."
8024   (interactive
8025    (list
8026     (progn
8027       (message "%s" (concat (this-command-keys) "- "))
8028       (read-char))))
8029   (message "")
8030   (gnus-summary-edit-article)
8031   (execute-kbd-macro (concat (this-command-keys) key))
8032   (gnus-article-edit-done))
8033
8034 ;;; Respooling
8035
8036 (defun gnus-summary-respool-query (&optional silent trace)
8037   "Query where the respool algorithm would put this article."
8038   (interactive)
8039   (let (gnus-mark-article-hook)
8040     (gnus-summary-select-article)
8041     (save-excursion
8042       (set-buffer gnus-original-article-buffer)
8043       (save-restriction
8044         (message-narrow-to-head)
8045         (let ((groups (nnmail-article-group 'identity trace)))
8046           (unless silent
8047             (if groups
8048                 (message "This message would go to %s"
8049                          (mapconcat 'car groups ", "))
8050               (message "This message would go to no groups"))
8051             groups))))))
8052
8053 (defun gnus-summary-respool-trace ()
8054   "Trace where the respool algorithm would put this article.
8055 Display a buffer showing all fancy splitting patterns which matched."
8056   (interactive)
8057   (gnus-summary-respool-query nil t))
8058
8059 ;; Summary marking commands.
8060
8061 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8062   "Mark articles which has the same subject as read, and then select the next.
8063 If UNMARK is positive, remove any kind of mark.
8064 If UNMARK is negative, tick articles."
8065   (interactive "P")
8066   (when unmark
8067     (setq unmark (prefix-numeric-value unmark)))
8068   (let ((count
8069          (gnus-summary-mark-same-subject
8070           (gnus-summary-article-subject) unmark)))
8071     ;; Select next unread article.  If auto-select-same mode, should
8072     ;; select the first unread article.
8073     (gnus-summary-next-article t (and gnus-auto-select-same
8074                                       (gnus-summary-article-subject)))
8075     (gnus-message 7 "%d article%s marked as %s"
8076                   count (if (= count 1) " is" "s are")
8077                   (if unmark "unread" "read"))))
8078
8079 (defun gnus-summary-kill-same-subject (&optional unmark)
8080   "Mark articles which has the same subject as read.
8081 If UNMARK is positive, remove any kind of mark.
8082 If UNMARK is negative, tick articles."
8083   (interactive "P")
8084   (when unmark
8085     (setq unmark (prefix-numeric-value unmark)))
8086   (let ((count
8087          (gnus-summary-mark-same-subject
8088           (gnus-summary-article-subject) unmark)))
8089     ;; If marked as read, go to next unread subject.
8090     (when (null unmark)
8091       ;; Go to next unread subject.
8092       (gnus-summary-next-subject 1 t))
8093     (gnus-message 7 "%d articles are marked as %s"
8094                   count (if unmark "unread" "read"))))
8095
8096 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8097   "Mark articles with same SUBJECT as read, and return marked number.
8098 If optional argument UNMARK is positive, remove any kinds of marks.
8099 If optional argument UNMARK is negative, mark articles as unread instead."
8100   (let ((count 1))
8101     (save-excursion
8102       (cond
8103        ((null unmark)                   ; Mark as read.
8104         (while (and
8105                 (progn
8106                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8107                   (gnus-summary-show-thread) t)
8108                 (gnus-summary-find-subject subject))
8109           (setq count (1+ count))))
8110        ((> unmark 0)                    ; Tick.
8111         (while (and
8112                 (progn
8113                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8114                   (gnus-summary-show-thread) t)
8115                 (gnus-summary-find-subject subject))
8116           (setq count (1+ count))))
8117        (t                               ; Mark as unread.
8118         (while (and
8119                 (progn
8120                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8121                   (gnus-summary-show-thread) t)
8122                 (gnus-summary-find-subject subject))
8123           (setq count (1+ count)))))
8124       (gnus-set-mode-line 'summary)
8125       ;; Return the number of marked articles.
8126       count)))
8127
8128 (defun gnus-summary-mark-as-processable (n &optional unmark)
8129   "Set the process mark on the next N articles.
8130 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8131 the process mark instead.  The difference between N and the actual
8132 number of articles marked is returned."
8133   (interactive "p")
8134   (let ((backward (< n 0))
8135         (n (abs n)))
8136     (while (and
8137             (> n 0)
8138             (if unmark
8139                 (gnus-summary-remove-process-mark
8140                  (gnus-summary-article-number))
8141               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8142             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8143       (setq n (1- n)))
8144     (when (/= 0 n)
8145       (gnus-message 7 "No more articles"))
8146     (gnus-summary-recenter)
8147     (gnus-summary-position-point)
8148     n))
8149
8150 (defun gnus-summary-unmark-as-processable (n)
8151   "Remove the process mark from the next N articles.
8152 If N is negative, unmark backward instead.  The difference between N and
8153 the actual number of articles unmarked is returned."
8154   (interactive "p")
8155   (gnus-summary-mark-as-processable n t))
8156
8157 (defun gnus-summary-unmark-all-processable ()
8158   "Remove the process mark from all articles."
8159   (interactive)
8160   (save-excursion
8161     (while gnus-newsgroup-processable
8162       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8163   (gnus-summary-position-point))
8164
8165 (defun gnus-summary-mark-as-expirable (n)
8166   "Mark N articles forward as expirable.
8167 If N is negative, mark backward instead.  The difference between N and
8168 the actual number of articles marked is returned."
8169   (interactive "p")
8170   (gnus-summary-mark-forward n gnus-expirable-mark))
8171
8172 (defun gnus-summary-mark-article-as-replied (article)
8173   "Mark ARTICLE replied and update the summary line."
8174   (push article gnus-newsgroup-replied)
8175   (let ((buffer-read-only nil))
8176     (when (gnus-summary-goto-subject article nil t)
8177       (gnus-summary-update-secondary-mark article))))
8178
8179 (defun gnus-summary-set-bookmark (article)
8180   "Set a bookmark in current article."
8181   (interactive (list (gnus-summary-article-number)))
8182   (when (or (not (get-buffer gnus-article-buffer))
8183             (not gnus-current-article)
8184             (not gnus-article-current)
8185             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8186     (error "No current article selected"))
8187   ;; Remove old bookmark, if one exists.
8188   (let ((old (assq article gnus-newsgroup-bookmarks)))
8189     (when old
8190       (setq gnus-newsgroup-bookmarks
8191             (delq old gnus-newsgroup-bookmarks))))
8192   ;; Set the new bookmark, which is on the form
8193   ;; (article-number . line-number-in-body).
8194   (push
8195    (cons article
8196          (save-excursion
8197            (set-buffer gnus-article-buffer)
8198            (count-lines
8199             (min (point)
8200                  (save-excursion
8201                    (goto-char (point-min))
8202                    (search-forward "\n\n" nil t)
8203                    (point)))
8204             (point))))
8205    gnus-newsgroup-bookmarks)
8206   (gnus-message 6 "A bookmark has been added to the current article."))
8207
8208 (defun gnus-summary-remove-bookmark (article)
8209   "Remove the bookmark from the current article."
8210   (interactive (list (gnus-summary-article-number)))
8211   ;; Remove old bookmark, if one exists.
8212   (let ((old (assq article gnus-newsgroup-bookmarks)))
8213     (if old
8214         (progn
8215           (setq gnus-newsgroup-bookmarks
8216                 (delq old gnus-newsgroup-bookmarks))
8217           (gnus-message 6 "Removed bookmark."))
8218       (gnus-message 6 "No bookmark in current article."))))
8219
8220 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8221 (defun gnus-summary-mark-as-dormant (n)
8222   "Mark N articles forward as dormant.
8223 If N is negative, mark backward instead.  The difference between N and
8224 the actual number of articles marked is returned."
8225   (interactive "p")
8226   (gnus-summary-mark-forward n gnus-dormant-mark))
8227
8228 (defun gnus-summary-set-process-mark (article)
8229   "Set the process mark on ARTICLE and update the summary line."
8230   (setq gnus-newsgroup-processable
8231         (cons article
8232               (delq article gnus-newsgroup-processable)))
8233   (when (gnus-summary-goto-subject article)
8234     (gnus-summary-show-thread)
8235     (gnus-summary-goto-subject article)
8236     (gnus-summary-update-secondary-mark article)))
8237
8238 (defun gnus-summary-remove-process-mark (article)
8239   "Remove the process mark from ARTICLE and update the summary line."
8240   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8241   (when (gnus-summary-goto-subject article)
8242     (gnus-summary-show-thread)
8243     (gnus-summary-goto-subject article)
8244     (gnus-summary-update-secondary-mark article)))
8245
8246 (defun gnus-summary-set-saved-mark (article)
8247   "Set the process mark on ARTICLE and update the summary line."
8248   (push article gnus-newsgroup-saved)
8249   (when (gnus-summary-goto-subject article)
8250     (gnus-summary-update-secondary-mark article)))
8251
8252 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8253   "Mark N articles as read forwards.
8254 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8255 The difference between N and the actual number of articles marked is
8256 returned.
8257 Iff NO-EXPIRE, auto-expiry will be inhibited."
8258   (interactive "p")
8259   (gnus-summary-show-thread)
8260   (let ((backward (< n 0))
8261         (gnus-summary-goto-unread
8262          (and gnus-summary-goto-unread
8263               (not (eq gnus-summary-goto-unread 'never))
8264               (not (memq mark (list gnus-unread-mark
8265                                     gnus-ticked-mark gnus-dormant-mark)))))
8266         (n (abs n))
8267         (mark (or mark gnus-del-mark)))
8268     (while (and (> n 0)
8269                 (gnus-summary-mark-article nil mark no-expire)
8270                 (zerop (gnus-summary-next-subject
8271                         (if backward -1 1)
8272                         (and gnus-summary-goto-unread
8273                              (not (eq gnus-summary-goto-unread 'never)))
8274                         t)))
8275       (setq n (1- n)))
8276     (when (/= 0 n)
8277       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8278     (gnus-summary-recenter)
8279     (gnus-summary-position-point)
8280     (gnus-set-mode-line 'summary)
8281     n))
8282
8283 (defun gnus-summary-mark-article-as-read (mark)
8284   "Mark the current article quickly as read with MARK."
8285   (let ((article (gnus-summary-article-number)))
8286     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8287     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8288     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8289     (push (cons article mark) gnus-newsgroup-reads)
8290     ;; Possibly remove from cache, if that is used.
8291     (when gnus-use-cache
8292       (gnus-cache-enter-remove-article article))
8293     ;; Allow the backend to change the mark.
8294     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8295     ;; Check for auto-expiry.
8296     (when (and gnus-newsgroup-auto-expire
8297                (memq mark gnus-auto-expirable-marks))
8298       (setq mark gnus-expirable-mark)
8299       ;; Let the backend know about the mark change.
8300       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8301       (push article gnus-newsgroup-expirable))
8302     ;; Set the mark in the buffer.
8303     (gnus-summary-update-mark mark 'unread)
8304     t))
8305
8306 (defun gnus-summary-mark-article-as-unread (mark)
8307   "Mark the current article quickly as unread with MARK."
8308   (let* ((article (gnus-summary-article-number))
8309          (old-mark (gnus-summary-article-mark article)))
8310     ;; Allow the backend to change the mark.
8311     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8312     (if (eq mark old-mark)
8313         t
8314       (if (<= article 0)
8315           (progn
8316             (gnus-error 1 "Can't mark negative article numbers")
8317             nil)
8318         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8319         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8320         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8321         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8322         (cond ((= mark gnus-ticked-mark)
8323                (push article gnus-newsgroup-marked))
8324               ((= mark gnus-dormant-mark)
8325                (push article gnus-newsgroup-dormant))
8326               (t
8327                (push article gnus-newsgroup-unreads)))
8328         (gnus-pull article gnus-newsgroup-reads)
8329
8330         ;; See whether the article is to be put in the cache.
8331         (and gnus-use-cache
8332              (vectorp (gnus-summary-article-header article))
8333              (save-excursion
8334                (gnus-cache-possibly-enter-article
8335                 gnus-newsgroup-name article
8336                 (= mark gnus-ticked-mark)
8337                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8338
8339         ;; Fix the mark.
8340         (gnus-summary-update-mark mark 'unread)
8341         t))))
8342
8343 (defun gnus-summary-mark-article (&optional article mark no-expire)
8344   "Mark ARTICLE with MARK.  MARK can be any character.
8345 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8346 `??' (dormant) and `?E' (expirable).
8347 If MARK is nil, then the default character `?r' is used.
8348 If ARTICLE is nil, then the article on the current line will be
8349 marked.
8350 Iff NO-EXPIRE, auto-expiry will be inhibited."
8351   ;; The mark might be a string.
8352   (when (stringp mark)
8353     (setq mark (aref mark 0)))
8354   ;; If no mark is given, then we check auto-expiring.
8355   (when (null mark)
8356     (setq mark gnus-del-mark))
8357   (when (and (not no-expire)
8358              gnus-newsgroup-auto-expire
8359              (memq mark gnus-auto-expirable-marks))
8360     (setq mark gnus-expirable-mark))
8361   (let ((article (or article (gnus-summary-article-number)))
8362         (old-mark (gnus-summary-article-mark article)))
8363     ;; Allow the backend to change the mark.
8364     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8365     (if (eq mark old-mark)
8366         t
8367       (unless article
8368         (error "No article on current line"))
8369       (if (not (if (or (= mark gnus-unread-mark)
8370                        (= mark gnus-ticked-mark)
8371                        (= mark gnus-dormant-mark))
8372                    (gnus-mark-article-as-unread article mark)
8373                  (gnus-mark-article-as-read article mark)))
8374           t
8375         ;; See whether the article is to be put in the cache.
8376         (and gnus-use-cache
8377              (not (= mark gnus-canceled-mark))
8378              (vectorp (gnus-summary-article-header article))
8379              (save-excursion
8380                (gnus-cache-possibly-enter-article
8381                 gnus-newsgroup-name article
8382                 (= mark gnus-ticked-mark)
8383                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8384
8385         (when (gnus-summary-goto-subject article nil t)
8386           (let ((buffer-read-only nil))
8387             (gnus-summary-show-thread)
8388             ;; Fix the mark.
8389             (gnus-summary-update-mark mark 'unread)
8390             t))))))
8391
8392 (defun gnus-summary-update-secondary-mark (article)
8393   "Update the secondary (read, process, cache) mark."
8394   (gnus-summary-update-mark
8395    (cond ((memq article gnus-newsgroup-processable)
8396           gnus-process-mark)
8397          ((memq article gnus-newsgroup-cached)
8398           gnus-cached-mark)
8399          ((memq article gnus-newsgroup-replied)
8400           gnus-replied-mark)
8401          ((memq article gnus-newsgroup-saved)
8402           gnus-saved-mark)
8403          (t gnus-unread-mark))
8404    'replied)
8405   (when (gnus-visual-p 'summary-highlight 'highlight)
8406     (gnus-run-hooks 'gnus-summary-update-hook))
8407   t)
8408
8409 (defun gnus-summary-update-mark (mark type)
8410   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8411         (buffer-read-only nil))
8412     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8413     (when forward
8414       (when (looking-at "\r")
8415         (incf forward))
8416       (when (<= (+ forward (point)) (point-max))
8417         ;; Go to the right position on the line.
8418         (goto-char (+ forward (point)))
8419         ;; Replace the old mark with the new mark.
8420         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8421         ;; Optionally update the marks by some user rule.
8422         (when (eq type 'unread)
8423           (gnus-data-set-mark
8424            (gnus-data-find (gnus-summary-article-number)) mark)
8425           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8426
8427 (defun gnus-mark-article-as-read (article &optional mark)
8428   "Enter ARTICLE in the pertinent lists and remove it from others."
8429   ;; Make the article expirable.
8430   (let ((mark (or mark gnus-del-mark)))
8431     (if (= mark gnus-expirable-mark)
8432         (push article gnus-newsgroup-expirable)
8433       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8434     ;; Remove from unread and marked lists.
8435     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8436     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8437     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8438     (push (cons article mark) gnus-newsgroup-reads)
8439     ;; Possibly remove from cache, if that is used.
8440     (when gnus-use-cache
8441       (gnus-cache-enter-remove-article article))
8442     t))
8443
8444 (defun gnus-mark-article-as-unread (article &optional mark)
8445   "Enter ARTICLE in the pertinent lists and remove it from others."
8446   (let ((mark (or mark gnus-ticked-mark)))
8447     (if (<= article 0)
8448         (progn
8449           (gnus-error 1 "Can't mark negative article numbers")
8450           nil)
8451       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8452             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8453             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8454             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8455
8456       ;; Unsuppress duplicates?
8457       (when gnus-suppress-duplicates
8458         (gnus-dup-unsuppress-article article))
8459
8460       (cond ((= mark gnus-ticked-mark)
8461              (push article gnus-newsgroup-marked))
8462             ((= mark gnus-dormant-mark)
8463              (push article gnus-newsgroup-dormant))
8464             (t
8465              (push article gnus-newsgroup-unreads)))
8466       (gnus-pull article gnus-newsgroup-reads)
8467       t)))
8468
8469 (defalias 'gnus-summary-mark-as-unread-forward
8470   'gnus-summary-tick-article-forward)
8471 (make-obsolete 'gnus-summary-mark-as-unread-forward
8472                'gnus-summary-tick-article-forward)
8473 (defun gnus-summary-tick-article-forward (n)
8474   "Tick N articles forwards.
8475 If N is negative, tick backwards instead.
8476 The difference between N and the number of articles ticked is returned."
8477   (interactive "p")
8478   (gnus-summary-mark-forward n gnus-ticked-mark))
8479
8480 (defalias 'gnus-summary-mark-as-unread-backward
8481   'gnus-summary-tick-article-backward)
8482 (make-obsolete 'gnus-summary-mark-as-unread-backward
8483                'gnus-summary-tick-article-backward)
8484 (defun gnus-summary-tick-article-backward (n)
8485   "Tick N articles backwards.
8486 The difference between N and the number of articles ticked is returned."
8487   (interactive "p")
8488   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8489
8490 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8491 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8492 (defun gnus-summary-tick-article (&optional article clear-mark)
8493   "Mark current article as unread.
8494 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8495 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8496   (interactive)
8497   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8498                                        gnus-ticked-mark)))
8499
8500 (defun gnus-summary-mark-as-read-forward (n)
8501   "Mark N articles as read forwards.
8502 If N is negative, mark backwards instead.
8503 The difference between N and the actual number of articles marked is
8504 returned."
8505   (interactive "p")
8506   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8507
8508 (defun gnus-summary-mark-as-read-backward (n)
8509   "Mark the N articles as read backwards.
8510 The difference between N and the actual number of articles marked is
8511 returned."
8512   (interactive "p")
8513   (gnus-summary-mark-forward
8514    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8515
8516 (defun gnus-summary-mark-as-read (&optional article mark)
8517   "Mark current article as read.
8518 ARTICLE specifies the article to be marked as read.
8519 MARK specifies a string to be inserted at the beginning of the line."
8520   (gnus-summary-mark-article article mark))
8521
8522 (defun gnus-summary-clear-mark-forward (n)
8523   "Clear marks from N articles forward.
8524 If N is negative, clear backward instead.
8525 The difference between N and the number of marks cleared is returned."
8526   (interactive "p")
8527   (gnus-summary-mark-forward n gnus-unread-mark))
8528
8529 (defun gnus-summary-clear-mark-backward (n)
8530   "Clear marks from N articles backward.
8531 The difference between N and the number of marks cleared is returned."
8532   (interactive "p")
8533   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8534
8535 (defun gnus-summary-mark-unread-as-read ()
8536   "Intended to be used by `gnus-summary-mark-article-hook'."
8537   (when (memq gnus-current-article gnus-newsgroup-unreads)
8538     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8539
8540 (defun gnus-summary-mark-read-and-unread-as-read ()
8541   "Intended to be used by `gnus-summary-mark-article-hook'."
8542   (let ((mark (gnus-summary-article-mark)))
8543     (when (or (gnus-unread-mark-p mark)
8544               (gnus-read-mark-p mark))
8545       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8546
8547 (defun gnus-summary-mark-region-as-read (point mark all)
8548   "Mark all unread articles between point and mark as read.
8549 If given a prefix, mark all articles between point and mark as read,
8550 even ticked and dormant ones."
8551   (interactive "r\nP")
8552   (save-excursion
8553     (let (article)
8554       (goto-char point)
8555       (beginning-of-line)
8556       (while (and
8557               (< (point) mark)
8558               (progn
8559                 (when (or all
8560                           (memq (setq article (gnus-summary-article-number))
8561                                 gnus-newsgroup-unreads))
8562                   (gnus-summary-mark-article article gnus-del-mark))
8563                 t)
8564               (gnus-summary-find-next))))))
8565
8566 (defun gnus-summary-mark-below (score mark)
8567   "Mark articles with score less than SCORE with MARK."
8568   (interactive "P\ncMark: ")
8569   (setq score (if score
8570                   (prefix-numeric-value score)
8571                 (or gnus-summary-default-score 0)))
8572   (save-excursion
8573     (set-buffer gnus-summary-buffer)
8574     (goto-char (point-min))
8575     (while
8576         (progn
8577           (and (< (gnus-summary-article-score) score)
8578                (gnus-summary-mark-article nil mark))
8579           (gnus-summary-find-next)))))
8580
8581 (defun gnus-summary-kill-below (&optional score)
8582   "Mark articles with score below SCORE as read."
8583   (interactive "P")
8584   (gnus-summary-mark-below score gnus-killed-mark))
8585
8586 (defun gnus-summary-clear-above (&optional score)
8587   "Clear all marks from articles with score above SCORE."
8588   (interactive "P")
8589   (gnus-summary-mark-above score gnus-unread-mark))
8590
8591 (defun gnus-summary-tick-above (&optional score)
8592   "Tick all articles with score above SCORE."
8593   (interactive "P")
8594   (gnus-summary-mark-above score gnus-ticked-mark))
8595
8596 (defun gnus-summary-mark-above (score mark)
8597   "Mark articles with score over SCORE with MARK."
8598   (interactive "P\ncMark: ")
8599   (setq score (if score
8600                   (prefix-numeric-value score)
8601                 (or gnus-summary-default-score 0)))
8602   (save-excursion
8603     (set-buffer gnus-summary-buffer)
8604     (goto-char (point-min))
8605     (while (and (progn
8606                   (when (> (gnus-summary-article-score) score)
8607                     (gnus-summary-mark-article nil mark))
8608                   t)
8609                 (gnus-summary-find-next)))))
8610
8611 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8612 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8613 (defun gnus-summary-limit-include-expunged (&optional no-error)
8614   "Display all the hidden articles that were expunged for low scores."
8615   (interactive)
8616   (let ((buffer-read-only nil))
8617     (let ((scored gnus-newsgroup-scored)
8618           headers h)
8619       (while scored
8620         (unless (gnus-summary-goto-subject (caar scored))
8621           (and (setq h (gnus-summary-article-header (caar scored)))
8622                (< (cdar scored) gnus-summary-expunge-below)
8623                (push h headers)))
8624         (setq scored (cdr scored)))
8625       (if (not headers)
8626           (when (not no-error)
8627             (error "No expunged articles hidden"))
8628         (goto-char (point-min))
8629         (gnus-summary-prepare-unthreaded (nreverse headers))
8630         (goto-char (point-min))
8631         (gnus-summary-position-point)
8632         t))))
8633
8634 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8635   "Mark all unread articles in this newsgroup as read.
8636 If prefix argument ALL is non-nil, ticked and dormant articles will
8637 also be marked as read.
8638 If QUIETLY is non-nil, no questions will be asked.
8639 If TO-HERE is non-nil, it should be a point in the buffer.  All
8640 articles before this point will be marked as read.
8641 Note that this function will only catch up the unread article
8642 in the current summary buffer limitation.
8643 The number of articles marked as read is returned."
8644   (interactive "P")
8645   (prog1
8646       (save-excursion
8647         (when (or quietly
8648                   (not gnus-interactive-catchup) ;Without confirmation?
8649                   gnus-expert-user
8650                   (gnus-y-or-n-p
8651                    (if all
8652                        "Mark absolutely all articles as read? "
8653                      "Mark all unread articles as read? ")))
8654           (if (and not-mark
8655                    (not gnus-newsgroup-adaptive)
8656                    (not gnus-newsgroup-auto-expire)
8657                    (not gnus-suppress-duplicates)
8658                    (or (not gnus-use-cache)
8659                        (eq gnus-use-cache 'passive)))
8660               (progn
8661                 (when all
8662                   (setq gnus-newsgroup-marked nil
8663                         gnus-newsgroup-dormant nil))
8664                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8665             ;; We actually mark all articles as canceled, which we
8666             ;; have to do when using auto-expiry or adaptive scoring.
8667             (gnus-summary-show-all-threads)
8668             (when (gnus-summary-first-subject (not all) t)
8669               (while (and
8670                       (if to-here (< (point) to-here) t)
8671                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8672                       (gnus-summary-find-next (not all) nil nil t))))
8673             (gnus-set-mode-line 'summary))
8674           t))
8675     (gnus-summary-position-point)))
8676
8677 (defun gnus-summary-catchup-to-here (&optional all)
8678   "Mark all unticked articles before the current one as read.
8679 If ALL is non-nil, also mark ticked and dormant articles as read."
8680   (interactive "P")
8681   (save-excursion
8682     (gnus-save-hidden-threads
8683       (let ((beg (point)))
8684         ;; We check that there are unread articles.
8685         (when (or all (gnus-summary-find-prev))
8686           (gnus-summary-catchup all t beg)))))
8687   (gnus-summary-position-point))
8688
8689 (defun gnus-summary-catchup-all (&optional quietly)
8690   "Mark all articles in this newsgroup as read."
8691   (interactive "P")
8692   (gnus-summary-catchup t quietly))
8693
8694 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8695   "Mark all unread articles in this group as read, then exit.
8696 If prefix argument ALL is non-nil, all articles are marked as read."
8697   (interactive "P")
8698   (when (gnus-summary-catchup all quietly nil 'fast)
8699     ;; Select next newsgroup or exit.
8700     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8701              (eq gnus-auto-select-next 'quietly))
8702         (gnus-summary-next-group nil)
8703       (gnus-summary-exit))))
8704
8705 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8706   "Mark all articles in this newsgroup as read, and then exit."
8707   (interactive "P")
8708   (gnus-summary-catchup-and-exit t quietly))
8709
8710 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8711   "Mark all articles in this group as read and select the next group.
8712 If given a prefix, mark all articles, unread as well as ticked, as
8713 read."
8714   (interactive "P")
8715   (save-excursion
8716     (gnus-summary-catchup all))
8717   (gnus-summary-next-group))
8718
8719 ;;;
8720 ;;; with article
8721 ;;;
8722
8723 (defmacro gnus-with-article (article &rest forms)
8724   "Select ARTICLE and perform FORMS in the original article buffer.
8725 Then replace the article with the result."
8726   `(progn
8727      ;; We don't want the article to be marked as read.
8728      (let (gnus-mark-article-hook)
8729        (gnus-summary-select-article t t nil ,article))
8730      (set-buffer gnus-original-article-buffer)
8731      ,@forms
8732      (if (not (gnus-check-backend-function
8733                'request-replace-article (car gnus-article-current)))
8734          (gnus-message 5 "Read-only group; not replacing")
8735        (unless (gnus-request-replace-article
8736                 ,article (car gnus-article-current)
8737                 (current-buffer) t)
8738          (error "Couldn't replace article")))
8739      ;; The cache and backlog have to be flushed somewhat.
8740      (when gnus-keep-backlog
8741        (gnus-backlog-remove-article
8742         (car gnus-article-current) (cdr gnus-article-current)))
8743      (when gnus-use-cache
8744        (gnus-cache-update-article
8745         (car gnus-article-current) (cdr gnus-article-current)))))
8746
8747 (put 'gnus-with-article 'lisp-indent-function 1)
8748 (put 'gnus-with-article 'edebug-form-spec '(form body))
8749
8750 ;; Thread-based commands.
8751
8752 (defun gnus-summary-articles-in-thread (&optional article)
8753   "Return a list of all articles in the current thread.
8754 If ARTICLE is non-nil, return all articles in the thread that starts
8755 with that article."
8756   (let* ((article (or article (gnus-summary-article-number)))
8757          (data (gnus-data-find-list article))
8758          (top-level (gnus-data-level (car data)))
8759          (top-subject
8760           (cond ((null gnus-thread-operation-ignore-subject)
8761                  (gnus-simplify-subject-re
8762                   (mail-header-subject (gnus-data-header (car data)))))
8763                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8764                  (gnus-simplify-subject-fuzzy
8765                   (mail-header-subject (gnus-data-header (car data)))))
8766                 (t nil)))
8767          (end-point (save-excursion
8768                       (if (gnus-summary-go-to-next-thread)
8769                           (point) (point-max))))
8770          articles)
8771     (while (and data
8772                 (< (gnus-data-pos (car data)) end-point))
8773       (when (or (not top-subject)
8774                 (string= top-subject
8775                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8776                              (gnus-simplify-subject-fuzzy
8777                               (mail-header-subject
8778                                (gnus-data-header (car data))))
8779                            (gnus-simplify-subject-re
8780                             (mail-header-subject
8781                              (gnus-data-header (car data)))))))
8782         (push (gnus-data-number (car data)) articles))
8783       (unless (and (setq data (cdr data))
8784                    (> (gnus-data-level (car data)) top-level))
8785         (setq data nil)))
8786     ;; Return the list of articles.
8787     (nreverse articles)))
8788
8789 (defun gnus-summary-rethread-current ()
8790   "Rethread the thread the current article is part of."
8791   (interactive)
8792   (let* ((gnus-show-threads t)
8793          (article (gnus-summary-article-number))
8794          (id (mail-header-id (gnus-summary-article-header)))
8795          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8796     (unless id
8797       (error "No article on the current line"))
8798     (gnus-rebuild-thread id)
8799     (gnus-summary-goto-subject article)))
8800
8801 (defun gnus-summary-reparent-thread ()
8802   "Make the current article child of the marked (or previous) article.
8803
8804 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8805 is non-nil or the Subject: of both articles are the same."
8806   (interactive)
8807   (unless (not (gnus-group-read-only-p))
8808     (error "The current newsgroup does not support article editing"))
8809   (unless (<= (length gnus-newsgroup-processable) 1)
8810     (error "No more than one article may be marked"))
8811   (save-window-excursion
8812     (let ((gnus-article-buffer " *reparent*")
8813           (current-article (gnus-summary-article-number))
8814           ;; First grab the marked article, otherwise one line up.
8815           (parent-article (if (not (null gnus-newsgroup-processable))
8816                               (car gnus-newsgroup-processable)
8817                             (save-excursion
8818                               (if (eq (forward-line -1) 0)
8819                                   (gnus-summary-article-number)
8820                                 (error "Beginning of summary buffer"))))))
8821       (unless (not (eq current-article parent-article))
8822         (error "An article may not be self-referential"))
8823       (let ((message-id (mail-header-id
8824                          (gnus-summary-article-header parent-article))))
8825         (unless (and message-id (not (equal message-id "")))
8826           (error "No message-id in desired parent"))
8827         (gnus-with-article current-article
8828           (save-restriction
8829             (goto-char (point-min))
8830             (message-narrow-to-head)
8831             (if (re-search-forward "^References: " nil t)
8832                 (progn
8833                   (re-search-forward "^[^ \t]" nil t)
8834                   (forward-line -1)
8835                   (end-of-line)
8836                   (insert " " message-id))
8837               (insert "References: " message-id "\n"))))
8838         (set-buffer gnus-summary-buffer)
8839         (gnus-summary-unmark-all-processable)
8840         (gnus-summary-update-article current-article)
8841         (gnus-summary-rethread-current)
8842         (gnus-message 3 "Article %d is now the child of article %d"
8843                       current-article parent-article)))))
8844
8845 (defun gnus-summary-toggle-threads (&optional arg)
8846   "Toggle showing conversation threads.
8847 If ARG is positive number, turn showing conversation threads on."
8848   (interactive "P")
8849   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8850     (setq gnus-show-threads
8851           (if (null arg) (not gnus-show-threads)
8852             (> (prefix-numeric-value arg) 0)))
8853     (gnus-summary-prepare)
8854     (gnus-summary-goto-subject current)
8855     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8856     (gnus-summary-position-point)))
8857
8858 (defun gnus-summary-show-all-threads ()
8859   "Show all threads."
8860   (interactive)
8861   (save-excursion
8862     (let ((buffer-read-only nil))
8863       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8864   (gnus-summary-position-point))
8865
8866 (defun gnus-summary-show-thread ()
8867   "Show thread subtrees.
8868 Returns nil if no thread was there to be shown."
8869   (interactive)
8870   (let ((buffer-read-only nil)
8871         (orig (point))
8872         ;; first goto end then to beg, to have point at beg after let
8873         (end (progn (end-of-line) (point)))
8874         (beg (progn (beginning-of-line) (point))))
8875     (prog1
8876         ;; Any hidden lines here?
8877         (search-forward "\r" end t)
8878       (subst-char-in-region beg end ?\^M ?\n t)
8879       (goto-char orig)
8880       (gnus-summary-position-point))))
8881
8882 (defun gnus-summary-hide-all-threads ()
8883   "Hide all thread subtrees."
8884   (interactive)
8885   (save-excursion
8886     (goto-char (point-min))
8887     (gnus-summary-hide-thread)
8888     (while (zerop (gnus-summary-next-thread 1 t))
8889       (gnus-summary-hide-thread)))
8890   (gnus-summary-position-point))
8891
8892 (defun gnus-summary-hide-thread ()
8893   "Hide thread subtrees.
8894 Returns nil if no threads were there to be hidden."
8895   (interactive)
8896   (let ((buffer-read-only nil)
8897         (start (point))
8898         (article (gnus-summary-article-number)))
8899     (goto-char start)
8900     ;; Go forward until either the buffer ends or the subthread
8901     ;; ends.
8902     (when (and (not (eobp))
8903                (or (zerop (gnus-summary-next-thread 1 t))
8904                    (goto-char (point-max))))
8905       (prog1
8906           (if (and (> (point) start)
8907                    (search-backward "\n" start t))
8908               (progn
8909                 (subst-char-in-region start (point) ?\n ?\^M)
8910                 (gnus-summary-goto-subject article))
8911             (goto-char start)
8912             nil)))))
8913
8914 (defun gnus-summary-go-to-next-thread (&optional previous)
8915   "Go to the same level (or less) next thread.
8916 If PREVIOUS is non-nil, go to previous thread instead.
8917 Return the article number moved to, or nil if moving was impossible."
8918   (let ((level (gnus-summary-thread-level))
8919         (way (if previous -1 1))
8920         (beg (point)))
8921     (forward-line way)
8922     (while (and (not (eobp))
8923                 (< level (gnus-summary-thread-level)))
8924       (forward-line way))
8925     (if (eobp)
8926         (progn
8927           (goto-char beg)
8928           nil)
8929       (setq beg (point))
8930       (prog1
8931           (gnus-summary-article-number)
8932         (goto-char beg)))))
8933
8934 (defun gnus-summary-next-thread (n &optional silent)
8935   "Go to the same level next N'th thread.
8936 If N is negative, search backward instead.
8937 Returns the difference between N and the number of skips actually
8938 done.
8939
8940 If SILENT, don't output messages."
8941   (interactive "p")
8942   (let ((backward (< n 0))
8943         (n (abs n)))
8944     (while (and (> n 0)
8945                 (gnus-summary-go-to-next-thread backward))
8946       (decf n))
8947     (unless silent
8948       (gnus-summary-position-point))
8949     (when (and (not silent) (/= 0 n))
8950       (gnus-message 7 "No more threads"))
8951     n))
8952
8953 (defun gnus-summary-prev-thread (n)
8954   "Go to the same level previous N'th thread.
8955 Returns the difference between N and the number of skips actually
8956 done."
8957   (interactive "p")
8958   (gnus-summary-next-thread (- n)))
8959
8960 (defun gnus-summary-go-down-thread ()
8961   "Go down one level in the current thread."
8962   (let ((children (gnus-summary-article-children)))
8963     (when children
8964       (gnus-summary-goto-subject (car children)))))
8965
8966 (defun gnus-summary-go-up-thread ()
8967   "Go up one level in the current thread."
8968   (let ((parent (gnus-summary-article-parent)))
8969     (when parent
8970       (gnus-summary-goto-subject parent))))
8971
8972 (defun gnus-summary-down-thread (n)
8973   "Go down thread N steps.
8974 If N is negative, go up instead.
8975 Returns the difference between N and how many steps down that were
8976 taken."
8977   (interactive "p")
8978   (let ((up (< n 0))
8979         (n (abs n)))
8980     (while (and (> n 0)
8981                 (if up (gnus-summary-go-up-thread)
8982                   (gnus-summary-go-down-thread)))
8983       (setq n (1- n)))
8984     (gnus-summary-position-point)
8985     (when (/= 0 n)
8986       (gnus-message 7 "Can't go further"))
8987     n))
8988
8989 (defun gnus-summary-up-thread (n)
8990   "Go up thread N steps.
8991 If N is negative, go up instead.
8992 Returns the difference between N and how many steps down that were
8993 taken."
8994   (interactive "p")
8995   (gnus-summary-down-thread (- n)))
8996
8997 (defun gnus-summary-top-thread ()
8998   "Go to the top of the thread."
8999   (interactive)
9000   (while (gnus-summary-go-up-thread))
9001   (gnus-summary-article-number))
9002
9003 (defun gnus-summary-kill-thread (&optional unmark)
9004   "Mark articles under current thread as read.
9005 If the prefix argument is positive, remove any kinds of marks.
9006 If the prefix argument is negative, tick articles instead."
9007   (interactive "P")
9008   (when unmark
9009     (setq unmark (prefix-numeric-value unmark)))
9010   (let ((articles (gnus-summary-articles-in-thread)))
9011     (save-excursion
9012       ;; Expand the thread.
9013       (gnus-summary-show-thread)
9014       ;; Mark all the articles.
9015       (while articles
9016         (gnus-summary-goto-subject (car articles))
9017         (cond ((null unmark)
9018                (gnus-summary-mark-article-as-read gnus-killed-mark))
9019               ((> unmark 0)
9020                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9021               (t
9022                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9023         (setq articles (cdr articles))))
9024     ;; Hide killed subtrees.
9025     (and (null unmark)
9026          gnus-thread-hide-killed
9027          (gnus-summary-hide-thread))
9028     ;; If marked as read, go to next unread subject.
9029     (when (null unmark)
9030       ;; Go to next unread subject.
9031       (gnus-summary-next-subject 1 t)))
9032   (gnus-set-mode-line 'summary))
9033
9034 ;; Summary sorting commands
9035
9036 (defun gnus-summary-sort-by-number (&optional reverse)
9037   "Sort the summary buffer by article number.
9038 Argument REVERSE means reverse order."
9039   (interactive "P")
9040   (gnus-summary-sort 'number reverse))
9041
9042 (defun gnus-summary-sort-by-author (&optional reverse)
9043   "Sort the summary buffer by author name alphabetically.
9044 If `case-fold-search' is non-nil, case of letters is ignored.
9045 Argument REVERSE means reverse order."
9046   (interactive "P")
9047   (gnus-summary-sort 'author reverse))
9048
9049 (defun gnus-summary-sort-by-subject (&optional reverse)
9050   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9051 If `case-fold-search' is non-nil, case of letters is ignored.
9052 Argument REVERSE means reverse order."
9053   (interactive "P")
9054   (gnus-summary-sort 'subject reverse))
9055
9056 (defun gnus-summary-sort-by-date (&optional reverse)
9057   "Sort the summary buffer by date.
9058 Argument REVERSE means reverse order."
9059   (interactive "P")
9060   (gnus-summary-sort 'date reverse))
9061
9062 (defun gnus-summary-sort-by-score (&optional reverse)
9063   "Sort the summary buffer by score.
9064 Argument REVERSE means reverse order."
9065   (interactive "P")
9066   (gnus-summary-sort 'score reverse))
9067
9068 (defun gnus-summary-sort-by-lines (&optional reverse)
9069   "Sort the summary buffer by the number of lines.
9070 Argument REVERSE means reverse order."
9071   (interactive "P")
9072   (gnus-summary-sort 'lines reverse))
9073
9074 (defun gnus-summary-sort-by-chars (&optional reverse)
9075   "Sort the summary buffer by article length.
9076 Argument REVERSE means reverse order."
9077   (interactive "P")
9078   (gnus-summary-sort 'chars reverse))
9079
9080 (defun gnus-summary-sort (predicate reverse)
9081   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9082   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9083          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9084          (gnus-thread-sort-functions
9085           (if (not reverse)
9086               thread
9087             `(lambda (t1 t2)
9088                (,thread t2 t1))))
9089          (gnus-sort-gathered-threads-function
9090           gnus-thread-sort-functions)
9091          (gnus-article-sort-functions
9092           (if (not reverse)
9093               article
9094             `(lambda (t1 t2)
9095                (,article t2 t1))))
9096          (buffer-read-only)
9097          (gnus-summary-prepare-hook nil))
9098     ;; We do the sorting by regenerating the threads.
9099     (gnus-summary-prepare)
9100     ;; Hide subthreads if needed.
9101     (when (and gnus-show-threads gnus-thread-hide-subtree)
9102       (gnus-summary-hide-all-threads))))
9103
9104 ;; Summary saving commands.
9105
9106 (defun gnus-summary-save-article (&optional n not-saved)
9107   "Save the current article using the default saver function.
9108 If N is a positive number, save the N next articles.
9109 If N is a negative number, save the N previous articles.
9110 If N is nil and any articles have been marked with the process mark,
9111 save those articles instead.
9112 The variable `gnus-default-article-saver' specifies the saver function."
9113   (interactive "P")
9114   (let* ((articles (gnus-summary-work-articles n))
9115          (save-buffer (save-excursion
9116                         (nnheader-set-temp-buffer " *Gnus Save*")))
9117          (num (length articles))
9118          header file)
9119     (dolist (article articles)
9120       (setq header (gnus-summary-article-header article))
9121       (if (not (vectorp header))
9122           ;; This is a pseudo-article.
9123           (if (assq 'name header)
9124               (gnus-copy-file (cdr (assq 'name header)))
9125             (gnus-message 1 "Article %d is unsaveable" article))
9126         ;; This is a real article.
9127         (save-window-excursion
9128           (gnus-summary-select-article t nil nil article))
9129         (save-excursion
9130           (set-buffer save-buffer)
9131           (erase-buffer)
9132           (insert-buffer-substring gnus-original-article-buffer))
9133         (setq file (gnus-article-save save-buffer file num))
9134         (gnus-summary-remove-process-mark article)
9135         (unless not-saved
9136           (gnus-summary-set-saved-mark article))))
9137     (gnus-kill-buffer save-buffer)
9138     (gnus-summary-position-point)
9139     (gnus-set-mode-line 'summary)
9140     n))
9141
9142 (defun gnus-summary-pipe-output (&optional arg)
9143   "Pipe the current article to a subprocess.
9144 If N is a positive number, pipe the N next articles.
9145 If N is a negative number, pipe the N previous articles.
9146 If N is nil and any articles have been marked with the process mark,
9147 pipe those articles instead."
9148   (interactive "P")
9149   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9150     (gnus-summary-save-article arg t))
9151   (gnus-configure-windows 'pipe))
9152
9153 (defun gnus-summary-save-article-mail (&optional arg)
9154   "Append the current article to an mail file.
9155 If N is a positive number, save the N next articles.
9156 If N is a negative number, save the N previous articles.
9157 If N is nil and any articles have been marked with the process mark,
9158 save those articles instead."
9159   (interactive "P")
9160   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9161     (gnus-summary-save-article arg)))
9162
9163 (defun gnus-summary-save-article-rmail (&optional arg)
9164   "Append the current article to an rmail file.
9165 If N is a positive number, save the N next articles.
9166 If N is a negative number, save the N previous articles.
9167 If N is nil and any articles have been marked with the process mark,
9168 save those articles instead."
9169   (interactive "P")
9170   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9171     (gnus-summary-save-article arg)))
9172
9173 (defun gnus-summary-save-article-file (&optional arg)
9174   "Append the current article to a file.
9175 If N is a positive number, save the N next articles.
9176 If N is a negative number, save the N previous articles.
9177 If N is nil and any articles have been marked with the process mark,
9178 save those articles instead."
9179   (interactive "P")
9180   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9181     (gnus-summary-save-article arg)))
9182
9183 (defun gnus-summary-write-article-file (&optional arg)
9184   "Write the current article to a file, deleting the previous file.
9185 If N is a positive number, save the N next articles.
9186 If N is a negative number, save the N previous articles.
9187 If N is nil and any articles have been marked with the process mark,
9188 save those articles instead."
9189   (interactive "P")
9190   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9191     (gnus-summary-save-article arg)))
9192
9193 (defun gnus-summary-save-article-body-file (&optional arg)
9194   "Append the current article body to a file.
9195 If N is a positive number, save the N next articles.
9196 If N is a negative number, save the N previous articles.
9197 If N is nil and any articles have been marked with the process mark,
9198 save those articles instead."
9199   (interactive "P")
9200   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9201     (gnus-summary-save-article arg)))
9202
9203 (defun gnus-summary-pipe-message (program)
9204   "Pipe the current article through PROGRAM."
9205   (interactive "sProgram: ")
9206   (gnus-summary-select-article)
9207   (let ((mail-header-separator ""))
9208     (gnus-eval-in-buffer-window gnus-article-buffer
9209       (save-restriction
9210         (widen)
9211         (let ((start (window-start))
9212               buffer-read-only)
9213           (message-pipe-buffer-body program)
9214           (set-window-start (get-buffer-window (current-buffer)) start))))))
9215
9216 (defun gnus-get-split-value (methods)
9217   "Return a value based on the split METHODS."
9218   (let (split-name method result match)
9219     (when methods
9220       (save-excursion
9221         (set-buffer gnus-original-article-buffer)
9222         (save-restriction
9223           (nnheader-narrow-to-headers)
9224           (while (and methods (not split-name))
9225             (goto-char (point-min))
9226             (setq method (pop methods))
9227             (setq match (car method))
9228             (when (cond
9229                    ((stringp match)
9230                     ;; Regular expression.
9231                     (ignore-errors
9232                       (re-search-forward match nil t)))
9233                    ((gnus-functionp match)
9234                     ;; Function.
9235                     (save-restriction
9236                       (widen)
9237                       (setq result (funcall match gnus-newsgroup-name))))
9238                    ((consp match)
9239                     ;; Form.
9240                     (save-restriction
9241                       (widen)
9242                       (setq result (eval match)))))
9243               (setq split-name (cdr method))
9244               (cond ((stringp result)
9245                      (push (expand-file-name
9246                             result gnus-article-save-directory)
9247                            split-name))
9248                     ((consp result)
9249                      (setq split-name (append result split-name)))))))))
9250     (nreverse split-name)))
9251
9252 (defun gnus-valid-move-group-p (group)
9253   (and (boundp group)
9254        (symbol-name group)
9255        (symbol-value group)
9256        (gnus-get-function (gnus-find-method-for-group
9257                            (symbol-name group)) 'request-accept-article t)))
9258
9259 (defun gnus-read-move-group-name (prompt default articles prefix)
9260   "Read a group name."
9261   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9262          (minibuffer-confirm-incomplete nil) ; XEmacs
9263          (prom
9264           (format "%s %s to:"
9265                   prompt
9266                   (if (> (length articles) 1)
9267                       (format "these %d articles" (length articles))
9268                     "this article")))
9269          (to-newsgroup
9270           (cond
9271            ((null split-name)
9272             (gnus-completing-read default prom
9273                                   gnus-active-hashtb
9274                                   'gnus-valid-move-group-p
9275                                   nil prefix
9276                                   'gnus-group-history))
9277            ((= 1 (length split-name))
9278             (gnus-completing-read (car split-name) prom
9279                                   gnus-active-hashtb
9280                                   'gnus-valid-move-group-p
9281                                   nil nil
9282                                   'gnus-group-history))
9283            (t
9284             (gnus-completing-read nil prom
9285                                   (mapcar (lambda (el) (list el))
9286                                           (nreverse split-name))
9287                                   nil nil nil
9288                                   'gnus-group-history))))
9289          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9290     (when to-newsgroup
9291       (if (or (string= to-newsgroup "")
9292               (string= to-newsgroup prefix))
9293           (setq to-newsgroup default))
9294       (unless to-newsgroup
9295         (error "No group name entered"))
9296       (or (gnus-active to-newsgroup)
9297           (gnus-activate-group to-newsgroup nil nil to-method)
9298           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9299                                      to-newsgroup))
9300               (or (and (gnus-request-create-group to-newsgroup to-method)
9301                        (gnus-activate-group
9302                         to-newsgroup nil nil to-method)
9303                        (gnus-subscribe-group to-newsgroup))
9304                   (error "Couldn't create group %s" to-newsgroup)))
9305           (error "No such group: %s" to-newsgroup)))
9306     to-newsgroup))
9307
9308 (defun gnus-summary-save-parts (type dir n &optional reverse)
9309   "Save parts matching TYPE to DIR.
9310 If REVERSE, save parts that do not match TYPE."
9311   (interactive
9312    (list (read-string "Save parts of type: " 
9313                       (or (car gnus-summary-save-parts-type-history)
9314                           gnus-summary-save-parts-default-mime)
9315                       'gnus-summary-save-parts-type-history)
9316          (setq gnus-summary-save-parts-last-directory
9317                (read-file-name "Save to directory: " 
9318                                gnus-summary-save-parts-last-directory
9319                                nil t))
9320          current-prefix-arg))
9321   (gnus-summary-iterate n
9322     (let ((gnus-display-mime-function nil)
9323           (gnus-inhibit-treatment t))
9324       (gnus-summary-select-article))
9325     (save-excursion
9326       (set-buffer gnus-article-buffer)
9327       (let ((handles (or gnus-article-mime-handles
9328                          (mm-dissect-buffer) (mm-uu-dissect))))
9329         (when handles
9330           (gnus-summary-save-parts-1 type dir handles reverse)
9331           (unless gnus-article-mime-handles ;; Don't destroy this case.
9332             (mm-destroy-parts handles)))))))
9333
9334 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9335   (if (stringp (car handle))
9336       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9337               (cdr handle))
9338     (when (if reverse
9339               (not (string-match type (mm-handle-media-type handle)))
9340             (string-match type (mm-handle-media-type handle)))
9341       (let ((file (expand-file-name
9342                    (file-name-nondirectory
9343                     (or
9344                      (mail-content-type-get
9345                       (mm-handle-disposition handle) 'filename)
9346                      (concat gnus-newsgroup-name
9347                              "." (number-to-string
9348                                   (cdr gnus-article-current)))))
9349                    dir)))
9350         (unless (file-exists-p file)
9351           (mm-save-part-to-file handle file))))))
9352
9353 ;; Summary extract commands
9354
9355 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9356   (let ((buffer-read-only nil)
9357         (article (gnus-summary-article-number))
9358         after-article b e)
9359     (unless (gnus-summary-goto-subject article)
9360       (error "No such article: %d" article))
9361     (gnus-summary-position-point)
9362     ;; If all commands are to be bunched up on one line, we collect
9363     ;; them here.
9364     (unless gnus-view-pseudos-separately
9365       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9366             files action)
9367         (while ps
9368           (setq action (cdr (assq 'action (car ps))))
9369           (setq files (list (cdr (assq 'name (car ps)))))
9370           (while (and ps (cdr ps)
9371                       (string= (or action "1")
9372                                (or (cdr (assq 'action (cadr ps))) "2")))
9373             (push (cdr (assq 'name (cadr ps))) files)
9374             (setcdr ps (cddr ps)))
9375           (when files
9376             (when (not (string-match "%s" action))
9377               (push " " files))
9378             (push " " files)
9379             (when (assq 'execute (car ps))
9380               (setcdr (assq 'execute (car ps))
9381                       (funcall (if (string-match "%s" action)
9382                                    'format 'concat)
9383                                action
9384                                (mapconcat
9385                                 (lambda (f)
9386                                   (if (equal f " ")
9387                                       f
9388                                     (mm-quote-arg f)))
9389                                 files " ")))))
9390           (setq ps (cdr ps)))))
9391     (if (and gnus-view-pseudos (not not-view))
9392         (while pslist
9393           (when (assq 'execute (car pslist))
9394             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9395                                   (eq gnus-view-pseudos 'not-confirm)))
9396           (setq pslist (cdr pslist)))
9397       (save-excursion
9398         (while pslist
9399           (setq after-article (or (cdr (assq 'article (car pslist)))
9400                                   (gnus-summary-article-number)))
9401           (gnus-summary-goto-subject after-article)
9402           (forward-line 1)
9403           (setq b (point))
9404           (insert "    " (file-name-nondirectory
9405                           (cdr (assq 'name (car pslist))))
9406                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9407           (setq e (point))
9408           (forward-line -1)             ; back to `b'
9409           (gnus-add-text-properties
9410            b (1- e) (list 'gnus-number gnus-reffed-article-number
9411                           gnus-mouse-face-prop gnus-mouse-face))
9412           (gnus-data-enter
9413            after-article gnus-reffed-article-number
9414            gnus-unread-mark b (car pslist) 0 (- e b))
9415           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9416           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9417           (setq pslist (cdr pslist)))))))
9418
9419 (defun gnus-pseudos< (p1 p2)
9420   (let ((c1 (cdr (assq 'action p1)))
9421         (c2 (cdr (assq 'action p2))))
9422     (and c1 c2 (string< c1 c2))))
9423
9424 (defun gnus-request-pseudo-article (props)
9425   (cond ((assq 'execute props)
9426          (gnus-execute-command (cdr (assq 'execute props)))))
9427   (let ((gnus-current-article (gnus-summary-article-number)))
9428     (gnus-run-hooks 'gnus-mark-article-hook)))
9429
9430 (defun gnus-execute-command (command &optional automatic)
9431   (save-excursion
9432     (gnus-article-setup-buffer)
9433     (set-buffer gnus-article-buffer)
9434     (setq buffer-read-only nil)
9435     (let ((command (if automatic command
9436                      (read-string "Command: " (cons command 0)))))
9437       (erase-buffer)
9438       (insert "$ " command "\n\n")
9439       (if gnus-view-pseudo-asynchronously
9440           (start-process "gnus-execute" (current-buffer) shell-file-name
9441                          shell-command-switch command)
9442         (call-process shell-file-name nil t nil
9443                       shell-command-switch command)))))
9444
9445 ;; Summary kill commands.
9446
9447 (defun gnus-summary-edit-global-kill (article)
9448   "Edit the \"global\" kill file."
9449   (interactive (list (gnus-summary-article-number)))
9450   (gnus-group-edit-global-kill article))
9451
9452 (defun gnus-summary-edit-local-kill ()
9453   "Edit a local kill file applied to the current newsgroup."
9454   (interactive)
9455   (setq gnus-current-headers (gnus-summary-article-header))
9456   (gnus-group-edit-local-kill
9457    (gnus-summary-article-number) gnus-newsgroup-name))
9458
9459 ;;; Header reading.
9460
9461 (defun gnus-read-header (id &optional header)
9462   "Read the headers of article ID and enter them into the Gnus system."
9463   (let ((group gnus-newsgroup-name)
9464         (gnus-override-method
9465          (or
9466           gnus-override-method
9467           (and (gnus-news-group-p gnus-newsgroup-name)
9468                (car (gnus-refer-article-methods)))))
9469         where)
9470     ;; First we check to see whether the header in question is already
9471     ;; fetched.
9472     (if (stringp id)
9473         ;; This is a Message-ID.
9474         (setq header (or header (gnus-id-to-header id)))
9475       ;; This is an article number.
9476       (setq header (or header (gnus-summary-article-header id))))
9477     (if (and header
9478              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9479         ;; We have found the header.
9480         header
9481       ;; If this is a sparse article, we have to nix out its
9482       ;; previous entry in the thread hashtb.
9483       (when (and header
9484                  (gnus-summary-article-sparse-p (mail-header-number header)))
9485         (let* ((parent (gnus-parent-id (mail-header-references header)))
9486                (thread (and parent (gnus-id-to-thread parent))))
9487           (when thread
9488             (delq (assq header thread) thread))))
9489       ;; We have to really fetch the header to this article.
9490       (save-excursion
9491         (set-buffer nntp-server-buffer)
9492         (when (setq where (gnus-request-head id group))
9493           (nnheader-fold-continuation-lines)
9494           (goto-char (point-max))
9495           (insert ".\n")
9496           (goto-char (point-min))
9497           (insert "211 ")
9498           (princ (cond
9499                   ((numberp id) id)
9500                   ((cdr where) (cdr where))
9501                   (header (mail-header-number header))
9502                   (t gnus-reffed-article-number))
9503                  (current-buffer))
9504           (insert " Article retrieved.\n"))
9505         (if (or (not where)
9506                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9507             ()                          ; Malformed head.
9508           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9509             (when (and (stringp id)
9510                        (not (string= (gnus-group-real-name group)
9511                                      (car where))))
9512               ;; If we fetched by Message-ID and the article came
9513               ;; from a different group, we fudge some bogus article
9514               ;; numbers for this article.
9515               (mail-header-set-number header gnus-reffed-article-number))
9516             (save-excursion
9517               (set-buffer gnus-summary-buffer)
9518               (decf gnus-reffed-article-number)
9519               (gnus-remove-header (mail-header-number header))
9520               (push header gnus-newsgroup-headers)
9521               (setq gnus-current-headers header)
9522               (push (mail-header-number header) gnus-newsgroup-limit)))
9523           header)))))
9524
9525 (defun gnus-remove-header (number)
9526   "Remove header NUMBER from `gnus-newsgroup-headers'."
9527   (if (and gnus-newsgroup-headers
9528            (= number (mail-header-number (car gnus-newsgroup-headers))))
9529       (pop gnus-newsgroup-headers)
9530     (let ((headers gnus-newsgroup-headers))
9531       (while (and (cdr headers)
9532                   (not (= number (mail-header-number (cadr headers)))))
9533         (pop headers))
9534       (when (cdr headers)
9535         (setcdr headers (cddr headers))))))
9536
9537 ;;;
9538 ;;; summary highlights
9539 ;;;
9540
9541 (defun gnus-highlight-selected-summary ()
9542   "Highlight selected article in summary buffer."
9543   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9544   (when gnus-summary-selected-face
9545     (save-excursion
9546       (let* ((beg (progn (beginning-of-line) (point)))
9547              (end (progn (end-of-line) (point)))
9548              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9549              (from (if (get-text-property beg gnus-mouse-face-prop)
9550                        beg
9551                      (or (next-single-property-change
9552                           beg gnus-mouse-face-prop nil end)
9553                          beg)))
9554              (to
9555               (if (= from end)
9556                   (- from 2)
9557                 (or (next-single-property-change
9558                      from gnus-mouse-face-prop nil end)
9559                     end))))
9560         ;; If no mouse-face prop on line we will have to = from = end,
9561         ;; so we highlight the entire line instead.
9562         (when (= (+ to 2) from)
9563           (setq from beg)
9564           (setq to end))
9565         (if gnus-newsgroup-selected-overlay
9566             ;; Move old overlay.
9567             (gnus-move-overlay
9568              gnus-newsgroup-selected-overlay from to (current-buffer))
9569           ;; Create new overlay.
9570           (gnus-overlay-put
9571            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9572            'face gnus-summary-selected-face))))))
9573
9574 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9575 (defun gnus-summary-highlight-line ()
9576   "Highlight current line according to `gnus-summary-highlight'."
9577   (let* ((list gnus-summary-highlight)
9578          (p (point))
9579          (end (progn (end-of-line) (point)))
9580          ;; now find out where the line starts and leave point there.
9581          (beg (progn (beginning-of-line) (point)))
9582          (article (gnus-summary-article-number))
9583          (score (or (cdr (assq (or article gnus-current-article)
9584                                gnus-newsgroup-scored))
9585                     gnus-summary-default-score 0))
9586          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9587          (inhibit-read-only t))
9588     ;; Eval the cars of the lists until we find a match.
9589     (let ((default gnus-summary-default-score))
9590       (while (and list
9591                   (not (eval (caar list))))
9592         (setq list (cdr list))))
9593     (let ((face (cdar list)))
9594       (unless (eq face (get-text-property beg 'face))
9595         (gnus-put-text-property-excluding-characters-with-faces
9596          beg end 'face
9597          (setq face (if (boundp face) (symbol-value face) face)))
9598         (when gnus-summary-highlight-line-function
9599           (funcall gnus-summary-highlight-line-function article face))))
9600     (goto-char p)))
9601
9602 (defun gnus-update-read-articles (group unread &optional compute)
9603   "Update the list of read articles in GROUP."
9604   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9605          (entry (gnus-gethash group gnus-newsrc-hashtb))
9606          (info (nth 2 entry))
9607          (prev 1)
9608          (unread (sort (copy-sequence unread) '<))
9609          read)
9610     (if (or (not info) (not active))
9611         ;; There is no info on this group if it was, in fact,
9612         ;; killed.  Gnus stores no information on killed groups, so
9613         ;; there's nothing to be done.
9614         ;; One could store the information somewhere temporarily,
9615         ;; perhaps...  Hmmm...
9616         ()
9617       ;; Remove any negative articles numbers.
9618       (while (and unread (< (car unread) 0))
9619         (setq unread (cdr unread)))
9620       ;; Remove any expired article numbers
9621       (while (and unread (< (car unread) (car active)))
9622         (setq unread (cdr unread)))
9623       ;; Compute the ranges of read articles by looking at the list of
9624       ;; unread articles.
9625       (while unread
9626         (when (/= (car unread) prev)
9627           (push (if (= prev (1- (car unread))) prev
9628                   (cons prev (1- (car unread))))
9629                 read))
9630         (setq prev (1+ (car unread)))
9631         (setq unread (cdr unread)))
9632       (when (<= prev (cdr active))
9633         (push (cons prev (cdr active)) read))
9634       (setq read (if (> (length read) 1) (nreverse read) read))
9635       (if compute
9636           read
9637         (save-excursion
9638           (let (setmarkundo)
9639             ;; Propagate the read marks to the backend.
9640             (when (gnus-check-backend-function 'request-set-mark group)
9641               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9642                     (add (gnus-remove-from-range read (gnus-info-read info))))
9643                 (when (or add del)
9644                   (unless (gnus-check-group group)
9645                     (error "Can't open server for %s" group))
9646                   (gnus-request-set-mark
9647                    group (delq nil (list (if add (list add 'add '(read)))
9648                                          (if del (list del 'del '(read))))))
9649                   (setq setmarkundo
9650                         `(gnus-request-set-mark
9651                           ,group
9652                           ',(delq nil (list
9653                                        (if del (list del 'add '(read)))
9654                                        (if add (list add 'del '(read))))))))))
9655             (set-buffer gnus-group-buffer)
9656             (gnus-undo-register
9657               `(progn
9658                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9659                  (gnus-info-set-read ',info ',(gnus-info-read info))
9660                  (gnus-get-unread-articles-in-group ',info 
9661                                                     (gnus-active ,group))
9662                  (gnus-group-update-group ,group t)
9663                  ,setmarkundo))))
9664         ;; Enter this list into the group info.
9665         (gnus-info-set-read info read)
9666         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9667         (gnus-get-unread-articles-in-group info (gnus-active group))
9668         t))))
9669
9670 (defun gnus-offer-save-summaries ()
9671   "Offer to save all active summary buffers."
9672   (save-excursion
9673     (let ((buflist (buffer-list))
9674           buffers bufname)
9675       ;; Go through all buffers and find all summaries.
9676       (while buflist
9677         (and (setq bufname (buffer-name (car buflist)))
9678              (string-match "Summary" bufname)
9679              (save-excursion
9680                (set-buffer bufname)
9681                ;; We check that this is, indeed, a summary buffer.
9682                (and (eq major-mode 'gnus-summary-mode)
9683                     ;; Also make sure this isn't bogus.
9684                     gnus-newsgroup-prepared
9685                     ;; Also make sure that this isn't a dead summary buffer.
9686                     (not gnus-dead-summary-mode)))
9687              (push bufname buffers))
9688         (setq buflist (cdr buflist)))
9689       ;; Go through all these summary buffers and offer to save them.
9690       (when buffers
9691         (map-y-or-n-p
9692          "Update summary buffer %s? "
9693          (lambda (buf)
9694            (switch-to-buffer buf)
9695            (gnus-summary-exit))
9696          buffers)))))
9697
9698 (defun gnus-summary-setup-default-charset ()
9699   "Setup newsgroup default charset."
9700   (if (equal gnus-newsgroup-name "nndraft:drafts")
9701       (setq gnus-newsgroup-charset nil)
9702     (let* ((name (and gnus-newsgroup-name
9703                       (gnus-group-real-name gnus-newsgroup-name)))
9704            (ignored-charsets
9705             (or gnus-newsgroup-ephemeral-ignored-charsets
9706                 (append
9707                  (and gnus-newsgroup-name
9708                       (or (gnus-group-find-parameter gnus-newsgroup-name
9709                                                      'ignored-charsets t)
9710                           (let ((alist gnus-group-ignored-charsets-alist)
9711                                 elem (charsets nil))
9712                             (while (setq elem (pop alist))
9713                               (when (and name
9714                                          (string-match (car elem) name))
9715                                 (setq alist nil
9716                                       charsets (cdr elem))))
9717                             charsets)))
9718                  gnus-newsgroup-ignored-charsets))))
9719       (setq gnus-newsgroup-charset
9720             (or gnus-newsgroup-ephemeral-charset
9721                 (and gnus-newsgroup-name
9722                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9723                          (let ((alist gnus-group-charset-alist)
9724                                elem charset)
9725                            (while (setq elem (pop alist))
9726                              (when (and name
9727                                         (string-match (car elem) name))
9728                                (setq alist nil
9729                                      charset (cadr elem))))
9730                            charset)))
9731                 gnus-default-charset))
9732       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9733            ignored-charsets))))
9734
9735 ;;;
9736 ;;; Mime Commands
9737 ;;;
9738
9739 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9740   "Display the current article buffer fully MIME-buttonized.
9741 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9742 treated as multipart/mixed."
9743   (interactive "P")
9744   (require 'gnus-art)
9745   (let ((gnus-unbuttonized-mime-types nil)
9746         (gnus-mime-display-multipart-as-mixed show-all-parts))
9747     (gnus-summary-show-article)))
9748
9749 (defun gnus-summary-repair-multipart (article)
9750   "Add a Content-Type header to a multipart article without one."
9751   (interactive (list (gnus-summary-article-number)))
9752   (gnus-with-article article
9753     (message-narrow-to-head)
9754     (goto-char (point-max))
9755     (widen)
9756     (when (search-forward "\n--" nil t)
9757       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9758         (message-narrow-to-head)
9759         (message-remove-header "Mime-Version")
9760         (message-remove-header "Content-Type")
9761         (goto-char (point-max))
9762         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9763                         separator))
9764         (insert "Mime-Version: 1.0\n")
9765         (widen))))
9766   (let (gnus-mark-article-hook)
9767     (gnus-summary-select-article t t nil article)))
9768
9769 (defun gnus-summary-toggle-display-buttonized ()
9770   "Toggle the buttonizing of the article buffer."
9771   (interactive)
9772   (require 'gnus-art)
9773   (if (setq gnus-inhibit-mime-unbuttonizing
9774             (not gnus-inhibit-mime-unbuttonizing))
9775       (let ((gnus-unbuttonized-mime-types nil))
9776         (gnus-summary-show-article))
9777     (gnus-summary-show-article)))
9778
9779 ;;;
9780 ;;; Generic summary marking commands
9781 ;;;
9782
9783 (defvar gnus-summary-marking-alist
9784   '((read gnus-del-mark "d")
9785     (unread gnus-unread-mark "u")
9786     (ticked gnus-ticked-mark "!")
9787     (dormant gnus-dormant-mark "?")
9788     (expirable gnus-expirable-mark "e"))
9789   "An alist of names/marks/keystrokes.")
9790
9791 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9792 (defvar gnus-summary-mark-map)
9793
9794 (defun gnus-summary-make-all-marking-commands ()
9795   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9796   (dolist (elem gnus-summary-marking-alist)
9797     (apply 'gnus-summary-make-marking-command elem)))
9798
9799 (defun gnus-summary-make-marking-command (name mark keystroke)
9800   (let ((map (make-sparse-keymap)))
9801     (define-key gnus-summary-generic-mark-map keystroke map)
9802     (dolist (lway `((next "next" next nil "n")
9803                     (next-unread "next unread" next t "N")
9804                     (prev "previous" prev nil "p")
9805                     (prev-unread "previous unread" prev t "P")
9806                     (nomove "" nil nil ,keystroke)))
9807       (let ((func (gnus-summary-make-marking-command-1
9808                    mark (car lway) lway name)))
9809         (setq func (eval func))
9810         (define-key map (nth 4 lway) func)))))
9811       
9812 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9813   `(defun ,(intern
9814             (format "gnus-summary-put-mark-as-%s%s"
9815                     name (if (eq way 'nomove)
9816                              ""
9817                            (concat "-" (symbol-name way)))))
9818      (n)
9819      ,(format
9820        "Mark the current article as %s%s.
9821 If N, the prefix, then repeat N times.
9822 If N is negative, move in reverse order.
9823 The difference between N and the actual number of articles marked is
9824 returned."
9825        name (cadr lway))
9826      (interactive "p")
9827      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9828     
9829 (defun gnus-summary-generic-mark (n mark move unread)
9830   "Mark N articles with MARK."
9831   (unless (eq major-mode 'gnus-summary-mode)
9832     (error "This command can only be used in the summary buffer"))
9833   (gnus-summary-show-thread)
9834   (let ((nummove
9835          (cond
9836           ((eq move 'next) 1)
9837           ((eq move 'prev) -1)
9838           (t 0))))
9839     (if (zerop nummove)
9840         (setq n 1)
9841       (when (< n 0)
9842         (setq n (abs n)
9843               nummove (* -1 nummove))))
9844     (while (and (> n 0)
9845                 (gnus-summary-mark-article nil mark)
9846                 (zerop (gnus-summary-next-subject nummove unread t)))
9847       (setq n (1- n)))
9848     (when (/= 0 n)
9849       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9850     (gnus-summary-recenter)
9851     (gnus-summary-position-point)
9852     (gnus-set-mode-line 'summary)
9853     n))
9854
9855 (gnus-summary-make-all-marking-commands)
9856
9857 (gnus-ems-redefine)
9858
9859 (provide 'gnus-sum)
9860
9861 (run-hooks 'gnus-sum-load-hook)
9862
9863 ;;; gnus-sum.el ends here