2000-11-02 09:33:01 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40 (autoload 'gnus-cache-write-active "gnus-cache")
41
42 (defcustom gnus-kill-summary-on-exit t
43   "*If non-nil, kill the summary buffer when you exit from it.
44 If nil, the summary will become a \"*Dead Summary*\" buffer, and
45 it will be killed sometime later."
46   :group 'gnus-summary-exit
47   :type 'boolean)
48
49 (defcustom gnus-fetch-old-headers nil
50   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
51 If an unread article in the group refers to an older, already read (or
52 just marked as read) article, the old article will not normally be
53 displayed in the Summary buffer.  If this variable is non-nil, Gnus
54 will attempt to grab the headers to the old articles, and thereby
55 build complete threads.  If it has the value `some', only enough
56 headers to connect otherwise loose threads will be displayed.  This
57 variable can also be a number.  In that case, no more than that number
58 of old headers will be fetched.  If it has the value `invisible', all
59 old headers will be fetched, but none will be displayed.
60
61 The server has to support NOV for any of this to work."
62   :group 'gnus-thread
63   :type '(choice (const :tag "off" nil)
64                  (const some)
65                  number
66                  (sexp :menu-tag "other" t)))
67
68 (defcustom gnus-refer-thread-limit 200
69   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
70 If t, fetch all the available old headers."
71   :group 'gnus-thread
72   :type '(choice number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-summary-make-false-root 'adopt
76   "*nil means that Gnus won't gather loose threads.
77 If the root of a thread has expired or been read in a previous
78 session, the information necessary to build a complete thread has been
79 lost.  Instead of having many small sub-threads from this original thread
80 scattered all over the summary buffer, Gnus can gather them.
81
82 If non-nil, Gnus will try to gather all loose sub-threads from an
83 original thread into one large thread.
84
85 If this variable is non-nil, it should be one of `none', `adopt',
86 `dummy' or `empty'.
87
88 If this variable is `none', Gnus will not make a false root, but just
89 present the sub-threads after another.
90 If this variable is `dummy', Gnus will create a dummy root that will
91 have all the sub-threads as children.
92 If this variable is `adopt', Gnus will make one of the \"children\"
93 the parent and mark all the step-children as such.
94 If this variable is `empty', the \"children\" are printed with empty
95 subject fields.  (Or rather, they will be printed with a string
96 given by the `gnus-summary-same-subject' variable.)"
97   :group 'gnus-thread
98   :type '(choice (const :tag "off" nil)
99                  (const none)
100                  (const dummy)
101                  (const adopt)
102                  (const empty)))
103
104 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
105   "*A regexp to match subjects to be excluded from loose thread gathering.
106 As loose thread gathering is done on subjects only, that means that
107 there can be many false gatherings performed.  By rooting out certain
108 common subjects, gathering might become saner."
109   :group 'gnus-thread
110   :type 'regexp)
111
112 (defcustom gnus-summary-gather-subject-limit nil
113   "*Maximum length of subject comparisons when gathering loose threads.
114 Use nil to compare full subjects.  Setting this variable to a low
115 number will help gather threads that have been corrupted by
116 newsreaders chopping off subject lines, but it might also mean that
117 unrelated articles that have subject that happen to begin with the
118 same few characters will be incorrectly gathered.
119
120 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
121 comparing subjects."
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const fuzzy)
125                  (sexp :menu-tag "on" t)))
126
127 (defcustom gnus-simplify-subject-functions nil
128   "List of functions taking a string argument that simplify subjects.
129 The functions are applied recursively.
130
131 Useful functions to put in this list include: `gnus-simplify-subject-re',
132 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
133   :group 'gnus-thread
134   :type '(repeat function))
135
136 (defcustom gnus-simplify-ignored-prefixes nil
137   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  regexp))
141
142 (defcustom gnus-build-sparse-threads nil
143   "*If non-nil, fill in the gaps in threads.
144 If `some', only fill in the gaps that are needed to tie loose threads
145 together.  If `more', fill in all leaf nodes that Gnus can find.  If
146 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  (const some)
150                  (const more)
151                  (sexp :menu-tag "all" t)))
152
153 (defcustom gnus-summary-thread-gathering-function
154   'gnus-gather-threads-by-subject
155   "*Function used for gathering loose threads.
156 There are two pre-defined functions: `gnus-gather-threads-by-subject',
157 which only takes Subjects into consideration; and
158 `gnus-gather-threads-by-references', which compared the References
159 headers of the articles to find matches."
160   :group 'gnus-thread
161   :type '(radio (function-item gnus-gather-threads-by-subject)
162                 (function-item gnus-gather-threads-by-references)
163                 (function :tag "other")))
164
165 (defcustom gnus-summary-same-subject ""
166   "*String indicating that the current article has the same subject as the previous.
167 This variable will only be used if the value of
168 `gnus-summary-make-false-root' is `empty'."
169   :group 'gnus-summary-format
170   :type 'string)
171
172 (defcustom gnus-summary-goto-unread t
173   "*If t, many commands will go to the next unread article.
174 This applies to marking commands as well as other commands that
175 \"naturally\" select the next article, like, for instance, `SPC' at
176 the end of an article.
177
178 If nil, the marking commands do NOT go to the next unread article
179 (they go to the next article instead).  If `never', commands that
180 usually go to the next unread article, will go to the next article,
181 whether it is read or not."
182   :group 'gnus-summary-marks
183   :link '(custom-manual "(gnus)Setting Marks")
184   :type '(choice (const :tag "off" nil)
185                  (const never)
186                  (sexp :menu-tag "on" t)))
187
188 (defcustom gnus-summary-default-score 0
189   "*Default article score level.
190 All scores generated by the score files will be added to this score.
191 If this variable is nil, scoring will be disabled."
192   :group 'gnus-score-default
193   :type '(choice (const :tag "disable")
194                  integer))
195
196 (defcustom gnus-summary-zcore-fuzz 0
197   "*Fuzziness factor for the zcore in the summary buffer.
198 Articles with scores closer than this to `gnus-summary-default-score'
199 will not be marked."
200   :group 'gnus-summary-format
201   :type 'integer)
202
203 (defcustom gnus-simplify-subject-fuzzy-regexp nil
204   "*Strings to be removed when doing fuzzy matches.
205 This can either be a regular expression or list of regular expressions
206 that will be removed from subject strings if fuzzy subject
207 simplification is selected."
208   :group 'gnus-thread
209   :type '(repeat regexp))
210
211 (defcustom gnus-show-threads t
212   "*If non-nil, display threads in summary mode."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-subtree nil
217   "*If non-nil, hide all threads initially.
218 If threads are hidden, you have to run the command
219 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
220 to expose hidden threads."
221   :group 'gnus-thread
222   :type 'boolean)
223
224 (defcustom gnus-thread-hide-killed t
225   "*If non-nil, hide killed threads automatically."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-ignore-subject t
230   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
231 If nil, articles that have different subjects from their parents will
232 start separate threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-operation-ignore-subject t
237   "*If non-nil, subjects will be ignored when doing thread commands.
238 This affects commands like `gnus-summary-kill-thread' and
239 `gnus-summary-lower-thread'.
240
241 If this variable is nil, articles in the same thread with different
242 subjects will not be included in the operation in question.  If this
243 variable is `fuzzy', only articles that have subjects that are fuzzily
244 equal will be included."
245   :group 'gnus-thread
246   :type '(choice (const :tag "off" nil)
247                  (const fuzzy)
248                  (sexp :tag "on" t)))
249
250 (defcustom gnus-thread-indent-level 4
251   "*Number that says how much each sub-thread should be indented."
252   :group 'gnus-thread
253   :type 'integer)
254
255 (defcustom gnus-auto-extend-newsgroup t
256   "*If non-nil, extend newsgroup forward and backward when requested."
257   :group 'gnus-summary-choose
258   :type 'boolean)
259
260 (defcustom gnus-auto-select-first t
261   "*If nil, don't select the first unread article when entering a group.
262 If this variable is `best', select the highest-scored unread article
263 in the group.  If t, select the first unread article.
264
265 This variable can also be a function to place point on a likely
266 subject line.  Useful values include `gnus-summary-first-unread-subject',
267 `gnus-summary-first-unread-article' and
268 `gnus-summary-best-unread-article'.
269
270 If you want to prevent automatic selection of the first unread article
271 in some newsgroups, set the variable to nil in
272 `gnus-select-group-hook'."
273   :group 'gnus-group-select
274   :type '(choice (const :tag "none" nil)
275                  (const best)
276                  (sexp :menu-tag "first" t)
277                  (function-item gnus-summary-first-unread-subject)
278                  (function-item gnus-summary-first-unread-article)
279                  (function-item gnus-summary-best-unread-article)))
280
281 (defcustom gnus-auto-select-next t
282   "*If non-nil, offer to go to the next group from the end of the previous.
283 If the value is t and the next newsgroup is empty, Gnus will exit
284 summary mode and go back to group mode.  If the value is neither nil
285 nor t, Gnus will select the following unread newsgroup.  In
286 particular, if the value is the symbol `quietly', the next unread
287 newsgroup will be selected without any confirmation, and if it is
288 `almost-quietly', the next group will be selected without any
289 confirmation if you are located on the last article in the group.
290 Finally, if this variable is `slightly-quietly', the `Z n' command
291 will go to the next group without confirmation."
292   :group 'gnus-summary-maneuvering
293   :type '(choice (const :tag "off" nil)
294                  (const quietly)
295                  (const almost-quietly)
296                  (const slightly-quietly)
297                  (sexp :menu-tag "on" t)))
298
299 (defcustom gnus-auto-select-same nil
300   "*If non-nil, select the next article with the same subject.
301 If there are no more articles with the same subject, go to
302 the first unread article."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-summary-check-current nil
307   "*If non-nil, consider the current article when moving.
308 The \"unread\" movement commands will stay on the same line if the
309 current article is unread."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-auto-center-summary t
314   "*If non-nil, always center the current summary buffer.
315 In particular, if `vertical' do only vertical recentering.  If non-nil
316 and non-`vertical', do both horizontal and vertical recentering."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "none" nil)
319                  (const vertical)
320                  (integer :tag "height")
321                  (sexp :menu-tag "both" t)))
322
323 (defcustom gnus-show-all-headers nil
324   "*If non-nil, don't hide any headers."
325   :group 'gnus-article-hiding
326   :group 'gnus-article-headers
327   :type 'boolean)
328
329 (defcustom gnus-summary-ignore-duplicates nil
330   "*If non-nil, ignore articles with identical Message-ID headers."
331   :group 'gnus-summary
332   :type 'boolean)
333
334 (defcustom gnus-single-article-buffer t
335   "*If non-nil, display all articles in the same buffer.
336 If nil, each group will get its own article buffer."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-break-pages t
341   "*If non-nil, do page breaking on articles.
342 The page delimiter is specified by the `gnus-page-delimiter'
343 variable."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable."
350   :group 'gnus-summary-mail
351   :type '(repeat (choice (list :value (fun) function)
352                          (cons :value ("" "") regexp (repeat string))
353                          (sexp :value nil))))
354
355 (defcustom gnus-unread-mark ?  ;Whitespace
356   "*Mark used for unread articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-ticked-mark ?!
361   "*Mark used for ticked articles."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-dormant-mark ??
366   "*Mark used for dormant articles."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-del-mark ?r
371   "*Mark used for del'd articles."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-read-mark ?R
376   "*Mark used for read articles."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-expirable-mark ?E
381   "*Mark used for expirable articles."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-killed-mark ?K
386   "*Mark used for killed articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-souped-mark ?F
391   "*Mark used for killed articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-kill-file-mark ?X
396   "*Mark used for articles killed by kill files."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-low-score-mark ?Y
401   "*Mark used for articles with a low score."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-catchup-mark ?C
406   "*Mark used for articles that are caught up."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-replied-mark ?A
411   "*Mark used for articles that have been replied to."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-cached-mark ?*
416   "*Mark used for articles that are in the cache."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-saved-mark ?S
421   "*Mark used for articles that have been saved to."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ancient-mark ?O
426   "*Mark used for ancient articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-sparse-mark ?Q
431   "*Mark used for sparsely reffed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-canceled-mark ?G
436   "*Mark used for canceled articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-duplicate-mark ?M
441   "*Mark used for duplicate articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-undownloaded-mark ?@
446   "*Mark used for articles that weren't downloaded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-downloadable-mark ?%
451   "*Mark used for articles that are to be downloaded."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-unsendable-mark ?=
456   "*Mark used for articles that won't be sent."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-score-over-mark ?+
461   "*Score mark used for articles with high scores."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-score-below-mark ?-
466   "*Score mark used for articles with low scores."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-empty-thread-mark ?  ;Whitespace
471   "*There is no thread under the article."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-not-empty-thread-mark ?=
476   "*There is a thread under the article."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-view-pseudo-asynchronously nil
481   "*If non-nil, Gnus will view pseudo-articles asynchronously."
482   :group 'gnus-extract-view
483   :type 'boolean)
484
485 (defcustom gnus-auto-expirable-marks
486   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
487         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
488         gnus-souped-mark gnus-duplicate-mark)
489   "*The list of marks converted into expiration if a group is auto-expirable."
490   :group 'gnus-summary
491   :type '(repeat character))
492
493 (defcustom gnus-inhibit-user-auto-expire t
494   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
495   :group 'gnus-summary
496   :type 'boolean)
497
498 (defcustom gnus-view-pseudos nil
499   "*If `automatic', pseudo-articles will be viewed automatically.
500 If `not-confirm', pseudos will be viewed automatically, and the user
501 will not be asked to confirm the command."
502   :group 'gnus-extract-view
503   :type '(choice (const :tag "off" nil)
504                  (const automatic)
505                  (const not-confirm)))
506
507 (defcustom gnus-view-pseudos-separately t
508   "*If non-nil, one pseudo-article will be created for each file to be viewed.
509 If nil, all files that use the same viewing command will be given as a
510 list of parameters to that command."
511   :group 'gnus-extract-view
512   :type 'boolean)
513
514 (defcustom gnus-insert-pseudo-articles t
515   "*If non-nil, insert pseudo-articles when decoding articles."
516   :group 'gnus-extract-view
517   :type 'boolean)
518
519 (defcustom gnus-summary-dummy-line-format
520   "  %(:                          :%) %S\n"
521   "*The format specification for the dummy roots in the summary buffer.
522 It works along the same lines as a normal formatting string,
523 with some simple extensions.
524
525 %S  The subject"
526   :group 'gnus-threading
527   :type 'string)
528
529 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
530   "*The format specification for the summary mode line.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions:
533
534 %G  Group name
535 %p  Unprefixed group name
536 %A  Current article number
537 %z  Current article score
538 %V  Gnus version
539 %U  Number of unread articles in the group
540 %e  Number of unselected articles in the group
541 %Z  A string with unread/unselected article counts
542 %g  Shortish group name
543 %S  Subject of the current article
544 %u  User-defined spec
545 %s  Current score file name
546 %d  Number of dormant articles
547 %r  Number of articles that have been marked as read in this session
548 %E  Number of articles expunged by the score files"
549   :group 'gnus-summary-format
550   :type 'string)
551
552 (defcustom gnus-list-identifiers nil
553   "Regexp that matches list identifiers to be removed from subject.
554 This can also be a list of regexps."
555   :group 'gnus-summary-format
556   :group 'gnus-article-hiding
557   :type '(choice (const :tag "none" nil)
558                  (regexp :value ".*")
559                  (repeat :value (".*") regexp)))
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-threading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook nil
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting summary mode.
704 This hook is not called from the non-updating exit commands like `Q'."
705   :group 'gnus-various
706   :type 'hook)
707
708 (defcustom gnus-summary-update-hook
709   (list 'gnus-summary-highlight-line)
710   "*A hook called when a summary line is changed.
711 The hook will not be called if `gnus-visual' is nil.
712
713 The default function `gnus-summary-highlight-line' will
714 highlight the line according to the `gnus-summary-highlight'
715 variable."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
720   "*A hook called when an article is selected for the first time.
721 The hook is intended to mark an article as read (or unread)
722 automatically when it is selected."
723   :group 'gnus-summary-choose
724   :type 'hook)
725
726 (defcustom gnus-group-no-more-groups-hook nil
727   "*A hook run when returning to group mode having no more (unread) groups."
728   :group 'gnus-group-select
729   :type 'hook)
730
731 (defcustom gnus-ps-print-hook nil
732   "*A hook run before ps-printing something from Gnus."
733   :group 'gnus-summary
734   :type 'hook)
735
736 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
737   "Face used for highlighting the current article in the summary buffer."
738   :group 'gnus-summary-visual
739   :type 'face)
740
741 (defcustom gnus-summary-highlight
742   '(((= mark gnus-canceled-mark)
743      . gnus-summary-cancelled-face)
744     ((and (> score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-high-ticked-face)
748     ((and (< score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-low-ticked-face)
752     ((or (= mark gnus-dormant-mark)
753          (= mark gnus-ticked-mark))
754      . gnus-summary-normal-ticked-face)
755     ((and (> score default) (= mark gnus-ancient-mark))
756      . gnus-summary-high-ancient-face)
757     ((and (< score default) (= mark gnus-ancient-mark))
758      . gnus-summary-low-ancient-face)
759     ((= mark gnus-ancient-mark)
760      . gnus-summary-normal-ancient-face)
761     ((and (> score default) (= mark gnus-unread-mark))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (= mark gnus-unread-mark))
764      . gnus-summary-low-unread-face)
765     ((= mark gnus-unread-mark)
766      . gnus-summary-normal-unread-face)
767     ((and (> score default) (memq mark (list gnus-downloadable-mark
768                                              gnus-undownloaded-mark)))
769      . gnus-summary-high-unread-face)
770     ((and (< score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-low-unread-face)
773     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
774      . gnus-summary-normal-unread-face)
775     ((> score default)
776      . gnus-summary-high-read-face)
777     ((< score default)
778      . gnus-summary-low-read-face)
779     (t
780      . gnus-summary-normal-read-face))
781   "*Controls the highlighting of summary buffer lines.
782
783 A list of (FORM . FACE) pairs.  When deciding how a a particular
784 summary line should be displayed, each form is evaluated.  The content
785 of the face field after the first true form is used.  You can change
786 how those summary lines are displayed, by editing the face field.
787
788 You can use the following variables in the FORM field.
789
790 score:   The articles score
791 default: The default article score.
792 below:   The score below which articles are automatically marked as read.
793 mark:    The articles mark."
794   :group 'gnus-summary-visual
795   :type '(repeat (cons (sexp :tag "Form" nil)
796                        face)))
797
798 (defcustom gnus-alter-header-function nil
799   "Function called to allow alteration of article header structures.
800 The function is called with one parameter, the article header vector,
801 which it may alter in any way.")
802
803 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
804   "Variable that says which function should be used to decode a string with encoded words.")
805
806 (defcustom gnus-extra-headers nil
807   "*Extra headers to parse."
808   :group 'gnus-summary
809   :type '(repeat symbol))
810
811 (defcustom gnus-ignored-from-addresses
812   (and user-mail-address (regexp-quote user-mail-address))
813   "*Regexp of From headers that may be suppressed in favor of To headers."
814   :group 'gnus-summary
815   :type 'regexp)
816
817 (defcustom gnus-group-charset-alist
818   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
819     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
820     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
821     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
822     ("^relcom\\>" koi8-r)
823     ("^fido7\\>" koi8-r)
824     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
825     ("^israel\\>" iso-8859-1)
826     ("^han\\>" euc-kr)
827     ("^alt.chinese.text.big5\\>" chinese-big5)
828     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
829     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
830     (".*" iso-8859-1))
831   "Alist of regexps (to match group names) and default charsets to be used when reading."
832   :type '(repeat (list (regexp :tag "Group")
833                        (symbol :tag "Charset")))
834   :group 'gnus-charset)
835
836 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
837   "List of charsets that should be ignored.
838 When these charsets are used in the \"charset\" parameter, the
839 default charset will be used instead."
840   :type '(repeat symbol)
841   :group 'gnus-charset)
842
843 (defcustom gnus-group-ignored-charsets-alist
844   '(("alt\\.chinese\\.text" iso-8859-1))
845   "Alist of regexps (to match group names) and charsets that should be ignored.
846 When these charsets are used in the \"charset\" parameter, the
847 default charset will be used instead."
848   :type '(repeat (cons (regexp :tag "Group")
849                        (repeat symbol)))
850   :group 'gnus-charset)
851
852 (defcustom gnus-group-highlight-words-alist nil
853   "Alist of group regexps and highlight regexps.
854 This variable uses the same syntax as `gnus-emphasis-alist'."
855   :type '(repeat (cons (regexp :tag "Group")
856                        (repeat (list (regexp :tag "Highlight regexp")
857                                      (number :tag "Group for entire word" 0)
858                                      (number :tag "Group for displayed part" 0)
859                                      (symbol :tag "Face"
860                                              gnus-emphasis-highlight-words)))))
861   :group 'gnus-summary-visual)
862
863 (defcustom gnus-summary-show-article-charset-alist
864   nil
865   "Alist of number and charset.
866 The article will be shown with the charset corresponding to the
867 numbered argument.
868 For example: ((1 . cn-gb-2312) (2 . big5))."
869   :type '(repeat (cons (number :tag "Argument" 1)
870                        (symbol :tag "Charset")))
871   :group 'gnus-charset)
872
873 (defcustom gnus-preserve-marks t
874   "Whether marks are preserved when moving, copying and respooling messages."
875   :type 'boolean
876   :group 'gnus-summary-marks)
877
878 (defcustom gnus-alter-articles-to-read-function nil
879   "Function to be called to alter the list of articles to be selected."
880   :type 'function
881   :group 'gnus-summary)
882
883 (defcustom gnus-orphan-score nil
884   "*All orphans get this score added.  Set in the score file."
885   :group 'gnus-score-default
886   :type '(choice (const nil)
887                  integer))
888
889 (defcustom gnus-summary-save-parts-default-mime "image/.*"
890   "*A regexp to match MIME parts when saving multiple parts of a message
891 with gnus-summary-save-parts (X m). This regexp will be used by default
892 when prompting the user for which type of files to save."
893   :group 'gnus-summary
894   :type 'regexp)
895
896
897 ;;; Internal variables
898
899 (defvar gnus-article-mime-handles nil)
900 (defvar gnus-article-decoded-p nil)
901 (defvar gnus-article-charset nil)
902 (defvar gnus-article-ignored-charsets nil)
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has been gathered together.")
915
916 (defvar gnus-summary-save-parts-type-history nil)
917 (defvar gnus-summary-save-parts-last-directory nil)
918
919 ;; Avoid highlighting in kill files.
920 (defvar gnus-summary-inhibit-highlight nil)
921 (defvar gnus-newsgroup-selected-overlay nil)
922 (defvar gnus-inhibit-limiting nil)
923 (defvar gnus-newsgroup-adaptive-score-file nil)
924 (defvar gnus-current-score-file nil)
925 (defvar gnus-current-move-group nil)
926 (defvar gnus-current-copy-group nil)
927 (defvar gnus-current-crosspost-group nil)
928
929 (defvar gnus-newsgroup-dependencies nil)
930 (defvar gnus-newsgroup-adaptive nil)
931 (defvar gnus-summary-display-article-function nil)
932 (defvar gnus-summary-highlight-line-function nil
933   "Function called after highlighting a summary line.")
934
935 (defvar gnus-summary-line-format-alist
936   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
937     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
938     (?s gnus-tmp-subject-or-nil ?s)
939     (?n gnus-tmp-name ?s)
940     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
941         ?s)
942     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
943             gnus-tmp-from) ?s)
944     (?F gnus-tmp-from ?s)
945     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
946     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
947     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
948     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
949     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
950     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
951     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
952     (?L gnus-tmp-lines ?d)
953     (?I gnus-tmp-indentation ?s)
954     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
955     (?R gnus-tmp-replied ?c)
956     (?\[ gnus-tmp-opening-bracket ?c)
957     (?\] gnus-tmp-closing-bracket ?c)
958     (?\> (make-string gnus-tmp-level ? ) ?s)
959     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
960     (?i gnus-tmp-score ?d)
961     (?z gnus-tmp-score-char ?c)
962     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
963     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
964     (?U gnus-tmp-unread ?c)
965     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
966     (?t (gnus-summary-number-of-articles-in-thread
967          (and (boundp 'thread) (car thread)) gnus-tmp-level)
968         ?d)
969     (?e (gnus-summary-number-of-articles-in-thread
970          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
971         ?c)
972     (?u gnus-tmp-user-defined ?s)
973     (?P (gnus-pick-line-number) ?d))
974   "An alist of format specifications that can appear in summary lines.
975 These are paired with what variables they correspond with, along with
976 the type of the variable (string, integer, character, etc).")
977
978 (defvar gnus-summary-dummy-line-format-alist
979   `((?S gnus-tmp-subject ?s)
980     (?N gnus-tmp-number ?d)
981     (?u gnus-tmp-user-defined ?s)))
982
983 (defvar gnus-summary-mode-line-format-alist
984   `((?G gnus-tmp-group-name ?s)
985     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
986     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
987     (?A gnus-tmp-article-number ?d)
988     (?Z gnus-tmp-unread-and-unselected ?s)
989     (?V gnus-version ?s)
990     (?U gnus-tmp-unread-and-unticked ?d)
991     (?S gnus-tmp-subject ?s)
992     (?e gnus-tmp-unselected ?d)
993     (?u gnus-tmp-user-defined ?s)
994     (?d (length gnus-newsgroup-dormant) ?d)
995     (?t (length gnus-newsgroup-marked) ?d)
996     (?r (length gnus-newsgroup-reads) ?d)
997     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
998     (?E gnus-newsgroup-expunged-tally ?d)
999     (?s (gnus-current-score-file-nondirectory) ?s)))
1000
1001 (defvar gnus-last-search-regexp nil
1002   "Default regexp for article search command.")
1003
1004 (defvar gnus-last-shell-command nil
1005   "Default shell command on article.")
1006
1007 (defvar gnus-newsgroup-begin nil)
1008 (defvar gnus-newsgroup-end nil)
1009 (defvar gnus-newsgroup-last-rmail nil)
1010 (defvar gnus-newsgroup-last-mail nil)
1011 (defvar gnus-newsgroup-last-folder nil)
1012 (defvar gnus-newsgroup-last-file nil)
1013 (defvar gnus-newsgroup-auto-expire nil)
1014 (defvar gnus-newsgroup-active nil)
1015
1016 (defvar gnus-newsgroup-data nil)
1017 (defvar gnus-newsgroup-data-reverse nil)
1018 (defvar gnus-newsgroup-limit nil)
1019 (defvar gnus-newsgroup-limits nil)
1020
1021 (defvar gnus-newsgroup-unreads nil
1022   "List of unread articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-unselected nil
1025   "List of unselected unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-reads nil
1028   "Alist of read articles and article marks in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-expunged-tally nil)
1031
1032 (defvar gnus-newsgroup-marked nil
1033   "List of ticked articles in the current newsgroup (a subset of unread art).")
1034
1035 (defvar gnus-newsgroup-killed nil
1036   "List of ranges of articles that have been through the scoring process.")
1037
1038 (defvar gnus-newsgroup-cached nil
1039   "List of articles that come from the article cache.")
1040
1041 (defvar gnus-newsgroup-saved nil
1042   "List of articles that have been saved.")
1043
1044 (defvar gnus-newsgroup-kill-headers nil)
1045
1046 (defvar gnus-newsgroup-replied nil
1047   "List of articles that have been replied to in the current newsgroup.")
1048
1049 (defvar gnus-newsgroup-expirable nil
1050   "List of articles in the current newsgroup that can be expired.")
1051
1052 (defvar gnus-newsgroup-processable nil
1053   "List of articles in the current newsgroup that can be processed.")
1054
1055 (defvar gnus-newsgroup-downloadable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-undownloaded nil
1059   "List of articles in the current newsgroup that haven't been downloaded..")
1060
1061 (defvar gnus-newsgroup-unsendable nil
1062   "List of articles in the current newsgroup that won't be sent.")
1063
1064 (defvar gnus-newsgroup-bookmarks nil
1065   "List of articles in the current newsgroup that have bookmarks.")
1066
1067 (defvar gnus-newsgroup-dormant nil
1068   "List of dormant articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-scored nil
1071   "List of scored articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-headers nil
1074   "List of article headers in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-threads nil)
1077
1078 (defvar gnus-newsgroup-prepared nil
1079   "Whether the current group has been prepared properly.")
1080
1081 (defvar gnus-newsgroup-ancient nil
1082   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-sparse nil)
1085
1086 (defvar gnus-current-article nil)
1087 (defvar gnus-article-current nil)
1088 (defvar gnus-current-headers nil)
1089 (defvar gnus-have-all-headers nil)
1090 (defvar gnus-last-article nil)
1091 (defvar gnus-newsgroup-history nil)
1092 (defvar gnus-newsgroup-charset nil)
1093 (defvar gnus-newsgroup-ephemeral-charset nil)
1094 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1095
1096 (defconst gnus-summary-local-variables
1097   '(gnus-newsgroup-name
1098     gnus-newsgroup-begin gnus-newsgroup-end
1099     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1100     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1101     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1102     gnus-newsgroup-unselected gnus-newsgroup-marked
1103     gnus-newsgroup-reads gnus-newsgroup-saved
1104     gnus-newsgroup-replied gnus-newsgroup-expirable
1105     gnus-newsgroup-processable gnus-newsgroup-killed
1106     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1107     gnus-newsgroup-unsendable
1108     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1109     gnus-newsgroup-headers gnus-newsgroup-threads
1110     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1111     gnus-current-article gnus-current-headers gnus-have-all-headers
1112     gnus-last-article gnus-article-internal-prepare-hook
1113     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1114     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1115     gnus-thread-expunge-below
1116     gnus-score-alist gnus-current-score-file
1117     (gnus-summary-expunge-below . global)
1118     (gnus-summary-mark-below . global)
1119     (gnus-orphan-score . global)
1120     gnus-newsgroup-active gnus-scores-exclude-files
1121     gnus-newsgroup-history gnus-newsgroup-ancient
1122     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1123     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1124     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1125     (gnus-newsgroup-expunged-tally . 0)
1126     gnus-cache-removable-articles gnus-newsgroup-cached
1127     gnus-newsgroup-data gnus-newsgroup-data-reverse
1128     gnus-newsgroup-limit gnus-newsgroup-limits
1129     gnus-newsgroup-charset)
1130   "Variables that are buffer-local to the summary buffers.")
1131
1132 (defvar gnus-newsgroup-variables nil
1133   "Variables that have separate values in the newsgroups.")
1134
1135 ;; Byte-compiler warning.
1136 (defvar gnus-article-mode-map)
1137
1138 ;; MIME stuff.
1139
1140 (defvar gnus-decode-encoded-word-methods
1141   '(mail-decode-encoded-word-string)
1142   "List of methods used to decode encoded words.
1143
1144 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1145 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1146 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1147 whose names match REGEXP.
1148
1149 For example:
1150 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1151  mail-decode-encoded-word-string
1152  (\"chinese\" . rfc1843-decode-string))")
1153
1154 (defvar gnus-decode-encoded-word-methods-cache nil)
1155
1156 (defun gnus-multi-decode-encoded-word-string (string)
1157   "Apply the functions from `gnus-encoded-word-methods' that match."
1158   (unless (and gnus-decode-encoded-word-methods-cache
1159                (eq gnus-newsgroup-name
1160                    (car gnus-decode-encoded-word-methods-cache)))
1161     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1162     (mapcar (lambda (x)
1163               (if (symbolp x)
1164                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1165                 (if (and gnus-newsgroup-name
1166                          (string-match (car x) gnus-newsgroup-name))
1167                     (nconc gnus-decode-encoded-word-methods-cache
1168                            (list (cdr x))))))
1169           gnus-decode-encoded-word-methods))
1170   (let ((xlist gnus-decode-encoded-word-methods-cache))
1171     (pop xlist)
1172     (while xlist
1173       (setq string (funcall (pop xlist) string))))
1174   string)
1175
1176 ;; Subject simplification.
1177
1178 (defun gnus-simplify-whitespace (str)
1179   "Remove excessive whitespace from STR."
1180   (let ((mystr str))
1181     ;; Multiple spaces.
1182     (while (string-match "[ \t][ \t]+" mystr)
1183       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1184                           " "
1185                           (substring mystr (match-end 0)))))
1186     ;; Leading spaces.
1187     (when (string-match "^[ \t]+" mystr)
1188       (setq mystr (substring mystr (match-end 0))))
1189     ;; Trailing spaces.
1190     (when (string-match "[ \t]+$" mystr)
1191       (setq mystr (substring mystr 0 (match-beginning 0))))
1192     mystr))
1193
1194 (defsubst gnus-simplify-subject-re (subject)
1195   "Remove \"Re:\" from subject lines."
1196   (if (string-match "^[Rr][Ee]: *" subject)
1197       (substring subject (match-end 0))
1198     subject))
1199
1200 (defun gnus-simplify-subject (subject &optional re-only)
1201   "Remove `Re:' and words in parentheses.
1202 If RE-ONLY is non-nil, strip leading `Re:'s only."
1203   (let ((case-fold-search t))           ;Ignore case.
1204     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1205     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1206       (setq subject (substring subject (match-end 0))))
1207     ;; Remove uninteresting prefixes.
1208     (when (and (not re-only)
1209                gnus-simplify-ignored-prefixes
1210                (string-match gnus-simplify-ignored-prefixes subject))
1211       (setq subject (substring subject (match-end 0))))
1212     ;; Remove words in parentheses from end.
1213     (unless re-only
1214       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1215         (setq subject (substring subject 0 (match-beginning 0)))))
1216     ;; Return subject string.
1217     subject))
1218
1219 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1220 ;; all whitespace.
1221 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1222   (goto-char (point-min))
1223   (while (re-search-forward regexp nil t)
1224     (replace-match (or newtext ""))))
1225
1226 (defun gnus-simplify-buffer-fuzzy ()
1227   "Simplify string in the buffer fuzzily.
1228 The string in the accessible portion of the current buffer is simplified.
1229 It is assumed to be a single-line subject.
1230 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1231 matter is removed.  Additional things can be deleted by setting
1232 `gnus-simplify-subject-fuzzy-regexp'."
1233   (let ((case-fold-search t)
1234         (modified-tick))
1235     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1236
1237     (while (not (eq modified-tick (buffer-modified-tick)))
1238       (setq modified-tick (buffer-modified-tick))
1239       (cond
1240        ((listp gnus-simplify-subject-fuzzy-regexp)
1241         (mapcar 'gnus-simplify-buffer-fuzzy-step
1242                 gnus-simplify-subject-fuzzy-regexp))
1243        (gnus-simplify-subject-fuzzy-regexp
1244         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1245       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1246       (gnus-simplify-buffer-fuzzy-step
1247        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1248       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1249
1250     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1251     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1252     (gnus-simplify-buffer-fuzzy-step " $")
1253     (gnus-simplify-buffer-fuzzy-step "^ +")))
1254
1255 (defun gnus-simplify-subject-fuzzy (subject)
1256   "Simplify a subject string fuzzily.
1257 See `gnus-simplify-buffer-fuzzy' for details."
1258   (save-excursion
1259     (gnus-set-work-buffer)
1260     (let ((case-fold-search t))
1261       ;; Remove uninteresting prefixes.
1262       (when (and gnus-simplify-ignored-prefixes
1263                  (string-match gnus-simplify-ignored-prefixes subject))
1264         (setq subject (substring subject (match-end 0))))
1265       (insert subject)
1266       (inline (gnus-simplify-buffer-fuzzy))
1267       (buffer-string))))
1268
1269 (defsubst gnus-simplify-subject-fully (subject)
1270   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1271   (cond
1272    (gnus-simplify-subject-functions
1273     (gnus-map-function gnus-simplify-subject-functions subject))
1274    ((null gnus-summary-gather-subject-limit)
1275     (gnus-simplify-subject-re subject))
1276    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1277     (gnus-simplify-subject-fuzzy subject))
1278    ((numberp gnus-summary-gather-subject-limit)
1279     (gnus-limit-string (gnus-simplify-subject-re subject)
1280                        gnus-summary-gather-subject-limit))
1281    (t
1282     subject)))
1283
1284 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1285   "Check whether two subjects are equal.
1286 If optional argument simple-first is t, first argument is already
1287 simplified."
1288   (cond
1289    ((null simple-first)
1290     (equal (gnus-simplify-subject-fully s1)
1291            (gnus-simplify-subject-fully s2)))
1292    (t
1293     (equal s1
1294            (gnus-simplify-subject-fully s2)))))
1295
1296 (defun gnus-summary-bubble-group ()
1297   "Increase the score of the current group.
1298 This is a handy function to add to `gnus-summary-exit-hook' to
1299 increase the score of each group you read."
1300   (gnus-group-add-score gnus-newsgroup-name))
1301
1302 \f
1303 ;;;
1304 ;;; Gnus summary mode
1305 ;;;
1306
1307 (put 'gnus-summary-mode 'mode-class 'special)
1308
1309 (when t
1310   ;; Non-orthogonal keys
1311
1312   (gnus-define-keys gnus-summary-mode-map
1313     " " gnus-summary-next-page
1314     "\177" gnus-summary-prev-page
1315     [delete] gnus-summary-prev-page
1316     [backspace] gnus-summary-prev-page
1317     "\r" gnus-summary-scroll-up
1318     "\M-\r" gnus-summary-scroll-down
1319     "n" gnus-summary-next-unread-article
1320     "p" gnus-summary-prev-unread-article
1321     "N" gnus-summary-next-article
1322     "P" gnus-summary-prev-article
1323     "\M-\C-n" gnus-summary-next-same-subject
1324     "\M-\C-p" gnus-summary-prev-same-subject
1325     "\M-n" gnus-summary-next-unread-subject
1326     "\M-p" gnus-summary-prev-unread-subject
1327     "." gnus-summary-first-unread-article
1328     "," gnus-summary-best-unread-article
1329     "\M-s" gnus-summary-search-article-forward
1330     "\M-r" gnus-summary-search-article-backward
1331     "<" gnus-summary-beginning-of-article
1332     ">" gnus-summary-end-of-article
1333     "j" gnus-summary-goto-article
1334     "^" gnus-summary-refer-parent-article
1335     "\M-^" gnus-summary-refer-article
1336     "u" gnus-summary-tick-article-forward
1337     "!" gnus-summary-tick-article-forward
1338     "U" gnus-summary-tick-article-backward
1339     "d" gnus-summary-mark-as-read-forward
1340     "D" gnus-summary-mark-as-read-backward
1341     "E" gnus-summary-mark-as-expirable
1342     "\M-u" gnus-summary-clear-mark-forward
1343     "\M-U" gnus-summary-clear-mark-backward
1344     "k" gnus-summary-kill-same-subject-and-select
1345     "\C-k" gnus-summary-kill-same-subject
1346     "\M-\C-k" gnus-summary-kill-thread
1347     "\M-\C-l" gnus-summary-lower-thread
1348     "e" gnus-summary-edit-article
1349     "#" gnus-summary-mark-as-processable
1350     "\M-#" gnus-summary-unmark-as-processable
1351     "\M-\C-t" gnus-summary-toggle-threads
1352     "\M-\C-s" gnus-summary-show-thread
1353     "\M-\C-h" gnus-summary-hide-thread
1354     "\M-\C-f" gnus-summary-next-thread
1355     "\M-\C-b" gnus-summary-prev-thread
1356     [(meta down)] gnus-summary-next-thread
1357     [(meta up)] gnus-summary-prev-thread
1358     "\M-\C-u" gnus-summary-up-thread
1359     "\M-\C-d" gnus-summary-down-thread
1360     "&" gnus-summary-execute-command
1361     "c" gnus-summary-catchup-and-exit
1362     "\C-w" gnus-summary-mark-region-as-read
1363     "\C-t" gnus-summary-toggle-truncation
1364     "?" gnus-summary-mark-as-dormant
1365     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1366     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1367     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1368     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1369     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1370     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1371     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1372     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1373     "=" gnus-summary-expand-window
1374     "\C-x\C-s" gnus-summary-reselect-current-group
1375     "\M-g" gnus-summary-rescan-group
1376     "w" gnus-summary-stop-page-breaking
1377     "\C-c\C-r" gnus-summary-caesar-message
1378     "f" gnus-summary-followup
1379     "F" gnus-summary-followup-with-original
1380     "C" gnus-summary-cancel-article
1381     "r" gnus-summary-reply
1382     "R" gnus-summary-reply-with-original
1383     "\C-c\C-f" gnus-summary-mail-forward
1384     "o" gnus-summary-save-article
1385     "\C-o" gnus-summary-save-article-mail
1386     "|" gnus-summary-pipe-output
1387     "\M-k" gnus-summary-edit-local-kill
1388     "\M-K" gnus-summary-edit-global-kill
1389     ;; "V" gnus-version
1390     "\C-c\C-d" gnus-summary-describe-group
1391     "q" gnus-summary-exit
1392     "Q" gnus-summary-exit-no-update
1393     "\C-c\C-i" gnus-info-find-node
1394     gnus-mouse-2 gnus-mouse-pick-article
1395     "m" gnus-summary-mail-other-window
1396     "a" gnus-summary-post-news
1397     "x" gnus-summary-limit-to-unread
1398     "s" gnus-summary-isearch-article
1399     "t" gnus-summary-toggle-header
1400     "g" gnus-summary-show-article
1401     "l" gnus-summary-goto-last-article
1402     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1403     "\C-d" gnus-summary-enter-digest-group
1404     "\M-\C-d" gnus-summary-read-document
1405     "\M-\C-e" gnus-summary-edit-parameters
1406     "\M-\C-a" gnus-summary-customize-parameters
1407     "\C-c\C-b" gnus-bug
1408     "*" gnus-cache-enter-article
1409     "\M-*" gnus-cache-remove-article
1410     "\M-&" gnus-summary-universal-argument
1411     "\C-l" gnus-recenter
1412     "I" gnus-summary-increase-score
1413     "L" gnus-summary-lower-score
1414     "\M-i" gnus-symbolic-argument
1415     "h" gnus-summary-select-article-buffer
1416
1417     "b" gnus-article-view-part
1418     "\M-t" gnus-summary-toggle-display-buttonized
1419
1420     "V" gnus-summary-score-map
1421     "X" gnus-uu-extract-map
1422     "S" gnus-summary-send-map)
1423
1424   ;; Sort of orthogonal keymap
1425   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1426     "t" gnus-summary-tick-article-forward
1427     "!" gnus-summary-tick-article-forward
1428     "d" gnus-summary-mark-as-read-forward
1429     "r" gnus-summary-mark-as-read-forward
1430     "c" gnus-summary-clear-mark-forward
1431     " " gnus-summary-clear-mark-forward
1432     "e" gnus-summary-mark-as-expirable
1433     "x" gnus-summary-mark-as-expirable
1434     "?" gnus-summary-mark-as-dormant
1435     "b" gnus-summary-set-bookmark
1436     "B" gnus-summary-remove-bookmark
1437     "#" gnus-summary-mark-as-processable
1438     "\M-#" gnus-summary-unmark-as-processable
1439     "S" gnus-summary-limit-include-expunged
1440     "C" gnus-summary-catchup
1441     "H" gnus-summary-catchup-to-here
1442     "\C-c" gnus-summary-catchup-all
1443     "k" gnus-summary-kill-same-subject-and-select
1444     "K" gnus-summary-kill-same-subject
1445     "P" gnus-uu-mark-map)
1446
1447   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1448     "c" gnus-summary-clear-above
1449     "u" gnus-summary-tick-above
1450     "m" gnus-summary-mark-above
1451     "k" gnus-summary-kill-below)
1452
1453   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1454     "/" gnus-summary-limit-to-subject
1455     "n" gnus-summary-limit-to-articles
1456     "w" gnus-summary-pop-limit
1457     "s" gnus-summary-limit-to-subject
1458     "a" gnus-summary-limit-to-author
1459     "u" gnus-summary-limit-to-unread
1460     "m" gnus-summary-limit-to-marks
1461     "M" gnus-summary-limit-exclude-marks
1462     "v" gnus-summary-limit-to-score
1463     "*" gnus-summary-limit-include-cached
1464     "D" gnus-summary-limit-include-dormant
1465     "T" gnus-summary-limit-include-thread
1466     "d" gnus-summary-limit-exclude-dormant
1467     "t" gnus-summary-limit-to-age
1468     "x" gnus-summary-limit-to-extra
1469     "E" gnus-summary-limit-include-expunged
1470     "c" gnus-summary-limit-exclude-childless-dormant
1471     "C" gnus-summary-limit-mark-excluded-as-read)
1472
1473   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1474     "n" gnus-summary-next-unread-article
1475     "p" gnus-summary-prev-unread-article
1476     "N" gnus-summary-next-article
1477     "P" gnus-summary-prev-article
1478     "\C-n" gnus-summary-next-same-subject
1479     "\C-p" gnus-summary-prev-same-subject
1480     "\M-n" gnus-summary-next-unread-subject
1481     "\M-p" gnus-summary-prev-unread-subject
1482     "f" gnus-summary-first-unread-article
1483     "b" gnus-summary-best-unread-article
1484     "j" gnus-summary-goto-article
1485     "g" gnus-summary-goto-subject
1486     "l" gnus-summary-goto-last-article
1487     "o" gnus-summary-pop-article)
1488
1489   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1490     "k" gnus-summary-kill-thread
1491     "l" gnus-summary-lower-thread
1492     "i" gnus-summary-raise-thread
1493     "T" gnus-summary-toggle-threads
1494     "t" gnus-summary-rethread-current
1495     "^" gnus-summary-reparent-thread
1496     "s" gnus-summary-show-thread
1497     "S" gnus-summary-show-all-threads
1498     "h" gnus-summary-hide-thread
1499     "H" gnus-summary-hide-all-threads
1500     "n" gnus-summary-next-thread
1501     "p" gnus-summary-prev-thread
1502     "u" gnus-summary-up-thread
1503     "o" gnus-summary-top-thread
1504     "d" gnus-summary-down-thread
1505     "#" gnus-uu-mark-thread
1506     "\M-#" gnus-uu-unmark-thread)
1507
1508   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1509     "g" gnus-summary-prepare
1510     "c" gnus-summary-insert-cached-articles)
1511
1512   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1513     "c" gnus-summary-catchup-and-exit
1514     "C" gnus-summary-catchup-all-and-exit
1515     "E" gnus-summary-exit-no-update
1516     "Q" gnus-summary-exit
1517     "Z" gnus-summary-exit
1518     "n" gnus-summary-catchup-and-goto-next-group
1519     "R" gnus-summary-reselect-current-group
1520     "G" gnus-summary-rescan-group
1521     "N" gnus-summary-next-group
1522     "s" gnus-summary-save-newsrc
1523     "P" gnus-summary-prev-group)
1524
1525   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1526     " " gnus-summary-next-page
1527     "n" gnus-summary-next-page
1528     "\177" gnus-summary-prev-page
1529     [delete] gnus-summary-prev-page
1530     "p" gnus-summary-prev-page
1531     "\r" gnus-summary-scroll-up
1532     "\M-\r" gnus-summary-scroll-down
1533     "<" gnus-summary-beginning-of-article
1534     ">" gnus-summary-end-of-article
1535     "b" gnus-summary-beginning-of-article
1536     "e" gnus-summary-end-of-article
1537     "^" gnus-summary-refer-parent-article
1538     "r" gnus-summary-refer-parent-article
1539     "D" gnus-summary-enter-digest-group
1540     "R" gnus-summary-refer-references
1541     "T" gnus-summary-refer-thread
1542     "g" gnus-summary-show-article
1543     "s" gnus-summary-isearch-article
1544     "P" gnus-summary-print-article
1545     "t" gnus-article-babel)
1546
1547   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1548     "b" gnus-article-add-buttons
1549     "B" gnus-article-add-buttons-to-head
1550     "o" gnus-article-treat-overstrike
1551     "e" gnus-article-emphasize
1552     "w" gnus-article-fill-cited-article
1553     "Q" gnus-article-fill-long-lines
1554     "C" gnus-article-capitalize-sentences
1555     "c" gnus-article-remove-cr
1556     "q" gnus-article-de-quoted-unreadable
1557     "6" gnus-article-de-base64-unreadable
1558     "Z" gnus-article-decode-HZ
1559     "h" gnus-article-wash-html
1560     "s" gnus-summary-force-verify-and-decrypt
1561     "f" gnus-article-display-x-face
1562     "l" gnus-summary-stop-page-breaking
1563     "r" gnus-summary-caesar-message
1564     "t" gnus-summary-toggle-header
1565     "v" gnus-summary-verbose-headers
1566     "H" gnus-article-strip-headers-in-body
1567     "d" gnus-article-treat-dumbquotes)
1568
1569   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1570     "a" gnus-article-hide
1571     "h" gnus-article-hide-headers
1572     "b" gnus-article-hide-boring-headers
1573     "s" gnus-article-hide-signature
1574     "c" gnus-article-hide-citation
1575     "C" gnus-article-hide-citation-in-followups
1576     "l" gnus-article-hide-list-identifiers
1577     "p" gnus-article-hide-pgp
1578     "B" gnus-article-strip-banner
1579     "P" gnus-article-hide-pem
1580     "\C-c" gnus-article-hide-citation-maybe)
1581
1582   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1583     "a" gnus-article-highlight
1584     "h" gnus-article-highlight-headers
1585     "c" gnus-article-highlight-citation
1586     "s" gnus-article-highlight-signature)
1587
1588   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1589     "w" gnus-article-decode-mime-words
1590     "c" gnus-article-decode-charset
1591     "v" gnus-mime-view-all-parts
1592     "b" gnus-article-view-part)
1593
1594   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1595     "z" gnus-article-date-ut
1596     "u" gnus-article-date-ut
1597     "l" gnus-article-date-local
1598     "e" gnus-article-date-lapsed
1599     "o" gnus-article-date-original
1600     "i" gnus-article-date-iso8601
1601     "s" gnus-article-date-user)
1602
1603   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1604     "t" gnus-article-remove-trailing-blank-lines
1605     "l" gnus-article-strip-leading-blank-lines
1606     "m" gnus-article-strip-multiple-blank-lines
1607     "a" gnus-article-strip-blank-lines
1608     "A" gnus-article-strip-all-blank-lines
1609     "s" gnus-article-strip-leading-space
1610     "e" gnus-article-strip-trailing-space)
1611
1612   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1613     "v" gnus-version
1614     "f" gnus-summary-fetch-faq
1615     "d" gnus-summary-describe-group
1616     "h" gnus-summary-describe-briefly
1617     "i" gnus-info-find-node)
1618
1619   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1620     "e" gnus-summary-expire-articles
1621     "\M-\C-e" gnus-summary-expire-articles-now
1622     "\177" gnus-summary-delete-article
1623     [delete] gnus-summary-delete-article
1624     [backspace] gnus-summary-delete-article
1625     "m" gnus-summary-move-article
1626     "r" gnus-summary-respool-article
1627     "w" gnus-summary-edit-article
1628     "c" gnus-summary-copy-article
1629     "B" gnus-summary-crosspost-article
1630     "q" gnus-summary-respool-query
1631     "t" gnus-summary-respool-trace
1632     "i" gnus-summary-import-article
1633     "p" gnus-summary-article-posted-p)
1634
1635   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1636     "o" gnus-summary-save-article
1637     "m" gnus-summary-save-article-mail
1638     "F" gnus-summary-write-article-file
1639     "r" gnus-summary-save-article-rmail
1640     "f" gnus-summary-save-article-file
1641     "b" gnus-summary-save-article-body-file
1642     "h" gnus-summary-save-article-folder
1643     "v" gnus-summary-save-article-vm
1644     "p" gnus-summary-pipe-output
1645     "s" gnus-soup-add-article)
1646
1647   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1648     "b" gnus-summary-display-buttonized
1649     "m" gnus-summary-repair-multipart
1650     "v" gnus-article-view-part
1651     "o" gnus-article-save-part
1652     "c" gnus-article-copy-part
1653     "e" gnus-article-externalize-part
1654     "i" gnus-article-inline-part
1655     "|" gnus-article-pipe-part))
1656
1657 (defun gnus-summary-make-menu-bar ()
1658   (gnus-turn-off-edit-menu 'summary)
1659
1660   (unless (boundp 'gnus-summary-misc-menu)
1661
1662     (easy-menu-define
1663      gnus-summary-kill-menu gnus-summary-mode-map ""
1664      (cons
1665       "Score"
1666       (nconc
1667        (list
1668         ["Enter score..." gnus-summary-score-entry t]
1669         ["Customize" gnus-score-customize t])
1670        (gnus-make-score-map 'increase)
1671        (gnus-make-score-map 'lower)
1672        '(("Mark"
1673           ["Kill below" gnus-summary-kill-below t]
1674           ["Mark above" gnus-summary-mark-above t]
1675           ["Tick above" gnus-summary-tick-above t]
1676           ["Clear above" gnus-summary-clear-above t])
1677          ["Current score" gnus-summary-current-score t]
1678          ["Set score" gnus-summary-set-score t]
1679          ["Switch current score file..." gnus-score-change-score-file t]
1680          ["Set mark below..." gnus-score-set-mark-below t]
1681          ["Set expunge below..." gnus-score-set-expunge-below t]
1682          ["Edit current score file" gnus-score-edit-current-scores t]
1683          ["Edit score file" gnus-score-edit-file t]
1684          ["Trace score" gnus-score-find-trace t]
1685          ["Find words" gnus-score-find-favourite-words t]
1686          ["Rescore buffer" gnus-summary-rescore t]
1687          ["Increase score..." gnus-summary-increase-score t]
1688          ["Lower score..." gnus-summary-lower-score t]))))
1689
1690     ;; Define both the Article menu in the summary buffer and the equivalent
1691     ;; Commands menu in the article buffer here for consistency.
1692     (let ((innards
1693            '(("Hide"
1694               ["All" gnus-article-hide t]
1695               ["Headers" gnus-article-hide-headers t]
1696               ["Signature" gnus-article-hide-signature t]
1697               ["Citation" gnus-article-hide-citation t]
1698               ["List identifiers" gnus-article-hide-list-identifiers t]
1699               ["PGP" gnus-article-hide-pgp t]
1700               ["Banner" gnus-article-strip-banner t]
1701               ["Boring headers" gnus-article-hide-boring-headers t])
1702              ("Highlight"
1703               ["All" gnus-article-highlight t]
1704               ["Headers" gnus-article-highlight-headers t]
1705               ["Signature" gnus-article-highlight-signature t]
1706               ["Citation" gnus-article-highlight-citation t])
1707              ("MIME"
1708               ["Words" gnus-article-decode-mime-words t]
1709               ["Charset" gnus-article-decode-charset t]
1710               ["QP" gnus-article-de-quoted-unreadable t]
1711               ["Base64" gnus-article-de-base64-unreadable t]
1712               ["View all" gnus-mime-view-all-parts t]
1713               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt])
1714              ("Date"
1715               ["Local" gnus-article-date-local t]
1716               ["ISO8601" gnus-article-date-iso8601 t]
1717               ["UT" gnus-article-date-ut t]
1718               ["Original" gnus-article-date-original t]
1719               ["Lapsed" gnus-article-date-lapsed t]
1720               ["User-defined" gnus-article-date-user t])
1721              ("Washing"
1722               ("Remove Blanks"
1723                ["Leading" gnus-article-strip-leading-blank-lines t]
1724                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1725                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1726                ["All of the above" gnus-article-strip-blank-lines t]
1727                ["All" gnus-article-strip-all-blank-lines t]
1728                ["Leading space" gnus-article-strip-leading-space t]
1729                ["Trailing space" gnus-article-strip-trailing-space t])
1730               ["Overstrike" gnus-article-treat-overstrike t]
1731               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1732               ["Emphasis" gnus-article-emphasize t]
1733               ["Word wrap" gnus-article-fill-cited-article t]
1734               ["Fill long lines" gnus-article-fill-long-lines t]
1735               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1736               ["CR" gnus-article-remove-cr t]
1737               ["Show X-Face" gnus-article-display-x-face t]
1738               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1739               ["Base64" gnus-article-de-base64-unreadable t]
1740               ["Rot 13" gnus-summary-caesar-message t]
1741               ["Unix pipe" gnus-summary-pipe-message t]
1742               ["Add buttons" gnus-article-add-buttons t]
1743               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1744               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1745               ["Verbose header" gnus-summary-verbose-headers t]
1746               ["Toggle header" gnus-summary-toggle-header t]
1747               ["Html" gnus-article-wash-html t]
1748               ["HZ" gnus-article-decode-HZ t])
1749              ("Output"
1750               ["Save in default format" gnus-summary-save-article t]
1751               ["Save in file" gnus-summary-save-article-file t]
1752               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1753               ["Save in MH folder" gnus-summary-save-article-folder t]
1754               ["Save in VM folder" gnus-summary-save-article-vm t]
1755               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1756               ["Save body in file" gnus-summary-save-article-body-file t]
1757               ["Pipe through a filter" gnus-summary-pipe-output t]
1758               ["Add to SOUP packet" gnus-soup-add-article t]
1759               ["Print" gnus-summary-print-article t])
1760              ("Backend"
1761               ["Respool article..." gnus-summary-respool-article t]
1762               ["Move article..." gnus-summary-move-article
1763                (gnus-check-backend-function
1764                 'request-move-article gnus-newsgroup-name)]
1765               ["Copy article..." gnus-summary-copy-article t]
1766               ["Crosspost article..." gnus-summary-crosspost-article
1767                (gnus-check-backend-function
1768                 'request-replace-article gnus-newsgroup-name)]
1769               ["Import file..." gnus-summary-import-article t]
1770               ["Check if posted" gnus-summary-article-posted-p t]
1771               ["Edit article" gnus-summary-edit-article
1772                (not (gnus-group-read-only-p))]
1773               ["Delete article" gnus-summary-delete-article
1774                (gnus-check-backend-function
1775                 'request-expire-articles gnus-newsgroup-name)]
1776               ["Query respool" gnus-summary-respool-query t]
1777               ["Trace respool" gnus-summary-respool-trace t]
1778               ["Delete expirable articles" gnus-summary-expire-articles-now
1779                (gnus-check-backend-function
1780                 'request-expire-articles gnus-newsgroup-name)])
1781              ("Extract"
1782               ["Uudecode" gnus-uu-decode-uu t]
1783               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1784               ["Unshar" gnus-uu-decode-unshar t]
1785               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1786               ["Save" gnus-uu-decode-save t]
1787               ["Binhex" gnus-uu-decode-binhex t]
1788               ["Postscript" gnus-uu-decode-postscript t])
1789              ("Cache"
1790               ["Enter article" gnus-cache-enter-article t]
1791               ["Remove article" gnus-cache-remove-article t])
1792              ["Translate" gnus-article-babel t]
1793              ["Select article buffer" gnus-summary-select-article-buffer t]
1794              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1795              ["Isearch article..." gnus-summary-isearch-article t]
1796              ["Beginning of the article" gnus-summary-beginning-of-article t]
1797              ["End of the article" gnus-summary-end-of-article t]
1798              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1799              ["Fetch referenced articles" gnus-summary-refer-references t]
1800              ["Fetch current thread" gnus-summary-refer-thread t]
1801              ["Fetch article with id..." gnus-summary-refer-article t]
1802              ["Redisplay" gnus-summary-show-article t])))
1803       (easy-menu-define
1804        gnus-summary-article-menu gnus-summary-mode-map ""
1805        (cons "Article" innards))
1806
1807       (easy-menu-define
1808        gnus-article-commands-menu gnus-article-mode-map ""
1809        (cons "Commands" innards)))
1810
1811     (easy-menu-define
1812      gnus-summary-thread-menu gnus-summary-mode-map ""
1813      '("Threads"
1814        ["Toggle threading" gnus-summary-toggle-threads t]
1815        ["Hide threads" gnus-summary-hide-all-threads t]
1816        ["Show threads" gnus-summary-show-all-threads t]
1817        ["Hide thread" gnus-summary-hide-thread t]
1818        ["Show thread" gnus-summary-show-thread t]
1819        ["Go to next thread" gnus-summary-next-thread t]
1820        ["Go to previous thread" gnus-summary-prev-thread t]
1821        ["Go down thread" gnus-summary-down-thread t]
1822        ["Go up thread" gnus-summary-up-thread t]
1823        ["Top of thread" gnus-summary-top-thread t]
1824        ["Mark thread as read" gnus-summary-kill-thread t]
1825        ["Lower thread score" gnus-summary-lower-thread t]
1826        ["Raise thread score" gnus-summary-raise-thread t]
1827        ["Rethread current" gnus-summary-rethread-current t]))
1828
1829     (easy-menu-define
1830      gnus-summary-post-menu gnus-summary-mode-map ""
1831      '("Post"
1832        ["Post an article" gnus-summary-post-news t]
1833        ["Followup" gnus-summary-followup t]
1834        ["Followup and yank" gnus-summary-followup-with-original t]
1835        ["Supersede article" gnus-summary-supersede-article t]
1836        ["Cancel article" gnus-summary-cancel-article t]
1837        ["Reply" gnus-summary-reply t]
1838        ["Reply and yank" gnus-summary-reply-with-original t]
1839        ["Wide reply" gnus-summary-wide-reply t]
1840        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1841        ["Mail forward" gnus-summary-mail-forward t]
1842        ["Post forward" gnus-summary-post-forward t]
1843        ["Digest and mail" gnus-uu-digest-mail-forward t]
1844        ["Digest and post" gnus-uu-digest-post-forward t]
1845        ["Resend message" gnus-summary-resend-message t]
1846        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1847        ["Send a mail" gnus-summary-mail-other-window t]
1848        ["Uuencode and post" gnus-uu-post-news t]
1849        ["Followup via news" gnus-summary-followup-to-mail t]
1850        ["Followup via news and yank"
1851         gnus-summary-followup-to-mail-with-original t]
1852        ;;("Draft"
1853        ;;["Send" gnus-summary-send-draft t]
1854        ;;["Send bounced" gnus-resend-bounced-mail t])
1855        ))
1856
1857     (easy-menu-define
1858      gnus-summary-misc-menu gnus-summary-mode-map ""
1859      '("Misc"
1860        ("Mark Read"
1861         ["Mark as read" gnus-summary-mark-as-read-forward t]
1862         ["Mark same subject and select"
1863          gnus-summary-kill-same-subject-and-select t]
1864         ["Mark same subject" gnus-summary-kill-same-subject t]
1865         ["Catchup" gnus-summary-catchup t]
1866         ["Catchup all" gnus-summary-catchup-all t]
1867         ["Catchup to here" gnus-summary-catchup-to-here t]
1868         ["Catchup region" gnus-summary-mark-region-as-read t]
1869         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1870        ("Mark Various"
1871         ["Tick" gnus-summary-tick-article-forward t]
1872         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1873         ["Remove marks" gnus-summary-clear-mark-forward t]
1874         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1875         ["Set bookmark" gnus-summary-set-bookmark t]
1876         ["Remove bookmark" gnus-summary-remove-bookmark t])
1877        ("Mark Limit"
1878         ["Marks..." gnus-summary-limit-to-marks t]
1879         ["Subject..." gnus-summary-limit-to-subject t]
1880         ["Author..." gnus-summary-limit-to-author t]
1881         ["Age..." gnus-summary-limit-to-age t]
1882         ["Extra..." gnus-summary-limit-to-extra t]
1883         ["Score" gnus-summary-limit-to-score t]
1884         ["Unread" gnus-summary-limit-to-unread t]
1885         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1886         ["Articles" gnus-summary-limit-to-articles t]
1887         ["Pop limit" gnus-summary-pop-limit t]
1888         ["Show dormant" gnus-summary-limit-include-dormant t]
1889         ["Hide childless dormant"
1890          gnus-summary-limit-exclude-childless-dormant t]
1891         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1892         ["Hide marked" gnus-summary-limit-exclude-marks t]
1893         ["Show expunged" gnus-summary-show-all-expunged t])
1894        ("Process Mark"
1895         ["Set mark" gnus-summary-mark-as-processable t]
1896         ["Remove mark" gnus-summary-unmark-as-processable t]
1897         ["Remove all marks" gnus-summary-unmark-all-processable t]
1898         ["Mark above" gnus-uu-mark-over t]
1899         ["Mark series" gnus-uu-mark-series t]
1900         ["Mark region" gnus-uu-mark-region t]
1901         ["Unmark region" gnus-uu-unmark-region t]
1902         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1903         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1904         ["Mark all" gnus-uu-mark-all t]
1905         ["Mark buffer" gnus-uu-mark-buffer t]
1906         ["Mark sparse" gnus-uu-mark-sparse t]
1907         ["Mark thread" gnus-uu-mark-thread t]
1908         ["Unmark thread" gnus-uu-unmark-thread t]
1909         ("Process Mark Sets"
1910          ["Kill" gnus-summary-kill-process-mark t]
1911          ["Yank" gnus-summary-yank-process-mark
1912           gnus-newsgroup-process-stack]
1913          ["Save" gnus-summary-save-process-mark t]))
1914        ("Scroll article"
1915         ["Page forward" gnus-summary-next-page t]
1916         ["Page backward" gnus-summary-prev-page t]
1917         ["Line forward" gnus-summary-scroll-up t])
1918        ("Move"
1919         ["Next unread article" gnus-summary-next-unread-article t]
1920         ["Previous unread article" gnus-summary-prev-unread-article t]
1921         ["Next article" gnus-summary-next-article t]
1922         ["Previous article" gnus-summary-prev-article t]
1923         ["Next unread subject" gnus-summary-next-unread-subject t]
1924         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1925         ["Next article same subject" gnus-summary-next-same-subject t]
1926         ["Previous article same subject" gnus-summary-prev-same-subject t]
1927         ["First unread article" gnus-summary-first-unread-article t]
1928         ["Best unread article" gnus-summary-best-unread-article t]
1929         ["Go to subject number..." gnus-summary-goto-subject t]
1930         ["Go to article number..." gnus-summary-goto-article t]
1931         ["Go to the last article" gnus-summary-goto-last-article t]
1932         ["Pop article off history" gnus-summary-pop-article t])
1933        ("Sort"
1934         ["Sort by number" gnus-summary-sort-by-number t]
1935         ["Sort by author" gnus-summary-sort-by-author t]
1936         ["Sort by subject" gnus-summary-sort-by-subject t]
1937         ["Sort by date" gnus-summary-sort-by-date t]
1938         ["Sort by score" gnus-summary-sort-by-score t]
1939         ["Sort by lines" gnus-summary-sort-by-lines t]
1940         ["Sort by characters" gnus-summary-sort-by-chars t])
1941        ("Help"
1942         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1943         ["Describe group" gnus-summary-describe-group t]
1944         ["Read manual" gnus-info-find-node t])
1945        ("Modes"
1946         ["Pick and read" gnus-pick-mode t]
1947         ["Binary" gnus-binary-mode t])
1948        ("Regeneration"
1949         ["Regenerate" gnus-summary-prepare t]
1950         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1951         ["Toggle threading" gnus-summary-toggle-threads t])
1952        ["Filter articles..." gnus-summary-execute-command t]
1953        ["Run command on subjects..." gnus-summary-universal-argument t]
1954        ["Search articles forward..." gnus-summary-search-article-forward t]
1955        ["Search articles backward..." gnus-summary-search-article-backward t]
1956        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1957        ["Expand window" gnus-summary-expand-window t]
1958        ["Expire expirable articles" gnus-summary-expire-articles
1959         (gnus-check-backend-function
1960          'request-expire-articles gnus-newsgroup-name)]
1961        ["Edit local kill file" gnus-summary-edit-local-kill t]
1962        ["Edit main kill file" gnus-summary-edit-global-kill t]
1963        ["Edit group parameters" gnus-summary-edit-parameters t]
1964        ["Customize group parameters" gnus-summary-customize-parameters t]
1965        ["Send a bug report" gnus-bug t]
1966        ("Exit"
1967         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1968         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1969         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1970         ["Exit group" gnus-summary-exit t]
1971         ["Exit group without updating" gnus-summary-exit-no-update t]
1972         ["Exit and goto next group" gnus-summary-next-group t]
1973         ["Exit and goto prev group" gnus-summary-prev-group t]
1974         ["Reselect group" gnus-summary-reselect-current-group t]
1975         ["Rescan group" gnus-summary-rescan-group t]
1976         ["Update dribble" gnus-summary-save-newsrc t])))
1977
1978     (gnus-run-hooks 'gnus-summary-menu-hook)))
1979
1980 (defun gnus-score-set-default (var value)
1981   "A version of set that updates the GNU Emacs menu-bar."
1982   (set var value)
1983   ;; It is the message that forces the active status to be updated.
1984   (message ""))
1985
1986 (defun gnus-make-score-map (type)
1987   "Make a summary score map of type TYPE."
1988   (if t
1989       nil
1990     (let ((headers '(("author" "from" string)
1991                      ("subject" "subject" string)
1992                      ("article body" "body" string)
1993                      ("article head" "head" string)
1994                      ("xref" "xref" string)
1995                      ("extra header" "extra" string)
1996                      ("lines" "lines" number)
1997                      ("followups to author" "followup" string)))
1998           (types '((number ("less than" <)
1999                            ("greater than" >)
2000                            ("equal" =))
2001                    (string ("substring" s)
2002                            ("exact string" e)
2003                            ("fuzzy string" f)
2004                            ("regexp" r))))
2005           (perms '(("temporary" (current-time-string))
2006                    ("permanent" nil)
2007                    ("immediate" now)))
2008           header)
2009       (list
2010        (apply
2011         'nconc
2012         (list
2013          (if (eq type 'lower)
2014              "Lower score"
2015            "Increase score"))
2016         (let (outh)
2017           (while headers
2018             (setq header (car headers))
2019             (setq outh
2020                   (cons
2021                    (apply
2022                     'nconc
2023                     (list (car header))
2024                     (let ((ts (cdr (assoc (nth 2 header) types)))
2025                           outt)
2026                       (while ts
2027                         (setq outt
2028                               (cons
2029                                (apply
2030                                 'nconc
2031                                 (list (caar ts))
2032                                 (let ((ps perms)
2033                                       outp)
2034                                   (while ps
2035                                     (setq outp
2036                                           (cons
2037                                            (vector
2038                                             (caar ps)
2039                                             (list
2040                                              'gnus-summary-score-entry
2041                                              (nth 1 header)
2042                                              (if (or (string= (nth 1 header)
2043                                                               "head")
2044                                                      (string= (nth 1 header)
2045                                                               "body"))
2046                                                  ""
2047                                                (list 'gnus-summary-header
2048                                                      (nth 1 header)))
2049                                              (list 'quote (nth 1 (car ts)))
2050                                              (list 'gnus-score-delta-default
2051                                                    nil)
2052                                              (nth 1 (car ps))
2053                                              t)
2054                                             t)
2055                                            outp))
2056                                     (setq ps (cdr ps)))
2057                                   (list (nreverse outp))))
2058                                outt))
2059                         (setq ts (cdr ts)))
2060                       (list (nreverse outt))))
2061                    outh))
2062             (setq headers (cdr headers)))
2063           (list (nreverse outh))))))))
2064
2065 \f
2066
2067 (defun gnus-summary-mode (&optional group)
2068   "Major mode for reading articles.
2069
2070 All normal editing commands are switched off.
2071 \\<gnus-summary-mode-map>
2072 Each line in this buffer represents one article.  To read an
2073 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2074 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2075 respectively.
2076
2077 You can also post articles and send mail from this buffer.  To
2078 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2079 of an article, type `\\[gnus-summary-reply]'.
2080
2081 There are approx. one gazillion commands you can execute in this
2082 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2083
2084 The following commands are available:
2085
2086 \\{gnus-summary-mode-map}"
2087   (interactive)
2088   (when (gnus-visual-p 'summary-menu 'menu)
2089     (gnus-summary-make-menu-bar))
2090   (kill-all-local-variables)
2091   (gnus-summary-make-local-variables)
2092   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2093     (gnus-summary-make-local-variables))
2094   (gnus-make-thread-indent-array)
2095   (gnus-simplify-mode-line)
2096   (setq major-mode 'gnus-summary-mode)
2097   (setq mode-name "Summary")
2098   (make-local-variable 'minor-mode-alist)
2099   (use-local-map gnus-summary-mode-map)
2100   (buffer-disable-undo)
2101   (setq buffer-read-only t)             ;Disable modification
2102   (setq truncate-lines t)
2103   (setq selective-display t)
2104   (setq selective-display-ellipses t)   ;Display `...'
2105   (gnus-summary-set-display-table)
2106   (gnus-set-default-directory)
2107   (setq gnus-newsgroup-name group)
2108   (make-local-variable 'gnus-summary-line-format)
2109   (make-local-variable 'gnus-summary-line-format-spec)
2110   (make-local-variable 'gnus-summary-dummy-line-format)
2111   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2112   (make-local-variable 'gnus-summary-mark-positions)
2113   (make-local-hook 'pre-command-hook)
2114   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2115   (gnus-run-hooks 'gnus-summary-mode-hook)
2116   (mm-enable-multibyte-mule4)
2117   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2118   (gnus-update-summary-mark-positions))
2119
2120 (defun gnus-summary-make-local-variables ()
2121   "Make all the local summary buffer variables."
2122   (let (global)
2123     (dolist (local gnus-summary-local-variables)
2124       (if (consp local)
2125           (progn
2126             (if (eq (cdr local) 'global)
2127                 ;; Copy the global value of the variable.
2128                 (setq global (symbol-value (car local)))
2129               ;; Use the value from the list.
2130               (setq global (eval (cdr local))))
2131             (set (make-local-variable (car local)) global))
2132         ;; Simple nil-valued local variable.
2133         (set (make-local-variable local) nil)))))
2134
2135 (defun gnus-summary-clear-local-variables ()
2136   (let ((locals gnus-summary-local-variables))
2137     (while locals
2138       (if (consp (car locals))
2139           (and (vectorp (caar locals))
2140                (set (caar locals) nil))
2141         (and (vectorp (car locals))
2142              (set (car locals) nil)))
2143       (setq locals (cdr locals)))))
2144
2145 ;; Summary data functions.
2146
2147 (defmacro gnus-data-number (data)
2148   `(car ,data))
2149
2150 (defmacro gnus-data-set-number (data number)
2151   `(setcar ,data ,number))
2152
2153 (defmacro gnus-data-mark (data)
2154   `(nth 1 ,data))
2155
2156 (defmacro gnus-data-set-mark (data mark)
2157   `(setcar (nthcdr 1 ,data) ,mark))
2158
2159 (defmacro gnus-data-pos (data)
2160   `(nth 2 ,data))
2161
2162 (defmacro gnus-data-set-pos (data pos)
2163   `(setcar (nthcdr 2 ,data) ,pos))
2164
2165 (defmacro gnus-data-header (data)
2166   `(nth 3 ,data))
2167
2168 (defmacro gnus-data-set-header (data header)
2169   `(setf (nth 3 ,data) ,header))
2170
2171 (defmacro gnus-data-level (data)
2172   `(nth 4 ,data))
2173
2174 (defmacro gnus-data-unread-p (data)
2175   `(= (nth 1 ,data) gnus-unread-mark))
2176
2177 (defmacro gnus-data-read-p (data)
2178   `(/= (nth 1 ,data) gnus-unread-mark))
2179
2180 (defmacro gnus-data-pseudo-p (data)
2181   `(consp (nth 3 ,data)))
2182
2183 (defmacro gnus-data-find (number)
2184   `(assq ,number gnus-newsgroup-data))
2185
2186 (defmacro gnus-data-find-list (number &optional data)
2187   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2188      (memq (assq ,number bdata)
2189            bdata)))
2190
2191 (defmacro gnus-data-make (number mark pos header level)
2192   `(list ,number ,mark ,pos ,header ,level))
2193
2194 (defun gnus-data-enter (after-article number mark pos header level offset)
2195   (let ((data (gnus-data-find-list after-article)))
2196     (unless data
2197       (error "No such article: %d" after-article))
2198     (setcdr data (cons (gnus-data-make number mark pos header level)
2199                        (cdr data)))
2200     (setq gnus-newsgroup-data-reverse nil)
2201     (gnus-data-update-list (cddr data) offset)))
2202
2203 (defun gnus-data-enter-list (after-article list &optional offset)
2204   (when list
2205     (let ((data (and after-article (gnus-data-find-list after-article)))
2206           (ilist list))
2207       (if (not (or data
2208                    after-article))
2209           (let ((odata gnus-newsgroup-data))
2210             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2211             (when offset
2212               (gnus-data-update-list odata offset)))
2213         ;; Find the last element in the list to be spliced into the main
2214         ;; list.
2215         (while (cdr list)
2216           (setq list (cdr list)))
2217         (if (not data)
2218             (progn
2219               (setcdr list gnus-newsgroup-data)
2220               (setq gnus-newsgroup-data ilist)
2221               (when offset
2222                 (gnus-data-update-list (cdr list) offset)))
2223           (setcdr list (cdr data))
2224           (setcdr data ilist)
2225           (when offset
2226             (gnus-data-update-list (cdr list) offset))))
2227       (setq gnus-newsgroup-data-reverse nil))))
2228
2229 (defun gnus-data-remove (article &optional offset)
2230   (let ((data gnus-newsgroup-data))
2231     (if (= (gnus-data-number (car data)) article)
2232         (progn
2233           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2234                 gnus-newsgroup-data-reverse nil)
2235           (when offset
2236             (gnus-data-update-list gnus-newsgroup-data offset)))
2237       (while (cdr data)
2238         (when (= (gnus-data-number (cadr data)) article)
2239           (setcdr data (cddr data))
2240           (when offset
2241             (gnus-data-update-list (cdr data) offset))
2242           (setq data nil
2243                 gnus-newsgroup-data-reverse nil))
2244         (setq data (cdr data))))))
2245
2246 (defmacro gnus-data-list (backward)
2247   `(if ,backward
2248        (or gnus-newsgroup-data-reverse
2249            (setq gnus-newsgroup-data-reverse
2250                  (reverse gnus-newsgroup-data)))
2251      gnus-newsgroup-data))
2252
2253 (defun gnus-data-update-list (data offset)
2254   "Add OFFSET to the POS of all data entries in DATA."
2255   (setq gnus-newsgroup-data-reverse nil)
2256   (while data
2257     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2258     (setq data (cdr data))))
2259
2260 (defun gnus-summary-article-pseudo-p (article)
2261   "Say whether this article is a pseudo article or not."
2262   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2263
2264 (defmacro gnus-summary-article-sparse-p (article)
2265   "Say whether this article is a sparse article or not."
2266   `(memq ,article gnus-newsgroup-sparse))
2267
2268 (defmacro gnus-summary-article-ancient-p (article)
2269   "Say whether this article is a sparse article or not."
2270   `(memq ,article gnus-newsgroup-ancient))
2271
2272 (defun gnus-article-parent-p (number)
2273   "Say whether this article is a parent or not."
2274   (let ((data (gnus-data-find-list number)))
2275     (and (cdr data)                     ; There has to be an article after...
2276          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2277             (gnus-data-level (nth 1 data))))))
2278
2279 (defun gnus-article-children (number)
2280   "Return a list of all children to NUMBER."
2281   (let* ((data (gnus-data-find-list number))
2282          (level (gnus-data-level (car data)))
2283          children)
2284     (setq data (cdr data))
2285     (while (and data
2286                 (= (gnus-data-level (car data)) (1+ level)))
2287       (push (gnus-data-number (car data)) children)
2288       (setq data (cdr data)))
2289     children))
2290
2291 (defmacro gnus-summary-skip-intangible ()
2292   "If the current article is intangible, then jump to a different article."
2293   '(let ((to (get-text-property (point) 'gnus-intangible)))
2294      (and to (gnus-summary-goto-subject to))))
2295
2296 (defmacro gnus-summary-article-intangible-p ()
2297   "Say whether this article is intangible or not."
2298   '(get-text-property (point) 'gnus-intangible))
2299
2300 (defun gnus-article-read-p (article)
2301   "Say whether ARTICLE is read or not."
2302   (not (or (memq article gnus-newsgroup-marked)
2303            (memq article gnus-newsgroup-unreads)
2304            (memq article gnus-newsgroup-unselected)
2305            (memq article gnus-newsgroup-dormant))))
2306
2307 ;; Some summary mode macros.
2308
2309 (defmacro gnus-summary-article-number ()
2310   "The article number of the article on the current line.
2311 If there isn's an article number here, then we return the current
2312 article number."
2313   '(progn
2314      (gnus-summary-skip-intangible)
2315      (or (get-text-property (point) 'gnus-number)
2316          (gnus-summary-last-subject))))
2317
2318 (defmacro gnus-summary-article-header (&optional number)
2319   "Return the header of article NUMBER."
2320   `(gnus-data-header (gnus-data-find
2321                       ,(or number '(gnus-summary-article-number)))))
2322
2323 (defmacro gnus-summary-thread-level (&optional number)
2324   "Return the level of thread that starts with article NUMBER."
2325   `(if (and (eq gnus-summary-make-false-root 'dummy)
2326             (get-text-property (point) 'gnus-intangible))
2327        0
2328      (gnus-data-level (gnus-data-find
2329                        ,(or number '(gnus-summary-article-number))))))
2330
2331 (defmacro gnus-summary-article-mark (&optional number)
2332   "Return the mark of article NUMBER."
2333   `(gnus-data-mark (gnus-data-find
2334                     ,(or number '(gnus-summary-article-number)))))
2335
2336 (defmacro gnus-summary-article-pos (&optional number)
2337   "Return the position of the line of article NUMBER."
2338   `(gnus-data-pos (gnus-data-find
2339                    ,(or number '(gnus-summary-article-number)))))
2340
2341 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2342 (defmacro gnus-summary-article-subject (&optional number)
2343   "Return current subject string or nil if nothing."
2344   `(let ((headers
2345           ,(if number
2346                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2347              '(gnus-data-header (assq (gnus-summary-article-number)
2348                                       gnus-newsgroup-data)))))
2349      (and headers
2350           (vectorp headers)
2351           (mail-header-subject headers))))
2352
2353 (defmacro gnus-summary-article-score (&optional number)
2354   "Return current article score."
2355   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2356                   gnus-newsgroup-scored))
2357        gnus-summary-default-score 0))
2358
2359 (defun gnus-summary-article-children (&optional number)
2360   "Return a list of article numbers that are children of article NUMBER."
2361   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2362          (level (gnus-data-level (car data)))
2363          l children)
2364     (while (and (setq data (cdr data))
2365                 (> (setq l (gnus-data-level (car data))) level))
2366       (and (= (1+ level) l)
2367            (push (gnus-data-number (car data))
2368                  children)))
2369     (nreverse children)))
2370
2371 (defun gnus-summary-article-parent (&optional number)
2372   "Return the article number of the parent of article NUMBER."
2373   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2374                                     (gnus-data-list t)))
2375          (level (gnus-data-level (car data))))
2376     (if (zerop level)
2377         ()                              ; This is a root.
2378       ;; We search until we find an article with a level less than
2379       ;; this one.  That function has to be the parent.
2380       (while (and (setq data (cdr data))
2381                   (not (< (gnus-data-level (car data)) level))))
2382       (and data (gnus-data-number (car data))))))
2383
2384 (defun gnus-unread-mark-p (mark)
2385   "Say whether MARK is the unread mark."
2386   (= mark gnus-unread-mark))
2387
2388 (defun gnus-read-mark-p (mark)
2389   "Say whether MARK is one of the marks that mark as read.
2390 This is all marks except unread, ticked, dormant, and expirable."
2391   (not (or (= mark gnus-unread-mark)
2392            (= mark gnus-ticked-mark)
2393            (= mark gnus-dormant-mark)
2394            (= mark gnus-expirable-mark))))
2395
2396 (defmacro gnus-article-mark (number)
2397   "Return the MARK of article NUMBER.
2398 This macro should only be used when computing the mark the \"first\"
2399 time; i.e., when generating the summary lines.  After that,
2400 `gnus-summary-article-mark' should be used to examine the
2401 marks of articles."
2402   `(cond
2403     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2404     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2405     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2406     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2407     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2408     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2409     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2410     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2411            gnus-ancient-mark))))
2412
2413 ;; Saving hidden threads.
2414
2415 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2416 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2417
2418 (defmacro gnus-save-hidden-threads (&rest forms)
2419   "Save hidden threads, eval FORMS, and restore the hidden threads."
2420   (let ((config (make-symbol "config")))
2421     `(let ((,config (gnus-hidden-threads-configuration)))
2422        (unwind-protect
2423            (save-excursion
2424              ,@forms)
2425          (gnus-restore-hidden-threads-configuration ,config)))))
2426
2427 (defun gnus-data-compute-positions ()
2428   "Compute the positions of all articles."
2429   (setq gnus-newsgroup-data-reverse nil)
2430   (let ((data gnus-newsgroup-data))
2431     (save-excursion
2432       (gnus-save-hidden-threads
2433         (gnus-summary-show-all-threads)
2434         (goto-char (point-min))
2435         (while data
2436           (while (get-text-property (point) 'gnus-intangible)
2437             (forward-line 1))
2438           (gnus-data-set-pos (car data) (+ (point) 3))
2439           (setq data (cdr data))
2440           (forward-line 1))))))
2441
2442 (defun gnus-hidden-threads-configuration ()
2443   "Return the current hidden threads configuration."
2444   (save-excursion
2445     (let (config)
2446       (goto-char (point-min))
2447       (while (search-forward "\r" nil t)
2448         (push (1- (point)) config))
2449       config)))
2450
2451 (defun gnus-restore-hidden-threads-configuration (config)
2452   "Restore hidden threads configuration from CONFIG."
2453   (save-excursion
2454     (let (point buffer-read-only)
2455       (while (setq point (pop config))
2456         (when (and (< point (point-max))
2457                    (goto-char point)
2458                    (eq (char-after) ?\n))
2459           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2460
2461 ;; Various summary mode internalish functions.
2462
2463 (defun gnus-mouse-pick-article (e)
2464   (interactive "e")
2465   (mouse-set-point e)
2466   (gnus-summary-next-page nil t))
2467
2468 (defun gnus-summary-set-display-table ()
2469   "Change the display table.
2470 Odd characters have a tendency to mess
2471 up nicely formatted displays - we make all possible glyphs
2472 display only a single character."
2473
2474   ;; We start from the standard display table, if any.
2475   (let ((table (or (copy-sequence standard-display-table)
2476                    (make-display-table)))
2477         (i 32))
2478     ;; Nix out all the control chars...
2479     (while (>= (setq i (1- i)) 0)
2480       (aset table i [??]))
2481     ;; ... but not newline and cr, of course.  (cr is necessary for the
2482     ;; selective display).
2483     (aset table ?\n nil)
2484     (aset table ?\r nil)
2485     ;; We keep TAB as well.
2486     (aset table ?\t nil)
2487     ;; We nix out any glyphs over 126 that are not set already.
2488     (let ((i 256))
2489       (while (>= (setq i (1- i)) 127)
2490         ;; Only modify if the entry is nil.
2491         (unless (aref table i)
2492           (aset table i [??]))))
2493     (setq buffer-display-table table)))
2494
2495 (defun gnus-summary-setup-buffer (group)
2496   "Initialize summary buffer."
2497   (let ((buffer (concat "*Summary " group "*")))
2498     (if (get-buffer buffer)
2499         (progn
2500           (set-buffer buffer)
2501           (setq gnus-summary-buffer (current-buffer))
2502           (not gnus-newsgroup-prepared))
2503       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2504       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2505       (gnus-summary-mode group)
2506       (when gnus-carpal
2507         (gnus-carpal-setup-buffer 'summary))
2508       (unless gnus-single-article-buffer
2509         (make-local-variable 'gnus-article-buffer)
2510         (make-local-variable 'gnus-article-current)
2511         (make-local-variable 'gnus-original-article-buffer))
2512       (setq gnus-newsgroup-name group)
2513       t)))
2514
2515 (defun gnus-set-global-variables ()
2516   "Set the global equivalents of the buffer-local variables.
2517 They are set to the latest values they had.  These reflect the summary
2518 buffer that was in action when the last article was fetched."
2519   (when (eq major-mode 'gnus-summary-mode)
2520     (setq gnus-summary-buffer (current-buffer))
2521     (let ((name gnus-newsgroup-name)
2522           (marked gnus-newsgroup-marked)
2523           (unread gnus-newsgroup-unreads)
2524           (headers gnus-current-headers)
2525           (data gnus-newsgroup-data)
2526           (summary gnus-summary-buffer)
2527           (article-buffer gnus-article-buffer)
2528           (original gnus-original-article-buffer)
2529           (gac gnus-article-current)
2530           (reffed gnus-reffed-article-number)
2531           (score-file gnus-current-score-file)
2532           (default-charset gnus-newsgroup-charset)
2533           vlist)
2534       (let ((locals gnus-newsgroup-variables))
2535         (while locals
2536           (if (consp (car locals))
2537               (push (eval (caar locals)) vlist)
2538             (push (eval (car locals)) vlist))
2539           (setq locals (cdr locals)))
2540         (setq vlist (nreverse vlist)))
2541       (save-excursion
2542         (set-buffer gnus-group-buffer)
2543         (setq gnus-newsgroup-name name
2544               gnus-newsgroup-marked marked
2545               gnus-newsgroup-unreads unread
2546               gnus-current-headers headers
2547               gnus-newsgroup-data data
2548               gnus-article-current gac
2549               gnus-summary-buffer summary
2550               gnus-article-buffer article-buffer
2551               gnus-original-article-buffer original
2552               gnus-reffed-article-number reffed
2553               gnus-current-score-file score-file
2554               gnus-newsgroup-charset default-charset)
2555         (let ((locals gnus-newsgroup-variables))
2556           (while locals
2557             (if (consp (car locals))
2558                 (set (caar locals) (pop vlist))
2559               (set (car locals) (pop vlist)))
2560             (setq locals (cdr locals))))
2561         ;; The article buffer also has local variables.
2562         (when (gnus-buffer-live-p gnus-article-buffer)
2563           (set-buffer gnus-article-buffer)
2564           (setq gnus-summary-buffer summary))))))
2565
2566 (defun gnus-summary-article-unread-p (article)
2567   "Say whether ARTICLE is unread or not."
2568   (memq article gnus-newsgroup-unreads))
2569
2570 (defun gnus-summary-first-article-p (&optional article)
2571   "Return whether ARTICLE is the first article in the buffer."
2572   (if (not (setq article (or article (gnus-summary-article-number))))
2573       nil
2574     (eq article (caar gnus-newsgroup-data))))
2575
2576 (defun gnus-summary-last-article-p (&optional article)
2577   "Return whether ARTICLE is the last article in the buffer."
2578   (if (not (setq article (or article (gnus-summary-article-number))))
2579       ;; All non-existent numbers are the last article.  :-)
2580       t
2581     (not (cdr (gnus-data-find-list article)))))
2582
2583 (defun gnus-make-thread-indent-array ()
2584   (let ((n 200))
2585     (unless (and gnus-thread-indent-array
2586                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2587       (setq gnus-thread-indent-array (make-vector 201 "")
2588             gnus-thread-indent-array-level gnus-thread-indent-level)
2589       (while (>= n 0)
2590         (aset gnus-thread-indent-array n
2591               (make-string (* n gnus-thread-indent-level) ? ))
2592         (setq n (1- n))))))
2593
2594 (defun gnus-update-summary-mark-positions ()
2595   "Compute where the summary marks are to go."
2596   (save-excursion
2597     (when (gnus-buffer-exists-p gnus-summary-buffer)
2598       (set-buffer gnus-summary-buffer))
2599     (let ((gnus-replied-mark 129)
2600           (gnus-score-below-mark 130)
2601           (gnus-score-over-mark 130)
2602           (gnus-download-mark 131)
2603           (spec gnus-summary-line-format-spec)
2604           gnus-visual pos)
2605       (save-excursion
2606         (gnus-set-work-buffer)
2607         (let ((gnus-summary-line-format-spec spec)
2608               (gnus-newsgroup-downloadable '((0 . t))))
2609           (gnus-summary-insert-line
2610            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2611           (goto-char (point-min))
2612           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2613                                              (- (point) 2)))))
2614           (goto-char (point-min))
2615           (push (cons 'replied (and (search-forward "\201" nil t)
2616                                     (- (point) 2)))
2617                 pos)
2618           (goto-char (point-min))
2619           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2620                 pos)
2621           (goto-char (point-min))
2622           (push (cons 'download
2623                       (and (search-forward "\203" nil t) (- (point) 2)))
2624                 pos)))
2625       (setq gnus-summary-mark-positions pos))))
2626
2627 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2628   "Insert a dummy root in the summary buffer."
2629   (beginning-of-line)
2630   (gnus-add-text-properties
2631    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2632    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2633
2634 (defun gnus-summary-from-or-to-or-newsgroups (header)
2635   (let ((to (cdr (assq 'To (mail-header-extra header))))
2636         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2637         (mail-parse-charset gnus-newsgroup-charset)
2638         (mail-parse-ignored-charsets
2639          (save-excursion (set-buffer gnus-summary-buffer)
2640                          gnus-newsgroup-ignored-charsets)))
2641     (cond
2642      ((and to
2643            gnus-ignored-from-addresses
2644            (string-match gnus-ignored-from-addresses
2645                          (mail-header-from header)))
2646       (concat "-> "
2647               (or (car (funcall gnus-extract-address-components
2648                                 (funcall
2649                                  gnus-decode-encoded-word-function to)))
2650                   (funcall gnus-decode-encoded-word-function to))))
2651      ((and newsgroups
2652            gnus-ignored-from-addresses
2653            (string-match gnus-ignored-from-addresses
2654                          (mail-header-from header)))
2655       (concat "=> " newsgroups))
2656      (t
2657       (or (car (funcall gnus-extract-address-components
2658                         (mail-header-from header)))
2659           (mail-header-from header))))))
2660
2661 (defun gnus-summary-insert-line (gnus-tmp-header
2662                                  gnus-tmp-level gnus-tmp-current
2663                                  gnus-tmp-unread gnus-tmp-replied
2664                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2665                                  &optional gnus-tmp-dummy gnus-tmp-score
2666                                  gnus-tmp-process)
2667   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2668          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2669          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2670          (gnus-tmp-score-char
2671           (if (or (null gnus-summary-default-score)
2672                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2673                       gnus-summary-zcore-fuzz))
2674               ?  ;Whitespace
2675             (if (< gnus-tmp-score gnus-summary-default-score)
2676                 gnus-score-below-mark gnus-score-over-mark)))
2677          (gnus-tmp-replied
2678           (cond (gnus-tmp-process gnus-process-mark)
2679                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2680                  gnus-cached-mark)
2681                 (gnus-tmp-replied gnus-replied-mark)
2682                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2683                  gnus-saved-mark)
2684                 (t gnus-unread-mark)))
2685          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2686          (gnus-tmp-name
2687           (cond
2688            ((string-match "<[^>]+> *$" gnus-tmp-from)
2689             (let ((beg (match-beginning 0)))
2690               (or (and (string-match "^\".+\"" gnus-tmp-from)
2691                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2692                   (substring gnus-tmp-from 0 beg))))
2693            ((string-match "(.+)" gnus-tmp-from)
2694             (substring gnus-tmp-from
2695                        (1+ (match-beginning 0)) (1- (match-end 0))))
2696            (t gnus-tmp-from)))
2697          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2698          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2699          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2700          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2701          (buffer-read-only nil))
2702     (when (string= gnus-tmp-name "")
2703       (setq gnus-tmp-name gnus-tmp-from))
2704     (unless (numberp gnus-tmp-lines)
2705       (setq gnus-tmp-lines 0))
2706     (gnus-put-text-property
2707      (point)
2708      (progn (eval gnus-summary-line-format-spec) (point))
2709      'gnus-number gnus-tmp-number)
2710     (when (gnus-visual-p 'summary-highlight 'highlight)
2711       (forward-line -1)
2712       (gnus-run-hooks 'gnus-summary-update-hook)
2713       (forward-line 1))))
2714
2715 (defun gnus-summary-update-line (&optional dont-update)
2716   "Update summary line after change."
2717   (when (and gnus-summary-default-score
2718              (not gnus-summary-inhibit-highlight))
2719     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2720            (article (gnus-summary-article-number))
2721            (score (gnus-summary-article-score article)))
2722       (unless dont-update
2723         (if (and gnus-summary-mark-below
2724                  (< (gnus-summary-article-score)
2725                     gnus-summary-mark-below))
2726             ;; This article has a low score, so we mark it as read.
2727             (when (memq article gnus-newsgroup-unreads)
2728               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2729           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2730             ;; This article was previously marked as read on account
2731             ;; of a low score, but now it has risen, so we mark it as
2732             ;; unread.
2733             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2734         (gnus-summary-update-mark
2735          (if (or (null gnus-summary-default-score)
2736                  (<= (abs (- score gnus-summary-default-score))
2737                      gnus-summary-zcore-fuzz))
2738              ?  ;Whitespace
2739            (if (< score gnus-summary-default-score)
2740                gnus-score-below-mark gnus-score-over-mark))
2741          'score))
2742       ;; Do visual highlighting.
2743       (when (gnus-visual-p 'summary-highlight 'highlight)
2744         (gnus-run-hooks 'gnus-summary-update-hook)))))
2745
2746 (defvar gnus-tmp-new-adopts nil)
2747
2748 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2749   "Return the number of articles in THREAD.
2750 This may be 0 in some cases -- if none of the articles in
2751 the thread are to be displayed."
2752   (let* ((number
2753           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2754           (cond
2755            ((not (listp thread))
2756             1)
2757            ((and (consp thread) (cdr thread))
2758             (apply
2759              '+ 1 (mapcar
2760                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2761            ((null thread)
2762             1)
2763            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2764             1)
2765            (t 0))))
2766     (when (and level (zerop level) gnus-tmp-new-adopts)
2767       (incf number
2768             (apply '+ (mapcar
2769                        'gnus-summary-number-of-articles-in-thread
2770                        gnus-tmp-new-adopts))))
2771     (if char
2772         (if (> number 1) gnus-not-empty-thread-mark
2773           gnus-empty-thread-mark)
2774       number)))
2775
2776 (defun gnus-summary-set-local-parameters (group)
2777   "Go through the local params of GROUP and set all variable specs in that list."
2778   (let ((params (gnus-group-find-parameter group))
2779         elem)
2780     (while params
2781       (setq elem (car params)
2782             params (cdr params))
2783       (and (consp elem)                 ; Has to be a cons.
2784            (consp (cdr elem))           ; The cdr has to be a list.
2785            (symbolp (car elem))         ; Has to be a symbol in there.
2786            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2787            (ignore-errors               ; So we set it.
2788              (make-local-variable (car elem))
2789              (set (car elem) (eval (nth 1 elem))))))))
2790
2791 (defun gnus-summary-read-group (group &optional show-all no-article
2792                                       kill-buffer no-display backward
2793                                       select-articles)
2794   "Start reading news in newsgroup GROUP.
2795 If SHOW-ALL is non-nil, already read articles are also listed.
2796 If NO-ARTICLE is non-nil, no article is selected initially.
2797 If NO-DISPLAY, don't generate a summary buffer."
2798   (let (result)
2799     (while (and group
2800                 (null (setq result
2801                             (let ((gnus-auto-select-next nil))
2802                               (or (gnus-summary-read-group-1
2803                                    group show-all no-article
2804                                    kill-buffer no-display
2805                                    select-articles)
2806                                   (setq show-all nil
2807                                         select-articles nil)))))
2808                 (eq gnus-auto-select-next 'quietly))
2809       (set-buffer gnus-group-buffer)
2810       ;; The entry function called above goes to the next
2811       ;; group automatically, so we go two groups back
2812       ;; if we are searching for the previous group.
2813       (when backward
2814         (gnus-group-prev-unread-group 2))
2815       (if (not (equal group (gnus-group-group-name)))
2816           (setq group (gnus-group-group-name))
2817         (setq group nil)))
2818     result))
2819
2820 (defun gnus-summary-read-group-1 (group show-all no-article
2821                                         kill-buffer no-display
2822                                         &optional select-articles)
2823   ;; Killed foreign groups can't be entered.
2824   (when (and (not (gnus-group-native-p group))
2825              (not (gnus-gethash group gnus-newsrc-hashtb)))
2826     (error "Dead non-native groups can't be entered"))
2827   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2828   (let* ((new-group (gnus-summary-setup-buffer group))
2829          (quit-config (gnus-group-quit-config group))
2830          (did-select (and new-group (gnus-select-newsgroup
2831                                      group show-all select-articles))))
2832     (cond
2833      ;; This summary buffer exists already, so we just select it.
2834      ((not new-group)
2835       (gnus-set-global-variables)
2836       (when kill-buffer
2837         (gnus-kill-or-deaden-summary kill-buffer))
2838       (gnus-configure-windows 'summary 'force)
2839       (gnus-set-mode-line 'summary)
2840       (gnus-summary-position-point)
2841       (message "")
2842       t)
2843      ;; We couldn't select this group.
2844      ((null did-select)
2845       (when (and (eq major-mode 'gnus-summary-mode)
2846                  (not (equal (current-buffer) kill-buffer)))
2847         (kill-buffer (current-buffer))
2848         (if (not quit-config)
2849             (progn
2850               ;; Update the info -- marks might need to be removed,
2851               ;; for instance.
2852               (gnus-summary-update-info)
2853               (set-buffer gnus-group-buffer)
2854               (gnus-group-jump-to-group group)
2855               (gnus-group-next-unread-group 1))
2856           (gnus-handle-ephemeral-exit quit-config)))
2857       (gnus-message 3 "Can't select group")
2858       nil)
2859      ;; The user did a `C-g' while prompting for number of articles,
2860      ;; so we exit this group.
2861      ((eq did-select 'quit)
2862       (and (eq major-mode 'gnus-summary-mode)
2863            (not (equal (current-buffer) kill-buffer))
2864            (kill-buffer (current-buffer)))
2865       (when kill-buffer
2866         (gnus-kill-or-deaden-summary kill-buffer))
2867       (if (not quit-config)
2868           (progn
2869             (set-buffer gnus-group-buffer)
2870             (gnus-group-jump-to-group group)
2871             (gnus-group-next-unread-group 1)
2872             (gnus-configure-windows 'group 'force))
2873         (gnus-handle-ephemeral-exit quit-config))
2874       ;; Finally signal the quit.
2875       (signal 'quit nil))
2876      ;; The group was successfully selected.
2877      (t
2878       (gnus-set-global-variables)
2879       ;; Save the active value in effect when the group was entered.
2880       (setq gnus-newsgroup-active
2881             (gnus-copy-sequence
2882              (gnus-active gnus-newsgroup-name)))
2883       ;; You can change the summary buffer in some way with this hook.
2884       (gnus-run-hooks 'gnus-select-group-hook)
2885       ;; Set any local variables in the group parameters.
2886       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2887       (gnus-update-format-specifications
2888        nil 'summary 'summary-mode 'summary-dummy)
2889       (gnus-update-summary-mark-positions)
2890       ;; Do score processing.
2891       (when gnus-use-scoring
2892         (gnus-possibly-score-headers))
2893       ;; Check whether to fill in the gaps in the threads.
2894       (when gnus-build-sparse-threads
2895         (gnus-build-sparse-threads))
2896       ;; Find the initial limit.
2897       (if gnus-show-threads
2898           (if show-all
2899               (let ((gnus-newsgroup-dormant nil))
2900                 (gnus-summary-initial-limit show-all))
2901             (gnus-summary-initial-limit show-all))
2902         ;; When untreaded, all articles are always shown.
2903         (setq gnus-newsgroup-limit
2904               (mapcar
2905                (lambda (header) (mail-header-number header))
2906                gnus-newsgroup-headers)))
2907       ;; Generate the summary buffer.
2908       (unless no-display
2909         (gnus-summary-prepare))
2910       (when gnus-use-trees
2911         (gnus-tree-open group)
2912         (setq gnus-summary-highlight-line-function
2913               'gnus-tree-highlight-article))
2914       ;; If the summary buffer is empty, but there are some low-scored
2915       ;; articles or some excluded dormants, we include these in the
2916       ;; buffer.
2917       (when (and (zerop (buffer-size))
2918                  (not no-display))
2919         (cond (gnus-newsgroup-dormant
2920                (gnus-summary-limit-include-dormant))
2921               ((and gnus-newsgroup-scored show-all)
2922                (gnus-summary-limit-include-expunged t))))
2923       ;; Function `gnus-apply-kill-file' must be called in this hook.
2924       (gnus-run-hooks 'gnus-apply-kill-hook)
2925       (if (and (zerop (buffer-size))
2926                (not no-display))
2927           (progn
2928             ;; This newsgroup is empty.
2929             (gnus-summary-catchup-and-exit nil t)
2930             (gnus-message 6 "No unread news")
2931             (when kill-buffer
2932               (gnus-kill-or-deaden-summary kill-buffer))
2933             ;; Return nil from this function.
2934             nil)
2935         ;; Hide conversation thread subtrees.  We cannot do this in
2936         ;; gnus-summary-prepare-hook since kill processing may not
2937         ;; work with hidden articles.
2938         (and gnus-show-threads
2939              gnus-thread-hide-subtree
2940              (gnus-summary-hide-all-threads))
2941         (when kill-buffer
2942           (gnus-kill-or-deaden-summary kill-buffer))
2943         ;; Show first unread article if requested.
2944         (if (and (not no-article)
2945                  (not no-display)
2946                  gnus-newsgroup-unreads
2947                  gnus-auto-select-first)
2948             (progn
2949               (gnus-configure-windows 'summary)
2950               (cond
2951                ((eq gnus-auto-select-first 'best)
2952                 (gnus-summary-best-unread-article))
2953                ((eq gnus-auto-select-first t)
2954                 (gnus-summary-first-unread-article))
2955                ((gnus-functionp gnus-auto-select-first)
2956                 (funcall gnus-auto-select-first))))
2957           ;; Don't select any articles, just move point to the first
2958           ;; article in the group.
2959           (goto-char (point-min))
2960           (gnus-summary-position-point)
2961           (gnus-configure-windows 'summary 'force)
2962           (gnus-set-mode-line 'summary))
2963         (when (get-buffer-window gnus-group-buffer t)
2964           ;; Gotta use windows, because recenter does weird stuff if
2965           ;; the current buffer ain't the displayed window.
2966           (let ((owin (selected-window)))
2967             (select-window (get-buffer-window gnus-group-buffer t))
2968             (when (gnus-group-goto-group group)
2969               (recenter))
2970             (select-window owin)))
2971         ;; Mark this buffer as "prepared".
2972         (setq gnus-newsgroup-prepared t)
2973         (gnus-run-hooks 'gnus-summary-prepared-hook)
2974         t)))))
2975
2976 (defun gnus-summary-prepare ()
2977   "Generate the summary buffer."
2978   (interactive)
2979   (let ((buffer-read-only nil))
2980     (erase-buffer)
2981     (setq gnus-newsgroup-data nil
2982           gnus-newsgroup-data-reverse nil)
2983     (gnus-run-hooks 'gnus-summary-generate-hook)
2984     ;; Generate the buffer, either with threads or without.
2985     (when gnus-newsgroup-headers
2986       (gnus-summary-prepare-threads
2987        (if gnus-show-threads
2988            (gnus-sort-gathered-threads
2989             (funcall gnus-summary-thread-gathering-function
2990                      (gnus-sort-threads
2991                       (gnus-cut-threads (gnus-make-threads)))))
2992          ;; Unthreaded display.
2993          (gnus-sort-articles gnus-newsgroup-headers))))
2994     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2995     ;; Call hooks for modifying summary buffer.
2996     (goto-char (point-min))
2997     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2998
2999 (defsubst gnus-general-simplify-subject (subject)
3000   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3001   (setq subject
3002         (cond
3003          ;; Truncate the subject.
3004          (gnus-simplify-subject-functions
3005           (gnus-map-function gnus-simplify-subject-functions subject))
3006          ((numberp gnus-summary-gather-subject-limit)
3007           (setq subject (gnus-simplify-subject-re subject))
3008           (if (> (length subject) gnus-summary-gather-subject-limit)
3009               (substring subject 0 gnus-summary-gather-subject-limit)
3010             subject))
3011          ;; Fuzzily simplify it.
3012          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3013           (gnus-simplify-subject-fuzzy subject))
3014          ;; Just remove the leading "Re:".
3015          (t
3016           (gnus-simplify-subject-re subject))))
3017
3018   (if (and gnus-summary-gather-exclude-subject
3019            (string-match gnus-summary-gather-exclude-subject subject))
3020       nil                               ; This article shouldn't be gathered
3021     subject))
3022
3023 (defun gnus-summary-simplify-subject-query ()
3024   "Query where the respool algorithm would put this article."
3025   (interactive)
3026   (gnus-summary-select-article)
3027   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3028
3029 (defun gnus-gather-threads-by-subject (threads)
3030   "Gather threads by looking at Subject headers."
3031   (if (not gnus-summary-make-false-root)
3032       threads
3033     (let ((hashtb (gnus-make-hashtable 1024))
3034           (prev threads)
3035           (result threads)
3036           subject hthread whole-subject)
3037       (while threads
3038         (setq subject (gnus-general-simplify-subject
3039                        (setq whole-subject (mail-header-subject
3040                                             (caar threads)))))
3041         (when subject
3042           (if (setq hthread (gnus-gethash subject hashtb))
3043               (progn
3044                 ;; We enter a dummy root into the thread, if we
3045                 ;; haven't done that already.
3046                 (unless (stringp (caar hthread))
3047                   (setcar hthread (list whole-subject (car hthread))))
3048                 ;; We add this new gathered thread to this gathered
3049                 ;; thread.
3050                 (setcdr (car hthread)
3051                         (nconc (cdar hthread) (list (car threads))))
3052                 ;; Remove it from the list of threads.
3053                 (setcdr prev (cdr threads))
3054                 (setq threads prev))
3055             ;; Enter this thread into the hash table.
3056             (gnus-sethash subject threads hashtb)))
3057         (setq prev threads)
3058         (setq threads (cdr threads)))
3059       result)))
3060
3061 (defun gnus-gather-threads-by-references (threads)
3062   "Gather threads by looking at References headers."
3063   (let ((idhashtb (gnus-make-hashtable 1024))
3064         (thhashtb (gnus-make-hashtable 1024))
3065         (prev threads)
3066         (result threads)
3067         ids references id gthread gid entered ref)
3068     (while threads
3069       (when (setq references (mail-header-references (caar threads)))
3070         (setq id (mail-header-id (caar threads))
3071               ids (gnus-split-references references)
3072               entered nil)
3073         (while (setq ref (pop ids))
3074           (setq ids (delete ref ids))
3075           (if (not (setq gid (gnus-gethash ref idhashtb)))
3076               (progn
3077                 (gnus-sethash ref id idhashtb)
3078                 (gnus-sethash id threads thhashtb))
3079             (setq gthread (gnus-gethash gid thhashtb))
3080             (unless entered
3081               ;; We enter a dummy root into the thread, if we
3082               ;; haven't done that already.
3083               (unless (stringp (caar gthread))
3084                 (setcar gthread (list (mail-header-subject (caar gthread))
3085                                       (car gthread))))
3086               ;; We add this new gathered thread to this gathered
3087               ;; thread.
3088               (setcdr (car gthread)
3089                       (nconc (cdar gthread) (list (car threads)))))
3090             ;; Add it into the thread hash table.
3091             (gnus-sethash id gthread thhashtb)
3092             (setq entered t)
3093             ;; Remove it from the list of threads.
3094             (setcdr prev (cdr threads))
3095             (setq threads prev))))
3096       (setq prev threads)
3097       (setq threads (cdr threads)))
3098     result))
3099
3100 (defun gnus-sort-gathered-threads (threads)
3101   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3102   (let ((result threads))
3103     (while threads
3104       (when (stringp (caar threads))
3105         (setcdr (car threads)
3106                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3107       (setq threads (cdr threads)))
3108     result))
3109
3110 (defun gnus-thread-loop-p (root thread)
3111   "Say whether ROOT is in THREAD."
3112   (let ((stack (list thread))
3113         (infloop 0)
3114         th)
3115     (while (setq thread (pop stack))
3116       (setq th (cdr thread))
3117       (while (and th
3118                   (not (eq (caar th) root)))
3119         (pop th))
3120       (if th
3121           ;; We have found a loop.
3122           (let (ref-dep)
3123             (setcdr thread (delq (car th) (cdr thread)))
3124             (if (boundp (setq ref-dep (intern "none"
3125                                               gnus-newsgroup-dependencies)))
3126                 (setcdr (symbol-value ref-dep)
3127                         (nconc (cdr (symbol-value ref-dep))
3128                                (list (car th))))
3129               (set ref-dep (list nil (car th))))
3130             (setq infloop 1
3131                   stack nil))
3132         ;; Push all the subthreads onto the stack.
3133         (push (cdr thread) stack)))
3134     infloop))
3135
3136 (defun gnus-make-threads ()
3137   "Go through the dependency hashtb and find the roots.  Return all threads."
3138   (let (threads)
3139     (while (catch 'infloop
3140              (mapatoms
3141               (lambda (refs)
3142                 ;; Deal with self-referencing References loops.
3143                 (when (and (car (symbol-value refs))
3144                            (not (zerop
3145                                  (apply
3146                                   '+
3147                                   (mapcar
3148                                    (lambda (thread)
3149                                      (gnus-thread-loop-p
3150                                       (car (symbol-value refs)) thread))
3151                                    (cdr (symbol-value refs)))))))
3152                   (setq threads nil)
3153                   (throw 'infloop t))
3154                 (unless (car (symbol-value refs))
3155                   ;; These threads do not refer back to any other articles,
3156                   ;; so they're roots.
3157                   (setq threads (append (cdr (symbol-value refs)) threads))))
3158               gnus-newsgroup-dependencies)))
3159     threads))
3160
3161 ;; Build the thread tree.
3162 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3163   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3164
3165 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3166 if it was already present.
3167
3168 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3169 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3170 Message-IDs will be renamed be renamed to a unique Message-ID before
3171 being entered.
3172
3173 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3174   (let* ((id (mail-header-id header))
3175          (id-dep (and id (intern id dependencies)))
3176          ref ref-dep ref-header)
3177     ;; Enter this `header' in the `dependencies' table.
3178     (cond
3179      ((not id-dep)
3180       (setq header nil))
3181      ;; The first two cases do the normal part: enter a new `header'
3182      ;; in the `dependencies' table.
3183      ((not (boundp id-dep))
3184       (set id-dep (list header)))
3185      ((null (car (symbol-value id-dep)))
3186       (setcar (symbol-value id-dep) header))
3187
3188      ;; From here the `header' was already present in the
3189      ;; `dependencies' table.
3190      (force-new
3191       ;; Overrides an existing entry;
3192       ;; just set the header part of the entry.
3193       (setcar (symbol-value id-dep) header))
3194
3195      ;; Renames the existing `header' to a unique Message-ID.
3196      ((not gnus-summary-ignore-duplicates)
3197       ;; An article with this Message-ID has already been seen.
3198       ;; We rename the Message-ID.
3199       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3200            (list header))
3201       (mail-header-set-id header id))
3202
3203      ;; The last case ignores an existing entry, except it adds any
3204      ;; additional Xrefs (in case the two articles came from different
3205      ;; servers.
3206      ;; Also sets `header' to `nil' meaning that the `dependencies'
3207      ;; table was *not* modified.
3208      (t
3209       (mail-header-set-xref
3210        (car (symbol-value id-dep))
3211        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3212                    "")
3213                (or (mail-header-xref header) "")))
3214       (setq header nil)))
3215
3216     (when header
3217       ;; First check if that we are not creating a References loop.
3218       (setq ref (gnus-parent-id (mail-header-references header)))
3219       (while (and ref
3220                   (setq ref-dep (intern-soft ref dependencies))
3221                   (boundp ref-dep)
3222                   (setq ref-header (car (symbol-value ref-dep))))
3223         (if (string= id ref)
3224             ;; Yuk!  This is a reference loop.  Make the article be a
3225             ;; root article.
3226             (progn
3227               (mail-header-set-references (car (symbol-value id-dep)) "none")
3228               (setq ref nil))
3229           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3230       (setq ref (gnus-parent-id (mail-header-references header)))
3231       (setq ref-dep (intern (or ref "none") dependencies))
3232       (if (boundp ref-dep)
3233           (setcdr (symbol-value ref-dep)
3234                   (nconc (cdr (symbol-value ref-dep))
3235                          (list (symbol-value id-dep))))
3236         (set ref-dep (list nil (symbol-value id-dep)))))
3237     header))
3238
3239 (defun gnus-build-sparse-threads ()
3240   (let ((headers gnus-newsgroup-headers)
3241         (mail-parse-charset gnus-newsgroup-charset)
3242         (gnus-summary-ignore-duplicates t)
3243         header references generation relations
3244         subject child end new-child date)
3245     ;; First we create an alist of generations/relations, where
3246     ;; generations is how much we trust the relation, and the relation
3247     ;; is parent/child.
3248     (gnus-message 7 "Making sparse threads...")
3249     (save-excursion
3250       (nnheader-set-temp-buffer " *gnus sparse threads*")
3251       (while (setq header (pop headers))
3252         (when (and (setq references (mail-header-references header))
3253                    (not (string= references "")))
3254           (insert references)
3255           (setq child (mail-header-id header)
3256                 subject (mail-header-subject header)
3257                 date (mail-header-date header)
3258                 generation 0)
3259           (while (search-backward ">" nil t)
3260             (setq end (1+ (point)))
3261             (when (search-backward "<" nil t)
3262               (setq new-child (buffer-substring (point) end))
3263               (push (list (incf generation)
3264                           child (setq child new-child)
3265                           subject date)
3266                     relations)))
3267           (when child
3268             (push (list (1+ generation) child nil subject) relations))
3269           (erase-buffer)))
3270       (kill-buffer (current-buffer)))
3271     ;; Sort over trustworthiness.
3272     (mapcar
3273      (lambda (relation)
3274        (when (gnus-dependencies-add-header
3275               (make-full-mail-header
3276                gnus-reffed-article-number
3277                (nth 3 relation) "" (or (nth 4 relation) "")
3278                (nth 1 relation)
3279                (or (nth 2 relation) "") 0 0 "")
3280               gnus-newsgroup-dependencies nil)
3281          (push gnus-reffed-article-number gnus-newsgroup-limit)
3282          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3283          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3284                gnus-newsgroup-reads)
3285          (decf gnus-reffed-article-number)))
3286      (sort relations 'car-less-than-car))
3287     (gnus-message 7 "Making sparse threads...done")))
3288
3289 (defun gnus-build-old-threads ()
3290   ;; Look at all the articles that refer back to old articles, and
3291   ;; fetch the headers for the articles that aren't there.  This will
3292   ;; build complete threads - if the roots haven't been expired by the
3293   ;; server, that is.
3294   (let ((mail-parse-charset gnus-newsgroup-charset)
3295         id heads)
3296     (mapatoms
3297      (lambda (refs)
3298        (when (not (car (symbol-value refs)))
3299          (setq heads (cdr (symbol-value refs)))
3300          (while heads
3301            (if (memq (mail-header-number (caar heads))
3302                      gnus-newsgroup-dormant)
3303                (setq heads (cdr heads))
3304              (setq id (symbol-name refs))
3305              (while (and (setq id (gnus-build-get-header id))
3306                          (not (car (gnus-id-to-thread id)))))
3307              (setq heads nil)))))
3308      gnus-newsgroup-dependencies)))
3309
3310 ;; This function has to be called with point after the article number
3311 ;; on the beginning of the line.
3312 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3313   (let ((eol (gnus-point-at-eol))
3314         (buffer (current-buffer))
3315         header)
3316
3317     ;; overview: [num subject from date id refs chars lines misc]
3318     (unwind-protect
3319         (progn
3320           (narrow-to-region (point) eol)
3321           (unless (eobp)
3322             (forward-char))
3323
3324           (setq header
3325                 (make-full-mail-header
3326                  number                 ; number
3327                  (funcall gnus-decode-encoded-word-function
3328                           (nnheader-nov-field)) ; subject
3329                  (funcall gnus-decode-encoded-word-function
3330                           (nnheader-nov-field)) ; from
3331                  (nnheader-nov-field)   ; date
3332                  (nnheader-nov-read-message-id) ; id
3333                  (nnheader-nov-field)   ; refs
3334                  (nnheader-nov-read-integer) ; chars
3335                  (nnheader-nov-read-integer) ; lines
3336                  (unless (eobp)
3337                    (if (looking-at "Xref: ")
3338                        (goto-char (match-end 0)))
3339                    (nnheader-nov-field)) ; Xref
3340                  (nnheader-nov-parse-extra)))) ; extra
3341
3342       (widen))
3343
3344     (when gnus-alter-header-function
3345       (funcall gnus-alter-header-function header))
3346     (gnus-dependencies-add-header header dependencies force-new)))
3347
3348 (defun gnus-build-get-header (id)
3349   "Look through the buffer of NOV lines and find the header to ID.
3350 Enter this line into the dependencies hash table, and return
3351 the id of the parent article (if any)."
3352   (let ((deps gnus-newsgroup-dependencies)
3353         found header)
3354     (prog1
3355         (save-excursion
3356           (set-buffer nntp-server-buffer)
3357           (let ((case-fold-search nil))
3358             (goto-char (point-min))
3359             (while (and (not found)
3360                         (search-forward id nil t))
3361               (beginning-of-line)
3362               (setq found (looking-at
3363                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3364                                    (regexp-quote id))))
3365               (or found (beginning-of-line 2)))
3366             (when found
3367               (beginning-of-line)
3368               (and
3369                (setq header (gnus-nov-parse-line
3370                              (read (current-buffer)) deps))
3371                (gnus-parent-id (mail-header-references header))))))
3372       (when header
3373         (let ((number (mail-header-number header)))
3374           (push number gnus-newsgroup-limit)
3375           (push header gnus-newsgroup-headers)
3376           (if (memq number gnus-newsgroup-unselected)
3377               (progn
3378                 (push number gnus-newsgroup-unreads)
3379                 (setq gnus-newsgroup-unselected
3380                       (delq number gnus-newsgroup-unselected)))
3381             (push number gnus-newsgroup-ancient)))))))
3382
3383 (defun gnus-build-all-threads ()
3384   "Read all the headers."
3385   (let ((gnus-summary-ignore-duplicates t)
3386         (mail-parse-charset gnus-newsgroup-charset)
3387         (dependencies gnus-newsgroup-dependencies)
3388         header article)
3389     (save-excursion
3390       (set-buffer nntp-server-buffer)
3391       (let ((case-fold-search nil))
3392         (goto-char (point-min))
3393         (while (not (eobp))
3394           (ignore-errors
3395             (setq article (read (current-buffer))
3396                   header (gnus-nov-parse-line article dependencies)))
3397           (when header
3398             (save-excursion
3399               (set-buffer gnus-summary-buffer)
3400               (push header gnus-newsgroup-headers)
3401               (if (memq (setq article (mail-header-number header))
3402                         gnus-newsgroup-unselected)
3403                   (progn
3404                     (push article gnus-newsgroup-unreads)
3405                     (setq gnus-newsgroup-unselected
3406                           (delq article gnus-newsgroup-unselected)))
3407                 (push article gnus-newsgroup-ancient)))
3408             (forward-line 1)))))))
3409
3410 (defun gnus-summary-update-article-line (article header)
3411   "Update the line for ARTICLE using HEADERS."
3412   (let* ((id (mail-header-id header))
3413          (thread (gnus-id-to-thread id)))
3414     (unless thread
3415       (error "Article in no thread"))
3416     ;; Update the thread.
3417     (setcar thread header)
3418     (gnus-summary-goto-subject article)
3419     (let* ((datal (gnus-data-find-list article))
3420            (data (car datal))
3421            (length (when (cdr datal)
3422                      (- (gnus-data-pos data)
3423                         (gnus-data-pos (cadr datal)))))
3424            (buffer-read-only nil)
3425            (level (gnus-summary-thread-level)))
3426       (gnus-delete-line)
3427       (gnus-summary-insert-line
3428        header level nil (gnus-article-mark article)
3429        (memq article gnus-newsgroup-replied)
3430        (memq article gnus-newsgroup-expirable)
3431        ;; Only insert the Subject string when it's different
3432        ;; from the previous Subject string.
3433        (if (and
3434             gnus-show-threads
3435             (gnus-subject-equal
3436              (condition-case ()
3437                  (mail-header-subject
3438                   (gnus-data-header
3439                    (cadr
3440                     (gnus-data-find-list
3441                      article
3442                      (gnus-data-list t)))))
3443                ;; Error on the side of excessive subjects.
3444                (error ""))
3445              (mail-header-subject header)))
3446            ""
3447          (mail-header-subject header))
3448        nil (cdr (assq article gnus-newsgroup-scored))
3449        (memq article gnus-newsgroup-processable))
3450       (when length
3451         (gnus-data-update-list
3452          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3453
3454 (defun gnus-summary-update-article (article &optional iheader)
3455   "Update ARTICLE in the summary buffer."
3456   (set-buffer gnus-summary-buffer)
3457   (let* ((header (gnus-summary-article-header article))
3458          (id (mail-header-id header))
3459          (data (gnus-data-find article))
3460          (thread (gnus-id-to-thread id))
3461          (references (mail-header-references header))
3462          (parent
3463           (gnus-id-to-thread
3464            (or (gnus-parent-id
3465                 (when (and references
3466                            (not (equal "" references)))
3467                   references))
3468                "none")))
3469          (buffer-read-only nil)
3470          (old (car thread)))
3471     (when thread
3472       (unless iheader
3473         (setcar thread nil)
3474         (when parent
3475           (delq thread parent)))
3476       (if (gnus-summary-insert-subject id header)
3477           ;; Set the (possibly) new article number in the data structure.
3478           (gnus-data-set-number data (gnus-id-to-article id))
3479         (setcar thread old)
3480         nil))))
3481
3482 (defun gnus-rebuild-thread (id &optional line)
3483   "Rebuild the thread containing ID.
3484 If LINE, insert the rebuilt thread starting on line LINE."
3485   (let ((buffer-read-only nil)
3486         old-pos current thread data)
3487     (if (not gnus-show-threads)
3488         (setq thread (list (car (gnus-id-to-thread id))))
3489       ;; Get the thread this article is part of.
3490       (setq thread (gnus-remove-thread id)))
3491     (setq old-pos (gnus-point-at-bol))
3492     (setq current (save-excursion
3493                     (and (zerop (forward-line -1))
3494                          (gnus-summary-article-number))))
3495     ;; If this is a gathered thread, we have to go some re-gathering.
3496     (when (stringp (car thread))
3497       (let ((subject (car thread))
3498             roots thr)
3499         (setq thread (cdr thread))
3500         (while thread
3501           (unless (memq (setq thr (gnus-id-to-thread
3502                                    (gnus-root-id
3503                                     (mail-header-id (caar thread)))))
3504                         roots)
3505             (push thr roots))
3506           (setq thread (cdr thread)))
3507         ;; We now have all (unique) roots.
3508         (if (= (length roots) 1)
3509             ;; All the loose roots are now one solid root.
3510             (setq thread (car roots))
3511           (setq thread (cons subject (gnus-sort-threads roots))))))
3512     (let (threads)
3513       ;; We then insert this thread into the summary buffer.
3514       (when line
3515         (goto-char (point-min))
3516         (forward-line (1- line)))
3517       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3518         (if gnus-show-threads
3519             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3520           (gnus-summary-prepare-unthreaded thread))
3521         (setq data (nreverse gnus-newsgroup-data))
3522         (setq threads gnus-newsgroup-threads))
3523       ;; We splice the new data into the data structure.
3524       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3525       ;;!!! then we want to insert at the beginning of the buffer.
3526       ;;!!! That happens to be true with Gnus now, but that may
3527       ;;!!! change in the future.  Perhaps.
3528       (gnus-data-enter-list
3529        (if line nil current) data (- (point) old-pos))
3530       (setq gnus-newsgroup-threads
3531             (nconc threads gnus-newsgroup-threads))
3532       (gnus-data-compute-positions))))
3533
3534 (defun gnus-number-to-header (number)
3535   "Return the header for article NUMBER."
3536   (let ((headers gnus-newsgroup-headers))
3537     (while (and headers
3538                 (not (= number (mail-header-number (car headers)))))
3539       (pop headers))
3540     (when headers
3541       (car headers))))
3542
3543 (defun gnus-parent-headers (in-headers &optional generation)
3544   "Return the headers of the GENERATIONeth parent of HEADERS."
3545   (unless generation
3546     (setq generation 1))
3547   (let ((parent t)
3548         (headers in-headers)
3549         references)
3550     (while (and parent
3551                 (not (zerop generation))
3552                 (setq references (mail-header-references headers)))
3553       (setq headers (if (and references
3554                              (setq parent (gnus-parent-id references)))
3555                         (car (gnus-id-to-thread parent))
3556                       nil))
3557       (decf generation))
3558     (and (not (eq headers in-headers))
3559          headers)))
3560
3561 (defun gnus-id-to-thread (id)
3562   "Return the (sub-)thread where ID appears."
3563   (gnus-gethash id gnus-newsgroup-dependencies))
3564
3565 (defun gnus-id-to-article (id)
3566   "Return the article number of ID."
3567   (let ((thread (gnus-id-to-thread id)))
3568     (when (and thread
3569                (car thread))
3570       (mail-header-number (car thread)))))
3571
3572 (defun gnus-id-to-header (id)
3573   "Return the article headers of ID."
3574   (car (gnus-id-to-thread id)))
3575
3576 (defun gnus-article-displayed-root-p (article)
3577   "Say whether ARTICLE is a root(ish) article."
3578   (let ((level (gnus-summary-thread-level article))
3579         (refs (mail-header-references  (gnus-summary-article-header article)))
3580         particle)
3581     (cond
3582      ((null level) nil)
3583      ((zerop level) t)
3584      ((null refs) t)
3585      ((null (gnus-parent-id refs)) t)
3586      ((and (= 1 level)
3587            (null (setq particle (gnus-id-to-article
3588                                  (gnus-parent-id refs))))
3589            (null (gnus-summary-thread-level particle)))))))
3590
3591 (defun gnus-root-id (id)
3592   "Return the id of the root of the thread where ID appears."
3593   (let (last-id prev)
3594     (while (and id (setq prev (car (gnus-id-to-thread id))))
3595       (setq last-id id
3596             id (gnus-parent-id (mail-header-references prev))))
3597     last-id))
3598
3599 (defun gnus-articles-in-thread (thread)
3600   "Return the list of articles in THREAD."
3601   (cons (mail-header-number (car thread))
3602         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3603
3604 (defun gnus-remove-thread (id &optional dont-remove)
3605   "Remove the thread that has ID in it."
3606   (let (headers thread last-id)
3607     ;; First go up in this thread until we find the root.
3608     (setq last-id (gnus-root-id id)
3609           headers (message-flatten-list (gnus-id-to-thread last-id)))
3610     ;; We have now found the real root of this thread.  It might have
3611     ;; been gathered into some loose thread, so we have to search
3612     ;; through the threads to find the thread we wanted.
3613     (let ((threads gnus-newsgroup-threads)
3614           sub)
3615       (while threads
3616         (setq sub (car threads))
3617         (if (stringp (car sub))
3618             ;; This is a gathered thread, so we look at the roots
3619             ;; below it to find whether this article is in this
3620             ;; gathered root.
3621             (progn
3622               (setq sub (cdr sub))
3623               (while sub
3624                 (when (member (caar sub) headers)
3625                   (setq thread (car threads)
3626                         threads nil
3627                         sub nil))
3628                 (setq sub (cdr sub))))
3629           ;; It's an ordinary thread, so we check it.
3630           (when (eq (car sub) (car headers))
3631             (setq thread sub
3632                   threads nil)))
3633         (setq threads (cdr threads)))
3634       ;; If this article is in no thread, then it's a root.
3635       (if thread
3636           (unless dont-remove
3637             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3638         (setq thread (gnus-id-to-thread last-id)))
3639       (when thread
3640         (prog1
3641             thread                      ; We return this thread.
3642           (unless dont-remove
3643             (if (stringp (car thread))
3644                 (progn
3645                   ;; If we use dummy roots, then we have to remove the
3646                   ;; dummy root as well.
3647                   (when (eq gnus-summary-make-false-root 'dummy)
3648                     ;; We go to the dummy root by going to
3649                     ;; the first sub-"thread", and then one line up.
3650                     (gnus-summary-goto-article
3651                      (mail-header-number (caadr thread)))
3652                     (forward-line -1)
3653                     (gnus-delete-line)
3654                     (gnus-data-compute-positions))
3655                   (setq thread (cdr thread))
3656                   (while thread
3657                     (gnus-remove-thread-1 (car thread))
3658                     (setq thread (cdr thread))))
3659               (gnus-remove-thread-1 thread))))))))
3660
3661 (defun gnus-remove-thread-1 (thread)
3662   "Remove the thread THREAD recursively."
3663   (let ((number (mail-header-number (pop thread)))
3664         d)
3665     (setq thread (reverse thread))
3666     (while thread
3667       (gnus-remove-thread-1 (pop thread)))
3668     (when (setq d (gnus-data-find number))
3669       (goto-char (gnus-data-pos d))
3670       (gnus-summary-show-thread)
3671       (gnus-data-remove
3672        number
3673        (- (gnus-point-at-bol)
3674           (prog1
3675               (1+ (gnus-point-at-eol))
3676             (gnus-delete-line)))))))
3677
3678 (defun gnus-sort-threads-1 (threads func)
3679   (sort (mapcar (lambda (thread)
3680                   (cons (car thread)
3681                         (and (cdr thread)
3682                              (gnus-sort-threads-1 (cdr thread) func))))
3683                 threads) func))
3684
3685 (defun gnus-sort-threads (threads)
3686   "Sort THREADS."
3687   (if (not gnus-thread-sort-functions)
3688       threads
3689     (gnus-message 8 "Sorting threads...")
3690     (prog1
3691         (gnus-sort-threads-1 
3692          threads 
3693          (gnus-make-sort-function gnus-thread-sort-functions))
3694       (gnus-message 8 "Sorting threads...done"))))
3695
3696 (defun gnus-sort-articles (articles)
3697   "Sort ARTICLES."
3698   (when gnus-article-sort-functions
3699     (gnus-message 7 "Sorting articles...")
3700     (prog1
3701         (setq gnus-newsgroup-headers
3702               (sort articles (gnus-make-sort-function
3703                               gnus-article-sort-functions)))
3704       (gnus-message 7 "Sorting articles...done"))))
3705
3706 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3707 (defmacro gnus-thread-header (thread)
3708   "Return header of first article in THREAD.
3709 Note that THREAD must never, ever be anything else than a variable -
3710 using some other form will lead to serious barfage."
3711   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3712   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3713   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3714         (vector thread) 2))
3715
3716 (defsubst gnus-article-sort-by-number (h1 h2)
3717   "Sort articles by article number."
3718   (< (mail-header-number h1)
3719      (mail-header-number h2)))
3720
3721 (defun gnus-thread-sort-by-number (h1 h2)
3722   "Sort threads by root article number."
3723   (gnus-article-sort-by-number
3724    (gnus-thread-header h1) (gnus-thread-header h2)))
3725
3726 (defsubst gnus-article-sort-by-lines (h1 h2)
3727   "Sort articles by article Lines header."
3728   (< (mail-header-lines h1)
3729      (mail-header-lines h2)))
3730
3731 (defun gnus-thread-sort-by-lines (h1 h2)
3732   "Sort threads by root article Lines header."
3733   (gnus-article-sort-by-lines
3734    (gnus-thread-header h1) (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-chars (h1 h2)
3737   "Sort articles by octet length."
3738   (< (mail-header-chars h1)
3739      (mail-header-chars h2)))
3740
3741 (defun gnus-thread-sort-by-chars (h1 h2)
3742   "Sort threads by root article octet length."
3743   (gnus-article-sort-by-chars
3744    (gnus-thread-header h1) (gnus-thread-header h2)))
3745
3746 (defsubst gnus-article-sort-by-author (h1 h2)
3747   "Sort articles by root author."
3748   (string-lessp
3749    (let ((extract (funcall
3750                    gnus-extract-address-components
3751                    (mail-header-from h1))))
3752      (or (car extract) (cadr extract) ""))
3753    (let ((extract (funcall
3754                    gnus-extract-address-components
3755                    (mail-header-from h2))))
3756      (or (car extract) (cadr extract) ""))))
3757
3758 (defun gnus-thread-sort-by-author (h1 h2)
3759   "Sort threads by root author."
3760   (gnus-article-sort-by-author
3761    (gnus-thread-header h1)  (gnus-thread-header h2)))
3762
3763 (defsubst gnus-article-sort-by-subject (h1 h2)
3764   "Sort articles by root subject."
3765   (string-lessp
3766    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3767    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3768
3769 (defun gnus-thread-sort-by-subject (h1 h2)
3770   "Sort threads by root subject."
3771   (gnus-article-sort-by-subject
3772    (gnus-thread-header h1) (gnus-thread-header h2)))
3773
3774 (defsubst gnus-article-sort-by-date (h1 h2)
3775   "Sort articles by root article date."
3776   (time-less-p
3777    (gnus-date-get-time (mail-header-date h1))
3778    (gnus-date-get-time (mail-header-date h2))))
3779
3780 (defun gnus-thread-sort-by-date (h1 h2)
3781   "Sort threads by root article date."
3782   (gnus-article-sort-by-date
3783    (gnus-thread-header h1) (gnus-thread-header h2)))
3784
3785 (defsubst gnus-article-sort-by-score (h1 h2)
3786   "Sort articles by root article score.
3787 Unscored articles will be counted as having a score of zero."
3788   (> (or (cdr (assq (mail-header-number h1)
3789                     gnus-newsgroup-scored))
3790          gnus-summary-default-score 0)
3791      (or (cdr (assq (mail-header-number h2)
3792                     gnus-newsgroup-scored))
3793          gnus-summary-default-score 0)))
3794
3795 (defun gnus-thread-sort-by-score (h1 h2)
3796   "Sort threads by root article score."
3797   (gnus-article-sort-by-score
3798    (gnus-thread-header h1) (gnus-thread-header h2)))
3799
3800 (defun gnus-thread-sort-by-total-score (h1 h2)
3801   "Sort threads by the sum of all scores in the thread.
3802 Unscored articles will be counted as having a score of zero."
3803   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3804
3805 (defun gnus-thread-total-score (thread)
3806   ;; This function find the total score of THREAD.
3807   (cond ((null thread)
3808          0)
3809         ((consp thread)
3810          (if (stringp (car thread))
3811              (apply gnus-thread-score-function 0
3812                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3813            (gnus-thread-total-score-1 thread)))
3814         (t
3815          (gnus-thread-total-score-1 (list thread)))))
3816
3817 (defun gnus-thread-total-score-1 (root)
3818   ;; This function find the total score of the thread below ROOT.
3819   (setq root (car root))
3820   (apply gnus-thread-score-function
3821          (or (append
3822               (mapcar 'gnus-thread-total-score
3823                       (cdr (gnus-id-to-thread (mail-header-id root))))
3824               (when (> (mail-header-number root) 0)
3825                 (list (or (cdr (assq (mail-header-number root)
3826                                      gnus-newsgroup-scored))
3827                           gnus-summary-default-score 0))))
3828              (list gnus-summary-default-score)
3829              '(0))))
3830
3831 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3832 (defvar gnus-tmp-prev-subject nil)
3833 (defvar gnus-tmp-false-parent nil)
3834 (defvar gnus-tmp-root-expunged nil)
3835 (defvar gnus-tmp-dummy-line nil)
3836
3837 (defvar gnus-tmp-header)
3838 (defun gnus-extra-header (type &optional header)
3839   "Return the extra header of TYPE."
3840   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3841       ""))
3842
3843 (defun gnus-summary-prepare-threads (threads)
3844   "Prepare summary buffer from THREADS and indentation LEVEL.
3845 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3846 or a straight list of headers."
3847   (gnus-message 7 "Generating summary...")
3848
3849   (setq gnus-newsgroup-threads threads)
3850   (beginning-of-line)
3851
3852   (let ((gnus-tmp-level 0)
3853         (default-score (or gnus-summary-default-score 0))
3854         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3855         thread number subject stack state gnus-tmp-gathered beg-match
3856         new-roots gnus-tmp-new-adopts thread-end
3857         gnus-tmp-header gnus-tmp-unread
3858         gnus-tmp-replied gnus-tmp-subject-or-nil
3859         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3860         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3861         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3862
3863     (setq gnus-tmp-prev-subject nil)
3864
3865     (if (vectorp (car threads))
3866         ;; If this is a straight (sic) list of headers, then a
3867         ;; threaded summary display isn't required, so we just create
3868         ;; an unthreaded one.
3869         (gnus-summary-prepare-unthreaded threads)
3870
3871       ;; Do the threaded display.
3872
3873       (while (or threads stack gnus-tmp-new-adopts new-roots)
3874
3875         (if (and (= gnus-tmp-level 0)
3876                  (or (not stack)
3877                      (= (caar stack) 0))
3878                  (not gnus-tmp-false-parent)
3879                  (or gnus-tmp-new-adopts new-roots))
3880             (if gnus-tmp-new-adopts
3881                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3882                       thread (list (car gnus-tmp-new-adopts))
3883                       gnus-tmp-header (caar thread)
3884                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3885               (when new-roots
3886                 (setq thread (list (car new-roots))
3887                       gnus-tmp-header (caar thread)
3888                       new-roots (cdr new-roots))))
3889
3890           (if threads
3891               ;; If there are some threads, we do them before the
3892               ;; threads on the stack.
3893               (setq thread threads
3894                     gnus-tmp-header (caar thread))
3895             ;; There were no current threads, so we pop something off
3896             ;; the stack.
3897             (setq state (car stack)
3898                   gnus-tmp-level (car state)
3899                   thread (cdr state)
3900                   stack (cdr stack)
3901                   gnus-tmp-header (caar thread))))
3902
3903         (setq gnus-tmp-false-parent nil)
3904         (setq gnus-tmp-root-expunged nil)
3905         (setq thread-end nil)
3906
3907         (if (stringp gnus-tmp-header)
3908             ;; The header is a dummy root.
3909             (cond
3910              ((eq gnus-summary-make-false-root 'adopt)
3911               ;; We let the first article adopt the rest.
3912               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3913                                                (cddar thread)))
3914               (setq gnus-tmp-gathered
3915                     (nconc (mapcar
3916                             (lambda (h) (mail-header-number (car h)))
3917                             (cddar thread))
3918                            gnus-tmp-gathered))
3919               (setq thread (cons (list (caar thread)
3920                                        (cadar thread))
3921                                  (cdr thread)))
3922               (setq gnus-tmp-level -1
3923                     gnus-tmp-false-parent t))
3924              ((eq gnus-summary-make-false-root 'empty)
3925               ;; We print adopted articles with empty subject fields.
3926               (setq gnus-tmp-gathered
3927                     (nconc (mapcar
3928                             (lambda (h) (mail-header-number (car h)))
3929                             (cddar thread))
3930                            gnus-tmp-gathered))
3931               (setq gnus-tmp-level -1))
3932              ((eq gnus-summary-make-false-root 'dummy)
3933               ;; We remember that we probably want to output a dummy
3934               ;; root.
3935               (setq gnus-tmp-dummy-line gnus-tmp-header)
3936               (setq gnus-tmp-prev-subject gnus-tmp-header))
3937              (t
3938               ;; We do not make a root for the gathered
3939               ;; sub-threads at all.
3940               (setq gnus-tmp-level -1)))
3941
3942           (setq number (mail-header-number gnus-tmp-header)
3943                 subject (mail-header-subject gnus-tmp-header))
3944
3945           (cond
3946            ;; If the thread has changed subject, we might want to make
3947            ;; this subthread into a root.
3948            ((and (null gnus-thread-ignore-subject)
3949                  (not (zerop gnus-tmp-level))
3950                  gnus-tmp-prev-subject
3951                  (not (inline
3952                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3953             (setq new-roots (nconc new-roots (list (car thread)))
3954                   thread-end t
3955                   gnus-tmp-header nil))
3956            ;; If the article lies outside the current limit,
3957            ;; then we do not display it.
3958            ((not (memq number gnus-newsgroup-limit))
3959             (setq gnus-tmp-gathered
3960                   (nconc (mapcar
3961                           (lambda (h) (mail-header-number (car h)))
3962                           (cdar thread))
3963                          gnus-tmp-gathered))
3964             (setq gnus-tmp-new-adopts (if (cdar thread)
3965                                           (append gnus-tmp-new-adopts
3966                                                   (cdar thread))
3967                                         gnus-tmp-new-adopts)
3968                   thread-end t
3969                   gnus-tmp-header nil)
3970             (when (zerop gnus-tmp-level)
3971               (setq gnus-tmp-root-expunged t)))
3972            ;; Perhaps this article is to be marked as read?
3973            ((and gnus-summary-mark-below
3974                  (< (or (cdr (assq number gnus-newsgroup-scored))
3975                         default-score)
3976                     gnus-summary-mark-below)
3977                  ;; Don't touch sparse articles.
3978                  (not (gnus-summary-article-sparse-p number))
3979                  (not (gnus-summary-article-ancient-p number)))
3980             (setq gnus-newsgroup-unreads
3981                   (delq number gnus-newsgroup-unreads))
3982             (if gnus-newsgroup-auto-expire
3983                 (push number gnus-newsgroup-expirable)
3984               (push (cons number gnus-low-score-mark)
3985                     gnus-newsgroup-reads))))
3986
3987           (when gnus-tmp-header
3988             ;; We may have an old dummy line to output before this
3989             ;; article.
3990             (when (and gnus-tmp-dummy-line
3991                        (gnus-subject-equal
3992                         gnus-tmp-dummy-line
3993                         (mail-header-subject gnus-tmp-header)))
3994               (gnus-summary-insert-dummy-line
3995                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3996               (setq gnus-tmp-dummy-line nil))
3997
3998             ;; Compute the mark.
3999             (setq gnus-tmp-unread (gnus-article-mark number))
4000
4001             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4002                                   gnus-tmp-header gnus-tmp-level)
4003                   gnus-newsgroup-data)
4004
4005             ;; Actually insert the line.
4006             (setq
4007              gnus-tmp-subject-or-nil
4008              (cond
4009               ((and gnus-thread-ignore-subject
4010                     gnus-tmp-prev-subject
4011                     (not (inline (gnus-subject-equal
4012                                   gnus-tmp-prev-subject subject))))
4013                subject)
4014               ((zerop gnus-tmp-level)
4015                (if (and (eq gnus-summary-make-false-root 'empty)
4016                         (memq number gnus-tmp-gathered)
4017                         gnus-tmp-prev-subject
4018                         (inline (gnus-subject-equal
4019                                  gnus-tmp-prev-subject subject)))
4020                    gnus-summary-same-subject
4021                  subject))
4022               (t gnus-summary-same-subject)))
4023             (if (and (eq gnus-summary-make-false-root 'adopt)
4024                      (= gnus-tmp-level 1)
4025                      (memq number gnus-tmp-gathered))
4026                 (setq gnus-tmp-opening-bracket ?\<
4027                       gnus-tmp-closing-bracket ?\>)
4028               (setq gnus-tmp-opening-bracket ?\[
4029                     gnus-tmp-closing-bracket ?\]))
4030             (setq
4031              gnus-tmp-indentation
4032              (aref gnus-thread-indent-array gnus-tmp-level)
4033              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4034              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4035                                 gnus-summary-default-score 0)
4036              gnus-tmp-score-char
4037              (if (or (null gnus-summary-default-score)
4038                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4039                          gnus-summary-zcore-fuzz))
4040                  ?  ;Whitespace
4041                (if (< gnus-tmp-score gnus-summary-default-score)
4042                    gnus-score-below-mark gnus-score-over-mark))
4043              gnus-tmp-replied
4044              (cond ((memq number gnus-newsgroup-processable)
4045                     gnus-process-mark)
4046                    ((memq number gnus-newsgroup-cached)
4047                     gnus-cached-mark)
4048                    ((memq number gnus-newsgroup-replied)
4049                     gnus-replied-mark)
4050                    ((memq number gnus-newsgroup-saved)
4051                     gnus-saved-mark)
4052                    (t gnus-unread-mark))
4053              gnus-tmp-from (mail-header-from gnus-tmp-header)
4054              gnus-tmp-name
4055              (cond
4056               ((string-match "<[^>]+> *$" gnus-tmp-from)
4057                (setq beg-match (match-beginning 0))
4058                (or (and (string-match "^\".+\"" gnus-tmp-from)
4059                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4060                    (substring gnus-tmp-from 0 beg-match)))
4061               ((string-match "(.+)" gnus-tmp-from)
4062                (substring gnus-tmp-from
4063                           (1+ (match-beginning 0)) (1- (match-end 0))))
4064               (t gnus-tmp-from)))
4065             (when (string= gnus-tmp-name "")
4066               (setq gnus-tmp-name gnus-tmp-from))
4067             (unless (numberp gnus-tmp-lines)
4068               (setq gnus-tmp-lines 0))
4069             (gnus-put-text-property
4070              (point)
4071              (progn (eval gnus-summary-line-format-spec) (point))
4072              'gnus-number number)
4073             (when gnus-visual-p
4074               (forward-line -1)
4075               (gnus-run-hooks 'gnus-summary-update-hook)
4076               (forward-line 1))
4077
4078             (setq gnus-tmp-prev-subject subject)))
4079
4080         (when (nth 1 thread)
4081           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4082         (incf gnus-tmp-level)
4083         (setq threads (if thread-end nil (cdar thread)))
4084         (unless threads
4085           (setq gnus-tmp-level 0)))))
4086   (gnus-message 7 "Generating summary...done"))
4087
4088 (defun gnus-summary-prepare-unthreaded (headers)
4089   "Generate an unthreaded summary buffer based on HEADERS."
4090   (let (header number mark)
4091
4092     (beginning-of-line)
4093
4094     (while headers
4095       ;; We may have to root out some bad articles...
4096       (when (memq (setq number (mail-header-number
4097                                 (setq header (pop headers))))
4098                   gnus-newsgroup-limit)
4099         ;; Mark article as read when it has a low score.
4100         (when (and gnus-summary-mark-below
4101                    (< (or (cdr (assq number gnus-newsgroup-scored))
4102                           gnus-summary-default-score 0)
4103                       gnus-summary-mark-below)
4104                    (not (gnus-summary-article-ancient-p number)))
4105           (setq gnus-newsgroup-unreads
4106                 (delq number gnus-newsgroup-unreads))
4107           (if gnus-newsgroup-auto-expire
4108               (push number gnus-newsgroup-expirable)
4109             (push (cons number gnus-low-score-mark)
4110                   gnus-newsgroup-reads)))
4111
4112         (setq mark (gnus-article-mark number))
4113         (push (gnus-data-make number mark (1+ (point)) header 0)
4114               gnus-newsgroup-data)
4115         (gnus-summary-insert-line
4116          header 0 number
4117          mark (memq number gnus-newsgroup-replied)
4118          (memq number gnus-newsgroup-expirable)
4119          (mail-header-subject header) nil
4120          (cdr (assq number gnus-newsgroup-scored))
4121          (memq number gnus-newsgroup-processable))))))
4122
4123 (defun gnus-summary-remove-list-identifiers ()
4124   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4125   (let ((regexp (if (stringp gnus-list-identifiers)
4126                     gnus-list-identifiers
4127                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4128     (dolist (header gnus-newsgroup-headers)
4129       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4130                                   " *\\)\\)+\\(Re: +\\)?\\)")
4131                           (mail-header-subject header))
4132         (mail-header-set-subject
4133          header (concat (substring (mail-header-subject header)
4134                                    0 (match-beginning 1))
4135                         (or
4136                          (match-string 3 (mail-header-subject header))
4137                          (match-string 5 (mail-header-subject header)))
4138                         (substring (mail-header-subject header)
4139                                    (match-end 1))))))))
4140
4141 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4142   "Select newsgroup GROUP.
4143 If READ-ALL is non-nil, all articles in the group are selected.
4144 If SELECT-ARTICLES, only select those articles from GROUP."
4145   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4146          ;;!!! Dirty hack; should be removed.
4147          (gnus-summary-ignore-duplicates
4148           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4149               t
4150             gnus-summary-ignore-duplicates))
4151          (info (nth 2 entry))
4152          articles fetched-articles cached)
4153
4154     (unless (gnus-check-server
4155              (setq gnus-current-select-method
4156                    (gnus-find-method-for-group group)))
4157       (error "Couldn't open server"))
4158
4159     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4160         (gnus-activate-group group)     ; Or we can activate it...
4161         (progn                          ; Or we bug out.
4162           (when (equal major-mode 'gnus-summary-mode)
4163             (kill-buffer (current-buffer)))
4164           (error "Couldn't request group %s: %s"
4165                  group (gnus-status-message group))))
4166
4167     (unless (gnus-request-group group t)
4168       (when (equal major-mode 'gnus-summary-mode)
4169         (kill-buffer (current-buffer)))
4170       (error "Couldn't request group %s: %s"
4171              group (gnus-status-message group)))
4172
4173     (setq gnus-newsgroup-name group)
4174     (setq gnus-newsgroup-unselected nil)
4175     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4176     (gnus-summary-setup-default-charset)
4177
4178     ;; Adjust and set lists of article marks.
4179     (when info
4180       (gnus-adjust-marked-articles info))
4181
4182     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4183     (when (gnus-virtual-group-p group)
4184       (setq cached gnus-newsgroup-cached))
4185
4186     (setq gnus-newsgroup-unreads
4187           (gnus-set-difference
4188            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4189            gnus-newsgroup-dormant))
4190
4191     (setq gnus-newsgroup-processable nil)
4192
4193     (gnus-update-read-articles group gnus-newsgroup-unreads)
4194
4195     (if (setq articles select-articles)
4196         (setq gnus-newsgroup-unselected
4197               (gnus-sorted-intersection
4198                gnus-newsgroup-unreads
4199                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4200       (setq articles (gnus-articles-to-read group read-all)))
4201
4202     (cond
4203      ((null articles)
4204       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4205       'quit)
4206      ((eq articles 0) nil)
4207      (t
4208       ;; Init the dependencies hash table.
4209       (setq gnus-newsgroup-dependencies
4210             (gnus-make-hashtable (length articles)))
4211       (gnus-set-global-variables)
4212       ;; Retrieve the headers and read them in.
4213       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4214       (setq gnus-newsgroup-headers
4215             (if (eq 'nov
4216                     (setq gnus-headers-retrieved-by
4217                           (gnus-retrieve-headers
4218                            articles gnus-newsgroup-name
4219                            ;; We might want to fetch old headers, but
4220                            ;; not if there is only 1 article.
4221                            (and (or (and
4222                                      (not (eq gnus-fetch-old-headers 'some))
4223                                      (not (numberp gnus-fetch-old-headers)))
4224                                     (> (length articles) 1))
4225                                 gnus-fetch-old-headers))))
4226                 (gnus-get-newsgroup-headers-xover
4227                  articles nil nil gnus-newsgroup-name t)
4228               (gnus-get-newsgroup-headers)))
4229       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4230
4231       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4232       (when cached
4233         (setq gnus-newsgroup-cached cached))
4234
4235       ;; Suppress duplicates?
4236       (when gnus-suppress-duplicates
4237         (gnus-dup-suppress-articles))
4238
4239       ;; Set the initial limit.
4240       (setq gnus-newsgroup-limit (copy-sequence articles))
4241       ;; Remove canceled articles from the list of unread articles.
4242       (setq gnus-newsgroup-unreads
4243             (gnus-set-sorted-intersection
4244              gnus-newsgroup-unreads
4245              (setq fetched-articles
4246                    (mapcar (lambda (headers) (mail-header-number headers))
4247                            gnus-newsgroup-headers))))
4248       ;; Removed marked articles that do not exist.
4249       (gnus-update-missing-marks
4250        (gnus-sorted-complement fetched-articles articles))
4251       ;; We might want to build some more threads first.
4252       (when (and gnus-fetch-old-headers
4253                  (eq gnus-headers-retrieved-by 'nov))
4254         (if (eq gnus-fetch-old-headers 'invisible)
4255             (gnus-build-all-threads)
4256           (gnus-build-old-threads)))
4257       ;; Let the Gnus agent mark articles as read.
4258       (when gnus-agent
4259         (gnus-agent-get-undownloaded-list))
4260       ;; Remove list identifiers from subject
4261       (when gnus-list-identifiers
4262         (gnus-summary-remove-list-identifiers))
4263       ;; Check whether auto-expire is to be done in this group.
4264       (setq gnus-newsgroup-auto-expire
4265             (gnus-group-auto-expirable-p group))
4266       ;; Set up the article buffer now, if necessary.
4267       (unless gnus-single-article-buffer
4268         (gnus-article-setup-buffer))
4269       ;; First and last article in this newsgroup.
4270       (when gnus-newsgroup-headers
4271         (setq gnus-newsgroup-begin
4272               (mail-header-number (car gnus-newsgroup-headers))
4273               gnus-newsgroup-end
4274               (mail-header-number
4275                (gnus-last-element gnus-newsgroup-headers))))
4276       ;; GROUP is successfully selected.
4277       (or gnus-newsgroup-headers t)))))
4278
4279 (defun gnus-articles-to-read (group &optional read-all)
4280   "Find out what articles the user wants to read."
4281   (let* ((articles
4282           ;; Select all articles if `read-all' is non-nil, or if there
4283           ;; are no unread articles.
4284           (if (or read-all
4285                   (and (zerop (length gnus-newsgroup-marked))
4286                        (zerop (length gnus-newsgroup-unreads)))
4287                   (eq (gnus-group-find-parameter group 'display)
4288                       'all))
4289               (or
4290                (gnus-uncompress-range (gnus-active group))
4291                (gnus-cache-articles-in-group group))
4292             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4293                           (copy-sequence gnus-newsgroup-unreads))
4294                   '<)))
4295          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4296          (scored (length scored-list))
4297          (number (length articles))
4298          (marked (+ (length gnus-newsgroup-marked)
4299                     (length gnus-newsgroup-dormant)))
4300          (select
4301           (cond
4302            ((numberp read-all)
4303             read-all)
4304            (t
4305             (condition-case ()
4306                 (cond
4307                  ((and (or (<= scored marked) (= scored number))
4308                        (numberp gnus-large-newsgroup)
4309                        (> number gnus-large-newsgroup))
4310                   (let ((input
4311                          (read-string
4312                           (format
4313                            "How many articles from %s (default %d): "
4314                            (gnus-limit-string gnus-newsgroup-name 35)
4315                            number))))
4316                     (if (string-match "^[ \t]*$" input) number input)))
4317                  ((and (> scored marked) (< scored number)
4318                        (> (- scored number) 20))
4319                   (let ((input
4320                          (read-string
4321                           (format "%s %s (%d scored, %d total): "
4322                                   "How many articles from"
4323                                   group scored number))))
4324                     (if (string-match "^[ \t]*$" input)
4325                         number input)))
4326                  (t number))
4327               (quit
4328                (message "Quit getting the articles to read")
4329                nil))))))
4330     (setq select (if (stringp select) (string-to-number select) select))
4331     (if (or (null select) (zerop select))
4332         select
4333       (if (and (not (zerop scored)) (<= (abs select) scored))
4334           (progn
4335             (setq articles (sort scored-list '<))
4336             (setq number (length articles)))
4337         (setq articles (copy-sequence articles)))
4338
4339       (when (< (abs select) number)
4340         (if (< select 0)
4341             ;; Select the N oldest articles.
4342             (setcdr (nthcdr (1- (abs select)) articles) nil)
4343           ;; Select the N most recent articles.
4344           (setq articles (nthcdr (- number select) articles))))
4345       (setq gnus-newsgroup-unselected
4346             (gnus-sorted-intersection
4347              gnus-newsgroup-unreads
4348              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4349       (when gnus-alter-articles-to-read-function
4350         (setq gnus-newsgroup-unreads
4351               (sort 
4352                (funcall gnus-alter-articles-to-read-function
4353                         gnus-newsgroup-name gnus-newsgroup-unreads)
4354                '<)))
4355       articles)))
4356
4357 (defun gnus-killed-articles (killed articles)
4358   (let (out)
4359     (while articles
4360       (when (inline (gnus-member-of-range (car articles) killed))
4361         (push (car articles) out))
4362       (setq articles (cdr articles)))
4363     out))
4364
4365 (defun gnus-uncompress-marks (marks)
4366   "Uncompress the mark ranges in MARKS."
4367   (let ((uncompressed '(score bookmark))
4368         out)
4369     (while marks
4370       (if (memq (caar marks) uncompressed)
4371           (push (car marks) out)
4372         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4373       (setq marks (cdr marks)))
4374     out))
4375
4376 (defun gnus-adjust-marked-articles (info)
4377   "Set all article lists and remove all marks that are no longer valid."
4378   (let* ((marked-lists (gnus-info-marks info))
4379          (active (gnus-active (gnus-info-group info)))
4380          (min (car active))
4381          (max (cdr active))
4382          (types gnus-article-mark-lists)
4383          (uncompressed '(score bookmark killed))
4384          marks var articles article mark)
4385
4386     (while marked-lists
4387       (setq marks (pop marked-lists))
4388       (set (setq var (intern (format "gnus-newsgroup-%s"
4389                                      (car (rassq (setq mark (car marks))
4390                                                  types)))))
4391            (if (memq (car marks) uncompressed) (cdr marks)
4392              (gnus-uncompress-range (cdr marks))))
4393
4394       (setq articles (symbol-value var))
4395
4396       ;; All articles have to be subsets of the active articles.
4397       (cond
4398        ;; Adjust "simple" lists.
4399        ((memq mark '(tick dormant expire reply save))
4400         (while articles
4401           (when (or (< (setq article (pop articles)) min) (> article max))
4402             (set var (delq article (symbol-value var))))))
4403        ;; Adjust assocs.
4404        ((memq mark uncompressed)
4405         (when (not (listp (cdr (symbol-value var))))
4406           (set var (list (symbol-value var))))
4407         (when (not (listp (cdr articles)))
4408           (setq articles (list articles)))
4409         (while articles
4410           (when (or (not (consp (setq article (pop articles))))
4411                     (< (car article) min)
4412                     (> (car article) max))
4413             (set var (delq article (symbol-value var))))))))))
4414
4415 (defun gnus-update-missing-marks (missing)
4416   "Go through the list of MISSING articles and remove them from the mark lists."
4417   (when missing
4418     (let ((types gnus-article-mark-lists)
4419           var m)
4420       ;; Go through all types.
4421       (while types
4422         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4423         (when (symbol-value var)
4424           ;; This list has articles.  So we delete all missing articles
4425           ;; from it.
4426           (setq m missing)
4427           (while m
4428             (set var (delq (pop m) (symbol-value var)))))))))
4429
4430 (defun gnus-update-marks ()
4431   "Enter the various lists of marked articles into the newsgroup info list."
4432   (let ((types gnus-article-mark-lists)
4433         (info (gnus-get-info gnus-newsgroup-name))
4434         (uncompressed '(score bookmark killed))
4435         type list newmarked symbol delta-marks)
4436     (when info
4437       ;; Add all marks lists to the list of marks lists.
4438       (while (setq type (pop types))
4439         (setq list (symbol-value
4440                     (setq symbol
4441                           (intern (format "gnus-newsgroup-%s"
4442                                           (car type))))))
4443
4444         (when list
4445           ;; Get rid of the entries of the articles that have the
4446           ;; default score.
4447           (when (and (eq (cdr type) 'score)
4448                      gnus-save-score
4449                      list)
4450             (let* ((arts list)
4451                    (prev (cons nil list))
4452                    (all prev))
4453               (while arts
4454                 (if (or (not (consp (car arts)))
4455                         (= (cdar arts) gnus-summary-default-score))
4456                     (setcdr prev (cdr arts))
4457                   (setq prev arts))
4458                 (setq arts (cdr arts)))
4459               (setq list (cdr all)))))
4460
4461         (unless (memq (cdr type) uncompressed)
4462           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4463        
4464         (when (gnus-check-backend-function
4465                'request-set-mark gnus-newsgroup-name)
4466           ;; propagate flags to server, with the following exceptions:
4467           ;; uncompressed:s are not proper flags (they are cons cells)
4468           ;; cache is a internal gnus flag
4469           ;; download are local to one gnus installation (well)
4470           ;; unsend are for nndraft groups only
4471           ;; xxx: generality of this?  this suits nnimap anyway
4472           (unless (memq (cdr type) (append '(cache download unsend)
4473                                            uncompressed))
4474             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4475                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4476                    (add (gnus-remove-from-range
4477                          (gnus-copy-sequence list) old)))
4478               (when add
4479                 (push (list add 'add (list (cdr type))) delta-marks))
4480               (when del
4481                 (push (list del 'del (list (cdr type))) delta-marks)))))
4482           
4483         (when list
4484           (push (cons (cdr type) list) newmarked)))
4485
4486       (when delta-marks
4487         (unless (gnus-check-group gnus-newsgroup-name)
4488           (error "Can't open server for %s" gnus-newsgroup-name))
4489         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4490           
4491       ;; Enter these new marks into the info of the group.
4492       (if (nthcdr 3 info)
4493           (setcar (nthcdr 3 info) newmarked)
4494         ;; Add the marks lists to the end of the info.
4495         (when newmarked
4496           (setcdr (nthcdr 2 info) (list newmarked))))
4497
4498       ;; Cut off the end of the info if there's nothing else there.
4499       (let ((i 5))
4500         (while (and (> i 2)
4501                     (not (nth i info)))
4502           (when (nthcdr (decf i) info)
4503             (setcdr (nthcdr i info) nil)))))))
4504
4505 (defun gnus-set-mode-line (where)
4506   "Set the mode line of the article or summary buffers.
4507 If WHERE is `summary', the summary mode line format will be used."
4508   ;; Is this mode line one we keep updated?
4509   (when (and (memq where gnus-updated-mode-lines)
4510              (symbol-value
4511               (intern (format "gnus-%s-mode-line-format-spec" where))))
4512     (let (mode-string)
4513       (save-excursion
4514         ;; We evaluate this in the summary buffer since these
4515         ;; variables are buffer-local to that buffer.
4516         (set-buffer gnus-summary-buffer)
4517         ;; We bind all these variables that are used in the `eval' form
4518         ;; below.
4519         (let* ((mformat (symbol-value
4520                          (intern
4521                           (format "gnus-%s-mode-line-format-spec" where))))
4522                (gnus-tmp-group-name (gnus-group-name-decode 
4523                                      gnus-newsgroup-name
4524                                      (gnus-group-name-charset 
4525                                       nil
4526                                       gnus-newsgroup-name)))
4527                (gnus-tmp-article-number (or gnus-current-article 0))
4528                (gnus-tmp-unread gnus-newsgroup-unreads)
4529                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4530                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4531                (gnus-tmp-unread-and-unselected
4532                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4533                             (zerop gnus-tmp-unselected))
4534                        "")
4535                       ((zerop gnus-tmp-unselected)
4536                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4537                       (t (format "{%d(+%d) more}"
4538                                  gnus-tmp-unread-and-unticked
4539                                  gnus-tmp-unselected))))
4540                (gnus-tmp-subject
4541                 (if (and gnus-current-headers
4542                          (vectorp gnus-current-headers))
4543                     (gnus-mode-string-quote
4544                      (mail-header-subject gnus-current-headers))
4545                   ""))
4546                bufname-length max-len
4547                gnus-tmp-header);; passed as argument to any user-format-funcs
4548           (setq mode-string (eval mformat))
4549           (setq bufname-length (if (string-match "%b" mode-string)
4550                                    (- (length
4551                                        (buffer-name
4552                                         (if (eq where 'summary)
4553                                             nil
4554                                           (get-buffer gnus-article-buffer))))
4555                                       2)
4556                                  0))
4557           (setq max-len (max 4 (if gnus-mode-non-string-length
4558                                    (- (window-width)
4559                                       gnus-mode-non-string-length
4560                                       bufname-length)
4561                                  (length mode-string))))
4562           ;; We might have to chop a bit of the string off...
4563           (when (> (length mode-string) max-len)
4564             (setq mode-string
4565                   (concat (truncate-string-to-width mode-string (- max-len 3))
4566                           "...")))
4567           ;; Pad the mode string a bit.
4568           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4569       ;; Update the mode line.
4570       (setq mode-line-buffer-identification
4571             (gnus-mode-line-buffer-identification (list mode-string)))
4572       (set-buffer-modified-p t))))
4573
4574 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4575   "Go through the HEADERS list and add all Xrefs to a hash table.
4576 The resulting hash table is returned, or nil if no Xrefs were found."
4577   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4578          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4579          (xref-hashtb (gnus-make-hashtable))
4580          start group entry number xrefs header)
4581     (while headers
4582       (setq header (pop headers))
4583       (when (and (setq xrefs (mail-header-xref header))
4584                  (not (memq (setq number (mail-header-number header))
4585                             unreads)))
4586         (setq start 0)
4587         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4588           (setq start (match-end 0))
4589           (setq group (if prefix
4590                           (concat prefix (substring xrefs (match-beginning 1)
4591                                                     (match-end 1)))
4592                         (substring xrefs (match-beginning 1) (match-end 1))))
4593           (setq number
4594                 (string-to-int (substring xrefs (match-beginning 2)
4595                                           (match-end 2))))
4596           (if (setq entry (gnus-gethash group xref-hashtb))
4597               (setcdr entry (cons number (cdr entry)))
4598             (gnus-sethash group (cons number nil) xref-hashtb)))))
4599     (and start xref-hashtb)))
4600
4601 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4602   "Look through all the headers and mark the Xrefs as read."
4603   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4604         name entry info xref-hashtb idlist method nth4)
4605     (save-excursion
4606       (set-buffer gnus-group-buffer)
4607       (when (setq xref-hashtb
4608                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4609         (mapatoms
4610          (lambda (group)
4611            (unless (string= from-newsgroup (setq name (symbol-name group)))
4612              (setq idlist (symbol-value group))
4613              ;; Dead groups are not updated.
4614              (and (prog1
4615                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4616                             info (nth 2 entry))
4617                     (when (stringp (setq nth4 (gnus-info-method info)))
4618                       (setq nth4 (gnus-server-to-method nth4))))
4619                   ;; Only do the xrefs if the group has the same
4620                   ;; select method as the group we have just read.
4621                   (or (gnus-methods-equal-p
4622                        nth4 (gnus-find-method-for-group from-newsgroup))
4623                       virtual
4624                       (equal nth4 (setq method (gnus-find-method-for-group
4625                                                 from-newsgroup)))
4626                       (and (equal (car nth4) (car method))
4627                            (equal (nth 1 nth4) (nth 1 method))))
4628                   gnus-use-cross-reference
4629                   (or (not (eq gnus-use-cross-reference t))
4630                       virtual
4631                       ;; Only do cross-references on subscribed
4632                       ;; groups, if that is what is wanted.
4633                       (<= (gnus-info-level info) gnus-level-subscribed))
4634                   (gnus-group-make-articles-read name idlist))))
4635          xref-hashtb)))))
4636
4637 (defun gnus-compute-read-articles (group articles)
4638   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4639          (info (nth 2 entry))
4640          (active (gnus-active group))
4641          ninfo)
4642     (when entry
4643       ;; First peel off all invalid article numbers.
4644       (when active
4645         (let ((ids articles)
4646               id first)
4647           (while (setq id (pop ids))
4648             (when (and first (> id (cdr active)))
4649               ;; We'll end up in this situation in one particular
4650               ;; obscure situation.  If you re-scan a group and get
4651               ;; a new article that is cross-posted to a different
4652               ;; group that has not been re-scanned, you might get
4653               ;; crossposted article that has a higher number than
4654               ;; Gnus believes possible.  So we re-activate this
4655               ;; group as well.  This might mean doing the
4656               ;; crossposting thingy will *increase* the number
4657               ;; of articles in some groups.  Tsk, tsk.
4658               (setq active (or (gnus-activate-group group) active)))
4659             (when (or (> id (cdr active))
4660                       (< id (car active)))
4661               (setq articles (delq id articles))))))
4662       ;; If the read list is nil, we init it.
4663       (if (and active
4664                (null (gnus-info-read info))
4665                (> (car active) 1))
4666           (setq ninfo (cons 1 (1- (car active))))
4667         (setq ninfo (gnus-info-read info)))
4668       ;; Then we add the read articles to the range.
4669       (gnus-add-to-range
4670        ninfo (setq articles (sort articles '<))))))
4671
4672 (defun gnus-group-make-articles-read (group articles)
4673   "Update the info of GROUP to say that ARTICLES are read."
4674   (let* ((num 0)
4675          (entry (gnus-gethash group gnus-newsrc-hashtb))
4676          (info (nth 2 entry))
4677          (active (gnus-active group))
4678          range)
4679     (when entry
4680       (setq range (gnus-compute-read-articles group articles))
4681       (save-excursion
4682         (set-buffer gnus-group-buffer)
4683         (gnus-undo-register
4684           `(progn
4685              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4686              (gnus-info-set-read ',info ',(gnus-info-read info))
4687              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4688              (gnus-group-update-group ,group t))))
4689       ;; Add the read articles to the range.
4690       (gnus-info-set-read info range)
4691       ;; Then we have to re-compute how many unread
4692       ;; articles there are in this group.
4693       (when active
4694         (cond
4695          ((not range)
4696           (setq num (- (1+ (cdr active)) (car active))))
4697          ((not (listp (cdr range)))
4698           (setq num (- (cdr active) (- (1+ (cdr range))
4699                                        (car range)))))
4700          (t
4701           (while range
4702             (if (numberp (car range))
4703                 (setq num (1+ num))
4704               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4705             (setq range (cdr range)))
4706           (setq num (- (cdr active) num))))
4707         ;; Update the number of unread articles.
4708         (setcar entry num)
4709         ;; Update the group buffer.
4710         (gnus-group-update-group group t)))))
4711
4712 (defvar gnus-newsgroup-none-id 0)
4713
4714 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4715   (let ((cur nntp-server-buffer)
4716         (dependencies
4717          (or dependencies
4718              (save-excursion (set-buffer gnus-summary-buffer)
4719                              gnus-newsgroup-dependencies)))
4720         headers id end ref
4721         (mail-parse-charset gnus-newsgroup-charset)
4722         (mail-parse-ignored-charsets
4723          (save-excursion (condition-case nil
4724                              (set-buffer gnus-summary-buffer)
4725                            (error))
4726                          gnus-newsgroup-ignored-charsets)))
4727     (save-excursion
4728       (set-buffer nntp-server-buffer)
4729       ;; Translate all TAB characters into SPACE characters.
4730       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4731       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4732       (gnus-run-hooks 'gnus-parse-headers-hook)
4733       (let ((case-fold-search t)
4734             in-reply-to header p lines chars)
4735         (goto-char (point-min))
4736         ;; Search to the beginning of the next header.  Error messages
4737         ;; do not begin with 2 or 3.
4738         (while (re-search-forward "^[23][0-9]+ " nil t)
4739           (setq id nil
4740                 ref nil)
4741           ;; This implementation of this function, with nine
4742           ;; search-forwards instead of the one re-search-forward and
4743           ;; a case (which basically was the old function) is actually
4744           ;; about twice as fast, even though it looks messier.  You
4745           ;; can't have everything, I guess.  Speed and elegance
4746           ;; doesn't always go hand in hand.
4747           (setq
4748            header
4749            (vector
4750             ;; Number.
4751             (prog1
4752                 (read cur)
4753               (end-of-line)
4754               (setq p (point))
4755               (narrow-to-region (point)
4756                                 (or (and (search-forward "\n.\n" nil t)
4757                                          (- (point) 2))
4758                                     (point))))
4759             ;; Subject.
4760             (progn
4761               (goto-char p)
4762               (if (search-forward "\nsubject: " nil t)
4763                   (funcall gnus-decode-encoded-word-function
4764                            (nnheader-header-value))
4765                 "(none)"))
4766             ;; From.
4767             (progn
4768               (goto-char p)
4769               (if (or (search-forward "\nfrom: " nil t)
4770                       (search-forward "\nfrom:" nil t))
4771                   (funcall gnus-decode-encoded-word-function
4772                            (nnheader-header-value))
4773                 "(nobody)"))
4774             ;; Date.
4775             (progn
4776               (goto-char p)
4777               (if (search-forward "\ndate: " nil t)
4778                   (nnheader-header-value) ""))
4779             ;; Message-ID.
4780             (progn
4781               (goto-char p)
4782               (setq id (if (re-search-forward
4783                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4784                            ;; We do it this way to make sure the Message-ID
4785                            ;; is (somewhat) syntactically valid.
4786                            (buffer-substring (match-beginning 1)
4787                                              (match-end 1))
4788                          ;; If there was no message-id, we just fake one
4789                          ;; to make subsequent routines simpler.
4790                          (nnheader-generate-fake-message-id))))
4791             ;; References.
4792             (progn
4793               (goto-char p)
4794               (if (search-forward "\nreferences: " nil t)
4795                   (progn
4796                     (setq end (point))
4797                     (prog1
4798                         (nnheader-header-value)
4799                       (setq ref
4800                             (buffer-substring
4801                              (progn
4802                                (end-of-line)
4803                                (search-backward ">" end t)
4804                                (1+ (point)))
4805                              (progn
4806                                (search-backward "<" end t)
4807                                (point))))))
4808                 ;; Get the references from the in-reply-to header if there
4809                 ;; were no references and the in-reply-to header looks
4810                 ;; promising.
4811                 (if (and (search-forward "\nin-reply-to: " nil t)
4812                          (setq in-reply-to (nnheader-header-value))
4813                          (string-match "<[^>]+>" in-reply-to))
4814                     (let (ref2)
4815                       (setq ref (substring in-reply-to (match-beginning 0)
4816                                            (match-end 0)))
4817                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4818                         (setq ref2 (substring in-reply-to (match-beginning 0)
4819                                               (match-end 0)))
4820                         (when (> (length ref2) (length ref))
4821                           (setq ref ref2)))
4822                       ref)
4823                   (setq ref nil))))
4824             ;; Chars.
4825             (progn
4826               (goto-char p)
4827               (if (search-forward "\nchars: " nil t)
4828                   (if (numberp (setq chars (ignore-errors (read cur))))
4829                       chars 0)
4830                 0))
4831             ;; Lines.
4832             (progn
4833               (goto-char p)
4834               (if (search-forward "\nlines: " nil t)
4835                   (if (numberp (setq lines (ignore-errors (read cur))))
4836                       lines 0)
4837                 0))
4838             ;; Xref.
4839             (progn
4840               (goto-char p)
4841               (and (search-forward "\nxref: " nil t)
4842                    (nnheader-header-value)))
4843             ;; Extra.
4844             (when gnus-extra-headers
4845               (let ((extra gnus-extra-headers)
4846                     out)
4847                 (while extra
4848                   (goto-char p)
4849                   (when (search-forward
4850                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4851                     (push (cons (car extra) (nnheader-header-value))
4852                           out))
4853                   (pop extra))
4854                 out))))
4855           (when (equal id ref)
4856             (setq ref nil))
4857
4858           (when gnus-alter-header-function
4859             (funcall gnus-alter-header-function header)
4860             (setq id (mail-header-id header)
4861                   ref (gnus-parent-id (mail-header-references header))))
4862
4863           (when (setq header
4864                       (gnus-dependencies-add-header
4865                        header dependencies force-new))
4866             (push header headers))
4867           (goto-char (point-max))
4868           (widen))
4869         (nreverse headers)))))
4870
4871 ;; Goes through the xover lines and returns a list of vectors
4872 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4873                                                   force-new dependencies
4874                                                   group also-fetch-heads)
4875   "Parse the news overview data in the server buffer.
4876 Return a list of headers that match SEQUENCE (see
4877 `nntp-retrieve-headers')."
4878   ;; Get the Xref when the users reads the articles since most/some
4879   ;; NNTP servers do not include Xrefs when using XOVER.
4880   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4881   (let ((mail-parse-charset gnus-newsgroup-charset)
4882         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4883         (cur nntp-server-buffer)
4884         (dependencies (or dependencies gnus-newsgroup-dependencies))
4885         number headers header)
4886     (save-excursion
4887       (set-buffer nntp-server-buffer)
4888       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4889       ;; Allow the user to mangle the headers before parsing them.
4890       (gnus-run-hooks 'gnus-parse-headers-hook)
4891       (goto-char (point-min))
4892       (while (not (eobp))
4893         (condition-case ()
4894             (while (and sequence (not (eobp)))
4895               (setq number (read cur))
4896               (while (and sequence
4897                           (< (car sequence) number))
4898                 (setq sequence (cdr sequence)))
4899               (and sequence
4900                    (eq number (car sequence))
4901                    (progn
4902                      (setq sequence (cdr sequence))
4903                      (setq header (inline
4904                                     (gnus-nov-parse-line
4905                                      number dependencies force-new))))
4906                    (push header headers))
4907               (forward-line 1))
4908           (error
4909            (gnus-error 4 "Strange nov line (%d)"
4910                        (count-lines (point-min) (point)))))
4911         (forward-line 1))
4912       ;; A common bug in inn is that if you have posted an article and
4913       ;; then retrieves the active file, it will answer correctly --
4914       ;; the new article is included.  However, a NOV entry for the
4915       ;; article may not have been generated yet, so this may fail.
4916       ;; We work around this problem by retrieving the last few
4917       ;; headers using HEAD.
4918       (if (or (not also-fetch-heads)
4919               (not sequence))
4920           ;; We (probably) got all the headers.
4921           (nreverse headers)
4922         (let ((gnus-nov-is-evil t))
4923           (nconc
4924            (nreverse headers)
4925            (when (gnus-retrieve-headers sequence group)
4926              (gnus-get-newsgroup-headers))))))))
4927
4928 (defun gnus-article-get-xrefs ()
4929   "Fill in the Xref value in `gnus-current-headers', if necessary.
4930 This is meant to be called in `gnus-article-internal-prepare-hook'."
4931   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4932                                  gnus-current-headers)))
4933     (or (not gnus-use-cross-reference)
4934         (not headers)
4935         (and (mail-header-xref headers)
4936              (not (string= (mail-header-xref headers) "")))
4937         (let ((case-fold-search t)
4938               xref)
4939           (save-restriction
4940             (nnheader-narrow-to-headers)
4941             (goto-char (point-min))
4942             (when (or (and (not (eobp))
4943                            (eq (downcase (char-after)) ?x)
4944                            (looking-at "Xref:"))
4945                       (search-forward "\nXref:" nil t))
4946               (goto-char (1+ (match-end 0)))
4947               (setq xref (buffer-substring (point)
4948                                            (progn (end-of-line) (point))))
4949               (mail-header-set-xref headers xref)))))))
4950
4951 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4952   "Find article ID and insert the summary line for that article.
4953 OLD-HEADER can either be a header or a line number to insert
4954 the subject line on."
4955   (let* ((line (and (numberp old-header) old-header))
4956          (old-header (and (vectorp old-header) old-header))
4957          (header (cond ((and old-header use-old-header)
4958                         old-header)
4959                        ((and (numberp id)
4960                              (gnus-number-to-header id))
4961                         (gnus-number-to-header id))
4962                        (t
4963                         (gnus-read-header id))))
4964          (number (and (numberp id) id))
4965          d)
4966     (when header
4967       ;; Rebuild the thread that this article is part of and go to the
4968       ;; article we have fetched.
4969       (when (and (not gnus-show-threads)
4970                  old-header)
4971         (when (and number
4972                    (setq d (gnus-data-find (mail-header-number old-header))))
4973           (goto-char (gnus-data-pos d))
4974           (gnus-data-remove
4975            number
4976            (- (gnus-point-at-bol)
4977               (prog1
4978                   (1+ (gnus-point-at-eol))
4979                 (gnus-delete-line))))))
4980       (when old-header
4981         (mail-header-set-number header (mail-header-number old-header)))
4982       (setq gnus-newsgroup-sparse
4983             (delq (setq number (mail-header-number header))
4984                   gnus-newsgroup-sparse))
4985       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4986       (push number gnus-newsgroup-limit)
4987       (gnus-rebuild-thread (mail-header-id header) line)
4988       (gnus-summary-goto-subject number nil t))
4989     (when (and (numberp number)
4990                (> number 0))
4991       ;; We have to update the boundaries even if we can't fetch the
4992       ;; article if ID is a number -- so that the next `P' or `N'
4993       ;; command will fetch the previous (or next) article even
4994       ;; if the one we tried to fetch this time has been canceled.
4995       (when (> number gnus-newsgroup-end)
4996         (setq gnus-newsgroup-end number))
4997       (when (< number gnus-newsgroup-begin)
4998         (setq gnus-newsgroup-begin number))
4999       (setq gnus-newsgroup-unselected
5000             (delq number gnus-newsgroup-unselected)))
5001     ;; Report back a success?
5002     (and header (mail-header-number header))))
5003
5004 ;;; Process/prefix in the summary buffer
5005
5006 (defun gnus-summary-work-articles (n)
5007   "Return a list of articles to be worked upon.
5008 The prefix argument, the list of process marked articles, and the
5009 current article will be taken into consideration."
5010   (save-excursion
5011     (set-buffer gnus-summary-buffer)
5012     (cond
5013      (n
5014       ;; A numerical prefix has been given.
5015       (setq n (prefix-numeric-value n))
5016       (let ((backward (< n 0))
5017             (n (abs (prefix-numeric-value n)))
5018             articles article)
5019         (save-excursion
5020           (while
5021               (and (> n 0)
5022                    (push (setq article (gnus-summary-article-number))
5023                          articles)
5024                    (if backward
5025                        (gnus-summary-find-prev nil article)
5026                      (gnus-summary-find-next nil article)))
5027             (decf n)))
5028         (nreverse articles)))
5029      ((and (gnus-region-active-p) (mark))
5030       (message "region active")
5031       ;; Work on the region between point and mark.
5032       (let ((max (max (point) (mark)))
5033             articles article)
5034         (save-excursion
5035           (goto-char (min (min (point) (mark))))
5036           (while
5037               (and
5038                (push (setq article (gnus-summary-article-number)) articles)
5039                (gnus-summary-find-next nil article)
5040                (< (point) max)))
5041           (nreverse articles))))
5042      (gnus-newsgroup-processable
5043       ;; There are process-marked articles present.
5044       ;; Save current state.
5045       (gnus-summary-save-process-mark)
5046       ;; Return the list.
5047       (reverse gnus-newsgroup-processable))
5048      (t
5049       ;; Just return the current article.
5050       (list (gnus-summary-article-number))))))
5051
5052 (defmacro gnus-summary-iterate (arg &rest forms)
5053   "Iterate over the process/prefixed articles and do FORMS.
5054 ARG is the interactive prefix given to the command.  FORMS will be
5055 executed with point over the summary line of the articles."
5056   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5057     `(let ((,articles (gnus-summary-work-articles ,arg)))
5058        (while ,articles
5059          (gnus-summary-goto-subject (car ,articles))
5060          ,@forms
5061          (pop ,articles)))))
5062
5063 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5064 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5065
5066 (defun gnus-summary-save-process-mark ()
5067   "Push the current set of process marked articles on the stack."
5068   (interactive)
5069   (push (copy-sequence gnus-newsgroup-processable)
5070         gnus-newsgroup-process-stack))
5071
5072 (defun gnus-summary-kill-process-mark ()
5073   "Push the current set of process marked articles on the stack and unmark."
5074   (interactive)
5075   (gnus-summary-save-process-mark)
5076   (gnus-summary-unmark-all-processable))
5077
5078 (defun gnus-summary-yank-process-mark ()
5079   "Pop the last process mark state off the stack and restore it."
5080   (interactive)
5081   (unless gnus-newsgroup-process-stack
5082     (error "Empty mark stack"))
5083   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5084
5085 (defun gnus-summary-process-mark-set (set)
5086   "Make SET into the current process marked articles."
5087   (gnus-summary-unmark-all-processable)
5088   (while set
5089     (gnus-summary-set-process-mark (pop set))))
5090
5091 ;;; Searching and stuff
5092
5093 (defun gnus-summary-search-group (&optional backward use-level)
5094   "Search for next unread newsgroup.
5095 If optional argument BACKWARD is non-nil, search backward instead."
5096   (save-excursion
5097     (set-buffer gnus-group-buffer)
5098     (when (gnus-group-search-forward
5099            backward nil (if use-level (gnus-group-group-level) nil))
5100       (gnus-group-group-name))))
5101
5102 (defun gnus-summary-best-group (&optional exclude-group)
5103   "Find the name of the best unread group.
5104 If EXCLUDE-GROUP, do not go to this group."
5105   (save-excursion
5106     (set-buffer gnus-group-buffer)
5107     (save-excursion
5108       (gnus-group-best-unread-group exclude-group))))
5109
5110 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5111   (if backward (gnus-summary-find-prev)
5112     (let* ((dummy (gnus-summary-article-intangible-p))
5113            (article (or article (gnus-summary-article-number)))
5114            (arts (gnus-data-find-list article))
5115            result)
5116       (when (and (not dummy)
5117                  (or (not gnus-summary-check-current)
5118                      (not unread)
5119                      (not (gnus-data-unread-p (car arts)))))
5120         (setq arts (cdr arts)))
5121       (when (setq result
5122                   (if unread
5123                       (progn
5124                         (while arts
5125                           (when (or (and undownloaded
5126                                          (eq gnus-undownloaded-mark
5127                                              (gnus-data-mark (car arts))))
5128                                     (gnus-data-unread-p (car arts)))
5129                             (setq result (car arts)
5130                                   arts nil))
5131                           (setq arts (cdr arts)))
5132                         result)
5133                     (car arts)))
5134         (goto-char (gnus-data-pos result))
5135         (gnus-data-number result)))))
5136
5137 (defun gnus-summary-find-prev (&optional unread article)
5138   (let* ((eobp (eobp))
5139          (article (or article (gnus-summary-article-number)))
5140          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5141          result)
5142     (when (and (not eobp)
5143                (or (not gnus-summary-check-current)
5144                    (not unread)
5145                    (not (gnus-data-unread-p (car arts)))))
5146       (setq arts (cdr arts)))
5147     (when (setq result
5148                 (if unread
5149                     (progn
5150                       (while arts
5151                         (when (gnus-data-unread-p (car arts))
5152                           (setq result (car arts)
5153                                 arts nil))
5154                         (setq arts (cdr arts)))
5155                       result)
5156                   (car arts)))
5157       (goto-char (gnus-data-pos result))
5158       (gnus-data-number result))))
5159
5160 (defun gnus-summary-find-subject (subject &optional unread backward article)
5161   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5162          (article (or article (gnus-summary-article-number)))
5163          (articles (gnus-data-list backward))
5164          (arts (gnus-data-find-list article articles))
5165          result)
5166     (when (or (not gnus-summary-check-current)
5167               (not unread)
5168               (not (gnus-data-unread-p (car arts))))
5169       (setq arts (cdr arts)))
5170     (while arts
5171       (and (or (not unread)
5172                (gnus-data-unread-p (car arts)))
5173            (vectorp (gnus-data-header (car arts)))
5174            (gnus-subject-equal
5175             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5176            (setq result (car arts)
5177                  arts nil))
5178       (setq arts (cdr arts)))
5179     (and result
5180          (goto-char (gnus-data-pos result))
5181          (gnus-data-number result))))
5182
5183 (defun gnus-summary-search-forward (&optional unread subject backward)
5184   "Search forward for an article.
5185 If UNREAD, look for unread articles.  If SUBJECT, look for
5186 articles with that subject.  If BACKWARD, search backward instead."
5187   (cond (subject (gnus-summary-find-subject subject unread backward))
5188         (backward (gnus-summary-find-prev unread))
5189         (t (gnus-summary-find-next unread))))
5190
5191 (defun gnus-recenter (&optional n)
5192   "Center point in window and redisplay frame.
5193 Also do horizontal recentering."
5194   (interactive "P")
5195   (when (and gnus-auto-center-summary
5196              (not (eq gnus-auto-center-summary 'vertical)))
5197     (gnus-horizontal-recenter))
5198   (recenter n))
5199
5200 (defun gnus-summary-recenter ()
5201   "Center point in the summary window.
5202 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5203 displayed, no centering will be performed."
5204   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5205   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5206   (interactive)
5207   (let* ((top (cond ((< (window-height) 4) 0)
5208                     ((< (window-height) 7) 1)
5209                     (t (if (numberp gnus-auto-center-summary)
5210                            gnus-auto-center-summary
5211                          2))))
5212          (height (1- (window-height)))
5213          (bottom (save-excursion (goto-char (point-max))
5214                                  (forward-line (- height))
5215                                  (point)))
5216          (window (get-buffer-window (current-buffer))))
5217     ;; The user has to want it.
5218     (when gnus-auto-center-summary
5219       (when (get-buffer-window gnus-article-buffer)
5220         ;; Only do recentering when the article buffer is displayed,
5221         ;; Set the window start to either `bottom', which is the biggest
5222         ;; possible valid number, or the second line from the top,
5223         ;; whichever is the least.
5224         (set-window-start
5225          window (min bottom (save-excursion
5226                               (forward-line (- top)) (point)))
5227          t))
5228       ;; Do horizontal recentering while we're at it.
5229       (when (and (get-buffer-window (current-buffer) t)
5230                  (not (eq gnus-auto-center-summary 'vertical)))
5231         (let ((selected (selected-window)))
5232           (select-window (get-buffer-window (current-buffer) t))
5233           (gnus-summary-position-point)
5234           (gnus-horizontal-recenter)
5235           (select-window selected))))))
5236
5237 (defun gnus-summary-jump-to-group (newsgroup)
5238   "Move point to NEWSGROUP in group mode buffer."
5239   ;; Keep update point of group mode buffer if visible.
5240   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5241       (save-window-excursion
5242         ;; Take care of tree window mode.
5243         (when (get-buffer-window gnus-group-buffer)
5244           (pop-to-buffer gnus-group-buffer))
5245         (gnus-group-jump-to-group newsgroup))
5246     (save-excursion
5247       ;; Take care of tree window mode.
5248       (if (get-buffer-window gnus-group-buffer)
5249           (pop-to-buffer gnus-group-buffer)
5250         (set-buffer gnus-group-buffer))
5251       (gnus-group-jump-to-group newsgroup))))
5252
5253 ;; This function returns a list of article numbers based on the
5254 ;; difference between the ranges of read articles in this group and
5255 ;; the range of active articles.
5256 (defun gnus-list-of-unread-articles (group)
5257   (let* ((read (gnus-info-read (gnus-get-info group)))
5258          (active (or (gnus-active group) (gnus-activate-group group)))
5259          (last (cdr active))
5260          first nlast unread)
5261     ;; If none are read, then all are unread.
5262     (if (not read)
5263         (setq first (car active))
5264       ;; If the range of read articles is a single range, then the
5265       ;; first unread article is the article after the last read
5266       ;; article.  Sounds logical, doesn't it?
5267       (if (and (not (listp (cdr read)))
5268                (or (< (car read) (car active))
5269                    (progn (setq read (list read))
5270                           nil)))
5271           (setq first (max (car active) (1+ (cdr read))))
5272         ;; `read' is a list of ranges.
5273         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5274                                   (caar read)))
5275                   1)
5276           (setq first (car active)))
5277         (while read
5278           (when first
5279             (while (< first nlast)
5280               (push first unread)
5281               (setq first (1+ first))))
5282           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5283           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5284           (setq read (cdr read)))))
5285     ;; And add the last unread articles.
5286     (while (<= first last)
5287       (push first unread)
5288       (setq first (1+ first)))
5289     ;; Return the list of unread articles.
5290     (delq 0 (nreverse unread))))
5291
5292 (defun gnus-list-of-read-articles (group)
5293   "Return a list of unread, unticked and non-dormant articles."
5294   (let* ((info (gnus-get-info group))
5295          (marked (gnus-info-marks info))
5296          (active (gnus-active group)))
5297     (and info active
5298          (gnus-set-difference
5299           (gnus-sorted-complement
5300            (gnus-uncompress-range active)
5301            (gnus-list-of-unread-articles group))
5302           (append
5303            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5304            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5305
5306 ;; Various summary commands
5307
5308 (defun gnus-summary-select-article-buffer ()
5309   "Reconfigure windows to show article buffer."
5310   (interactive)
5311   (if (not (gnus-buffer-live-p gnus-article-buffer))
5312       (error "There is no article buffer for this summary buffer")
5313     (gnus-configure-windows 'article)
5314     (select-window (get-buffer-window gnus-article-buffer))))
5315
5316 (defun gnus-summary-universal-argument (arg)
5317   "Perform any operation on all articles that are process/prefixed."
5318   (interactive "P")
5319   (let ((articles (gnus-summary-work-articles arg))
5320         func article)
5321     (if (eq
5322          (setq
5323           func
5324           (key-binding
5325            (read-key-sequence
5326             (substitute-command-keys
5327              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5328          'undefined)
5329         (gnus-error 1 "Undefined key")
5330       (save-excursion
5331         (while articles
5332           (gnus-summary-goto-subject (setq article (pop articles)))
5333           (let (gnus-newsgroup-processable)
5334             (command-execute func))
5335           (gnus-summary-remove-process-mark article)))))
5336   (gnus-summary-position-point))
5337
5338 (defun gnus-summary-toggle-truncation (&optional arg)
5339   "Toggle truncation of summary lines.
5340 With arg, turn line truncation on iff arg is positive."
5341   (interactive "P")
5342   (setq truncate-lines
5343         (if (null arg) (not truncate-lines)
5344           (> (prefix-numeric-value arg) 0)))
5345   (redraw-display))
5346
5347 (defun gnus-summary-reselect-current-group (&optional all rescan)
5348   "Exit and then reselect the current newsgroup.
5349 The prefix argument ALL means to select all articles."
5350   (interactive "P")
5351   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5352     (error "Ephemeral groups can't be reselected"))
5353   (let ((current-subject (gnus-summary-article-number))
5354         (group gnus-newsgroup-name))
5355     (setq gnus-newsgroup-begin nil)
5356     (gnus-summary-exit)
5357     ;; We have to adjust the point of group mode buffer because
5358     ;; point was moved to the next unread newsgroup by exiting.
5359     (gnus-summary-jump-to-group group)
5360     (when rescan
5361       (save-excursion
5362         (gnus-group-get-new-news-this-group 1)))
5363     (gnus-group-read-group all t)
5364     (gnus-summary-goto-subject current-subject nil t)))
5365
5366 (defun gnus-summary-rescan-group (&optional all)
5367   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5368   (interactive "P")
5369   (gnus-summary-reselect-current-group all t))
5370
5371 (defun gnus-summary-update-info (&optional non-destructive)
5372   (save-excursion
5373     (let ((group gnus-newsgroup-name))
5374       (when group
5375         (when gnus-newsgroup-kill-headers
5376           (setq gnus-newsgroup-killed
5377                 (gnus-compress-sequence
5378                  (nconc
5379                   (gnus-set-sorted-intersection
5380                    (gnus-uncompress-range gnus-newsgroup-killed)
5381                    (setq gnus-newsgroup-unselected
5382                          (sort gnus-newsgroup-unselected '<)))
5383                   (setq gnus-newsgroup-unreads
5384                         (sort gnus-newsgroup-unreads '<)))
5385                  t)))
5386         (unless (listp (cdr gnus-newsgroup-killed))
5387           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5388         (let ((headers gnus-newsgroup-headers))
5389           ;; Set the new ranges of read articles.
5390           (save-excursion
5391             (set-buffer gnus-group-buffer)
5392             (gnus-undo-force-boundary))
5393           (gnus-update-read-articles
5394            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5395           ;; Set the current article marks.
5396           (let ((gnus-newsgroup-scored
5397                  (if (and (not gnus-save-score)
5398                           (not non-destructive))
5399                      nil
5400                    gnus-newsgroup-scored)))
5401             (save-excursion
5402               (gnus-update-marks)))
5403           ;; Do the cross-ref thing.
5404           (when gnus-use-cross-reference
5405             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5406           ;; Do not switch windows but change the buffer to work.
5407           (set-buffer gnus-group-buffer)
5408           (unless (gnus-ephemeral-group-p group)
5409             (gnus-group-update-group group)))))))
5410
5411 (defun gnus-summary-save-newsrc (&optional force)
5412   "Save the current number of read/marked articles in the dribble buffer.
5413 The dribble buffer will then be saved.
5414 If FORCE (the prefix), also save the .newsrc file(s)."
5415   (interactive "P")
5416   (gnus-summary-update-info t)
5417   (if force
5418       (gnus-save-newsrc-file)
5419     (gnus-dribble-save)))
5420
5421 (defun gnus-summary-exit (&optional temporary)
5422   "Exit reading current newsgroup, and then return to group selection mode.
5423 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5424   (interactive)
5425   (gnus-set-global-variables)
5426   (when (gnus-buffer-live-p gnus-article-buffer)
5427     (save-excursion
5428       (set-buffer gnus-article-buffer)
5429       (mm-destroy-parts gnus-article-mime-handles)
5430       ;; Set it to nil for safety reason.
5431       (setq gnus-article-mime-handle-alist nil)
5432       (setq gnus-article-mime-handles nil)))
5433   (gnus-kill-save-kill-buffer)
5434   (gnus-async-halt-prefetch)
5435   (let* ((group gnus-newsgroup-name)
5436          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5437          (mode major-mode)
5438          (group-point nil)
5439          (buf (current-buffer)))
5440     (unless quit-config
5441       ;; Do adaptive scoring, and possibly save score files.
5442       (when gnus-newsgroup-adaptive
5443         (gnus-score-adaptive))
5444       (when gnus-use-scoring
5445         (gnus-score-save)))
5446     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5447     ;; If we have several article buffers, we kill them at exit.
5448     (unless gnus-single-article-buffer
5449       (gnus-kill-buffer gnus-original-article-buffer)
5450       (setq gnus-article-current nil))
5451     (when gnus-use-cache
5452       (gnus-cache-possibly-remove-articles)
5453       (gnus-cache-save-buffers))
5454     (gnus-async-prefetch-remove-group group)
5455     (when gnus-suppress-duplicates
5456       (gnus-dup-enter-articles))
5457     (when gnus-use-trees
5458       (gnus-tree-close group))
5459     (when gnus-use-cache
5460       (gnus-cache-write-active))
5461     ;; Remove entries for this group.
5462     (nnmail-purge-split-history (gnus-group-real-name group))
5463     ;; Make all changes in this group permanent.
5464     (unless quit-config
5465       (gnus-run-hooks 'gnus-exit-group-hook)
5466       (gnus-summary-update-info))
5467     (gnus-close-group group)
5468     ;; Make sure where we were, and go to next newsgroup.
5469     (set-buffer gnus-group-buffer)
5470     (unless quit-config
5471       (gnus-group-jump-to-group group))
5472     (gnus-run-hooks 'gnus-summary-exit-hook)
5473     (unless (or quit-config
5474                 ;; If this group has disappeared from the summary
5475                 ;; buffer, don't skip forwards.
5476                 (not (string= group (gnus-group-group-name))))
5477       (gnus-group-next-unread-group 1))
5478     (setq group-point (point))
5479     (if temporary
5480         nil                             ;Nothing to do.
5481       ;; If we have several article buffers, we kill them at exit.
5482       (unless gnus-single-article-buffer
5483         (gnus-kill-buffer gnus-article-buffer)
5484         (gnus-kill-buffer gnus-original-article-buffer)
5485         (setq gnus-article-current nil))
5486       (set-buffer buf)
5487       (if (not gnus-kill-summary-on-exit)
5488           (gnus-deaden-summary)
5489         ;; We set all buffer-local variables to nil.  It is unclear why
5490         ;; this is needed, but if we don't, buffer-local variables are
5491         ;; not garbage-collected, it seems.  This would the lead to en
5492         ;; ever-growing Emacs.
5493         (gnus-summary-clear-local-variables)
5494         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5495           (gnus-summary-clear-local-variables))
5496         (when (get-buffer gnus-article-buffer)
5497           (bury-buffer gnus-article-buffer))
5498         ;; We clear the global counterparts of the buffer-local
5499         ;; variables as well, just to be on the safe side.
5500         (set-buffer gnus-group-buffer)
5501         (gnus-summary-clear-local-variables)
5502         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5503           (gnus-summary-clear-local-variables))
5504         ;; Return to group mode buffer.
5505         (when (eq mode 'gnus-summary-mode)
5506           (gnus-kill-buffer buf)))
5507       (setq gnus-current-select-method gnus-select-method)
5508       (pop-to-buffer gnus-group-buffer)
5509       (if (not quit-config)
5510           (progn
5511             (goto-char group-point)
5512             (gnus-configure-windows 'group 'force))
5513         (gnus-handle-ephemeral-exit quit-config))
5514       ;; Clear the current group name.
5515       (unless quit-config
5516         (setq gnus-newsgroup-name nil)))))
5517
5518 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5519 (defun gnus-summary-exit-no-update (&optional no-questions)
5520   "Quit reading current newsgroup without updating read article info."
5521   (interactive)
5522   (let* ((group gnus-newsgroup-name)
5523          (quit-config (gnus-group-quit-config group)))
5524     (when (or no-questions
5525               gnus-expert-user
5526               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5527       (gnus-async-halt-prefetch)
5528       (mapcar 'funcall
5529               (delq 'gnus-summary-expire-articles
5530                     (copy-sequence gnus-summary-prepare-exit-hook)))
5531       (when (gnus-buffer-live-p gnus-article-buffer)
5532         (save-excursion
5533           (set-buffer gnus-article-buffer)
5534           (mm-destroy-parts gnus-article-mime-handles)
5535           ;; Set it to nil for safety reason.
5536           (setq gnus-article-mime-handle-alist nil)
5537           (setq gnus-article-mime-handles nil)))
5538       ;; If we have several article buffers, we kill them at exit.
5539       (unless gnus-single-article-buffer
5540         (gnus-kill-buffer gnus-article-buffer)
5541         (gnus-kill-buffer gnus-original-article-buffer)
5542         (setq gnus-article-current nil))
5543       (if (not gnus-kill-summary-on-exit)
5544           (gnus-deaden-summary)
5545         (gnus-close-group group)
5546         (gnus-summary-clear-local-variables)
5547         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5548           (gnus-summary-clear-local-variables))
5549         (set-buffer gnus-group-buffer)
5550         (gnus-summary-clear-local-variables)
5551         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5552           (gnus-summary-clear-local-variables))
5553         (when (get-buffer gnus-summary-buffer)
5554           (kill-buffer gnus-summary-buffer)))
5555       (unless gnus-single-article-buffer
5556         (setq gnus-article-current nil))
5557       (when gnus-use-trees
5558         (gnus-tree-close group))
5559       (gnus-async-prefetch-remove-group group)
5560       (when (get-buffer gnus-article-buffer)
5561         (bury-buffer gnus-article-buffer))
5562       ;; Return to the group buffer.
5563       (gnus-configure-windows 'group 'force)
5564       ;; Clear the current group name.
5565       (setq gnus-newsgroup-name nil)
5566       (when (equal (gnus-group-group-name) group)
5567         (gnus-group-next-unread-group 1))
5568       (when quit-config
5569         (gnus-handle-ephemeral-exit quit-config)))))
5570
5571 (defun gnus-handle-ephemeral-exit (quit-config)
5572   "Handle movement when leaving an ephemeral group.
5573 The state which existed when entering the ephemeral is reset."
5574   (if (not (buffer-name (car quit-config)))
5575       (gnus-configure-windows 'group 'force)
5576     (set-buffer (car quit-config))
5577     (cond ((eq major-mode 'gnus-summary-mode)
5578            (gnus-set-global-variables))
5579           ((eq major-mode 'gnus-article-mode)
5580            (save-excursion
5581              ;; The `gnus-summary-buffer' variable may point
5582              ;; to the old summary buffer when using a single
5583              ;; article buffer.
5584              (unless (gnus-buffer-live-p gnus-summary-buffer)
5585                (set-buffer gnus-group-buffer))
5586              (set-buffer gnus-summary-buffer)
5587              (gnus-set-global-variables))))
5588     (if (or (eq (cdr quit-config) 'article)
5589             (eq (cdr quit-config) 'pick))
5590         (progn
5591           ;; The current article may be from the ephemeral group
5592           ;; thus it is best that we reload this article
5593           (gnus-summary-show-article)
5594           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5595               (gnus-configure-windows 'pick 'force)
5596             (gnus-configure-windows (cdr quit-config) 'force)))
5597       (gnus-configure-windows (cdr quit-config) 'force))
5598     (when (eq major-mode 'gnus-summary-mode)
5599       (gnus-summary-next-subject 1 nil t)
5600       (gnus-summary-recenter)
5601       (gnus-summary-position-point))))
5602
5603 ;;; Dead summaries.
5604
5605 (defvar gnus-dead-summary-mode-map nil)
5606
5607 (unless gnus-dead-summary-mode-map
5608   (setq gnus-dead-summary-mode-map (make-keymap))
5609   (suppress-keymap gnus-dead-summary-mode-map)
5610   (substitute-key-definition
5611    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5612   (let ((keys '("\C-d" "\r" "\177" [delete])))
5613     (while keys
5614       (define-key gnus-dead-summary-mode-map
5615         (pop keys) 'gnus-summary-wake-up-the-dead))))
5616
5617 (defvar gnus-dead-summary-mode nil
5618   "Minor mode for Gnus summary buffers.")
5619
5620 (defun gnus-dead-summary-mode (&optional arg)
5621   "Minor mode for Gnus summary buffers."
5622   (interactive "P")
5623   (when (eq major-mode 'gnus-summary-mode)
5624     (make-local-variable 'gnus-dead-summary-mode)
5625     (setq gnus-dead-summary-mode
5626           (if (null arg) (not gnus-dead-summary-mode)
5627             (> (prefix-numeric-value arg) 0)))
5628     (when gnus-dead-summary-mode
5629       (gnus-add-minor-mode
5630        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5631
5632 (defun gnus-deaden-summary ()
5633   "Make the current summary buffer into a dead summary buffer."
5634   ;; Kill any previous dead summary buffer.
5635   (when (and gnus-dead-summary
5636              (buffer-name gnus-dead-summary))
5637     (save-excursion
5638       (set-buffer gnus-dead-summary)
5639       (when gnus-dead-summary-mode
5640         (kill-buffer (current-buffer)))))
5641   ;; Make this the current dead summary.
5642   (setq gnus-dead-summary (current-buffer))
5643   (gnus-dead-summary-mode 1)
5644   (let ((name (buffer-name)))
5645     (when (string-match "Summary" name)
5646       (rename-buffer
5647        (concat (substring name 0 (match-beginning 0)) "Dead "
5648                (substring name (match-beginning 0)))
5649        t)
5650       (bury-buffer))))
5651
5652 (defun gnus-kill-or-deaden-summary (buffer)
5653   "Kill or deaden the summary BUFFER."
5654   (save-excursion
5655     (when (and (buffer-name buffer)
5656                (not gnus-single-article-buffer))
5657       (save-excursion
5658         (set-buffer buffer)
5659         (gnus-kill-buffer gnus-article-buffer)
5660         (gnus-kill-buffer gnus-original-article-buffer)))
5661     (cond (gnus-kill-summary-on-exit
5662            (when (and gnus-use-trees
5663                       (gnus-buffer-exists-p buffer))
5664              (save-excursion
5665                (set-buffer buffer)
5666                (gnus-tree-close gnus-newsgroup-name)))
5667            (gnus-kill-buffer buffer))
5668           ((gnus-buffer-exists-p buffer)
5669            (save-excursion
5670              (set-buffer buffer)
5671              (gnus-deaden-summary))))))
5672
5673 (defun gnus-summary-wake-up-the-dead (&rest args)
5674   "Wake up the dead summary buffer."
5675   (interactive)
5676   (gnus-dead-summary-mode -1)
5677   (let ((name (buffer-name)))
5678     (when (string-match "Dead " name)
5679       (rename-buffer
5680        (concat (substring name 0 (match-beginning 0))
5681                (substring name (match-end 0)))
5682        t)))
5683   (gnus-message 3 "This dead summary is now alive again"))
5684
5685 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5686 (defun gnus-summary-fetch-faq (&optional faq-dir)
5687   "Fetch the FAQ for the current group.
5688 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5689 in."
5690   (interactive
5691    (list
5692     (when current-prefix-arg
5693       (completing-read
5694        "Faq dir: " (and (listp gnus-group-faq-directory)
5695                         (mapcar (lambda (file) (list file))
5696                                 gnus-group-faq-directory))))))
5697   (let (gnus-faq-buffer)
5698     (when (setq gnus-faq-buffer
5699                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5700       (gnus-configure-windows 'summary-faq))))
5701
5702 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5703 (defun gnus-summary-describe-group (&optional force)
5704   "Describe the current newsgroup."
5705   (interactive "P")
5706   (gnus-group-describe-group force gnus-newsgroup-name))
5707
5708 (defun gnus-summary-describe-briefly ()
5709   "Describe summary mode commands briefly."
5710   (interactive)
5711   (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")))
5712
5713 ;; Walking around group mode buffer from summary mode.
5714
5715 (defun gnus-summary-next-group (&optional no-article target-group backward)
5716   "Exit current newsgroup and then select next unread newsgroup.
5717 If prefix argument NO-ARTICLE is non-nil, no article is selected
5718 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5719 previous group instead."
5720   (interactive "P")
5721   ;; Stop pre-fetching.
5722   (gnus-async-halt-prefetch)
5723   (let ((current-group gnus-newsgroup-name)
5724         (current-buffer (current-buffer))
5725         entered)
5726     ;; First we semi-exit this group to update Xrefs and all variables.
5727     ;; We can't do a real exit, because the window conf must remain
5728     ;; the same in case the user is prompted for info, and we don't
5729     ;; want the window conf to change before that...
5730     (gnus-summary-exit t)
5731     (while (not entered)
5732       ;; Then we find what group we are supposed to enter.
5733       (set-buffer gnus-group-buffer)
5734       (gnus-group-jump-to-group current-group)
5735       (setq target-group
5736             (or target-group
5737                 (if (eq gnus-keep-same-level 'best)
5738                     (gnus-summary-best-group gnus-newsgroup-name)
5739                   (gnus-summary-search-group backward gnus-keep-same-level))))
5740       (if (not target-group)
5741           ;; There are no further groups, so we return to the group
5742           ;; buffer.
5743           (progn
5744             (gnus-message 5 "Returning to the group buffer")
5745             (setq entered t)
5746             (when (gnus-buffer-live-p current-buffer)
5747               (set-buffer current-buffer)
5748               (gnus-summary-exit))
5749             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5750         ;; We try to enter the target group.
5751         (gnus-group-jump-to-group target-group)
5752         (let ((unreads (gnus-group-group-unread)))
5753           (if (and (or (eq t unreads)
5754                        (and unreads (not (zerop unreads))))
5755                    (gnus-summary-read-group
5756                     target-group nil no-article
5757                     (and (buffer-name current-buffer) current-buffer)
5758                     nil backward))
5759               (setq entered t)
5760             (setq current-group target-group
5761                   target-group nil)))))))
5762
5763 (defun gnus-summary-prev-group (&optional no-article)
5764   "Exit current newsgroup and then select previous unread newsgroup.
5765 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5766   (interactive "P")
5767   (gnus-summary-next-group no-article nil t))
5768
5769 ;; Walking around summary lines.
5770
5771 (defun gnus-summary-first-subject (&optional unread undownloaded)
5772   "Go to the first unread subject.
5773 If UNREAD is non-nil, go to the first unread article.
5774 Returns the article selected or nil if there are no unread articles."
5775   (interactive "P")
5776   (prog1
5777       (cond
5778        ;; Empty summary.
5779        ((null gnus-newsgroup-data)
5780         (gnus-message 3 "No articles in the group")
5781         nil)
5782        ;; Pick the first article.
5783        ((not unread)
5784         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5785         (gnus-data-number (car gnus-newsgroup-data)))
5786        ;; No unread articles.
5787        ((null gnus-newsgroup-unreads)
5788         (gnus-message 3 "No more unread articles")
5789         nil)
5790        ;; Find the first unread article.
5791        (t
5792         (let ((data gnus-newsgroup-data))
5793           (while (and data
5794                       (and (not (and undownloaded
5795                                      (eq gnus-undownloaded-mark
5796                                          (gnus-data-mark (car data)))))
5797                            (not (gnus-data-unread-p (car data)))))
5798             (setq data (cdr data)))
5799           (when data
5800             (goto-char (gnus-data-pos (car data)))
5801             (gnus-data-number (car data))))))
5802     (gnus-summary-position-point)))
5803
5804 (defun gnus-summary-next-subject (n &optional unread dont-display)
5805   "Go to next N'th summary line.
5806 If N is negative, go to the previous N'th subject line.
5807 If UNREAD is non-nil, only unread articles are selected.
5808 The difference between N and the actual number of steps taken is
5809 returned."
5810   (interactive "p")
5811   (let ((backward (< n 0))
5812         (n (abs n)))
5813     (while (and (> n 0)
5814                 (if backward
5815                     (gnus-summary-find-prev unread)
5816                   (gnus-summary-find-next unread)))
5817       (unless (zerop (setq n (1- n)))
5818         (gnus-summary-show-thread)))
5819     (when (/= 0 n)
5820       (gnus-message 7 "No more%s articles"
5821                     (if unread " unread" "")))
5822     (unless dont-display
5823       (gnus-summary-recenter)
5824       (gnus-summary-position-point))
5825     n))
5826
5827 (defun gnus-summary-next-unread-subject (n)
5828   "Go to next N'th unread summary line."
5829   (interactive "p")
5830   (gnus-summary-next-subject n t))
5831
5832 (defun gnus-summary-prev-subject (n &optional unread)
5833   "Go to previous N'th summary line.
5834 If optional argument UNREAD is non-nil, only unread article is selected."
5835   (interactive "p")
5836   (gnus-summary-next-subject (- n) unread))
5837
5838 (defun gnus-summary-prev-unread-subject (n)
5839   "Go to previous N'th unread summary line."
5840   (interactive "p")
5841   (gnus-summary-next-subject (- n) t))
5842
5843 (defun gnus-summary-goto-subject (article &optional force silent)
5844   "Go the subject line of ARTICLE.
5845 If FORCE, also allow jumping to articles not currently shown."
5846   (interactive "nArticle number: ")
5847   (let ((b (point))
5848         (data (gnus-data-find article)))
5849     ;; We read in the article if we have to.
5850     (and (not data)
5851          force
5852          (gnus-summary-insert-subject
5853           article
5854           (if (or (numberp force) (vectorp force)) force)
5855           t)
5856          (setq data (gnus-data-find article)))
5857     (goto-char b)
5858     (if (not data)
5859         (progn
5860           (unless silent
5861             (gnus-message 3 "Can't find article %d" article))
5862           nil)
5863       (goto-char (gnus-data-pos data))
5864       (gnus-summary-position-point)
5865       article)))
5866
5867 ;; Walking around summary lines with displaying articles.
5868
5869 (defun gnus-summary-expand-window (&optional arg)
5870   "Make the summary buffer take up the entire Emacs frame.
5871 Given a prefix, will force an `article' buffer configuration."
5872   (interactive "P")
5873   (if arg
5874       (gnus-configure-windows 'article 'force)
5875     (gnus-configure-windows 'summary 'force)))
5876
5877 (defun gnus-summary-display-article (article &optional all-header)
5878   "Display ARTICLE in article buffer."
5879   (gnus-set-global-variables)
5880   (when (gnus-buffer-live-p gnus-article-buffer)
5881     (with-current-buffer gnus-article-buffer
5882       (setq gnus-article-charset gnus-newsgroup-charset)
5883       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5884       (mm-enable-multibyte-mule4)))
5885   (if (null article)
5886       nil
5887     (prog1
5888         (if gnus-summary-display-article-function
5889             (funcall gnus-summary-display-article-function article all-header)
5890           (gnus-article-prepare article all-header))
5891       (gnus-run-hooks 'gnus-select-article-hook)
5892       (when (and gnus-current-article
5893                  (not (zerop gnus-current-article)))
5894         (gnus-summary-goto-subject gnus-current-article))
5895       (gnus-summary-recenter)
5896       (when (and gnus-use-trees gnus-show-threads)
5897         (gnus-possibly-generate-tree article)
5898         (gnus-highlight-selected-tree article))
5899       ;; Successfully display article.
5900       (gnus-article-set-window-start
5901        (cdr (assq article gnus-newsgroup-bookmarks))))))
5902
5903 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5904   "Select the current article.
5905 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5906 non-nil, the article will be re-fetched even if it already present in
5907 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5908 be displayed."
5909   ;; Make sure we are in the summary buffer to work around bbdb bug.
5910   (unless (eq major-mode 'gnus-summary-mode)
5911     (set-buffer gnus-summary-buffer))
5912   (let ((article (or article (gnus-summary-article-number)))
5913         (all-headers (not (not all-headers))) ;Must be T or NIL.
5914         gnus-summary-display-article-function)
5915     (and (not pseudo)
5916          (gnus-summary-article-pseudo-p article)
5917          (error "This is a pseudo-article"))
5918     (save-excursion
5919       (set-buffer gnus-summary-buffer)
5920       (if (or (and gnus-single-article-buffer
5921                    (or (null gnus-current-article)
5922                        (null gnus-article-current)
5923                        (null (get-buffer gnus-article-buffer))
5924                        (not (eq article (cdr gnus-article-current)))
5925                        (not (equal (car gnus-article-current)
5926                                    gnus-newsgroup-name))))
5927               (and (not gnus-single-article-buffer)
5928                    (or (null gnus-current-article)
5929                        (not (eq gnus-current-article article))))
5930               force)
5931           ;; The requested article is different from the current article.
5932           (progn
5933             (gnus-summary-display-article article all-headers)
5934             (when (gnus-buffer-live-p gnus-article-buffer)
5935                (with-current-buffer gnus-article-buffer
5936                 (if (not gnus-article-decoded-p) ;; a local variable
5937                     (mm-disable-multibyte-mule4))))
5938             (when (or all-headers gnus-show-all-headers)
5939               (gnus-article-show-all-headers))
5940             (gnus-article-set-window-start
5941              (cdr (assq article gnus-newsgroup-bookmarks)))
5942             article)
5943         (when (or all-headers gnus-show-all-headers)
5944           (gnus-article-show-all-headers))
5945         'old))))
5946
5947 (defun gnus-summary-force-verify-and-decrypt ()
5948   (interactive)
5949   (let ((mm-verify-option 'known)
5950         (mm-decrypt-option 'known))
5951     (gnus-summary-select-article nil 'force)))
5952
5953 (defun gnus-summary-set-current-mark (&optional current-mark)
5954   "Obsolete function."
5955   nil)
5956
5957 (defun gnus-summary-next-article (&optional unread subject backward push)
5958   "Select the next article.
5959 If UNREAD, only unread articles are selected.
5960 If SUBJECT, only articles with SUBJECT are selected.
5961 If BACKWARD, the previous article is selected instead of the next."
5962   (interactive "P")
5963   (cond
5964    ;; Is there such an article?
5965    ((and (gnus-summary-search-forward unread subject backward)
5966          (or (gnus-summary-display-article (gnus-summary-article-number))
5967              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5968     (gnus-summary-position-point))
5969    ;; If not, we try the first unread, if that is wanted.
5970    ((and subject
5971          gnus-auto-select-same
5972          (gnus-summary-first-unread-article))
5973     (gnus-summary-position-point)
5974     (gnus-message 6 "Wrapped"))
5975    ;; Try to get next/previous article not displayed in this group.
5976    ((and gnus-auto-extend-newsgroup
5977          (not unread) (not subject))
5978     (gnus-summary-goto-article
5979      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5980      nil (count-lines (point-min) (point))))
5981    ;; Go to next/previous group.
5982    (t
5983     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5984       (gnus-summary-jump-to-group gnus-newsgroup-name))
5985     (let ((cmd last-command-char)
5986           (point
5987            (save-excursion
5988              (set-buffer gnus-group-buffer)
5989              (point)))
5990           (group
5991            (if (eq gnus-keep-same-level 'best)
5992                (gnus-summary-best-group gnus-newsgroup-name)
5993              (gnus-summary-search-group backward gnus-keep-same-level))))
5994       ;; For some reason, the group window gets selected.  We change
5995       ;; it back.
5996       (select-window (get-buffer-window (current-buffer)))
5997       ;; Select next unread newsgroup automagically.
5998       (cond
5999        ((or (not gnus-auto-select-next)
6000             (not cmd))
6001         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6002        ((or (eq gnus-auto-select-next 'quietly)
6003             (and (eq gnus-auto-select-next 'slightly-quietly)
6004                  push)
6005             (and (eq gnus-auto-select-next 'almost-quietly)
6006                  (gnus-summary-last-article-p)))
6007         ;; Select quietly.
6008         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6009             (gnus-summary-exit)
6010           (gnus-message 7 "No more%s articles (%s)..."
6011                         (if unread " unread" "")
6012                         (if group (concat "selecting " group)
6013                           "exiting"))
6014           (gnus-summary-next-group nil group backward)))
6015        (t
6016         (when (gnus-key-press-event-p last-input-event)
6017           (gnus-summary-walk-group-buffer
6018            gnus-newsgroup-name cmd unread backward point))))))))
6019
6020 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6021   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6022                       (?\C-p (gnus-group-prev-unread-group 1))))
6023         (cursor-in-echo-area t)
6024         keve key group ended)
6025     (save-excursion
6026       (set-buffer gnus-group-buffer)
6027       (goto-char start)
6028       (setq group
6029             (if (eq gnus-keep-same-level 'best)
6030                 (gnus-summary-best-group gnus-newsgroup-name)
6031               (gnus-summary-search-group backward gnus-keep-same-level))))
6032     (while (not ended)
6033       (gnus-message
6034        5 "No more%s articles%s" (if unread " unread" "")
6035        (if (and group
6036                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6037            (format " (Type %s for %s [%s])"
6038                    (single-key-description cmd) group
6039                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6040          (format " (Type %s to exit %s)"
6041                  (single-key-description cmd)
6042                  gnus-newsgroup-name)))
6043       ;; Confirm auto selection.
6044       (setq key (car (setq keve (gnus-read-event-char))))
6045       (setq ended t)
6046       (cond
6047        ((assq key keystrokes)
6048         (let ((obuf (current-buffer)))
6049           (switch-to-buffer gnus-group-buffer)
6050           (when group
6051             (gnus-group-jump-to-group group))
6052           (eval (cadr (assq key keystrokes)))
6053           (setq group (gnus-group-group-name))
6054           (switch-to-buffer obuf))
6055         (setq ended nil))
6056        ((equal key cmd)
6057         (if (or (not group)
6058                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6059             (gnus-summary-exit)
6060           (gnus-summary-next-group nil group backward)))
6061        (t
6062         (push (cdr keve) unread-command-events))))))
6063
6064 (defun gnus-summary-next-unread-article ()
6065   "Select unread article after current one."
6066   (interactive)
6067   (gnus-summary-next-article
6068    (or (not (eq gnus-summary-goto-unread 'never))
6069        (gnus-summary-last-article-p (gnus-summary-article-number)))
6070    (and gnus-auto-select-same
6071         (gnus-summary-article-subject))))
6072
6073 (defun gnus-summary-prev-article (&optional unread subject)
6074   "Select the article after the current one.
6075 If UNREAD is non-nil, only unread articles are selected."
6076   (interactive "P")
6077   (gnus-summary-next-article unread subject t))
6078
6079 (defun gnus-summary-prev-unread-article ()
6080   "Select unread article before current one."
6081   (interactive)
6082   (gnus-summary-prev-article
6083    (or (not (eq gnus-summary-goto-unread 'never))
6084        (gnus-summary-first-article-p (gnus-summary-article-number)))
6085    (and gnus-auto-select-same
6086         (gnus-summary-article-subject))))
6087
6088 (defun gnus-summary-next-page (&optional lines circular)
6089   "Show next page of the selected article.
6090 If at the end of the current article, select the next article.
6091 LINES says how many lines should be scrolled up.
6092
6093 If CIRCULAR is non-nil, go to the start of the article instead of
6094 selecting the next article when reaching the end of the current
6095 article."
6096   (interactive "P")
6097   (setq gnus-summary-buffer (current-buffer))
6098   (gnus-set-global-variables)
6099   (let ((article (gnus-summary-article-number))
6100         (article-window (get-buffer-window gnus-article-buffer t))
6101         endp)
6102     ;; If the buffer is empty, we have no article.
6103     (unless article
6104       (error "No article to select"))
6105     (gnus-configure-windows 'article)
6106     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6107         (if (and (eq gnus-summary-goto-unread 'never)
6108                  (not (gnus-summary-last-article-p article)))
6109             (gnus-summary-next-article)
6110           (gnus-summary-next-unread-article))
6111       (if (or (null gnus-current-article)
6112               (null gnus-article-current)
6113               (/= article (cdr gnus-article-current))
6114               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6115           ;; Selected subject is different from current article's.
6116           (gnus-summary-display-article article)
6117         (when article-window
6118           (gnus-eval-in-buffer-window gnus-article-buffer
6119             (setq endp (gnus-article-next-page lines)))
6120           (when endp
6121             (cond (circular
6122                    (gnus-summary-beginning-of-article))
6123                   (lines
6124                    (gnus-message 3 "End of message"))
6125                   ((null lines)
6126                    (if (and (eq gnus-summary-goto-unread 'never)
6127                             (not (gnus-summary-last-article-p article)))
6128                        (gnus-summary-next-article)
6129                      (gnus-summary-next-unread-article))))))))
6130     (gnus-summary-recenter)
6131     (gnus-summary-position-point)))
6132
6133 (defun gnus-summary-prev-page (&optional lines move)
6134   "Show previous page of selected article.
6135 Argument LINES specifies lines to be scrolled down.
6136 If MOVE, move to the previous unread article if point is at
6137 the beginning of the buffer."
6138   (interactive "P")
6139   (let ((article (gnus-summary-article-number))
6140         (article-window (get-buffer-window gnus-article-buffer t))
6141         endp)
6142     (gnus-configure-windows 'article)
6143     (if (or (null gnus-current-article)
6144             (null gnus-article-current)
6145             (/= article (cdr gnus-article-current))
6146             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6147         ;; Selected subject is different from current article's.
6148         (gnus-summary-display-article article)
6149       (gnus-summary-recenter)
6150       (when article-window
6151         (gnus-eval-in-buffer-window gnus-article-buffer
6152           (setq endp (gnus-article-prev-page lines)))
6153         (when (and move endp)
6154           (cond (lines
6155                  (gnus-message 3 "Beginning of message"))
6156                 ((null lines)
6157                  (if (and (eq gnus-summary-goto-unread 'never)
6158                           (not (gnus-summary-first-article-p article)))
6159                      (gnus-summary-prev-article)
6160                    (gnus-summary-prev-unread-article))))))))
6161   (gnus-summary-position-point))
6162
6163 (defun gnus-summary-prev-page-or-article (&optional lines)
6164   "Show previous page of selected article.
6165 Argument LINES specifies lines to be scrolled down.
6166 If at the beginning of the article, go to the next article."
6167   (interactive "P")
6168   (gnus-summary-prev-page lines t))
6169
6170 (defun gnus-summary-scroll-up (lines)
6171   "Scroll up (or down) one line current article.
6172 Argument LINES specifies lines to be scrolled up (or down if negative)."
6173   (interactive "p")
6174   (gnus-configure-windows 'article)
6175   (gnus-summary-show-thread)
6176   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6177     (gnus-eval-in-buffer-window gnus-article-buffer
6178       (cond ((> lines 0)
6179              (when (gnus-article-next-page lines)
6180                (gnus-message 3 "End of message")))
6181             ((< lines 0)
6182              (gnus-article-prev-page (- lines))))))
6183   (gnus-summary-recenter)
6184   (gnus-summary-position-point))
6185
6186 (defun gnus-summary-scroll-down (lines)
6187   "Scroll down (or up) one line current article.
6188 Argument LINES specifies lines to be scrolled down (or up if negative)."
6189   (interactive "p")
6190   (gnus-summary-scroll-up (- lines)))
6191
6192 (defun gnus-summary-next-same-subject ()
6193   "Select next article which has the same subject as current one."
6194   (interactive)
6195   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6196
6197 (defun gnus-summary-prev-same-subject ()
6198   "Select previous article which has the same subject as current one."
6199   (interactive)
6200   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6201
6202 (defun gnus-summary-next-unread-same-subject ()
6203   "Select next unread article which has the same subject as current one."
6204   (interactive)
6205   (gnus-summary-next-article t (gnus-summary-article-subject)))
6206
6207 (defun gnus-summary-prev-unread-same-subject ()
6208   "Select previous unread article which has the same subject as current one."
6209   (interactive)
6210   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6211
6212 (defun gnus-summary-first-unread-article ()
6213   "Select the first unread article.
6214 Return nil if there are no unread articles."
6215   (interactive)
6216   (prog1
6217       (when (gnus-summary-first-subject t)
6218         (gnus-summary-show-thread)
6219         (gnus-summary-first-subject t)
6220         (gnus-summary-display-article (gnus-summary-article-number)))
6221     (gnus-summary-position-point)))
6222
6223 (defun gnus-summary-first-unread-subject ()
6224   "Place the point on the subject line of the first unread article.
6225 Return nil if there are no unread articles."
6226   (interactive)
6227   (prog1
6228       (when (gnus-summary-first-subject t)
6229         (gnus-summary-show-thread)
6230         (gnus-summary-first-subject t))
6231     (gnus-summary-position-point)))
6232
6233 (defun gnus-summary-first-article ()
6234   "Select the first article.
6235 Return nil if there are no articles."
6236   (interactive)
6237   (prog1
6238       (when (gnus-summary-first-subject)
6239         (gnus-summary-show-thread)
6240         (gnus-summary-first-subject)
6241         (gnus-summary-display-article (gnus-summary-article-number)))
6242     (gnus-summary-position-point)))
6243
6244 (defun gnus-summary-best-unread-article ()
6245   "Select the unread article with the highest score."
6246   (interactive)
6247   (let ((best -1000000)
6248         (data gnus-newsgroup-data)
6249         article score)
6250     (while data
6251       (and (gnus-data-unread-p (car data))
6252            (> (setq score
6253                     (gnus-summary-article-score (gnus-data-number (car data))))
6254               best)
6255            (setq best score
6256                  article (gnus-data-number (car data))))
6257       (setq data (cdr data)))
6258     (prog1
6259         (if article
6260             (gnus-summary-goto-article article)
6261           (error "No unread articles"))
6262       (gnus-summary-position-point))))
6263
6264 (defun gnus-summary-last-subject ()
6265   "Go to the last displayed subject line in the group."
6266   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6267     (when article
6268       (gnus-summary-goto-subject article))))
6269
6270 (defun gnus-summary-goto-article (article &optional all-headers force)
6271   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6272 If ALL-HEADERS is non-nil, no header lines are hidden.
6273 If FORCE, go to the article even if it isn't displayed.  If FORCE
6274 is a number, it is the line the article is to be displayed on."
6275   (interactive
6276    (list
6277     (completing-read
6278      "Article number or Message-ID: "
6279      (mapcar (lambda (number) (list (int-to-string number)))
6280              gnus-newsgroup-limit))
6281     current-prefix-arg
6282     t))
6283   (prog1
6284       (if (and (stringp article)
6285                (string-match "@" article))
6286           (gnus-summary-refer-article article)
6287         (when (stringp article)
6288           (setq article (string-to-number article)))
6289         (if (gnus-summary-goto-subject article force)
6290             (gnus-summary-display-article article all-headers)
6291           (gnus-message 4 "Couldn't go to article %s" article) nil))
6292     (gnus-summary-position-point)))
6293
6294 (defun gnus-summary-goto-last-article ()
6295   "Go to the previously read article."
6296   (interactive)
6297   (prog1
6298       (when gnus-last-article
6299         (gnus-summary-goto-article gnus-last-article nil t))
6300     (gnus-summary-position-point)))
6301
6302 (defun gnus-summary-pop-article (number)
6303   "Pop one article off the history and go to the previous.
6304 NUMBER articles will be popped off."
6305   (interactive "p")
6306   (let (to)
6307     (setq gnus-newsgroup-history
6308           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6309     (if to
6310         (gnus-summary-goto-article (car to) nil t)
6311       (error "Article history empty")))
6312   (gnus-summary-position-point))
6313
6314 ;; Summary commands and functions for limiting the summary buffer.
6315
6316 (defun gnus-summary-limit-to-articles (n)
6317   "Limit the summary buffer to the next N articles.
6318 If not given a prefix, use the process marked articles instead."
6319   (interactive "P")
6320   (prog1
6321       (let ((articles (gnus-summary-work-articles n)))
6322         (setq gnus-newsgroup-processable nil)
6323         (gnus-summary-limit articles))
6324     (gnus-summary-position-point)))
6325
6326 (defun gnus-summary-pop-limit (&optional total)
6327   "Restore the previous limit.
6328 If given a prefix, remove all limits."
6329   (interactive "P")
6330   (when total
6331     (setq gnus-newsgroup-limits
6332           (list (mapcar (lambda (h) (mail-header-number h))
6333                         gnus-newsgroup-headers))))
6334   (unless gnus-newsgroup-limits
6335     (error "No limit to pop"))
6336   (prog1
6337       (gnus-summary-limit nil 'pop)
6338     (gnus-summary-position-point)))
6339
6340 (defun gnus-summary-limit-to-subject (subject &optional header)
6341   "Limit the summary buffer to articles that have subjects that match a regexp."
6342   (interactive "sLimit to subject (regexp): ")
6343   (unless header
6344     (setq header "subject"))
6345   (when (not (equal "" subject))
6346     (prog1
6347         (let ((articles (gnus-summary-find-matching
6348                          (or header "subject") subject 'all)))
6349           (unless articles
6350             (error "Found no matches for \"%s\"" subject))
6351           (gnus-summary-limit articles))
6352       (gnus-summary-position-point))))
6353
6354 (defun gnus-summary-limit-to-author (from)
6355   "Limit the summary buffer to articles that have authors that match a regexp."
6356   (interactive "sLimit to author (regexp): ")
6357   (gnus-summary-limit-to-subject from "from"))
6358
6359 (defun gnus-summary-limit-to-age (age &optional younger-p)
6360   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6361 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6362 articles that are younger than AGE days."
6363   (interactive
6364    (let ((younger current-prefix-arg)
6365          (days-got nil)
6366          days)
6367      (while (not days-got)
6368        (setq days (if younger
6369                       (read-string "Limit to articles within (in days): ")
6370                     (read-string "Limit to articles old than (in days): ")))
6371        (when (> (length days) 0)
6372          (setq days (read days)))
6373        (if (numberp days)
6374            (setq days-got t)
6375          (message "Please enter a number.")
6376          (sleep-for 1)))
6377      (list days younger)))
6378   (prog1
6379       (let ((data gnus-newsgroup-data)
6380             (cutoff (days-to-time age))
6381             articles d date is-younger)
6382         (while (setq d (pop data))
6383           (when (and (vectorp (gnus-data-header d))
6384                      (setq date (mail-header-date (gnus-data-header d))))
6385             (setq is-younger (time-less-p
6386                               (time-since (condition-case ()
6387                                               (date-to-time date)
6388                                             (error '(0 0))))
6389                               cutoff))
6390             (when (if younger-p
6391                       is-younger
6392                     (not is-younger))
6393               (push (gnus-data-number d) articles))))
6394         (gnus-summary-limit (nreverse articles)))
6395     (gnus-summary-position-point)))
6396
6397 (defun gnus-summary-limit-to-extra (header regexp)
6398   "Limit the summary buffer to articles that match an 'extra' header."
6399   (interactive
6400    (let ((header
6401           (intern
6402            (gnus-completing-read
6403             (symbol-name (car gnus-extra-headers))
6404             "Limit extra header:"
6405             (mapcar (lambda (x)
6406                       (cons (symbol-name x) x))
6407                     gnus-extra-headers)
6408             nil
6409             t))))
6410      (list header
6411            (read-string (format "Limit to header %s (regexp): " header)))))
6412   (when (not (equal "" regexp))
6413     (prog1
6414         (let ((articles (gnus-summary-find-matching
6415                          (cons 'extra header) regexp 'all)))
6416           (unless articles
6417             (error "Found no matches for \"%s\"" regexp))
6418           (gnus-summary-limit articles))
6419       (gnus-summary-position-point))))
6420
6421 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6422 (make-obsolete
6423  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6424
6425 (defun gnus-summary-limit-to-unread (&optional all)
6426   "Limit the summary buffer to articles that are not marked as read.
6427 If ALL is non-nil, limit strictly to unread articles."
6428   (interactive "P")
6429   (if all
6430       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6431     (gnus-summary-limit-to-marks
6432      ;; Concat all the marks that say that an article is read and have
6433      ;; those removed.
6434      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6435            gnus-killed-mark gnus-kill-file-mark
6436            gnus-low-score-mark gnus-expirable-mark
6437            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6438            gnus-duplicate-mark gnus-souped-mark)
6439      'reverse)))
6440
6441 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6442 (make-obsolete 'gnus-summary-delete-marked-with
6443                'gnus-summary-limit-exlude-marks)
6444
6445 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6446   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6447 If REVERSE, limit the summary buffer to articles that are marked
6448 with MARKS.  MARKS can either be a string of marks or a list of marks.
6449 Returns how many articles were removed."
6450   (interactive "sMarks: ")
6451   (gnus-summary-limit-to-marks marks t))
6452
6453 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6454   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6455 If REVERSE (the prefix), limit the summary buffer to articles that are
6456 not marked with MARKS.  MARKS can either be a string of marks or a
6457 list of marks.
6458 Returns how many articles were removed."
6459   (interactive "sMarks: \nP")
6460   (prog1
6461       (let ((data gnus-newsgroup-data)
6462             (marks (if (listp marks) marks
6463                      (append marks nil))) ; Transform to list.
6464             articles)
6465         (while data
6466           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6467                   (memq (gnus-data-mark (car data)) marks))
6468             (push (gnus-data-number (car data)) articles))
6469           (setq data (cdr data)))
6470         (gnus-summary-limit articles))
6471     (gnus-summary-position-point)))
6472
6473 (defun gnus-summary-limit-to-score (&optional score)
6474   "Limit to articles with score at or above SCORE."
6475   (interactive "P")
6476   (setq score (if score
6477                   (prefix-numeric-value score)
6478                 (or gnus-summary-default-score 0)))
6479   (let ((data gnus-newsgroup-data)
6480         articles)
6481     (while data
6482       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6483                 score)
6484         (push (gnus-data-number (car data)) articles))
6485       (setq data (cdr data)))
6486     (prog1
6487         (gnus-summary-limit articles)
6488       (gnus-summary-position-point))))
6489
6490 (defun gnus-summary-limit-include-thread (id)
6491   "Display all the hidden articles that in the current thread."
6492   (interactive (list (mail-header-id (gnus-summary-article-header))))
6493   (let ((articles (gnus-articles-in-thread
6494                    (gnus-id-to-thread (gnus-root-id id)))))
6495     (prog1
6496         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6497       (gnus-summary-position-point))))
6498
6499 (defun gnus-summary-limit-include-dormant ()
6500   "Display all the hidden articles that are marked as dormant.
6501 Note that this command only works on a subset of the articles currently
6502 fetched for this group."
6503   (interactive)
6504   (unless gnus-newsgroup-dormant
6505     (error "There are no dormant articles in this group"))
6506   (prog1
6507       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6508     (gnus-summary-position-point)))
6509
6510 (defun gnus-summary-limit-exclude-dormant ()
6511   "Hide all dormant articles."
6512   (interactive)
6513   (prog1
6514       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6515     (gnus-summary-position-point)))
6516
6517 (defun gnus-summary-limit-exclude-childless-dormant ()
6518   "Hide all dormant articles that have no children."
6519   (interactive)
6520   (let ((data (gnus-data-list t))
6521         articles d children)
6522     ;; Find all articles that are either not dormant or have
6523     ;; children.
6524     (while (setq d (pop data))
6525       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6526                 (and (setq children
6527                            (gnus-article-children (gnus-data-number d)))
6528                      (let (found)
6529                        (while children
6530                          (when (memq (car children) articles)
6531                            (setq children nil
6532                                  found t))
6533                          (pop children))
6534                        found)))
6535         (push (gnus-data-number d) articles)))
6536     ;; Do the limiting.
6537     (prog1
6538         (gnus-summary-limit articles)
6539       (gnus-summary-position-point))))
6540
6541 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6542   "Mark all unread excluded articles as read.
6543 If ALL, mark even excluded ticked and dormants as read."
6544   (interactive "P")
6545   (let ((articles (gnus-sorted-complement
6546                    (sort
6547                     (mapcar (lambda (h) (mail-header-number h))
6548                             gnus-newsgroup-headers)
6549                     '<)
6550                    (sort gnus-newsgroup-limit '<)))
6551         article)
6552     (setq gnus-newsgroup-unreads
6553           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6554     (if all
6555         (setq gnus-newsgroup-dormant nil
6556               gnus-newsgroup-marked nil
6557               gnus-newsgroup-reads
6558               (nconc
6559                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6560                gnus-newsgroup-reads))
6561       (while (setq article (pop articles))
6562         (unless (or (memq article gnus-newsgroup-dormant)
6563                     (memq article gnus-newsgroup-marked))
6564           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6565
6566 (defun gnus-summary-limit (articles &optional pop)
6567   (if pop
6568       ;; We pop the previous limit off the stack and use that.
6569       (setq articles (car gnus-newsgroup-limits)
6570             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6571     ;; We use the new limit, so we push the old limit on the stack.
6572     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6573   ;; Set the limit.
6574   (setq gnus-newsgroup-limit articles)
6575   (let ((total (length gnus-newsgroup-data))
6576         (data (gnus-data-find-list (gnus-summary-article-number)))
6577         (gnus-summary-mark-below nil)   ; Inhibit this.
6578         found)
6579     ;; This will do all the work of generating the new summary buffer
6580     ;; according to the new limit.
6581     (gnus-summary-prepare)
6582     ;; Hide any threads, possibly.
6583     (and gnus-show-threads
6584          gnus-thread-hide-subtree
6585          (gnus-summary-hide-all-threads))
6586     ;; Try to return to the article you were at, or one in the
6587     ;; neighborhood.
6588     (when data
6589       ;; We try to find some article after the current one.
6590       (while data
6591         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6592           (setq data nil
6593                 found t))
6594         (setq data (cdr data))))
6595     (unless found
6596       ;; If there is no data, that means that we were after the last
6597       ;; article.  The same goes when we can't find any articles
6598       ;; after the current one.
6599       (goto-char (point-max))
6600       (gnus-summary-find-prev))
6601     (gnus-set-mode-line 'summary)
6602     ;; We return how many articles were removed from the summary
6603     ;; buffer as a result of the new limit.
6604     (- total (length gnus-newsgroup-data))))
6605
6606 (defsubst gnus-invisible-cut-children (threads)
6607   (let ((num 0))
6608     (while threads
6609       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6610         (incf num))
6611       (pop threads))
6612     (< num 2)))
6613
6614 (defsubst gnus-cut-thread (thread)
6615   "Go forwards in the thread until we find an article that we want to display."
6616   (when (or (eq gnus-fetch-old-headers 'some)
6617             (eq gnus-fetch-old-headers 'invisible)
6618             (numberp gnus-fetch-old-headers)
6619             (eq gnus-build-sparse-threads 'some)
6620             (eq gnus-build-sparse-threads 'more))
6621     ;; Deal with old-fetched headers and sparse threads.
6622     (while (and
6623             thread
6624             (or
6625              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6626              (gnus-summary-article-ancient-p
6627               (mail-header-number (car thread))))
6628             (if (or (<= (length (cdr thread)) 1)
6629                     (eq gnus-fetch-old-headers 'invisible))
6630                 (setq gnus-newsgroup-limit
6631                       (delq (mail-header-number (car thread))
6632                             gnus-newsgroup-limit)
6633                       thread (cadr thread))
6634               (when (gnus-invisible-cut-children (cdr thread))
6635                 (let ((th (cdr thread)))
6636                   (while th
6637                     (if (memq (mail-header-number (caar th))
6638                               gnus-newsgroup-limit)
6639                         (setq thread (car th)
6640                               th nil)
6641                       (setq th (cdr th))))))))))
6642   thread)
6643
6644 (defun gnus-cut-threads (threads)
6645   "Cut off all uninteresting articles from the beginning of threads."
6646   (when (or (eq gnus-fetch-old-headers 'some)
6647             (eq gnus-fetch-old-headers 'invisible)
6648             (numberp gnus-fetch-old-headers)
6649             (eq gnus-build-sparse-threads 'some)
6650             (eq gnus-build-sparse-threads 'more))
6651     (let ((th threads))
6652       (while th
6653         (setcar th (gnus-cut-thread (car th)))
6654         (setq th (cdr th)))))
6655   ;; Remove nixed out threads.
6656   (delq nil threads))
6657
6658 (defun gnus-summary-initial-limit (&optional show-if-empty)
6659   "Figure out what the initial limit is supposed to be on group entry.
6660 This entails weeding out unwanted dormants, low-scored articles,
6661 fetch-old-headers verbiage, and so on."
6662   ;; Most groups have nothing to remove.
6663   (if (or gnus-inhibit-limiting
6664           (and (null gnus-newsgroup-dormant)
6665                (not (eq gnus-fetch-old-headers 'some))
6666                (not (numberp gnus-fetch-old-headers))
6667                (not (eq gnus-fetch-old-headers 'invisible))
6668                (null gnus-summary-expunge-below)
6669                (not (eq gnus-build-sparse-threads 'some))
6670                (not (eq gnus-build-sparse-threads 'more))
6671                (null gnus-thread-expunge-below)
6672                (not gnus-use-nocem)))
6673       ()                                ; Do nothing.
6674     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6675     (setq gnus-newsgroup-limit nil)
6676     (mapatoms
6677      (lambda (node)
6678        (unless (car (symbol-value node))
6679          ;; These threads have no parents -- they are roots.
6680          (let ((nodes (cdr (symbol-value node)))
6681                thread)
6682            (while nodes
6683              (if (and gnus-thread-expunge-below
6684                       (< (gnus-thread-total-score (car nodes))
6685                          gnus-thread-expunge-below))
6686                  (gnus-expunge-thread (pop nodes))
6687                (setq thread (pop nodes))
6688                (gnus-summary-limit-children thread))))))
6689      gnus-newsgroup-dependencies)
6690     ;; If this limitation resulted in an empty group, we might
6691     ;; pop the previous limit and use it instead.
6692     (when (and (not gnus-newsgroup-limit)
6693                show-if-empty)
6694       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6695     gnus-newsgroup-limit))
6696
6697 (defun gnus-summary-limit-children (thread)
6698   "Return 1 if this subthread is visible and 0 if it is not."
6699   ;; First we get the number of visible children to this thread.  This
6700   ;; is done by recursing down the thread using this function, so this
6701   ;; will really go down to a leaf article first, before slowly
6702   ;; working its way up towards the root.
6703   (when thread
6704     (let ((children
6705            (if (cdr thread)
6706                (apply '+ (mapcar 'gnus-summary-limit-children
6707                                  (cdr thread)))
6708              0))
6709           (number (mail-header-number (car thread)))
6710           score)
6711       (if (and
6712            (not (memq number gnus-newsgroup-marked))
6713            (or
6714             ;; If this article is dormant and has absolutely no visible
6715             ;; children, then this article isn't visible.
6716             (and (memq number gnus-newsgroup-dormant)
6717                  (zerop children))
6718             ;; If this is "fetch-old-headered" and there is no
6719             ;; visible children, then we don't want this article.
6720             (and (or (eq gnus-fetch-old-headers 'some)
6721                      (numberp gnus-fetch-old-headers))
6722                  (gnus-summary-article-ancient-p number)
6723                  (zerop children))
6724             ;; If this is "fetch-old-headered" and `invisible', then
6725             ;; we don't want this article.
6726             (and (eq gnus-fetch-old-headers 'invisible)
6727                  (gnus-summary-article-ancient-p number))
6728             ;; If this is a sparsely inserted article with no children,
6729             ;; we don't want it.
6730             (and (eq gnus-build-sparse-threads 'some)
6731                  (gnus-summary-article-sparse-p number)
6732                  (zerop children))
6733             ;; If we use expunging, and this article is really
6734             ;; low-scored, then we don't want this article.
6735             (when (and gnus-summary-expunge-below
6736                        (< (setq score
6737                                 (or (cdr (assq number gnus-newsgroup-scored))
6738                                     gnus-summary-default-score))
6739                           gnus-summary-expunge-below))
6740               ;; We increase the expunge-tally here, but that has
6741               ;; nothing to do with the limits, really.
6742               (incf gnus-newsgroup-expunged-tally)
6743               ;; We also mark as read here, if that's wanted.
6744               (when (and gnus-summary-mark-below
6745                          (< score gnus-summary-mark-below))
6746                 (setq gnus-newsgroup-unreads
6747                       (delq number gnus-newsgroup-unreads))
6748                 (if gnus-newsgroup-auto-expire
6749                     (push number gnus-newsgroup-expirable)
6750                   (push (cons number gnus-low-score-mark)
6751                         gnus-newsgroup-reads)))
6752               t)
6753             ;; Check NoCeM things.
6754             (if (and gnus-use-nocem
6755                      (gnus-nocem-unwanted-article-p
6756                       (mail-header-id (car thread))))
6757                 (progn
6758                   (setq gnus-newsgroup-unreads
6759                         (delq number gnus-newsgroup-unreads))
6760                   t))))
6761           ;; Nope, invisible article.
6762           0
6763         ;; Ok, this article is to be visible, so we add it to the limit
6764         ;; and return 1.
6765         (push number gnus-newsgroup-limit)
6766         1))))
6767
6768 (defun gnus-expunge-thread (thread)
6769   "Mark all articles in THREAD as read."
6770   (let* ((number (mail-header-number (car thread))))
6771     (incf gnus-newsgroup-expunged-tally)
6772     ;; We also mark as read here, if that's wanted.
6773     (setq gnus-newsgroup-unreads
6774           (delq number gnus-newsgroup-unreads))
6775     (if gnus-newsgroup-auto-expire
6776         (push number gnus-newsgroup-expirable)
6777       (push (cons number gnus-low-score-mark)
6778             gnus-newsgroup-reads)))
6779   ;; Go recursively through all subthreads.
6780   (mapcar 'gnus-expunge-thread (cdr thread)))
6781
6782 ;; Summary article oriented commands
6783
6784 (defun gnus-summary-refer-parent-article (n)
6785   "Refer parent article N times.
6786 If N is negative, go to ancestor -N instead.
6787 The difference between N and the number of articles fetched is returned."
6788   (interactive "p")
6789   (let ((skip 1)
6790         error header ref)
6791     (when (not (natnump n))
6792       (setq skip (abs n)
6793             n 1))
6794     (while (and (> n 0)
6795                 (not error))
6796       (setq header (gnus-summary-article-header))
6797       (if (and (eq (mail-header-number header)
6798                    (cdr gnus-article-current))
6799                (equal gnus-newsgroup-name
6800                       (car gnus-article-current)))
6801           ;; If we try to find the parent of the currently
6802           ;; displayed article, then we take a look at the actual
6803           ;; References header, since this is slightly more
6804           ;; reliable than the References field we got from the
6805           ;; server.
6806           (save-excursion
6807             (set-buffer gnus-original-article-buffer)
6808             (nnheader-narrow-to-headers)
6809             (unless (setq ref (message-fetch-field "references"))
6810               (setq ref (message-fetch-field "in-reply-to")))
6811             (widen))
6812         (setq ref
6813               ;; It's not the current article, so we take a bet on
6814               ;; the value we got from the server.
6815               (mail-header-references header)))
6816       (if (and ref
6817                (not (equal ref "")))
6818           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6819             (gnus-message 1 "Couldn't find parent"))
6820         (gnus-message 1 "No references in article %d"
6821                       (gnus-summary-article-number))
6822         (setq error t))
6823       (decf n))
6824     (gnus-summary-position-point)
6825     n))
6826
6827 (defun gnus-summary-refer-references ()
6828   "Fetch all articles mentioned in the References header.
6829 Return the number of articles fetched."
6830   (interactive)
6831   (let ((ref (mail-header-references (gnus-summary-article-header)))
6832         (current (gnus-summary-article-number))
6833         (n 0))
6834     (if (or (not ref)
6835             (equal ref ""))
6836         (error "No References in the current article")
6837       ;; For each Message-ID in the References header...
6838       (while (string-match "<[^>]*>" ref)
6839         (incf n)
6840         ;; ... fetch that article.
6841         (gnus-summary-refer-article
6842          (prog1 (match-string 0 ref)
6843            (setq ref (substring ref (match-end 0))))))
6844       (gnus-summary-goto-subject current)
6845       (gnus-summary-position-point)
6846       n)))
6847
6848 (defun gnus-summary-refer-thread (&optional limit)
6849   "Fetch all articles in the current thread.
6850 If LIMIT (the numerical prefix), fetch that many old headers instead
6851 of what's specified by the `gnus-refer-thread-limit' variable."
6852   (interactive "P")
6853   (let ((id (mail-header-id (gnus-summary-article-header)))
6854         (limit (if limit (prefix-numeric-value limit)
6855                  gnus-refer-thread-limit)))
6856     ;; We want to fetch LIMIT *old* headers, but we also have to
6857     ;; re-fetch all the headers in the current buffer, because many of
6858     ;; them may be undisplayed.  So we adjust LIMIT.
6859     (when (numberp limit)
6860       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6861     (unless (eq gnus-fetch-old-headers 'invisible)
6862       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6863       ;; Retrieve the headers and read them in.
6864       (if (eq (gnus-retrieve-headers
6865                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6866               'nov)
6867           (gnus-build-all-threads)
6868         (error "Can't fetch thread from backends that don't support NOV"))
6869       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6870     (gnus-summary-limit-include-thread id)))
6871
6872 (defun gnus-summary-refer-article (message-id)
6873   "Fetch an article specified by MESSAGE-ID."
6874   (interactive "sMessage-ID: ")
6875   (when (and (stringp message-id)
6876              (not (zerop (length message-id))))
6877     ;; Construct the correct Message-ID if necessary.
6878     ;; Suggested by tale@pawl.rpi.edu.
6879     (unless (string-match "^<" message-id)
6880       (setq message-id (concat "<" message-id)))
6881     (unless (string-match ">$" message-id)
6882       (setq message-id (concat message-id ">")))
6883     (let* ((header (gnus-id-to-header message-id))
6884            (sparse (and header
6885                         (gnus-summary-article-sparse-p
6886                          (mail-header-number header))
6887                         (memq (mail-header-number header)
6888                               gnus-newsgroup-limit)))
6889            number)
6890       (cond
6891        ;; If the article is present in the buffer we just go to it.
6892        ((and header
6893              (or (not (gnus-summary-article-sparse-p
6894                        (mail-header-number header)))
6895                  sparse))
6896         (prog1
6897             (gnus-summary-goto-article
6898              (mail-header-number header) nil t)
6899           (when sparse
6900             (gnus-summary-update-article (mail-header-number header)))))
6901        (t
6902         ;; We fetch the article.
6903         (catch 'found
6904           (dolist (gnus-override-method (gnus-refer-article-methods))
6905             (gnus-check-server gnus-override-method)
6906             ;; Fetch the header, and display the article.
6907             (when (setq number (gnus-summary-insert-subject message-id))
6908               (gnus-summary-select-article nil nil nil number)
6909               (throw 'found t)))
6910           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6911
6912 (defun gnus-refer-article-methods ()
6913   "Return a list of referrable methods."
6914   (cond
6915    ;; No method, so we default to current and native.
6916    ((null gnus-refer-article-method)
6917     (list gnus-current-select-method gnus-select-method))
6918    ;; Current.
6919    ((eq 'current gnus-refer-article-method)
6920     (list gnus-current-select-method))
6921    ;; List of select methods.
6922    ((not (and (symbolp (car gnus-refer-article-method))
6923               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
6924     (let (out)
6925       (dolist (method gnus-refer-article-method)
6926         (push (if (eq 'current method)
6927                   gnus-current-select-method
6928                 method)
6929               out))
6930       (nreverse out)))
6931    ;; One single select method.
6932    (t
6933     (list gnus-refer-article-method))))
6934
6935 (defun gnus-summary-edit-parameters ()
6936   "Edit the group parameters of the current group."
6937   (interactive)
6938   (gnus-group-edit-group gnus-newsgroup-name 'params))
6939
6940 (defun gnus-summary-customize-parameters ()
6941   "Customize the group parameters of the current group."
6942   (interactive)
6943   (gnus-group-customize gnus-newsgroup-name))
6944
6945 (defun gnus-summary-enter-digest-group (&optional force)
6946   "Enter an nndoc group based on the current article.
6947 If FORCE, force a digest interpretation.  If not, try
6948 to guess what the document format is."
6949   (interactive "P")
6950   (let ((conf gnus-current-window-configuration))
6951     (save-excursion
6952       (gnus-summary-select-article))
6953     (setq gnus-current-window-configuration conf)
6954     (let* ((name (format "%s-%d"
6955                          (gnus-group-prefixed-name
6956                           gnus-newsgroup-name (list 'nndoc ""))
6957                          (save-excursion
6958                            (set-buffer gnus-summary-buffer)
6959                            gnus-current-article)))
6960            (ogroup gnus-newsgroup-name)
6961            (params (append (gnus-info-params (gnus-get-info ogroup))
6962                            (list (cons 'to-group ogroup))
6963                            (list (cons 'save-article-group ogroup))))
6964            (case-fold-search t)
6965            (buf (current-buffer))
6966            dig to-address)
6967       (save-excursion
6968         (set-buffer gnus-original-article-buffer)
6969         ;; Have the digest group inherit the main mail address of
6970         ;; the parent article.
6971         (when (setq to-address (or (message-fetch-field "reply-to")
6972                                    (message-fetch-field "from")))
6973           (setq params (append 
6974                         (list (cons 'to-address 
6975                                     (funcall gnus-decode-encoded-word-function
6976                                              to-address))))))
6977         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6978         (insert-buffer-substring gnus-original-article-buffer)
6979         ;; Remove lines that may lead nndoc to misinterpret the
6980         ;; document type.
6981         (narrow-to-region
6982          (goto-char (point-min))
6983          (or (search-forward "\n\n" nil t) (point)))
6984         (goto-char (point-min))
6985         (delete-matching-lines "^Path:\\|^From ")
6986         (widen))
6987       (unwind-protect
6988           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6989                     (gnus-newsgroup-ephemeral-ignored-charsets
6990                      gnus-newsgroup-ignored-charsets))
6991                 (gnus-group-read-ephemeral-group
6992                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6993                               (nndoc-article-type
6994                                ,(if force 'mbox 'guess))) t))
6995               ;; Make all postings to this group go to the parent group.
6996               (nconc (gnus-info-params (gnus-get-info name))
6997                      params)
6998             ;; Couldn't select this doc group.
6999             (switch-to-buffer buf)
7000             (gnus-set-global-variables)
7001             (gnus-configure-windows 'summary)
7002             (gnus-message 3 "Article couldn't be entered?"))
7003         (kill-buffer dig)))))
7004
7005 (defun gnus-summary-read-document (n)
7006   "Open a new group based on the current article(s).
7007 This will allow you to read digests and other similar
7008 documents as newsgroups.
7009 Obeys the standard process/prefix convention."
7010   (interactive "P")
7011   (let* ((articles (gnus-summary-work-articles n))
7012          (ogroup gnus-newsgroup-name)
7013          (params (append (gnus-info-params (gnus-get-info ogroup))
7014                          (list (cons 'to-group ogroup))))
7015          article group egroup groups vgroup)
7016     (while (setq article (pop articles))
7017       (setq group (format "%s-%d" gnus-newsgroup-name article))
7018       (gnus-summary-remove-process-mark article)
7019       (when (gnus-summary-display-article article)
7020         (save-excursion
7021           (with-temp-buffer
7022             (insert-buffer-substring gnus-original-article-buffer)
7023             ;; Remove some headers that may lead nndoc to make
7024             ;; the wrong guess.
7025             (message-narrow-to-head)
7026             (goto-char (point-min))
7027             (delete-matching-lines "^\\(Path\\):\\|^From ")
7028             (widen)
7029             (if (setq egroup
7030                       (gnus-group-read-ephemeral-group
7031                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7032                                      (nndoc-article-type guess))
7033                        t nil t))
7034                 (progn
7035                   ;; Make all postings to this group go to the parent group.
7036                   (nconc (gnus-info-params (gnus-get-info egroup))
7037                          params)
7038                   (push egroup groups))
7039               ;; Couldn't select this doc group.
7040               (gnus-error 3 "Article couldn't be entered"))))))
7041     ;; Now we have selected all the documents.
7042     (cond
7043      ((not groups)
7044       (error "None of the articles could be interpreted as documents"))
7045      ((gnus-group-read-ephemeral-group
7046        (setq vgroup (format
7047                      "nnvirtual:%s-%s" gnus-newsgroup-name
7048                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7049        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7050        t
7051        (cons (current-buffer) 'summary)))
7052      (t
7053       (error "Couldn't select virtual nndoc group")))))
7054
7055 (defun gnus-summary-isearch-article (&optional regexp-p)
7056   "Do incremental search forward on the current article.
7057 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7058   (interactive "P")
7059   (gnus-summary-select-article)
7060   (gnus-configure-windows 'article)
7061   (gnus-eval-in-buffer-window gnus-article-buffer
7062     (save-restriction
7063       (widen)
7064       (isearch-forward regexp-p))))
7065
7066 (defun gnus-summary-search-article-forward (regexp &optional backward)
7067   "Search for an article containing REGEXP forward.
7068 If BACKWARD, search backward instead."
7069   (interactive
7070    (list (read-string
7071           (format "Search article %s (regexp%s): "
7072                   (if current-prefix-arg "backward" "forward")
7073                   (if gnus-last-search-regexp
7074                       (concat ", default " gnus-last-search-regexp)
7075                     "")))
7076          current-prefix-arg))
7077   (if (string-equal regexp "")
7078       (setq regexp (or gnus-last-search-regexp ""))
7079     (setq gnus-last-search-regexp regexp))
7080   (if (gnus-summary-search-article regexp backward)
7081       (gnus-summary-show-thread)
7082     (error "Search failed: \"%s\"" regexp)))
7083
7084 (defun gnus-summary-search-article-backward (regexp)
7085   "Search for an article containing REGEXP backward."
7086   (interactive
7087    (list (read-string
7088           (format "Search article backward (regexp%s): "
7089                   (if gnus-last-search-regexp
7090                       (concat ", default " gnus-last-search-regexp)
7091                     "")))))
7092   (gnus-summary-search-article-forward regexp 'backward))
7093
7094 (defun gnus-summary-search-article (regexp &optional backward)
7095   "Search for an article containing REGEXP.
7096 Optional argument BACKWARD means do search for backward.
7097 `gnus-select-article-hook' is not called during the search."
7098   ;; We have to require this here to make sure that the following
7099   ;; dynamic binding isn't shadowed by autoloading.
7100   (require 'gnus-async)
7101   (require 'gnus-art)
7102   (let ((gnus-select-article-hook nil)  ;Disable hook.
7103         (gnus-article-prepare-hook nil)
7104         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7105         (gnus-use-article-prefetch nil)
7106         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7107         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7108         (sum (current-buffer))
7109         (gnus-display-mime-function nil)
7110         (found nil)
7111         point)
7112     (gnus-save-hidden-threads
7113       (gnus-summary-select-article)
7114       (set-buffer gnus-article-buffer)
7115       (goto-char (window-point (get-buffer-window (current-buffer))))
7116       (when backward
7117         (forward-line -1))
7118       (while (not found)
7119         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7120         (if (if backward
7121                 (re-search-backward regexp nil t)
7122               (re-search-forward regexp nil t))
7123             ;; We found the regexp.
7124             (progn
7125               (setq found 'found)
7126               (beginning-of-line)
7127               (set-window-start
7128                (get-buffer-window (current-buffer))
7129                (point))
7130               (forward-line 1)
7131               (set-window-point
7132                (get-buffer-window (current-buffer))
7133                (point))
7134               (set-buffer sum)
7135               (setq point (point)))
7136           ;; We didn't find it, so we go to the next article.
7137           (set-buffer sum)
7138           (setq found 'not)
7139           (while (eq found 'not)
7140             (if (not (if backward (gnus-summary-find-prev)
7141                        (gnus-summary-find-next)))
7142                 ;; No more articles.
7143                 (setq found t)
7144               ;; Select the next article and adjust point.
7145               (unless (gnus-summary-article-sparse-p
7146                        (gnus-summary-article-number))
7147                 (setq found nil)
7148                 (gnus-summary-select-article)
7149                 (set-buffer gnus-article-buffer)
7150                 (widen)
7151                 (goto-char (if backward (point-max) (point-min))))))))
7152       (gnus-message 7 ""))
7153     ;; Return whether we found the regexp.
7154     (when (eq found 'found)
7155       (goto-char point)
7156       (gnus-summary-show-thread)
7157       (gnus-summary-goto-subject gnus-current-article)
7158       (gnus-summary-position-point)
7159       t)))
7160
7161 (defun gnus-summary-find-matching (header regexp &optional backward unread
7162                                           not-case-fold)
7163   "Return a list of all articles that match REGEXP on HEADER.
7164 The search stars on the current article and goes forwards unless
7165 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7166 If UNREAD is non-nil, only unread articles will
7167 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7168 in the comparisons."
7169   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7170                 (gnus-data-find-list
7171                  (gnus-summary-article-number) (gnus-data-list backward))))
7172         (case-fold-search (not not-case-fold))
7173         articles d func)
7174     (if (consp header)
7175         (if (eq (car header) 'extra)
7176             (setq func
7177                   `(lambda (h)
7178                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7179                          "")))
7180           (error "%s is an invalid header" header))
7181       (unless (fboundp (intern (concat "mail-header-" header)))
7182         (error "%s is not a valid header" header))
7183       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7184     (while data
7185       (setq d (car data))
7186       (and (or (not unread)             ; We want all articles...
7187                (gnus-data-unread-p d))  ; Or just unreads.
7188            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7189            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7190            (push (gnus-data-number d) articles)) ; Success!
7191       (setq data (cdr data)))
7192     (nreverse articles)))
7193
7194 (defun gnus-summary-execute-command (header regexp command &optional backward)
7195   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7196 If HEADER is an empty string (or nil), the match is done on the entire
7197 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7198   (interactive
7199    (list (let ((completion-ignore-case t))
7200            (completing-read
7201             "Header name: "
7202             (mapcar (lambda (string) (list string))
7203                     '("Number" "Subject" "From" "Lines" "Date"
7204                       "Message-ID" "Xref" "References" "Body"))
7205             nil 'require-match))
7206          (read-string "Regexp: ")
7207          (read-key-sequence "Command: ")
7208          current-prefix-arg))
7209   (when (equal header "Body")
7210     (setq header ""))
7211   ;; Hidden thread subtrees must be searched as well.
7212   (gnus-summary-show-all-threads)
7213   ;; We don't want to change current point nor window configuration.
7214   (save-excursion
7215     (save-window-excursion
7216       (gnus-message 6 "Executing %s..." (key-description command))
7217       ;; We'd like to execute COMMAND interactively so as to give arguments.
7218       (gnus-execute header regexp
7219                     `(call-interactively ',(key-binding command))
7220                     backward)
7221       (gnus-message 6 "Executing %s...done" (key-description command)))))
7222
7223 (defun gnus-summary-beginning-of-article ()
7224   "Scroll the article back to the beginning."
7225   (interactive)
7226   (gnus-summary-select-article)
7227   (gnus-configure-windows 'article)
7228   (gnus-eval-in-buffer-window gnus-article-buffer
7229     (widen)
7230     (goto-char (point-min))
7231     (when gnus-page-broken
7232       (gnus-narrow-to-page))))
7233
7234 (defun gnus-summary-end-of-article ()
7235   "Scroll to the end of the article."
7236   (interactive)
7237   (gnus-summary-select-article)
7238   (gnus-configure-windows 'article)
7239   (gnus-eval-in-buffer-window gnus-article-buffer
7240     (widen)
7241     (goto-char (point-max))
7242     (recenter -3)
7243     (when gnus-page-broken
7244       (gnus-narrow-to-page))))
7245
7246 (defun gnus-summary-print-article (&optional filename n)
7247   "Generate and print a PostScript image of the N next (mail) articles.
7248
7249 If N is negative, print the N previous articles.  If N is nil and articles
7250 have been marked with the process mark, print these instead.
7251
7252 If the optional first argument FILENAME is nil, send the image to the
7253 printer.  If FILENAME is a string, save the PostScript image in a file with
7254 that name.  If FILENAME is a number, prompt the user for the name of the file
7255 to save in."
7256   (interactive (list (ps-print-preprint current-prefix-arg)
7257                      current-prefix-arg))
7258   (dolist (article (gnus-summary-work-articles n))
7259     (gnus-summary-select-article nil nil 'pseudo article)
7260     (gnus-eval-in-buffer-window gnus-article-buffer
7261       (let ((buffer (generate-new-buffer " *print*")))
7262         (unwind-protect
7263             (progn
7264               (copy-to-buffer buffer (point-min) (point-max))
7265               (set-buffer buffer)
7266               (gnus-article-delete-invisible-text)
7267               (let ((ps-left-header
7268                      (list
7269                       (concat "("
7270                               (mail-header-subject gnus-current-headers) ")")
7271                       (concat "("
7272                               (mail-header-from gnus-current-headers) ")")))
7273                     (ps-right-header
7274                      (list
7275                       "/pagenumberstring load"
7276                       (concat "("
7277                               (mail-header-date gnus-current-headers) ")"))))
7278                 (gnus-run-hooks 'gnus-ps-print-hook)
7279                 (save-excursion
7280                   (ps-print-buffer-with-faces filename))))
7281           (kill-buffer buffer))))))
7282
7283 (defun gnus-summary-show-article (&optional arg)
7284   "Force re-fetching of the current article.
7285 If ARG (the prefix) is a number, show the article with the charset
7286 defined in `gnus-summary-show-article-charset-alist', or the charset
7287 inputed.
7288 If ARG (the prefix) is non-nil and not a number, show the raw article
7289 without any article massaging functions being run."
7290   (interactive "P")
7291   (cond
7292    ((numberp arg)
7293     (let ((gnus-newsgroup-charset
7294            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7295                (read-coding-system "Charset: ")))
7296           (gnus-newsgroup-ignored-charsets 'gnus-all))
7297       (gnus-summary-select-article nil 'force)
7298       (let ((deps gnus-newsgroup-dependencies)
7299             head header)
7300         (save-excursion
7301           (set-buffer gnus-original-article-buffer)
7302           (save-restriction
7303             (message-narrow-to-head)
7304             (setq head (buffer-string)))
7305           (with-temp-buffer
7306             (insert (format "211 %d Article retrieved.\n"
7307                             (cdr gnus-article-current)))
7308             (insert head)
7309             (insert ".\n")
7310             (let ((nntp-server-buffer (current-buffer)))
7311               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7312         (gnus-data-set-header
7313          (gnus-data-find (cdr gnus-article-current))
7314          header)
7315         (gnus-summary-update-article-line
7316          (cdr gnus-article-current) header))))
7317    ((not arg)
7318     ;; Select the article the normal way.
7319     (gnus-summary-select-article nil 'force))
7320    (t
7321     ;; We have to require this here to make sure that the following
7322     ;; dynamic binding isn't shadowed by autoloading.
7323     (require 'gnus-async)
7324     (require 'gnus-art)
7325     ;; Bind the article treatment functions to nil.
7326     (let ((gnus-have-all-headers t)
7327           gnus-article-prepare-hook
7328           gnus-article-decode-hook
7329           gnus-display-mime-function
7330           gnus-break-pages)
7331       ;; Destroy any MIME parts.
7332       (when (gnus-buffer-live-p gnus-article-buffer)
7333         (save-excursion
7334           (set-buffer gnus-article-buffer)
7335           (mm-destroy-parts gnus-article-mime-handles)
7336           ;; Set it to nil for safety reason.
7337           (setq gnus-article-mime-handle-alist nil)
7338           (setq gnus-article-mime-handles nil)))
7339       (gnus-summary-select-article nil 'force))))
7340   (gnus-summary-goto-subject gnus-current-article)
7341   (gnus-summary-position-point))
7342
7343 (defun gnus-summary-verbose-headers (&optional arg)
7344   "Toggle permanent full header display.
7345 If ARG is a positive number, turn header display on.
7346 If ARG is a negative number, turn header display off."
7347   (interactive "P")
7348   (setq gnus-show-all-headers
7349         (cond ((or (not (numberp arg))
7350                    (zerop arg))
7351                (not gnus-show-all-headers))
7352               ((natnump arg)
7353                t)))
7354   (gnus-summary-show-article))
7355
7356 (defun gnus-summary-toggle-header (&optional arg)
7357   "Show the headers if they are hidden, or hide them if they are shown.
7358 If ARG is a positive number, show the entire header.
7359 If ARG is a negative number, hide the unwanted header lines."
7360   (interactive "P")
7361   (save-excursion
7362     (set-buffer gnus-article-buffer)
7363     (save-restriction
7364       (let* ((buffer-read-only nil)
7365              (inhibit-point-motion-hooks t)
7366              hidden e)
7367         (setq hidden
7368               (if (numberp arg)
7369                   (>= arg 0)
7370                 (save-restriction
7371                   (article-narrow-to-head)
7372                   (gnus-article-hidden-text-p 'headers))))
7373         (goto-char (point-min))
7374         (when (search-forward "\n\n" nil t)
7375           (delete-region (point-min) (1- (point))))
7376         (goto-char (point-min))
7377         (save-excursion
7378           (set-buffer gnus-original-article-buffer)
7379           (goto-char (point-min))
7380           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7381         (insert-buffer-substring gnus-original-article-buffer 1 e)
7382         (save-restriction
7383           (narrow-to-region (point-min) (point))
7384           (article-decode-encoded-words)
7385           (if  hidden
7386               (let ((gnus-treat-hide-headers nil)
7387                     (gnus-treat-hide-boring-headers nil))
7388                 (setq gnus-article-wash-types
7389                       (delq 'headers gnus-article-wash-types))
7390                 (gnus-treat-article 'head))
7391             (gnus-treat-article 'head)))
7392         (gnus-set-mode-line 'article)))))
7393
7394 (defun gnus-summary-show-all-headers ()
7395   "Make all header lines visible."
7396   (interactive)
7397   (gnus-article-show-all-headers))
7398
7399 (defun gnus-summary-caesar-message (&optional arg)
7400   "Caesar rotate the current article by 13.
7401 The numerical prefix specifies how many places to rotate each letter
7402 forward."
7403   (interactive "P")
7404   (gnus-summary-select-article)
7405   (let ((mail-header-separator ""))
7406     (gnus-eval-in-buffer-window gnus-article-buffer
7407       (save-restriction
7408         (widen)
7409         (let ((start (window-start))
7410               buffer-read-only)
7411           (message-caesar-buffer-body arg)
7412           (set-window-start (get-buffer-window (current-buffer)) start))))))
7413
7414 (defun gnus-summary-stop-page-breaking ()
7415   "Stop page breaking in the current article."
7416   (interactive)
7417   (gnus-summary-select-article)
7418   (gnus-eval-in-buffer-window gnus-article-buffer
7419     (widen)
7420     (when (gnus-visual-p 'page-marker)
7421       (let ((buffer-read-only nil))
7422         (gnus-remove-text-with-property 'gnus-prev)
7423         (gnus-remove-text-with-property 'gnus-next))
7424       (setq gnus-page-broken nil))))
7425
7426 (defun gnus-summary-move-article (&optional n to-newsgroup
7427                                             select-method action)
7428   "Move the current article to a different newsgroup.
7429 If N is a positive number, move the N next articles.
7430 If N is a negative number, move the N previous articles.
7431 If N is nil and any articles have been marked with the process mark,
7432 move those articles instead.
7433 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7434 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7435 re-spool using this method.
7436
7437 For this function to work, both the current newsgroup and the
7438 newsgroup that you want to move to have to support the `request-move'
7439 and `request-accept' functions.
7440
7441 ACTION can be either `move' (the default), `crosspost' or `copy'."
7442   (interactive "P")
7443   (unless action
7444     (setq action 'move))
7445   ;; Disable marking as read.
7446   (let (gnus-mark-article-hook)
7447     (save-window-excursion
7448       (gnus-summary-select-article)))
7449   ;; Check whether the source group supports the required functions.
7450   (cond ((and (eq action 'move)
7451               (not (gnus-check-backend-function
7452                     'request-move-article gnus-newsgroup-name)))
7453          (error "The current group does not support article moving"))
7454         ((and (eq action 'crosspost)
7455               (not (gnus-check-backend-function
7456                     'request-replace-article gnus-newsgroup-name)))
7457          (error "The current group does not support article editing")))
7458   (let ((articles (gnus-summary-work-articles n))
7459         (prefix (if (gnus-check-backend-function
7460                     'request-move-article gnus-newsgroup-name)
7461                     (gnus-group-real-prefix gnus-newsgroup-name)
7462                   ""))
7463         (names '((move "Move" "Moving")
7464                  (copy "Copy" "Copying")
7465                  (crosspost "Crosspost" "Crossposting")))
7466         (copy-buf (save-excursion
7467                     (nnheader-set-temp-buffer " *copy article*")))
7468         art-group to-method new-xref article to-groups)
7469     (unless (assq action names)
7470       (error "Unknown action %s" action))
7471     ;; Read the newsgroup name.
7472     (when (and (not to-newsgroup)
7473                (not select-method))
7474       (setq to-newsgroup
7475             (gnus-read-move-group-name
7476              (cadr (assq action names))
7477              (symbol-value (intern (format "gnus-current-%s-group" action)))
7478              articles prefix))
7479       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7480     (setq to-method (or select-method
7481                         (gnus-server-to-method
7482                          (gnus-group-method to-newsgroup))))
7483     ;; Check the method we are to move this article to...
7484     (unless (gnus-check-backend-function
7485              'request-accept-article (car to-method))
7486       (error "%s does not support article copying" (car to-method)))
7487     (unless (gnus-check-server to-method)
7488       (error "Can't open server %s" (car to-method)))
7489     (gnus-message 6 "%s to %s: %s..."
7490                   (caddr (assq action names))
7491                   (or (car select-method) to-newsgroup) articles)
7492     (while articles
7493       (setq article (pop articles))
7494       (setq
7495        art-group
7496        (cond
7497         ;; Move the article.
7498         ((eq action 'move)
7499          ;; Remove this article from future suppression.
7500          (gnus-dup-unsuppress-article article)
7501          (gnus-request-move-article
7502           article                       ; Article to move
7503           gnus-newsgroup-name           ; From newsgroup
7504           (nth 1 (gnus-find-method-for-group
7505                   gnus-newsgroup-name)) ; Server
7506           (list 'gnus-request-accept-article
7507                 to-newsgroup (list 'quote select-method)
7508                 (not articles) t)       ; Accept form
7509           (not articles)))              ; Only save nov last time
7510         ;; Copy the article.
7511         ((eq action 'copy)
7512          (save-excursion
7513            (set-buffer copy-buf)
7514            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7515              (gnus-request-accept-article
7516               to-newsgroup select-method (not articles) t))))
7517         ;; Crosspost the article.
7518         ((eq action 'crosspost)
7519          (let ((xref (message-tokenize-header
7520                       (mail-header-xref (gnus-summary-article-header article))
7521                       " ")))
7522            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7523                                   ":" article))
7524            (unless xref
7525              (setq xref (list (system-name))))
7526            (setq new-xref
7527                  (concat
7528                   (mapconcat 'identity
7529                              (delete "Xref:" (delete new-xref xref))
7530                              " ")
7531                   " " new-xref))
7532            (save-excursion
7533              (set-buffer copy-buf)
7534              ;; First put the article in the destination group.
7535              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7536              (when (consp (setq art-group
7537                                 (gnus-request-accept-article
7538                                  to-newsgroup select-method (not articles))))
7539                (setq new-xref (concat new-xref " " (car art-group)
7540                                       ":" (cdr art-group)))
7541                ;; Now we have the new Xrefs header, so we insert
7542                ;; it and replace the new article.
7543                (nnheader-replace-header "Xref" new-xref)
7544                (gnus-request-replace-article
7545                 (cdr art-group) to-newsgroup (current-buffer))
7546                art-group))))))
7547       (cond
7548        ((not art-group)
7549         (gnus-message 1 "Couldn't %s article %s: %s"
7550                       (cadr (assq action names)) article
7551                       (nnheader-get-report (car to-method))))
7552        ((eq art-group 'junk)
7553         (when (eq action 'move)
7554           (gnus-summary-mark-article article gnus-canceled-mark)
7555           (gnus-message 4 "Deleted article %s" article)))
7556        (t
7557         (let* ((pto-group (gnus-group-prefixed-name
7558                            (car art-group) to-method))
7559                (entry
7560                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7561                (info (nth 2 entry))
7562                (to-group (gnus-info-group info))
7563                to-marks)
7564           ;; Update the group that has been moved to.
7565           (when (and info
7566                      (memq action '(move copy)))
7567             (unless (member to-group to-groups)
7568               (push to-group to-groups))
7569
7570             (unless (memq article gnus-newsgroup-unreads)
7571               (push 'read to-marks)
7572               (gnus-info-set-read
7573                info (gnus-add-to-range (gnus-info-read info)
7574                                        (list (cdr art-group)))))
7575
7576             ;; See whether the article is to be put in the cache.
7577             (let ((marks gnus-article-mark-lists)
7578                   (to-article (cdr art-group)))
7579
7580               ;; Enter the article into the cache in the new group,
7581               ;; if that is required.
7582               (when gnus-use-cache
7583                 (gnus-cache-possibly-enter-article
7584                  to-group to-article
7585                  (memq article gnus-newsgroup-marked)
7586                  (memq article gnus-newsgroup-dormant)
7587                  (memq article gnus-newsgroup-unreads)))
7588
7589               (when gnus-preserve-marks
7590                 ;; Copy any marks over to the new group.
7591                 (when (and (equal to-group gnus-newsgroup-name)
7592                            (not (memq article gnus-newsgroup-unreads)))
7593                   ;; Mark this article as read in this group.
7594                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7595                   (setcdr (gnus-active to-group) to-article)
7596                   (setcdr gnus-newsgroup-active to-article))
7597
7598                 (while marks
7599                   (when (memq article (symbol-value
7600                                        (intern (format "gnus-newsgroup-%s"
7601                                                        (caar marks)))))
7602                     (push (cdar marks) to-marks)
7603                     ;; If the other group is the same as this group,
7604                     ;; then we have to add the mark to the list.
7605                     (when (equal to-group gnus-newsgroup-name)
7606                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7607                            (cons to-article
7608                                  (symbol-value
7609                                   (intern (format "gnus-newsgroup-%s"
7610                                                   (caar marks)))))))
7611                     ;; Copy the marks to other group.
7612                     (gnus-add-marked-articles
7613                      to-group (cdar marks) (list to-article) info))
7614                   (setq marks (cdr marks)))
7615
7616                 (gnus-request-set-mark to-group (list (list (list to-article)
7617                                                             'set
7618                                                             to-marks))))
7619
7620               (gnus-dribble-enter
7621                (concat "(gnus-group-set-info '"
7622                        (gnus-prin1-to-string (gnus-get-info to-group))
7623                        ")"))))
7624
7625           ;; Update the Xref header in this article to point to
7626           ;; the new crossposted article we have just created.
7627           (when (eq action 'crosspost)
7628             (save-excursion
7629               (set-buffer copy-buf)
7630               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7631               (nnheader-replace-header "Xref" new-xref)
7632               (gnus-request-replace-article
7633                article gnus-newsgroup-name (current-buffer)))))
7634
7635         ;;;!!!Why is this necessary?
7636         (set-buffer gnus-summary-buffer)
7637
7638         (gnus-summary-goto-subject article)
7639         (when (eq action 'move)
7640           (gnus-summary-mark-article article gnus-canceled-mark))))
7641       (gnus-summary-remove-process-mark article))
7642     ;; Re-activate all groups that have been moved to.
7643     (while to-groups
7644       (save-excursion
7645         (set-buffer gnus-group-buffer)
7646         (when (gnus-group-goto-group (car to-groups) t)
7647           (gnus-group-get-new-news-this-group 1 t))
7648         (pop to-groups)))
7649
7650     (gnus-kill-buffer copy-buf)
7651     (gnus-summary-position-point)
7652     (gnus-set-mode-line 'summary)))
7653
7654 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7655   "Move the current article to a different newsgroup.
7656 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7657 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7658 re-spool using this method."
7659   (interactive "P")
7660   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7661
7662 (defun gnus-summary-crosspost-article (&optional n)
7663   "Crosspost the current article to some other group."
7664   (interactive "P")
7665   (gnus-summary-move-article n nil nil 'crosspost))
7666
7667 (defcustom gnus-summary-respool-default-method nil
7668   "Default method for respooling an article.
7669 If nil, use to the current newsgroup method."
7670   :type '(choice (gnus-select-method :value (nnml ""))
7671                  (const nil))
7672   :group 'gnus-summary-mail)
7673
7674 (defun gnus-summary-respool-article (&optional n method)
7675   "Respool the current article.
7676 The article will be squeezed through the mail spooling process again,
7677 which means that it will be put in some mail newsgroup or other
7678 depending on `nnmail-split-methods'.
7679 If N is a positive number, respool the N next articles.
7680 If N is a negative number, respool the N previous articles.
7681 If N is nil and any articles have been marked with the process mark,
7682 respool those articles instead.
7683
7684 Respooling can be done both from mail groups and \"real\" newsgroups.
7685 In the former case, the articles in question will be moved from the
7686 current group into whatever groups they are destined to.  In the
7687 latter case, they will be copied into the relevant groups."
7688   (interactive
7689    (list current-prefix-arg
7690          (let* ((methods (gnus-methods-using 'respool))
7691                 (methname
7692                  (symbol-name (or gnus-summary-respool-default-method
7693                                   (car (gnus-find-method-for-group
7694                                         gnus-newsgroup-name)))))
7695                 (method
7696                  (gnus-completing-read
7697                   methname "What backend do you want to use when respooling?"
7698                   methods nil t nil 'gnus-mail-method-history))
7699                 ms)
7700            (cond
7701             ((zerop (length (setq ms (gnus-servers-using-backend
7702                                       (intern method)))))
7703              (list (intern method) ""))
7704             ((= 1 (length ms))
7705              (car ms))
7706             (t
7707              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7708                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7709                            ms-alist))))))))
7710   (unless method
7711     (error "No method given for respooling"))
7712   (if (assoc (symbol-name
7713               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7714              (gnus-methods-using 'respool))
7715       (gnus-summary-move-article n nil method)
7716     (gnus-summary-copy-article n nil method)))
7717
7718 (defun gnus-summary-import-article (file)
7719   "Import an arbitrary file into a mail newsgroup."
7720   (interactive "fImport file: ")
7721   (let ((group gnus-newsgroup-name)
7722         (now (current-time))
7723         atts lines)
7724     (unless (gnus-check-backend-function 'request-accept-article group)
7725       (error "%s does not support article importing" group))
7726     (or (file-readable-p file)
7727         (not (file-regular-p file))
7728         (error "Can't read %s" file))
7729     (save-excursion
7730       (set-buffer (gnus-get-buffer-create " *import file*"))
7731       (erase-buffer)
7732       (nnheader-insert-file-contents file)
7733       (goto-char (point-min))
7734       (unless (nnheader-article-p)
7735         ;; This doesn't look like an article, so we fudge some headers.
7736         (setq atts (file-attributes file)
7737               lines (count-lines (point-min) (point-max)))
7738         (insert "From: " (read-string "From: ") "\n"
7739                 "Subject: " (read-string "Subject: ") "\n"
7740                 "Date: " (message-make-date (nth 5 atts))
7741                 "\n"
7742                 "Message-ID: " (message-make-message-id) "\n"
7743                 "Lines: " (int-to-string lines) "\n"
7744                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7745       (gnus-request-accept-article group nil t)
7746       (kill-buffer (current-buffer)))))
7747
7748 (defun gnus-summary-article-posted-p ()
7749   "Say whether the current (mail) article is available from news as well.
7750 This will be the case if the article has both been mailed and posted."
7751   (interactive)
7752   (let ((id (mail-header-references (gnus-summary-article-header)))
7753         (gnus-override-method (car (gnus-refer-article-methods))))
7754     (if (gnus-request-head id "")
7755         (gnus-message 2 "The current message was found on %s"
7756                       gnus-override-method)
7757       (gnus-message 2 "The current message couldn't be found on %s"
7758                     gnus-override-method)
7759       nil)))
7760
7761 (defun gnus-summary-expire-articles (&optional now)
7762   "Expire all articles that are marked as expirable in the current group."
7763   (interactive)
7764   (when (gnus-check-backend-function
7765          'request-expire-articles gnus-newsgroup-name)
7766     ;; This backend supports expiry.
7767     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7768            (expirable (if total
7769                           (progn
7770                             ;; We need to update the info for
7771                             ;; this group for `gnus-list-of-read-articles'
7772                             ;; to give us the right answer.
7773                             (gnus-run-hooks 'gnus-exit-group-hook)
7774                             (gnus-summary-update-info)
7775                             (gnus-list-of-read-articles gnus-newsgroup-name))
7776                         (setq gnus-newsgroup-expirable
7777                               (sort gnus-newsgroup-expirable '<))))
7778            (expiry-wait (if now 'immediate
7779                           (gnus-group-find-parameter
7780                            gnus-newsgroup-name 'expiry-wait)))
7781            (nnmail-expiry-target
7782             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7783                 nnmail-expiry-target))
7784            es)
7785       (when expirable
7786         ;; There are expirable articles in this group, so we run them
7787         ;; through the expiry process.
7788         (gnus-message 6 "Expiring articles...")
7789         (unless (gnus-check-group gnus-newsgroup-name)
7790           (error "Can't open server for %s" gnus-newsgroup-name))
7791         ;; The list of articles that weren't expired is returned.
7792         (save-excursion
7793           (if expiry-wait
7794               (let ((nnmail-expiry-wait-function nil)
7795                     (nnmail-expiry-wait expiry-wait))
7796                 (setq es (gnus-request-expire-articles
7797                           expirable gnus-newsgroup-name)))
7798             (setq es (gnus-request-expire-articles
7799                       expirable gnus-newsgroup-name)))
7800           (unless total
7801             (setq gnus-newsgroup-expirable es))
7802           ;; We go through the old list of expirable, and mark all
7803           ;; really expired articles as nonexistent.
7804           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
7805             (let ((gnus-use-cache nil))
7806               (while expirable
7807                 (unless (memq (car expirable) es)
7808                   (when (gnus-data-find (car expirable))
7809                     (gnus-summary-mark-article
7810                      (car expirable) gnus-canceled-mark)))
7811                 (setq expirable (cdr expirable))))))
7812         (gnus-message 6 "Expiring articles...done")))))
7813
7814 (defun gnus-summary-expire-articles-now ()
7815   "Expunge all expirable articles in the current group.
7816 This means that *all* articles that are marked as expirable will be
7817 deleted forever, right now."
7818   (interactive)
7819   (or gnus-expert-user
7820       (gnus-yes-or-no-p
7821        "Are you really, really, really sure you want to delete all these messages? ")
7822       (error "Phew!"))
7823   (gnus-summary-expire-articles t))
7824
7825 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7826 (defun gnus-summary-delete-article (&optional n)
7827   "Delete the N next (mail) articles.
7828 This command actually deletes articles.  This is not a marking
7829 command.  The article will disappear forever from your life, never to
7830 return.
7831 If N is negative, delete backwards.
7832 If N is nil and articles have been marked with the process mark,
7833 delete these instead."
7834   (interactive "P")
7835   (unless (gnus-check-backend-function 'request-expire-articles
7836                                        gnus-newsgroup-name)
7837     (error "The current newsgroup does not support article deletion"))
7838   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7839     (error "Couldn't open server"))
7840   ;; Compute the list of articles to delete.
7841   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7842         not-deleted)
7843     (if (and gnus-novice-user
7844              (not (gnus-yes-or-no-p
7845                    (format "Do you really want to delete %s forever? "
7846                            (if (> (length articles) 1)
7847                                (format "these %s articles" (length articles))
7848                              "this article")))))
7849         ()
7850       ;; Delete the articles.
7851       (setq not-deleted (gnus-request-expire-articles
7852                          articles gnus-newsgroup-name 'force))
7853       (while articles
7854         (gnus-summary-remove-process-mark (car articles))
7855         ;; The backend might not have been able to delete the article
7856         ;; after all.
7857         (unless (memq (car articles) not-deleted)
7858           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7859         (setq articles (cdr articles)))
7860       (when not-deleted
7861         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7862     (gnus-summary-position-point)
7863     (gnus-set-mode-line 'summary)
7864     not-deleted))
7865
7866 (defun gnus-summary-edit-article (&optional arg)
7867   "Edit the current article.
7868 This will have permanent effect only in mail groups.
7869 If ARG is nil, edit the decoded articles.
7870 If ARG is 1, edit the raw articles. 
7871 If ARG is 2, edit the raw articles even in read-only groups.
7872 Otherwise, allow editing of articles even in read-only
7873 groups."
7874   (interactive "P")
7875   (let (force raw)
7876     (cond 
7877      ((null arg))
7878      ((eq arg 1) (setq raw t))
7879      ((eq arg 2) (setq raw t
7880                        force t))
7881      (t (setq force t)))
7882     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
7883         (error "Can't edit the raw article in group nndraft:drafts."))
7884     (save-excursion
7885       (set-buffer gnus-summary-buffer)
7886       (let ((mail-parse-charset gnus-newsgroup-charset)
7887             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7888         (gnus-set-global-variables)
7889         (when (and (not force)
7890                    (gnus-group-read-only-p))
7891           (error "The current newsgroup does not support article editing"))
7892         (gnus-summary-show-article t)
7893         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
7894           (with-current-buffer gnus-article-buffer
7895             (mm-enable-multibyte-mule4)))
7896         (if (equal gnus-newsgroup-name "nndraft:drafts")
7897             (setq raw t))
7898         (gnus-article-edit-article
7899          (if raw 'ignore 
7900            #'(lambda () 
7901                (let ((mbl mml-buffer-list))
7902                  (setq mml-buffer-list nil)
7903                  (mime-to-mml)
7904                  (make-local-hook 'kill-buffer-hook)
7905                  (let ((mml-buffer-list mml-buffer-list))
7906                    (setq mml-buffer-list mbl)
7907                    (make-local-variable 'mml-buffer-list))
7908                  (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
7909          `(lambda (no-highlight)
7910             (let ((mail-parse-charset ',gnus-newsgroup-charset)
7911                   (message-options message-options)
7912                   (message-options-set-recipient)
7913                   (mail-parse-ignored-charsets 
7914                    ',gnus-newsgroup-ignored-charsets))
7915               ,(if (not raw) '(progn 
7916                                 (mml-to-mime)
7917                                 (mml-destroy-buffers)
7918                                 (remove-hook 'kill-buffer-hook 
7919                                              'mml-destroy-buffers t)
7920                                 (kill-local-variable 'mml-buffer-list)))
7921               (gnus-summary-edit-article-done
7922                ,(or (mail-header-references gnus-current-headers) "")
7923                ,(gnus-group-read-only-p) 
7924                ,gnus-summary-buffer no-highlight))))))))
7925
7926 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7927
7928 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7929                                                  no-highlight)
7930   "Make edits to the current article permanent."
7931   (interactive)
7932   ;; Replace the article.
7933   (let ((buf (current-buffer)))
7934     (with-temp-buffer
7935       (insert-buffer-substring buf)
7936       (if (and (not read-only)
7937                (not (gnus-request-replace-article
7938                      (cdr gnus-article-current) (car gnus-article-current)
7939                      (current-buffer) t)))
7940           (error "Couldn't replace article")
7941         ;; Update the summary buffer.
7942         (if (and references
7943                  (equal (message-tokenize-header references " ")
7944                         (message-tokenize-header
7945                          (or (message-fetch-field "references") "") " ")))
7946             ;; We only have to update this line.
7947             (save-excursion
7948               (save-restriction
7949                 (message-narrow-to-head)
7950                 (let ((head (buffer-string))
7951                       header)
7952                   (with-temp-buffer
7953                     (insert (format "211 %d Article retrieved.\n"
7954                                     (cdr gnus-article-current)))
7955                     (insert head)
7956                     (insert ".\n")
7957                     (let ((nntp-server-buffer (current-buffer)))
7958                       (setq header (car (gnus-get-newsgroup-headers
7959                                          (save-excursion
7960                                            (set-buffer gnus-summary-buffer)
7961                                            gnus-newsgroup-dependencies)
7962                                          t))))
7963                     (save-excursion
7964                       (set-buffer gnus-summary-buffer)
7965                       (gnus-data-set-header
7966                        (gnus-data-find (cdr gnus-article-current))
7967                        header)
7968                       (gnus-summary-update-article-line
7969                        (cdr gnus-article-current) header))))))
7970           ;; Update threads.
7971           (set-buffer (or buffer gnus-summary-buffer))
7972           (gnus-summary-update-article (cdr gnus-article-current)))
7973         ;; Prettify the article buffer again.
7974         (unless no-highlight
7975           (save-excursion
7976             (set-buffer gnus-article-buffer)
7977             ;;;!!! Fix this -- article should be rehighlighted.
7978             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7979             (set-buffer gnus-original-article-buffer)
7980             (gnus-request-article
7981              (cdr gnus-article-current)
7982              (car gnus-article-current) (current-buffer))))
7983         ;; Prettify the summary buffer line.
7984         (when (gnus-visual-p 'summary-highlight 'highlight)
7985           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7986
7987 (defun gnus-summary-edit-wash (key)
7988   "Perform editing command KEY in the article buffer."
7989   (interactive
7990    (list
7991     (progn
7992       (message "%s" (concat (this-command-keys) "- "))
7993       (read-char))))
7994   (message "")
7995   (gnus-summary-edit-article)
7996   (execute-kbd-macro (concat (this-command-keys) key))
7997   (gnus-article-edit-done))
7998
7999 ;;; Respooling
8000
8001 (defun gnus-summary-respool-query (&optional silent trace)
8002   "Query where the respool algorithm would put this article."
8003   (interactive)
8004   (let (gnus-mark-article-hook)
8005     (gnus-summary-select-article)
8006     (save-excursion
8007       (set-buffer gnus-original-article-buffer)
8008       (save-restriction
8009         (message-narrow-to-head)
8010         (let ((groups (nnmail-article-group 'identity trace)))
8011           (unless silent
8012             (if groups
8013                 (message "This message would go to %s"
8014                          (mapconcat 'car groups ", "))
8015               (message "This message would go to no groups"))
8016             groups))))))
8017
8018 (defun gnus-summary-respool-trace ()
8019   "Trace where the respool algorithm would put this article.
8020 Display a buffer showing all fancy splitting patterns which matched."
8021   (interactive)
8022   (gnus-summary-respool-query nil t))
8023
8024 ;; Summary marking commands.
8025
8026 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8027   "Mark articles which has the same subject as read, and then select the next.
8028 If UNMARK is positive, remove any kind of mark.
8029 If UNMARK is negative, tick articles."
8030   (interactive "P")
8031   (when unmark
8032     (setq unmark (prefix-numeric-value unmark)))
8033   (let ((count
8034          (gnus-summary-mark-same-subject
8035           (gnus-summary-article-subject) unmark)))
8036     ;; Select next unread article.  If auto-select-same mode, should
8037     ;; select the first unread article.
8038     (gnus-summary-next-article t (and gnus-auto-select-same
8039                                       (gnus-summary-article-subject)))
8040     (gnus-message 7 "%d article%s marked as %s"
8041                   count (if (= count 1) " is" "s are")
8042                   (if unmark "unread" "read"))))
8043
8044 (defun gnus-summary-kill-same-subject (&optional unmark)
8045   "Mark articles which has the same subject as read.
8046 If UNMARK is positive, remove any kind of mark.
8047 If UNMARK is negative, tick articles."
8048   (interactive "P")
8049   (when unmark
8050     (setq unmark (prefix-numeric-value unmark)))
8051   (let ((count
8052          (gnus-summary-mark-same-subject
8053           (gnus-summary-article-subject) unmark)))
8054     ;; If marked as read, go to next unread subject.
8055     (when (null unmark)
8056       ;; Go to next unread subject.
8057       (gnus-summary-next-subject 1 t))
8058     (gnus-message 7 "%d articles are marked as %s"
8059                   count (if unmark "unread" "read"))))
8060
8061 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8062   "Mark articles with same SUBJECT as read, and return marked number.
8063 If optional argument UNMARK is positive, remove any kinds of marks.
8064 If optional argument UNMARK is negative, mark articles as unread instead."
8065   (let ((count 1))
8066     (save-excursion
8067       (cond
8068        ((null unmark)                   ; Mark as read.
8069         (while (and
8070                 (progn
8071                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8072                   (gnus-summary-show-thread) t)
8073                 (gnus-summary-find-subject subject))
8074           (setq count (1+ count))))
8075        ((> unmark 0)                    ; Tick.
8076         (while (and
8077                 (progn
8078                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8079                   (gnus-summary-show-thread) t)
8080                 (gnus-summary-find-subject subject))
8081           (setq count (1+ count))))
8082        (t                               ; Mark as unread.
8083         (while (and
8084                 (progn
8085                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8086                   (gnus-summary-show-thread) t)
8087                 (gnus-summary-find-subject subject))
8088           (setq count (1+ count)))))
8089       (gnus-set-mode-line 'summary)
8090       ;; Return the number of marked articles.
8091       count)))
8092
8093 (defun gnus-summary-mark-as-processable (n &optional unmark)
8094   "Set the process mark on the next N articles.
8095 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8096 the process mark instead.  The difference between N and the actual
8097 number of articles marked is returned."
8098   (interactive "p")
8099   (let ((backward (< n 0))
8100         (n (abs n)))
8101     (while (and
8102             (> n 0)
8103             (if unmark
8104                 (gnus-summary-remove-process-mark
8105                  (gnus-summary-article-number))
8106               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8107             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8108       (setq n (1- n)))
8109     (when (/= 0 n)
8110       (gnus-message 7 "No more articles"))
8111     (gnus-summary-recenter)
8112     (gnus-summary-position-point)
8113     n))
8114
8115 (defun gnus-summary-unmark-as-processable (n)
8116   "Remove the process mark from the next N articles.
8117 If N is negative, unmark backward instead.  The difference between N and
8118 the actual number of articles unmarked is returned."
8119   (interactive "p")
8120   (gnus-summary-mark-as-processable n t))
8121
8122 (defun gnus-summary-unmark-all-processable ()
8123   "Remove the process mark from all articles."
8124   (interactive)
8125   (save-excursion
8126     (while gnus-newsgroup-processable
8127       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8128   (gnus-summary-position-point))
8129
8130 (defun gnus-summary-mark-as-expirable (n)
8131   "Mark N articles forward as expirable.
8132 If N is negative, mark backward instead.  The difference between N and
8133 the actual number of articles marked is returned."
8134   (interactive "p")
8135   (gnus-summary-mark-forward n gnus-expirable-mark))
8136
8137 (defun gnus-summary-mark-article-as-replied (article)
8138   "Mark ARTICLE replied and update the summary line."
8139   (push article gnus-newsgroup-replied)
8140   (let ((buffer-read-only nil))
8141     (when (gnus-summary-goto-subject article nil t)
8142       (gnus-summary-update-secondary-mark article))))
8143
8144 (defun gnus-summary-set-bookmark (article)
8145   "Set a bookmark in current article."
8146   (interactive (list (gnus-summary-article-number)))
8147   (when (or (not (get-buffer gnus-article-buffer))
8148             (not gnus-current-article)
8149             (not gnus-article-current)
8150             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8151     (error "No current article selected"))
8152   ;; Remove old bookmark, if one exists.
8153   (let ((old (assq article gnus-newsgroup-bookmarks)))
8154     (when old
8155       (setq gnus-newsgroup-bookmarks
8156             (delq old gnus-newsgroup-bookmarks))))
8157   ;; Set the new bookmark, which is on the form
8158   ;; (article-number . line-number-in-body).
8159   (push
8160    (cons article
8161          (save-excursion
8162            (set-buffer gnus-article-buffer)
8163            (count-lines
8164             (min (point)
8165                  (save-excursion
8166                    (goto-char (point-min))
8167                    (search-forward "\n\n" nil t)
8168                    (point)))
8169             (point))))
8170    gnus-newsgroup-bookmarks)
8171   (gnus-message 6 "A bookmark has been added to the current article."))
8172
8173 (defun gnus-summary-remove-bookmark (article)
8174   "Remove the bookmark from the current article."
8175   (interactive (list (gnus-summary-article-number)))
8176   ;; Remove old bookmark, if one exists.
8177   (let ((old (assq article gnus-newsgroup-bookmarks)))
8178     (if old
8179         (progn
8180           (setq gnus-newsgroup-bookmarks
8181                 (delq old gnus-newsgroup-bookmarks))
8182           (gnus-message 6 "Removed bookmark."))
8183       (gnus-message 6 "No bookmark in current article."))))
8184
8185 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8186 (defun gnus-summary-mark-as-dormant (n)
8187   "Mark N articles forward as dormant.
8188 If N is negative, mark backward instead.  The difference between N and
8189 the actual number of articles marked is returned."
8190   (interactive "p")
8191   (gnus-summary-mark-forward n gnus-dormant-mark))
8192
8193 (defun gnus-summary-set-process-mark (article)
8194   "Set the process mark on ARTICLE and update the summary line."
8195   (setq gnus-newsgroup-processable
8196         (cons article
8197               (delq article gnus-newsgroup-processable)))
8198   (when (gnus-summary-goto-subject article)
8199     (gnus-summary-show-thread)
8200     (gnus-summary-goto-subject article)
8201     (gnus-summary-update-secondary-mark article)))
8202
8203 (defun gnus-summary-remove-process-mark (article)
8204   "Remove the process mark from ARTICLE and update the summary line."
8205   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8206   (when (gnus-summary-goto-subject article)
8207     (gnus-summary-show-thread)
8208     (gnus-summary-goto-subject article)
8209     (gnus-summary-update-secondary-mark article)))
8210
8211 (defun gnus-summary-set-saved-mark (article)
8212   "Set the process mark on ARTICLE and update the summary line."
8213   (push article gnus-newsgroup-saved)
8214   (when (gnus-summary-goto-subject article)
8215     (gnus-summary-update-secondary-mark article)))
8216
8217 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8218   "Mark N articles as read forwards.
8219 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8220 The difference between N and the actual number of articles marked is
8221 returned.
8222 Iff NO-EXPIRE, auto-expiry will be inhibited."
8223   (interactive "p")
8224   (gnus-summary-show-thread)
8225   (let ((backward (< n 0))
8226         (gnus-summary-goto-unread
8227          (and gnus-summary-goto-unread
8228               (not (eq gnus-summary-goto-unread 'never))
8229               (not (memq mark (list gnus-unread-mark
8230                                     gnus-ticked-mark gnus-dormant-mark)))))
8231         (n (abs n))
8232         (mark (or mark gnus-del-mark)))
8233     (while (and (> n 0)
8234                 (gnus-summary-mark-article nil mark no-expire)
8235                 (zerop (gnus-summary-next-subject
8236                         (if backward -1 1)
8237                         (and gnus-summary-goto-unread
8238                              (not (eq gnus-summary-goto-unread 'never)))
8239                         t)))
8240       (setq n (1- n)))
8241     (when (/= 0 n)
8242       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8243     (gnus-summary-recenter)
8244     (gnus-summary-position-point)
8245     (gnus-set-mode-line 'summary)
8246     n))
8247
8248 (defun gnus-summary-mark-article-as-read (mark)
8249   "Mark the current article quickly as read with MARK."
8250   (let ((article (gnus-summary-article-number)))
8251     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8252     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8253     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8254     (push (cons article mark) gnus-newsgroup-reads)
8255     ;; Possibly remove from cache, if that is used.
8256     (when gnus-use-cache
8257       (gnus-cache-enter-remove-article article))
8258     ;; Allow the backend to change the mark.
8259     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8260     ;; Check for auto-expiry.
8261     (when (and gnus-newsgroup-auto-expire
8262                (memq mark gnus-auto-expirable-marks))
8263       (setq mark gnus-expirable-mark)
8264       ;; Let the backend know about the mark change.
8265       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8266       (push article gnus-newsgroup-expirable))
8267     ;; Set the mark in the buffer.
8268     (gnus-summary-update-mark mark 'unread)
8269     t))
8270
8271 (defun gnus-summary-mark-article-as-unread (mark)
8272   "Mark the current article quickly as unread with MARK."
8273   (let* ((article (gnus-summary-article-number))
8274          (old-mark (gnus-summary-article-mark article)))
8275     ;; Allow the backend to change the mark.
8276     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8277     (if (eq mark old-mark)
8278         t
8279       (if (<= article 0)
8280           (progn
8281             (gnus-error 1 "Can't mark negative article numbers")
8282             nil)
8283         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8284         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8285         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8286         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8287         (cond ((= mark gnus-ticked-mark)
8288                (push article gnus-newsgroup-marked))
8289               ((= mark gnus-dormant-mark)
8290                (push article gnus-newsgroup-dormant))
8291               (t
8292                (push article gnus-newsgroup-unreads)))
8293         (gnus-pull article gnus-newsgroup-reads)
8294
8295         ;; See whether the article is to be put in the cache.
8296         (and gnus-use-cache
8297              (vectorp (gnus-summary-article-header article))
8298              (save-excursion
8299                (gnus-cache-possibly-enter-article
8300                 gnus-newsgroup-name article
8301                 (= mark gnus-ticked-mark)
8302                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8303
8304         ;; Fix the mark.
8305         (gnus-summary-update-mark mark 'unread)
8306         t))))
8307
8308 (defun gnus-summary-mark-article (&optional article mark no-expire)
8309   "Mark ARTICLE with MARK.  MARK can be any character.
8310 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8311 `??' (dormant) and `?E' (expirable).
8312 If MARK is nil, then the default character `?r' is used.
8313 If ARTICLE is nil, then the article on the current line will be
8314 marked.
8315 Iff NO-EXPIRE, auto-expiry will be inhibited."
8316   ;; The mark might be a string.
8317   (when (stringp mark)
8318     (setq mark (aref mark 0)))
8319   ;; If no mark is given, then we check auto-expiring.
8320   (when (null mark)
8321     (setq mark gnus-del-mark))
8322   (when (and (not no-expire)
8323              gnus-newsgroup-auto-expire
8324              (memq mark gnus-auto-expirable-marks))
8325     (setq mark gnus-expirable-mark))
8326   (let ((article (or article (gnus-summary-article-number)))
8327         (old-mark (gnus-summary-article-mark article)))
8328     ;; Allow the backend to change the mark.
8329     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8330     (if (eq mark old-mark)
8331         t
8332       (unless article
8333         (error "No article on current line"))
8334       (if (not (if (or (= mark gnus-unread-mark)
8335                        (= mark gnus-ticked-mark)
8336                        (= mark gnus-dormant-mark))
8337                    (gnus-mark-article-as-unread article mark)
8338                  (gnus-mark-article-as-read article mark)))
8339           t
8340         ;; See whether the article is to be put in the cache.
8341         (and gnus-use-cache
8342              (not (= mark gnus-canceled-mark))
8343              (vectorp (gnus-summary-article-header article))
8344              (save-excursion
8345                (gnus-cache-possibly-enter-article
8346                 gnus-newsgroup-name article
8347                 (= mark gnus-ticked-mark)
8348                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8349
8350         (when (gnus-summary-goto-subject article nil t)
8351           (let ((buffer-read-only nil))
8352             (gnus-summary-show-thread)
8353             ;; Fix the mark.
8354             (gnus-summary-update-mark mark 'unread)
8355             t))))))
8356
8357 (defun gnus-summary-update-secondary-mark (article)
8358   "Update the secondary (read, process, cache) mark."
8359   (gnus-summary-update-mark
8360    (cond ((memq article gnus-newsgroup-processable)
8361           gnus-process-mark)
8362          ((memq article gnus-newsgroup-cached)
8363           gnus-cached-mark)
8364          ((memq article gnus-newsgroup-replied)
8365           gnus-replied-mark)
8366          ((memq article gnus-newsgroup-saved)
8367           gnus-saved-mark)
8368          (t gnus-unread-mark))
8369    'replied)
8370   (when (gnus-visual-p 'summary-highlight 'highlight)
8371     (gnus-run-hooks 'gnus-summary-update-hook))
8372   t)
8373
8374 (defun gnus-summary-update-mark (mark type)
8375   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8376         (buffer-read-only nil))
8377     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8378     (when forward
8379       (when (looking-at "\r")
8380         (incf forward))
8381       (when (<= (+ forward (point)) (point-max))
8382         ;; Go to the right position on the line.
8383         (goto-char (+ forward (point)))
8384         ;; Replace the old mark with the new mark.
8385         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8386         ;; Optionally update the marks by some user rule.
8387         (when (eq type 'unread)
8388           (gnus-data-set-mark
8389            (gnus-data-find (gnus-summary-article-number)) mark)
8390           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8391
8392 (defun gnus-mark-article-as-read (article &optional mark)
8393   "Enter ARTICLE in the pertinent lists and remove it from others."
8394   ;; Make the article expirable.
8395   (let ((mark (or mark gnus-del-mark)))
8396     (if (= mark gnus-expirable-mark)
8397         (push article gnus-newsgroup-expirable)
8398       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8399     ;; Remove from unread and marked lists.
8400     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8401     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8402     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8403     (push (cons article mark) gnus-newsgroup-reads)
8404     ;; Possibly remove from cache, if that is used.
8405     (when gnus-use-cache
8406       (gnus-cache-enter-remove-article article))
8407     t))
8408
8409 (defun gnus-mark-article-as-unread (article &optional mark)
8410   "Enter ARTICLE in the pertinent lists and remove it from others."
8411   (let ((mark (or mark gnus-ticked-mark)))
8412     (if (<= article 0)
8413         (progn
8414           (gnus-error 1 "Can't mark negative article numbers")
8415           nil)
8416       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8417             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8418             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8419             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8420
8421       ;; Unsuppress duplicates?
8422       (when gnus-suppress-duplicates
8423         (gnus-dup-unsuppress-article article))
8424
8425       (cond ((= mark gnus-ticked-mark)
8426              (push article gnus-newsgroup-marked))
8427             ((= mark gnus-dormant-mark)
8428              (push article gnus-newsgroup-dormant))
8429             (t
8430              (push article gnus-newsgroup-unreads)))
8431       (gnus-pull article gnus-newsgroup-reads)
8432       t)))
8433
8434 (defalias 'gnus-summary-mark-as-unread-forward
8435   'gnus-summary-tick-article-forward)
8436 (make-obsolete 'gnus-summary-mark-as-unread-forward
8437                'gnus-summary-tick-article-forward)
8438 (defun gnus-summary-tick-article-forward (n)
8439   "Tick N articles forwards.
8440 If N is negative, tick backwards instead.
8441 The difference between N and the number of articles ticked is returned."
8442   (interactive "p")
8443   (gnus-summary-mark-forward n gnus-ticked-mark))
8444
8445 (defalias 'gnus-summary-mark-as-unread-backward
8446   'gnus-summary-tick-article-backward)
8447 (make-obsolete 'gnus-summary-mark-as-unread-backward
8448                'gnus-summary-tick-article-backward)
8449 (defun gnus-summary-tick-article-backward (n)
8450   "Tick N articles backwards.
8451 The difference between N and the number of articles ticked is returned."
8452   (interactive "p")
8453   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8454
8455 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8456 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8457 (defun gnus-summary-tick-article (&optional article clear-mark)
8458   "Mark current article as unread.
8459 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8460 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8461   (interactive)
8462   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8463                                        gnus-ticked-mark)))
8464
8465 (defun gnus-summary-mark-as-read-forward (n)
8466   "Mark N articles as read forwards.
8467 If N is negative, mark backwards instead.
8468 The difference between N and the actual number of articles marked is
8469 returned."
8470   (interactive "p")
8471   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8472
8473 (defun gnus-summary-mark-as-read-backward (n)
8474   "Mark the N articles as read backwards.
8475 The difference between N and the actual number of articles marked is
8476 returned."
8477   (interactive "p")
8478   (gnus-summary-mark-forward
8479    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8480
8481 (defun gnus-summary-mark-as-read (&optional article mark)
8482   "Mark current article as read.
8483 ARTICLE specifies the article to be marked as read.
8484 MARK specifies a string to be inserted at the beginning of the line."
8485   (gnus-summary-mark-article article mark))
8486
8487 (defun gnus-summary-clear-mark-forward (n)
8488   "Clear marks from N articles forward.
8489 If N is negative, clear backward instead.
8490 The difference between N and the number of marks cleared is returned."
8491   (interactive "p")
8492   (gnus-summary-mark-forward n gnus-unread-mark))
8493
8494 (defun gnus-summary-clear-mark-backward (n)
8495   "Clear marks from N articles backward.
8496 The difference between N and the number of marks cleared is returned."
8497   (interactive "p")
8498   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8499
8500 (defun gnus-summary-mark-unread-as-read ()
8501   "Intended to be used by `gnus-summary-mark-article-hook'."
8502   (when (memq gnus-current-article gnus-newsgroup-unreads)
8503     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8504
8505 (defun gnus-summary-mark-read-and-unread-as-read ()
8506   "Intended to be used by `gnus-summary-mark-article-hook'."
8507   (let ((mark (gnus-summary-article-mark)))
8508     (when (or (gnus-unread-mark-p mark)
8509               (gnus-read-mark-p mark))
8510       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8511
8512 (defun gnus-summary-mark-region-as-read (point mark all)
8513   "Mark all unread articles between point and mark as read.
8514 If given a prefix, mark all articles between point and mark as read,
8515 even ticked and dormant ones."
8516   (interactive "r\nP")
8517   (save-excursion
8518     (let (article)
8519       (goto-char point)
8520       (beginning-of-line)
8521       (while (and
8522               (< (point) mark)
8523               (progn
8524                 (when (or all
8525                           (memq (setq article (gnus-summary-article-number))
8526                                 gnus-newsgroup-unreads))
8527                   (gnus-summary-mark-article article gnus-del-mark))
8528                 t)
8529               (gnus-summary-find-next))))))
8530
8531 (defun gnus-summary-mark-below (score mark)
8532   "Mark articles with score less than SCORE with MARK."
8533   (interactive "P\ncMark: ")
8534   (setq score (if score
8535                   (prefix-numeric-value score)
8536                 (or gnus-summary-default-score 0)))
8537   (save-excursion
8538     (set-buffer gnus-summary-buffer)
8539     (goto-char (point-min))
8540     (while
8541         (progn
8542           (and (< (gnus-summary-article-score) score)
8543                (gnus-summary-mark-article nil mark))
8544           (gnus-summary-find-next)))))
8545
8546 (defun gnus-summary-kill-below (&optional score)
8547   "Mark articles with score below SCORE as read."
8548   (interactive "P")
8549   (gnus-summary-mark-below score gnus-killed-mark))
8550
8551 (defun gnus-summary-clear-above (&optional score)
8552   "Clear all marks from articles with score above SCORE."
8553   (interactive "P")
8554   (gnus-summary-mark-above score gnus-unread-mark))
8555
8556 (defun gnus-summary-tick-above (&optional score)
8557   "Tick all articles with score above SCORE."
8558   (interactive "P")
8559   (gnus-summary-mark-above score gnus-ticked-mark))
8560
8561 (defun gnus-summary-mark-above (score mark)
8562   "Mark articles with score over SCORE with MARK."
8563   (interactive "P\ncMark: ")
8564   (setq score (if score
8565                   (prefix-numeric-value score)
8566                 (or gnus-summary-default-score 0)))
8567   (save-excursion
8568     (set-buffer gnus-summary-buffer)
8569     (goto-char (point-min))
8570     (while (and (progn
8571                   (when (> (gnus-summary-article-score) score)
8572                     (gnus-summary-mark-article nil mark))
8573                   t)
8574                 (gnus-summary-find-next)))))
8575
8576 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8577 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8578 (defun gnus-summary-limit-include-expunged (&optional no-error)
8579   "Display all the hidden articles that were expunged for low scores."
8580   (interactive)
8581   (let ((buffer-read-only nil))
8582     (let ((scored gnus-newsgroup-scored)
8583           headers h)
8584       (while scored
8585         (unless (gnus-summary-goto-subject (caar scored))
8586           (and (setq h (gnus-summary-article-header (caar scored)))
8587                (< (cdar scored) gnus-summary-expunge-below)
8588                (push h headers)))
8589         (setq scored (cdr scored)))
8590       (if (not headers)
8591           (when (not no-error)
8592             (error "No expunged articles hidden"))
8593         (goto-char (point-min))
8594         (gnus-summary-prepare-unthreaded (nreverse headers))
8595         (goto-char (point-min))
8596         (gnus-summary-position-point)
8597         t))))
8598
8599 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8600   "Mark all unread articles in this newsgroup as read.
8601 If prefix argument ALL is non-nil, ticked and dormant articles will
8602 also be marked as read.
8603 If QUIETLY is non-nil, no questions will be asked.
8604 If TO-HERE is non-nil, it should be a point in the buffer.  All
8605 articles before this point will be marked as read.
8606 Note that this function will only catch up the unread article
8607 in the current summary buffer limitation.
8608 The number of articles marked as read is returned."
8609   (interactive "P")
8610   (prog1
8611       (save-excursion
8612         (when (or quietly
8613                   (not gnus-interactive-catchup) ;Without confirmation?
8614                   gnus-expert-user
8615                   (gnus-y-or-n-p
8616                    (if all
8617                        "Mark absolutely all articles as read? "
8618                      "Mark all unread articles as read? ")))
8619           (if (and not-mark
8620                    (not gnus-newsgroup-adaptive)
8621                    (not gnus-newsgroup-auto-expire)
8622                    (not gnus-suppress-duplicates)
8623                    (or (not gnus-use-cache)
8624                        (eq gnus-use-cache 'passive)))
8625               (progn
8626                 (when all
8627                   (setq gnus-newsgroup-marked nil
8628                         gnus-newsgroup-dormant nil))
8629                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8630             ;; We actually mark all articles as canceled, which we
8631             ;; have to do when using auto-expiry or adaptive scoring.
8632             (gnus-summary-show-all-threads)
8633             (when (gnus-summary-first-subject (not all) t)
8634               (while (and
8635                       (if to-here (< (point) to-here) t)
8636                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8637                       (gnus-summary-find-next (not all) nil nil t))))
8638             (gnus-set-mode-line 'summary))
8639           t))
8640     (gnus-summary-position-point)))
8641
8642 (defun gnus-summary-catchup-to-here (&optional all)
8643   "Mark all unticked articles before the current one as read.
8644 If ALL is non-nil, also mark ticked and dormant articles as read."
8645   (interactive "P")
8646   (save-excursion
8647     (gnus-save-hidden-threads
8648       (let ((beg (point)))
8649         ;; We check that there are unread articles.
8650         (when (or all (gnus-summary-find-prev))
8651           (gnus-summary-catchup all t beg)))))
8652   (gnus-summary-position-point))
8653
8654 (defun gnus-summary-catchup-all (&optional quietly)
8655   "Mark all articles in this newsgroup as read."
8656   (interactive "P")
8657   (gnus-summary-catchup t quietly))
8658
8659 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8660   "Mark all unread articles in this group as read, then exit.
8661 If prefix argument ALL is non-nil, all articles are marked as read."
8662   (interactive "P")
8663   (when (gnus-summary-catchup all quietly nil 'fast)
8664     ;; Select next newsgroup or exit.
8665     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8666              (eq gnus-auto-select-next 'quietly))
8667         (gnus-summary-next-group nil)
8668       (gnus-summary-exit))))
8669
8670 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8671   "Mark all articles in this newsgroup as read, and then exit."
8672   (interactive "P")
8673   (gnus-summary-catchup-and-exit t quietly))
8674
8675 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8676   "Mark all articles in this group as read and select the next group.
8677 If given a prefix, mark all articles, unread as well as ticked, as
8678 read."
8679   (interactive "P")
8680   (save-excursion
8681     (gnus-summary-catchup all))
8682   (gnus-summary-next-group))
8683
8684 ;;;
8685 ;;; with article
8686 ;;;
8687
8688 (defmacro gnus-with-article (article &rest forms)
8689   "Select ARTICLE and perform FORMS in the original article buffer.
8690 Then replace the article with the result."
8691   `(progn
8692      ;; We don't want the article to be marked as read.
8693      (let (gnus-mark-article-hook)
8694        (gnus-summary-select-article t t nil ,article))
8695      (set-buffer gnus-original-article-buffer)
8696      ,@forms
8697      (if (not (gnus-check-backend-function
8698                'request-replace-article (car gnus-article-current)))
8699          (gnus-message 5 "Read-only group; not replacing")
8700        (unless (gnus-request-replace-article
8701                 ,article (car gnus-article-current)
8702                 (current-buffer) t)
8703          (error "Couldn't replace article")))
8704      ;; The cache and backlog have to be flushed somewhat.
8705      (when gnus-keep-backlog
8706        (gnus-backlog-remove-article
8707         (car gnus-article-current) (cdr gnus-article-current)))
8708      (when gnus-use-cache
8709        (gnus-cache-update-article
8710         (car gnus-article-current) (cdr gnus-article-current)))))
8711
8712 (put 'gnus-with-article 'lisp-indent-function 1)
8713 (put 'gnus-with-article 'edebug-form-spec '(form body))
8714
8715 ;; Thread-based commands.
8716
8717 (defun gnus-summary-articles-in-thread (&optional article)
8718   "Return a list of all articles in the current thread.
8719 If ARTICLE is non-nil, return all articles in the thread that starts
8720 with that article."
8721   (let* ((article (or article (gnus-summary-article-number)))
8722          (data (gnus-data-find-list article))
8723          (top-level (gnus-data-level (car data)))
8724          (top-subject
8725           (cond ((null gnus-thread-operation-ignore-subject)
8726                  (gnus-simplify-subject-re
8727                   (mail-header-subject (gnus-data-header (car data)))))
8728                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8729                  (gnus-simplify-subject-fuzzy
8730                   (mail-header-subject (gnus-data-header (car data)))))
8731                 (t nil)))
8732          (end-point (save-excursion
8733                       (if (gnus-summary-go-to-next-thread)
8734                           (point) (point-max))))
8735          articles)
8736     (while (and data
8737                 (< (gnus-data-pos (car data)) end-point))
8738       (when (or (not top-subject)
8739                 (string= top-subject
8740                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8741                              (gnus-simplify-subject-fuzzy
8742                               (mail-header-subject
8743                                (gnus-data-header (car data))))
8744                            (gnus-simplify-subject-re
8745                             (mail-header-subject
8746                              (gnus-data-header (car data)))))))
8747         (push (gnus-data-number (car data)) articles))
8748       (unless (and (setq data (cdr data))
8749                    (> (gnus-data-level (car data)) top-level))
8750         (setq data nil)))
8751     ;; Return the list of articles.
8752     (nreverse articles)))
8753
8754 (defun gnus-summary-rethread-current ()
8755   "Rethread the thread the current article is part of."
8756   (interactive)
8757   (let* ((gnus-show-threads t)
8758          (article (gnus-summary-article-number))
8759          (id (mail-header-id (gnus-summary-article-header)))
8760          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8761     (unless id
8762       (error "No article on the current line"))
8763     (gnus-rebuild-thread id)
8764     (gnus-summary-goto-subject article)))
8765
8766 (defun gnus-summary-reparent-thread ()
8767   "Make the current article child of the marked (or previous) article.
8768
8769 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8770 is non-nil or the Subject: of both articles are the same."
8771   (interactive)
8772   (unless (not (gnus-group-read-only-p))
8773     (error "The current newsgroup does not support article editing"))
8774   (unless (<= (length gnus-newsgroup-processable) 1)
8775     (error "No more than one article may be marked"))
8776   (save-window-excursion
8777     (let ((gnus-article-buffer " *reparent*")
8778           (current-article (gnus-summary-article-number))
8779           ;; First grab the marked article, otherwise one line up.
8780           (parent-article (if (not (null gnus-newsgroup-processable))
8781                               (car gnus-newsgroup-processable)
8782                             (save-excursion
8783                               (if (eq (forward-line -1) 0)
8784                                   (gnus-summary-article-number)
8785                                 (error "Beginning of summary buffer"))))))
8786       (unless (not (eq current-article parent-article))
8787         (error "An article may not be self-referential"))
8788       (let ((message-id (mail-header-id
8789                          (gnus-summary-article-header parent-article))))
8790         (unless (and message-id (not (equal message-id "")))
8791           (error "No message-id in desired parent"))
8792         (gnus-with-article current-article
8793           (save-restriction
8794             (goto-char (point-min))
8795             (message-narrow-to-head)
8796             (if (re-search-forward "^References: " nil t)
8797                 (progn
8798                   (re-search-forward "^[^ \t]" nil t)
8799                   (forward-line -1)
8800                   (end-of-line)
8801                   (insert " " message-id))
8802               (insert "References: " message-id "\n"))))
8803         (set-buffer gnus-summary-buffer)
8804         (gnus-summary-unmark-all-processable)
8805         (gnus-summary-update-article current-article)
8806         (gnus-summary-rethread-current)
8807         (gnus-message 3 "Article %d is now the child of article %d"
8808                       current-article parent-article)))))
8809
8810 (defun gnus-summary-toggle-threads (&optional arg)
8811   "Toggle showing conversation threads.
8812 If ARG is positive number, turn showing conversation threads on."
8813   (interactive "P")
8814   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8815     (setq gnus-show-threads
8816           (if (null arg) (not gnus-show-threads)
8817             (> (prefix-numeric-value arg) 0)))
8818     (gnus-summary-prepare)
8819     (gnus-summary-goto-subject current)
8820     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8821     (gnus-summary-position-point)))
8822
8823 (defun gnus-summary-show-all-threads ()
8824   "Show all threads."
8825   (interactive)
8826   (save-excursion
8827     (let ((buffer-read-only nil))
8828       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8829   (gnus-summary-position-point))
8830
8831 (defun gnus-summary-show-thread ()
8832   "Show thread subtrees.
8833 Returns nil if no thread was there to be shown."
8834   (interactive)
8835   (let ((buffer-read-only nil)
8836         (orig (point))
8837         ;; first goto end then to beg, to have point at beg after let
8838         (end (progn (end-of-line) (point)))
8839         (beg (progn (beginning-of-line) (point))))
8840     (prog1
8841         ;; Any hidden lines here?
8842         (search-forward "\r" end t)
8843       (subst-char-in-region beg end ?\^M ?\n t)
8844       (goto-char orig)
8845       (gnus-summary-position-point))))
8846
8847 (defun gnus-summary-hide-all-threads ()
8848   "Hide all thread subtrees."
8849   (interactive)
8850   (save-excursion
8851     (goto-char (point-min))
8852     (gnus-summary-hide-thread)
8853     (while (zerop (gnus-summary-next-thread 1 t))
8854       (gnus-summary-hide-thread)))
8855   (gnus-summary-position-point))
8856
8857 (defun gnus-summary-hide-thread ()
8858   "Hide thread subtrees.
8859 Returns nil if no threads were there to be hidden."
8860   (interactive)
8861   (let ((buffer-read-only nil)
8862         (start (point))
8863         (article (gnus-summary-article-number)))
8864     (goto-char start)
8865     ;; Go forward until either the buffer ends or the subthread
8866     ;; ends.
8867     (when (and (not (eobp))
8868                (or (zerop (gnus-summary-next-thread 1 t))
8869                    (goto-char (point-max))))
8870       (prog1
8871           (if (and (> (point) start)
8872                    (search-backward "\n" start t))
8873               (progn
8874                 (subst-char-in-region start (point) ?\n ?\^M)
8875                 (gnus-summary-goto-subject article))
8876             (goto-char start)
8877             nil)))))
8878
8879 (defun gnus-summary-go-to-next-thread (&optional previous)
8880   "Go to the same level (or less) next thread.
8881 If PREVIOUS is non-nil, go to previous thread instead.
8882 Return the article number moved to, or nil if moving was impossible."
8883   (let ((level (gnus-summary-thread-level))
8884         (way (if previous -1 1))
8885         (beg (point)))
8886     (forward-line way)
8887     (while (and (not (eobp))
8888                 (< level (gnus-summary-thread-level)))
8889       (forward-line way))
8890     (if (eobp)
8891         (progn
8892           (goto-char beg)
8893           nil)
8894       (setq beg (point))
8895       (prog1
8896           (gnus-summary-article-number)
8897         (goto-char beg)))))
8898
8899 (defun gnus-summary-next-thread (n &optional silent)
8900   "Go to the same level next N'th thread.
8901 If N is negative, search backward instead.
8902 Returns the difference between N and the number of skips actually
8903 done.
8904
8905 If SILENT, don't output messages."
8906   (interactive "p")
8907   (let ((backward (< n 0))
8908         (n (abs n)))
8909     (while (and (> n 0)
8910                 (gnus-summary-go-to-next-thread backward))
8911       (decf n))
8912     (unless silent
8913       (gnus-summary-position-point))
8914     (when (and (not silent) (/= 0 n))
8915       (gnus-message 7 "No more threads"))
8916     n))
8917
8918 (defun gnus-summary-prev-thread (n)
8919   "Go to the same level previous N'th thread.
8920 Returns the difference between N and the number of skips actually
8921 done."
8922   (interactive "p")
8923   (gnus-summary-next-thread (- n)))
8924
8925 (defun gnus-summary-go-down-thread ()
8926   "Go down one level in the current thread."
8927   (let ((children (gnus-summary-article-children)))
8928     (when children
8929       (gnus-summary-goto-subject (car children)))))
8930
8931 (defun gnus-summary-go-up-thread ()
8932   "Go up one level in the current thread."
8933   (let ((parent (gnus-summary-article-parent)))
8934     (when parent
8935       (gnus-summary-goto-subject parent))))
8936
8937 (defun gnus-summary-down-thread (n)
8938   "Go down thread N steps.
8939 If N is negative, go up instead.
8940 Returns the difference between N and how many steps down that were
8941 taken."
8942   (interactive "p")
8943   (let ((up (< n 0))
8944         (n (abs n)))
8945     (while (and (> n 0)
8946                 (if up (gnus-summary-go-up-thread)
8947                   (gnus-summary-go-down-thread)))
8948       (setq n (1- n)))
8949     (gnus-summary-position-point)
8950     (when (/= 0 n)
8951       (gnus-message 7 "Can't go further"))
8952     n))
8953
8954 (defun gnus-summary-up-thread (n)
8955   "Go up thread N steps.
8956 If N is negative, go up instead.
8957 Returns the difference between N and how many steps down that were
8958 taken."
8959   (interactive "p")
8960   (gnus-summary-down-thread (- n)))
8961
8962 (defun gnus-summary-top-thread ()
8963   "Go to the top of the thread."
8964   (interactive)
8965   (while (gnus-summary-go-up-thread))
8966   (gnus-summary-article-number))
8967
8968 (defun gnus-summary-kill-thread (&optional unmark)
8969   "Mark articles under current thread as read.
8970 If the prefix argument is positive, remove any kinds of marks.
8971 If the prefix argument is negative, tick articles instead."
8972   (interactive "P")
8973   (when unmark
8974     (setq unmark (prefix-numeric-value unmark)))
8975   (let ((articles (gnus-summary-articles-in-thread)))
8976     (save-excursion
8977       ;; Expand the thread.
8978       (gnus-summary-show-thread)
8979       ;; Mark all the articles.
8980       (while articles
8981         (gnus-summary-goto-subject (car articles))
8982         (cond ((null unmark)
8983                (gnus-summary-mark-article-as-read gnus-killed-mark))
8984               ((> unmark 0)
8985                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8986               (t
8987                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8988         (setq articles (cdr articles))))
8989     ;; Hide killed subtrees.
8990     (and (null unmark)
8991          gnus-thread-hide-killed
8992          (gnus-summary-hide-thread))
8993     ;; If marked as read, go to next unread subject.
8994     (when (null unmark)
8995       ;; Go to next unread subject.
8996       (gnus-summary-next-subject 1 t)))
8997   (gnus-set-mode-line 'summary))
8998
8999 ;; Summary sorting commands
9000
9001 (defun gnus-summary-sort-by-number (&optional reverse)
9002   "Sort the summary buffer by article number.
9003 Argument REVERSE means reverse order."
9004   (interactive "P")
9005   (gnus-summary-sort 'number reverse))
9006
9007 (defun gnus-summary-sort-by-author (&optional reverse)
9008   "Sort the summary buffer by author name alphabetically.
9009 If `case-fold-search' is non-nil, case of letters is ignored.
9010 Argument REVERSE means reverse order."
9011   (interactive "P")
9012   (gnus-summary-sort 'author reverse))
9013
9014 (defun gnus-summary-sort-by-subject (&optional reverse)
9015   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9016 If `case-fold-search' is non-nil, case of letters is ignored.
9017 Argument REVERSE means reverse order."
9018   (interactive "P")
9019   (gnus-summary-sort 'subject reverse))
9020
9021 (defun gnus-summary-sort-by-date (&optional reverse)
9022   "Sort the summary buffer by date.
9023 Argument REVERSE means reverse order."
9024   (interactive "P")
9025   (gnus-summary-sort 'date reverse))
9026
9027 (defun gnus-summary-sort-by-score (&optional reverse)
9028   "Sort the summary buffer by score.
9029 Argument REVERSE means reverse order."
9030   (interactive "P")
9031   (gnus-summary-sort 'score reverse))
9032
9033 (defun gnus-summary-sort-by-lines (&optional reverse)
9034   "Sort the summary buffer by the number of lines.
9035 Argument REVERSE means reverse order."
9036   (interactive "P")
9037   (gnus-summary-sort 'lines reverse))
9038
9039 (defun gnus-summary-sort-by-chars (&optional reverse)
9040   "Sort the summary buffer by article length.
9041 Argument REVERSE means reverse order."
9042   (interactive "P")
9043   (gnus-summary-sort 'chars reverse))
9044
9045 (defun gnus-summary-sort (predicate reverse)
9046   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9047   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9048          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9049          (gnus-thread-sort-functions
9050           (if (not reverse)
9051               thread
9052             `(lambda (t1 t2)
9053                (,thread t2 t1))))
9054          (gnus-sort-gathered-threads-function
9055           gnus-thread-sort-functions)
9056          (gnus-article-sort-functions
9057           (if (not reverse)
9058               article
9059             `(lambda (t1 t2)
9060                (,article t2 t1))))
9061          (buffer-read-only)
9062          (gnus-summary-prepare-hook nil))
9063     ;; We do the sorting by regenerating the threads.
9064     (gnus-summary-prepare)
9065     ;; Hide subthreads if needed.
9066     (when (and gnus-show-threads gnus-thread-hide-subtree)
9067       (gnus-summary-hide-all-threads))))
9068
9069 ;; Summary saving commands.
9070
9071 (defun gnus-summary-save-article (&optional n not-saved)
9072   "Save the current article using the default saver function.
9073 If N is a positive number, save the N next articles.
9074 If N is a negative number, save the N previous articles.
9075 If N is nil and any articles have been marked with the process mark,
9076 save those articles instead.
9077 The variable `gnus-default-article-saver' specifies the saver function."
9078   (interactive "P")
9079   (let* ((articles (gnus-summary-work-articles n))
9080          (save-buffer (save-excursion
9081                         (nnheader-set-temp-buffer " *Gnus Save*")))
9082          (num (length articles))
9083          header file)
9084     (dolist (article articles)
9085       (setq header (gnus-summary-article-header article))
9086       (if (not (vectorp header))
9087           ;; This is a pseudo-article.
9088           (if (assq 'name header)
9089               (gnus-copy-file (cdr (assq 'name header)))
9090             (gnus-message 1 "Article %d is unsaveable" article))
9091         ;; This is a real article.
9092         (save-window-excursion
9093           (gnus-summary-select-article t nil nil article))
9094         (save-excursion
9095           (set-buffer save-buffer)
9096           (erase-buffer)
9097           (insert-buffer-substring gnus-original-article-buffer))
9098         (setq file (gnus-article-save save-buffer file num))
9099         (gnus-summary-remove-process-mark article)
9100         (unless not-saved
9101           (gnus-summary-set-saved-mark article))))
9102     (gnus-kill-buffer save-buffer)
9103     (gnus-summary-position-point)
9104     (gnus-set-mode-line 'summary)
9105     n))
9106
9107 (defun gnus-summary-pipe-output (&optional arg)
9108   "Pipe the current article to a subprocess.
9109 If N is a positive number, pipe the N next articles.
9110 If N is a negative number, pipe the N previous articles.
9111 If N is nil and any articles have been marked with the process mark,
9112 pipe those articles instead."
9113   (interactive "P")
9114   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9115     (gnus-summary-save-article arg t))
9116   (gnus-configure-windows 'pipe))
9117
9118 (defun gnus-summary-save-article-mail (&optional arg)
9119   "Append the current article to an mail file.
9120 If N is a positive number, save the N next articles.
9121 If N is a negative number, save the N previous articles.
9122 If N is nil and any articles have been marked with the process mark,
9123 save those articles instead."
9124   (interactive "P")
9125   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9126     (gnus-summary-save-article arg)))
9127
9128 (defun gnus-summary-save-article-rmail (&optional arg)
9129   "Append the current article to an rmail file.
9130 If N is a positive number, save the N next articles.
9131 If N is a negative number, save the N previous articles.
9132 If N is nil and any articles have been marked with the process mark,
9133 save those articles instead."
9134   (interactive "P")
9135   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9136     (gnus-summary-save-article arg)))
9137
9138 (defun gnus-summary-save-article-file (&optional arg)
9139   "Append the current article to a file.
9140 If N is a positive number, save the N next articles.
9141 If N is a negative number, save the N previous articles.
9142 If N is nil and any articles have been marked with the process mark,
9143 save those articles instead."
9144   (interactive "P")
9145   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9146     (gnus-summary-save-article arg)))
9147
9148 (defun gnus-summary-write-article-file (&optional arg)
9149   "Write the current article to a file, deleting the previous file.
9150 If N is a positive number, save the N next articles.
9151 If N is a negative number, save the N previous articles.
9152 If N is nil and any articles have been marked with the process mark,
9153 save those articles instead."
9154   (interactive "P")
9155   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9156     (gnus-summary-save-article arg)))
9157
9158 (defun gnus-summary-save-article-body-file (&optional arg)
9159   "Append the current article body to a file.
9160 If N is a positive number, save the N next articles.
9161 If N is a negative number, save the N previous articles.
9162 If N is nil and any articles have been marked with the process mark,
9163 save those articles instead."
9164   (interactive "P")
9165   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9166     (gnus-summary-save-article arg)))
9167
9168 (defun gnus-summary-pipe-message (program)
9169   "Pipe the current article through PROGRAM."
9170   (interactive "sProgram: ")
9171   (gnus-summary-select-article)
9172   (let ((mail-header-separator ""))
9173     (gnus-eval-in-buffer-window gnus-article-buffer
9174       (save-restriction
9175         (widen)
9176         (let ((start (window-start))
9177               buffer-read-only)
9178           (message-pipe-buffer-body program)
9179           (set-window-start (get-buffer-window (current-buffer)) start))))))
9180
9181 (defun gnus-get-split-value (methods)
9182   "Return a value based on the split METHODS."
9183   (let (split-name method result match)
9184     (when methods
9185       (save-excursion
9186         (set-buffer gnus-original-article-buffer)
9187         (save-restriction
9188           (nnheader-narrow-to-headers)
9189           (while (and methods (not split-name))
9190             (goto-char (point-min))
9191             (setq method (pop methods))
9192             (setq match (car method))
9193             (when (cond
9194                    ((stringp match)
9195                     ;; Regular expression.
9196                     (ignore-errors
9197                       (re-search-forward match nil t)))
9198                    ((gnus-functionp match)
9199                     ;; Function.
9200                     (save-restriction
9201                       (widen)
9202                       (setq result (funcall match gnus-newsgroup-name))))
9203                    ((consp match)
9204                     ;; Form.
9205                     (save-restriction
9206                       (widen)
9207                       (setq result (eval match)))))
9208               (setq split-name (cdr method))
9209               (cond ((stringp result)
9210                      (push (expand-file-name
9211                             result gnus-article-save-directory)
9212                            split-name))
9213                     ((consp result)
9214                      (setq split-name (append result split-name)))))))))
9215     (nreverse split-name)))
9216
9217 (defun gnus-valid-move-group-p (group)
9218   (and (boundp group)
9219        (symbol-name group)
9220        (symbol-value group)
9221        (gnus-get-function (gnus-find-method-for-group
9222                            (symbol-name group)) 'request-accept-article t)))
9223
9224 (defun gnus-read-move-group-name (prompt default articles prefix)
9225   "Read a group name."
9226   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9227          (minibuffer-confirm-incomplete nil) ; XEmacs
9228          (prom
9229           (format "%s %s to:"
9230                   prompt
9231                   (if (> (length articles) 1)
9232                       (format "these %d articles" (length articles))
9233                     "this article")))
9234          (to-newsgroup
9235           (cond
9236            ((null split-name)
9237             (gnus-completing-read default prom
9238                                   gnus-active-hashtb
9239                                   'gnus-valid-move-group-p
9240                                   nil prefix
9241                                   'gnus-group-history))
9242            ((= 1 (length split-name))
9243             (gnus-completing-read (car split-name) prom
9244                                   gnus-active-hashtb
9245                                   'gnus-valid-move-group-p
9246                                   nil nil
9247                                   'gnus-group-history))
9248            (t
9249             (gnus-completing-read nil prom
9250                                   (mapcar (lambda (el) (list el))
9251                                           (nreverse split-name))
9252                                   nil nil nil
9253                                   'gnus-group-history))))
9254          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9255     (when to-newsgroup
9256       (if (or (string= to-newsgroup "")
9257               (string= to-newsgroup prefix))
9258           (setq to-newsgroup default))
9259       (unless to-newsgroup
9260         (error "No group name entered"))
9261       (or (gnus-active to-newsgroup)
9262           (gnus-activate-group to-newsgroup nil nil to-method)
9263           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9264                                      to-newsgroup))
9265               (or (and (gnus-request-create-group to-newsgroup to-method)
9266                        (gnus-activate-group
9267                         to-newsgroup nil nil to-method)
9268                        (gnus-subscribe-group to-newsgroup))
9269                   (error "Couldn't create group %s" to-newsgroup)))
9270           (error "No such group: %s" to-newsgroup)))
9271     to-newsgroup))
9272
9273 (defun gnus-summary-save-parts (type dir n &optional reverse)
9274   "Save parts matching TYPE to DIR.
9275 If REVERSE, save parts that do not match TYPE."
9276   (interactive
9277    (list (read-string "Save parts of type: " 
9278                       (or (car gnus-summary-save-parts-type-history)
9279                           gnus-summary-save-parts-default-mime)
9280                       'gnus-summary-save-parts-type-history)
9281          (setq gnus-summary-save-parts-last-directory
9282                (read-file-name "Save to directory: " 
9283                                gnus-summary-save-parts-last-directory
9284                                nil t))
9285          current-prefix-arg))
9286   (gnus-summary-iterate n
9287     (let ((gnus-display-mime-function nil)
9288           (gnus-inhibit-treatment t))
9289       (gnus-summary-select-article))
9290     (save-excursion
9291       (set-buffer gnus-article-buffer)
9292       (let ((handles (or gnus-article-mime-handles
9293                          (mm-dissect-buffer) (mm-uu-dissect))))
9294         (when handles
9295           (gnus-summary-save-parts-1 type dir handles reverse)
9296           (unless gnus-article-mime-handles ;; Don't destroy this case.
9297             (mm-destroy-parts handles)))))))
9298
9299 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9300   (if (stringp (car handle))
9301       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9302               (cdr handle))
9303     (when (if reverse
9304               (not (string-match type (mm-handle-media-type handle)))
9305             (string-match type (mm-handle-media-type handle)))
9306       (let ((file (expand-file-name
9307                    (file-name-nondirectory
9308                     (or
9309                      (mail-content-type-get
9310                       (mm-handle-disposition handle) 'filename)
9311                      (concat gnus-newsgroup-name
9312                              "." (number-to-string
9313                                   (cdr gnus-article-current)))))
9314                    dir)))
9315         (unless (file-exists-p file)
9316           (mm-save-part-to-file handle file))))))
9317
9318 ;; Summary extract commands
9319
9320 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9321   (let ((buffer-read-only nil)
9322         (article (gnus-summary-article-number))
9323         after-article b e)
9324     (unless (gnus-summary-goto-subject article)
9325       (error "No such article: %d" article))
9326     (gnus-summary-position-point)
9327     ;; If all commands are to be bunched up on one line, we collect
9328     ;; them here.
9329     (unless gnus-view-pseudos-separately
9330       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9331             files action)
9332         (while ps
9333           (setq action (cdr (assq 'action (car ps))))
9334           (setq files (list (cdr (assq 'name (car ps)))))
9335           (while (and ps (cdr ps)
9336                       (string= (or action "1")
9337                                (or (cdr (assq 'action (cadr ps))) "2")))
9338             (push (cdr (assq 'name (cadr ps))) files)
9339             (setcdr ps (cddr ps)))
9340           (when files
9341             (when (not (string-match "%s" action))
9342               (push " " files))
9343             (push " " files)
9344             (when (assq 'execute (car ps))
9345               (setcdr (assq 'execute (car ps))
9346                       (funcall (if (string-match "%s" action)
9347                                    'format 'concat)
9348                                action
9349                                (mapconcat
9350                                 (lambda (f)
9351                                   (if (equal f " ")
9352                                       f
9353                                     (mm-quote-arg f)))
9354                                 files " ")))))
9355           (setq ps (cdr ps)))))
9356     (if (and gnus-view-pseudos (not not-view))
9357         (while pslist
9358           (when (assq 'execute (car pslist))
9359             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9360                                   (eq gnus-view-pseudos 'not-confirm)))
9361           (setq pslist (cdr pslist)))
9362       (save-excursion
9363         (while pslist
9364           (setq after-article (or (cdr (assq 'article (car pslist)))
9365                                   (gnus-summary-article-number)))
9366           (gnus-summary-goto-subject after-article)
9367           (forward-line 1)
9368           (setq b (point))
9369           (insert "    " (file-name-nondirectory
9370                           (cdr (assq 'name (car pslist))))
9371                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9372           (setq e (point))
9373           (forward-line -1)             ; back to `b'
9374           (gnus-add-text-properties
9375            b (1- e) (list 'gnus-number gnus-reffed-article-number
9376                           gnus-mouse-face-prop gnus-mouse-face))
9377           (gnus-data-enter
9378            after-article gnus-reffed-article-number
9379            gnus-unread-mark b (car pslist) 0 (- e b))
9380           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9381           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9382           (setq pslist (cdr pslist)))))))
9383
9384 (defun gnus-pseudos< (p1 p2)
9385   (let ((c1 (cdr (assq 'action p1)))
9386         (c2 (cdr (assq 'action p2))))
9387     (and c1 c2 (string< c1 c2))))
9388
9389 (defun gnus-request-pseudo-article (props)
9390   (cond ((assq 'execute props)
9391          (gnus-execute-command (cdr (assq 'execute props)))))
9392   (let ((gnus-current-article (gnus-summary-article-number)))
9393     (gnus-run-hooks 'gnus-mark-article-hook)))
9394
9395 (defun gnus-execute-command (command &optional automatic)
9396   (save-excursion
9397     (gnus-article-setup-buffer)
9398     (set-buffer gnus-article-buffer)
9399     (setq buffer-read-only nil)
9400     (let ((command (if automatic command
9401                      (read-string "Command: " (cons command 0)))))
9402       (erase-buffer)
9403       (insert "$ " command "\n\n")
9404       (if gnus-view-pseudo-asynchronously
9405           (start-process "gnus-execute" (current-buffer) shell-file-name
9406                          shell-command-switch command)
9407         (call-process shell-file-name nil t nil
9408                       shell-command-switch command)))))
9409
9410 ;; Summary kill commands.
9411
9412 (defun gnus-summary-edit-global-kill (article)
9413   "Edit the \"global\" kill file."
9414   (interactive (list (gnus-summary-article-number)))
9415   (gnus-group-edit-global-kill article))
9416
9417 (defun gnus-summary-edit-local-kill ()
9418   "Edit a local kill file applied to the current newsgroup."
9419   (interactive)
9420   (setq gnus-current-headers (gnus-summary-article-header))
9421   (gnus-group-edit-local-kill
9422    (gnus-summary-article-number) gnus-newsgroup-name))
9423
9424 ;;; Header reading.
9425
9426 (defun gnus-read-header (id &optional header)
9427   "Read the headers of article ID and enter them into the Gnus system."
9428   (let ((group gnus-newsgroup-name)
9429         (gnus-override-method
9430          (or
9431           gnus-override-method
9432           (and (gnus-news-group-p gnus-newsgroup-name)
9433                (car (gnus-refer-article-methods)))))
9434         where)
9435     ;; First we check to see whether the header in question is already
9436     ;; fetched.
9437     (if (stringp id)
9438         ;; This is a Message-ID.
9439         (setq header (or header (gnus-id-to-header id)))
9440       ;; This is an article number.
9441       (setq header (or header (gnus-summary-article-header id))))
9442     (if (and header
9443              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9444         ;; We have found the header.
9445         header
9446       ;; If this is a sparse article, we have to nix out its
9447       ;; previous entry in the thread hashtb.
9448       (when (and header
9449                  (gnus-summary-article-sparse-p (mail-header-number header)))
9450         (let* ((parent (gnus-parent-id (mail-header-references header)))
9451                (thread (and parent (gnus-id-to-thread parent))))
9452           (when thread
9453             (delq (assq header thread) thread))))
9454       ;; We have to really fetch the header to this article.
9455       (save-excursion
9456         (set-buffer nntp-server-buffer)
9457         (when (setq where (gnus-request-head id group))
9458           (nnheader-fold-continuation-lines)
9459           (goto-char (point-max))
9460           (insert ".\n")
9461           (goto-char (point-min))
9462           (insert "211 ")
9463           (princ (cond
9464                   ((numberp id) id)
9465                   ((cdr where) (cdr where))
9466                   (header (mail-header-number header))
9467                   (t gnus-reffed-article-number))
9468                  (current-buffer))
9469           (insert " Article retrieved.\n"))
9470         (if (or (not where)
9471                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9472             ()                          ; Malformed head.
9473           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9474             (when (and (stringp id)
9475                        (not (string= (gnus-group-real-name group)
9476                                      (car where))))
9477               ;; If we fetched by Message-ID and the article came
9478               ;; from a different group, we fudge some bogus article
9479               ;; numbers for this article.
9480               (mail-header-set-number header gnus-reffed-article-number))
9481             (save-excursion
9482               (set-buffer gnus-summary-buffer)
9483               (decf gnus-reffed-article-number)
9484               (gnus-remove-header (mail-header-number header))
9485               (push header gnus-newsgroup-headers)
9486               (setq gnus-current-headers header)
9487               (push (mail-header-number header) gnus-newsgroup-limit)))
9488           header)))))
9489
9490 (defun gnus-remove-header (number)
9491   "Remove header NUMBER from `gnus-newsgroup-headers'."
9492   (if (and gnus-newsgroup-headers
9493            (= number (mail-header-number (car gnus-newsgroup-headers))))
9494       (pop gnus-newsgroup-headers)
9495     (let ((headers gnus-newsgroup-headers))
9496       (while (and (cdr headers)
9497                   (not (= number (mail-header-number (cadr headers)))))
9498         (pop headers))
9499       (when (cdr headers)
9500         (setcdr headers (cddr headers))))))
9501
9502 ;;;
9503 ;;; summary highlights
9504 ;;;
9505
9506 (defun gnus-highlight-selected-summary ()
9507   "Highlight selected article in summary buffer."
9508   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9509   (when gnus-summary-selected-face
9510     (save-excursion
9511       (let* ((beg (progn (beginning-of-line) (point)))
9512              (end (progn (end-of-line) (point)))
9513              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9514              (from (if (get-text-property beg gnus-mouse-face-prop)
9515                        beg
9516                      (or (next-single-property-change
9517                           beg gnus-mouse-face-prop nil end)
9518                          beg)))
9519              (to
9520               (if (= from end)
9521                   (- from 2)
9522                 (or (next-single-property-change
9523                      from gnus-mouse-face-prop nil end)
9524                     end))))
9525         ;; If no mouse-face prop on line we will have to = from = end,
9526         ;; so we highlight the entire line instead.
9527         (when (= (+ to 2) from)
9528           (setq from beg)
9529           (setq to end))
9530         (if gnus-newsgroup-selected-overlay
9531             ;; Move old overlay.
9532             (gnus-move-overlay
9533              gnus-newsgroup-selected-overlay from to (current-buffer))
9534           ;; Create new overlay.
9535           (gnus-overlay-put
9536            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9537            'face gnus-summary-selected-face))))))
9538
9539 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9540 (defun gnus-summary-highlight-line ()
9541   "Highlight current line according to `gnus-summary-highlight'."
9542   (let* ((list gnus-summary-highlight)
9543          (p (point))
9544          (end (progn (end-of-line) (point)))
9545          ;; now find out where the line starts and leave point there.
9546          (beg (progn (beginning-of-line) (point)))
9547          (article (gnus-summary-article-number))
9548          (score (or (cdr (assq (or article gnus-current-article)
9549                                gnus-newsgroup-scored))
9550                     gnus-summary-default-score 0))
9551          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9552          (inhibit-read-only t))
9553     ;; Eval the cars of the lists until we find a match.
9554     (let ((default gnus-summary-default-score))
9555       (while (and list
9556                   (not (eval (caar list))))
9557         (setq list (cdr list))))
9558     (let ((face (cdar list)))
9559       (unless (eq face (get-text-property beg 'face))
9560         (gnus-put-text-property-excluding-characters-with-faces
9561          beg end 'face
9562          (setq face (if (boundp face) (symbol-value face) face)))
9563         (when gnus-summary-highlight-line-function
9564           (funcall gnus-summary-highlight-line-function article face))))
9565     (goto-char p)))
9566
9567 (defun gnus-update-read-articles (group unread &optional compute)
9568   "Update the list of read articles in GROUP."
9569   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9570          (entry (gnus-gethash group gnus-newsrc-hashtb))
9571          (info (nth 2 entry))
9572          (prev 1)
9573          (unread (sort (copy-sequence unread) '<))
9574          read)
9575     (if (or (not info) (not active))
9576         ;; There is no info on this group if it was, in fact,
9577         ;; killed.  Gnus stores no information on killed groups, so
9578         ;; there's nothing to be done.
9579         ;; One could store the information somewhere temporarily,
9580         ;; perhaps...  Hmmm...
9581         ()
9582       ;; Remove any negative articles numbers.
9583       (while (and unread (< (car unread) 0))
9584         (setq unread (cdr unread)))
9585       ;; Remove any expired article numbers
9586       (while (and unread (< (car unread) (car active)))
9587         (setq unread (cdr unread)))
9588       ;; Compute the ranges of read articles by looking at the list of
9589       ;; unread articles.
9590       (while unread
9591         (when (/= (car unread) prev)
9592           (push (if (= prev (1- (car unread))) prev
9593                   (cons prev (1- (car unread))))
9594                 read))
9595         (setq prev (1+ (car unread)))
9596         (setq unread (cdr unread)))
9597       (when (<= prev (cdr active))
9598         (push (cons prev (cdr active)) read))
9599       (setq read (if (> (length read) 1) (nreverse read) read))
9600       (if compute
9601           read
9602         (save-excursion
9603           (let (setmarkundo)
9604             ;; Propagate the read marks to the backend.
9605             (when (gnus-check-backend-function 'request-set-mark group)
9606               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9607                     (add (gnus-remove-from-range read (gnus-info-read info))))
9608                 (when (or add del)
9609                   (unless (gnus-check-group group)
9610                     (error "Can't open server for %s" group))
9611                   (gnus-request-set-mark
9612                    group (delq nil (list (if add (list add 'add '(read)))
9613                                          (if del (list del 'del '(read))))))
9614                   (setq setmarkundo
9615                         `(gnus-request-set-mark
9616                           ,group
9617                           ',(delq nil (list
9618                                        (if del (list del 'add '(read)))
9619                                        (if add (list add 'del '(read))))))))))
9620             (set-buffer gnus-group-buffer)
9621             (gnus-undo-register
9622               `(progn
9623                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9624                  (gnus-info-set-read ',info ',(gnus-info-read info))
9625                  (gnus-get-unread-articles-in-group ',info 
9626                                                     (gnus-active ,group))
9627                  (gnus-group-update-group ,group t)
9628                  ,setmarkundo))))
9629         ;; Enter this list into the group info.
9630         (gnus-info-set-read info read)
9631         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9632         (gnus-get-unread-articles-in-group info (gnus-active group))
9633         t))))
9634
9635 (defun gnus-offer-save-summaries ()
9636   "Offer to save all active summary buffers."
9637   (save-excursion
9638     (let ((buflist (buffer-list))
9639           buffers bufname)
9640       ;; Go through all buffers and find all summaries.
9641       (while buflist
9642         (and (setq bufname (buffer-name (car buflist)))
9643              (string-match "Summary" bufname)
9644              (save-excursion
9645                (set-buffer bufname)
9646                ;; We check that this is, indeed, a summary buffer.
9647                (and (eq major-mode 'gnus-summary-mode)
9648                     ;; Also make sure this isn't bogus.
9649                     gnus-newsgroup-prepared
9650                     ;; Also make sure that this isn't a dead summary buffer.
9651                     (not gnus-dead-summary-mode)))
9652              (push bufname buffers))
9653         (setq buflist (cdr buflist)))
9654       ;; Go through all these summary buffers and offer to save them.
9655       (when buffers
9656         (map-y-or-n-p
9657          "Update summary buffer %s? "
9658          (lambda (buf)
9659            (switch-to-buffer buf)
9660            (gnus-summary-exit))
9661          buffers)))))
9662
9663 (defun gnus-summary-setup-default-charset ()
9664   "Setup newsgroup default charset."
9665   (if (equal gnus-newsgroup-name "nndraft:drafts")
9666       (setq gnus-newsgroup-charset nil)
9667     (let* ((name (and gnus-newsgroup-name
9668                       (gnus-group-real-name gnus-newsgroup-name)))
9669            (ignored-charsets
9670             (or gnus-newsgroup-ephemeral-ignored-charsets
9671                 (append
9672                  (and gnus-newsgroup-name
9673                       (or (gnus-group-find-parameter gnus-newsgroup-name
9674                                                      'ignored-charsets t)
9675                           (let ((alist gnus-group-ignored-charsets-alist)
9676                                 elem (charsets nil))
9677                             (while (setq elem (pop alist))
9678                               (when (and name
9679                                          (string-match (car elem) name))
9680                                 (setq alist nil
9681                                       charsets (cdr elem))))
9682                             charsets)))
9683                  gnus-newsgroup-ignored-charsets))))
9684       (setq gnus-newsgroup-charset
9685             (or gnus-newsgroup-ephemeral-charset
9686                 (and gnus-newsgroup-name
9687                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9688                          (let ((alist gnus-group-charset-alist)
9689                                elem charset)
9690                            (while (setq elem (pop alist))
9691                              (when (and name
9692                                         (string-match (car elem) name))
9693                                (setq alist nil
9694                                      charset (cadr elem))))
9695                            charset)))
9696                 gnus-default-charset))
9697       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9698            ignored-charsets))))
9699
9700 ;;;
9701 ;;; Mime Commands
9702 ;;;
9703
9704 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9705   "Display the current article buffer fully MIME-buttonized.
9706 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9707 treated as multipart/mixed."
9708   (interactive "P")
9709   (require 'gnus-art)
9710   (let ((gnus-unbuttonized-mime-types nil)
9711         (gnus-mime-display-multipart-as-mixed show-all-parts))
9712     (gnus-summary-show-article)))
9713
9714 (defun gnus-summary-repair-multipart (article)
9715   "Add a Content-Type header to a multipart article without one."
9716   (interactive (list (gnus-summary-article-number)))
9717   (gnus-with-article article
9718     (message-narrow-to-head)
9719     (goto-char (point-max))
9720     (widen)
9721     (when (search-forward "\n--" nil t)
9722       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9723         (message-narrow-to-head)
9724         (message-remove-header "Mime-Version")
9725         (message-remove-header "Content-Type")
9726         (goto-char (point-max))
9727         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9728                         separator))
9729         (insert "Mime-Version: 1.0\n")
9730         (widen))))
9731   (let (gnus-mark-article-hook)
9732     (gnus-summary-select-article t t nil article)))
9733
9734 (defun gnus-summary-toggle-display-buttonized ()
9735   "Toggle the buttonizing of the article buffer."
9736   (interactive)
9737   (require 'gnus-art)
9738   (if (setq gnus-inhibit-mime-unbuttonizing
9739             (not gnus-inhibit-mime-unbuttonizing))
9740       (let ((gnus-unbuttonized-mime-types nil))
9741         (gnus-summary-show-article))
9742     (gnus-summary-show-article)))
9743
9744 ;;;
9745 ;;; Generic summary marking commands
9746 ;;;
9747
9748 (defvar gnus-summary-marking-alist
9749   '((read gnus-del-mark "d")
9750     (unread gnus-unread-mark "u")
9751     (ticked gnus-ticked-mark "!")
9752     (dormant gnus-dormant-mark "?")
9753     (expirable gnus-expirable-mark "e"))
9754   "An alist of names/marks/keystrokes.")
9755
9756 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9757 (defvar gnus-summary-mark-map)
9758
9759 (defun gnus-summary-make-all-marking-commands ()
9760   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9761   (dolist (elem gnus-summary-marking-alist)
9762     (apply 'gnus-summary-make-marking-command elem)))
9763
9764 (defun gnus-summary-make-marking-command (name mark keystroke)
9765   (let ((map (make-sparse-keymap)))
9766     (define-key gnus-summary-generic-mark-map keystroke map)
9767     (dolist (lway `((next "next" next nil "n")
9768                     (next-unread "next unread" next t "N")
9769                     (prev "previous" prev nil "p")
9770                     (prev-unread "previous unread" prev t "P")
9771                     (nomove "" nil nil ,keystroke)))
9772       (let ((func (gnus-summary-make-marking-command-1
9773                    mark (car lway) lway name)))
9774         (setq func (eval func))
9775         (define-key map (nth 4 lway) func)))))
9776       
9777 (defun gnus-summary-make-marking-command-1 (mark way lway name)
9778   `(defun ,(intern
9779             (format "gnus-summary-put-mark-as-%s%s"
9780                     name (if (eq way 'nomove)
9781                              ""
9782                            (concat "-" (symbol-name way)))))
9783      (n)
9784      ,(format
9785        "Mark the current article as %s%s.
9786 If N, the prefix, then repeat N times.
9787 If N is negative, move in reverse order.
9788 The difference between N and the actual number of articles marked is
9789 returned."
9790        name (cadr lway))
9791      (interactive "p")
9792      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9793     
9794 (defun gnus-summary-generic-mark (n mark move unread)
9795   "Mark N articles with MARK."
9796   (unless (eq major-mode 'gnus-summary-mode)
9797     (error "This command can only be used in the summary buffer"))
9798   (gnus-summary-show-thread)
9799   (let ((nummove
9800          (cond
9801           ((eq move 'next) 1)
9802           ((eq move 'prev) -1)
9803           (t 0))))
9804     (if (zerop nummove)
9805         (setq n 1)
9806       (when (< n 0)
9807         (setq n (abs n)
9808               nummove (* -1 nummove))))
9809     (while (and (> n 0)
9810                 (gnus-summary-mark-article nil mark)
9811                 (zerop (gnus-summary-next-subject nummove unread t)))
9812       (setq n (1- n)))
9813     (when (/= 0 n)
9814       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9815     (gnus-summary-recenter)
9816     (gnus-summary-position-point)
9817     (gnus-set-mode-line 'summary)
9818     n))
9819
9820 (gnus-summary-make-all-marking-commands)
9821
9822 (gnus-ems-redefine)
9823
9824 (provide 'gnus-sum)
9825
9826 (run-hooks 'gnus-sum-load-hook)
9827
9828 ;;; gnus-sum.el ends here