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